--- loncom/publisher/loncfile.pm 2010/11/27 23:20:46 1.104 +++ loncom/publisher/loncfile.pm 2019/03/06 02:31:16 1.125 @@ -9,7 +9,7 @@ # and displays a page showing the results of the action. # # -# $Id: loncfile.pm,v 1.104 2010/11/27 23:20:46 raeburn Exp $ +# $Id: loncfile.pm,v 1.125 2019/03/06 02:31:16 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -37,7 +37,7 @@ =head1 NAME -Apache::loncfile - Construction space file management. +Apache::loncfile - Authoring space file management. =head1 SYNOPSIS @@ -68,7 +68,6 @@ use File::Basename; use File::Copy; use HTML::Entities(); use Apache::Constants qw(:common :http :methods); -use Apache::loncacc; use Apache::lonnet; use Apache::loncommon(); use Apache::lonlocal; @@ -102,7 +101,7 @@ my $r; # Needs to be global for some =cut sub Debug { - # Put out the indicated message butonly if DEBUG is true. + # Put out the indicated message but only if DEBUG is true. if ($DEBUG) { my ($r,$message) = @_; $r->log_reason($message); @@ -110,14 +109,15 @@ sub Debug { } sub done { - my ($url)=@_; - my $done=&mt("Done"); - return(<$done - -ENDDONE + my ($destfn) = @_; + return + '

' + .&Apache::lonhtmlcommon::confirm_success(&mt("Done")) + .'
'.&mt("Continue").'' + .'' + .'

'; } =pod @@ -158,24 +158,32 @@ Global References sub URLToPath { my $Url = shift; &Debug($r, "UrlToPath got: $Url"); - $Url=~ s/\/+/\//g; - $Url=~ s/^https?\:\/\/[^\/]+//; - $Url=~ s/^\///; - $Url=~ s/(\~|priv\/)($match_username)\//\/home\/$2\/public_html\//; + $Url=~ s{^https?\://[^/]+}{}; + $Url=~ s{//+}{/}g; + $Url=~ s{^/}{}; + $Url=$Apache::lonnet::perlvar{'lonDocRoot'}."/$Url"; &Debug($r, "Returning $Url \n"); return $Url; } sub url { - my $fn=shift; - $fn=~s/^\/home\/($match_username)\/public\_html/\/priv\/$1/; - $fn=&HTML::Entities::encode($fn,'<>"&'); + my ($fn,$context) = @_; + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; + $fn=~ s/^\Q$londocroot\E//; + $fn=~s{/\./}{/}g; + if ($context eq 'js') { + &js_escape(\$fn); + } else { + $fn=&HTML::Entities::encode($fn,'\'<>"&'); + } return $fn; } sub display { my $fn=shift; - $fn=~s-^/home/($match_username)/public_html-/priv/$1-; + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; + $fn=~s/^\Q$londocroot\E//; + $fn=~s{/\./}{/}g; return ''.$fn.''; } @@ -186,9 +194,9 @@ sub display { sub obsolete_unpub { my ($user,$domain,$construct)=@_; + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; my $published=$construct; - $published=~ - s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//; + $published=~s{^\Q$londocroot/priv/\E}{$londocroot/res/}; if (-e $published) { if (&Apache::lonnet::metadata($published,'obsolete')) { return 1; @@ -202,12 +210,13 @@ sub obsolete_unpub { # see if directory is empty # ignores any .meta, .save, .bak, and .log files created for a previously # published file, which has since been marked obsolete and deleted. +# ignores a .DS_Store file put there when viewing directory via webDAV on MacOS. sub empty_directory { my ($dirname,$phase) = @_; if (opendir DIR, $dirname) { my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and .. if (@files) { - my @orphans = grep(/\.(meta|save|log|bak)$/,@files); + my @orphans = grep(/\.(meta|save|log|bak|DS_Store)$/,@files); if (scalar(@files) - scalar(@orphans) > 0) { return 0; } else { @@ -230,7 +239,7 @@ sub empty_directory { =item exists($user, $domain, $file) - Determine if a resource file name has been published or exists + Determine if a resource filename has been published or exists in the construction space. Parameters: @@ -269,33 +278,33 @@ sub exists { my ($user, $domain, $construct, $creating) = @_; $creating ||= 'file'; + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; my $published=$construct; - $published=~ - s{^/home/$user/public_html/}{/home/httpd/html/res/$domain/$user/}; + $published=~s{^\Q$londocroot/priv/\E}{$londocroot/res/}; my ($type,$result); if ( -d $construct ) { - return ('error','

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

'); + return ('error','

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

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

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

'; + $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.").'

