File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49: download - view: text, annotated - select for diffs
Tue Jun 17 23:22:21 2014 UTC (9 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_X, version_2_11_1, version_2_11_0, HEAD
- On Apache/SSL servers syllabus should always be served by http:// to avoid
  "mixed content" issue when syllabus is an external resource on http:// server).
  - Set $env{'request.use_absolute'} and pass in args to start page so postdata
    is preserved. (bug 6662).
  - Code used for same purpose in rat/lonwrapper.pm rev. 1.44 for external
    resources moved to lonacc.pm to facilitate re-use.
- Detect if syllabus template replaced with uploaded PDF or link to external
  PDF file, and send as arg in call to lonwrapper.pm, to circumvent lack of
  scrolling for multi-page PDFs in iframes on iOS.

    1: # The LearningOnline Network with CAPA
    2: # Wrapper for external and binary files as standalone resources
    3: #
    4: # $Id: lonwrapper.pm,v 1.49 2014/06/17 23:22:21 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,$title) = @_;
   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:             if ($title eq '') {
   74:                 $title = $env{'form.title'};
   75:                 if ($title eq '') {
   76:                     unless ($env{'request.enc'}) {
   77:                         ($title) = ($url =~ m{/([^/]+)$});
   78:                         $title =~ s/(\?[^\?]+)$//;
   79:                     }
   80:                 }
   81:             }
   82:             unless ($title eq '') {
   83:                 $output .= $title.'<br />';
   84:             }
   85:             $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
   86:         } else {
   87:             $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
   88:                        '<iframe src="'.$url.'" height="100%" width="100%" frameborder="0">'."\n".
   89:                        "$lt{'noif'} $noiframe\n".
   90:                        "</iframe>\n".
   91:                        "</div>\n";
   92:         }
   93:         $output .= $endpage;
   94:         return $output;
   95:     } else {
   96:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
   97:         \$(document).ready( function() {
   98:             \$(window).unbind('resize').resize(function(){
   99:                 var header;
  100:                 var offset = 5;
  101:                 var height = 0;
  102:                 var hdrtop = 0;
  103:                 if (\$('div.LC_head_subbox:first').length) {
  104:                     header = \$('div.LC_head_subbox:first');
  105:                     offset = 9;
  106:                 } else {
  107:                     if (\$('#LC_breadcrumbs').length) {
  108:                         header = \$('#LC_breadcrumbs');
  109:                     }
  110:                 }
  111:                 if (header.length) {
  112:                     height = header.height();
  113:                     hdrtop = header.position().top;
  114:                 }
  115:                 var pos = height + hdrtop + offset;
  116:                 \$('.LC_iframecontainer').css('top', pos);
  117:             });
  118:         });
  119:         window.onload = function(){  \$(window).trigger('resize') };
  120: SCRIPT
  121:         # javascript will position the iframe if window was resized (or zoomed)
  122:         return <<ENDFRAME;
  123:         $startpage
  124:         $script
  125:         <div class="LC_iframecontainer">
  126:             <iframe src="$url">$lt{'noif'} $noiframe</iframe>
  127:         </div>
  128:         $endpage
  129: ENDFRAME
  130:     }
  131: }
  132: 
  133: sub handler {
  134:     my $r=shift;
  135:     &Apache::loncommon::content_type($r,'text/html');
  136:     $r->send_http_header;
  137: 
  138:     return OK if $r->header_only;
  139: 
  140:     my $url = $r->uri;
  141:     my ($is_ext,$brcrum,$absolute,$is_pdf);
  142: 
  143:     for ($url){
  144:         s|^/adm/wrapper||;
  145:         $is_ext = $_ =~ s|^/ext/|http://|;         
  146:         s|http://https://|https://|;
  147:         s|&colon;|:|g;              
  148:     }
  149: 
  150:     if ($url =~ /\.pdf$/i) {
  151:         $is_pdf = 1;
  152:     }
  153:  
  154:     if ($is_ext) {
  155:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  156:             ['forceedit','register','folderpath','symb','idx','title']);
  157:         if (($env{'form.forceedit'}) &&
  158:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  159:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  160:              ($env{'form.symb'} =~ /^uploaded/))) {
  161:             $r->print(
  162:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  163:                                                        $env{'form.symb'},
  164:                                                        $env{'form.idx'}));
  165:             return OK;
  166:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  167:             my $crstype = &Apache::loncommon::course_type();
  168:             my $title = $env{'form.title'};
  169:             if ($title eq '') {
  170:                 $title = &mt('External Resource');
  171:             }
  172:             $brcrum =
  173:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  174:         }
  175:     }
  176: 
  177: #
  178: # Actual URL
  179: #
  180:     if ($url=~/$LONCAPA::assess_re/) {
  181: #
  182: # This is uploaded homework
  183: #
  184:         $env{'request.state'}='uploaded';
  185:         &Apache::lonhomework::renderpage($r,$url);
  186:     } else {
  187: #
  188: # This is not homework
  189: #
  190:         if ($is_ext) {
  191:             $absolute = $env{'request.use_absolute'};
  192:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  193:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  194:         }
  195: 
  196:         unless ($ENV{'QUERY_STRING'} eq '') {
  197:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  198:         }
  199: 
  200:         # encrypt url if not external
  201:         &Apache::lonenc::check_encrypt(\$url) if $url !~ /^https?\:/ ;
  202: 
  203:         $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf) );
  204: 
  205:     } # not just the menu
  206:     
  207:     return OK;
  208: } # handler
  209: 
  210: 1;
  211: __END__
  212: 
  213: =pod
  214: 
  215: =head1 NAME
  216: 
  217: Apache::lonwrapper - External and binary file management.
  218: 
  219: =head1 SYNOPSIS
  220: 
  221: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  222: 
  223: This is part of the LearningOnline Network with CAPA project
  224: described at http://www.lon-capa.org.
  225: 
  226: =head1 Subroutines
  227: 
  228: =over
  229: 
  230: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf,$title))
  231: 
  232: =over
  233: 
  234: =item $url
  235: 
  236: url to display by including in an iframe within a
  237: LON-CAPA page which has a standard LON-CAPA inline menu.
  238: 
  239: =item $brcrum
  240: 
  241: breadcrumbs for unregistered urls
  242: (i.e., external resources in Supplemental Content).
  243: 
  244: =item $absolute
  245: 
  246: contains protocol (http or https) followed by
  247: the hostname, if menu items in the standard LON-CAPA
  248: interface created by the call to loncommon::start_page()
  249: within &wrapper() need to use absolute URLs rather than
  250: relative URLs.
  251: 
  252: That will be the case where an external resource has been 
  253: served from port 80, when the server customarily serves
  254: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  255: is used to switch requests for external resources and
  256: the syllabus: /public/<domain>/<courseid>/syllabus
  257: (which might also point at an external resource)
  258: from https:// to http:// where the the URL of the remote site 
  259: specified in the resource itself is http://.
  260: 
  261: This is done to avoid default mixed content blocking
  262: in Firefox 23 and later, when serving from Apache/SSL.
  263: 
  264: =item $is_ext
  265: 
  266: true if URL is for an external resource.
  267: 
  268: =item $is_pdf
  269: 
  270: true if URL is for a PDF (based on file extension).
  271: 
  272: =item $title
  273: 
  274: optional. If wrapped item is a PDF, and $env{'browser.mobile'} 
  275: is true, a link to a PDF is shown. The "title" will be displayed
  276: above the link, but if not provided as an arg, $env{'form.title'}
  277: will be used, otherwise, the filename will be displayed (unless
  278: hidden URL set for the resource).
  279: 
  280: =back
  281: 
  282: Returns markup for the entire page.
  283: 
  284: =item handler()
  285: 
  286: =back
  287: 
  288: =cut
  289: 

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