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