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