Annotation of rat/lonwrapper.pm, revision 1.49.2.7

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

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