File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.47: download - view: text, annotated - select for diffs
Tue May 20 11:47:06 2014 UTC (9 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC3, HEAD
- Circumvent lack of scrolling for multi-page PDFs in iFrames on iOS
  by substituting inline display in iframe with a link when viewing on
  mobile device (Main Content area).

    1: # The LearningOnline Network with CAPA
    2: # Wrapper for external and binary files as standalone resources
    3: #
    4: # $Id: lonwrapper.pm,v 1.47 2014/05/20 11:47:06 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: package Apache::lonwrapper;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common);
   34: use Apache::lonenc();
   35: use Apache::lonnet;
   36: use Apache::lonlocal;
   37: use Apache::loncommon();
   38: use Apache::lonhtmlcommon();
   39: use Apache::lonextresedit();
   40: 
   41: # ================================================================ Main Handler
   42: sub wrapper {
   43:     my ($url,$brcrum,$absolute,$is_ext,$is_pdf) = @_;
   44: 
   45:     my $forcereg;
   46:     unless ($env{'form.folderpath'}) {
   47:         $forcereg = 1;
   48:     }
   49: 
   50:     my %lt = &Apache::lonlocal::texthash(
   51:                                           'noif' => 'No iframe support.',
   52:                                           'show' => 'Show content in pop-up window',
   53:                                         );
   54: 
   55:     my $noiframe = &Apache::loncommon::modal_link($url,$lt{'show'},500,400);
   56:     my $args = {'bgcolor' => '#FFFFFF'};
   57:     if ($forcereg) {
   58:         $args->{'force_register'} = $forcereg;
   59:     }
   60:     if (ref($brcrum) eq 'ARRAY') {
   61:         $args->{'bread_crumbs'} = $brcrum;
   62:     }
   63:     if ($absolute) {
   64:         $args->{'use_absolute'} = $absolute; 
   65:     }
   66: 
   67:     my $startpage = &Apache::loncommon::start_page('Menu',undef,$args);
   68:     my $endpage = &Apache::loncommon::end_page();
   69: 
   70:     if ($env{'browser.mobile'}) {
   71:         my $output = $startpage;
   72:         if ($is_pdf) {
   73:             my $title = $env{'form.title'};
   74:             if ($title eq '') {
   75:                 unless ($env{'request.enc'}) {
   76:                     ($title) = ($url =~ m{/([^/]+)$});
   77:                     $title =~ s/(\?[^\?]+)$//;
   78:                 }
   79:             }
   80:             unless ($title eq '') {
   81:                 $output .= $title.'<br />';
   82:             }
   83:             $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
   84:         } else {
   85:             $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
   86:                        '<iframe src="$url" height="100%" width="100%" frameborder="0">'."\n".
   87:                        "$lt{'noif'} $noiframe\n".
   88:                        "</iframe>\n".
   89:                        "</div>\n";
   90:             $output .= '<iframe src="$url" height="100%" width="100%" frameborder="0">'."\n".
   91:                        "$lt{'noif'} $noiframe\n".
   92:                        '</iframe>';
   93:         }
   94:         $output .= $endpage;
   95:         return $output;
   96:     } else {
   97:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
   98:         \$(document).ready( function() {
   99:             \$(window).unbind('resize').resize(function(){
  100:                 var header;
  101:                 var offset = 5;
  102:                 var height = 0;
  103:                 var hdrtop = 0;
  104:                 if (\$('div.LC_head_subbox:first').length) {
  105:                     header = \$('div.LC_head_subbox:first');
  106:                     offset = 9;
  107:                 } else {
  108:                     if (\$('#LC_breadcrumbs').length) {
  109:                         header = \$('#LC_breadcrumbs');
  110:                     }
  111:                 }
  112:                 if (header.length) {
  113:                     height = header.height();
  114:                     hdrtop = header.position().top;
  115:                 }
  116:                 var pos = height + hdrtop + offset;
  117:                 \$('.LC_iframecontainer').css('top', pos);
  118:             });
  119:         });
  120:         window.onload = function(){  \$(window).trigger('resize') };
  121: SCRIPT
  122:         # javascript will position the iframe if window was resized (or zoomed)
  123:         return <<ENDFRAME;
  124:         $startpage
  125:         $script
  126:         <div class="LC_iframecontainer">
  127:             <iframe src="$url">$lt{'noif'} $noiframe</iframe>
  128:         </div>
  129:         $endpage
  130: ENDFRAME
  131:     }
  132: }
  133: 
  134: sub handler {
  135:     my $r=shift;
  136:     &Apache::loncommon::content_type($r,'text/html');
  137:     $r->send_http_header;
  138: 
  139:     return OK if $r->header_only;
  140: 
  141:     my $url = $r->uri;
  142:     my ($is_ext,$brcrum,$absolute,$is_pdf);
  143: 
  144:     for ($url){
  145:         s|^/adm/wrapper||;
  146:         $is_ext = $_ =~ s|^/ext/|http://|;         
  147:         s|http://https://|https://|;
  148:         s|&colon;|:|g;              
  149:     }
  150: 
  151:     if ($url =~ /\.pdf$/i) {
  152:         $is_pdf = 1;
  153:     }
  154:  
  155:     if ($is_ext) {
  156:         my $hostname = $r->hostname();
  157:         my $lonhost = &Apache::lonnet::host_from_dns($hostname);
  158:         if ($lonhost) {
  159:             my $actual = &Apache::lonnet::absolute_url($hostname);
  160:             my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname; 
  161:             unless ($actual eq $expected) {
  162:                 $absolute = $expected;
  163:             }
  164:         }
  165:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  166:             ['forceedit','register','folderpath','symb','idx','title']);
  167:         if (($env{'form.forceedit'}) &&
  168:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  169:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  170:              ($env{'form.symb'} =~ /^uploaded/))) {
  171:             $r->print(
  172:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  173:                                                        $env{'form.symb'},
  174:                                                        $env{'form.idx'}));
  175:             return OK;
  176:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  177:             my $crstype = &Apache::loncommon::course_type();
  178:             my $title = $env{'form.title'};
  179:             if ($title eq '') {
  180:                 $title = &mt('External Resource');
  181:             }
  182:             $brcrum =
  183:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  184:         }
  185:     }
  186: 
  187: #
  188: # Actual URL
  189: #
  190:     if ($url=~/$LONCAPA::assess_re/) {
  191: #
  192: # This is uploaded homework
  193: #
  194:         $env{'request.state'}='uploaded';
  195:         &Apache::lonhomework::renderpage($r,$url);
  196:     } else {
  197: #
  198: # This is not homework
  199: #
  200:         if ($is_ext) {
  201:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  202:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  203:         }
  204: 
  205:         unless ($ENV{'QUERY_STRING'} eq '') {
  206:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  207:         }
  208: 
  209:         # encrypt url if not external
  210:         &Apache::lonenc::check_encrypt(\$url) if $url !~ /^https?\:/ ;
  211: 
  212:         $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf) );
  213: 
  214:     } # not just the menu
  215:     
  216:     return OK;
  217: } # handler
  218: 
  219: 1;
  220: __END__
  221: 
  222: =pod
  223: 
  224: =head1 NAME
  225: 
  226: Apache::lonwrapper - External and binary file management.
  227: 
  228: =head1 SYNOPSIS
  229: 
  230: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  231: 
  232: This is part of the LearningOnline Network with CAPA project
  233: described at http://www.lon-capa.org.
  234: 
  235: =head1 Subroutines
  236: 
  237: =over
  238: 
  239: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf))
  240: 
  241: =over
  242: 
  243: =item $url
  244: 
  245: url to display by including in an iframe within a
  246: LON-CAPA page which has a standard LON-CAPA inline menu.
  247: 
  248: =item $brcrum
  249: 
  250: breadcrumbs for unregistered urls
  251: (i.e., external resources in Supplemental Content).
  252: 
  253: =item $absolute
  254: 
  255: contains protocol (http or https) followed by
  256: the hostname, if menu items in the standard LON-CAPA
  257: interface created by the call to loncommon::start_page()
  258: within &wrapper() need to use absolute URLs rather than
  259: relative URLs.
  260: 
  261: That will be the case where an external resource has been 
  262: served from port 80, when the server customarily serves
  263: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  264: is used to switch requests for external resources 
  265: from https:// to http:// where the the URL of the remote site 
  266: specified in the resource itself is http://.
  267: 
  268: This is done to avoid default mixed content blocking
  269: in Firefox 23 and later, when serving from Apache/SSL.
  270: 
  271: =item $is_ext
  272: 
  273: true if URL is for an external resource.
  274: 
  275: =item $is_pdf
  276: 
  277: true if URL is for a PDF (based on file extension).
  278: 
  279: =back
  280: 
  281: Returns markup for the entire page.
  282: 
  283: =item handler()
  284: 
  285: =back
  286: 
  287: =cut
  288: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>