--- loncom/publisher/loncfile.pm 2006/11/22 22:10:58 1.78 +++ loncom/publisher/loncfile.pm 2007/10/18 21:58:21 1.87 @@ -9,7 +9,7 @@ # and displays a page showing the results of the action. # # -# $Id: loncfile.pm,v 1.78 2006/11/22 22:10:58 banghart Exp $ +# $Id: loncfile.pm,v 1.87 2007/10/18 21:58:21 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -72,9 +72,8 @@ use Apache::loncacc; use Apache::lonnet; use Apache::loncommon(); use Apache::lonlocal; -use lib '/home/httpd/lib/perl/'; -use LONCAPA; - +use LONCAPA qw(:DEFAULT :match); + my $DEBUG=0; my $r; # Needs to be global for some stuff RF. @@ -151,22 +150,22 @@ sub URLToPath { $Url=~ s/\/+/\//g; $Url=~ s/^http\:\/\/[^\/]+//; $Url=~ s/^\///; - $Url=~ s/(\~|priv\/)(\w+)\//\/home\/$2\/public_html\//; + $Url=~ s/(\~|priv\/)($match_username)\//\/home\/$2\/public_html\//; &Debug($r, "Returning $Url \n"); return $Url; } sub url { my $fn=shift; - $fn=~s/^\/home\/(\w+)\/public\_html/\/priv\/$1/; + $fn=~s/^\/home\/($match_username)\/public\_html/\/priv\/$1/; $fn=&HTML::Entities::encode($fn,'<>"&'); return $fn; } sub display { my $fn=shift; - $fn=~s-^/home/(\w+)/public_html-/priv/$1-; - return ''.$fn.''; + $fn=~s-^/home/($match_username)/public_html-/priv/$1-; + return ''.$fn.''; } @@ -227,12 +226,16 @@ sub empty_directory { =over 4 -=item $user - string [in] - Name of the user for which to check. +=item $user - string [in] - Name of the user for which to check. -=item $domain - string [in] - Name of the domain in which the resource +=item $domain - string [in] - Name of the domain in which the resource might have been published. -=item $file - string [in] - Name of the file. +=item $file - string [in] - Name of the file. + +=item $creating - string [in] - optional, type of object being created, + either 'directory' or 'file'. Defaults to + 'file' if unspecified. =back @@ -240,6 +243,9 @@ Returns: =over 4 +=item string - Either undef, 'warning' or 'error' depending on the + type of problem + =item string - Either where the resource exists as an html string that can be embedded in a dialog or an empty string if the resource does not exist. @@ -249,20 +255,39 @@ Returns: =cut sub exists { - my ($user, $domain, $construct) = @_; + my ($user, $domain, $construct, $creating) = @_; + $creating ||= 'file'; + my $published=$construct; $published=~ - s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//; - my $result=''; + s{^/home/$user/public_html/}{/home/httpd/html/res/$domain/$user/}; + my ($type,$result); if ( -d $construct ) { - return &mt('Error: destination for operation is an existing directory.'); + return ('error','

'.&mt('Error: destination for operation is an existing directory.').'

'); + } + if ( -e $published) { - $result.='

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

'; + if ( -e $construct ) { + $type = 'warning'; + $result.='

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

'; + } else { + my $published_type = (-d $published) ? 'directory' : 'file'; + + if ($published_type eq $creating) { + $type = 'warning'; + $result.='

'.&mt("Warning: a published $published_type of this name exists.").'

'; + } else { + $type = 'error'; + $result.='

'.&mt("Error: a published $published_type of this name exists.").'

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

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

'; + $type = 'warning'; + $result.='

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

'; } - return $result; + + return ($type,$result); } =pod @@ -301,9 +326,9 @@ sub checksuffix { my $newsuffix; if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; } if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; } - if ($oldsuffix ne $newsuffix) { + if (lc($oldsuffix) ne lc($newsuffix)) { $result.= - '

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

'; + '

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

