--- loncom/publisher/loncfile.pm 2003/11/19 14:57:32 1.44 +++ loncom/publisher/loncfile.pm 2004/03/02 16:48:27 1.52 @@ -9,7 +9,7 @@ # and displays a page showing the results of the action. # # -# $Id: loncfile.pm,v 1.44 2003/11/19 14:57:32 taceyjo1 Exp $ +# $Id: loncfile.pm,v 1.52 2004/03/02 16:48:27 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -72,8 +72,9 @@ use Apache::loncacc; use Apache::Log (); use Apache::lonnet; use Apache::loncommon(); +use Apache::lonlocal; -my $DEBUG=2; +my $DEBUG=0; my $r; # Needs to be global for some stuff RF. =pod @@ -110,31 +111,12 @@ sub Debug { # Put out the indicated message butonly if DEBUG is true. if ($DEBUG) { - $r->log_reason($message); + $r->log_reason($message); } } =pod -=item checksuffix($old, $new) - - Determine if a resource filename suffix (the stuff after the .) would change -as a result of this operation. - - Parameters: - -=over 4 - -=item $old = string [in] Previous filename. - -=item $new = string [in] Resultant filename. - -=back - -=cut - -=pod - =item URLToPath($url) Convert a URL to a file system path. @@ -191,6 +173,28 @@ sub display { return ''.$fn.''; } + +# see if the file is +# a) published (return 0 if not) +# b) if, so obsolete (return 0 if not) + +sub obsolete_unpub { + my ($user,$domain,$construct)=@_; + my $published=$construct; + $published=~ + s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//; + if (-e $published) { + if (&Apache::lonnet::metadata($published,'obsolete')) { + return 1; + } + return 0; + } else { + return 1; + } +} + + + =pod =item exists($user, $domain, $file) @@ -230,12 +234,12 @@ sub exists { s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//; my $result=''; if ( -d $construct ) { - return 'Error: destination for operation is an existing directory.'; + return &mt('Error: destination for operation is an existing directory.'); } if ( -e $published) { - $result.='

Warning: target file exists, and has been published!

'; + $result.='

'.&mt('Warning: target file exists, and has been published!').'

'; } elsif ( -e $construct) { - $result.='

Warning: target file exists!

'; + $result.='

'.&mt('Warning: target file exists!').'

'; } return $result; @@ -279,7 +283,7 @@ sub checksuffix { if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; } if ($oldsuffix ne $newsuffix) { $result.= - '

Warning: change of MIME type!

'; + '

'.&mt('Warning: change of MIME type!').'

'; } return $result; } @@ -288,7 +292,7 @@ sub cleanDest { my ($request,$dest)=@_; #remove bad characters if ($dest=~/[\#\?&]/) { - $request->print("

Invalid characters in requested name have been removed.

"); + $request->print("

".&mt('Invalid characters in requested name have been removed.')."

"); $dest=~s/[\#\?&]//g; } return $dest; @@ -331,9 +335,9 @@ Parameters: sub CloseForm1 { my ($request, $fn) = @_; - $request->print('

'); + $request->print('

'); $request->print('

'); + '" method="POST">

'); } @@ -363,7 +367,7 @@ Parameters: sub CloseForm2 { my ($request, $user, $fn) = @_; - $request->print('

Done

'); + $request->print('

'.&mt('Done').'

'); } =pod @@ -402,13 +406,15 @@ new filename relative to the current dir =cut sub Rename1 { - my ($request, $user, $domain, $fn, $newfilename) = @_; + my ($request, $user, $domain, $fn, $newfilename, $style) = @_; if(-e $fn) { if($newfilename) { # is dest a dir - if (-d $newfilename) { - if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; } + if ($style eq 'move') { + if (-d $newfilename) { + if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; } + } } if ($newfilename =~ m|/[^\.]+$|) { #no extension add on original extension @@ -429,20 +435,31 @@ sub Rename1 { my $return=&exists($user, $domain, $newfilename); $request->print($return); if ($return =~/^Error:/) { - $request->print('
Cancel'); + $request->print('
'.&mt('Cancel').''); + return; + } + unless (&obsolete_unpub($user,$domain,$fn)) { + $request->print('

