File:  [LON-CAPA] / loncom / publisher / lonupload.pm
Revision 1.36: download - view: text, annotated - select for diffs
Tue Jan 15 16:04:38 2008 UTC (16 years, 4 months ago) by www
Branches: MAIN
CVS tags: version_2_6_X, version_2_6_3, version_2_6_2, version_2_6_1, HEAD
Bug #5533: Untranslatable upload interface, cancel button

    1: 
    2: # The LearningOnline Network with CAPA
    3: # Handler to upload files into construction space
    4: #
    5: # $Id: lonupload.pm,v 1.36 2008/01/15 16:04:38 www Exp $
    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: #
   29: ###
   30: 
   31: package Apache::lonupload;
   32: 
   33: use strict;
   34: use Apache::File;
   35: use File::Copy;
   36: use File::Basename;
   37: use Apache::Constants qw(:common :http :methods);
   38: use Apache::loncacc;
   39: use Apache::loncommon();
   40: use Apache::lonnet;
   41: use HTML::Entities();
   42: use Apache::lonlocal;
   43: use Apache::lonnet;
   44: use LONCAPA();
   45: 
   46: my $DEBUG=0;
   47: 
   48: sub Debug {
   49:     # Put out the indicated message but only if DEBUG is true.
   50:     if ($DEBUG) {
   51: 	my ($r,$message) = @_;
   52: 	$r->log_reason($message);
   53:     }
   54: }
   55: 
   56: sub upfile_store {
   57:     my $r=shift;
   58: 	
   59:     my $fname=$env{'form.upfile.filename'};
   60:     $fname=~s/\W//g;
   61:     
   62:     chomp($env{'form.upfile'});
   63:   
   64:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
   65: 		  '_upload_'.$fname.'_'.time.'_'.$$;
   66:     {
   67:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
   68:                                    '/tmp/'.$datatoken.'.tmp');
   69:        print $fh $env{'form.upfile'};
   70:     }
   71:     return $datatoken;
   72: }
   73: 
   74: 
   75: sub phaseone {
   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:     }
   83:     $env{'form.upfile.filename'}=~s/\\/\//g;
   84:     $env{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
   85:     if ($env{'form.upfile.filename'}) {
   86: 	$fn=~s/\/[^\/]+$//;
   87: 	$fn=~s/([^\/])$/$1\//;
   88: 	$fn.=$env{'form.upfile.filename'};
   89: 	$fn=~s/^\///;
   90: 	$fn=~s/(\/)+/\//g;
   91: 
   92: #    Fn is the full path to the destination filename.
   93: #    
   94: 
   95: 	&Debug($r, "Filename for upload: $fn");
   96: 	if (($fn) && ($fn!~/\/$/)) {
   97: 	    $r->print('<form action="'.$action.'" method="post" name="fileupload">'.
   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.
  102: 		      '" />'.&mt('Save uploaded file as ').
  103:                       "<span class='LC_filename'>/priv/$uname/</span>".
  104:                       '<input type="text" size="50" name="filename" value="'.$fn.
  105:                       '" /><br />'.
  106: 		      '<br />'.&mt('Choose file type:').'
  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').'
  111: </select>'.&Apache::loncommon::help_open_topic("Uploading_File_Options").'
  112: <br />
  113: <br />
  114: ');
  115:             $r->print('<input type="button" value="'.&mt('Save').'" onClick="javascript:verifyForm()"/></form>');
  116: 	    # Check for bad extension and warn user
  117: 	    if ($fn=~/\.(\w+)$/ && 
  118: 		(&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
  119: 		$r->print('<span class="LC_error">'.&mt('The extension on this file,').
  120: 			  ' "'.$1.'"'.&mt(', is reserved internally by LON-CAPA.').
  121: 			  ' <br />'.&mt('Please change the extension.').'</span>');
  122: 	    } elsif($fn=~/\.(\w+)$/ && 
  123: 		    !defined(&Apache::loncommon::fileembstyle($1))) {
  124: 		$r->print('<span class="LC_error">'.&mt('The extension on this file,').
  125: 			  ' "'.$1.'"'.&mt(', is not recognized by LON-CAPA.').
  126: 			  ' <br />'.&mt('Please change the extension.').
  127: 			  '</span>');
  128: 	    }
  129: 	} else {
  130: 	    $r->print('<span class="LC_error">'.&mt('Illegal filename.').'</span>');
  131: 	}
  132:     } else {
  133: 	$r->print('<span class="LC_error">'.&mt('No upload file specified.').'</span>');
  134:     }
  135: }
  136: 
  137: sub phasetwo {
  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:     }
  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);
  153: #     target is the full filesystem path of the destination file.
  154: 	my $base = &File::Basename::basename($fn);
  155: 	my $path = &File::Basename::dirname($fn);
  156: 	$base    = &HTML::Entities::encode($base,'<>&"');
  157: 	my $url  = $path."/".$base; 
  158: 	&Debug($r, "URL is now ".$url);
  159: 	my $datatoken=$env{'form.datatoken'};
  160: 	if (($fn) && ($datatoken)) {
  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'})) {
  168: 		$r->print('<form action="'.$action.'" method="post">'.
  169: 			  &mt('File [_1] exists. Overwrite?','<span class="LC_filename">'.$fn.'</span>').
  170: 			  '<input type="hidden" name="phase" value="two" />'.
  171: 			  '<input type="hidden" name="filename" value="'."$url".'" />'.
  172: 			  '<input type="hidden" name="datatoken" value="'.$datatoken.'" />'.
  173: 			  '<input type="submit" name="override" value="'.&mt('Yes').'" />'.
  174:                           '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
  175:                           '</form>');
  176:             } else {
  177: 		my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
  178: 		my $dirpath=$path.'/';
  179: 		$dirpath=~s/\/+/\//g;
  180: 		# Check for bad extension and disallow upload
  181: 		if ($fn=~/\.(\w+)$/ && 
  182: 		    (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
  183: 		    $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.
  184: 			      &mt('could not be copied.').'<br />'.
  185: 			      '<span class="LC_error">'.
  186: 			      &mt('The extension on this file is reserved internally by LON-CAPA.').
  187: 			      '</span>');
  188: 		    $r->print('<br /><font size=+2><a href="'.$dirpath.'">'.
  189: 			      &mt('Back to Directory').'</a></font>');
  190: 		} elsif ($fn=~/\.(\w+)$/ && 
  191: 			 !defined(&Apache::loncommon::fileembstyle($1))) {
  192: 		    $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.
  193: 			      &mt('could not be copied.').'<br />'.
  194: 			      '<span class="LC_error">'.
  195: 			      &mt('The extension on this file is not recognized by LON-CAPA.').
  196: 			      '</span>');
  197: 		    $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
  198: 			      &mt('Back to Directory').'</a></font>');
  199: 		} elsif (-d $target) {
  200: 		    $r->print('File <span class="LC_filename">'.$fn.'</span> could not be copied.<br />'.
  201: 			      '<span class="LC_error">'.
  202: 			      &mt('The target is an existing directory.').
  203: 			      '</span><br />');
  204: 		    $r->print('<font size="+2"><a href="'.$dirpath.'">'.
  205: 			      &mt('Back to Directory').'</a></font>');
  206: 		} elsif (copy($source,$target)) {
  207: 		    chmod(0660, $target); # Set permissions to rw-rw---.
  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.'">'.
  214: 			      &mt('View file').'</a></font>');
  215: 		        $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
  216: 			      &mt('Back to Directory').'</a></font><br />');
  217:                     }
  218: 		} else {
  219: 		    $r->print('Failed to copy: '.$!);
  220: 		    $r->print('<br /><font size="+2"><a href="'.$path.'">'.
  221: 			      &mt('Back to Directory').'</a></font>');
  222: 		}
  223: 	    }
  224: 	} else {
  225: 	    $r->print('<span class="LC_error">'.
  226: 		      &mt('Please use browser "Back" button and pick a filename').
  227: 		      '</span><br />');
  228: 	}
  229:     } else {
  230: 	$r->print('<span class="LC_error">'.
  231: 		  &mt('Please use browser "Back" button and pick a filename').
  232: 		  '</span><br />>');
  233:     }
  234:     return $returnflag;
  235: }
  236: 
  237: # ---------------------------------------------------------------- Main Handler
  238: sub handler {
  239: 
  240:     my $r=shift;
  241: 
  242:     my $uname;
  243:     my $udom;
  244:     my $javascript = '';
  245: #
  246: # phase two: re-attach user
  247: #
  248:     if ($env{'form.uploaduname'}) {
  249: 	$env{'form.filename'}='/priv/'.$env{'form.uploaduname'}.'/'.
  250: 	    $env{'form.filename'};
  251:     }
  252: 
  253:     unless ($env{'form.phase'} eq 'two') {
  254: 	my %body_layout = ('rightmargin'  => "0",
  255: 			   'leftmargin'   => "0",
  256: 			   'marginwidth'  => "0",
  257: 			   'topmargin'    => "0",
  258: 			   'marginheight' => "0");
  259: 	my $start_page = 
  260: 	    &Apache::loncommon::start_page('Importing a Testbank file into LON-CAPA',
  261: 					   undef,
  262: 					   {'only_body'   => 1,
  263: 					    'add_entries' => \%body_layout,
  264: 					    'js_ready'    => 1,});
  265: 	my $end_page = 
  266: 	    &Apache::loncommon::end_page({'js_ready' => 1,});
  267: 
  268:         $javascript = qq|
  269: function verifyForm() {
  270:     var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
  271:     if (mode == "testbank") {
  272:         document.fileupload.action = "/adm/testbank";
  273:     }
  274:     if (mode == "imsimport") {
  275:         document.fileupload.action = "/adm/imsimport";
  276:     }
  277:     if (mode == "standard") {
  278:         document.fileupload.action = "/adm/upload";
  279:     }
  280:     document.fileupload.submit();
  281: }
  282: 	|;
  283:     }
  284:     ($uname,$udom)=
  285: 	&Apache::loncacc::constructaccess($env{'form.filename'},
  286: 					  $r->dir_config('lonDefDomain'));
  287:     unless (($uname) && ($udom)) {
  288: 	$r->log_reason($uname.' at '.$udom.
  289: 		       ' trying to publish file '.$env{'form.filename'}.
  290: 		       ' - not authorized', 
  291: 		       $r->filename); 
  292: 	return HTTP_NOT_ACCEPTABLE;
  293:     }
  294:     
  295:     my $fn;
  296:     if ($env{'form.filename'}) {
  297: 	$fn=$env{'form.filename'};
  298: 	$fn=~s/^http\:\/\/[^\/]+\///;
  299: 	$fn=~s/^\///;
  300: 	$fn=~s{(~|priv/)($LONCAPA::username_re)}{};
  301: 	$fn=~s/\/+/\//g;
  302:     } else {
  303: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
  304: 		       ' unspecified filename for upload', $r->filename); 
  305: 	return HTTP_NOT_FOUND;
  306:     }
  307: 
  308: # ----------------------------------------------------------- Start page output
  309: 
  310: 
  311:     &Apache::loncommon::content_type($r,'text/html');
  312:     $r->send_http_header;
  313: 
  314:    $javascript = "<script type=\"text/javascript\">\n//<!--\n".
  315: 	$javascript."\n// --></script>\n";
  316: 
  317:     $r->print(&Apache::loncommon::start_page('Upload file to Construction Space',
  318: 					     $javascript));
  319:   
  320:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
  321: 	$r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
  322: 		  &mt(' at ').$udom.'</font></h3>');
  323:     }
  324: 
  325:     if ($env{'form.phase'} eq 'two') {
  326: 	&phasetwo($r,$fn,$uname,$udom);
  327:     } else {
  328: 	&phaseone($r,$fn,$uname,$udom);
  329:     }
  330: 
  331:     $r->print(&Apache::loncommon::end_page());
  332:     return OK;  
  333: }
  334: 
  335: 1;
  336: __END__
  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>