File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.77: download - view: text, annotated - select for diffs
Tue Oct 19 00:16:32 2021 UTC (2 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- On iOS 13-15 "Request Mobile Website" is needed to view a PDF in Safari.
  Browser agent string decoded at request time instead of using value set
  at session initialization, to accommodate user toggling via Aa control.

    1: # The LearningOnline Network with CAPA
    2: # Wrapper for external and binary files as standalone resources
    3: #
    4: # $Id: lonwrapper.pm,v 1.77 2021/10/19 00:16:32 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: use Apache::lonexttool();
   41: use Apache::lonhomework();
   42: use LONCAPA qw(:DEFAULT :match);
   43: use HTML::Entities();
   44: 
   45: # ================================================================ Main Handler
   46: sub wrapper {
   47:     my ($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,
   48:         $title,$width,$height) = @_;
   49: 
   50:     my $forcereg;
   51:     unless ($env{'form.folderpath'}) {
   52:         $forcereg = 1;
   53:     }
   54:     my %lt = &Apache::lonlocal::texthash(
   55:                                           'noif' => 'No iframe support.',
   56:                                           'show' => 'Show content in pop-up window',
   57:                                         );
   58: 
   59:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
   60:         &Apache::loncommon::decode_user_agent($r);
   61: 
   62:     my ($anchor,$uselink);
   63:     if ($is_ext) {
   64:         if ($env{'form.symb'}) {
   65:             (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
   66:             if ($res =~ /(#[^#]+)$/) {
   67:                 $anchor = $1;
   68:             }
   69:         } elsif ($env{'form.anchor'} ne '') {
   70:             $anchor = '#'.$env{'form.anchor'};
   71:         }
   72:         unless (($is_pdf) && ($clientmobile)) {
   73:             my $hostname = $r->hostname();
   74:             my $lonhost = $r->dir_config('lonHostID');
   75:             my $ip = &Apache::lonnet::get_host_ip($lonhost);
   76:             $uselink = &Apache::loncommon::is_nonframeable($url,$absolute,$hostname,$ip);
   77:         }
   78:     }
   79: 
   80:     my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
   81:     my $args = {'bgcolor' => '#FFFFFF'};
   82:     if ($forcereg) {
   83:         $args->{'force_register'} = $forcereg;
   84:     }
   85:     if (ref($brcrum) eq 'ARRAY') {
   86:         $args->{'bread_crumbs'} = $brcrum;
   87:     }
   88:     if ($absolute) {
   89:         $args->{'use_absolute'} = $absolute;
   90:     }
   91:     if ($env{'form.only_body'}) {
   92:         $args->{'only_body'} = $env{'form.only_body'};
   93:     }
   94: 
   95:     my ($countdown,$donemsg,$headjs);
   96:     if (($exttool) && (&Apache::lonnet::EXT('resource.0.gradable') =~ /^yes$/i)) {
   97:         $Apache::lonhomework::browse = &Apache::lonnet::allowed('bre',$url);
   98:         if ($env{'form.markaccess'}) {
   99:             my $symb=&Apache::lonnet::symbread($url);
  100:             my @interval=&Apache::lonnet::EXT('resource.0.interval',$symb);
  101:             my ($timelimit) = split(/_/,$interval[0]);
  102:             my $setres = &Apache::lonnet::set_first_access($interval[1],$timelimit);
  103:             if ($setres eq 'ok') {
  104:                 delete($env{'form.markaccess'});
  105:             }
  106:         } elsif ($env{'form.LC_interval_done'} eq 'true') {
  107:             my $symb=&Apache::lonnet::symbread($url);
  108:             if ($symb) {
  109:                 (my $donebuttonresult,$donemsg) = &Apache::lonhomework::zero_timer($symb);
  110:                 undef($env{'form.LC_interval_done'});
  111:                 undef($env{'form.LC_interval_done_proctorpass'});
  112:             }
  113:         }
  114:         my ($status,$result,$resource_due) =
  115:             &Apache::lonexttool::gradabletool_access_check();
  116:         undef($Apache::lonhomework::browse);
  117:         if ($status eq 'CAN_ANSWER') {
  118:             if ($resource_due) {
  119:                 my $time_left = $resource_due - time();
  120:                 if ($resource_due && ($time_left > 0)) {
  121:                     $countdown ='
  122: <script type="text/javascript">
  123: // <![CDATA['."\n".
  124:                              &Apache::lonhtmlcommon::countdown().'
  125: // ]]>
  126: </script>'."\n".
  127:                     &Apache::lonhtmlcommon::set_due_date($resource_due);
  128:                 }
  129:             }
  130:         } else {
  131:             if ($status eq 'SHOW_ANSWER') {
  132:                 $result = &Apache::lonexttool::display_score().
  133:                           &Apache::lonfeedback::list_discussion('tool','OPEN');
  134:             }
  135:             return &Apache::loncommon::start_page('Menu',undef,$args).
  136:                    $result.
  137:                    &Apache::loncommon::end_page();
  138:         }
  139:     }
  140: 
  141: #
  142: # Where iframe is in use, if window.onload() executes before the custom resize function
  143: # has been defined (jQuery), two global javascript vars (LCnotready and LCresizedef)
  144: # are used to ensure document.ready() triggers a call to resize, so the iframe contents
  145: # do not obscure the Functions menu.
  146: #
  147: 
  148:     unless ($clientmobile || ($exttool eq 'window') || ($exttool eq 'tab') || $uselink) {
  149:         $headjs = '
  150: <script type="text/javascript">
  151: // <![CDATA[
  152: var LCnotready = 0;
  153: var LCresizedef = 0;
  154: // ]]>
  155: </script>'."\n";
  156:     }
  157: 
  158:     my $startpage = &Apache::loncommon::start_page('Menu',$headjs,$args).$countdown.$donemsg;
  159:     my $endpage = &Apache::loncommon::end_page();
  160: 
  161:     if (($uselink) && ($title eq '')) {
  162:         if ($env{'form.symb'}) {
  163:             $title=&Apache::lonnet::gettitle($env{'form.symb'});
  164:         } else {
  165:             my $symb=&Apache::lonnet::symbread($r->uri);
  166:             if ($symb) {
  167:                 $title=&Apache::lonnet::gettitle($symb);
  168:             }
  169:         }
  170:     }
  171:     if ($clientmobile || ($exttool eq 'window') || ($exttool eq 'tab')) {
  172:         my $output = $startpage;
  173:         if ($is_pdf) {
  174:             $linktext = &mt('Link to PDF (for mobile devices)');
  175:             $output .= &create_link($url,$anchor,$title,$linktext);
  176:         } elsif (($exttool eq 'window') || ($exttool eq 'tab')) {
  177:             if ($linktext eq '') {
  178:                 $linktext = &mt('Launch External Tool');
  179:             }
  180:             $url = &HTML::Entities::encode($url,'"<>&');
  181:             if ($exttool eq 'tab') {
  182:                 $output .= '<div>'.
  183:                            '<a href="'.$url.'" target="LCExternalToolTab" style="padding:0;clear:both;margin:0;border:0">'.
  184:                            $linktext.'</a>'.
  185:                            '</div>';
  186:             } else {
  187:                 $output .= <<"ENDLINK";
  188: <script type="text/javascript">
  189: // <![CDATA[
  190: var windowObjectReference = null;
  191: var PreviousUrl;
  192: 
  193: function openSinglePopup(strUrl) {
  194:     if (windowObjectReference == null || windowObjectReference.closed) {
  195:         windowObjectReference = window.open(strUrl, "LCExternalToolPopUp",
  196:                                             "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
  197:     } else if(PreviousUrl != strUrl) {
  198:         windowObjectReference = window.open(strUrl, "LCExternalToolPopUp",
  199:                                             "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
  200:         windowObjectReference.focus();
  201:     } else {
  202:         windowObjectReference.focus();
  203:     };
  204:     PreviousUrl = strUrl;
  205: }
  206: // ]]>
  207: </script>
  208: <div>
  209: <a href="$url" target="LCExternalToolPopUp" onclick="openSinglePopup(this.href); return false;">
  210: $linktext</a>
  211: </div>
  212: ENDLINK
  213:             }
  214:             if ($explanation ne '') {
  215:                 $output .= '<div>'.$explanation.'</div>';
  216:             }
  217:             if (&Apache::lonnet::EXT('resource.0.gradable')) {
  218:                 $output .= &Apache::lonfeedback::list_discussion('tool','OPEN');
  219:             }
  220:         } else {
  221:             if ($uselink) {
  222:                 $linktext = &mt('Link to resource');
  223:                 $output .= &create_link($url,$anchor,$title,$linktext);
  224:             } else {
  225:                 my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  226:                 $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
  227:                            '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
  228:                            "$lt{'noif'} $noiframe\n".
  229:                            "</iframe>\n".
  230:                            "</div>\n";
  231:             }
  232:         }
  233:         $output .= $endpage;
  234:         return $output;
  235:     } elsif ($uselink) {
  236:         $linktext = &mt('Link to resource');
  237:         return $startpage.&create_link($url,$anchor,$title,$linktext).$endpage;
  238:     } else {
  239:         my $offset = 5;
  240:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
  241:         if ($env{'form.inhibitmenu'} eq 'yes') {
  242:             $offset = 0;
  243:         }
  244:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
  245:         \$(document).ready( function() {
  246:             \$(window).unbind('resize').resize(function(){
  247:                 var header = null;
  248:                 var offset = $offset;
  249:                 var height = 0;
  250:                 var hdrtop = 0;
  251:                 if (\$('div.LC_head_subbox:first').length) {
  252:                     header = \$('div.LC_head_subbox:first');
  253:                     offset = 9;
  254:                 } else {
  255:                     if (\$('#LC_breadcrumbs').length) {
  256:                         header = \$('#LC_breadcrumbs');
  257:                     }
  258:                 }
  259:                 if (header != null && header.length) {
  260:                     height = header.height();
  261:                     hdrtop = header.position().top;
  262:                 }
  263:                 var pos = height + hdrtop + offset;
  264:                 \$('.LC_iframecontainer').css('top', pos);
  265:             });
  266:             LCresizedef = 1;
  267:             if (LCnotready == 1) {
  268:                 LCnotready = 0;
  269:                 \$(window).trigger('resize');
  270:             }
  271:         });
  272:         window.onload = function(){
  273:              if (LCresizedef) {
  274:                  LCnotready = 0;
  275:                  \$(window).trigger('resize');
  276:              } else {
  277:                  LCnotready = 1;
  278:              }
  279:         };
  280: SCRIPT
  281:         # javascript will position the iframe if window was resized (or zoomed)
  282:         my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  283:         return <<ENDFRAME;
  284:         $startpage
  285:         $script
  286:         <div class="LC_iframecontainer">
  287:             <iframe src="$dest">$lt{'noif'} $noiframe</iframe>
  288:         </div>
  289:         $endpage
  290: ENDFRAME
  291:     }
  292: }
  293: 
  294: sub create_link {
  295:     my ($url,$anchor,$title,$linktext) = @_;
  296:     my $shownlink;
  297:     if ($title eq '') {
  298:         $title = $env{'form.title'};
  299:         if ($title eq '') {
  300:             unless ($env{'request.enc'}) {
  301:                 ($title) = ($url =~ m{/([^/]+)$});
  302:                 $title =~ s/(\?[^\?]+)$//;
  303:             }
  304:         }
  305:     }
  306:     unless ($title eq '') {
  307:         $shownlink = '<span style="font-weight:bold;">'.$title.'</span><br />';
  308:     }
  309:     my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  310:     $shownlink .= '<a href="'.$dest.'">'.$linktext.'</a>';
  311:     return $shownlink;
  312: }
  313: 
  314: sub handler {
  315:     my $r=shift;
  316:     &Apache::loncommon::content_type($r,'text/html');
  317:     $r->send_http_header;
  318: 
  319:     return OK if $r->header_only;
  320: 
  321:     my $url = $r->uri;
  322:     my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum,$hostname,
  323:         $linktext,$explanation,$width,$height);
  324: 
  325:     for ($url){
  326:         s|^/adm/wrapper||;
  327:         $is_ext = $_ =~ s|^/ext/|http://|;
  328:         s|http://https://|https://| if ($is_ext);
  329:         s|&colon;|:|g;
  330:     }
  331: 
  332:     if ($url =~ /\.pdf$/i) {
  333:         $is_pdf = 1;
  334:     } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
  335:         $cdom = $1;
  336:         $cnum = $2;
  337:         my $marker = $3;
  338:         $exttool = 'iframe';
  339:         my $exttoolremote;
  340:         my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target','linktext','explanation','id','width','height'],
  341:                                             $cdom,$cnum);
  342:         if ($toolhash{'id'}) {
  343:             my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
  344:             if (ref($ltitools{$toolhash{'id'}}) eq 'HASH') {
  345:                 $exttoolremote = $ltitools{$toolhash{'id'}}{'url'};
  346:             }
  347:         }
  348:         if ($toolhash{'target'} eq 'window') {
  349:             $exttool = 'window';
  350:             $width = $toolhash{'width'};
  351:             $height = $toolhash{'height'};
  352:         } elsif ($toolhash{'target'} eq 'tab') {
  353:             $exttool = 'tab';
  354:         }
  355:         if (($exttool eq 'window') || ($exttool eq 'tab')) {
  356:             $linktext = $toolhash{'linktext'};
  357:             $explanation = $toolhash{'explanation'};
  358:         } elsif (($exttoolremote =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  359:             $exttool = 'tab';
  360:         }
  361:     }
  362:     if (($is_ext) || ($exttool)) {
  363:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  364:             ['forceedit','register','folderpath','symb','idx','title','anchor']);
  365:         if (($env{'form.forceedit'}) &&
  366:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  367:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  368:              ($env{'form.symb'} =~ /^uploaded/))) {
  369:             if ($env{'form.symb'}) {
  370:                 (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
  371:                 if ($res =~ /(#[^#]+)$/) {
  372:                     $url .= $1;
  373:                 }
  374:             } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  375:                 if ($env{'form.anchor'} ne '') {
  376:                     $url .= '#'.$env{'form.anchor'};
  377:                 }
  378:             }
  379:             my $type = 'ext';
  380:             if ($exttool) {
  381:                 $type = 'tool';
  382:             } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  383:                 $hostname = $r->hostname();
  384:             }
  385:             $r->print(
  386:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  387:                                                        $env{'form.symb'},
  388:                                                        $env{'form.idx'},$type,$cdom,
  389:                                                        $cnum,$hostname));
  390:             return OK;
  391:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  392:             my $crstype = &Apache::loncommon::course_type();
  393:             my $title = $env{'form.title'};
  394:             if ($title eq '') {
  395:                 if ($is_ext) {
  396:                     $title = &mt('External Resource');
  397:                 } else {
  398:                     $title = &mt('External Tool');
  399:                 }
  400:             }
  401:             $brcrum =
  402:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  403:         }
  404:     }
  405: 
  406: #
  407: # Actual URL
  408: #
  409:     if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
  410: #
  411: # This is uploaded homework
  412: #
  413:         $env{'request.state'}='uploaded';
  414:         &Apache::lonhomework::renderpage($r,$url);
  415:     } else {
  416: #
  417: # This is not homework
  418: #
  419:         if (($is_ext) || ($exttool)) {
  420:             $absolute = $env{'request.use_absolute'};
  421:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  422:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  423:         }
  424: 
  425:         unless ($ENV{'QUERY_STRING'} eq '') {
  426:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  427:         }
  428: 
  429:         # encrypt url if not external
  430:         unless ($is_ext) {
  431:             &Apache::lonenc::check_encrypt(\$url);
  432:         }
  433: 
  434:         $r->print( wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
  435:                            $linktext,$explanation,undef,$width,$height) );
  436: 
  437:     } # not just the menu
  438:     
  439:     return OK;
  440: } # handler
  441: 
  442: 1;
  443: __END__
  444: 
  445: =pod
  446: 
  447: =head1 NAME
  448: 
  449: Apache::lonwrapper - External and binary file management.
  450: 
  451: =head1 SYNOPSIS
  452: 
  453: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  454: 
  455: This is part of the LearningOnline Network with CAPA project
  456: described at http://www.lon-capa.org.
  457: 
  458: =head1 Subroutines
  459: 
  460: =over
  461: 
  462: =item wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,$title,$width,$height)
  463: 
  464: =over
  465: 
  466: =item $r
  467: 
  468: request object
  469: 
  470: =item $url
  471: 
  472: url to display either by including in an iframe within a
  473: LON-CAPA page which has a standard LON-CAPA inline menu,
  474: or in some cases launched in a separate tab or window,
  475: launched via a link in a LON-CAPA page with standard inline
  476: menu.
  477: 
  478: =item $brcrum
  479: 
  480: breadcrumbs for unregistered urls
  481: (i.e., external resources in Supplemental Content).
  482: 
  483: =item $absolute
  484: 
  485: contains protocol (http or https) followed by
  486: the hostname, if menu items in the standard LON-CAPA
  487: interface created by the call to loncommon::start_page()
  488: within &wrapper() need to use absolute URLs rather than
  489: relative URLs.
  490: 
  491: That will be the case where an external resource has been 
  492: served from port 80, when the server customarily serves
  493: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  494: is used to switch requests for external resources and
  495: the syllabus: /public/<domain>/<courseid>/syllabus
  496: (which might also point at an external resource)
  497: from https:// to http:// where the the URL of the remote site 
  498: specified in the resource itself is http://.
  499: 
  500: This is done to avoid default mixed content blocking
  501: in Firefox 23 and later, when serving from Apache/SSL.
  502: 
  503: =item $is_ext
  504: 
  505: true if URL is for an external resource.
  506: 
  507: =item $is_pdf
  508: 
  509: true if URL is for a PDF (based on file extension).
  510: 
  511: =item $exttool
  512: 
  513: If URL is for an External Tool, will contain the target type: iframe, window or tab.
  514: 
  515: =item $linktext
  516: 
  517: optional. If URL is for an External Tool, and target type is window or tab, 
  518: then the link text may be an option set in the course for each tool instance,
  519: or may be a default defined in the domain for all instances of the tool.
  520: 
  521: =item $explanation
  522: 
  523: optional. If URL is for an External Tool, and target type is window or tab, 
  524: then the explanation is an option set in the course for each tool instance,
  525: or may be a default defined in the domain for all instances of the tool.
  526: 
  527: =item $title
  528: 
  529: optional. If wrapped item is a PDF, and $clientmobile is true,
  530: a link to a PDF is shown. The "title" will be displayed
  531: above the link, but if not provided as an arg, $env{'form.title'}
  532: will be used, otherwise, the filename will be displayed (unless
  533: hidden URL set for the resource).
  534: 
  535: =item $width
  536: 
  537: optional. If URL is for an External Tool, and target type is window,
  538: then a default width may have been defined in the domain for all instances of 
  539: the tool.  If so, that width will be used for the window opened (via a link)
  540: to launch the external tool.
  541: 
  542: =item $height
  543: 
  544: optional. If URL is for an External Tool, and target type is window,
  545: then a default height may have been defined in the domain for all instances of 
  546: the tool.  If so, that height will be used for the window opened (via a link)
  547: to launch the external tool.
  548: 
  549: =back
  550: 
  551: Returns markup for the entire page.
  552: 
  553: =item handler()
  554: 
  555: Content handler for requests for: /adm/wrapper/...
  556: used for content to be displayed in an iframe, or launched in a separate tab
  557: or window via a link.  The target URL is extracted from the requested URL, by
  558: removing the /adm/wrapper prefix. 
  559: 
  560: The target URL will typically be a PDF served from the current server, an 
  561: external resource URL served from a different server, or an external tool
  562: (from an LTI Provider) launched from LON-CAPA (as LTI Consumer) and launched
  563: via a link.
  564: 
  565: If the request included forceedit in the query string, and the requester has
  566: rights to modify course content, then the editor will be didplayed to allow
  567: changes to be made to the resource (e.g., change the URL of the external resource,
  568: or change the setting for the external tool instance.
  569: 
  570: If not in edit mode, then the wrapper() subroutine will be called to generate the
  571: standard LON-CAPA inline menu, and then either a link to launch a separate tab or
  572: window, or an iframe to display the content inline. 
  573: 
  574: =back
  575: 
  576: =cut
  577: 

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