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

1.12      foxr        1: 
1.1       www         2: # The LearningOnline Network with CAPA
                      3: # Handler to upload files into construction space
                      4: #
1.28    ! raeburn     5: # $Id: lonupload.pm,v 1.27 2004/05/14 14:53:31 www 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.12      foxr       40: use Apache::Log();
1.13      foxr       41: use Apache::lonnet;
1.14      foxr       42: use HTML::Entities();
1.20      www        43: use Apache::lonlocal;
1.12      foxr       44: 
                     45: my $DEBUG=0;
                     46: 
                     47: sub Debug {
                     48:   
1.22      albertel   49:     # Marshall the parameters.
1.12      foxr       50:   
1.22      albertel   51:     my $r       = shift;
                     52:     my $log     = $r->log;
                     53:     my $message = shift;
1.12      foxr       54:   
1.22      albertel   55:     # Put out the indicated message butonly if DEBUG is false.
1.12      foxr       56:   
1.22      albertel   57:     if ($DEBUG) {
                     58: 	$log->debug($message);
                     59:     }
1.12      foxr       60: }
1.1       www        61: 
1.2       www        62: sub upfile_store {
                     63:     my $r=shift;
                     64: 	
                     65:     my $fname=$ENV{'form.upfile.filename'};
                     66:     $fname=~s/\W//g;
                     67:     
1.18      www        68:     chomp($ENV{'form.upfile'});
1.1       www        69:   
1.2       www        70:     my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
                     71: 		  '_upload_'.$fname.'_'.time.'_'.$$;
                     72:     {
                     73:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
                     74:                                    '/tmp/'.$datatoken.'.tmp');
                     75:        print $fh $ENV{'form.upfile'};
1.1       www        76:     }
1.2       www        77:     return $datatoken;
                     78: }
                     79: 
                     80: 
                     81: sub phaseone {
1.25      raeburn    82:     my ($r,$fn,$uname,$udom,$mode)=@_;
                     83:     my $action = '/adm/upload';
                     84:     if ($mode eq 'testbank') {
                     85:         $action = '/adm/testbank';
                     86:     } elsif ($mode eq 'imsimport') {
                     87:         $action = '/adm/imsimport';
                     88:     }
1.22      albertel   89:     $ENV{'form.upfile.filename'}=~s/\\/\//g;
                     90:     $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
                     91:     if ($ENV{'form.upfile.filename'}) {
                     92: 	$fn=~s/\/[^\/]+$//;
                     93: 	$fn=~s/([^\/])$/$1\//;
                     94: 	$fn.=$ENV{'form.upfile.filename'};
                     95: 	$fn=~s/^\///;
                     96: 	$fn=~s/(\/)+/\//g;
1.13      foxr       97: 
                     98: #    Fn is the full path to the destination filename.
                     99: #    
                    100: 
1.22      albertel  101: 	&Debug($r, "Filename for upload: $fn");
                    102: 	if (($fn) && ($fn!~/\/$/)) {
1.28    ! raeburn   103: 	    $r->print('<form action="'.$action.'" method="post" name="fileupload">'.
1.23      albertel  104: 		      '<input type="hidden" name="phase" value="two" />'.
                    105: 		      '<input type="hidden" name="datatoken" value="'.
                    106: 		      &upfile_store.'" />'.
                    107: 		      '<input type="hidden" name="uploaduname" value="'.$uname.
                    108: 		      '" />'.&mt('Store uploaded file as ').
1.25      raeburn   109:                       "<tt>/priv/$uname/</tt>".
                    110:                       '<input type="text" size="50" name="filename" value="'.$fn.
                    111:                       '" /><br />');
                    112:             $r->print('<br />'.&mt('Please indicate the type of file you are uploading. The possible types of file are as follows:').'
                    113: <ul>
                    114:  <li><b>'.&mt('Regular file:').'</b>'.&mt(' A file that requires no special handling during upload. The "Regular file" designation applies to html files, image files etc., as well as to zip, tar or gzip files that you wish to decompress after upload. In the case of a zip/tar/gz file etc., once the file has been uploaded, a "Decompress" link will automatically be displayed adjacent to the name of the file in the display of construction space directory contents. You will be able to decompress this file by clicking the link.').'</li>     
                    115:  <li><b>'.&mt('Testbank file:').'</b>'.&mt(' a testbank file containing plain text (ascii) questions and answers, which you plan to convert to LON-CAPA problems. The following question types can be converted: 1 of N multiple choice questions, individual True/False questions, groups of True/False questions, Fill-in-the-blank questions, Ranking questions, and Essay/short answer questions. Specific information about the format of the questions, foils, and correct answers is available ').'<a href="javascript:testbankWin()">'.&mt('here').'</a>,'.&mt(' and is also included in the pages displayed during step-by-step conversion of the testbank. The original testbank file can be removed from your construction space later, once the testbank questions have been converted.').'</li>
                    116: <li><b>'.&mt('IMS package').':</b>'.&mt(' a file containing course content from another Course Management System (e.g., Blackboard or ANGEL) packaged according to the IMS 1.1 specification.  The original IMS package file can be removed from your construction space later, once the package has been decompressed and the files converted to LON-CAPA sequence, page, problem, or bulletin board files, or stored as html, image or movie files etc., as appropriate.').'</li>
                    117: </ul>
                    118: <br />'.&mt('Choose file type:').'
                    119: <select name="filetype">
                    120:  <option value="standard" selected>'.&mt('Regular file').'
                    121:  <option value="testbank">'.&mt('Testbank file').'
                    122:  <option value="imsimport">'.&mt('IMS package').'
                    123: </select>
                    124: <br />
                    125: <br />
                    126: ');
                    127:             $r->print('<input type="button" value="'.&mt('Store').'" onClick="javascript:verifyForm()"/></form>');
1.22      albertel  128: 	    # Check for bad extension and warn user
                    129: 	    if ($fn=~/\.(\w+)$/ && 
                    130: 		(&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.23      albertel  131: 		$r->print('<font color="red">'.&mt('The extension on this file,').
1.22      albertel  132: 			  ' "'.$1.'"'.&mt(', is reserved internally by LON-CAPA.').
1.23      albertel  133: 			  ' <br />'.&mt('Please change the extension.').'</font>');
1.22      albertel  134: 	    } elsif($fn=~/\.(\w+)$/ && 
                    135: 		    !defined(&Apache::loncommon::fileembstyle($1))) {
1.23      albertel  136: 		$r->print('<font color="red">'.&mt('The extension on this file,').
1.22      albertel  137: 			  ' "'.$1.'"'.&mt(', is not recognized by LON-CAPA.').
1.23      albertel  138: 			  ' <br />'.&mt('Please change the extension.').
1.22      albertel  139: 			  '</font>');
                    140: 	    }
                    141: 	} else {
1.23      albertel  142: 	    $r->print('<font color="red">'.&mt('Illegal filename.').'</font>');
1.22      albertel  143: 	}
                    144:     } else {
1.23      albertel  145: 	$r->print('<font color="red">'.&mt('No upload file specified.').'</font>');
1.22      albertel  146:     }
1.1       www       147: }
                    148: 
                    149: sub phasetwo {
1.25      raeburn   150:     my ($r,$tfn,$uname,$udom,$mode)=@_;
                    151:     my $action = '/adm/upload';
                    152:     my $returnflag = '';
                    153:     if ($mode eq 'testbank') {
                    154:         $action = '/adm/testbank';
                    155:     } elsif ($mode eq 'imsimport') {
                    156:         $action = '/adm/imsimport';
                    157:     }
1.22      albertel  158:     my $fn='/priv/'.$uname.'/'.$tfn;
                    159:     $fn=~s/\/+/\//g;
                    160:     &Debug($r, "Filename is ".$tfn);
                    161:     if ($tfn) {
                    162: 	&Debug($r, "Filename for tfn = ".$tfn);
                    163: 	my $target='/home/'.$uname.'/public_html'.$tfn;
                    164: 	&Debug($r, "target -> ".$target);
1.13      foxr      165: #     target is the full filesystem path of the destination file.
1.22      albertel  166: 	my $base = &File::Basename::basename($fn);
                    167: 	my $path = &File::Basename::dirname($fn);
1.26      albertel  168: 	$base    = &HTML::Entities::encode($base,'<>&"');
1.22      albertel  169: 	my $url  = $path."/".$base; 
                    170: 	&Debug($r, "URL is now ".$url);
                    171: 	my $datatoken=$ENV{'form.datatoken'};
                    172: 	if (($fn) && ($datatoken)) {
                    173: 	    if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
1.25      raeburn   174: 		$r->print('<form action="'.$action.'" method="post">'.
1.22      albertel  175: 			  &mt('File').' <tt>'.$fn.'</tt> '.
                    176: 			  &mt('exists. Overwrite?').' '.
1.23      albertel  177: 			  '<input type="hidden" name="phase" value="two" />'.
                    178: 			  '<input type="hidden" name="filename" value="'."$url".'" />'.
                    179: 			  '<input type="hidden" name="datatoken" value="'.$datatoken.'" />'.
                    180: 			  '<input type="submit" name="override" value="'.&mt('Yes').'" /></form>');
1.22      albertel  181: 	    } else {
                    182: 		my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
1.27      www       183: 		my $dirpath=$path.'/';
                    184: 		$dirpath=~s/\/+/\//g;
1.22      albertel  185: 		# Check for bad extension and disallow upload
                    186: 		if ($fn=~/\.(\w+)$/ && 
                    187: 		    (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
                    188: 		    $r->print(&mt('File').' <tt>'.$fn.'</tt> '.
                    189: 			      &mt('could not be copied.').'<br />'.
1.23      albertel  190: 			      '<font color="red">'.
1.22      albertel  191: 			      &mt('The extension on this file is reserved internally by LON-CAPA.').
                    192: 			      '</font>');
1.27      www       193: 		    $r->print('<br /><font size=+2><a href="'.$dirpath.'">'.
1.22      albertel  194: 			      &mt('Back to Directory').'</a></font>');
                    195: 		} elsif ($fn=~/\.(\w+)$/ && 
                    196: 			 !defined(&Apache::loncommon::fileembstyle($1))) {
                    197: 		    $r->print(&mt('File').' <tt>'.$fn.'</tt> '.
                    198: 			      &mt('could not be copied.').'<br />'.
1.23      albertel  199: 			      '<font color="red">'.
1.22      albertel  200: 			      &mt('The extension on this file is not recognized by LON-CAPA.').
                    201: 			      '</font>');
1.27      www       202: 		    $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
1.22      albertel  203: 			      &mt('Back to Directory').'</a></font>');
                    204: 		} elsif (-d $target) {
                    205: 		    $r->print('File <tt>'.$fn.'</tt> could not be copied.<br />'.
1.23      albertel  206: 			      '<font color="red">'.
1.22      albertel  207: 			      &mt('The target is an existing directory.').
1.23      albertel  208: 			      '</font><br />');
1.27      www       209: 		    $r->print('<font size="+2"><a href="'.$dirpath.'">'.
1.22      albertel  210: 			      &mt('Back to Directory').'</a></font>');
                    211: 		} elsif (copy($source,$target)) {
                    212: 		    chmod(0660, $target); # Set permissions to rw-rw---.
1.25      raeburn   213:                     if ($mode eq 'testbank' || $mode eq 'imsimport') {
                    214:                         $r->print(&mt("Your file - $fn - was uploaded successfully")."<br /><br />");
                    215:                         $returnflag = 'ok';
                    216:                     } else {
                    217:                         $r->print(&mt('File copied.'));
                    218: 		        $r->print('<br /><font size="+2"><a href="'.$url.'">'.
1.22      albertel  219: 			      &mt('View file').'</a></font>');
1.27      www       220: 		        $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.
1.23      albertel  221: 			      &mt('Back to Directory').'</a></font><br />');
1.25      raeburn   222:                     }
1.22      albertel  223: 		} else {
                    224: 		    $r->print('Failed to copy: '.$!);
1.23      albertel  225: 		    $r->print('<br /><font size="+2"><a href="'.$path.'">'.
1.22      albertel  226: 			      &mt('Back to Directory').'</a></font>');
                    227: 		}
                    228: 	    }
                    229: 	} else {
1.23      albertel  230: 	    $r->print('<font size="+1" color="red">'.
1.22      albertel  231: 		      &mt('Please use browser "Back" button and pick a filename').
1.24      albertel  232: 		      '</font><br />');
1.22      albertel  233: 	}
1.1       www       234:     } else {
1.22      albertel  235: 	$r->print('<font size=+1 color=red>'.
                    236: 		  &mt('Please use browser "Back" button and pick a filename').
1.24      albertel  237: 		  '</font><br />>');
1.1       www       238:     }
1.25      raeburn   239:     return $returnflag;
1.1       www       240: }
                    241: 