'; } return $result; } @@ -316,6 +341,7 @@ sub cleanDest { $foundbad=1; $dest=~s/\.//g; } + $dest =~ s/(\s+$|^\s+)//g; if ($dest=~/[\#\?&%\":]/) { $foundbad=1; $dest=~s/[\#\?&%\":]//g; @@ -324,12 +350,18 @@ sub cleanDest { my ($newpath)=($dest=~m|(.*)/|); $newpath=&relativeDest($fn,$newpath,$uname); if (! -d "$newpath") { - $request->print("

".&mt('You have requested to create file in directory [_1] which doesn\'t exist. The requested directory path has been removed from the requested file name.','"'.$newpath.'"')."

"); + $request->print("

".&mt('You have requested to create file in directory [_1] which doesn\'t exist. The requested directory path has been removed from the requested file name.','"'.&display($newpath).'"')."

"); $dest=~s|.*/||; } } + if ($dest =~ /\.(\d+)\.(\w+)$/){ + $request->print('' + .&mt('Bad filename [_1].
(name).(number).(extension) not allowed.
Removing the .number. from requested filename.',&display($dest)) + .'
'); + $dest =~ s/\.(\d+)(\.\w+)$/$2/; + } if ($foundbad) { - $request->print("

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

"); + $request->print("

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

"); } return $dest; } @@ -464,9 +496,9 @@ sub Rename1 { if (-d $fn) { $newfilename=~/\.(\w+)$/; if (&Apache::loncommon::fileembstyle($1) eq 'ssi') { - $request->print('
'. + $request->print('
'. &mt('Cannot change MIME type of a directory'). - '
'. + ''. '
'.&mt('Cancel').''); return; } @@ -476,9 +508,9 @@ sub Rename1 { while ($newfilename=~m:/\.\./:) { $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/.. } - my $return=&exists($user, $domain, $newfilename); + my ($type, $return)=&exists($user, $domain, $newfilename); $request->print($return); - if ($return =~/^Error:/) { + if ($type eq 'error') { $request->print('
'.&mt('Cancel').''); return; } @@ -496,7 +528,7 @@ sub Rename1 { $request->print('

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

'); + '


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

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

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

'); @@ -604,9 +636,9 @@ sub Copy1 { $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/.. } $request->print(&checksuffix($fn,$newfilename)); - my $return=&exists($user, $domain, $newfilename); + my ($type,$return)=&exists($user, $domain, $newfilename); $request->print($return); - if ($return =~/^Error:/) { + if ($type eq 'error') { $request->print('
'.&mt('Cancel').''); return; } @@ -662,9 +694,10 @@ causes the newdir operation to transitio sub NewDir1 { my ($request, $username, $domain, $fn, $newfilename, $mode) = @_; - my $result=&exists($username,$domain,$newfilename); - if ($result) { - $request->print(''.$result.''); + my ($type, $result)=&exists($username,$domain,$newfilename,'directory'); + $request->print($result); + if ($type eq 'error') { + $request->print(''); } else { if ($mode eq 'testbank') { $request->print(''); @@ -738,29 +771,31 @@ sub NewFile1 { ##Informs User (name).(number).(extension) not allowed if($newfilename =~ /\.(\d+)\.(\w+)$/){ - $r->print(''.$newfilename. + $r->print(''.$newfilename. ' - '.&mt('Bad Filename').'
('.&mt('name').').('.&mt('number').').('.&mt('extension').') '. - ' '.&mt('Not Allowed').'
'); + ' '.&mt('Not Allowed').''); return; } if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){ - $r->print(''.$newfilename. + $r->print(''.$newfilename. ' - '.&mt('Bad Filename').'
('.&mt('Must not include').' '.$1.') '. - ' '.&mt('Not Allowed').'
'); + ' '.&mt('Not Allowed').''); return; } if ($newfilename !~ /\Q.$extension\E$/) { - if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) { + if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) { #already has an extension strip it and add in expected one - $newfilename =~ s|.([^\.]+)$||; + $newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|; } $newfilename.=".$extension"; } } - my $result=&exists($user,$domain,$newfilename); - if($result) { - $request->print(''.$result.''); + my ($type, $result)=&exists($user,$domain,$newfilename); + $request->print($result); + if ($type eq 'error') { + $request->print(''); } else { + $request->print('

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

