File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.49.2.4.2.1: download - view: text, annotated - select for diffs
Thu Jun 22 02:11:30 2017 UTC (6 years, 10 months ago) by raeburn
Branches: version_2_11_2_msu
Diff to branchpoint 1.49.2.4: preferred, unified
- For 2.11.2 (modified).
  Use 2.12 feature (LTI) pre-release, in modified 2.11.2. Domain's
  configuration must be set to prohibit hosting of sessions for
  domain's users outside own domain, and modification must be in
  place on all nodes in domain.

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

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