File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49.2.7.2.6: download - view: text, annotated - select for diffs
Tue Oct 19 11:27:51 2021 UTC (2 years, 6 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 1.77

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

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