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

1.12      foxr        1: 
1.1       www         2: # The LearningOnline Network with CAPA
                      3: # Handler to upload files into construction space
                      4: #
1.25    ! raeburn     5: # $Id: lonupload.pm,v 1.24 2003/11/08 11:13:50 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.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.25    ! raeburn   103: 	    $r->print('<form action="'.$action.'" method="post">'.
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);
                    168: 	$base    = &HTML::Entities::encode($base);
                    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';
                    183: 		# Check for bad extension and disallow upload
                    184: 		if ($fn=~/\.(\w+)$/ && 
                    185: 		    (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
                    186: 		    $r->print(&mt('File').' <tt>'.$fn.'</tt> '.
                    187: 			      &mt('could not be copied.').'<br />'.
1.23      albertel  188: 			      '<font color="red">'.
1.22      albertel  189: 			      &mt('The extension on this file is reserved internally by LON-CAPA.').
                    190: 			      '</font>');
1.24      albertel  191: 		    $r->print('<br /><font size=+2><a href="'.$path.'">'.
1.22      albertel  192: 			      &mt('Back to Directory').'</a></font>');
                    193: 		} elsif ($fn=~/\.(\w+)$/ && 
                    194: 			 !defined(&Apache::loncommon::fileembstyle($1))) {
                    195: 		    $r->print(&mt('File').' <tt>'.$fn.'</tt> '.
                    196: 			      &mt('could not be copied.').'<br />'.
1.23      albertel  197: 			      '<font color="red">'.
1.22      albertel  198: 			      &mt('The extension on this file is not recognized by LON-CAPA.').
                    199: 			      '</font>');
1.23      albertel  200: 		    $r->print('<br /><font size="+2"><a href="'.$path.'">'.
1.22      albertel  201: 			      &mt('Back to Directory').'</a></font>');
                    202: 		} elsif (-d $target) {
                    203: 		    $r->print('File <tt>'.$fn.'</tt> could not be copied.<br />'.
1.23      albertel  204: 			      '<font color="red">'.
1.22      albertel  205: 			      &mt('The target is an existing directory.').
1.23      albertel  206: 			      '</font><br />');
                    207: 		    $r->print('<font size="+2"><a href="'.$path.'">'.
1.22      albertel  208: 			      &mt('Back to Directory').'</a></font>');
                    209: 		} elsif (copy($source,$target)) {
                    210: 		    chmod(0660, $target); # Set permissions to rw-rw---.
1.25    ! raeburn   211:                     if ($mode eq 'testbank' || $mode eq 'imsimport') {
        !           212:                         $r->print(&mt("Your file - $fn - was uploaded successfully")."<br /><br />");
        !           213:                         $returnflag = 'ok';
        !           214:                     } else {
        !           215:                         $r->print(&mt('File copied.'));
        !           216: 		        $r->print('<br /><font size="+2"><a href="'.$url.'">'.
1.22      albertel  217: 			      &mt('View file').'</a></font>');
1.25    ! raeburn   218: 		        $r->print('<br /><font size="+2"><a href="'.$path.'">'.
1.23      albertel  219: 			      &mt('Back to Directory').'</a></font><br />');
1.25    ! raeburn   220:                     }
1.22      albertel  221: 		} else {
                    222: 		    $r->print('Failed to copy: '.$!);
1.23      albertel  223: 		    $r->print('<br /><font size="+2"><a href="'.$path.'">'.
1.22      albertel  224: 			      &mt('Back to Directory').'</a></font>');
                    225: 		}
                    226: 	    }
                    227: 	} else {
1.23      albertel  228: 	    $r->print('<font size="+1" color="red">'.
1.22      albertel  229: 		      &mt('Please use browser "Back" button and pick a filename').
1.24      albertel  230: 		      '</font><br />');
1.22      albertel  231: 	}
1.1       www       232:     } else {
1.22      albertel  233: 	$r->print('<font size=+1 color=red>'.
                    234: 		  &mt('Please use browser "Back" button and pick a filename').
1.24      albertel  235: 		  '</font><br />>');
1.1       www       236:     }
1.25    ! raeburn   237:     return $returnflag;
1.1       www       238: }
                    239: 