1.10      harris41  242: # ---------------------------------------------------------------- Main Handler
1.1       www       243: sub handler {
                    244: 
1.22      albertel  245:     my $r=shift;
1.1       www       246: 
1.22      albertel  247:     my $uname;
                    248:     my $udom;
1.25      raeburn   249:     my $javascript = '';
1.18      www       250: #
                    251: # phase two: re-attach user
                    252: #
1.22      albertel  253:     if ($ENV{'form.uploaduname'}) {
                    254: 	$ENV{'form.filename'}='/priv/'.$ENV{'form.uploaduname'}.'/'.
                    255: 	    $ENV{'form.filename'};
                    256:     }
                    257: 
1.25      raeburn   258:     unless ($ENV{'form.phase'} eq 'two') {
                    259:         $javascript = qq|
                    260: function verifyForm() {
1.28    ! raeburn   261:     var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
1.25      raeburn   262:     if (mode == "testbank") {
1.28    ! raeburn   263:         document.fileupload.action = "/adm/testbank";
1.25      raeburn   264:     }
                    265:     if (mode == "imsimport") {
1.28    ! raeburn   266:         document.fileupload.action = "/adm/imsimport";
1.25      raeburn   267:     }
                    268:     if (mode == "standard") {
1.28    ! raeburn   269:         document.fileupload.action = "/adm/upload";
1.25      raeburn   270:     }
1.28    ! raeburn   271:     document.fileupload.submit();
1.25      raeburn   272: }
                    273: 
                    274: function testbankWin() {
                    275:   newWindow = window.open("","testbankinfo","HEIGHT=400,WIDTH=750,scrollbars=yes")
                    276:   newWindow.document.open()
                    277:   newWindow.document.write("<html><head><title>'Importing a Testbank file into LON-CAPA</title><meta http-equiv='pragma' content='no-cache'>\\n")
                    278:   newWindow.document.write("</head><body bgcolor='#CCFFDD' topmargin='0' leftmargin='0' marginheight='0'marginwidth='0' rightmargin='0'>\\n")
                    279:   newWindow.document.write("<img border='0' src='/adm/lonInterFace/author.jpg' alt='[Author Header]'>\\n")
                    280:   newWindow.document.write("<table border='0' cellspacing='0' cellpadding='0' width='95%' bgcolor='#CCFFDD'>\\n")
                    281:   newWindow.document.write("<tr><td width='2'>&nbsp;</td><td width='3'>&nbsp;</td>\\n")
                    282:   newWindow.document.write("<td><font face='arial,helvetica,sans-serif'><h3>Importing Testbank questions into LON-CAPA</h3>")
                    283:   newWindow.document.write("<font face='arial,helvetica,sans-serif'><br />Four requirements must be met to ensure that you will succeed in building LON-CAPA problem files using your plain text file containing testbank questions.")
                    284:   newWindow.document.write("<ol><li>The questions and answers you upload must be in plain text format.  Any header lines should occur before the text containing the questions and answers.</li>")
                    285:   newWindow.document.write("<li>All questions must occur before any of the answers.  Each question should be numbered sequentially using a number followed immediately by a space, a period, or enclosed in parentheses, i.e., 1 , 1., (1), 1), or (1 .</li>")
                    286:    newWindow.document.write("<li>One or more correct answers should be provided for all questions (although blank answers may be provided for <i>essay</i> questions).  Answers should be numbered sequentially, using the same scheme as used for the questions, and must occur after all the questions.")
                    287:     newWindow.document.write("<li><i>Multiple choice</i> and <i>multiple answer correct</i> questions should consist of (i) the question number followed by (ii) a question stem beginning on the same line and (iii) two or more foils, with each foil beginning on a new line and prefixed by a unique letter, or Roman numeral, listed in alphabetic or numeric order, beginning at a (alphabetic) or i (Roman numeral), followed by a period, or enclosed in parentheses, i.e., a., (a), i., or (i) .</li>")
                    288:      newWindow.document.write("<li>If <i>fill-in-the-blank</i> or <i>multiple answer</i> questions have more than one correct answer, each answer should appear in a comma-, tab-, space-, or new line-delimited list. </li></ol>")
                    289:   newWindow.document.write("</td></tr>\\n")
                    290:   newWindow.document.write("</table></body></html>")
                    291:   newWindow.document.close()
                    292:   newWindow.focus()
                    293: }
                    294: |;
                    295:     }
1.22      albertel  296:     ($uname,$udom)=
                    297: 	&Apache::loncacc::constructaccess($ENV{'form.filename'},
                    298: 					  $r->dir_config('lonDefDomain'));
                    299:     unless (($uname) && ($udom)) {
                    300: 	$r->log_reason($uname.' at '.$udom.
                    301: 		       ' trying to publish file '.$ENV{'form.filename'}.
                    302: 		       ' - not authorized', 
                    303: 		       $r->filename); 
                    304: 	return HTTP_NOT_ACCEPTABLE;
                    305:     }
                    306:     
                    307:     my $fn;
                    308:     if ($ENV{'form.filename'}) {
                    309: 	$fn=$ENV{'form.filename'};
                    310: 	$fn=~s/^http\:\/\/[^\/]+\///;
                    311: 	$fn=~s/^\///;
                    312: 	$fn=~s/(\~|priv\/)(\w+)//;
                    313: 	$fn=~s/\/+/\//g;
                    314:     } else {
                    315: 	$r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    316: 		       ' unspecified filename for upload', $r->filename); 
                    317: 	return HTTP_NOT_FOUND;
                    318:     }
