Annotation of rat/lonwrapper.pm, revision 1.48

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Wrapper for external and binary files as standalone resources
                      3: #
1.48    ! raeburn     4: # $Id: lonwrapper.pm,v 1.47 2014/05/20 11:47:06 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.47      raeburn    43:     my ($url,$brcrum,$absolute,$is_ext,$is_pdf) = @_;
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: 
                     55:     my $noiframe = &Apache::loncommon::modal_link($url,$lt{'show'},500,400);
1.42      raeburn    56:     my $args = {'bgcolor' => '#FFFFFF'};
                     57:     if ($forcereg) {
                     58:         $args->{'force_register'} = $forcereg;
                     59:     }
                     60:     if (ref($brcrum) eq 'ARRAY') {
1.45      raeburn    61:         $args->{'bread_crumbs'} = $brcrum;
1.42      raeburn    62:     }
1.44      raeburn    63:     if ($absolute) {
                     64:         $args->{'use_absolute'} = $absolute; 
                     65:     }
1.42      raeburn    66: 
1.46      raeburn    67:     my $startpage = &Apache::loncommon::start_page('Menu',undef,$args);
                     68:     my $endpage = &Apache::loncommon::end_page();
1.38      droeschl   69: 
1.46      raeburn    70:     if ($env{'browser.mobile'}) {
1.47      raeburn    71:         my $output = $startpage;
                     72:         if ($is_pdf) {
                     73:             my $title = $env{'form.title'};
                     74:             if ($title eq '') {
                     75:                 unless ($env{'request.enc'}) {
                     76:                     ($title) = ($url =~ m{/([^/]+)$});
                     77:                     $title =~ s/(\?[^\?]+)$//;
                     78:                 }
                     79:             }
                     80:             unless ($title eq '') {
                     81:                 $output .= $title.'<br />';
                     82:             }
                     83:             $output .= '<a href="'.$url.'">'.&mt('Link to PDF (for mobile devices)').'</a>';
                     84:         } else {
                     85:             $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
1.48    ! raeburn    86:                        '<iframe src="'.$url.'" height="100%" width="100%" frameborder="0">'."\n".
1.47      raeburn    87:                        "$lt{'noif'} $noiframe\n".
                     88:                        "</iframe>\n".
                     89:                        "</div>\n";
                     90:         }
                     91:         $output .= $endpage;
                     92:         return $output;
1.46      raeburn    93:     } else {
                     94:         my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
                     95:         \$(document).ready( function() {
                     96:             \$(window).unbind('resize').resize(function(){
                     97:                 var header;
                     98:                 var offset = 5;
                     99:                 var height = 0;
                    100:                 var hdrtop = 0;
                    101:                 if (\$('div.LC_head_subbox:first').length) {
                    102:                     header = \$('div.LC_head_subbox:first');
                    103:                     offset = 9;
                    104:                 } else {
                    105:                     if (\$('#LC_breadcrumbs').length) {
                    106:                         header = \$('#LC_breadcrumbs');
                    107:                     }
                    108:                 }
                    109:                 if (header.length) {
                    110:                     height = header.height();
                    111:                     hdrtop = header.position().top;
1.42      raeburn   112:                 }
1.46      raeburn   113:                 var pos = height + hdrtop + offset;
                    114:                 \$('.LC_iframecontainer').css('top', pos);
                    115:             });
1.38      droeschl  116:         });
1.46      raeburn   117:         window.onload = function(){  \$(window).trigger('resize') };
1.39      droeschl  118: SCRIPT
1.46      raeburn   119:         # javascript will position the iframe if window was resized (or zoomed)
                    120:         return <<ENDFRAME;
                    121:         $startpage
                    122:         $script
                    123:         <div class="LC_iframecontainer">
                    124:             <iframe src="$url">$lt{'noif'} $noiframe</iframe>
                    125:         </div>
                    126:         $endpage
1.38      droeschl  127: ENDFRAME
1.46      raeburn   128:     }
1.21      albertel  129: }
                    130: 
                    131: sub handler {
                    132:     my $r=shift;
                    133:     &Apache::loncommon::content_type($r,'text/html');
                    134:     $r->send_http_header;
                    135: 
                    136:     return OK if $r->header_only;
                    137: 
1.38      droeschl  138:     my $url = $r->uri;
1.47      raeburn   139:     my ($is_ext,$brcrum,$absolute,$is_pdf);
1.38      droeschl  140: 
                    141:     for ($url){
                    142:         s|^/adm/wrapper||;
                    143:         $is_ext = $_ =~ s|^/ext/|http://|;         
                    144:         s|http://https://|https://|;
                    145:         s|&colon;|:|g;              
1.21      albertel  146:     }
                    147: 
1.47      raeburn   148:     if ($url =~ /\.pdf$/i) {
                    149:         $is_pdf = 1;
                    150:     }
                    151:  
1.42      raeburn   152:     if ($is_ext) {
1.44      raeburn   153:         my $hostname = $r->hostname();
                    154:         my $lonhost = &Apache::lonnet::host_from_dns($hostname);
                    155:         if ($lonhost) {
                    156:             my $actual = &Apache::lonnet::absolute_url($hostname);
                    157:             my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname; 
                    158:             unless ($actual eq $expected) {
                    159:                 $absolute = $expected;
                    160:             }
                    161:         }
1.42      raeburn   162:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    163:             ['forceedit','register','folderpath','symb','idx','title']);
                    164:         if (($env{'form.forceedit'}) &&
                    165:             (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
                    166:             (($env{'form.folderpath'} =~ /^supplemental/) ||
                    167:              ($env{'form.symb'} =~ /^uploaded/))) {
                    168:             $r->print(
                    169:                 &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
                    170:                                                        $env{'form.symb'},
                    171:                                                        $env{'form.idx'}));
                    172:             return OK;
                    173:         } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
                    174:             my $crstype = &Apache::loncommon::course_type();
1.43      raeburn   175:             my $title = $env{'form.title'};
                    176:             if ($title eq '') {
                    177:                 $title = &mt('External Resource');
                    178:             }
1.42      raeburn   179:             $brcrum =
1.43      raeburn   180:                 &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
1.42      raeburn   181:         }
                    182:     }
                    183: 