'.&mt('Cannot rename or move non-obsolete published file').'

'. + '
'.&mt('Cancel').''); return; } + my $action; + if ($style eq 'rename') { + $action=&mt('Rename'); + } else { + $action=&mt('Move'); + } $request->print('

Rename '.&display($fn). + '" />

'.$action.' '.&display($fn). '
to '.&display($newfilename).'?

'); &CloseForm1($request, $fn); } else { - $request->print('

No new filename specified.

'); + $request->print('

'.&mt('No new filename specified.').'

'); return; } } else { - $request->print('

No such file: '.&display($fn).'

'); + $request->print('

'.&mt('No such file').': '.&display($fn).'

'); return; } @@ -477,10 +494,15 @@ sub Delete1 { if( -e $fn) { $request->print(''); - $request->print('

Delete '.&display($fn).'?

'); + unless (&obsolete_unpub($user,$domain,$fn)) { + $request->print('

'.&mt('Cannot delete non-obsolete published file').'

'. + '
'.&mt('Cancel').''); + return; + } + $request->print('

'.&mt('Delete').' '.&display($fn).'?

'); &CloseForm1($request, $fn); } else { - $request->print('

No such file: '.&display($fn).'

'); + $request->print('

'.&mt('No such file').': '.&display($fn).'

'); } } @@ -531,16 +553,16 @@ sub Copy1 { my $return=&exists($user, $domain, $newfilename); $request->print($return); if ($return =~/^Error:/) { - $request->print('
Cancel'); + $request->print('
'.&mt('Cancel').''); return; } $request->print('

Copy '.&display($fn).'
to '. + '" />

'.&mt('Copy').' '.&display($fn).'
to '. &display($newfilename).'?

'); &CloseForm1($request, $fn); } else { - $request->print('

No such file: '.&display($fn).'

'); + $request->print('

'.&mt('No such file').': '.&display($fn).'

'); } } @@ -585,14 +607,19 @@ causes the newdir operation to transitio sub NewDir1 { - my ($request, $username, $domain, $fn, $newfilename) = @_; + my ($request, $username, $domain, $fn, $newfilename, $mode) = @_; my $result=&exists($username,$domain,$newfilename); if ($result) { $request->print(''.$result.''); } else { + if ($mode eq 'testbank') { + $request->print(''); + } elsif ($mode eq 'imsimport') { + $request->print(''); + } $request->print('

Make new directory '. + $newfilename.'" />

'.&mt('Make new directory').' '. &display($newfilename).'?

'); &CloseForm1($request, $fn); } @@ -603,10 +630,10 @@ sub Decompress1 { my ($request, $user, $domain, $fn) = @_; if( -e $fn) { $request->print(''); - $request->print('

Decompress '.&display($fn).'?

'); + $request->print('

'.&mt('Decompress').' '.&display($fn).'?

'); &CloseForm1($request, $fn); } else { - $request->print('

No such file: '.&display($fn).'

'); + $request->print('

'.&mt('No such file').': '.&display($fn).'

'); } } =pod @@ -659,8 +686,8 @@ sub NewFile1 { ##Informs User (name).(number).(extension) not allowed if($newfilename =~ /\.(\d+)\.(\w+)$/){ $r->print(''.$newfilename. - ' - Bad Filename
(name).(number).(extension)'. - ' Not Allowed
'); + ' - '.&mt('Bad Filename').'
('.&mt('name').').('.&mt('number').').('.&mt('extension').')'. + ' '.&mt('Not Allowed').''); return; } if ($newfilename !~ /\Q.$extension\E$/) { @@ -675,12 +702,12 @@ sub NewFile1 { if($result) { $request->print(''.$result.''); } else { - $request->print('

Make new file '.&display($newfilename).'?

'); + $request->print('

'.&mt('Make new file').' '.&display($newfilename).'?

'); $request->print(''); $request->print('

'); + '" method="POST">

'); $request->print('

'); + '" method="POST">

