--- loncom/publisher/loncfile.pm 2003/08/03 00:40:00 1.36 +++ loncom/publisher/loncfile.pm 2003/11/19 15:06:33 1.45 @@ -9,7 +9,7 @@ # and displays a page showing the results of the action. # # -# $Id: loncfile.pm,v 1.36 2003/08/03 00:40:00 www Exp $ +# $Id: loncfile.pm,v 1.45 2003/11/19 15:06:33 taceyjo1 Exp $ # # Copyright Michigan State University Board of Trustees # @@ -110,7 +110,7 @@ sub Debug { # Put out the indicated message butonly if DEBUG is true. if ($DEBUG) { - $log->debug($message); + $log->debug($message); } } @@ -152,6 +152,7 @@ Global References sub URLToPath { my $Url = shift; &Debug($r, "UrlToPath got: $Url"); + $Url=~ s/\/+/\//g; $Url=~ s/^http\:\/\/[^\/]+//; $Url=~ s/^\///; $Url=~ s/(\~|priv\/)(\w+)\//\/home\/$2\/public_html\//; @@ -167,83 +168,13 @@ sub url { sub display { my $fn=shift; - $fn=~s/^\/home\/(\w+)\/public\_html//; + $fn=~s-^/home/(\w+)/public_html-/priv/$1-; return ''.$fn.''; } =pod -=item PublicationPath($domain, $user, $dir, $file) - - Determines the filesystem path corresponding to a published resource - specification. The returned value is the path. -Parameters: - -=over 4 - -=item $domain - string [in] Name of the domain within which the resource is - stored. - -=item $user - string [in] Name of the user asking about the resource. - -=item $dir - Directory path relative to the top of the resource space. - -=item $file - name of the resource file itself without path info. - -=back - -=over 4 - -Returns: - -=item string - full path to the file if it exists in publication space. - -=back - -=cut - -sub PublicationPath -{ - my ($domain, $user, $dir, $file)=@_; - - return '/home/httpd/html/res/'.$domain.'/'.$user.'/'.$dir.'/'. - $file; -} - -=pod - -=item ConstructionPath($domain, $user, $dir, $file) - - Determines the filesystem path corresponding to a construction space - resource specification. The returned value is the path -Parameters: - -=over 4 - -=item $user - string [in] Name of the user asking about the resource. - -=item $dir - Directory path relative to the top of the resource space. - -=item $file - name of the resource file itself without path info. - -Returns: - -=item string - full path to the file if it exists in Construction space. - -=back - -=cut - -sub ConstructionPath { - my ($user, $dir, $file) = @_; - - return '/home/'.$user.'/public_html/'.$dir.'/'.$file; - -} - -=pod - -=item exists($user, $domain, $directory, $file) +=item exists($user, $domain, $file) Determine if a resource file name has been published or exists in the construction space. @@ -257,9 +188,6 @@ sub ConstructionPath { =item $domain - string [in] - Name of the domain in which the resource might have been published. -=item $dir - string [in] - Path relative to construction or resource space - in which the resource might live. - =item $file - string [in] - Name of the file. =back @@ -277,27 +205,19 @@ Returns: =cut sub exists { - my ($user, $domain, $dir, $file) = @_; - - # Create complete paths in publication and construction space. - 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; + my ($user, $domain, $construct) = @_; + my $published=$construct; + $published=~ +s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//; + my $result=''; if ( -d $construct ) { - return 'Error: destination for operation is a directory.'; + return 'Error: destination for operation is an existing directory.'; } if ( -e $published) { $result.='

Warning: target file exists, and has been published!

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

Warning: target file exists!

