File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.73: download - view: text, annotated - select for diffs
Sun Feb 16 23:07:36 2020 UTC (4 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Prevent overlap of iframe and Functions menu in corner case.

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

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