--- loncom/publisher/loncfile.pm 2002/09/10 02:31:26 1.18 +++ loncom/publisher/loncfile.pm 2003/06/20 15:30:13 1.33 @@ -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.18 2002/09/10 02:31:26 foxr Exp $ +# +# $Id: loncfile.pm,v 1.33 2003/06/20 15:30:13 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -93,6 +92,7 @@ use Apache::Constants qw(:common :http : use Apache::loncacc; use Apache::Log (); use Apache::lonnet; +use Apache::loncommon(); my $DEBUG=0; my $r; # Needs to be global for some stuff RF. @@ -128,7 +128,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); @@ -323,20 +323,24 @@ sub exists { my ($user, $domain, $dir, $file) = @_; # Create complete paths in publication and construction space. - - my $published = &PublicationPath($domain, $user, $dir, $file); - my $construct = &ConstructionPath($user, $dir, $file); + my $relativedir=$dir; + $relativedir=s|/home/\Q$user\E/public_html||; + my $published = &PublicationPath($domain, $user, $relativedir, $file); + my $construct = &ConstructionPath($user, $relativedir, $file); # If the resource exists in either space indicate this fact. # Note that the check for existence in resource space is stricter. my $result; + if ( -d $construct ) { + 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; @@ -380,10 +384,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) @@ -407,9 +422,9 @@ sub CloseForm1 { &Debug($request, "Cancel url is: ".$cancelurl); - $request->print(''); + $request->print(''); $request->print(''); + '" method="POST">'); } @@ -441,7 +456,7 @@ Parameters: sub CloseForm2 { my ($request, $user, $directory) = @_; - $request->print('Rename '.$filename.' to '. - $dir.'/'.$newfilename.'?
'); + '" />Rename '.$filename.
+ '
to '.
+ $dest.'?
No new filename specified
'); @@ -541,8 +575,8 @@ sub Delete1 { if( -e $filename) { - $request->print(''); + $request->print(''); $request->print('Delete '.$filename.'?
'); &CloseForm1($request, $cancelurl); } else { @@ -587,14 +621,19 @@ sub Copy1 { $cancelurl =~ s/\/public_html//; - if(-e $filename) { $request->print(&checksuffix($filename,$newfilename)); - $request->print(&exists($user, $domain, $dir, $newfilename)); - $request->print('Cancel'); + return; + } + my $dest=&SimplifyDir($dir,$newfilename); + $request->print('Copy '.$filename.' to'. - ''.$dir.'/'.$newfilename.'/?
'); + '" />Copy '.$filename.'
to '.
+ ''.$dest.'?
No such file '.$filename.'
'); @@ -603,6 +642,34 @@ sub Copy1 { =pod +=item SimplifyDir + + Removes all extra / and all .. references + +Parameters: + +=over 4 + +=item $dir - string [in] a directory name + +=item $file - string [in] a file reference relative to $dir + +=back + +Results: the concatenated path. + +=cut + +sub SimplifyDir { + my ($dir,$file) = @_; + my $location = $dir. '/'.$file; + $location=~s://+:/:g; # remove duplicate / + while ($location=~m:/\.\./:) {$location=~s:/[^/]+/\.\./:/:g;}#remove dir/.. + return $location; +} + +=pod + =item NewDir1 Does all phase 1 processing of directory creation: @@ -653,8 +720,8 @@ sub NewDir1 $request->print('Directory exists.
'); } else { - $request->print('Make new directory '. + $request->print('
Make new directory '. $path."/".$newdir.'?
'); &CloseForm1($request, $cancelurl); @@ -663,6 +730,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 @@ -699,16 +848,19 @@ sub phaseone { # my $conspace=ConstructionPathFromRelative($uname, $fn); - - $r->print(''); + } } - } =pod @@ -770,9 +937,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 { @@ -816,11 +994,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; @@ -900,11 +1078,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; @@ -949,11 +1127,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, @@ -973,12 +1156,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') { @@ -988,7 +1178,7 @@ sub phasetwo { # Once a resource is deleted, we just list the directory that # previously held it. # - $dest = $dir."/"; # Parent dir. + $dest = $dir."/."; # Parent dir. } elsif ($ENV{'form.action'} eq 'copy') { if($ENV{'form.newfilename'}) { if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) { @@ -997,7 +1187,7 @@ sub phasetwo { $dest = $ENV{'form.newfilename'}; } else { - $r->print('No New filename specified'); + $r->print('
No New filename specified
'); return; } @@ -1016,18 +1206,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'}.'