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.11.2.3 2023/01/21 23:25:16 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 Apache::lonnavmaps();
42: use LONCAPA qw(:DEFAULT :match);
43: use HTML::Entities();
44: use Digest::MD5();
45:
46: # ================================================================ Main Handler
47: sub wrapper {
48: my ($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,
49: $title,$width,$height,$reuse,$is_supp) = @_;
50:
51: my $forcereg;
52: unless ($env{'form.folderpath'}) {
53: $forcereg = 1;
54: }
55:
56: my %lt = &Apache::lonlocal::texthash(
57: 'noif' => 'No iframe support.',
58: 'show' => 'Show content in pop-up window',
59: );
60:
61: (undef,undef,undef,undef,undef,undef,my $clientmobile) =
62: &Apache::loncommon::decode_user_agent($r);
63:
64: my ($anchor,$uselink);
65: if ($is_ext) {
66: if ($env{'form.symb'}) {
67: (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
68: if ($res =~ /(#[^#]+)$/) {
69: $anchor = $1;
70: }
71: } elsif ($env{'form.anchor'} ne '') {
72: $anchor = '#'.$env{'form.anchor'};
73: }
74: if (($is_ext eq 'tab') || ($is_ext eq 'window')) {
75: $uselink = 1;
76: }
77: unless (($is_pdf && $clientmobile) || $uselink) {
78: my $hostname = $r->hostname();
79: my $lonhost = $r->dir_config('lonHostID');
80: my $ip = &Apache::lonnet::get_host_ip($lonhost);
81: $uselink = &Apache::loncommon::is_nonframeable($url,$absolute,$hostname,$ip);
82: }
83: }
84:
85: my $noiframe = &Apache::loncommon::modal_link($url.$anchor,$lt{'show'},500,400);
86: my $args = {'bgcolor' => '#FFFFFF'};
87: if ($forcereg) {
88: $args->{'force_register'} = $forcereg;
89: }
90: if (ref($brcrum) eq 'ARRAY') {
91: $args->{'bread_crumbs'} = $brcrum;
92: }
93: if ($absolute) {
94: $args->{'use_absolute'} = $absolute;
95: }
96: if ($env{'form.only_body'}) {
97: $args->{'only_body'} = $env{'form.only_body'};
98: }
99:
100: my $headjs;
101:
102: #
103: # Where iframe is in use, if window.onload() executes before the custom resize function
104: # has been defined (jQuery), two global javascript vars (LCnotready and LCresizedef)
105: # are used to ensure document.ready() triggers a call to resize, so the iframe contents
106: # do not obscure the Functions menu.
107: #
108:
109: unless ($clientmobile || ($exttool eq 'window') || ($exttool eq 'tab') || $uselink) {
110: $headjs = '
111: <script type="text/javascript">
112: // <![CDATA[
113: var LCnotready = 0;
114: var LCresizedef = 0;
115: // ]]>
116: </script>'."\n";
117: }
118:
119: my $startpage = &Apache::loncommon::start_page('Menu',$headjs,$args);
120: my $endpage = &Apache::loncommon::end_page();
121:
122: if (($uselink) && ($title eq '')) {
123: if ($env{'form.symb'}) {
124: $title=&Apache::lonnet::gettitle($env{'form.symb'});
125: } elsif (!$is_supp) {
126: my $symb=&Apache::lonnet::symbread($r->uri);
127: if ($symb) {
128: $title=&Apache::lonnet::gettitle($symb);
129: }
130: }
131: }
132: if ($clientmobile || ($exttool eq 'window') || ($exttool eq 'tab') ||
133: ($is_ext eq 'tab') || ($is_ext eq 'window')) {
134: my $output = $startpage;
135: if ($is_pdf) {
136: $linktext = &mt('Link to PDF (for mobile devices)');
137: $output .= &create_link($url,$anchor,$title,$linktext);
138: } elsif (($exttool eq 'window') || ($exttool eq 'tab') ||
139: ($is_ext eq 'tab') || ($is_ext eq 'window')) {
140: my $preamble;
141: if ($linktext eq '') {
142: if ($exttool) {
143: $linktext = &mt('Launch External Tool');
144: } else {
145: $linktext = &mt('Link to External Resource');
146: }
147: }
148: if ($exttool) {
149: $url = &HTML::Entities::encode($url,'"<>&');
150: } else {
151: $url = &HTML::Entities::encode($url.$anchor,'&<>"');
152: }
153: if (($exttool eq 'tab') || ($is_ext eq 'tab')) {
154: my $target;
155: if ($exttool) {
156: $target = 'LCExternalToolTab';
157: } else {
158: if ($reuse) {
159: $target = 'LCExternalResTab';
160: } else {
161: $target = '_blank';
162: }
163: if ($title ne '') {
164: $preamble = '<span style="font-weight:bold;">'.$title.'</span><br />';
165: }
166: }
167: $output .= '<div>'.$preamble.
168: '<a href="'.$url.'" target="'.$target.'" style="padding:0;clear:both;margin:0;border:0">'.
169: $linktext.'</a>'.
170: '</div>';
171: } else {
172: my ($target,$extlinkimg);
173: if ($exttool) {
174: $target = 'LCExternalToolPopUp';
175: } else {
176: $target = 'LCExternalResPopUp';
177: if ($title ne '') {
178: $preamble = '<span style="font-weight:bold;">'.$title.'</span><br />';
179: }
180: $extlinkimg = '<img src="'.&Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL').'/externallink.gif').'" width="19" height="18" border="0" />';
181: unless ($reuse) {
182: my $resid;
183: if ($env{'request.course.id'}) {
184: unless (($is_supp) || ($env{'form.folderpath'} =~ /^supplemental/)) {
185: my $symb=&Apache::lonnet::symbread($r->uri);
186: if ($symb) {
187: my $navmap = Apache::lonnavmaps::navmap->new();
188: if (ref($navmap)) {
189: my $res = $navmap->getBySymb($symb);
190: if (ref($res)) {
191: $resid = $res->id;
192: $resid =~ s/\./_/g;
193: }
194: }
195: }
196: }
197: }
198: if ($resid eq '') {
199: $resid = substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 8);
200: }
201: &js_escape(\$resid);
202: $target .= $resid;
203: }
204: }
205: $output .= <<"ENDLINK";
206: <script type="text/javascript">
207: // <![CDATA[
208: var windowObjectReference = null;
209: var PreviousUrl;
210:
211: function openSinglePopup(strUrl) {
212: if (windowObjectReference == null || windowObjectReference.closed) {
213: windowObjectReference = window.open(strUrl, "$target",
214: "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
215: } else if(PreviousUrl != strUrl) {
216: windowObjectReference = window.open(strUrl, "$target",
217: "height=$height,width=$width,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no'");
218: windowObjectReference.focus();
219: } else {
220: windowObjectReference.focus();
221: };
222: PreviousUrl = strUrl;
223: }
224: // ]]>
225: </script>
226: <div>$preamble
227: <a href="$url" target="$target" onclick="openSinglePopup(this.href); return false;">
228: $linktext$extlinkimg</a>
229: </div>
230: ENDLINK
231: }
232: if ($exttool) {
233: if ($explanation ne '') {
234: $output .= '<div>'.$explanation.'</div>';
235: }
236: }
237: } else {
238: if ($uselink) {
239: $linktext = &mt('Link to resource');
240: $output .= &create_link($url,$anchor,$title,$linktext);
241: } else {
242: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
243: $output .= '<div style="overflow:scroll; -webkit-overflow-scrolling:touch;">'."\n".
244: '<iframe src="'.$dest.'" height="100%" width="100%" frameborder="0">'."\n".
245: "$lt{'noif'} $noiframe\n".
246: "</iframe>\n".
247: "</div>\n";
248: }
249: }
250: $output .= $endpage;
251: return $output;
252: } elsif ($uselink) {
253: $linktext = &mt('Link to resource');
254: return $startpage.&create_link($url,$anchor,$title,$linktext).$endpage;
255: } else {
256: my $offset = 5;
257: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
258: if ($env{'form.inhibitmenu'} eq 'yes') {
259: $offset = 0;
260: }
261: my $script = &Apache::lonhtmlcommon::scripttag(<<SCRIPT);
262: \$(document).ready( function() {
263: \$(window).unbind('resize').resize(function(){
264: var header = null;
265: var offset = $offset;
266: var height = 0;
267: var hdrtop = 0;
268: if (\$('div.LC_head_subbox:first').length) {
269: header = \$('div.LC_head_subbox:first');
270: offset = 9;
271: } else {
272: if (\$('#LC_breadcrumbs').length) {
273: header = \$('#LC_breadcrumbs');
274: }
275: }
276: if (header != null && header.length) {
277: height = header.height();
278: hdrtop = header.position().top;
279: }
280: var pos = height + hdrtop + offset;
281: \$('.LC_iframecontainer').css('top', pos);
282: });
283: LCresizedef = 1;
284: if (LCnotready == 1) {
285: LCnotready = 0;
286: \$(window).trigger('resize');
287: }
288: });
289: window.onload = function(){
290: if (LCresizedef) {
291: LCnotready = 0;
292: \$(window).trigger('resize');
293: } else {
294: LCnotready = 1;
295: }
296: };
297: SCRIPT
298: # javascript will position the iframe if window was resized (or zoomed)
299: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
300: return <<ENDFRAME;
301: $startpage
302: $script
303: <div class="LC_iframecontainer">
304: <iframe src="$dest">$lt{'noif'} $noiframe</iframe>
305: </div>
306: $endpage
307: ENDFRAME
308: }
309: }
310:
311: sub create_link {
312: my ($url,$anchor,$title,$linktext) = @_;
313: my $shownlink;
314: if ($title eq '') {
315: $title = $env{'form.title'};
316: if ($title eq '') {
317: unless ($env{'request.enc'}) {
318: ($title) = ($url =~ m{/([^/]+)$});
319: $title =~ s/(\?[^\?]+)$//;
320: }
321: }
322: }
323: unless ($title eq '') {
324: $shownlink = '<span style="font-weight:bold;">'.$title.'</span><br />';
325: }
326: my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
327: $shownlink .= '<a href="'.$dest.'">'.$linktext.'</a>';
328: return $shownlink;
329: }
330:
331: sub handler {
332: my $r=shift;
333: &Apache::loncommon::content_type($r,'text/html');
334: $r->send_http_header;
335:
336: return OK if $r->header_only;
337:
338: my $url = $r->uri;
339: my ($is_ext,$brcrum,$absolute,$is_pdf,$exttool,$cdom,$cnum,$hostname,
340: $linktext,$explanation,$width,$height,$reuse,$is_supp);
341:
342: for ($url){
343: s|^/adm/wrapper||;
344: $is_ext = $_ =~ s|^/ext/|http://|;
345: s|http://https://|https://| if ($is_ext);
346: s|:|:|g;
347: }
348:
349: if ($url =~ /\.pdf$/i) {
350: $is_pdf = 1;
351: } elsif (($is_ext) && ($env{'request.course.id'})) {
352: if ($env{'course.'.$env{'request.course.id'}.'.extresource'}) {
353: (my $selected,$reuse,$width,$height) = split(/:/,$env{'course.'.$env{'request.course.id'}.'.extresource'});
354: if ($selected eq 'tab') {
355: $is_ext = 'tab';
356: $width = '';
357: $height = '';
358: } elsif ($selected eq 'window') {
359: $is_ext = 'window';
360: unless ($width =~ /^\d+$/) {
361: $width = '';
362: }
363: unless ($height =~ /^\d+$/) {
364: $height = '';
365: }
366: } else {
367: $width = '';
368: $height = '';
369: $reuse = '';
370: }
371: }
372: } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
373: $cdom = $1;
374: $cnum = $2;
375: my $marker = $3;
376: $exttool = 'iframe';
377: my $exttoolremote;
378: my %toolhash = &Apache::lonnet::get('exttool_'.$marker,['target','linktext','explanation','id','width','height'],
379: $cdom,$cnum);
380: if ($toolhash{'id'}) {
381: my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
382: if (ref($ltitools{$toolhash{'id'}}) eq 'HASH') {
383: $exttoolremote = $ltitools{$toolhash{'id'}}{'url'};
384: }
385: }
386: if ($toolhash{'target'} eq 'window') {
387: $exttool = 'window';
388: $width = $toolhash{'width'};
389: $height = $toolhash{'height'};
390: } elsif ($toolhash{'target'} eq 'tab') {
391: $exttool = 'tab';
392: }
393: if (($exttool eq 'window') || ($exttool eq 'tab')) {
394: $linktext = $toolhash{'linktext'};
395: $explanation = $toolhash{'explanation'};
396: } elsif (($exttoolremote =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
397: $exttool = 'tab';
398: }
399: }
400: if (($is_ext) || ($exttool)) {
401: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
402: ['forceedit','register','folderpath','symb','idx','title','anchor']);
403: if (($env{'form.forceedit'}) &&
404: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) &&
405: (($env{'form.folderpath'} =~ /^supplemental/) ||
406: ($env{'form.symb'} =~ /^uploaded/))) {
407: if ($env{'form.symb'}) {
408: (undef,undef,my $res) = &Apache::lonnet::decode_symb($env{'form.symb'});
409: if ($res =~ /(#[^#]+)$/) {
410: $url .= $1;
411: }
412: } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
413: if ($env{'form.anchor'} ne '') {
414: $url .= '#'.$env{'form.anchor'};
415: }
416: $is_supp = 1;
417: }
418: my $type = 'ext';
419: if ($exttool) {
420: $type = 'tool';
421: } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
422: $hostname = $r->hostname();
423: }
424: $r->print(
425: &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
426: $env{'form.symb'},
427: $env{'form.idx'},$type,$cdom,
428: $cnum,$hostname));
429: return OK;
430: } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
431: my $crstype = &Apache::loncommon::course_type();
432: my $title = $env{'form.title'};
433: if ($title eq '') {
434: if ($is_ext) {
435: $title = &mt('External Resource');
436: } else {
437: $title = &mt('External Tool');
438: }
439: }
440: $title = &HTML::Entities::encode($title,'\'"<>&');
441: $is_supp = 1;
442: if ($env{'request.course.id'}) {
443: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
444: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
445: &Apache::loncommon::validate_folderpath(1,'',$cnum,$cdom);
446: }
447: $brcrum =
448: &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
449: }
450: } elsif ($env{'request.course.id'}) {
451: my $courseurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'});
452: $courseurl =~ s{^/}{};
453: if ($url =~ m{^\Q/uploaded/$courseurl/supplemental/\E}) {
454: $is_supp = 1;
455: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
456: ['folderpath','title']);
457: if ($env{'form.folderpath'}) {
458: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
459: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
460: &Apache::loncommon::validate_folderpath(1,'',$cnum,$cdom);
461: }
462: my $title = $env{'form.title'};
463: $title = &HTML::Entities::encode($title,'\'"<>&');
464: my $crstype = &Apache::loncommon::course_type();
465: $brcrum =
466: &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
467:
468: }
469: }
470:
471: #
472: # Actual URL
473: #
474: if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
475: #
476: # This is uploaded homework
477: #
478: $env{'request.state'}='uploaded';
479: &Apache::lonhomework::renderpage($r,$url);
480: } else {
481: #
482: # This is not homework
483: #
484: if (($is_ext) || ($exttool)) {
485: $absolute = $env{'request.use_absolute'};
486: $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
487: $ENV{'QUERY_STRING'} =~ s/\&$//;
488: }
489:
490: unless ($ENV{'QUERY_STRING'} eq '') {
491: $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
492: }
493:
494: # encrypt url if not external
495: unless ($is_ext) {
496: &Apache::lonenc::check_encrypt(\$url);
497: }
498:
499: $r->print( wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
500: $linktext,$explanation,undef,$width,$height,$reuse,
501: $is_supp) );
502:
503: } # not just the menu
504:
505: return OK;
506: } # handler
507:
508: 1;
509: __END__
510:
511: =pod
512:
513: =head1 NAME
514:
515: Apache::lonwrapper - External and binary file management.
516:
517: =head1 SYNOPSIS
518:
519: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
520:
521: This is part of the LearningOnline Network with CAPA project
522: described at http://www.lon-capa.org.
523:
524: =head1 Subroutines
525:
526: =over
527:
528: =item wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,$title,$width,$height,$reuse,$is_supp)
529:
530: =over
531:
532: =item $r
533:
534: request object
535:
536: =item $url
537:
538: url to display either by including in an iframe within a
539: LON-CAPA page which has a standard LON-CAPA inline menu,
540: or in some cases launched in a separate tab or window,
541: launched via a link in a LON-CAPA page with standard inline
542: menu.
543:
544: =item $brcrum
545:
546: breadcrumbs for unregistered urls
547: (i.e., external resources in Supplemental Content).
548:
549: =item $absolute
550:
551: contains protocol (http or https) followed by
552: the hostname, if menu items in the standard LON-CAPA
553: interface created by the call to loncommon::start_page()
554: within &wrapper() need to use absolute URLs rather than
555: relative URLs.
556:
557: That will be the case where an external resource has been
558: served from port 80, when the server customarily serves
559: requests using Apache/SSL (i.e., port 443). mod_rewrite
560: is used to switch requests for external resources and
561: the syllabus: /public/<domain>/<courseid>/syllabus
562: (which might also point at an external resource)
563: from https:// to http:// where the the URL of the remote site
564: specified in the resource itself is http://.
565:
566: This is done to avoid default mixed content blocking
567: in Firefox 23 and later, when serving from Apache/SSL.
568:
569: =item $is_ext
570:
571: true if URL is for an external resource. Default true value
572: is 1 (display in iframe, unless $uselink is true).
573: If external resource is to be displayed in a tab,
574: value of $is_ext will be tab, if to be displayed in a pop-up window,
575: value of $is_ext will be window.
576:
577: =item $is_pdf
578:
579: true if URL is for a PDF (based on file extension).
580:
581: =item $exttool
582:
583: If URL is for an External Tool, will contain the target type: iframe, window or tab.
584:
585: =item $linktext
586:
587: optional. If URL is for an External Tool, and target type is window or tab,
588: then the link text may be an option set in the course for each tool instance,
589: or may be a default defined in the domain for all instances of the tool.
590:
591: =item $explanation
592:
593: optional. If URL is for an External Tool, and target type is window or tab,
594: then the explanation is an option set in the course for each tool instance,
595: or may be a default defined in the domain for all instances of the tool.
596:
597: =item $title
598:
599: optional. If wrapped item is a PDF, and $clientmobile is true,
600: a link to a PDF is shown. The "title" will be displayed
601: above the link, but if not provided as an arg, $env{'form.title'}
602: will be used, otherwise, the filename will be displayed (unless
603: hidden URL set for the resource).
604:
605: =item $width
606:
607: optional. If URL is for an External Tool, and target type is window,
608: then a default width may have been defined in the domain for all instances of
609: the tool. If so, that width will be used for the window opened (via a link)
610: to launch the external tool. If the URL is for an External Resource, and
611: $is_ext is window, then a default width (px) may have been defined in the current
612: course for all external resource instances.
613:
614: =item $height
615:
616: optional. If URL is for an External Tool, and target type is window,
617: then a default height may have been defined in the domain for all instances of
618: the tool. If so, that height will be used for the window opened (via a link)
619: to launch the external tool. If the URL is for an External Resource, and
620: $is_ext is window, then a default height (px) may have been defined in the current
621: course for all external resource instances.
622:
623: =item $reuse
624:
625: optional. If the URL is for an External Resource, and $is_ext is tab or window,
626: then $reuse will be true if the same tab or window is to be reused for display
627: of all external resource instances in a a course.
628:
629: =back
630:
631: Returns markup for the entire page.
632:
633: =item handler()
634:
635: Content handler for requests for: /adm/wrapper/...
636: used for content to be displayed in an iframe, or launched in a separate tab
637: or window via a link. The target URL is extracted from the requested URL, by
638: removing the /adm/wrapper prefix.
639:
640: The target URL will typically be a PDF served from the current server, an
641: external resource URL served from a different server, or an external tool
642: (from an LTI Provider) launched from LON-CAPA (as LTI Consumer) and launched
643: via a link.
644:
645: If the request included forceedit in the query string, and the requester has
646: rights to modify course content, then the editor will be displayed to allow
647: changes to be made to the resource (e.g., change the URL of the external resource,
648: or change the setting for the external tool instance).
649:
650: If not in edit mode, then the wrapper() subroutine will be called to generate the
651: standard LON-CAPA inline menu, and then either a link to launch a separate tab or
652: window, or an iframe to display the content inline.
653:
654: =back
655:
656: =cut
657:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>