1.21      albertel  184: #
                    185: # Actual URL
                    186: #
1.41      www       187:     if ($url=~/$LONCAPA::assess_re/) {
1.21      albertel  188: #
                    189: # This is uploaded homework
                    190: #
1.38      droeschl  191:         $env{'request.state'}='uploaded';
                    192:         &Apache::lonhomework::renderpage($r,$url);
1.21      albertel  193:     } else {
                    194: #
                    195: # This is not homework
                    196: #
1.38      droeschl  197:         if ($is_ext) {
                    198:             $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
1.42      raeburn   199:             $ENV{'QUERY_STRING'} =~ s/\&$//;
1.38      droeschl  200:         }
                    201: 
1.35      raeburn   202:         unless ($ENV{'QUERY_STRING'} eq '') {
1.38      droeschl  203:             $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
1.35      raeburn   204:         }
1.38      droeschl  205: 
                    206:         # encrypt url if not external
                    207:         &Apache::lonenc::check_encrypt(\$url) if $url !~ /^https?\:/ ;
                    208: 
1.47      raeburn   209:         $r->print( wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf) );
1.38      droeschl  210: 
1.21      albertel  211:     } # not just the menu
1.38      droeschl  212:     
1.21      albertel  213:     return OK;
1.13      www       214: } # handler
1.1       www       215: 
                    216: 1;
                    217: __END__
                    218: 
1.30      jms       219: =pod
1.1       www       220: 
1.30      jms       221: =head1 NAME
                    222: 
                    223: Apache::lonwrapper - External and binary file management.
                    224: 
                    225: =head1 SYNOPSIS
                    226: 
                    227: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
                    228: 
                    229: This is part of the LearningOnline Network with CAPA project
                    230: described at http://www.lon-capa.org.
                    231: 
                    232: =head1 Subroutines
                    233: 
                    234: =over
                    235: 
1.47      raeburn   236: =item wrapper($url,$brcrum,$absolute,$is_ext,$is_pdf))
1.30      jms       237: 
1.45      raeburn   238: =over
                    239: 
1.46      raeburn   240: =item $url
                    241: 
1.45      raeburn   242: url to display by including in an iframe within a
                    243: LON-CAPA page which has a standard LON-CAPA inline menu.
                    244: 
                    245: =item $brcrum
                    246: 
                    247: breadcrumbs for unregistered urls
1.42      raeburn   248: (i.e., external resources in Supplemental Content).
1.45      raeburn   249: 
1.46      raeburn   250: =item $absolute
1.45      raeburn   251: 
                    252: contains protocol (http or https) followed by
                    253: the hostname, if menu items in the standard LON-CAPA
                    254: interface created by the call to loncommon::start_page()
                    255: within &wrapper() need to use absolute URLs rather than
                    256: relative URLs.
                    257: 
                    258: That will be the case where an external resource has been 
                    259: served from port 80, when the server customarily serves
                    260: requests using Apache/SSL (i.e., port 443). mod_rewrite 
1.46      raeburn   261: is used to switch requests for external resources 
1.45      raeburn   262: from https:// to http:// where the the URL of the remote site 
                    263: specified in the resource itself is http://.
                    264: 
                    265: This is done to avoid default mixed content blocking
                    266: in Firefox 23 and later, when serving from Apache/SSL.
                    267: 
                    268: =item $is_ext
                    269: 
                    270: true if URL is for an external resource.
                    271: 
1.47      raeburn   272: =item $is_pdf
                    273: 
                    274: true if URL is for a PDF (based on file extension).
                    275: 
1.45      raeburn   276: =back
                    277: 
1.38      droeschl  278: Returns markup for the entire page.
1.30      jms       279: 
                    280: =item handler()
                    281: 
                    282: =back
                    283: 
                    284: =cut
1.1       www       285: 

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