--- loncom/publisher/loncfile.pm 2003/01/09 22:11:52 1.21 +++ loncom/publisher/loncfile.pm 2003/06/19 21:04:37 1.32 @@ -7,10 +7,9 @@ # presents a page that describes the proposed action to the user # and requests confirmation. The second phase commits the action # and displays a page showing the results of the action. -# - # -# $Id: loncfile.pm,v 1.21 2003/01/09 22:11:52 albertel Exp $ +# +# $Id: loncfile.pm,v 1.32 2003/06/19 21:04:37 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -128,7 +127,7 @@ sub Debug { my $log = $r->log; my $message = shift; - # Put out the indicated message butonly if DEBUG is false. + # Put out the indicated message butonly if DEBUG is true. if ($DEBUG) { $log->debug($message); @@ -336,10 +335,10 @@ sub exists { return 'Error: destination for operation is a directory.'; } if ( -e $published) { - $result.='
Warning: target file exists, and has been published!
'; + $result.='Warning: target file exists, and has been published!
'; } elsif ( -e $construct) { - $result.='Warning: target file exists!
'; + $result.='Warning: target file exists!
'; } return $result; @@ -384,10 +383,21 @@ sub checksuffix { if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; } if ($oldsuffix ne $newsuffix) { $result.= - 'Warning: change of MIME type!
'; + 'Warning: change of MIME type!
'; } return $result; } + +sub cleanDest { + my ($request,$dest)=@_; + #remove bad characters + if ($dest=~/[\#\?&]/) { + $request->print("Invalid characters in requested name have been removed.
"); + $dest=~s/[\#\?&]//g; + } + return $dest; +} + =pod =item CloseForm1($request, $user, $file) @@ -411,9 +421,9 @@ sub CloseForm1 { &Debug($request, "Cancel url is: ".$cancelurl); - $request->print(''); + $request->print(''); $request->print(''); + '" method="POST">'); } @@ -445,7 +455,7 @@ Parameters: sub CloseForm2 { my ($request, $user, $directory) = @_; - $request->print('Rename '.$filename.'
to '.
+ '" />
Rename '.$filename.
+ '
to '.
$dest.'?
Delete '.$filename.'?
'); &CloseForm1($request, $cancelurl); } else { @@ -606,9 +629,9 @@ sub Copy1 { return; } my $dest=&SimplifyDir($dir,$newfilename); - $request->print('Copy '.$filename.'
to '.
+ '" />
Copy '.$filename.'
to '.
''.$dest.'?
Directory exists.
'); } else { - $request->print('Make new directory '. + $request->print('
Make new directory '. $path."/".$newdir.'?
'); &CloseForm1($request, $cancelurl); @@ -706,6 +729,88 @@ sub NewDir1 =pod +=item NewFile1 + + Does all phase 1 processing of file creation: + Ensures that the user provides a new filename, adds proper extension + if needed and that the file does not already exist, if it is a html, + problem, page, or sequence, it then creates a form link to hand the + actual creation off to the proper handler. + +Parameters: + +=over 4 + +=item $request - Apache Request Object [in] - Server request object for the + current url. + +=item $username - Name of the user that is requesting the directory creation. + +=item $domain - Name of the domain of the user + +=item $dir - current absolute diretory + +=item $newfilename + - Name of the file to be created; no path information +=back + +Side Effects: + +=over 4 + +=item 2 new forms are displayed. Clicking on the confirmation button +causes the browser to attempt to load the specfied URL, allowing the +proper handler to take care of file creation. There is also a Cancle +button which returns you to the driectory listing you came from + +=back + +=cut + + +sub NewFile1 { + my ($request, $user, $domain, $dir, $newfilename) = @_; + + &Debug($request, "Dir is : ".$dir); + &Debug($request, "Newfile is : ".$newfilename); + + my $cancelurl = "/priv/".$dir; + $cancelurl =~ s/\/home\///; + $cancelurl =~ s/\/public_html//; + + if ($ENV{'form.action'} =~ /new(.+)file/) { + my $extension=$1; + if ($newfilename !~ /\Q.$extension\E$/) { + if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) { + #already has an extension strip it and add in expected one + $newfilename =~ s|.([^\.]+)$||; + } + $newfilename.=".$extension"; + } + } + + my $fullpath = $dir.'/'.$newfilename; + + &Debug($request, "Full path is : ".$fullpath); + + if(-e $fullpath) { + $request->print('File exists.
'); + } + else { + $request->print('Make new file '.$dir.'/'.$newfilename.'?
'); + my $dest=&MakeFinalUrl($request,$fullpath); + &Debug($request, "Cancel url is: ".$cancelurl); + &Debug($request, "Dest url is: ".$dest); + $request->print(''); + $request->print(''); + $request->print(''); + } +} + +=pod + =item phaseone($r, $fn, $uname, $udom) Peforms phase one processing of the request. In phase one, error messages @@ -742,16 +847,19 @@ sub phaseone { # my $conspace=ConstructionPathFromRelative($uname, $fn); - - $r->print(''); + } } - } =pod @@ -813,9 +936,20 @@ sub Rename2 { $newfile); if(-e $oldfile) { - unless(rename($oldfile, - $directory.'/'.$newfile)) { - $request->print('Error: '.$!.''); + my $dest; + + if ($oldfile =~ m|/$|) { + #renaming a dir + $oldfile =~ s|/$||; + $dest=$directory; + $dest=~s|(/)([^/]*)$|$1|; + $dest.='/'.$newfile; + } else { + $dest=$directory.'/'.$newfile; + } + + unless(rename($oldfile,$dest)) { + $request->print('Error: '.$!.''); return 0; } else {} } else { @@ -859,11 +993,11 @@ sub Delete2 { if(-e $filename) { unless(unlink($filename)) { - $request->print('Error: '.$!.''); + $request->print('Error: '.$!.''); return 0; } } else { - $request->print('No such file. print('
No such file.
print(' copy Error: '.$!.''); + $request->print(' copy Error: '.$!.''); return 0; } else { unless (chmod(0660, $newfile)) { - $request->print(' chmod error: '.$!.''); + $request->print(' chmod error: '.$!.''); return 0; } return 1; @@ -943,11 +1077,11 @@ sub NewDir2 { my ($request, $user, $newdirectory) = @_; unless(mkdir($newdirectory, 02770)) { - $request->print('Error: '.$!.''); + $request->print('Error: '.$!.''); return 0; } unless(chmod(02770, ($newdirectory))) { - $request->print(' Error: '.$!.''); + $request->print(' Error: '.$!.''); return 0; } return 1; @@ -992,11 +1126,16 @@ sub phasetwo { # Break down the file into it's component pieces. - $fn=~/(.*)\/([^\/]+)\.(\w+)$/; - my $dir=$1; # Directory path - my $main=$2; # Filename. - my $suffix=$3; # Extension. - + my $dir; # Directory path + my $main; # Filename. + my $suffix; # Extension. + + if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) { + $dir=$1; # Directory path + $main=$2; # Filename. + $suffix=$3; # Extension. + } + my $dest; # On success this is where we'll go. &Debug($r, @@ -1016,12 +1155,19 @@ sub phasetwo { if ($ENV{'form.action'} eq 'rename') { # Rename. if($ENV{'form.newfilename'}) { + if (!defined($dir)) { + $fn=~m:^(.*)/:; + $dir=$1; + } if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) { return; } # Prepend the directory to the new name to form the basis of the # url of the new resource. # + #renaming a dir + #remove last element for current dir + if ($fn =~ m|/$|) { $dir =~ s|/[^/]*$||; } $dest = $dir."/".$ENV{'form.newfilename'}; } } elsif ($ENV{'form.action'} eq 'delete') { @@ -1040,7 +1186,7 @@ sub phasetwo { $dest = $ENV{'form.newfilename'}; } else { - $r->print('No New filename specified'); + $r->print('
No New filename specified
'); return; } @@ -1059,19 +1205,24 @@ sub phasetwo { # Substitute for priv for the first home in $dir to get our # construction space path. # + $dest=&MakeFinalUrl($r,$dest); + + $r->print('Unknown Action'); + $r->print('
Unknown Action '.$ENV{'form.action'}.'