File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.69: download - view: text, annotated - select for diffs
Thu May 2 02:12:31 2019 UTC (4 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6910
Gracefully handle display (and preview) for External Resources for which
Content-Security-Policy or X-Frame-Options prevent display in iframe in LC.

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

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