File:  [LON-CAPA] / rat / lonwrapper.pm
Revision 1.51: download - view: text, annotated - select for diffs
Mon Feb 22 03:36:52 2016 UTC (8 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6806. Support anchor in URL set for an external resource.

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

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