'; } - return $result; } @@ -467,6 +387,10 @@ sub Rename1 { if(-e $fn) { if($newfilename) { + # is dest a dir + if (-d $newfilename) { + if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; } + } if ($newfilename =~ m|/[^\.]+$|) { #no extension add on original extension if ($fn =~ m|/[^\.]*\.([^\.]+)$|) { @@ -475,13 +399,15 @@ sub Rename1 { } $request->print(&checksuffix($fn, $newfilename)); #renaming a dir, delete the trailing / - #remove last element for current dir - my $dir=$fn; - if ($fn =~ m|/$|) { - $fn =~ s|/$||; - $dir =~ s|/[^/]*$||; + #remove second to last element for current dir + if (-d $fn) { + $newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/; + } + $newfilename=~s://+:/:g; # remove duplicate / + while ($newfilename=~m:/\.\./:) { + $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/.. } - my $return=&exists($user, $domain, $dir, $newfilename); + my $return=&exists($user, $domain, $newfilename); $request->print($return); if ($return =~/^Error:/) { $request->print('
Cancel'); @@ -567,24 +493,36 @@ Parameters: =cut sub Copy1 { - my ($request, $user, $domain, $fn, $newfilename) = @_; + my ($request, $user, $domain, $fn, $newfilename) = @_; - if(-e $fn) { - $request->print(&checksuffix($fn,$newfilename)); - my $return=&exists($user, $domain, $fn, $newfilename); - $request->print($return); - if ($return =~/^Error:/) { - $request->print('
Cancel'); - return; + if(-e $fn) { + # is dest a dir + if (-d $newfilename) { + if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; } + } + if ($newfilename =~ m|/[^\.]+$|) { + #no extension add on original extension + if ($fn =~ m|/[^\.]*\.([^\.]+)$|) { $newfilename.='.'.$1; } + } + $newfilename=~s://+:/:g; # remove duplicate / + while ($newfilename=~m:/\.\./:) { + $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/.. + } + $request->print(&checksuffix($fn,$newfilename)); + my $return=&exists($user, $domain, $newfilename); + $request->print($return); + if ($return =~/^Error:/) { + $request->print('
Cancel'); + return; + } + $request->print('

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

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

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

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

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

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

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

'); - } } =pod @@ -630,10 +568,10 @@ sub NewDir1 { my ($request, $username, $domain, $fn, $newfilename) = @_; - if(-e $newfilename) { - $request->print('

Directory exists.

'); - } - else { + my $result=&exists($username,$domain,$newfilename); + if ($result) { + $request->print(''.$result.''); + } else { $request->print('

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

'); @@ -641,6 +579,17 @@ sub NewDir1 } } + +sub Decompress1 { + my ($request, $user, $domain, $fn) = @_; + if( -e $fn) { + $request->print(''); + $request->print('

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

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

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

'); + } +} =pod =item NewFile1 @@ -687,6 +636,14 @@ sub NewFile1 { if ($ENV{'form.action'} =~ /new(.+)file/) { my $extension=$1; + + ##Informs User (name).(number).(extension) not allowed + if($newfilename =~ /\.(\d+)\.(\w+)$/){ + $r->print(''.$newfilename. + ' - Bad Filename
(name).(number).(extension)'. + ' Not Allowed
'); + return; + } if ($newfilename !~ /\Q.$extension\E$/) { if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) { #already has an extension strip it and add in expected one @@ -695,11 +652,10 @@ sub NewFile1 { $newfilename.=".$extension"; } } - - if(-e $newfilename) { - $request->print('

File exists.

'); - } - else { + my $result=&exists($user,$domain,$newfilename); + if($result) { + $request->print(''.$result.''); + } else { $request->print('

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

'); $request->print(''); $request->print('
'. ''. ''. @@ -752,6 +707,8 @@ sub phaseone { &Rename1($r, $uname, $udom, $fn, $newfilename); } elsif ($ENV{'form.action'} eq 'delete') { &Delete1($r, $uname, $udom, $fn); + } elsif ($ENV{'form.action'} eq 'decompress') { + &Decompress1($r, $uname, $udom, $fn); } elsif ($ENV{'form.action'} eq 'copy') { if($newfilename) { &Copy1($r, $uname, $udom, $fn, $newfilename); @@ -818,27 +775,47 @@ sub Rename2 { " new file ".$newfile."\n"); &Debug($request, "Target is: ".$directory.'/'. $newfile); + if (-e $oldfile) { - if(-e $oldfile) { - 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)) { + my $oRN=$oldfile; + my $nRN=$newfile; + unless (rename($oldfile,$newfile)) { $request->print('Error: '.$!.''); return 0; - } else {} + } + ## If old name.(extension) exits, move under new name. + ## If it doesn't exist and a new.(extension) exists + ## delete it (only concern when renaming over files) + my $tmp1=$oRN.'.meta'; + my $tmp2=$nRN.'.meta'; + if(-e $tmp1){ + unless(rename($tmp1,$tmp2)){ } + } elsif(-e $tmp2){ + unlink $tmp2; + } + $tmp1=$oRN.'.save'; + $tmp2=$nRN.'.save'; + if(-e $tmp1){ + unless(rename($tmp1,$tmp2)){ } + } elsif(-e $tmp2){ + unlink $tmp2; + } + $tmp1=$oRN.'.log'; + $tmp2=$nRN.'.log'; + if(-e $tmp1){ + unless(rename($tmp1,$tmp2)){ } + } elsif(-e $tmp2){ + unlink $tmp2; + } + $tmp1=$oRN.'.bak'; + $tmp2=$nRN.'.bak'; + if(-e $tmp1){ + unless(rename($tmp1,$tmp2)){ } + } elsif(-e $tmp2){ + unlink $tmp2; + } } else { - $request->print("

