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