--- loncom/publisher/loncfile.pm 2012/06/04 19:23:41 1.115 +++ loncom/publisher/loncfile.pm 2023/07/14 14:32:57 1.126 @@ -9,7 +9,7 @@ # and displays a page showing the results of the action. # # -# $Id: loncfile.pm,v 1.115 2012/06/04 19:23:41 raeburn Exp $ +# $Id: loncfile.pm,v 1.126 2023/07/14 14:32:57 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 @@ -167,11 +167,15 @@ sub URLToPath { } sub url { - my $fn=shift; + my ($fn,$context) = @_; my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; $fn=~ s/^\Q$londocroot\E//; $fn=~s{/\./}{/}g; - $fn=&HTML::Entities::encode($fn,'<>"&'); + if ($context eq 'js') { + &js_escape(\$fn); + } else { + $fn=&HTML::Entities::encode($fn,'\'<>"&'); + } return $fn; } @@ -235,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: @@ -279,28 +283,28 @@ sub exists { $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); @@ -344,15 +348,16 @@ 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,$udom)=@_; + my ($dest,$subdir,$fn,$uname,$udom)=@_; #remove bad characters my $foundbad=0; + my $warnings; my $error=''; if ($subdir && $dest =~/\./) { $foundbad=1; @@ -367,30 +372,29 @@ sub cleanDest { my ($newpath)=($dest=~m|(.*)/|); ($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,$error); + return ($dest,$error,$warnings); } sub relativeDest { @@ -409,7 +413,7 @@ sub relativeDest { while ($newfilename=~m{/\.\./}) { $newfilename=~ s{/[^/]+/\.\./}{/}g; #remove dir/.. } - my ($authorname,$authordom)=&Apache::loncacc::constructaccess($newfilename); + my ($authorname,$authordom)=&Apache::lonnet::constructaccess($newfilename); unless (($authorname) && ($authordom)) { my $otherdir = &display($newfilename); $error = &mt('Access denied to [_1]',$otherdir); @@ -437,9 +441,9 @@ Parameters: sub CloseForm1 { my ($request, $fn) = @_; - $request->print('

'); - $request->print('

'); + $request->print(''); + $request->print('
'. + '
'); } @@ -469,7 +473,7 @@ Parameters: sub CloseForm2 { my ($request, $user, $fn) = @_; - $request->print(&done(&url($fn))); + $request->print(&done($fn)); } =pod @@ -700,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( '' @@ -825,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: @@ -838,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; @@ -859,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; @@ -872,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.').'
'. @@ -880,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; } @@ -949,8 +974,8 @@ sub phaseone { my $doingdir=0; if ($env{'form.action'} eq 'newdir') { $doingdir=1; } - my ($newfilename,$error) = - &cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname,$udom); + my ($newfilename,$error,$warnings) = + &cleanDest($env{'form.newfilename'},$doingdir,$fn,$uname,$udom); unless ($error) { ($newfilename,$error)=&relativeDest($fn,$newfilename,$uname,$udom); } @@ -961,58 +986,69 @@ sub phaseone { } else { $dirlist=$fn; } + if ($warnings) { + $r->print($warnings); + } $r->print('
'.$error.'
'. - '

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

'); + '

'.&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); + } } } @@ -1099,7 +1135,7 @@ sub Rename2 { } } else { $request->print( - '

' + '

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

' @@ -1387,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)); } } } @@ -1402,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'}); @@ -1433,7 +1477,7 @@ sub handler { return HTTP_NOT_FOUND; } - unless ($fn) { + unless ($fn) { &Debug($r, "loncfile::handler - doctored url is empty"); $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}. ' trying to cfile non-existing file', $r->filename); @@ -1442,7 +1486,7 @@ sub handler { # ----------------------------------------------------------- Start page output - my ($uname,$udom) = &Apache::loncacc::constructaccess($fn); + my ($uname,$udom) = &Apache::lonnet::constructaccess($fn); &Debug($r, "loncfile::handler constructaccess uname = $uname domain = $udom"); if (($uname eq '') || ($udom eq '')) { @@ -1476,20 +1520,33 @@ function writeDone() { my $londocroot = $r->dir_config('lonDocRoot'); my $trailfile = $fn; $trailfile =~ s{^/(priv/)}{$londocroot/$1}; - + # Breadcrumbs + my $crsauthor; + my $text = 'Authoring Space'; + my $title = 'Authoring Space File Operation', + my $href = &Apache::loncommon::authorspace(&url($fn)); + if ($env{'request.course.id'}) { + my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; + my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; + if ($href eq "/priv/$cdom/$cnum/") { + $text = 'Course Authoring Space'; + $title = 'Course Authoring Space File Operation', + $crsauthor = 1; + } + } &Apache::lonhtmlcommon::clear_breadcrumbs(); &Apache::lonhtmlcommon::add_breadcrumb({ - 'text' => 'Construction Space', - 'href' => &Apache::loncommon::authorspace($fn), + 'text' => $text, + 'href' => $href, }); &Apache::lonhtmlcommon::add_breadcrumb({ 'text' => 'File Operation', - 'title' => 'Construction Space File Operation', + 'title' => $title, 'href' => '', }); - $r->print(&Apache::loncommon::start_page('Construction Space File Operation', + $r->print(&Apache::loncommon::start_page($title, $js, {'add_entries' => \%loaditem,}) .&Apache::lonhtmlcommon::breadcrumbs() @@ -1497,48 +1554,67 @@ function writeDone() { &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('

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

' - ); + unless ($crsauthor) { + $r->print('

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

' + ); + } } &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'}}) { + if ($crsauthor) { + my @disallowed = qw(page sequence rights library); + my $newtype; + if ($env{'form.action'} =~ /^new(\w+)file$/) { + $newtype = $1; + } elsif ($env{'form.action'} eq 'newfile') { + ($newtype) = ($env{'form.newfilename'} =~ m{\.([^/.]+)$}); + $newtype = lc($newtype); + } + if (($newtype ne '') && + (grep(/^\Q$newtype\E$/,@disallowed))) { + $r->print('

' + .&mt('Creation of a new file of type: [_1] is not permitted in Course Authoring Space',$newtype) + .'

' + .&Apache::loncommon::end_page() + ); + return OK; + } + } + $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);