'); $request->print(''); $request->print('
'.&mt('Error').': '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } ## If old name.(extension) exits, move under new name. @@ -969,12 +1005,12 @@ sub Delete2 { my ($request, $user, $filename) = @_; if (-d $filename) { unless (&empty_directory($filename,'Delete2')) { - $request->print(' '.&mt('Error: Directory Non Empty').''); + $request->print(''.&mt('Error: Directory Non Empty').''); return 0; } else { if(-e $filename) { unless(rmdir($filename)) { - $request->print(''.&mt('Error').': '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } } else { @@ -985,7 +1021,7 @@ sub Delete2 { } else { if(-e $filename) { unless(unlink($filename)) { - $request->print(''.&mt('Error').': '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } } else { @@ -1029,20 +1065,20 @@ sub Copy2 { &Debug($request ,"Will try to copy $oldfile to $newfile"); if(-e $oldfile) { if ($oldfile eq $newfile) { - $request->print(' '.&mt('Warning').': '.&mt('Name of new file is the same as name of old file').' - '.&mt('no action taken').'.'); + $request->print(''.&mt('Warning').': '.&mt('Name of new file is the same as name of old file').' - '.&mt('no action taken').'.'); return 1; } unless (copy($oldfile, $newfile)) { - $request->print(' '.&mt('copy Error').': '.$!.''); + $request->print(''.&mt('copy Error').': '.$!.''); return 0; } elsif (!chmod(0660, $newfile)) { - $request->print(' '.&mt('chmod error').': '.$!.''); + $request->print(''.&mt('chmod error').': '.$!.''); return 0; } elsif (-e $oldfile.'.meta' && !copy($oldfile.'.meta', $newfile.'.meta') && !chmod(0660, $newfile.'.meta')) { - $request->print(' '.&mt('copy metadata error'). - ': '.$!.''); + $request->print(''.&mt('copy metadata error'). + ': '.$!.''); return 0; } else { return 1; @@ -1080,11 +1116,11 @@ sub NewDir2 { my ($request, $user, $newdirectory) = @_; unless(mkdir($newdirectory, 02770)) { - $request->print(''.&mt('Error').': '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } unless(chmod(02770, ($newdirectory))) { - $request->print(' '.&mt('Error').': '.$!.''); + $request->print(''.&mt('Error').': '.$!.''); return 0; } return 1; @@ -1151,8 +1187,10 @@ sub phasetwo { $suffix=$1; #This is the actually filename extension if it exists $main=~s/\.\w+$//; #strip the extension } - my $dest; # On success this is where we'll go. - + my $dest; # + my $dest_dir; # On success this is where we'll go. + my $disp_newname; # + my $dest_newname; # &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix"); &Debug($r," newfilename = ".$env{'form.newfilename'}); @@ -1180,6 +1218,10 @@ sub phasetwo { return; } $dest = $dir."/"; + $dest_newname = $env{'form.newfilename'}; + $env{'form.newfilename'} =~ /.+(\/.+$)/; + $disp_newname = $1; + $disp_newname =~ s/\///; } } elsif ($env{'form.action'} eq 'delete') { if(!&Delete2($r, $uname, $env{'form.newfilename'})) { @@ -1210,7 +1252,12 @@ sub phasetwo { 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').'

'); + if ($env{'form.action'} eq 'rename') { + $r->print('

'.&mt('Return to Directory').'

'); + $r->print('

'.$disp_newname.'

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

'.&mt('Done').'

'); + } } } @@ -1300,8 +1347,8 @@ function writeDone() { $r->print('

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

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

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

'); + $r->print('

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

'); } @@ -1325,6 +1372,7 @@ function writeDone() { $env{'form.action'} eq 'newsequencefile' || $env{'form.action'} eq 'newrightsfile' || $env{'form.action'} eq 'newstyfile' || + $env{'form.action'} eq 'newtaskfile' || $env{'form.action'} eq 'newlibraryfile' || $env{'form.action'} eq 'Select Action' ) { $r->print('

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

');