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.2 2022/08/30 11:48:33 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) = @_;
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: } else {
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 ($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);
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: }
417: my $type = 'ext';
418: if ($exttool) {
419: $type = 'tool';
420: } elsif (($url =~ /^http:/) && ($ENV{'SERVER_PORT'} == 443)) {
421: $hostname = $r->hostname();
422: }
423: $r->print(
424: &Apache::lonextresedit::display_editor($url,$env{'form.folderpath'},
425: $env{'form.symb'},
426: $env{'form.idx'},$type,$cdom,
427: $cnum,$hostname));
428: return OK;
429: } elsif ($env{'form.folderpath'} =~ /^supplemental/) {
430: my $crstype = &Apache::loncommon::course_type();
431: my $title = $env{'form.title'};
432: if ($title eq '') {
433: if ($is_ext) {
434: $title = &mt('External Resource');
435: } else {
436: $title = &mt('External Tool');
437: }
438: }
439: $brcrum =
440: &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
441: }
442: }
443:
444: #
445: # Actual URL
446: #
447: if (($url=~/$LONCAPA::assess_re/) && (!$exttool)) {
448: #
449: # This is uploaded homework
450: #
451: $env{'request.state'}='uploaded';
452: &Apache::lonhomework::renderpage($r,$url);
453: } else {
454: #
455: # This is not homework
456: #
457: if (($is_ext) || ($exttool)) {
458: $absolute = $env{'request.use_absolute'};
459: $ENV{'QUERY_STRING'} =~ s/(^|\&)symb=[^\&]*/$1/;
460: $ENV{'QUERY_STRING'} =~ s/\&$//;
461: }
462:
463: unless ($ENV{'QUERY_STRING'} eq '') {
464: $url.=(($url=~/\?/)?'&':'?').$ENV{'QUERY_STRING'};
465: }
466:
467: # encrypt url if not external
468: unless ($is_ext) {
469: &Apache::lonenc::check_encrypt(\$url);
470: }
471:
472: $r->print( wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,
473: $linktext,$explanation,undef,$width,$height,$reuse) );
474:
475: } # not just the menu
476:
477: return OK;
478: } # handler
479:
480: 1;
481: __END__
482:
483: =pod
484:
485: =head1 NAME
486:
487: Apache::lonwrapper - External and binary file management.
488:
489: =head1 SYNOPSIS
490:
491: Wrapper for external and binary files as standalone resources. Edit handler for rat maps; TeX content handler.
492:
493: This is part of the LearningOnline Network with CAPA project
494: described at http://www.lon-capa.org.
495:
496: =head1 Subroutines
497:
498: =over
499:
500: =item wrapper($r,$url,$brcrum,$absolute,$is_ext,$is_pdf,$exttool,$linktext,$explanation,$title,$width,$height,$reuse)
501:
502: =over
503:
504: =item $r
505:
506: request object
507:
508: =item $url
509:
510: url to display either by including in an iframe within a
511: LON-CAPA page which has a standard LON-CAPA inline menu,
512: or in some cases launched in a separate tab or window,
513: launched via a link in a LON-CAPA page with standard inline
514: menu.
515:
516: =item $brcrum
517:
518: breadcrumbs for unregistered urls
519: (i.e., external resources in Supplemental Content).
520:
521: =item $absolute
522:
523: contains protocol (http or https) followed by
524: the hostname, if menu items in the standard LON-CAPA
525: interface created by the call to loncommon::start_page()
526: within &wrapper() need to use absolute URLs rather than
527: relative URLs.
528:
529: That will be the case where an external resource has been
530: served from port 80, when the server customarily serves
531: requests using Apache/SSL (i.e., port 443). mod_rewrite
532: is used to switch requests for external resources and
533: the syllabus: /public/<domain>/<courseid>/syllabus
534: (which might also point at an external resource)
535: from https:// to http:// where the the URL of the remote site
536: specified in the resource itself is http://.
537:
538: This is done to avoid default mixed content blocking
539: in Firefox 23 and later, when serving from Apache/SSL.
540:
541: =item $is_ext
542:
543: true if URL is for an external resource. Default true value
544: is 1 (display in iframe, unless $uselink is true).
545: If external resource is to be displayed in a tab,
546: value of $is_ext will be tab, if to be displayed in a pop-up window,
547: value of $is_ext will be window.
548:
549: =item $is_pdf
550:
551: true if URL is for a PDF (based on file extension).
552:
553: =item $exttool
554:
555: If URL is for an External Tool, will contain the target type: iframe, window or tab.
556:
557: =item $linktext
558:
559: optional. If URL is for an External Tool, and target type is window or tab,
560: then the link text may be an option set in the course for each tool instance,
561: or may be a default defined in the domain for all instances of the tool.
562:
563: =item $explanation
564:
565: optional. If URL is for an External Tool, and target type is window or tab,
566: then the explanation is an option set in the course for each tool instance,
567: or may be a default defined in the domain for all instances of the tool.
568:
569: =item $title
570:
571: optional. If wrapped item is a PDF, and $clientmobile is true,
572: a link to a PDF is shown. The "title" will be displayed
573: above the link, but if not provided as an arg, $env{'form.title'}
574: will be used, otherwise, the filename will be displayed (unless
575: hidden URL set for the resource).
576:
577: =item $width
578:
579: optional. If URL is for an External Tool, and target type is window,
580: then a default width may have been defined in the domain for all instances of
581: the tool. If so, that width will be used for the window opened (via a link)
582: to launch the external tool. If the URL is for an External Resource, and
583: $is_ext is window, then a default width (px) may have been defined in the current
584: course for all external resource instances.
585:
586: =item $height
587:
588: optional. If URL is for an External Tool, and target type is window,
589: then a default height may have been defined in the domain for all instances of
590: the tool. If so, that height will be used for the window opened (via a link)
591: to launch the external tool. If the URL is for an External Resource, and
592: $is_ext is window, then a default height (px) may have been defined in the current
593: course for all external resource instances.
594:
595: =item $reuse
596:
597: optional. If the URL is for an External Resource, and $is_ext is tab or window,
598: then $reuse will be true if the same tab or window is to be reused for display
599: of all external resource instances in a a course.
600:
601: =back
602:
603: Returns markup for the entire page.
604:
605: =item handler()
606:
607: Content handler for requests for: /adm/wrapper/...
608: used for content to be displayed in an iframe, or launched in a separate tab
609: or window via a link. The target URL is extracted from the requested URL, by
610: removing the /adm/wrapper prefix.
611:
612: The target URL will typically be a PDF served from the current server, an
613: external resource URL served from a different server, or an external tool
614: (from an LTI Provider) launched from LON-CAPA (as LTI Consumer) and launched
615: via a link.
616:
617: If the request included forceedit in the query string, and the requester has
618: rights to modify course content, then the editor will be displayed to allow
619: changes to be made to the resource (e.g., change the URL of the external resource,
620: or change the setting for the external tool instance).
621:
622: If not in edit mode, then the wrapper() subroutine will be called to generate the
623: standard LON-CAPA inline menu, and then either a link to launch a separate tab or
624: window, or an iframe to display the content inline.
625:
626: =back
627:
628: =cut
629:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>