version 1.1, 2001/04/05 19:35:15
|
version 1.4, 2001/05/25 17:03:58
|
Line 15
|
Line 15
|
# |
# |
# 03/31,04/03 Gerd Kortemeyer) |
# 03/31,04/03 Gerd Kortemeyer) |
# |
# |
# 04/05 Gerd Kortemeyer |
# 04/05,04/09,05/25 Gerd Kortemeyer |
|
|
package Apache::lonupload; |
package Apache::lonupload; |
|
|
Line 23 use strict;
|
Line 23 use strict;
|
use Apache::File; |
use Apache::File; |
use File::Copy; |
use File::Copy; |
use Apache::Constants qw(:common :http :methods); |
use Apache::Constants qw(:common :http :methods); |
|
use Apache::loncacc; |
|
|
sub phaseone { |
sub upfile_store { |
my ($r,$fn,$uname,$udom)=@_; |
my $r=shift; |
my $docroot=$r->dir_config('lonDocRoot'); |
|
|
my $fname=$ENV{'form.upfile.filename'}; |
my $urldir='/res/'.$udom.'/'.$uname.$fn; |
$fname=~s/\W//g; |
$urldir=~s/\/[^\/]+$/\//; |
|
|
chop($ENV{'form.upfile'}); |
my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn; |
|
my $resdir=$resfn; |
|
$resdir=~s/\/[^\/]+$/\//; |
|
|
|
$fn=~/^\/(.+)\.(\w+)$/; |
|
my $main=$1; |
|
my $suffix=$2; |
|
|
|
$r->print('<form action=/adm/retrieve method=post>'. |
my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}. |
'<input type=hidden name=filename value="'.$fn.'">'. |
'_upload_'.$fname.'_'.time.'_'.$$; |
'<input type=hidden name=phase value=two>'. |
{ |
'<table border=2><tr><th>Select</th><th>Version</th>'. |
my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons'). |
'<th>Became this version on ...</th>'. |
'/tmp/'.$datatoken.'.tmp'); |
'<th>Metadata</th></tr>'); |
print $fh $ENV{'form.upfile'}; |
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>'); |
|
} |
|
} |
} |
closedir(DIR); |
return $datatoken; |
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>'); |
|
} |
} |
|
|
sub phasetwo { |
|
|
sub phaseone { |
my ($r,$fn,$uname,$udom)=@_; |
my ($r,$fn,$uname,$udom)=@_; |
if ($ENV{'form.version'}) { |
$fn=~s/\/[^\/]+$//; |
my $version=$ENV{'form.version'}; |
$fn=~s/([^\/])$/$1\//; |
if ($version eq 'new') { |
$fn.=$ENV{'form.upfile.filename'}; |
$r->print('<h3>Retrieving current (most recent) version</h3>'); |
$fn=~s/^\///; |
} else { |
$fn=~s/(\/)+/\//g; |
$r->print('<h3>Retrieving old version '.$version.'</h3>'); |
|
} |
if (($fn) && ($fn!~/\/$/)) { |
my $logfile; |
$r->print( |
my $ctarget='/home/'.$uname.'/public_html'.$fn; |
'<form action=/adm/upload method=post>'. |
my $vfn=$fn; |
'<input type=hidden name=phase value=two>'. |
if ($version ne 'new') { |
'<input type=hidden name=datatoken value="'.&upfile_store.'">'. |
$vfn=~s/\.(\w+)$/\.$version\.$1/; |
'Store uploaded file as '. |
} |
'<input type=text size=50 name=filename value="/priv/'. |
my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn; |
$uname.'/'.$fn.'"><br>'. |
unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) { |
'<input type=submit value="Store"></form>'); |
$r->print( |
} else { |
'<font color=red>No write permission to user directory, FAIL</font>'); |
$r->print('<font color=red>Illegal filename.</font>'); |
} |
} |
print $logfile |
} |
"\n\n================= Retrieve ".localtime()." ================\n". |
|
"Version: $version\nSource: $csource\nTarget: $ctarget\n"; |
sub phasetwo { |
$r->print('<p>Copying file: '); |
my ($r,$fn,$uname,$udom)=@_; |
if (copy($csource,$ctarget)) { |
if ($fn=~/^\/priv\/$uname\//) { |
$r->print('ok<p>'); |
my $tfn=$fn; |
print $logfile "Copied sucessfully.\n\n"; |
$tfn=~s/^\/(\~|priv)\/(\w+)//; |
} else { |
my $target='/home/'.$uname.'/public_html'.$tfn; |
my $error=$!; |
my $datatoken=$ENV{'form.datatoken'}; |
$r->print('fail, '.$error.'<p>'); |
if (($fn) && ($datatoken)) { |
print $logfile "Copy failed: $error\n\n"; |
if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) { |
} |
$r->print( |
$r->print('<font size=+2><a href="/priv/'.$uname.$fn. |
'<form action=/adm/upload method=post>'. |
'">Back to '.$fn.'</a></font>'); |
'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 { |
} else { |
$r->print( |
$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); |
&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 { |
sub handler { |
|
|
my $r=shift; |
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; |
my $fn; |
|
|
if ($ENV{'form.filename'}) { |
if ($ENV{'form.filename'}) { |
$fn=$ENV{'form.filename'}; |
$fn=$ENV{'form.filename'}; |
$fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//; |
$fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//; |
} else { |
} else { |
$r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}. |
$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; |
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 |
# ----------------------------------------------------------- Start page output |
|
|
my $uname=$ENV{'user.name'}; |
|
my $udom=$ENV{'user.domain'}; |
|
|
|
$r->content_type('text/html'); |
$r->content_type('text/html'); |
$r->send_http_header; |
$r->send_http_header; |
Line 151 sub handler {
|
Line 145 sub handler {
|
'<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>'); |
'<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') { |
if ($ENV{'form.phase'} eq 'two') { |
&phasetwo($r,$fn,$uname,$udom); |
&phasetwo($r,$fn,$uname,$udom); |