File:
[LON-CAPA] /
loncom /
publisher /
lonupload.pm
Revision
1.64:
download - view:
text,
annotated -
select for diffs
Wed Jul 3 05:03:19 2013 UTC (11 years ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Bug 6168 (a part).
- Display warning if copying file would exceed quota in authoring space
- Testing if quota would be exceeded by file upload moved from lonupload.pm
to &excess_filesize_authorspace() routine in loncommon.pm to facilitate reuse.
1: # The LearningOnline Network with CAPA
2: # Handler to upload files into construction space
3: #
4: # $Id: lonupload.pm,v 1.64 2013/07/03 05:03:19 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: =head1 NAME
31:
32: Apache::lonupload - upload files into construction space
33:
34: =head1 SYNOPSIS
35:
36: Invoked by /etc/httpd/conf/srm.conf:
37:
38: <Location /adm/upload>
39: PerlAccessHandler Apache::lonacc
40: SetHandler perl-script
41: PerlHandler Apache::lonupload
42: ErrorDocument 403 /adm/login
43: ErrorDocument 404 /adm/notfound.html
44: ErrorDocument 406 /adm/unauthorized.html
45: ErrorDocument 500 /adm/errorhandler
46: </Location>
47:
48: =head1 INTRODUCTION
49:
50: This module uploads a file sitting on a client computer into
51: library server construction space.
52:
53: This is part of the LearningOnline Network with CAPA project
54: described at http://www.lon-capa.org.
55:
56: =head1 HANDLER SUBROUTINE
57:
58: This routine is called by Apache and mod_perl.
59:
60: =over 4
61:
62: =item *
63:
64: Initialize variables
65:
66: =item *
67:
68: Start page output
69:
70: =item *
71:
72: output relevant interface phase (phaseone, phasetwo, phasethree or phasefour)
73:
74: =item *
75:
76: (phase one is to specify upload file; phase two is to handle conditions
77: subsequent to specification--like overwriting an existing file; phase three
78: is to handle processing of secondary uploads - of embedded objects in an
79: html file).
80:
81: =back
82:
83: =head1 OTHER SUBROUTINES
84:
85: =over
86:
87: =item phaseone()
88:
89: Interface for specifying file to upload.
90:
91: =item phasetwo()
92:
93: Interface for handling post-conditions about uploading (such
94: as overwriting an existing file).
95:
96: =item phasethree()
97:
98: Interface for handling secondary uploads of embedded objects
99: in an html file.
100:
101: =item phasefour()
102:
103: Interface for handling optional renaming of links to embedded
104: objects.
105:
106: =item upfile_store()
107:
108: Store contents of uploaded file into temporary space. Invoked
109: by phaseone subroutine.
110:
111: =item check_extension()
112:
113: Checks if filename extension is permitted and checks type
114: of file - if html file, calls parser to check for embedded objects.
115: Invoked by phasetwo subroutine.
116:
117: =back
118:
119: =cut
120:
121: package Apache::lonupload;
122:
123: use strict;
124: use Apache::File;
125: use File::Copy;
126: use File::Basename;
127: use Apache::Constants qw(:common :http :methods);
128: use Apache::loncommon();
129: use Apache::lonnet;
130: use HTML::Entities();
131: use Apache::lonlocal;
132: use Apache::lonnet;
133: use LONCAPA();
134:
135: my $DEBUG=0;
136:
137: sub Debug {
138: # Put out the indicated message but only if DEBUG is true.
139: if ($DEBUG) {
140: my ($r,$message) = @_;
141: $r->log_reason($message);
142: }
143: }
144:
145: sub upfile_store {
146: my $r=shift;
147:
148: my $fname=$env{'form.upfile.filename'};
149: $fname=~s/\W//g;
150:
151: chomp($env{'form.upfile'});
152:
153: my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
154: '_upload_'.$fname.'_'.time.'_'.$$;
155: {
156: my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
157: '/tmp/'.$datatoken.'.tmp');
158: print $fh $env{'form.upfile'};
159: }
160: return $datatoken;
161: }
162:
163: sub phaseone {
164: my ($r,$fn,$mode,$uname,$udom)=@_;
165: my $action = '/adm/upload';
166: if ($mode eq 'testbank') {
167: $action = '/adm/testbank';
168: } elsif ($mode eq 'imsimport') {
169: $action = '/adm/imsimport';
170: }
171:
172: # Check for file to be uploaded
173: $env{'form.upfile.filename'}=~s/\\/\//g;
174: $env{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
175: if (!$env{'form.upfile.filename'}) {
176: $r->print('<p class="LC_warning">'.&mt('No upload file specified.').'</p>'.
177: &earlyout($fn,$uname,$udom));
178: return;
179: }
180:
181: # Append the name of the uploaded file
182: $fn.=$env{'form.upfile.filename'};
183: $fn=~s/(\/)+/\//g;
184:
185: # Check for illegal filename
186: &Debug($r, "Filename for upload: $fn");
187: if (!(($fn) && ($fn!~/\/$/))) {
188: $r->print('<p class="LC_warning">'.&mt('Illegal filename.').'</p>');
189: return;
190: }
191: # Check if quota exceeded
192: my $filesize = length($env{'form.upfile'});
193: if (!$filesize) {
194: $r->print('<p class="LC_warning">'.
195: &mt('Unable to upload [_1]. (size = [_2] bytes)',
196: '<span class="LC_filename">'.$env{'form.upfile.filename'}.'</span>',
197: $filesize).'<br />'.
198: &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
199: '</p>'.
200: &earlyout($fn,$uname,$udom));
201: return;
202: }
203: $filesize = int($filesize/1000); #expressed in kb
204: my $authorspace = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname";
205: my $output = &Apache::loncommon::excess_filesize_authorspace($uname,$udom,$authorspace,
206: $env{'form.upfile.filename'},$filesize,'upload');
207: if ($output) {
208: $r->print($output.&earlyout($fn,$uname,$udom));
209: return;
210: }
211:
212: # Split part that I can change from the part that I cannot change
213: my ($fn1,$fn2)=($fn=~/^(\/priv\/[^\/]+\/[^\/]+\/)(.*)$/);
214: # Display additional options for upload
215: # and upload button
216: $r->print(
217: '<form action="'.$action.'" method="post" name="fileupload">'
218: .'<input type="hidden" name="phase" value="two" />'
219: .'<input type="hidden" name="datatoken" value="'.&upfile_store.'" />'
220: );
221: $r->print(
222: &Apache::lonhtmlcommon::start_pick_box()
223: .&Apache::lonhtmlcommon::row_title(&mt('Save uploaded file as'))
224: .'<span class="LC_filename">'.$fn1.'</span>'
225: .'<input type="hidden" name="filename1" value="'.$fn1.'" />'
226: .'<input type="text" size="50" name="filename2" value="'.$fn2.'" />'
227: .&Apache::lonhtmlcommon::row_closure()
228: .&Apache::lonhtmlcommon::row_title(&mt('File Type'))
229: .'<select name="filetype">'
230: .'<option value="standard" selected="selected">'.&mt('Regular file').'</option>'
231: .'<option value="testbank">'.&mt('Testbank file').'</option>'
232: .'<option value="imsimport">'.&mt('IMS package').'</option>'
233: .'</select>'.&Apache::loncommon::help_open_topic("Uploading_File_Options")
234: .&Apache::lonhtmlcommon::row_closure(1)
235: .&Apache::lonhtmlcommon::end_pick_box()
236: );
237: $r->print(
238: '<p>'
239: .'<input type="button" value="'.&mt('Upload').'" onclick="javascript:verifyForm()"/>'
240: .'</p>'
241: .'</form>'
242: );
243:
244: # Check for bad extension and warn user
245: if ($fn=~/\.(\w+)$/ &&
246: (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
247: $r->print('<p class="LC_error">'
248: .&mt('The extension on this file, [_1], is reserved internally by LON-CAPA.',
249: '<span class="LC_filename">'.$1.'</span>')
250: .' <br />'.&mt('Please change the extension.')
251: .'</p>');
252: } elsif($fn=~/\.(\w+)$/ &&
253: !defined(&Apache::loncommon::fileembstyle($1))) {
254: $r->print('<p class="LC_error">'
255: .&mt('The extension on this file, [_1], is not recognized by LON-CAPA.',
256: '<span class="LC_filename">'.$1.'</span>')
257: .' <br />'.&mt('Please change the extension.')
258: .'</p>');
259: }
260: }
261:
262: sub phasetwo {
263: my ($r,$fn,$mode)=@_;
264:
265: my $output;
266: my $action = '/adm/upload';
267: my $returnflag = '';
268: if ($mode eq 'testbank') {
269: $action = '/adm/testbank';
270: } elsif ($mode eq 'imsimport') {
271: $action = '/adm/imsimport';
272: }
273: $fn=~s/\/+/\//g;
274: if ($fn) {
275: my $target= $r->dir_config('lonDocRoot').'/'.$fn;
276: &Debug($r, "target -> ".$target);
277: # target is the full filesystem path of the destination file.
278: my $base = &File::Basename::basename($fn);
279: my $path = &File::Basename::dirname($fn);
280: $base = &HTML::Entities::encode($base,'<>&"');
281: my $url = $path."/".$base;
282: &Debug($r, "URL is now ".$url);
283: my $datatoken=$env{'form.datatoken'};
284: if (($fn) && ($datatoken)) {
285: if ($env{'form.cancel'}) {
286: my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
287: my $dirpath=$path.'/';
288: $dirpath=~s/\/+/\//g;
289: $output .= '<p class="LC_warning">'.&mt('Upload cancelled.').'</p>'
290: .'<p><a href="'.$dirpath.'">'.
291: &mt('Back to Directory').'</a></p>';
292: } elsif ((-e $target) && (!$env{'form.override'})) {
293: $output .= '<form action="'.$action.'" method="post">'
294: .'<p class="LC_warning">'
295: .&mt('File [_1] already exists.',
296: '<span class="LC_filename">'.$fn.'</span>')
297: .'<input type="hidden" name="phase" value="two" />'
298: .'<input type="hidden" name="filename" value="'.$url.'" />'
299: .'<input type="hidden" name="datatoken" value="'.$datatoken.'" />'
300: .'<p>'
301: .'<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'
302: .' <input type="submit" name="override" value="'.&mt('Overwrite').'" />'
303: .'</p>'
304: .'</form>';
305: } else {
306: my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
307: my $dirpath=$path.'/';
308: $dirpath=~s/\/+/\//g;
309: # Check for bad extension and disallow upload
310: my $result;
311: ($result,$returnflag) = &check_extension($fn,$mode,$source,$target,$action,$dirpath,$url);
312: $output .= $result;
313: }
314: } else {
315: $output .= '<span class="LC_error">'.
316: &mt('Please use browser "Back" button and pick a filename').
317: '</span><br />';
318: }
319: } else {
320: $output .= '<span class="LC_error">'.
321: &mt('Please use browser "Back" button and pick a filename').
322: '</span><br />';
323: }
324: return ($output,$returnflag);
325: }
326:
327: sub check_extension {
328: my ($fn,$mode,$source,$target,$action,$dirpath,$url) = @_;
329: my ($result,$returnflag);
330: # Check for bad extension and disallow upload
331: if ($fn=~/\.(\w+)$/ &&
332: (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
333: $result .= '<p class="LC_warning">'.
334: &mt('File [_1] could not be copied.',
335: '<span class="LC_filename">'.$fn.'</span> ').
336: '<br />'.
337: &mt('The extension on this file is reserved internally by LON-CAPA.').
338: '</p>';
339: } elsif ($fn=~/\.(\w+)$/ &&
340: !defined(&Apache::loncommon::fileembstyle($1))) {
341: $result .= '<p class="LC_warning">'.
342: &mt('File [_1] could not be copied.',
343: '<span class="LC_filename">'.$fn.'</span> ').
344: '<br />'.
345: &mt('The extension on this file is not recognized by LON-CAPA.').
346: '</p>';
347: } elsif (-d $target) {
348: $result .= '<p class="LC_warning">'.
349: &mt('File [_1] could not be copied.',
350: '<span class="LC_filename">'.$fn.'</span>').
351: '<br />'.
352: &mt('The target is an existing directory.').
353: '</p>';
354: } elsif (copy($source,$target)) {
355: chmod(0660, $target); # Set permissions to rw-rw---.
356: if ($mode eq 'testbank' || $mode eq 'imsimport') {
357: $returnflag = 'ok';
358: $result .= '<p class="LC_success">'
359: .&mt('Your file - [_1] - was uploaded successfully.',
360: '<span class="LC_filename">'.$fn.'<span>')
361: .'</p>';
362: } else {
363: $result .= '<p class="LC_success">'
364: .&mt('File copied.')
365: .'</p>';
366: }
367: # Check for embedded objects.
368: my (%allfiles,%codebase);
369: my ($text,$header,$css,$js);
370: if (($mode ne 'imsimport') && ($target =~ /\.(htm|html|shtml)$/i)) {
371: my (%allfiles,%codebase);
372: &Apache::lonnet::extract_embedded_items($target,\%allfiles,\%codebase);
373: if (keys(%allfiles) > 0) {
374: my ($currentpath) = ($url =~ m{^(.+)/[^/]+$});
375: my $state = &embedded_form_elems('upload_embedded',$url,$mode);
376: my ($embedded,$num,$pathchg) =
377: &Apache::loncommon::ask_for_embedded_content($action,$state,\%allfiles,
378: \%codebase,
379: {'error_on_invalid_names' => 1,
380: 'ignore_remote_references' => 1,
381: 'current_path' => $currentpath});
382: if ($embedded) {
383: $result .= '<h3>'.&mt('Reference Warning').'</h3>';
384: if ($num) {
385: $result .= '<p>'.&mt('Completed upload of the file.').' '.&mt('This file contained references to other files.').'</p>'.
386: '<p>'.&mt('Please select the locations from which the referenced files are to be uploaded.').'</p>'.
387: $embedded;
388: if ($mode eq 'testbank') {
389: $returnflag = 'embedded';
390: $result .= '<p>'.&mt('Or [_1]continue[_2] the testbank import without these files.','<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
391: }
392: } else {
393: $result .= '<p>'.&mt('Completed upload of the file.').'</p>'.$embedded;
394: if ($pathchg) {
395: if ($mode eq 'testbank') {
396: $returnflag = 'embedded';
397: $result .= '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the references(s).','<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
398: }
399: }
400: }
401: }
402: }
403: }
404: if (($mode ne 'imsimport') && ($mode ne 'testbank')) {
405: $result .= '<br /><a href="'.$url.'">'.
406: &mt('View file').'</a>';
407: }
408: } else {
409: $result .= &mt('Failed to copy: [_1].',$!);
410: }
411: if ($mode ne 'imsimport' && $mode ne 'testbank') {
412: $result .= '<br /><a href="'.$dirpath.'">'.
413: &mt('Back to Directory').'</a><br />';
414: }
415: return ($result,$returnflag);
416: }
417:
418: sub phasethree {
419: my ($r,$fn,$uname,$udom,$mode) = @_;
420:
421: my $action = '/adm/upload';
422: if ($mode eq 'testbank') {
423: $action = '/adm/testbank';
424: } elsif ($mode eq 'imsimport') {
425: $action = '/adm/imsimport';
426: }
427: my $url_root = "/priv/$udom/$uname";
428: my $dir_root = $r->dir_config('lonDocRoot').$url_root;
429: my $path = &File::Basename::dirname($fn);
430: $path =~ s{^\Q$url_root\E}{};
431: my $filename = &HTML::Entities::encode($env{'form.filename'},'<>&"');
432: my $state = &embedded_form_elems('modify_orightml',$filename,$mode).
433: '<input type="hidden" name="phase" value="four" />';
434: my ($result,$returnflag) =
435: &Apache::loncommon::upload_embedded($mode,$path,$uname,$udom,
436: $dir_root,$url_root,undef,
437: undef,undef,$state,$action);
438: if ($mode ne 'imsimport' && $mode ne 'testbank') {
439: $result .= '<br /><h3><a href="'.$fn.'">'.
440: &mt('View main file').'</a></h3>'.
441: '<h3><a href="'.$url_root.$path.'">'.
442: &mt('Back to Directory').'</a></h3><br />';
443: }
444: return ($result,$returnflag);
445: }
446:
447: sub embedded_form_elems {
448: my ($action,$filename,$mode) = @_;
449: return <<STATE;
450: <input type="hidden" name="action" value="$action" />
451: <input type="hidden" name="mode" value="$mode" />
452: <input type="hidden" name="filename" value="$filename" />
453: STATE
454: }
455:
456: sub phasefour {
457: my ($r,$fn,$uname,$udom,$mode) = @_;
458:
459: my $action = '/adm/upload';
460: if ($mode eq 'testbank') {
461: $action = '/adm/testbank';
462: } elsif ($mode eq 'imsimport') {
463: $action = '/adm/imsimport';
464: }
465: my $result;
466: my $url_root = "/priv/$udom/$uname";
467: my $dir_root = $r->dir_config('lonDocRoot').$url_root;
468: my $path = &File::Basename::dirname($fn);
469: $path =~ s{^\Q$url_root\E}{};
470: my $outcome =
471: &Apache::loncommon::modify_html_refs($mode,$path,$uname,$udom,$dir_root);
472: $result .= $outcome;
473: if ($mode ne 'imsimport' && $mode ne 'testbank') {
474: $result .= '<br /><h3><a href="'.$fn.'">'.
475: &mt('View main file').'</a></h3>'.
476: '<h3><a href="'.$url_root.$path.'">'.
477: &mt('Back to Directory').'</a></h3><br />';
478: }
479: return $result;
480: }
481:
482: sub earlyout {
483: my ($fn,$uname,$udom) = @_;
484: if ($fn =~ m{^(/priv/$udom/$uname(?:.*)/)[^/]*}) {
485: return &Apache::lonhtmlcommon::actionbox(
486: ['<a href="'.$1.'">'.&mt('Return to Directory').'</a>']);
487: }
488: return;
489: }
490:
491: # ---------------------------------------------------------------- Main Handler
492: sub handler {
493:
494: my $r=shift;
495: my $javascript = '';
496: my $fn=$env{'form.filename'};
497:
498: if ($env{'form.filename1'}) {
499: $fn=$env{'form.filename1'}.$env{'form.filename2'};
500: }
501: $fn=~s/\/+/\//g;
502:
503: unless ($fn) {
504: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
505: ' unspecified filename for upload', $r->filename);
506: return HTTP_NOT_FOUND;
507: }
508:
509: my ($uname,$udom)=&Apache::lonnet::constructaccess($fn);
510:
511: unless (($uname) && ($udom)) {
512: $r->log_reason($uname.' at '.$udom.
513: ' trying to publish file '.$env{'form.filename'}.
514: ' - not authorized',
515: $r->filename);
516: return HTTP_NOT_ACCEPTABLE;
517: }
518:
519: # ----------------------------------------------------------- Start page output
520:
521: &Apache::loncommon::content_type($r,'text/html');
522: $r->send_http_header;
523:
524: unless ($env{'form.phase'} eq 'two') {
525: $javascript = <<"ENDJS";
526: <script type="text/javascript">
527: // <![CDATA[
528: function verifyForm() {
529: var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
530: if (mode == "testbank") {
531: document.fileupload.action = "/adm/testbank";
532: }
533: if (mode == "imsimport") {
534: document.fileupload.action = "/adm/imsimport";
535: }
536: if (mode == "standard") {
537: document.fileupload.action = "/adm/upload";
538: }
539: document.fileupload.submit();
540: }
541: // ]]>
542: </script>
543: ENDJS
544: }
545:
546: my $londocroot = $r->dir_config('lonDocRoot');
547: my $trailfile = $fn;
548: $trailfile =~ s{^/(priv/)}{$londocroot/$1};
549:
550: # Breadcrumbs
551: my $brcrum = [{'href' => &Apache::loncommon::authorspace($fn),
552: 'text' => 'Authoring Space'},
553: {'href' => '/adm/upload',
554: 'text' => 'Upload file to Authoring Space'}];
555: $r->print(&Apache::loncommon::start_page('Upload file to Authoring Space',
556: $javascript,
557: {'bread_crumbs' => $brcrum,})
558: .&Apache::loncommon::head_subbox(
559: &Apache::loncommon::CSTR_pageheader($trailfile))
560: );
561:
562: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
563: $r->print('<p class="LC_info">'
564: .&mt('Co-Author [_1]',$uname.':'.$udom)
565: .'</p>'
566: );
567: }
568: if ($env{'form.phase'} eq 'four') {
569: my $output = &phasefour($r,$fn,$uname,$udom,'author');
570: $r->print($output);
571: } elsif ($env{'form.phase'} eq 'three') {
572: my ($output,$rtnflag) = &phasethree($r,$fn,$uname,$udom,'author');
573: $r->print($output);
574: } elsif ($env{'form.phase'} eq 'two') {
575: my ($output,$returnflag) = &phasetwo($r,$fn);
576: $r->print($output);
577: } else {
578: &phaseone($r,$fn,undef,$uname,$udom);
579: }
580:
581: $r->print(&Apache::loncommon::end_page());
582: return OK;
583: }
584:
585: 1;
586: __END__
587:
588:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>