'); } } @@ -717,27 +744,31 @@ sub phaseone { my $newfilename=&cleanDest($r,$ENV{'form.newfilename'}); $newfilename=&relativeDest($fn,$newfilename,$uname); - &Debug($r, "Newfile: $newfilename"); $r->print('
'. ''. ''. ''); if ($ENV{'form.action'} eq 'rename') { - &Rename1($r, $uname, $udom, $fn, $newfilename); + &Rename1($r, $uname, $udom, $fn, $newfilename, 'rename'); + } elsif ($ENV{'form.action'} eq 'move') { + &Rename1($r, $uname, $udom, $fn, $newfilename, 'move'); } elsif ($ENV{'form.action'} eq 'delete') { &Delete1($r, $uname, $udom, $fn); } elsif ($ENV{'form.action'} eq 'decompress') { - &Apache::lonnet::appenv('cgi.path' => $newfilename); &Decompress1($r, $uname, $udom, $fn); } elsif ($ENV{'form.action'} eq 'copy') { if($newfilename) { &Copy1($r, $uname, $udom, $fn, $newfilename); } else { - $r->print('

No new filename specified.

'); + $r->print('

'.&mt('No new filename specified.').'

'); } } elsif ($ENV{'form.action'} eq 'newdir') { - &NewDir1($r, $uname, $udom, $fn, $newfilename); + my $mode = ''; + if (exists($ENV{'form.callingmode'}) ) { + $mode = $ENV{'form.callingmode'}; + } + &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode); } elsif ($ENV{'form.action'} eq 'newfile' || $ENV{'form.action'} eq 'newhtmlfile' || $ENV{'form.action'} eq 'newproblemfile' || @@ -749,7 +780,7 @@ sub phaseone { if ($newfilename) { &NewFile1($r, $uname, $udom, $fn, $newfilename); } else { - $r->print('

No new filename specified.

'); + $r->print('

'.&mt('No new filename specified.').'

'); } } } @@ -801,7 +832,7 @@ sub Rename2 { my $oRN=$oldfile; my $nRN=$newfile; unless (rename($oldfile,$newfile)) { - $request->print('Error: '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } ## If old name.(extension) exits, move under new name. @@ -836,7 +867,7 @@ sub Rename2 { unlink $tmp2; } } else { - $request->print("

No such file: ".&display($oldfile).'

'); + $request->print("

".&mt('No such file').": ".&display($oldfile).'

'); return 0; } return 1; @@ -872,16 +903,34 @@ Returns: sub Delete2 { my ($request, $user, $filename) = @_; - - if(-e $filename) { - unless(unlink($filename)) { - $request->print('Error: '.$!.''); + if(opendir DIR, $filename) { + my @files=readdir(DIR); + shift @files; shift @files; # takes off . and .. + if(@files) { + $request->print(' '.&mt('Error: Directory Non Empty').''); + return 0; + } else { + if(-e $filename) { + unless(rmdir($filename)) { + $request->print(''.&mt('Error').': '.$!.''); + return 0; + } + } else { + $request->print('

'.&mt('No such file').'.

'); + return 0; + } + } + } else { + if(-e $filename) { + unless(unlink($filename)) { + $request->print(''.&mt('Error').': '.$!.''); + return 0; + } + } else { + $request->print('

'.&mt('No such file').'.

'); return 0; - } - } else { - $request->print('

No such file.

