Annotation of loncom/publisher/lonupload.pm, revision 1.35

1.12      foxr        1: 
1.1       www         2: # The LearningOnline Network with CAPA
                      3: # Handler to upload files into construction space
                      4: #
1.35    ! albertel    5: # $Id: lonupload.pm,v 1.34 2006/12/06 22:22:39 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.29      albertel  161: 	    if ((-e $target) && ($env{'form.override'} ne 'Yes')) {
1.25      raeburn   162: 		$r->print('<form action="'.$action.'" method="post">'.
1.33      albertel  163: 			  &mt('File').' <span class="LC_filename">'.$fn.'</span> '.
1.22      albertel  164: 			  &mt('exists. Overwrite?').' '.
1.23      albertel  165: 			  '<input type="hidden" name="phase" value="two" />'.
                    166: 			  '<input type="hidden" name="filename" value="'."$url".'" />'.
                    167: 			  '<input type="hidden" name="datatoken" value="'.$datatoken.'" />'.
                    168: 			  '<input type="submit" name="override" value="'.&mt('Yes').'" /></form>');
1.22      albertel  169: 	    } else {
                    170: 		my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
1.27      www       171: 		my $dirpath=$path.'/';
                    172: 		$dirpath=~s/\/+/\//g;
1.22      albertel  173: 		# Check for bad extension and disallow upload
                    174: 		if ($fn=~/\.(\w+)$/ && 
                    175: 		    (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.33      albertel  176: 		    $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.
1.22      albertel  177: 			      &mt('could not be copied.').'<br />'.
1.33      albertel  178: 			      '<span class="LC_error">'.
1.22      albertel  179: 			      &mt('The extension on this file is reserved internally by LON-CAPA.').
1.33      albertel  180: 			      '</span>');
1.27      www       181: 		    $r->print('<br /><font size=+2><a href="'.$dirpath.'">'.
1.22      albertel  182: 			      &mt('Back to Directory').'</a></font>');
                    183: 		} elsif ($fn=~/\.(\w+)$/ && 
                    184: 			 !defined(&Apache::loncommon::fileembstyle($1))) {
1.33      albertel  185: 		    $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.
1.22      albertel  186: 			      &mt('could not be copied.').'<br />'.
1.33      albertel  187: 			      '<span class="LC_error">'.
1.22      albertel  188: 			      &mt('The extension on this file is not recognized by LON-CAPA.').
1.33      albertel  189: 			      '</span>');
1.27      www       190: 		    $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
1.22      albertel  191: 			      &mt('Back to Directory').'</a></font>');
                    192: 		} elsif (-d $target) {
1.33      albertel  193: 		    $r->print('File <span class="LC_filename">'.$fn.'</span> could not be copied.<br />'.
                    194: 			      '<span class="LC_error">'.
1.22      albertel  195: 			      &mt('The target is an existing directory.').
1.33      albertel  196: 			      '</span><br />');
1.27      www       197: 		    $r->print('<font size="+2"><a href="'.$dirpath.'">'.
1.22      albertel  198: 			      &mt('Back to Directory').'</a></font>');
                    199: 		} elsif (copy($source,$target)) {
                    200: 		    chmod(0660, $target); # Set permissions to rw-rw---.
1.25      raeburn   201:                     if ($mode eq 'testbank' || $mode eq 'imsimport') {
                    202:                         $r->print(&mt("Your file - $fn - was uploaded successfully")."<br /><br />");
                    203:                         $returnflag = 'ok';
                    204:                     } else {
                    205:                         $r->print(&mt('File copied.'));
                    206: 		        $r->print('<br /><font size="+2"><a href="'.$url.'">'.
1.22      albertel  207: 			      &mt('View file').'</a></font>');
1.27      www       208: 		        $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
1.23      albertel  209: 			      &mt('Back to Directory').'</a></font><br />');
1.25      raeburn   210:                     }
1.22      albertel  211: 		} else {
                    212: 		    $r->print('Failed to copy: '.$!);
1.23      albertel  213: 		    $r->print('<br /><font size="+2"><a href="'.$path.'">'.
1.22      albertel  214: 			      &mt('Back to Directory').'</a></font>');
                    215: 		}
                    216: 	    }
                    217: 	} else {
1.33      albertel  218: 	    $r->print('<span class="LC_error">'.
1.22      albertel  219: 		      &mt('Please use browser "Back" button and pick a filename').
1.33      albertel  220: 		      '</span><br />');
1.22      albertel  221: 	}
1.1       www       222:     } else {
1.33      albertel  223: 	$r->print('<span class="LC_error">'.
1.22      albertel  224: 		  &mt('Please use browser "Back" button and pick a filename').
1.33      albertel  225: 		  '</span><br />>');
1.1       www       226:     }
1.25      raeburn   227:     return $returnflag;
1.1       www       228: }
                    229: 
