File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49.2.1: download - view: text, annotated - select for diffs
Fri Aug 5 21:34:54 2016 UTC (7 years, 8 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.49: preferred, unified
- For 2.11
  - Backport 1.51

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

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