File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49.2.10: download - view: text, annotated - select for diffs
Thu Mar 5 22:05:50 2020 UTC (4 years, 1 month ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_3_uiuc, version_2_11_3
Diff to branchpoint 1.49: preferred, unified
- For 2.11
  Backport 1.76

    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.10 2020/03/05 22:05:50 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 ($r,$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,$uselink,$linktext);
   56:     if ($is_ext) {
   57:         if ($env{'form.symb'}) {
   58:             (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
   59:             if ($res =~ /(#[^#]+)$/) {
   60:                 $anchor = $1;
   61:             }
   62:         } elsif ($env{'form.anchor'} ne '') {
   63:             $anchor = '#'.$env{'form.anchor'};
   64:         }
   65:         unless (($is_pdf) && ($env{'browser.mobile'})) {
   66:             my $hostname = $r->hostname();
   67:             my $lonhost = $r->dir_config('lonHostID');
   68:             my $ip = &Apache::lonnet::get_host_ip($lonhost);
   69:             $uselink = &Apache::loncommon::is_nonframeable($url,$absolute,$hostname,$ip);
   70:         }
   71:     }
   72: 
   73:     my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
   74:     my $args = {'bgcolor' => '#FFFFFF'};
   75:     if ($forcereg) {
   76:         $args->{'force_register'} = $forcereg;
   77:     }
   78:     if (ref($brcrum) eq 'ARRAY') {
   79:         $args->{'bread_crumbs'} = $brcrum;
   80:     }
   81:     if ($absolute) {
   82:         $args->{'use_absolute'} = $absolute;
   83:     }
   84:     if ($env{'form.only_body'}) {
   85:         $args->{'only_body'} = $env{'form.only_body'};
   86:     }
   87: 
   88:     my $headjs;
   89: 
   90: #
   91: # Where iframe is in use, if window.onload() executes before the custom resize function
   92: # has been defined (jQuery), two global javascript vars (LCnotready and LCresizedef)
   93: # are used to ensure document.ready() triggers a call to resize, so the iframe contents
   94: # do not obscure the Functions menu.
   95: #
   96: 
   97:     unless (($env{'browser.mobile'}) || $uselink) {
   98:         $headjs = '
   99: <script type="text/javascript">
  100: // <![CDATA[
  101: var LCnotready = 0;
  102: var LCresizedef = 0;
  103: // ]]>
  104: </script>'."\n";
  105:     }
  106: 
  107:     my $startpage = &Apache::loncommon::start_page('Menu',$headjs,$args);
  108:     my $endpage = &Apache::loncommon::end_page();
  109: 
  110:     if (($uselink) && ($title eq '')) {
  111:         if ($env{'form.symb'}) {
  112:             $title=&Apache::lonnet::gettitle($env{'form.symb'});
  113:         } else {
  114:             my $symb=&Apache::lonnet::symbread($r->uri);
  115:             if ($symb) {
  116:                 $title=&Apache::lonnet::gettitle($symb);
  117:             }
  118:         }
  119:     }
  120:     if ($env{'browser.mobile'}) {
  121:         my $output = $startpage;
  122:         if ($is_pdf) {
  123:             $linktext = &mt('Link to PDF (for mobile devices)');
  124:             $output .= &create_link($url,$anchor,$title,$linktext);
  125:         } else {
  126:             if ($uselink) {
  127:                 $linktext = &mt('Link to resource');
  128:                 $output .= &create_link($url,$anchor,$title,$linktext);
  129:             } else {
  130:                 my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  131:                 $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
  132:                            '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
  133:                            "$lt{'noif'} $noiframe\n".
  134:                            "</iframe>\n".
  135:                            "</div>\n";
  136:             }
  137:         }
  138:         $output .= $endpage;
  139:         return $output;
  140:     } elsif ($uselink) {
  141:         $linktext = &mt('Link to resource');
  142:         return $startpage.&create_link($url,$anchor,$title,$linktext).$endpage;
  143:     } else {
  144:         my $offset = 5;
  145:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
  146:         if ($env{'form.inhibitmenu'} eq 'yes') {
  147:             $offset = 0;
  148:         }
  149:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
  150:         \$(document).ready( function() {
  151:             \$(window).unbind('resize').resize(function(){
  152:                 var header = null;
  153:                 var offset = $offset;
  154:                 var height = 0;
  155:                 var hdrtop = 0;
  156:                 if (\$('div.LC_head_subbox:first').length) {
  157:                     header = \$('div.LC_head_subbox:first');
  158:                     offset = 9;
  159:                 } else {
  160:                     if (\$('#LC_breadcrumbs').length) {
  161:                         header = \$('#LC_breadcrumbs');
  162:                     }
  163:                 }
  164:                 if (header != null && header.length) {
  165:                     height = header.height();
  166:                     hdrtop = header.position().top;
  167:                 }
  168:                 var pos = height + hdrtop + offset;
  169:                 \$('.LC_iframecontainer').css('top', pos);
  170:             });
  171:             LCresizedef = 1;
  172:             if (LCnotready == 1) {
  173:                 LCnotready = 0;
  174:                 \$(window).trigger('resize');
  175:             }
  176:         });
  177:         window.onload = function(){
  178:             if (LCresizedef) {
  179:                 LCnotready = 0;
  180:                 \$(window).trigger('resize');
  181:             } else {
  182:                 LCnotready = 1;
  183:             }
  184:         };
  185: SCRIPT
  186:         # javascript will position the iframe if window was resized (or zoomed)
  187:         return <<ENDFRAME;
  188:         $startpage
  189:         $script
  190:         <div class="LC_iframecontainer">
  191:             <iframe src="$url$anchor">$lt{'noif'} $noiframe</iframe>
  192:         </div>
  193:         $endpage
  194: ENDFRAME
  195:     }
  196: }
  197: 
  198: sub create_link {
  199:     my ($url,$anchor,$title,$linktext) = @_;
  200:     my $shownlink;
  201:     if ($title eq '') {
  202:         $title = $env{'form.title'};
  203:         if ($title eq '') {
  204:             unless ($env{'request.enc'}) {
  205:                 ($title) = ($url =~ m{/([^/]+)$});
  206:                 $title =~ s/(\?[^\?]+)$//;
  207:             }
  208:         }
  209:     }
  210:     unless ($title eq '') {
  211:         $shownlink = '<span style="font-weight:bold;">'.$title.'</span><br />';
  212:     }
  213:     my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  214:     $shownlink .= '<a href="'.$dest.'">'.$linktext.'</a>';
  215:     return $shownlink;
  216: }
  217: 
  218: sub handler {
  219:     my $r=shift;
  220:     &Apache::loncommon::content_type($r,'text/html');
  221:     $r->send_http_header;
  222: 
  223:     return OK if $r->header_only;
  224: 
  225:     my $url = $r->uri;
  226:     my ($is_ext,$brcrum,$absolute,$is_pdf,$cdom,$cnum,$hostname);
  227: 
  228:     for ($url){
  229:         s|^/adm/wrapper||;
  230:         $is_ext = $_ =~ s|^/ext/|http://|;
  231:         s|http://https://|https://| if ($is_ext);
  232:         s|&colon;|:|g;
  233:     }
  234: 
  235:     if ($url =~ /\.pdf$/i) {
  236:         $is_pdf = 1;
  237:     }
  238:  
  239:     if ($is_ext) {
  240:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  241:             ['forceedit','register','folderpath','symb','idx','title','anchor']);
  242:         if (($env{'form.forceedit'}) &&
  243:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  244:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  245:              ($env{'form.symb'} =~ /^uploaded/))) {
  246:             if ($env{'form.symb'}) {
  247:                 (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
  248:                 if ($res =~ /(#[^#]+)$/) {
  249:                     $url .= $1;
  250:                 }
  251:             } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  252:                 if ($env{'form.anchor'} ne '') {
  253:                     $url .= '#'.$env{'form.anchor'};
  254:                 }
  255:             }
  256:             if (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  257:                 $hostname = $r->hostname();
  258:             }
  259:             my $type = 'ext';
  260:             $r->print(
  261:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  262:                                                        $env{'form.symb'},
  263:                                                        $env{'form.idx'},$type,$cdom,
  264:                                                        $cnum,$hostname));
  265:             return OK;
  266:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  267:             my $crstype = &Apache::loncommon::course_type();
  268:             my $title = $env{'form.title'};
  269:             if ($title eq '') {
  270:                 $title = &mt('External Resource');
  271:             }
  272:             $brcrum =
  273:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  274:         }
  275:     }
  276: 
  277: #
  278: # Actual URL
  279: #
  280:     if ($url=~/$LONCAPA::assess_re/) {
  281: #
  282: # This is uploaded homework
  283: #
  284:         $env{'request.state'}='uploaded';
  285:         &Apache::lonhomework::renderpage($r,$url);
  286:     } else {
  287: #
  288: # This is not homework
  289: #
  290:         if ($is_ext) {
  291:             $absolute = $env{'request.use_absolute'};
  292:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  293:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  294:         }
  295: 
  296:         unless ($ENV{'QUERY_STRING'} eq '') {
  297:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  298:         }
  299: 
  300:         # encrypt url if not external
  301:         &Apache::lonenc::check_encrypt(\$url) if $url !~ /^https?\:/ ;
  302: 
  303:         $r->print( wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf) );
  304: 
  305:     } # not just the menu
  306:     
  307:     return OK;
  308: } # handler
  309: 
  310: 1;
  311: __END__
  312: 
  313: =pod
  314: 
  315: =head1 NAME
  316: 
  317: Apache::lonwrapper - External and binary file management.
  318: 
  319: =head1 SYNOPSIS
  320: 
  321: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  322: 
  323: This is part of the LearningOnline Network with CAPA project
  324: described at http://www.lon-capa.org.
  325: 
  326: =head1 Subroutines
  327: 
  328: =over
  329: 
  330: =item wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$title))
  331: 
  332: =over
  333: 
  334: =item $r
  335: 
  336: request object
  337: 
  338: =item $url
  339: 
  340: url to display by including in an iframe within a
  341: LON-CAPA page which has a standard LON-CAPA inline menu.
  342: 
  343: =item $brcrum
  344: 
  345: breadcrumbs for unregistered urls
  346: (i.e., external resources in Supplemental Content).
  347: 
  348: =item $absolute
  349: 
  350: contains protocol (http or https) followed by
  351: the hostname, if menu items in the standard LON-CAPA
  352: interface created by the call to loncommon::start_page()
  353: within &wrapper() need to use absolute URLs rather than
  354: relative URLs.
  355: 
  356: That will be the case where an external resource has been 
  357: served from port 80, when the server customarily serves
  358: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  359: is used to switch requests for external resources and
  360: the syllabus: /public/<domain>/<courseid>/syllabus
  361: (which might also point at an external resource)
  362: from https:// to http:// where the the URL of the remote site 
  363: specified in the resource itself is http://.
  364: 
  365: This is done to avoid default mixed content blocking
  366: in Firefox 23 and later, when serving from Apache/SSL.
  367: 
  368: =item $is_ext
  369: 
  370: true if URL is for an external resource.
  371: 
  372: =item $is_pdf
  373: 
  374: true if URL is for a PDF (based on file extension).
  375: 
  376: =item $title
  377: 
  378: optional. If wrapped item is a PDF, and $env{'browser.mobile'} 
  379: is true, a link to a PDF is shown. The "title" will be displayed
  380: above the link, but if not provided as an arg, $env{'form.title'}
  381: will be used, otherwise, the filename will be displayed (unless
  382: hidden URL set for the resource).
  383: 
  384: =back
  385: 
  386: Returns markup for the entire page.
  387: 
  388: =item handler()
  389: 
  390: =back
  391: 
  392: =cut
  393: 

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