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