--- loncom/publisher/loncfile.pm 2009/05/28 17:08:38 1.101 +++ loncom/publisher/loncfile.pm 2013/06/04 22:20:16 1.119 @@ -9,7 +9,7 @@ # and displays a page showing the results of the action. # # -# $Id: loncfile.pm,v 1.101 2009/05/28 17:08:38 bisitz Exp $ +# $Id: loncfile.pm,v 1.119 2013/06/04 22:20: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 ($url) = @_; + return + '

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

'; } =pod @@ -158,24 +158,28 @@ 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/; + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; + $fn=~ s/^\Q$londocroot\E//; + $fn=~s{/\./}{/}g; $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 +190,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 +206,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 +235,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,9 +274,9 @@ 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.').'

'); @@ -345,9 +350,10 @@ sub checksuffix { } sub cleanDest { - my ($request,$dest,$subdir,$fn,$uname)=@_; + my ($request,$dest,$subdir,$fn,$uname,$udom)=@_; #remove bad characters my $foundbad=0; + my $error=''; if ($subdir && $dest =~/\./) { $foundbad=1; $dest=~s/\.//g; @@ -359,18 +365,18 @@ 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).'') + .&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('Bad filename [_1]',&display($dest)) .'
' .&mt('[_1](name).(number).(extension)[_2] not allowed.','','') .'
' @@ -384,24 +390,31 @@ sub cleanDest { .'

' ); } - return $dest; + return ($dest,$error); } 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 +437,9 @@ Parameters: sub CloseForm1 { my ($request, $fn) = @_; - $request->print('

'); - $request->print('

'); + $request->print(''); + $request->print('
'. + '
'); } @@ -552,8 +565,9 @@ sub Rename1 { $request->print('' .'

' - .&mt($action.' [_1] to [_2]?' - ,&display($fn),&display($newfilename)) + .&mt($action.' [_1] to [_2]?', + &display($fn), + &display($newfilename)) .'

' ); &CloseForm1($request, $fn); @@ -563,8 +577,8 @@ sub Rename1 { } } else { $request->print('

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

' ); return; @@ -625,15 +639,15 @@ sub Delete1 { } } $request->print('

' - .&mt('Delete [_1]?' - ,''.&display($fn).'') + .&mt('Delete [_1]?', + &display($fn)) .'

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

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

' ); } @@ -689,19 +703,20 @@ sub Copy1 { $request->print('
'.&mt('Cancel').''); return; } - $request->print('' - .'

' - .&mt('Copy [_1] to [_2]?' - ,''.&display($fn).'' - ,''.&display($newfilename).'') - .'

' + $request->print( + '' + .'

' + .&mt('Copy [_1] to [_2]?', + &display($fn), + &display($newfilename)) + .'

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

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

' ); } @@ -754,16 +769,15 @@ sub NewDir1 { if ($type eq 'error') { $request->print(''); } else { - if ($mode eq 'testbank') { - $request->print(''); - } elsif ($mode eq 'imsimport') { - $request->print(''); + if (($mode eq 'testbank') || ($mode eq 'imsimport')) { + $request->print(''."\n". + ''); } $request->print('' .'

' - .&mt('Make new directory [_1]?' - ,''.&display($newfilename).'') + .&mt('Make new directory [_1]?', + &display($newfilename)) .'

' ); &CloseForm1($request, $fn); @@ -776,15 +790,15 @@ sub Decompress1 { if( -e $fn) { $request->print(''); $request->print('

' - .&mt('Decompress [_1]?' - ,''.&display($fn).'') + .&mt('Decompress [_1]?', + &display($fn)) .'

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

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

' ); } @@ -811,7 +825,7 @@ 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 @@ -824,7 +838,7 @@ 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 @@ -855,7 +869,7 @@ sub NewFile1 { $extension = $1; } - my @okexts = qw(xml html xhtml htm xhtm problem page sequence rights sty library js css txt); + 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('

'. @@ -866,7 +880,7 @@ sub NewFile1 { '

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

'. '

'); @@ -935,8 +949,23 @@ 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) = + &cleanDest($r,$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; + } + $r->print('
'.$error.'
'. + '

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

'); + return; + } $r->print('
'. ''. ''. @@ -1069,10 +1098,11 @@ sub Rename2 { unlink $tmp2; } } else { - $request->print('

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

' + $request->print( + '

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

' ); return 0; } @@ -1357,11 +1387,19 @@ 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))); } @@ -1411,14 +1449,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', @@ -1445,31 +1480,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) .'

' ); @@ -1477,37 +1516,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);