File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49.2.11: download - view: text, annotated - select for diffs
Wed Dec 15 00:41:48 2021 UTC (2 years, 4 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4
Diff to branchpoint 1.49: preferred, unified
- For 2.11
  Backport 1.77 (part)

    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.11 2021/12/15 00:41:48 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: 
   41: # ================================================================ Main Handler
   42: sub wrapper {
   43:     my ($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$title) = @_;
   44: 
   45:     my $forcereg;
   46:     unless ($env{'form.folderpath'}) {
   47:         $forcereg = 1;
   48:     }
   49: 
   50:     my %lt = &Apache::lonlocal::texthash(
   51:                                           'noif' => 'No iframe support.',
   52:                                           'show' => 'Show content in pop-up window',
   53:                                         );
   54: 
   55:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
   56:         &Apache::loncommon::decode_user_agent($r);
   57: 
   58:     my ($anchor,$uselink,$linktext);
   59:     if ($is_ext) {
   60:         if ($env{'form.symb'}) {
   61:             (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
   62:             if ($res =~ /(#[^#]+)$/) {
   63:                 $anchor = $1;
   64:             }
   65:         } elsif ($env{'form.anchor'} ne '') {
   66:             $anchor = '#'.$env{'form.anchor'};
   67:         }
   68:         unless (($is_pdf) && ($clientmobile)) {
   69:             my $hostname = $r->hostname();
   70:             my $lonhost = $r->dir_config('lonHostID');
   71:             my $ip = &Apache::lonnet::get_host_ip($lonhost);
   72:             $uselink = &Apache::loncommon::is_nonframeable($url,$absolute,$hostname,$ip);
   73:         }
   74:     }
   75: 
   76:     my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
   77:     my $args = {'bgcolor' => '#FFFFFF'};
   78:     if ($forcereg) {
   79:         $args->{'force_register'} = $forcereg;
   80:     }
   81:     if (ref($brcrum) eq 'ARRAY') {
   82:         $args->{'bread_crumbs'} = $brcrum;
   83:     }
   84:     if ($absolute) {
   85:         $args->{'use_absolute'} = $absolute;
   86:     }
   87:     if ($env{'form.only_body'}) {
   88:         $args->{'only_body'} = $env{'form.only_body'};
   89:     }
   90: 
   91:     my $headjs;
   92: 
   93: #
   94: # Where iframe is in use, if window.onload() executes before the custom resize function
   95: # has been defined (jQuery), two global javascript vars (LCnotready and LCresizedef)
   96: # are used to ensure document.ready() triggers a call to resize, so the iframe contents
   97: # do not obscure the Functions menu.
   98: #
   99: 
  100:     unless ($clientmobile || $uselink) {
  101:         $headjs = '
  102: <script type="text/javascript">
  103: // <![CDATA[
  104: var LCnotready = 0;
  105: var LCresizedef = 0;
  106: // ]]>
  107: </script>'."\n";
  108:     }
  109: 
  110:     my $startpage = &Apache::loncommon::start_page('Menu',$headjs,$args);
  111:     my $endpage = &Apache::loncommon::end_page();
  112: 
  113:     if (($uselink) && ($title eq '')) {
  114:         if ($env{'form.symb'}) {
  115:             $title=&Apache::lonnet::gettitle($env{'form.symb'});
  116:         } else {
  117:             my $symb=&Apache::lonnet::symbread($r->uri);
  118:             if ($symb) {
  119:                 $title=&Apache::lonnet::gettitle($symb);
  120:             }
  121:         }
  122:     }
  123:     if ($clientmobile) {
  124:         my $output = $startpage;
  125:         if ($is_pdf) {
  126:             $linktext = &mt('Link to PDF (for mobile devices)');
  127:             $output .= &create_link($url,$anchor,$title,$linktext);
  128:         } else {
  129:             if ($uselink) {
  130:                 $linktext = &mt('Link to resource');
  131:                 $output .= &create_link($url,$anchor,$title,$linktext);
  132:             } else {
  133:                 my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  134:                 $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
  135:                            '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
  136:                            "$lt{'noif'} $noiframe\n".
  137:                            "</iframe>\n".
  138:                            "</div>\n";
  139:             }
  140:         }
  141:         $output .= $endpage;
  142:         return $output;
  143:     } elsif ($uselink) {
  144:         $linktext = &mt('Link to resource');
  145:         return $startpage.&create_link($url,$anchor,$title,$linktext).$endpage;
  146:     } else {
  147:         my $offset = 5;
  148:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
  149:         if ($env{'form.inhibitmenu'} eq 'yes') {
  150:             $offset = 0;
  151:         }
  152:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
  153:         \$(document).ready( function() {
  154:             \$(window).unbind('resize').resize(function(){
  155:                 var header = null;
  156:                 var offset = $offset;
  157:                 var height = 0;
  158:                 var hdrtop = 0;
  159:                 if (\$('div.LC_head_subbox:first').length) {
  160:                     header = \$('div.LC_head_subbox:first');
  161:                     offset = 9;
  162:                 } else {
  163:                     if (\$('#LC_breadcrumbs').length) {
  164:                         header = \$('#LC_breadcrumbs');
  165:                     }
  166:                 }
  167:                 if (header != null && header.length) {
  168:                     height = header.height();
  169:                     hdrtop = header.position().top;
  170:                 }
  171:                 var pos = height + hdrtop + offset;
  172:                 \$('.LC_iframecontainer').css('top', pos);
  173:             });
  174:             LCresizedef = 1;
  175:             if (LCnotready == 1) {
  176:                 LCnotready = 0;
  177:                 \$(window).trigger('resize');
  178:             }
  179:         });
  180:         window.onload = function(){
  181:             if (LCresizedef) {
  182:                 LCnotready = 0;
  183:                 \$(window).trigger('resize');
  184:             } else {
  185:                 LCnotready = 1;
  186:             }
  187:         };
  188: SCRIPT
  189:         # javascript will position the iframe if window was resized (or zoomed)
  190:         return <<ENDFRAME;
  191:         $startpage
  192:         $script
  193:         <div class="LC_iframecontainer">
  194:             <iframe src="$url$anchor">$lt{'noif'} $noiframe</iframe>
  195:         </div>
  196:         $endpage
  197: ENDFRAME
  198:     }
  199: }
  200: 
  201: sub create_link {
  202:     my ($url,$anchor,$title,$linktext) = @_;
  203:     my $shownlink;
  204:     if ($title eq '') {
  205:         $title = $env{'form.title'};
  206:         if ($title eq '') {
  207:             unless ($env{'request.enc'}) {
  208:                 ($title) = ($url =~ m{/([^/]+)$});
  209:                 $title =~ s/(\?[^\?]+)$//;
  210:             }
  211:         }
  212:     }
  213:     unless ($title eq '') {
  214:         $shownlink = '<span style="font-weight:bold;">'.$title.'</span><br />';
  215:     }
  216:     my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
  217:     $shownlink .= '<a href="'.$dest.'">'.$linktext.'</a>';
  218:     return $shownlink;
  219: }
  220: 
  221: sub handler {
  222:     my $r=shift;
  223:     &Apache::loncommon::content_type($r,'text/html');
  224:     $r->send_http_header;
  225: 
  226:     return OK if $r->header_only;
  227: 
  228:     my $url = $r->uri;
  229:     my ($is_ext,$brcrum,$absolute,$is_pdf,$cdom,$cnum,$hostname);
  230: 
  231:     for ($url){
  232:         s|^/adm/wrapper||;
  233:         $is_ext = $_ =~ s|^/ext/|http://|;
  234:         s|http://https://|https://| if ($is_ext);
  235:         s|&colon;|:|g;
  236:     }
  237: 
  238:     if ($url =~ /\.pdf$/i) {
  239:         $is_pdf = 1;
  240:     }
  241:  
  242:     if ($is_ext) {
  243:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  244:             ['forceedit','register','folderpath','symb','idx','title','anchor']);
  245:         if (($env{'form.forceedit'}) &&
  246:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
  247:             (($env{'form.folderpath'} =~ /^supplemental/) ||
  248:              ($env{'form.symb'} =~ /^uploaded/))) {
  249:             if ($env{'form.symb'}) {
  250:                 (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
  251:                 if ($res =~ /(#[^#]+)$/) {
  252:                     $url .= $1;
  253:                 }
  254:             } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  255:                 if ($env{'form.anchor'} ne '') {
  256:                     $url .= '#'.$env{'form.anchor'};
  257:                 }
  258:             }
  259:             if (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
  260:                 $hostname = $r->hostname();
  261:             }
  262:             my $type = 'ext';
  263:             $r->print(
  264:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
  265:                                                        $env{'form.symb'},
  266:                                                        $env{'form.idx'},$type,$cdom,
  267:                                                        $cnum,$hostname));
  268:             return OK;
  269:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
  270:             my $crstype = &Apache::loncommon::course_type();
  271:             my $title = $env{'form.title'};
  272:             if ($title eq '') {
  273:                 $title = &mt('External Resource');
  274:             }
  275:             $brcrum =
  276:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  277:         }
  278:     }
  279: 
  280: #
  281: # Actual URL
  282: #
  283:     if ($url=~/$LONCAPA::assess_re/) {
  284: #
  285: # This is uploaded homework
  286: #
  287:         $env{'request.state'}='uploaded';
  288:         &Apache::lonhomework::renderpage($r,$url);
  289:     } else {
  290: #
  291: # This is not homework
  292: #
  293:         if ($is_ext) {
  294:             $absolute = $env{'request.use_absolute'};
  295:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
  296:             $ENV{'QUERY_STRING'} =~ s/\&$//;
  297:         }
  298: 
  299:         unless ($ENV{'QUERY_STRING'} eq '') {
  300:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
  301:         }
  302: 
  303:         # encrypt url if not external
  304:         &Apache::lonenc::check_encrypt(\$url) if $url !~ /^https?\:/ ;
  305: 
  306:         $r->print( wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf) );
  307: 
  308:     } # not just the menu
  309:     
  310:     return OK;
  311: } # handler
  312: 
  313: 1;
  314: __END__
  315: 
  316: =pod
  317: 
  318: =head1 NAME
  319: 
  320: Apache::lonwrapper - External and binary file management.
  321: 
  322: =head1 SYNOPSIS
  323: 
  324: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
  325: 
  326: This is part of the LearningOnline Network with CAPA project
  327: described at http://www.lon-capa.org.
  328: 
  329: =head1 Subroutines
  330: 
  331: =over
  332: 
  333: =item wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$title))
  334: 
  335: =over
  336: 
  337: =item $r
  338: 
  339: request object
  340: 
  341: =item $url
  342: 
  343: url to display by including in an iframe within a
  344: LON-CAPA page which has a standard LON-CAPA inline menu.
  345: 
  346: =item $brcrum
  347: 
  348: breadcrumbs for unregistered urls
  349: (i.e., external resources in Supplemental Content).
  350: 
  351: =item $absolute
  352: 
  353: contains protocol (http or https) followed by
  354: the hostname, if menu items in the standard LON-CAPA
  355: interface created by the call to loncommon::start_page()
  356: within &wrapper() need to use absolute URLs rather than
  357: relative URLs.
  358: 
  359: That will be the case where an external resource has been 
  360: served from port 80, when the server customarily serves
  361: requests using Apache/SSL (i.e., port 443). mod_rewrite 
  362: is used to switch requests for external resources and
  363: the syllabus: /public/<domain>/<courseid>/syllabus
  364: (which might also point at an external resource)
  365: from https:// to http:// where the the URL of the remote site 
  366: specified in the resource itself is http://.
  367: 
  368: This is done to avoid default mixed content blocking
  369: in Firefox 23 and later, when serving from Apache/SSL.
  370: 
  371: =item $is_ext
  372: 
  373: true if URL is for an external resource.
  374: 
  375: =item $is_pdf
  376: 
  377: true if URL is for a PDF (based on file extension).
  378: 
  379: =item $title
  380: 
  381: optional. If wrapped item is a PDF, and $clientmobile is true,
  382: a link to a PDF is shown. The "title" will be displayed
  383: above the link, but if not provided as an arg, $env{'form.title'}
  384: will be used, otherwise, the filename will be displayed (unless
  385: hidden URL set for the resource).
  386: 
  387: =back
  388: 
  389: Returns markup for the entire page.
  390: 
  391: =item handler()
  392: 
  393: =back
  394: 
  395: =cut
  396: 

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