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