File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49.2.7.2.3: download - view: text, annotated - select for diffs
Sat Jul 18 20:34:58 2020 UTC (3 years, 9 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 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.7.2.3 2020/07/18 20:34:58 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 LONCAPA qw(:DEFAULT :match);
   42: use HTML::Entities();
   43: 
   44: # ================================================================ Main Handler
   45: sub wrapper {
   46:     my ($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,
   47:         $title,$width,$height) = @_;
   48: 
   49:     my $forcereg;
   50:     unless ($env{'form.folderpath'}) {
   51:         $forcereg = 1;
   52:     }
   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 $headjs;
   93: 
   94: #
   95: # Where iframe is in use, if window.onload() executes before the custom resize function
   96: # has been defined (jQuery), two global javascript vars (LCnotready and LCresizedef)
   97: # are used to ensure document.ready() triggers a call to resize, so the iframe contents
   98: # do not obscure the Functions menu.
   99: #
  100: 
  101:     unless (($env{'browser.mobile'}) || ($exttool eq 'window') || ($exttool eq 'tab') || $uselink) {
  102:         $headjs = '
  103: <script type="text/javascript">
  104: // <![CDATA[
  105: var LCnotready = 0;
  106: var LCresizedef = 0;
  107: // ]]>
  108: </script>'."\n";
  109:     }
  110: 
  111:     my $startpage = &Apache::loncommon::start_page('Menu',$headjs,$args);
  112:     my $endpage = &Apache::loncommon::end_page();
  113: 
  114:     if (($uselink) && ($title eq '')) {
  115:         if ($env{'form.symb'}) {
  116:             $title=&Apache::lonnet::gettitle($env{'form.symb'});
  117:         } else {
  118:             my $symb=&Apache::lonnet::symbread($r->uri);
  119:             if ($symb) {
  120:                 $title=&Apache::lonnet::gettitle($symb);
  121:             }
  122:         }
  123:     }
  124:     if (($env{'browser.mobile'}) || ($exttool eq 'window') || ($exttool eq 'tab')) {
  125:         my $output = $startpage;
  126:         if ($is_pdf) {
  127:             $linktext = &mt('Link to PDF (for mobile devices)');
  128:             $output .= &create_link($url,$anchor,$title,$linktext);
  129:         } elsif (($exttool eq 'window') || ($exttool eq 'tab')) {
  130:             if ($linktext eq '') {
  131:                 $linktext = &mt('Launch External Tool');
  132:             }
  133:             $url = &HTML::Entities::encode($url,'"<>&');
  134:             if ($exttool eq 'tab') {
  135:                 $output .= '<div>'.
  136:                            '<a href="'.$url.'" target="LCExternalToolTab" style="padding:0;clear:both;margin:0;border:0">'.
  137:                            $linktext.'</a>'.
  138:                            '</div>';
  139:             } else {
  140:                 $output .= <<"ENDLINK";
  141: <script type="text/javascript">
  142: // <![CDATA[
  143: var windowObjectReference = null;
  144: var PreviousUrl;
  145: 
  146: function openSinglePopup(strUrl) {
  147:     if (windowObjectReference == null || windowObjectReference.closed) {
  148:         windowObjectReference = window.open(strUrl, "LCExternalToolPopUp",
  149:                                             "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
  150:     } else if(PreviousUrl != strUrl) {
  151:         windowObjectReference = window.open(strUrl, "LCExternalToolPopUp",
  152:                                             "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
  153:         windowObjectReference.focus();
  154:     } else {
  155:         windowObjectReference.focus();
  156:     };
  157:     PreviousUrl = strUrl;
  158: }
  159: // ]]>
  160: </script>
  161: <div>
  162: <a href="$url" target="LCExternalToolPopUp" onclick="openSinglePopup(this.href); return false;">
  163: $linktext</a>
  164: </div>
  165: ENDLINK
  166:             }
  167:             if ($explanation ne '') {
  168:                 $output .= '<div>'.$explanation.'</div>';
  169:             }
  170:         } else {
  171:             if ($uselink) {
  172:                 $linktext = &mt('Link to resource');
  173:                 $output .= &create_link($url,$anchor,$title,$linktext);
  174:             } else {
  175:                 my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  176:                 $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
  177:                            '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
  178:                            "$lt{'noif'} $noiframe\n".
  179:                            "</iframe>\n".
  180:                            "</div>\n";
  181:             }
  182:         }
  183:         $output .= $endpage;
  184:         return $output;
  185:     } elsif ($uselink) {
  186:         $linktext = &mt('Link to resource');
  187:         return $startpage.&create_link($url,$anchor,$title,$linktext).$endpage;
  188:     } else {
  189:         my $offset = 5;
  190:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
  191:         if ($env{'form.inhibitmenu'} eq 'yes') {
  192:             $offset = 0;
  193:         }
  194:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
  195:         \$(document).ready( function() {
  196:             \$(window).unbind('resize').resize(function(){
  197:                 var header = null;
  198:                 var offset = $offset;
  199:                 var height = 0;
  200:                 var hdrtop = 0;
  201:                 if (\$('div.LC_head_subbox:first').length) {
  202:                     header = \$('div.LC_head_subbox:first');
  203:                     offset = 9;
  204:                 } else {
  205:                     if (\$('#LC_breadcrumbs').length) {
  206:                         header = \$('#LC_breadcrumbs');
  207:                     }
  208:                 }
  209:                 if (header != null && header.length) {
  210:                     height = header.height();
  211:                     hdrtop = header.position().top;
  212:                 }
  213:                 var pos = height + hdrtop + offset;
  214:                 \$('.LC_iframecontainer').css('top', pos);
  215:             });
  216:             LCresizedef = 1;
  217:             if (LCnotready == 1) {
  218:                 LCnotready = 0;
  219:                 \$(window).trigger('resize');
  220:             }
  221:         });
  222:         window.onload = function(){
  223:             if (LCresizedef) {
  224:                 LCnotready = 0;
  225:                 \$(window).trigger('resize');
  226:             } else {
  227:                 LCnotready = 1;
  228:             }
  229:         };
  230: SCRIPT
  231:         # javascript will position the iframe if window was resized (or zoomed)
  232:         my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  233:         return <<ENDFRAME;
  234:         $startpage
  235:         $script
  236:         <div class="LC_iframecontainer">
  237:             <iframe src="$dest">$lt{'noif'} $noiframe</iframe>
  238:         </div>
  239:         $endpage
  240: ENDFRAME
  241:     }
  242: }
  243: 
  244: sub create_link {
  245:     my ($url,$anchor,$title,$linktext) = @_;
  246:     my $shownlink;
  247:     if ($title eq '') {
  248:         $title = $env{'form.title'};
  249:         if ($title eq '') {
  250:             unless ($env{'request.enc'}) {
  251:                 ($title) = ($url =~ m{/([^/]+)$});
  252:                 $title =~ s/(\?[^\?]+)$//;
  253:             }
  254:         }
  255:     }
  256:     unless ($title eq '') {
  257:         $shownlink = '<span style="font-weight:bold;">'.$title.'</span><br />';
  258:     }
  259:     my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  260:     $shownlink .= '<a href="'.$dest.'">'.$linktext.'</a>';
  261:     return $shownlink;
  262: }
  263: 
  264: sub handler {
  265:     my $r=shift;
  266:     &Apache::loncommon::content_type($r,'text/html');
  267:     $r->send_http_header;
  268: 
  269:     return OK if $r->header_only;
  270: 
  271:     my $url = $r->uri;
  272:     my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum,$hostname,
  273:         $linktext,$explanation,$width,$height);
  274: 
  275:     for ($url){
  276:         s|^/adm/wrapper||;
  277:         $is_ext = $_ =~ s|^/ext/|http://|;
  278:         s|http://https://|https://| if ($is_ext);
  279:         s|&colon;|:|g;
  280:     }
  281: 
  282:     if ($url =~ /\.pdf$/i) {
  283:         $is_pdf = 1;
  284:     } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
  285:         $cdom = $1;
  286:         $cnum = $2;
  287:         my $marker = $3;
  288:         $exttool = 'iframe';
  289:         my $exttoolremote;
  290:         my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target','linktext','explanation','id','width','height'],
  291:                                             $cdom,$cnum);
  292:         if ($toolhash{'id'}) {
  293:             my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
  294:             if (ref($ltitools{$toolhash{'id'}}) eq 'HASH') {
  295:                 $exttoolremote = $ltitools{$toolhash{'id'}}{'url'};
  296:             }
  297:         }
  298:         if ($toolhash{'target'} eq 'window') {
  299:             $exttool = 'window';
  300:             $width = $toolhash{'width'};
  301:             $height = $toolhash{'height'};
  302:         } elsif ($toolhash{'target'} eq 'tab') {
  303:             $exttool = 'tab';
  304:         }
  305:         if (($exttool eq 'window') || ($exttool eq 'tab')) {
  306:             $linktext = $toolhash{'linktext'};
  307:             $explanation = $toolhash{'explanation'};
  308:         } elsif (($exttoolremote =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  309:             $exttool = 'tab';
  310:         }
  311:     }
  312:     if (($is_ext) || ($exttool)) {
  313:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  314:             ['forceedit','register','folderpath','symb','idx','title','anchor']);
  315:         if (($env{'form.forceedit'}) &&
  316:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  317:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  318:              ($env{'form.symb'} =~ /^uploaded/))) {
  319:             if ($env{'form.symb'}) {
  320:                 (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
  321:                 if ($res =~ /(#[^#]+)$/) {
  322:                     $url .= $1;
  323:                 }
  324:             } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  325:                 if ($env{'form.anchor'} ne '') {
  326:                     $url .= '#'.$env{'form.anchor'};
  327:                 }
  328:             }
  329:             my $type = 'ext';
  330:             if ($exttool) {
  331:                 $type = 'tool';
  332:             } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  333:                 $hostname = $r->hostname();
  334:             }
  335:             $r->print(
  336:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  337:                                                        $env{'form.symb'},
  338:                                                        $env{'form.idx'},$type,$cdom,
  339:                                                        $cnum,$hostname));
  340:             return OK;
  341:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  342:             my $crstype = &Apache::loncommon::course_type();
  343:             my $title = $env{'form.title'};
  344:             if ($title eq '') {
  345:                 if ($is_ext) {
  346:                     $title = &mt('External Resource');
  347:                 } else {
  348:                     $title = &mt('External Tool');
  349:                 }
  350:             }
  351:             $brcrum =
  352:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  353:         }
  354:     }
  355: 
  356: #
  357: # Actual URL
  358: #
  359:     if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
  360: #
  361: # This is uploaded homework
  362: #
  363:         $env{'request.state'}='uploaded';
  364:         &Apache::lonhomework::renderpage($r,$url);
  365:     } else {
  366: #
  367: # This is not homework
  368: #
  369:         if (($is_ext) || ($exttool)) {
  370:             $absolute = $env{'request.use_absolute'};
  371:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  372:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  373:         }
  374: 
  375:         unless ($ENV{'QUERY_STRING'} eq '') {
  376:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  377:         }
  378: 
  379:         # encrypt url if not external
  380:         unless ($is_ext) {
  381:             &Apache::lonenc::check_encrypt(\$url);
  382:         }
  383: 
  384:         $r->print( wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
  385:                            $linktext,$explanation,undef,$width,$height) );
  386: 
  387:     } # not just the menu
  388:     
  389:     return OK;
  390: } # handler
  391: 
  392: 1;
  393: __END__
  394: 
  395: =pod
  396: 
  397: =head1 NAME
  398: 
  399: Apache::lonwrapper - External and binary file management.
  400: 
  401: =head1 SYNOPSIS
  402: 
  403: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  404: 
  405: This is part of the LearningOnline Network with CAPA project
  406: described at http://www.lon-capa.org.
  407: 
  408: =head1 Subroutines
  409: 
  410: =over
  411: 
  412: =item wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,$title,$width,$height)
  413: 
  414: =over
  415: 
  416: =item $r
  417: 
  418: request object
  419: 
  420: =item $url
  421: 
  422: url to display by including in an iframe within a
  423: LON-CAPA page which has a standard LON-CAPA inline menu.
  424: 
  425: =item $brcrum
  426: 
  427: breadcrumbs for unregistered urls
  428: (i.e., external resources in Supplemental Content).
  429: 
  430: =item $absolute
  431: 
  432: contains protocol (http or https) followed by
  433: the hostname, if menu items in the standard LON-CAPA
  434: interface created by the call to loncommon::start_page()
  435: within &wrapper() need to use absolute URLs rather than
  436: relative URLs.
  437: 
  438: That will be the case where an external resource has been 
  439: served from port 80, when the server customarily serves
  440: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  441: is used to switch requests for external resources and
  442: the syllabus: /public/<domain>/<courseid>/syllabus
  443: (which might also point at an external resource)
  444: from https:// to http:// where the the URL of the remote site 
  445: specified in the resource itself is http://.
  446: 
  447: This is done to avoid default mixed content blocking
  448: in Firefox 23 and later, when serving from Apache/SSL.
  449: 
  450: =item $is_ext
  451: 
  452: true if URL is for an external resource.
  453: 
  454: =item $is_pdf
  455: 
  456: true if URL is for a PDF (based on file extension).
  457: 
  458: =item $title
  459: 
  460: optional. If wrapped item is a PDF, and $env{'browser.mobile'} 
  461: is true, a link to a PDF is shown. The "title" will be displayed
  462: above the link, but if not provided as an arg, $env{'form.title'}
  463: will be used, otherwise, the filename will be displayed (unless
  464: hidden URL set for the resource).
  465: 
  466: =back
  467: 
  468: Returns markup for the entire page.
  469: 
  470: =item handler()
  471: 
  472: =back
  473: 
  474: =cut
  475: 

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