'; + $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.").'

'; + $result.='

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

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

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

'; + $result.='

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

'; } return ($type,$result); @@ -339,15 +348,17 @@ sub checksuffix { if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; } if (lc($oldsuffix) ne lc($newsuffix)) { $result.= - '

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

'; + '

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

'; } return $result; } sub cleanDest { - my ($request,$dest,$subdir,$fn,$uname)=@_; + my ($dest,$subdir,$fn,$uname,$udom)=@_; #remove bad characters my $foundbad=0; + my $warnings; + my $error=''; if ($subdir && $dest =~/\./) { $foundbad=1; $dest=~s/\.//g; @@ -359,49 +370,55 @@ sub cleanDest { } if ($dest=~m|/|) { my ($newpath)=($dest=~m|(.*)/|); - $newpath=&relativeDest($fn,$newpath,$uname); + ($newpath,$error)=&relativeDest($fn,$newpath,$uname,$udom); 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." - ,&display($newpath)) - .'

'); + $warnings = '

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

'; $dest=~s|.*/||; } } - if ($dest =~ /\.(\d+)\.(\w+)$/){ - $request->print('

' - .&mt('Bad filename [_1]',&display($dest)) - .'
' - .&mt('[_1](name).(number).(extension)[_2] not allowed.','','') - .'
' - .&mt('Removing the [_1].number.[_2] from requested filename.','','') - .'

'); + if ($dest =~ /\.(\d+)\.(\w+)$/) { + $warnings .= '

' + .&mt('Bad filename [_1]',&display($dest)) + .'
' + .&mt('[_1](name).(number).(extension)[_2] not allowed.','','') + .'
' + .&mt('Removing the [_1].number.[_2] from requested filename.','','') + .'

'; $dest =~ s/\.(\d+)(\.\w+)$/$2/; } if ($foundbad) { - $request->print('

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

' - ); + $warnings .= '

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

'; } - return $dest; + return ($dest,$error,$warnings); } sub relativeDest { - my ($fn,$newfilename,$uname)=@_; + my ($fn,$newfilename,$uname,$udom)=@_; + my $error = ''; if ($newfilename=~/^\//) { # absolute, simply add path - $newfilename='/home/'.$uname.'/public_html/'; + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; + $newfilename="$londocroot/res/$udom/$uname/"; } else { my $dir=$fn; - $dir=~s/\/[^\/]+$//; + $dir=~s{/[^/]+$}{}; $newfilename=$dir.'/'.$newfilename; } - $newfilename=~s://+:/:g; # remove duplicate / - while ($newfilename=~m:/\.\./:) { - $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/.. + $newfilename=~s{//+}{/}g; # remove duplicate / + while ($newfilename=~m{/\.\./}) { + $newfilename=~ s{/[^/]+/\.\./}{/}g; #remove dir/.. + } + my ($authorname,$authordom)=&Apache::lonnet::constructaccess($newfilename); + unless (($authorname) && ($authordom)) { + my $otherdir = &display($newfilename); + $error = &mt('Access denied to [_1]',$otherdir); } - return $newfilename; + return ($newfilename,$error); } =pod @@ -424,9 +441,9 @@ Parameters: sub CloseForm1 { my ($request, $fn) = @_; - $request->print('

'); - $request->print('

'); + $request->print(''); + $request->print('
'. + '
'); } @@ -456,7 +473,7 @@ Parameters: sub CloseForm2 { my ($request, $user, $fn) = @_; - $request->print(&done(&url($fn))); + $request->print(&done($fn)); } =pod @@ -687,9 +704,20 @@ sub Copy1 { my ($type,$return)=&exists($user, $domain, $newfilename); $request->print($return); if ($type eq 'error') { - $request->print('
'.&mt('Cancel').''); + $request->print('
'.&mt('Cancel').''); return; } +# Check if there is enough space. + my @fileinfo = stat($fn); + my ($dir,$fname) = ($fn =~ m{^(.+/)([^/]+)$}); + my $filesize = $fileinfo[7]; + $filesize = int($filesize/1000); #expressed in kb + my $output = &Apache::loncommon::excess_filesize_warning($user,$domain,'author', + $fname,$filesize,'copy'); + if ($output) { + $request->print($output.'
'.&mt('Cancel').''); + return; + } $request->print( '' @@ -812,10 +840,13 @@ Parameters: =item $domain - Name of the domain of the user -=item $fn - Source file name +=item $fn - Source filename =item $newfilename - Name of the file to be created; no path information + +=item $warnings - Information about changes to filename made by cleanDest(). + =back Side Effects: @@ -825,15 +856,15 @@ Side Effects: =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 Cancel -button which returns you to the driectory listing you came from +button which returns you to the directory listing you came from =back =cut sub NewFile1 { - my ($request, $user, $domain, $fn, $newfilename) = @_; - return if (&filename_check($newfilename) ne 'ok'); + my ($request, $user, $domain, $fn, $newfilename, $warnings) = @_; + return if (&filename_check($newfilename,$warnings) ne 'ok'); if ($env{'form.action'} =~ /new(.+)file/) { my $extension=$1; @@ -846,8 +877,8 @@ sub NewFile1 { } } my ($type, $result)=&exists($user,$domain,$newfilename); - $request->print($result); if ($type eq 'error') { + $request->print($warnings.$result); $request->print(''); } else { my $extension; @@ -859,6 +890,7 @@ sub NewFile1 { my @okexts = qw(xml html xhtml htm xhtm problem page sequence rights sty task library js css txt); if (($extension eq '') || (!grep(/^\Q$extension\E/,@okexts))) { my $validexts = '.'.join(', .',@okexts); + $request->print($warnings.$result); $request->print('