1.10      harris41  230: # ---------------------------------------------------------------- Main Handler
1.1       www       231: sub handler {
                    232: 
1.22      albertel  233:     my $r=shift;
1.1       www       234: 
1.22      albertel  235:     my $uname;
                    236:     my $udom;
1.25      raeburn   237:     my $javascript = '';
1.18      www       238: #
                    239: # phase two: re-attach user
                    240: #
1.29      albertel  241:     if ($env{'form.uploaduname'}) {
                    242: 	$env{'form.filename'}='/priv/'.$env{'form.uploaduname'}.'/'.
                    243: 	    $env{'form.filename'};
1.22      albertel  244:     }
                    245: 
1.29      albertel  246:     unless ($env{'form.phase'} eq 'two') {
1.32      albertel  247: 	my %body_layout = ('rightmargin'  => "0",
                    248: 			   'leftmargin'   => "0",
                    249: 			   'marginwidth'  => "0",
                    250: 			   'topmargin'    => "0",
                    251: 			   'marginheight' => "0");
1.31      albertel  252: 	my $start_page = 
                    253: 	    &Apache::loncommon::start_page('Importing a Testbank file into LON-CAPA',
                    254: 					   undef,
                    255: 					   {'only_body'   => 1,
1.32      albertel  256: 					    'add_entries' => \%body_layout,
1.31      albertel  257: 					    'js_ready'    => 1,});
                    258: 	my $end_page = 
                    259: 	    &Apache::loncommon::end_page({'js_ready' => 1,});
                    260: 
1.25      raeburn   261:         $javascript = qq|
                    262: function verifyForm() {
1.28      raeburn   263:     var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
1.25      raeburn   264:     if (mode == "testbank") {
1.28      raeburn   265:         document.fileupload.action = "/adm/testbank";
1.25      raeburn   266:     }
                    267:     if (mode == "imsimport") {
1.28      raeburn   268:         document.fileupload.action = "/adm/imsimport";
1.25      raeburn   269:     }
                    270:     if (mode == "standard") {
1.28      raeburn   271:         document.fileupload.action = "/adm/upload";
1.25      raeburn   272:     }
1.28      raeburn   273:     document.fileupload.submit();
1.25      raeburn   274: }
1.33      albertel  275: 	|;
1.25      raeburn   276:     }
1.22      albertel  277:     ($uname,$udom)=
1.29      albertel  278: 	&Apache::loncacc::constructaccess($env{'form.filename'},
1.22      albertel  279: 					  $r->dir_config('lonDefDomain'));
                    280:     unless (($uname) && ($udom)) {
                    281: 	$r->log_reason($uname.' at '.$udom.
1.29      albertel  282: 		       ' trying to publish file '.$env{'form.filename'}.
1.22      albertel  283: 		       ' - not authorized', 
                    284: 		       $r->filename); 
                    285: 	return HTTP_NOT_ACCEPTABLE;
                    286:     }
                    287:     
                    288:     my $fn;
1.29      albertel  289:     if ($env{'form.filename'}) {
                    290: 	$fn=$env{'form.filename'};
1.22      albertel  291: 	$fn=~s/^http\:\/\/[^\/]+\///;
                    292: 	$fn=~s/^\///;
1.34      albertel  293: 	$fn=~s{(~|priv/)($LONCAPA::username_re)}{};
1.22      albertel  294: 	$fn=~s/\/+/\//g;
                    295:     } else {
1.29      albertel  296: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.22      albertel  297: 		       ' unspecified filename for upload', $r->filename); 
                    298: 	return HTTP_NOT_FOUND;
                    299:     }