No such file: /home".$user.'/public_html'. - $oldfile.'

'); + $request->print("

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

'); return 0; } return 1; @@ -970,7 +947,16 @@ sub NewDir2 { } return 1; } - +sub decompress2 { + my ($r, $user, $dir, $file) = @_; + &Apache::lonnet::appenv('cgi.file' => $file); + &Apache::lonnet::appenv('cgi.dir' => $dir); + my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl'); + $r->print($result); + &Apache::lonnet::delenv('cgi.file'); + &Apache::lonnet::delenv('cgi.dir'); + return 1; +} =pod =item phasetwo($r, $fn, $uname, $udom) @@ -1036,8 +1022,16 @@ sub phasetwo { "loncfie::phase2 action is $ENV{'form.action'}"); # Select the appropriate processing sub. - - if ($ENV{'form.action'} eq 'rename') { # Rename. + if ($ENV{'form.action'} eq 'decompress') { + $main .= '.'; + $main .= $suffix; + if(!&decompress2($r, $uname, $dir, $main)) { + return ; + } + $dest = $dir."/."; + + + } elsif ($ENV{'form.action'} eq 'rename') { # Rename. if($ENV{'form.newfilename'}) { if (!defined($dir)) { $fn=~m:^(.*)/:; @@ -1046,13 +1040,7 @@ sub phasetwo { 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'}; + $dest = &url($ENV{'form.newfilename'}); } } elsif ($ENV{'form.action'} eq 'delete') { if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) { @@ -1065,7 +1053,7 @@ sub phasetwo { } elsif ($ENV{'form.action'} eq 'copy') { if($ENV{'form.newfilename'}) { if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) { - return + return ; } $dest = $ENV{'form.newfilename'}; @@ -1075,38 +1063,13 @@ sub phasetwo { } } elsif ($ENV{'form.action'} eq 'newdir') { - # - # Since the newfilename form field is construction space - # relative, ew need to prepend the current path; now in $fn. - # - my $newdir= $fn.$ENV{'form.newfilename'}; + my $newdir= $ENV{'form.newfilename'}; if(!&NewDir2($r, $uname, $newdir)) { return; } $dest = $newdir."/" } - # - # Substitute for priv for the first home in $dir to get our - # construction space path. - # - $dest=&MakeFinalUrl($r,$dest); - - $r->print('

Done

'); -} - -sub MakeFinalUrl { - my($r,$dest)=@_; - &Debug($r, "Final url is: $dest"); - $dest =~ s|/home/|/priv/|; - $dest =~ s|/public_html||; - - my $base = &File::Basename::basename($dest); - my $dpath= &File::Basename::dirname($dest); - if ($base eq '.') { $base=''; } - $dest = &HTML::Entities::encode($dpath.'/'.$base); - - &Debug($r, "Final url after rewrite: $dest"); - return $dest; + $r->print('

Done

'); } sub handler { @@ -1125,9 +1088,20 @@ sub handler { my $fn; if ($ENV{'form.filename'}) { + + &Debug($r, "test: $ENV{'form.filename'}"); $fn=&Apache::lonnet::unescape($ENV{'form.filename'}); $fn=&URLToPath($fn); - } elsif ($ENV{'form.qualifiedfilename'}) { + } + #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'}); + $fn=&URLToPath($fn); + $ENV{'form.action'}="decompress"; + } + + elsif ($ENV{'form.qualifiedfilename'}) { $fn=$ENV{'form.qualifiedfilename'}; } else { &Debug($r, "loncfile::handler - no form.filename"); @@ -1184,6 +1158,8 @@ sub handler { $r->print('

Rename

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

New Directory

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

Decompress

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

Copy

'); } elsif ($ENV{'form.action'} eq 'newfile' ||