--- loncom/publisher/lonupload.pm 2001/04/05 19:35:15 1.1 +++ loncom/publisher/lonupload.pm 2001/05/25 17:03:58 1.4 @@ -15,7 +15,7 @@ # # 03/31,04/03 Gerd Kortemeyer) # -# 04/05 Gerd Kortemeyer +# 04/05,04/09,05/25 Gerd Kortemeyer package Apache::lonupload; @@ -23,124 +23,118 @@ use strict; use Apache::File; use File::Copy; use Apache::Constants qw(:common :http :methods); +use Apache::loncacc; -sub phaseone { - my ($r,$fn,$uname,$udom)=@_; - my $docroot=$r->dir_config('lonDocRoot'); - - my $urldir='/res/'.$udom.'/'.$uname.$fn; - $urldir=~s/\/[^\/]+$/\//; - - my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn; - my $resdir=$resfn; - $resdir=~s/\/[^\/]+$/\//; - - $fn=~/^\/(.+)\.(\w+)$/; - my $main=$1; - my $suffix=$2; +sub upfile_store { + my $r=shift; + + my $fname=$ENV{'form.upfile.filename'}; + $fname=~s/\W//g; + + chop($ENV{'form.upfile'}); - $r->print('<form action=/adm/retrieve method=post>'. - '<input type=hidden name=filename value="'.$fn.'">'. - '<input type=hidden name=phase value=two>'. - '<table border=2><tr><th>Select</th><th>Version</th>'. - '<th>Became this version on ...</th>'. - '<th>Metadata</th></tr>'); - my $filename; - opendir(DIR,$resdir); - while ($filename=readdir(DIR)) { - if ($filename=~/^$main\.(\d+)\.$suffix$/) { - my $version=$1; - my ($rdev,$rino,$rmode,$rnlink, - $ruid,$rgid,$rrdev,$rsize, - $ratime,$rmtime,$rctime, - $rblksize,$rblocks)=stat($resdir.'/'.$filename); - $r->print('<tr><td><input type=radio name=version value="'. - $version.'"></td><th>'.$version.'</th><td>'. - localtime($rmtime).'</td><td>'. - '<a href="'.$urldir.$filename.'.meta" target=cat>'. - 'Metadata Version '.$version.'</a></td></tr>'); - } + my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}. + '_upload_'.$fname.'_'.time.'_'.$$; + { + my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons'). + '/tmp/'.$datatoken.'.tmp'); + print $fh $ENV{'form.upfile'}; } - closedir(DIR); - my ($rdev,$rino,$rmode,$rnlink, - $ruid,$rgid,$rrdev,$rsize, - $ratime,$rmtime,$rctime, - $rblksize,$rblocks)=stat($resfn); - $r->print('<tr><td><input type=radio name=version value="new"></td>'. - '<th>Current</th><td>'.localtime($rmtime). - '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'. - 'Metadata current version</a></td></tr></table><p>'. - '<font size=+1 color=red>Retrieval of an old version will '. - 'overwrite the file currently in construction space</font><p>'. - '<input type=submit value="Retrieve version"></form>'); + return $datatoken; } -sub phasetwo { + +sub phaseone { my ($r,$fn,$uname,$udom)=@_; - if ($ENV{'form.version'}) { - my $version=$ENV{'form.version'}; - if ($version eq 'new') { - $r->print('<h3>Retrieving current (most recent) version</h3>'); - } else { - $r->print('<h3>Retrieving old version '.$version.'</h3>'); - } - my $logfile; - my $ctarget='/home/'.$uname.'/public_html'.$fn; - my $vfn=$fn; - if ($version ne 'new') { - $vfn=~s/\.(\w+)$/\.$version\.$1/; - } - my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn; - unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) { - $r->print( - '<font color=red>No write permission to user directory, FAIL</font>'); - } - print $logfile -"\n\n================= Retrieve ".localtime()." ================\n". -"Version: $version\nSource: $csource\nTarget: $ctarget\n"; - $r->print('<p>Copying file: '); - if (copy($csource,$ctarget)) { - $r->print('ok<p>'); - print $logfile "Copied sucessfully.\n\n"; - } else { - my $error=$!; - $r->print('fail, '.$error.'<p>'); - print $logfile "Copy failed: $error\n\n"; - } - $r->print('<font size=+2><a href="/priv/'.$uname.$fn. - '">Back to '.$fn.'</a></font>'); + $fn=~s/\/[^\/]+$//; + $fn=~s/([^\/])$/$1\//; + $fn.=$ENV{'form.upfile.filename'}; + $fn=~s/^\///; + $fn=~s/(\/)+/\//g; + + if (($fn) && ($fn!~/\/$/)) { + $r->print( + '<form action=/adm/upload method=post>'. + '<input type=hidden name=phase value=two>'. + '<input type=hidden name=datatoken value="'.&upfile_store.'">'. + 'Store uploaded file as '. + '<input type=text size=50 name=filename value="/priv/'. + $uname.'/'.$fn.'"><br>'. + '<input type=submit value="Store"></form>'); + } else { + $r->print('<font color=red>Illegal filename.</font>'); + } +} + +sub phasetwo { + my ($r,$fn,$uname,$udom)=@_; + if ($fn=~/^\/priv\/$uname\//) { + my $tfn=$fn; + $tfn=~s/^\/(\~|priv)\/(\w+)//; + my $target='/home/'.$uname.'/public_html'.$tfn; + my $datatoken=$ENV{'form.datatoken'}; + if (($fn) && ($datatoken)) { + if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) { + $r->print( + '<form action=/adm/upload method=post>'. + 'File <tt>'.$fn.'</tt> exists. Overwrite? '. + '<input type=hidden name=phase value=two>'. + '<input type=hidden name=filename value="'.$fn.'">'. + '<input type=hidden name=datatoken value="'.$datatoken.'">'. + '<input type=submit name=override value="Yes"></form>'); + } else { + my $source=$r->dir_config('lonDaemons'). + '/tmp/'.$datatoken.'.tmp'; + if (copy($source,$target)) { + $r->print('File copied.'); + $r->print('<p><font size=+2><a href="'.$fn. + '">View file</a></font>'); + } else { + $r->print('Failed to copy: '.$!); + } + } } else { $r->print( - '<font size=+1 color=red>Please pick a version to retrieve</font><p>'); + '<font size=+1 color=red>Please pick a filename</font><p>'); &phaseone($r,$fn,$uname,$udom); } + } else { + $r->print( + '<font size=+1 color=red>Please pick a filename</font><p>'); + &phaseone($r,$fn,$uname,$udom); + } } sub handler { my $r=shift; + my $uname; + my $udom; + + unless (($uname,$udom)= + &Apache::loncacc::constructaccess( + $ENV{'form.filename'},$r->dir_config('lonDefDomain'))) { + $r->log_reason($uname.' at '.$udom. + ' trying to publish file '.$ENV{'form.filename'}. + ' - not authorized', + $r->filename); + return HTTP_NOT_ACCEPTABLE; + } + my $fn; if ($ENV{'form.filename'}) { $fn=$ENV{'form.filename'}; - $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//; + $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//; } else { $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}. - ' unspecified filename for retrieval', $r->filename); + ' unspecified filename for upload', $r->filename); return HTTP_NOT_FOUND; } - unless ($fn) { - $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}. - ' trying to retrieve non-existing file', $r->filename); - return HTTP_NOT_FOUND; - } - # ----------------------------------------------------------- Start page output - my $uname=$ENV{'user.name'}; - my $udom=$ENV{'user.domain'}; $r->content_type('text/html'); $r->send_http_header; @@ -151,7 +145,13 @@ sub handler { '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>'); - $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>'); + $r->print('<h1>Upload file to Construction Space</h1>'); + + if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) { + $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom. + '</font></h3>'); + } + if ($ENV{'form.phase'} eq 'two') { &phasetwo($r,$fn,$uname,$udom);