1.1       www       300: 
                    301: # ----------------------------------------------------------- Start page output
                    302: 
                    303: 
1.22      albertel  304:     &Apache::loncommon::content_type($r,'text/html');
                    305:     $r->send_http_header;
1.1       www       306: 
1.31      albertel  307:    $javascript = "<script type=\"text/javascript\">\n//<!--\n".
                    308: 	$javascript."\n// --></script>\n";
1.1       www       309: 
1.31      albertel  310:     $r->print(&Apache::loncommon::start_page('Upload file to Construction Space',
                    311: 					     $javascript));
1.3       www       312:   
1.29      albertel  313:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.22      albertel  314: 	$r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    315: 		  &mt(' at ').$udom.'</font></h3>');
                    316:     }
                    317: 
1.29      albertel  318:     if ($env{'form.phase'} eq 'two') {
1.22      albertel  319: 	&phasetwo($r,$fn,$uname,$udom);
                    320:     } else {
                    321: 	&phaseone($r,$fn,$uname,$udom);
                    322:     }
1.1       www       323: 
1.31      albertel  324:     $r->print(&Apache::loncommon::end_page());
1.22      albertel  325:     return OK;  
1.1       www       326: }
1.7       www       327: 
                    328: 1;
                    329: __END__
1.10      harris41  330: 
                    331: =head1 NAME
                    332: 
                    333: Apache::lonupload - upload files into construction space
                    334: 
                    335: =head1 SYNOPSIS
                    336: 
                    337: Invoked by /etc/httpd/conf/srm.conf:
                    338: 
                    339:  <Location /adm/upload>
                    340:  PerlAccessHandler       Apache::lonacc
                    341:  SetHandler perl-script
                    342:  PerlHandler Apache::lonupload
                    343:  ErrorDocument     403 /adm/login
                    344:  ErrorDocument     404 /adm/notfound.html
                    345:  ErrorDocument     406 /adm/unauthorized.html
                    346:  ErrorDocument	  500 /adm/errorhandler
                    347:  </Location>
                    348: 
                    349: =head1 INTRODUCTION
                    350: 
                    351: This module uploads a file sitting on a client computer into 
                    352: library server construction space.
                    353: 
                    354: This is part of the LearningOnline Network with CAPA project
                    355: described at http://www.lon-capa.org.
                    356: 
                    357: =head1 HANDLER SUBROUTINE
                    358: 
                    359: This routine is called by Apache and mod_perl.
                    360: 
                    361: =over 4
                    362: 
                    363: =item *
                    364: 
                    365: Initialize variables
                    366: 
                    367: =item *
                    368: 
                    369: Start page output
                    370: 
                    371: =item *
                    372: 
                    373: output relevant interface phase (phaseone or phasetwo)
                    374: 
                    375: =item *
                    376: 
                    377: (phase one is to specify upload file; phase two is to handle conditions
                    378: subsequent to specification--like overwriting an existing file)
                    379: 
                    380: =back
                    381: 
                    382: =head1 OTHER SUBROUTINES
                    383: 
                    384: =over 4
                    385: 
                    386: =item *
                    387: 
                    388: phaseone() : Interface for specifying file to upload.
                    389: 
                    390: =item *
                    391: 
                    392: phasetwo() : Interface for handling post-conditions about uploading (such
                    393: as overwriting an existing file).
                    394: 
                    395: =item *
                    396: 
                    397: upfile_store() : Store contents of uploaded file into temporary space.  Invoked
                    398: by phaseone subroutine.
                    399: 
                    400: =back
                    401: 
                    402: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>