print(' copy Error: '.$!.''); + $request->print(' '.&mt('copy Error').': '.$!.''); return 0; } else { unless (chmod(0660, $newfile)) { - $request->print(' chmod error: '.$!.''); + $request->print(' '.&mt('chmod error').': '.$!.''); return 0; } return 1; } } else { - $request->print('

No such file

'); + $request->print('

'.&mt('No such file').'

'); return 0; } return 1; @@ -959,11 +1008,11 @@ sub NewDir2 { my ($request, $user, $newdirectory) = @_; unless(mkdir($newdirectory, 02770)) { - $request->print('Error: '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } unless(chmod(02770, ($newdirectory))) { - $request->print(' Error: '.$!.''); + $request->print(' '.&mt('Error').': '.$!.''); return 0; } return 1; @@ -1020,13 +1069,14 @@ sub phasetwo { my $dir; # Directory path my $main; # Filename. my $suffix; # Extension. - - if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) { + if ($fn=~m:(.*)/([^/]+):) { $dir=$1; # Directory path $main=$2; # Filename. - $suffix=$3; # Extension. - } - + } + if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions + $main=$`; #This is what is before the match (.) so it's just the main filename, yea it's nasty + $suffix=$1; #This is the actually filename extension if it exists + } my $dest; # On success this is where we'll go. &Debug($r, @@ -1079,7 +1129,7 @@ sub phasetwo { $dest = $ENV{'form.newfilename'}; } else { - $r->print('

No New filename specified

'); + $r->print('

'.&mt('No New filename specified').'

'); return; } @@ -1090,7 +1140,11 @@ sub phasetwo { } $dest = $newdir."/" } - $r->print('

Done

'); + if ( ($ENV{'form.action'} eq 'newdir') && ($ENV{'form.phase'} eq 'two') && ( ($ENV{'form.callingmode'} eq 'testbank') || ($ENV{'form.callingmode'} eq 'imsimport') ) ) { + $r->print('

'.&mt('Done').'

'); + } else { + $r->print('

'.&mt('Done').'

'); + } } sub handler { @@ -1114,6 +1168,7 @@ sub handler { $fn=&Apache::lonnet::unescape($ENV{'form.filename'}); $fn=&URLToPath($fn); } + #Just hijack the script only the first time around to inject the correct information for further processing elsif($ENV{'QUERY_STRING'} && $ENV{'form.phase'} ne 'two') { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress']); $fn=&Apache::lonnet::unescape($ENV{'form.decompress'}); @@ -1154,18 +1209,33 @@ sub handler { } - $r->content_type('text/html'); + &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; - $r->print('LON-CAPA Construction Space'); - - $r->print(&Apache::loncommon::bodytag('Construction Space File Operation')); + if ( ($ENV{'form.action'} eq 'newdir') && ($ENV{'form.phase'} eq 'two') && ( ($ENV{'form.callingmode'} eq 'testbank') || ($ENV{'form.callingmode'} eq 'imsimport') ) ) { + my $newdirname = $ENV{'form.newfilename'}; + $r->print('LON-CAPA Construction Space + |); + my $loaditem = 'onLoad="writeDone()"'; + $r->print(&Apache::loncommon::bodytag('Construction Space File Operation','',$loaditem)); + } else { + $r->print('LON-CAPA Construction Space'); + $r->print(&Apache::loncommon::bodytag('Construction Space File Operation')); + } - $r->print('

Location: '.&display($fn).'

'); + $r->print('

'.&mt('Location').': '.&display($fn).'

'); if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) { - $r->print('

Co-Author: '.$uname.' at '.$udom. + $r->print('

'.&mt('Co-Author').': '.$uname.' at '.$udom. '

'); } @@ -1173,15 +1243,17 @@ sub handler { &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} "); if ($ENV{'form.action'} eq 'delete') { - $r->print('

Delete

'); + $r->print('

'.&mt('Delete').'

'); } elsif ($ENV{'form.action'} eq 'rename') { - $r->print('

Rename

'); + $r->print('

'.&mt('Rename').'

'); + } elsif ($ENV{'form.action'} eq 'move') { + $r->print('

'.&mt('Move').'

'); } elsif ($ENV{'form.action'} eq 'newdir') { - $r->print('

New Directory

'); + $r->print('

'.&mt('New Directory').'

'); } elsif ($ENV{'form.action'} eq 'decompress') { - $r->print('

Decompress

'); + $r->print('

'.&mt('Decompress').'

'); } elsif ($ENV{'form.action'} eq 'copy') { - $r->print('

Copy

'); + $r->print('

'.&mt('Copy').'

'); } elsif ($ENV{'form.action'} eq 'newfile' || $ENV{'form.action'} eq 'newhtmlfile' || $ENV{'form.action'} eq 'newproblemfile' || @@ -1190,9 +1262,9 @@ sub handler { $ENV{'form.action'} eq 'newrightsfile' || $ENV{'form.action'} eq 'newstyfile' || $ENV{'form.action'} eq 'Select Action' ) { - $r->print('

New Resource

'); + $r->print('

'.&mt('New Resource').'

'); } else { - $r->print('

Unknown Action '.$ENV{'form.action'}.'

'); + $r->print('

'.&mt('Unknown Action').' '.$ENV{'form.action'}.'

'); return OK; } if ($ENV{'form.phase'} eq 'two') {