'. &mt('Invalid filename: ').&display($newfilename).'

'. &mt('The name of the new file needs to end with an appropriate file extension to indicate the type of file to create.').'
'. @@ -867,20 +899,26 @@ sub NewFile1 { '

'. ''. ''. - ''.&mt('Enter a file name: ').' '. + ''.&mt('Enter a filename: ').' '. '

'. '

'); - return; + } elsif (($type ne 'warning') && ($warnings eq '') && ($result eq '')) { + my $query = ""; + $query .= "?mode=" . $env{'form.mode'} unless (!exists($env{'form.mode'}) || !length($env{'form.mode'})); + $request->print(' + '); + } else { + $request->print($warnings.$result); + $request->print('

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

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

'); + $request->print('

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

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

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

'); - $request->print('

'); } return; } @@ -936,55 +974,81 @@ sub phaseone { my $doingdir=0; if ($env{'form.action'} eq 'newdir') { $doingdir=1; } - my $newfilename=&cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname); - $newfilename=&relativeDest($fn,$newfilename,$uname); + my ($newfilename,$error,$warnings) = + &cleanDest($env{'form.newfilename'},$doingdir,$fn,$uname,$udom); + unless ($error) { + ($newfilename,$error)=&relativeDest($fn,$newfilename,$uname,$udom); + } + if ($error) { + my $dirlist; + if ($fn=~m{^(.*/)[^/]+$}) { + $dirlist=$1; + } else { + $dirlist=$fn; + } + if ($warnings) { + $r->print($warnings); + } + $r->print('
'.$error.'
'. + '

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