1.10      harris41  240: # ---------------------------------------------------------------- Main Handler
1.1       www       241: sub handler {
                    242: 
1.22      albertel  243:     my $r=shift;
1.1       www       244: 
1.22      albertel  245:     my $uname;
                    246:     my $udom;
1.25    ! raeburn   247:     my $javascript = '';
1.18      www       248: #
                    249: # phase two: re-attach user
                    250: #
1.22      albertel  251:     if ($ENV{'form.uploaduname'}) {
                    252: 	$ENV{'form.filename'}='/priv/'.$ENV{'form.uploaduname'}.'/'.
                    253: 	    $ENV{'form.filename'};
                    254:     }
                    255: 
1.25    ! raeburn   256:     unless ($ENV{'form.phase'} eq 'two') {
        !           257:         $javascript = qq|
        !           258: function verifyForm() {
        !           259:     var mode = document.forms[0].filetype.options[document.forms[0].filetype.selectedIndex].value
        !           260:     if (mode == "testbank") {
        !           261:         document.forms[0].action = "/adm/testbank";
        !           262:     }
        !           263:     if (mode == "imsimport") {
        !           264:         document.forms[0].action = "/adm/imsimport";
        !           265:     }
        !           266:     if (mode == "standard") {
        !           267:         document.forms[0].action = "/adm/upload";
        !           268:     }
        !           269:     document.forms[0].submit();
        !           270: }
        !           271: 
        !           272: function testbankWin() {
        !           273:   newWindow = window.open("","testbankinfo","HEIGHT=400,WIDTH=750,scrollbars=yes")
        !           274:   newWindow.document.open()
        !           275:   newWindow.document.write("<html><head><title>'Importing a Testbank file into LON-CAPA</title><meta http-equiv='pragma' content='no-cache'>\\n")
        !           276:   newWindow.document.write("</head><body bgcolor='#CCFFDD' topmargin='0' leftmargin='0' marginheight='0'marginwidth='0' rightmargin='0'>\\n")
        !           277:   newWindow.document.write("<img border='0' src='/adm/lonInterFace/author.jpg' alt='[Author Header]'>\\n")
        !           278:   newWindow.document.write("<table border='0' cellspacing='0' cellpadding='0' width='95%' bgcolor='#CCFFDD'>\\n")
        !           279:   newWindow.document.write("<tr><td width='2'>&nbsp;</td><td width='3'>&nbsp;</td>\\n")
        !           280:   newWindow.document.write("<td><font face='arial,helvetica,sans-serif'><h3>Importing Testbank questions into LON-CAPA</h3>")
        !           281:   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.")
        !           282:   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>")
        !           283:   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>")
        !           284:    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.")
        !           285:     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>")
        !           286:      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>")
        !           287:   newWindow.document.write("</td></tr>\\n")
        !           288:   newWindow.document.write("</table></body></html>")
        !           289:   newWindow.document.close()
        !           290:   newWindow.focus()
        !           291: }
        !           292: |;
        !           293:     }
1.22      albertel  294:     ($uname,$udom)=
                    295: 	&Apache::loncacc::constructaccess($ENV{'form.filename'},
                    296: 					  $r->dir_config('lonDefDomain'));
                    297:     unless (($uname) && ($udom)) {
                    298: 	$r->log_reason($uname.' at '.$udom.
                    299: 		       ' trying to publish file '.$ENV{'form.filename'}.
                    300: 		       ' - not authorized', 
                    301: 		       $r->filename); 
                    302: 	return HTTP_NOT_ACCEPTABLE;
                    303:     }
                    304:     
                    305:     my $fn;
                    306:     if ($ENV{'form.filename'}) {
                    307: 	$fn=$ENV{'form.filename'};
                    308: 	$fn=~s/^http\:\/\/[^\/]+\///;
                    309: 	$fn=~s/^\///;
                    310: 	$fn=~s/(\~|priv\/)(\w+)//;
                    311: 	$fn=~s/\/+/\//g;
                    312:     } else {
                    313: 	$r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    314: 		       ' unspecified filename for upload', $r->filename); 
                    315: 	return HTTP_NOT_FOUND;
                    316:     }
