Annotation of loncom/publisher/lonupload.pm, revision 1.36
1.12 foxr 1:
1.1 www 2: # The LearningOnline Network with CAPA
3: # Handler to upload files into construction space
4: #
1.36 ! www 5: # $Id: lonupload.pm,v 1.35 2007/05/02 01:34:23 albertel Exp $
1.8 matthew 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
1.10 harris41 29: ###
1.1 www 30:
31: package Apache::lonupload;
32:
33: use strict;
34: use Apache::File;
35: use File::Copy;
1.13 foxr 36: use File::Basename;
1.1 www 37: use Apache::Constants qw(:common :http :methods);
1.3 www 38: use Apache::loncacc;
1.10 harris41 39: use Apache::loncommon();
1.13 foxr 40: use Apache::lonnet;
1.14 foxr 41: use HTML::Entities();
1.20 www 42: use Apache::lonlocal;
1.29 albertel 43: use Apache::lonnet;
1.34 albertel 44: use LONCAPA();
1.12 foxr 45:
46: my $DEBUG=0;
47:
48: sub Debug {
1.30 albertel 49: # Put out the indicated message but only if DEBUG is true.
1.22 albertel 50: if ($DEBUG) {
1.30 albertel 51: my ($r,$message) = @_;
52: $r->log_reason($message);
1.22 albertel 53: }
1.12 foxr 54: }
1.1 www 55:
1.2 www 56: sub upfile_store {
57: my $r=shift;
58:
1.29 albertel 59: my $fname=$env{'form.upfile.filename'};
1.2 www 60: $fname=~s/\W//g;
61:
1.29 albertel 62: chomp($env{'form.upfile'});
1.1 www 63:
1.29 albertel 64: my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
1.2 www 65: '_upload_'.$fname.'_'.time.'_'.$$;
66: {
67: my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
68: '/tmp/'.$datatoken.'.tmp');
1.29 albertel 69: print $fh $env{'form.upfile'};
1.1 www 70: }
1.2 www 71: return $datatoken;
72: }
73:
74:
75: sub phaseone {
1.25 raeburn 76: my ($r,$fn,$uname,$udom,$mode)=@_;
77: my $action = '/adm/upload';
78: if ($mode eq 'testbank') {
79: $action = '/adm/testbank';
80: } elsif ($mode eq 'imsimport') {
81: $action = '/adm/imsimport';
82: }
1.29 albertel 83: $env{'form.upfile.filename'}=~s/\\/\//g;
84: $env{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
85: if ($env{'form.upfile.filename'}) {
1.22 albertel 86: $fn=~s/\/[^\/]+$//;
87: $fn=~s/([^\/])$/$1\//;
1.29 albertel 88: $fn.=$env{'form.upfile.filename'};
1.22 albertel 89: $fn=~s/^\///;
90: $fn=~s/(\/)+/\//g;
1.13 foxr 91:
92: # Fn is the full path to the destination filename.
93: #
94:
1.22 albertel 95: &Debug($r, "Filename for upload: $fn");
96: if (($fn) && ($fn!~/\/$/)) {
1.28 raeburn 97: $r->print('<form action="'.$action.'" method="post" name="fileupload">'.
1.23 albertel 98: '<input type="hidden" name="phase" value="two" />'.
99: '<input type="hidden" name="datatoken" value="'.
100: &upfile_store.'" />'.
101: '<input type="hidden" name="uploaduname" value="'.$uname.
1.35 albertel 102: '" />'.&mt('Save uploaded file as ').
1.33 albertel 103: "<span class='LC_filename'>/priv/$uname/</span>".
1.25 raeburn 104: '<input type="text" size="50" name="filename" value="'.$fn.
1.33 albertel 105: '" /><br />'.
106: '<br />'.&mt('Choose file type:').'
1.25 raeburn 107: <select name="filetype">
108: <option value="standard" selected>'.&mt('Regular file').'
109: <option value="testbank">'.&mt('Testbank file').'
110: <option value="imsimport">'.&mt('IMS package').'
1.33 albertel 111: </select>'.&Apache::loncommon::help_open_topic("Uploading_File_Options").'
1.25 raeburn 112: <br />
113: <br />
114: ');
1.35 albertel 115: $r->print('<input type="button" value="'.&mt('Save').'" onClick="javascript:verifyForm()"/></form>');
1.22 albertel 116: # Check for bad extension and warn user
117: if ($fn=~/\.(\w+)$/ &&
118: (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.33 albertel 119: $r->print('<span class="LC_error">'.&mt('The extension on this file,').
1.22 albertel 120: ' "'.$1.'"'.&mt(', is reserved internally by LON-CAPA.').
1.33 albertel 121: ' <br />'.&mt('Please change the extension.').'</span>');
1.22 albertel 122: } elsif($fn=~/\.(\w+)$/ &&
123: !defined(&Apache::loncommon::fileembstyle($1))) {
1.33 albertel 124: $r->print('<span class="LC_error">'.&mt('The extension on this file,').
1.22 albertel 125: ' "'.$1.'"'.&mt(', is not recognized by LON-CAPA.').
1.23 albertel 126: ' <br />'.&mt('Please change the extension.').
1.33 albertel 127: '</span>');
1.22 albertel 128: }
129: } else {
1.33 albertel 130: $r->print('<span class="LC_error">'.&mt('Illegal filename.').'</span>');
1.22 albertel 131: }
132: } else {
1.33 albertel 133: $r->print('<span class="LC_error">'.&mt('No upload file specified.').'</span>');
1.22 albertel 134: }
1.1 www 135: }
136:
137: sub phasetwo {
1.25 raeburn 138: my ($r,$tfn,$uname,$udom,$mode)=@_;
139: my $action = '/adm/upload';
140: my $returnflag = '';
141: if ($mode eq 'testbank') {
142: $action = '/adm/testbank';
143: } elsif ($mode eq 'imsimport') {
144: $action = '/adm/imsimport';
145: }
1.22 albertel 146: my $fn='/priv/'.$uname.'/'.$tfn;
147: $fn=~s/\/+/\//g;
148: &Debug($r, "Filename is ".$tfn);
149: if ($tfn) {
150: &Debug($r, "Filename for tfn = ".$tfn);
151: my $target='/home/'.$uname.'/public_html'.$tfn;
152: &Debug($r, "target -> ".$target);
1.13 foxr 153: # target is the full filesystem path of the destination file.
1.22 albertel 154: my $base = &File::Basename::basename($fn);
155: my $path = &File::Basename::dirname($fn);
1.26 albertel 156: $base = &HTML::Entities::encode($base,'<>&"');
1.22 albertel 157: my $url = $path."/".$base;
158: &Debug($r, "URL is now ".$url);
1.29 albertel 159: my $datatoken=$env{'form.datatoken'};
1.22 albertel 160: if (($fn) && ($datatoken)) {
1.36 ! www 161: if ($env{'form.cancel'}) {
! 162: my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
! 163: my $dirpath=$path.'/';
! 164: $dirpath=~s/\/+/\//g;
! 165: $r->print(&mt('Upload cancelled.').'<br /><font size="+2"><a href="'.$dirpath.'">'.
! 166: &mt('Back to Directory').'</a></font>');
! 167: } elsif ((-e $target) && (!$env{'form.override'})) {
1.25 raeburn 168: $r->print('<form action="'.$action.'" method="post">'.
1.36 ! www 169: &mt('File [_1] exists. Overwrite?','<span class="LC_filename">'.$fn.'</span>').
1.23 albertel 170: '<input type="hidden" name="phase" value="two" />'.
171: '<input type="hidden" name="filename" value="'."$url".'" />'.
172: '<input type="hidden" name="datatoken" value="'.$datatoken.'" />'.
1.36 ! www 173: '<input type="submit" name="override" value="'.&mt('Yes').'" />'.
! 174: '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
! 175: '</form>');
! 176: } else {
1.22 albertel 177: my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
1.27 www 178: my $dirpath=$path.'/';
179: $dirpath=~s/\/+/\//g;
1.22 albertel 180: # Check for bad extension and disallow upload
181: if ($fn=~/\.(\w+)$/ &&
182: (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.33 albertel 183: $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.
1.22 albertel 184: &mt('could not be copied.').'<br />'.
1.33 albertel 185: '<span class="LC_error">'.
1.22 albertel 186: &mt('The extension on this file is reserved internally by LON-CAPA.').
1.33 albertel 187: '</span>');
1.27 www 188: $r->print('<br /><font size=+2><a href="'.$dirpath.'">'.
1.22 albertel 189: &mt('Back to Directory').'</a></font>');
190: } elsif ($fn=~/\.(\w+)$/ &&
191: !defined(&Apache::loncommon::fileembstyle($1))) {
1.33 albertel 192: $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.
1.22 albertel 193: &mt('could not be copied.').'<br />'.
1.33 albertel 194: '<span class="LC_error">'.
1.22 albertel 195: &mt('The extension on this file is not recognized by LON-CAPA.').
1.33 albertel 196: '</span>');
1.27 www 197: $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
1.22 albertel 198: &mt('Back to Directory').'</a></font>');
199: } elsif (-d $target) {
1.33 albertel 200: $r->print('File <span class="LC_filename">'.$fn.'</span> could not be copied.<br />'.
201: '<span class="LC_error">'.
1.22 albertel 202: &mt('The target is an existing directory.').
1.33 albertel 203: '</span><br />');
1.27 www 204: $r->print('<font size="+2"><a href="'.$dirpath.'">'.
1.22 albertel 205: &mt('Back to Directory').'</a></font>');
206: } elsif (copy($source,$target)) {
207: chmod(0660, $target); # Set permissions to rw-rw---.
1.25 raeburn 208: if ($mode eq 'testbank' || $mode eq 'imsimport') {
209: $r->print(&mt("Your file - $fn - was uploaded successfully")."<br /><br />");
210: $returnflag = 'ok';
211: } else {
212: $r->print(&mt('File copied.'));
213: $r->print('<br /><font size="+2"><a href="'.$url.'">'.
1.22 albertel 214: &mt('View file').'</a></font>');
1.27 www 215: $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
1.23 albertel 216: &mt('Back to Directory').'</a></font><br />');
1.25 raeburn 217: }
1.22 albertel 218: } else {
219: $r->print('Failed to copy: '.$!);
1.23 albertel 220: $r->print('<br /><font size="+2"><a href="'.$path.'">'.
1.22 albertel 221: &mt('Back to Directory').'</a></font>');
222: }
223: }
224: } else {
1.33 albertel 225: $r->print('<span class="LC_error">'.
1.22 albertel 226: &mt('Please use browser "Back" button and pick a filename').
1.33 albertel 227: '</span><br />');
1.22 albertel 228: }
1.1 www 229: } else {
1.33 albertel 230: $r->print('<span class="LC_error">'.
1.22 albertel 231: &mt('Please use browser "Back" button and pick a filename').
1.33 albertel 232: '</span><br />>');
1.1 www 233: }
1.25 raeburn 234: return $returnflag;
1.1 www 235: }
236:
1.10 harris41 237: # ---------------------------------------------------------------- Main Handler
1.1 www 238: sub handler {
239:
1.22 albertel 240: my $r=shift;
1.1 www 241:
1.22 albertel 242: my $uname;
243: my $udom;
1.25 raeburn 244: my $javascript = '';
1.18 www 245: #
246: # phase two: re-attach user
247: #
1.29 albertel 248: if ($env{'form.uploaduname'}) {
249: $env{'form.filename'}='/priv/'.$env{'form.uploaduname'}.'/'.
250: $env{'form.filename'};
1.22 albertel 251: }
252:
1.29 albertel 253: unless ($env{'form.phase'} eq 'two') {
1.32 albertel 254: my %body_layout = ('rightmargin' => "0",
255: 'leftmargin' => "0",
256: 'marginwidth' => "0",
257: 'topmargin' => "0",
258: 'marginheight' => "0");
1.31 albertel 259: my $start_page =
260: &Apache::loncommon::start_page('Importing a Testbank file into LON-CAPA',
261: undef,
262: {'only_body' => 1,
1.32 albertel 263: 'add_entries' => \%body_layout,
1.31 albertel 264: 'js_ready' => 1,});
265: my $end_page =
266: &Apache::loncommon::end_page({'js_ready' => 1,});
267:
1.25 raeburn 268: $javascript = qq|
269: function verifyForm() {
1.28 raeburn 270: var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
1.25 raeburn 271: if (mode == "testbank") {
1.28 raeburn 272: document.fileupload.action = "/adm/testbank";
1.25 raeburn 273: }
274: if (mode == "imsimport") {
1.28 raeburn 275: document.fileupload.action = "/adm/imsimport";
1.25 raeburn 276: }
277: if (mode == "standard") {
1.28 raeburn 278: document.fileupload.action = "/adm/upload";
1.25 raeburn 279: }
1.28 raeburn 280: document.fileupload.submit();
1.25 raeburn 281: }
1.33 albertel 282: |;
1.25 raeburn 283: }
1.22 albertel 284: ($uname,$udom)=
1.29 albertel 285: &Apache::loncacc::constructaccess($env{'form.filename'},
1.22 albertel 286: $r->dir_config('lonDefDomain'));
287: unless (($uname) && ($udom)) {
288: $r->log_reason($uname.' at '.$udom.
1.29 albertel 289: ' trying to publish file '.$env{'form.filename'}.
1.22 albertel 290: ' - not authorized',
291: $r->filename);
292: return HTTP_NOT_ACCEPTABLE;
293: }
294:
295: my $fn;
1.29 albertel 296: if ($env{'form.filename'}) {
297: $fn=$env{'form.filename'};
1.22 albertel 298: $fn=~s/^http\:\/\/[^\/]+\///;
299: $fn=~s/^\///;
1.34 albertel 300: $fn=~s{(~|priv/)($LONCAPA::username_re)}{};
1.22 albertel 301: $fn=~s/\/+/\//g;
302: } else {
1.29 albertel 303: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.22 albertel 304: ' unspecified filename for upload', $r->filename);
305: return HTTP_NOT_FOUND;
306: }
1.1 www 307:
308: # ----------------------------------------------------------- Start page output
309:
310:
1.22 albertel 311: &Apache::loncommon::content_type($r,'text/html');
312: $r->send_http_header;
1.1 www 313:
1.31 albertel 314: $javascript = "<script type=\"text/javascript\">\n//<!--\n".
315: $javascript."\n// --></script>\n";
1.1 www 316:
1.31 albertel 317: $r->print(&Apache::loncommon::start_page('Upload file to Construction Space',
318: $javascript));
1.3 www 319:
1.29 albertel 320: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.22 albertel 321: $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
322: &mt(' at ').$udom.'</font></h3>');
323: }
324:
1.29 albertel 325: if ($env{'form.phase'} eq 'two') {
1.22 albertel 326: &phasetwo($r,$fn,$uname,$udom);
327: } else {
328: &phaseone($r,$fn,$uname,$udom);
329: }
1.1 www 330:
1.31 albertel 331: $r->print(&Apache::loncommon::end_page());
1.22 albertel 332: return OK;
1.1 www 333: }
1.7 www 334:
335: 1;
336: __END__
1.10 harris41 337:
338: =head1 NAME
339:
340: Apache::lonupload - upload files into construction space
341:
342: =head1 SYNOPSIS
343:
344: Invoked by /etc/httpd/conf/srm.conf:
345:
346: <Location /adm/upload>
347: PerlAccessHandler Apache::lonacc
348: SetHandler perl-script
349: PerlHandler Apache::lonupload
350: ErrorDocument 403 /adm/login
351: ErrorDocument 404 /adm/notfound.html
352: ErrorDocument 406 /adm/unauthorized.html
353: ErrorDocument 500 /adm/errorhandler
354: </Location>
355:
356: =head1 INTRODUCTION
357:
358: This module uploads a file sitting on a client computer into
359: library server construction space.
360:
361: This is part of the LearningOnline Network with CAPA project
362: described at http://www.lon-capa.org.
363:
364: =head1 HANDLER SUBROUTINE
365:
366: This routine is called by Apache and mod_perl.
367:
368: =over 4
369:
370: =item *
371:
372: Initialize variables
373:
374: =item *
375:
376: Start page output
377:
378: =item *
379:
380: output relevant interface phase (phaseone or phasetwo)
381:
382: =item *
383:
384: (phase one is to specify upload file; phase two is to handle conditions
385: subsequent to specification--like overwriting an existing file)
386:
387: =back
388:
389: =head1 OTHER SUBROUTINES
390:
391: =over 4
392:
393: =item *
394:
395: phaseone() : Interface for specifying file to upload.
396:
397: =item *
398:
399: phasetwo() : Interface for handling post-conditions about uploading (such
400: as overwriting an existing file).
401:
402: =item *
403:
404: upfile_store() : Store contents of uploaded file into temporary space. Invoked
405: by phaseone subroutine.
406:
407: =back
408:
409: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>