'); + return; + } $r->print('
'. ''. ''. ''); - - if ($env{'form.action'} eq 'rename') { - &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') { - &Decompress1($r, $uname, $udom, $fn); - } elsif ($env{'form.action'} eq 'copy') { - if($newfilename) { - &Copy1($r, $uname, $udom, $fn, $newfilename); - } else { - $r->print('

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

' - ); - } - } elsif ($env{'form.action'} eq 'newdir') { - 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' || - $env{'form.action'} eq 'newpagefile' || - $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') { + + if ($env{'form.action'} eq 'newfile' || + $env{'form.action'} eq 'newhtmlfile' || + $env{'form.action'} eq 'newproblemfile' || + $env{'form.action'} eq 'newpagefile' || + $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') { my $empty=&mt('Type Name Here'); - if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) { - &NewFile1($r, $uname, $udom, $fn, $newfilename); - } else { + if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) { + &NewFile1($r, $uname, $udom, $fn, $newfilename, $warnings); + } else { + if ($warnings) { + $r->print($warnings); + } $r->print('

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

' ); - } + } + } else { + if ($warnings) { + $r->print($warnings); + } + if ($env{'form.action'} eq 'rename') { + &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') { + &Decompress1($r, $uname, $udom, $fn); + } elsif ($env{'form.action'} eq 'copy') { + if ($newfilename) { + &Copy1($r, $uname, $udom, $fn, $newfilename); + } else { + $r->print('

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

' + ); + } + } elsif ($env{'form.action'} eq 'newdir') { + my $mode = ''; + if (exists($env{'form.callingmode'}) ) { + $mode = $env{'form.callingmode'}; + } + &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode); + } } } @@ -1071,7 +1135,7 @@ sub Rename2 { } } else { $request->print( - '

' + '

' .&mt('No such file: [_1]', &display($oldfile)) .'

' @@ -1359,13 +1423,21 @@ sub phasetwo { $dest = $newdir."/"; } 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').'

'); + $r->print( + '

' + .&Apache::lonhtmlcommon::confirm_success(&mt('Done')) + .'
'.&mt('Continue').'' + .'

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

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

'); - $r->print('

'.$disp_newname.'

'); + $r->print( + '

'.&Apache::lonhtmlcommon::confirm_success(&mt('Done')).'

' + .&Apache::lonhtmlcommon::actionbox( + [''.&mt('Return to Directory').'', + ''.$disp_newname.''])); } else { - $r->print(&done(&url($dest))); + $r->print(&done($dest)); } } } @@ -1374,7 +1446,7 @@ sub handler { $r=shift; - &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename']); + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename','mode']); &Debug($r, "loncfile.pm - handler entered"); &Debug($r, " filename: ".$env{'form.filename'}); @@ -1413,14 +1485,11 @@ sub handler { } # ----------------------------------------------------------- Start page output - my $uname; - my $udom; - ($uname,$udom)= - &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain')); + my ($uname,$udom) = &Apache::lonnet::constructaccess($fn); &Debug($r, "loncfile::handler constructaccess uname = $uname domain = $udom"); - unless (($uname) && ($udom)) { + if (($uname eq '') || ($udom eq '')) { $r->log_reason($uname.' at '.$udom. ' trying to manipulate file '.$env{'form.filename'}. ' ('.$fn.') - not authorized', @@ -1447,31 +1516,35 @@ function writeDone() { |; $loaditem{'onload'} = "writeDone()"; } + + my $londocroot = $r->dir_config('lonDocRoot'); + my $trailfile = $fn; + $trailfile =~ s{^/(priv/)}{$londocroot/$1}; # Breadcrumbs &Apache::lonhtmlcommon::clear_breadcrumbs(); &Apache::lonhtmlcommon::add_breadcrumb({ - 'text' => 'Construction Space', - 'href' => &Apache::loncommon::authorspace(), + 'text' => 'Authoring Space', + 'href' => &Apache::loncommon::authorspace($fn), }); &Apache::lonhtmlcommon::add_breadcrumb({ 'text' => 'File Operation', - 'title' => 'Construction Space File Operation', + 'title' => 'Authoring Space File Operation', 'href' => '', }); - $r->print(&Apache::loncommon::start_page('Construction Space File Operation', + $r->print(&Apache::loncommon::start_page('Authoring Space File Operation', $js, {'add_entries' => \%loaditem,}) .&Apache::lonhtmlcommon::breadcrumbs() .&Apache::loncommon::head_subbox( - &Apache::loncommon::CSTR_pageheader()) + &Apache::loncommon::CSTR_pageheader($trailfile)) ); - $r->print('

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

'); + $r->print('

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

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

' + $r->print('

' .&mt('Co-Author [_1]',$uname.':'.$udom) .'

' ); @@ -1479,37 +1552,35 @@ function writeDone() { &Debug($r, "loncfile::handler Form action is $env{'form.action'} "); - if ($env{'form.action'} eq 'delete') { - $r->print('

'.&mt('Delete').'

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

'.&mt('Rename').'

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

'.&mt('Move').'

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

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

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

'.&mt('Decompress').'

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

'.&mt('Copy').'

'); - } elsif ($env{'form.action'} eq 'newfile' || - $env{'form.action'} eq 'newhtmlfile' || - $env{'form.action'} eq 'newproblemfile' || - $env{'form.action'} eq 'newpagefile' || - $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').'

'); + my %action = &Apache::lonlocal::texthash( + 'delete' => 'Delete', + 'rename' => 'Rename', + 'move' => 'Move', + 'newdir' => 'New Directory', + 'decompress' => 'Decompress', + 'copy' => 'Copy', + 'newfile' => 'New Resource', + 'newhtmlfile' => 'New Resource', + 'newproblemfile' => 'New Resource', + 'newpagefile' => 'New Resource', + 'newsequencefile' => 'New Resource', + 'newrightsfile' => 'New Resource', + 'newstyfile' => 'New Resource', + 'newtaskfile' => 'New Resource', + 'newlibraryfile' => 'New Resource', + 'Select Action' => 'New Resource', + ); + if ($action{$env{'form.action'}}) { + $r->print('

'.$action{$env{'form.action'}}.'

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

' - .&mt('Unknown Action').' '.$env{'form.action'} + .&mt('Unknown Action: [_1]',$env{'form.action'}) .'

' .&Apache::loncommon::end_page() ); - return OK; + return OK; } + if ($env{'form.phase'} eq 'two') { &Debug($r, "loncfile::handler entering phase2"); &phasetwo($r,$fn,$uname,$udom);