1.1       www       317: 
                    318: # ----------------------------------------------------------- Start page output
                    319: 
                    320: 
1.22      albertel  321:     &Apache::loncommon::content_type($r,'text/html');
                    322:     $r->send_http_header;
1.1       www       323: 
1.25    ! raeburn   324:     $r->print("<html><head><title>LON-CAPA Construction Space</title><script type=\"text/javascript\">\n//<!--\n$javascript\n// --></script>\n</head>");
1.1       www       325: 
1.22      albertel  326:     $r->print(&Apache::loncommon::bodytag('Upload file to Construction Space'));
1.3       www       327:   
1.22      albertel  328:     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    329: 	$r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    330: 		  &mt(' at ').$udom.'</font></h3>');
                    331:     }
                    332: 
                    333:     if ($ENV{'form.phase'} eq 'two') {
                    334: 	&phasetwo($r,$fn,$uname,$udom);
                    335:     } else {
                    336: 	&phaseone($r,$fn,$uname,$udom);
                    337:     }
1.1       www       338: 
1.22      albertel  339:     $r->print('</body></html>');
                    340:     return OK;  
1.1       www       341: }
1.7       www       342: 
                    343: 1;
                    344: __END__
1.10      harris41  345: 
                    346: =head1 NAME
                    347: 
                    348: Apache::lonupload - upload files into construction space
                    349: 
                    350: =head1 SYNOPSIS
                    351: 
                    352: Invoked by /etc/httpd/conf/srm.conf:
                    353: 
                    354:  <Location /adm/upload>
                    355:  PerlAccessHandler       Apache::lonacc
                    356:  SetHandler perl-script
                    357:  PerlHandler Apache::lonupload
                    358:  ErrorDocument     403 /adm/login
                    359:  ErrorDocument     404 /adm/notfound.html
                    360:  ErrorDocument     406 /adm/unauthorized.html
                    361:  ErrorDocument	  500 /adm/errorhandler
                    362:  </Location>
                    363: 
                    364: =head1 INTRODUCTION
                    365: 
                    366: This module uploads a file sitting on a client computer into 
                    367: library server construction space.
                    368: 
                    369: This is part of the LearningOnline Network with CAPA project
                    370: described at http://www.lon-capa.org.
                    371: 
                    372: =head1 HANDLER SUBROUTINE
                    373: 
                    374: This routine is called by Apache and mod_perl.
                    375: 
                    376: =over 4
                    377: 
                    378: =item *
                    379: 
                    380: Initialize variables
                    381: 
                    382: =item *
                    383: 
                    384: Start page output
                    385: 
                    386: =item *
                    387: 
                    388: output relevant interface phase (phaseone or phasetwo)
                    389: 
                    390: =item *
                    391: 
                    392: (phase one is to specify upload file; phase two is to handle conditions
                    393: subsequent to specification--like overwriting an existing file)
                    394: 
                    395: =back
                    396: 
                    397: =head1 OTHER SUBROUTINES
                    398: 
                    399: =over 4
                    400: 
                    401: =item *
                    402: 
                    403: phaseone() : Interface for specifying file to upload.
                    404: 
                    405: =item *
                    406: 
                    407: phasetwo() : Interface for handling post-conditions about uploading (such
                    408: as overwriting an existing file).
                    409: 
                    410: =item *
                    411: 
                    412: upfile_store() : Store contents of uploaded file into temporary space.  Invoked
                    413: by phaseone subroutine.
                    414: 
                    415: =back
                    416: 
                    417: =cut

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