1.1       www       319: 
                    320: # ----------------------------------------------------------- Start page output
                    321: 
                    322: 
1.22      albertel  323:     &Apache::loncommon::content_type($r,'text/html');
                    324:     $r->send_http_header;
1.1       www       325: 
1.25      raeburn   326:     $r->print("<html><head><title>LON-CAPA Construction Space</title><script type=\"text/javascript\">\n//<!--\n$javascript\n// --></script>\n</head>");
1.1       www       327: 
1.22      albertel  328:     $r->print(&Apache::loncommon::bodytag('Upload file to Construction Space'));
1.3       www       329:   
1.22      albertel  330:     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    331: 	$r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    332: 		  &mt(' at ').$udom.'</font></h3>');
                    333:     }
                    334: 
                    335:     if ($ENV{'form.phase'} eq 'two') {
                    336: 	&phasetwo($r,$fn,$uname,$udom);
                    337:     } else {
                    338: 	&phaseone($r,$fn,$uname,$udom);
                    339:     }
1.1       www       340: 
1.22      albertel  341:     $r->print('</body></html>');
                    342:     return OK;  
1.1       www       343: }
1.7       www       344: 
                    345: 1;
                    346: __END__
1.10      harris41  347: 
                    348: =head1 NAME
                    349: 
                    350: Apache::lonupload - upload files into construction space
                    351: 
                    352: =head1 SYNOPSIS
                    353: 
                    354: Invoked by /etc/httpd/conf/srm.conf:
                    355: 
                    356:  <Location /adm/upload>
                    357:  PerlAccessHandler       Apache::lonacc
                    358:  SetHandler perl-script
                    359:  PerlHandler Apache::lonupload
                    360:  ErrorDocument     403 /adm/login
                    361:  ErrorDocument     404 /adm/notfound.html
                    362:  ErrorDocument     406 /adm/unauthorized.html
                    363:  ErrorDocument	  500 /adm/errorhandler
                    364:  </Location>
                    365: 
                    366: =head1 INTRODUCTION
                    367: 
                    368: This module uploads a file sitting on a client computer into 
                    369: library server construction space.
                    370: 
                    371: This is part of the LearningOnline Network with CAPA project
                    372: described at http://www.lon-capa.org.
                    373: 
                    374: =head1 HANDLER SUBROUTINE
                    375: 
                    376: This routine is called by Apache and mod_perl.
                    377: 
                    378: =over 4
                    379: 
                    380: =item *
                    381: 
                    382: Initialize variables
                    383: 
                    384: =item *
                    385: 
                    386: Start page output
                    387: 
                    388: =item *
                    389: 
                    390: output relevant interface phase (phaseone or phasetwo)
                    391: 
                    392: =item *
                    393: 
                    394: (phase one is to specify upload file; phase two is to handle conditions
                    395: subsequent to specification--like overwriting an existing file)
                    396: 
                    397: =back
                    398: 
                    399: =head1 OTHER SUBROUTINES
                    400: 
                    401: =over 4
                    402: 
                    403: =item *
                    404: 
                    405: phaseone() : Interface for specifying file to upload.
                    406: 
                    407: =item *
                    408: 
                    409: phasetwo() : Interface for handling post-conditions about uploading (such
                    410: as overwriting an existing file).
                    411: 
                    412: =item *
                    413: 
                    414: upfile_store() : Store contents of uploaded file into temporary space.  Invoked
                    415: by phaseone subroutine.
                    416: 
                    417: =back
                    418: 
                    419: =cut

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