Annotation of loncom/publisher/loncfile.pm, revision 1.107

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to rename files, etc, in construction space
                      3: #
1.9       foxr        4: #  This file responds to the various buttons and events
                      5: #  in the top frame of the construction space directory.
                      6: #  Each event is processed in two phases.  The first phase
                      7: #  presents a page that describes the proposed action to the user
                      8: #  and requests confirmation.  The second phase commits the action
                      9: #  and displays a page showing the results of the action.
                     10: #
1.24      albertel   11: #
1.107   ! www        12: # $Id: loncfile.pm,v 1.106 2011/03/01 01:36:55 raeburn Exp $
1.7       albertel   13: #
                     14: # Copyright Michigan State University Board of Trustees
                     15: #
                     16: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     17: #
                     18: # LON-CAPA is free software; you can redistribute it and/or modify
                     19: # it under the terms of the GNU General Public License as published by
                     20: # the Free Software Foundation; either version 2 of the License, or
                     21: # (at your option) any later version.
                     22: #
                     23: # LON-CAPA is distributed in the hope that it will be useful,
                     24: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     25: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     26: # GNU General Public License for more details.
                     27: #
                     28: # You should have received a copy of the GNU General Public License
                     29: # along with LON-CAPA; if not, write to the Free Software
                     30: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     31: #
                     32: # /home/httpd/html/adm/gpl.txt
                     33: #
                     34: # http://www.lon-capa.org/
                     35: #
1.12      foxr       36: =pod
                     37: 
                     38: =head1 NAME
                     39: 
                     40: Apache::loncfile - Construction space file management.
                     41: 
                     42: =head1 SYNOPSIS
                     43:  
                     44:  Content handler for buttons on the top frame of the construction space 
                     45: directory.
                     46: 
                     47: =head1 INTRODUCTION
                     48: 
                     49:   loncfile is invoked when buttons in the top frame of the construction 
1.17      harris41   50: space directory listing are clicked.   All operations proceed in two phases.
1.12      foxr       51: The first phase describes to the user exactly what will be done.  If the user
                     52: confirms the operation, the second phase commits the operation and indicates
                     53: completion.  When the user dismisses the output of phase2, they are returned to
                     54: an "appropriate" directory listing in general.
                     55: 
                     56:     This is part of the LearningOnline Network with CAPA project
                     57: described at http://www.lon-capa.org.
                     58: 
                     59: =head2 Subroutines
                     60: 
                     61: =cut
1.1       www        62: 
                     63: package Apache::loncfile;
                     64: 
                     65: use strict;
                     66: use Apache::File;
1.15      foxr       67: use File::Basename;
1.1       www        68: use File::Copy;
1.18      foxr       69: use HTML::Entities();
1.1       www        70: use Apache::Constants qw(:common :http :methods);
                     71: use Apache::loncacc;
1.15      foxr       72: use Apache::lonnet;
1.33      www        73: use Apache::loncommon();
1.47      sakharuk   74: use Apache::lonlocal;
1.80      albertel   75: use LONCAPA qw(:DEFAULT :match);
                     76: 
1.9       foxr       77: 
1.45      taceyjo1   78: my $DEBUG=0;
1.10      foxr       79: my $r;				# Needs to be global for some stuff RF.
1.12      foxr       80: 
                     81: =pod
                     82: 
                     83: =item Debug($request, $message)
                     84: 
1.17      harris41   85:   If debugging is enabled puts out a debugging message determined by the
1.12      foxr       86:   caller.  The debug message goes to the Apache error log file. Debugging
1.17      harris41   87:   is enabled by setting the module global DEBUG variable to nonzero (TRUE).
1.12      foxr       88: 
                     89:  Parameters:
                     90: 
                     91: =over 4
                     92:  
1.17      harris41   93: =item $request - The current request operation.
1.12      foxr       94: 
1.17      harris41   95: =item $message - The message to put in the log file.
1.12      foxr       96: 
                     97: =back
                     98:   
                     99:  Returns:
                    100:    nothing.
                    101: 
                    102: =cut
                    103: 
1.9       foxr      104: sub Debug {
1.55      albertel  105:     # Put out the indicated message butonly if DEBUG is true.
                    106:     if ($DEBUG) {
1.73      albertel  107: 	my ($r,$message) = @_;
1.55      albertel  108: 	$r->log_reason($message);
                    109:     }
1.12      foxr      110: }
                    111: 
1.89      www       112: sub done {
                    113:    my ($url)=@_;
                    114:    my $done=&mt("Done");
                    115:    return(<<ENDDONE);
                    116: <a href="$url">$done</a>
                    117: <script type="text/javascript">
                    118: location.href="$url";
                    119: </script>
                    120: ENDDONE
                    121: }
                    122: 
1.12      foxr      123: =pod
                    124: 
                    125: =item URLToPath($url)
                    126: 
                    127:   Convert a URL to a file system path.
                    128:   
                    129:   In order to manipulate the construction space objects, it is necessary
                    130:   to access url identified objects a filespace objects.  This function
                    131:   translates a construction space URL to a file system path.
                    132:  Parameters:
                    133: 
                    134: =over 4
                    135: 
                    136: =item  Url    - string [in] The url to convert.
                    137:   
                    138: =back
                    139:   
                    140:  Returns:
                    141: 
                    142: =over 4
                    143: 
1.16      harris41  144: =item  The corresponding file system path. 
1.12      foxr      145: 
                    146: =back
                    147: 
                    148: Global References
                    149: 
                    150: =over 4
                    151: 
                    152: =item  $r      - Request object [in] Referenced in the &Debug calls.
                    153: 
                    154: =back
                    155: 
                    156: =cut
                    157: 
                    158: sub URLToPath {
1.55      albertel  159:     my $Url = shift;
                    160:     &Debug($r, "UrlToPath got: $Url");
                    161:     $Url=~ s/\/+/\//g;
1.91      raeburn   162:     $Url=~ s/^https?\:\/\/[^\/]+//;
1.55      albertel  163:     $Url=~ s/^\///;
1.80      albertel  164:     $Url=~ s/(\~|priv\/)($match_username)\//\/home\/$2\/public_html\//;
1.55      albertel  165:     &Debug($r, "Returning $Url \n");
                    166:     return $Url;
1.12      foxr      167: }
                    168: 
1.36      www       169: sub url {
                    170:     my $fn=shift;
1.80      albertel  171:     $fn=~s/^\/home\/($match_username)\/public\_html/\/priv\/$1/;
1.56      albertel  172:     $fn=&HTML::Entities::encode($fn,'<>"&');
1.36      www       173:     return $fn;
                    174: }
                    175: 
                    176: sub display {
                    177:     my $fn=shift;
1.107   ! www       178:     $fn=~s/^\/home\/httpd\/html//;
        !           179:     $fn=~s/\/\.\//\//g;
1.87      albertel  180:     return '<span class="LC_filename">'.$fn.'</span>';
1.36      www       181: }
                    182: 
1.50      www       183: 
                    184: # see if the file is
                    185: # a) published (return 0 if not)
                    186: # b) if, so obsolete (return 0 if not)
                    187: 
                    188: sub obsolete_unpub {
                    189:     my ($user,$domain,$construct)=@_;
                    190:     my $published=$construct;
                    191:     $published=~
                    192: 	s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//;
                    193:     if (-e $published) {
                    194: 	if (&Apache::lonnet::metadata($published,'obsolete')) {
                    195: 	    return 1;
                    196: 	}
                    197: 	return 0;
                    198:     } else {
                    199: 	return 1;
                    200:     }
                    201: }
                    202: 
1.70      raeburn   203: # see if directory is empty
1.74      www       204: # ignores any .meta, .save, .bak, and .log files created for a previously
1.70      raeburn   205: # published file, which has since been marked obsolete and deleted.
                    206: sub empty_directory {
                    207:     my ($dirname,$phase) = @_;
                    208:     if (opendir DIR, $dirname) {
                    209:         my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
                    210:         if (@files) { 
1.74      www       211:             my @orphans = grep(/\.(meta|save|log|bak)$/,@files);
1.70      raeburn   212:             if (scalar(@files) - scalar(@orphans) > 0) { 
                    213:                 return 0;
                    214:             } else {
                    215:                 if (($phase eq 'Delete2') && (@orphans > 0)) {
                    216:                     foreach my $file (@orphans) {
1.74      www       217:                         if ($file =~ /\.(meta|save|log|bak)$/) {
1.70      raeburn   218:                             unlink($dirname.$file);
                    219:                         }
                    220:                     }
                    221:                 }
                    222:             }
                    223:         }
                    224:         closedir(DIR);
                    225:         return 1;
                    226:     }
                    227:     return 0;
                    228: }
1.50      www       229: 
1.12      foxr      230: =pod
                    231: 
1.39      www       232: =item exists($user, $domain, $file)
1.12      foxr      233: 
1.17      harris41  234:    Determine if a resource file name has been published or exists
1.12      foxr      235:    in the construction space.
                    236: 
                    237:  Parameters:
                    238: 
                    239: =over 4
                    240: 
1.85      albertel  241: =item  $user     - string [in] - Name of the user for which to check.
1.12      foxr      242: 
1.85      albertel  243: =item  $domain   - string [in] - Name of the domain in which the resource
1.12      foxr      244:                           might have been published.
                    245: 
1.85      albertel  246: =item  $file     - string [in] - Name of the file.
                    247: 
                    248: =item  $creating - string [in] - optional, type of object being created,
                    249:                                either 'directory' or 'file'. Defaults to
                    250:                                'file' if unspecified.
1.12      foxr      251: 
                    252: =back
                    253: 
                    254: Returns:
                    255: 
                    256: =over 4
                    257: 
1.83      albertel  258: =item  string - Either undef, 'warning' or 'error' depending on the
                    259:                 type of problem
                    260: 
1.12      foxr      261: =item  string - Either where the resource exists as an html string that can
                    262:            be embedded in a dialog or an empty string if the resource
                    263:            does not exist.
                    264:   
                    265: =back
                    266: 
                    267: =cut
                    268: 
1.8       albertel  269: sub exists {
1.85      albertel  270:     my ($user, $domain, $construct, $creating) = @_;
                    271:     $creating ||= 'file';
                    272: 
1.55      albertel  273:     my $published=$construct;
                    274:     $published=~
1.83      albertel  275: 	s{^/home/$user/public_html/}{/home/httpd/html/res/$domain/$user/};
                    276:     my ($type,$result);
1.55      albertel  277:     if ( -d $construct ) {
1.83      albertel  278: 	return ('error','<p><span class="LC_error">'.&mt('Error: destination for operation is an existing directory.').'</span></p>');
                    279: 	
1.55      albertel  280:     }
1.83      albertel  281: 
1.55      albertel  282:     if ( -e $published) {
1.83      albertel  283: 	if ( -e $construct ) {
                    284: 	    $type = 'warning';
                    285: 	    $result.='<p><span class="LC_warning">'.&mt('Warning: target file exists, and has been published!').'</span></p>';
                    286: 	} else {
1.85      albertel  287: 	    my $published_type = (-d $published) ? 'directory' : 'file';
                    288: 
                    289: 	    if ($published_type eq $creating) {
                    290: 		$type = 'warning';
                    291: 		$result.='<p><span class="LC_warning">'.&mt("Warning: a published $published_type of this name exists.").'</span></p>';
                    292: 	    } else {
                    293: 		$type = 'error';
                    294: 		$result.='<p><span class="LC_error">'.&mt("Error: a published $published_type of this name exists.").'</span></p>';
                    295: 	    }
1.83      albertel  296: 	}
1.55      albertel  297:     } elsif ( -e $construct) {
1.83      albertel  298: 	$type = 'warning';
                    299: 	$result.='<p><span class="LC_warning">'.&mt('Warning: target file exists!').'</span></p>';
1.55      albertel  300:     }
1.83      albertel  301: 
                    302:     return ($type,$result);
1.8       albertel  303: }
                    304: 
1.12      foxr      305: =pod
                    306: 
                    307: =item checksuffix($old, $new)
                    308:         
                    309:   Determine if a resource filename suffix (the stuff after the .) would change
                    310: as a result of this operation.
                    311: 
                    312:  Parameters:
                    313: 
                    314: =over 4
                    315: 
                    316: =item  $old   = string [in]  Previous filename.
                    317: 
                    318: =item  $new   = string [in]  Resultant filename.
                    319: 
                    320: =back
                    321: 
                    322:  Returns:
                    323: 
                    324: =over 4
                    325: 
1.17      harris41  326: =item    Empty string if everything worked.
1.12      foxr      327: 
                    328: =item    String containing an error message if there was a problem.
                    329: 
                    330: =back
                    331: 
                    332: =cut
                    333: 
1.8       albertel  334: sub checksuffix {
                    335:     my ($old,$new) = @_;
                    336:     my $result;
                    337:     my $oldsuffix;
                    338:     my $newsuffix;
                    339:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
                    340:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
1.82      albertel  341:     if (lc($oldsuffix) ne lc($newsuffix)) {
1.12      foxr      342: 	$result.=
1.82      albertel  343:             '<p><span class="LC_warning">'.&mt('Warning: change of MIME type!').'</span></p>';
1.8       albertel  344:     }
                    345:     return $result;
                    346: }
1.32      albertel  347: 
                    348: sub cleanDest {
1.64      albertel  349:     my ($request,$dest,$subdir,$fn,$uname)=@_;
1.32      albertel  350:     #remove bad characters
1.58      albertel  351:     my $foundbad=0;
1.106     raeburn   352:     my $error='';
1.58      albertel  353:     if ($subdir && $dest =~/\./) {
                    354: 	$foundbad=1;
                    355: 	$dest=~s/\.//g;
                    356:     }
1.87      albertel  357:     $dest =~ s/(\s+$|^\s+)//g;
1.72      albertel  358:     if  ($dest=~/[\#\?&%\":]/) {
1.58      albertel  359: 	$foundbad=1;
1.72      albertel  360: 	$dest=~s/[\#\?&%\":]//g;
1.58      albertel  361:     }
1.63      albertel  362:     if ($dest=~m|/|) {
                    363: 	my ($newpath)=($dest=~m|(.*)/|);
1.106     raeburn   364: 	($newpath,$error)=&relativeDest($fn,$newpath,$uname);
1.64      albertel  365: 	if (! -d "$newpath") {
1.100     bisitz    366: 	    $request->print('<p><span class="LC_warning">'
1.92      bisitz    367:                            .&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."
1.103     bisitz    368:                                ,&display($newpath))
1.93      bisitz    369:                            .'</span></p>');
1.63      albertel  370: 	    $dest=~s|.*/||;
                    371: 	}
                    372:     }
1.84      albertel  373:     if ($dest =~ /\.(\d+)\.(\w+)$/){
1.100     bisitz    374: 	$request->print('<p><span class="LC_warning">'
1.103     bisitz    375: 			.&mt('Bad filename [_1]',&display($dest))
1.93      bisitz    376:                         .'<br />'
                    377:                         .&mt('[_1](name).(number).(extension)[_2] not allowed.','<tt>','</tt>')
                    378:                         .'<br />'
                    379:                         .&mt('Removing the [_1].number.[_2] from requested filename.','<tt>','</tt>')
1.100     bisitz    380: 			.'</span></p>');
1.84      albertel  381: 	$dest =~ s/\.(\d+)(\.\w+)$/$2/;
                    382:     }
1.58      albertel  383:     if ($foundbad) {
1.100     bisitz    384:         $request->print('<p><span class="LC_warning">'
                    385:                        .&mt('Invalid characters in requested name have been removed.')
                    386:                         .'</span></p>'
                    387:         );
1.32      albertel  388:     }
1.106     raeburn   389:     return ($dest,$error);
1.32      albertel  390: }
                    391: 
1.36      www       392: sub relativeDest {
                    393:     my ($fn,$newfilename,$uname)=@_;
1.106     raeburn   394:     my $error = '';
1.36      www       395:     if ($newfilename=~/^\//) {
                    396: # absolute, simply add path
                    397: 	$newfilename='/home/'.$uname.'/public_html/';
                    398:     } else {
                    399: 	my $dir=$fn;
                    400: 	$dir=~s/\/[^\/]+$//;
                    401: 	$newfilename=$dir.'/'.$newfilename;
                    402:     }
                    403:     $newfilename=~s://+:/:g; # remove duplicate /
                    404:     while ($newfilename=~m:/\.\./:) {
                    405: 	$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    406:     }
1.106     raeburn   407:     if ($newfilename =~ m{^/home/($match_username)/(?:public\_html|priv)/}) {
                    408:         my $otheruname = $1;
                    409:         unless ($otheruname eq $uname) {
                    410:             my ($authorname,$authordom)=
                    411:                 &Apache::loncacc::constructaccess($newfilename,$env{'request.role.domain'});
                    412:             unless (($authorname eq $otheruname) && ($authordom ne '')) {
                    413:                 my $otherdir = &display($newfilename);
                    414:                 $error = &mt('Access denied to [_1]',$otherdir);
                    415:             }
                    416:         }
                    417:     }
                    418:     return ($newfilename,$error);
1.36      www       419: }
                    420: 
1.12      foxr      421: =pod
                    422: 
                    423: =item CloseForm1($request, $user, $file)
                    424: 
                    425:    Close of a form on the successful completion of phase 1 processing
                    426: 
                    427: Parameters:
                    428: 
                    429: =over 4
                    430: 
                    431: =item  $request - Apache Request Object [in] - Apache server request object.
                    432: 
1.13      foxr      433: =item  $cancelurl - the url to go to on cancel.
1.12      foxr      434: 
                    435: =back
                    436: 
                    437: =cut
                    438: 
                    439: sub CloseForm1 {
1.55      albertel  440:     my ($request,  $fn) = @_;
                    441:     $request->print('<p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
                    442:     $request->print('<form action="'.&url($fn).
1.97      bisitz    443: 		    '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.12      foxr      444: }
                    445: 
                    446: 
                    447: =pod
                    448: 
                    449: =item CloseForm2($request, $user, $directory)
                    450: 
                    451:    Successfully close off the phase 2 form.
                    452: 
                    453: Parameters:
                    454: 
                    455: =over 4
                    456: 
                    457: =item   $request    - Apache Request object [in] - The request that is being
                    458:                  executed.
                    459: 
                    460: =item   $user       - string [in] - Name of the user that is initiating the
                    461:                  request.
                    462: 
                    463: =item   $directory  - string [in] - Directory in which the operation is 
                    464:                  being done relative to the top level construction space
                    465:                  directory.
                    466: 
                    467: =back
                    468: 
                    469: =cut
                    470: 
                    471: sub CloseForm2 {
1.55      albertel  472:     my ($request, $user, $fn) = @_;
1.89      www       473:     $request->print(&done(&url($fn)));
1.12      foxr      474: }
                    475: 
                    476: =pod
                    477: 
                    478: =item Rename1($request, $filename, $user, $domain, $dir)
                    479:  
                    480:    Perform phase 1 processing of the file rename operation.
                    481: 
                    482: Parameters:
                    483: 
                    484: =over 4
                    485: 
                    486: =item  $request   - Apache Request Object [in] The request object for the 
                    487: current request.
                    488: 
                    489: =item  $filename  - The filename relative to construction space.
                    490: 
                    491: =item  $user      - Name of the user making the request.
                    492: 
                    493: =item  $domain    - User login domain.
                    494: 
                    495: =item  $dir       - Directory specification of the path to the file.
                    496: 
                    497: =back
                    498: 
                    499: Side effects:
                    500: 
                    501: =over 4
                    502: 
                    503: =item A new form is displayed prompting for confirmation.  The newfilename
                    504: hidden field of this form is loaded with
                    505: new filename relative to the current directory ($dir).
                    506: 
                    507: =back
                    508: 
                    509: =cut  
                    510: 
                    511: sub Rename1 {
1.52      albertel  512:     my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
1.36      www       513: 
                    514:     if(-e $fn) {
                    515: 	if($newfilename) {
1.42      albertel  516: 	    # is dest a dir
1.52      albertel  517: 	    if ($style eq 'move') {
                    518: 		if (-d $newfilename) {
                    519: 		    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    520: 		}
1.42      albertel  521: 	    }
1.29      albertel  522: 	    if ($newfilename =~ m|/[^\.]+$|) {
1.36      www       523: 		#no extension add on original extension
                    524: 		if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
1.24      albertel  525: 		    $newfilename.='.'.$1;
                    526: 		}
                    527: 	    }
1.36      www       528: 	    $request->print(&checksuffix($fn, $newfilename));
1.27      albertel  529: 	    #renaming a dir, delete the trailing /
1.39      www       530:             #remove second to last element for current dir
                    531: 	    if (-d $fn) {
1.53      www       532: 		$newfilename=~/\.(\w+)$/;
                    533: 		if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
1.100     bisitz    534: 		    $request->print('<p><span class="LC_error">'.
                    535: 				    &mt('Cannot change MIME type of a directory.').
1.82      albertel  536: 				    '</span>'.
1.100     bisitz    537: 				    '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>');
1.53      www       538: 		    return;
                    539: 		}
1.39      www       540: 		$newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
1.27      albertel  541: 	    }
1.42      albertel  542: 	    $newfilename=~s://+:/:g; # remove duplicate /
                    543: 	    while ($newfilename=~m:/\.\./:) {
                    544: 		$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    545: 	    }
1.83      albertel  546: 	    my ($type, $return)=&exists($user, $domain, $newfilename);
1.21      albertel  547: 	    $request->print($return);
1.83      albertel  548: 	    if ($type eq 'error') {
1.47      sakharuk  549: 		$request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.21      albertel  550: 		return;
                    551: 	    }
1.50      www       552: 	    unless (&obsolete_unpub($user,$domain,$fn)) {
1.100     bisitz    553:                 $request->print('<p><span class="LC_error">'
                    554:                                .&mt('Cannot rename or move non-obsolete published file.')
                    555:                                .'</span><br />'
                    556:                                .'<a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
                    557:                 );
1.50      www       558: 		return;
                    559: 	    }
1.52      albertel  560: 	    my $action;
                    561: 	    if ($style eq 'rename') {
1.100     bisitz    562: 		$action='Rename';
1.52      albertel  563: 	    } else {
1.100     bisitz    564: 		$action='Move';
1.52      albertel  565: 	    }
1.100     bisitz    566:             $request->print('<input type="hidden" name="newfilename" value="'
                    567:                            .$newfilename.'" />'
                    568:                            .'<p>'
1.103     bisitz    569:                            .&mt($action.' [_1] to [_2]?',
                    570:                                 &display($fn),
                    571:                                 &display($newfilename))
1.100     bisitz    572:                            .'</p>'
                    573:         );
1.36      www       574: 	    &CloseForm1($request, $fn);
1.12      foxr      575: 	} else {
1.100     bisitz    576: 	    $request->print('<p class="LC_error">'.&mt('No new filename specified.').'</p></form>');
1.12      foxr      577: 	    return;
                    578: 	}
                    579:     } else {
1.100     bisitz    580:         $request->print('<p class="LC_error">'
1.103     bisitz    581:                        .&mt('No such file: [_1]',
                    582:                             &display($fn))
1.100     bisitz    583:                        .'</p></form>'
                    584:         );
1.12      foxr      585: 	return;
                    586:     }
                    587:     
                    588: }
1.55      albertel  589: 
1.12      foxr      590: =pod
                    591: 
                    592: =item Delete1
                    593: 
                    594:    Performs phase 1 processing of the delete operation.  In phase one
                    595:   we just check to be sure the file exists.
                    596: 
                    597: Parameters:
                    598: 
                    599: =over 4
                    600: 
1.36      www       601: =item   $request   - Apache Request Object [in] request object for the current 
1.12      foxr      602:                 request.
                    603: 
1.36      www       604: =item   $user      - string [in]  Name of the user initiating the request.
1.12      foxr      605: 
1.36      www       606: =item   $domain    - string [in]  Domain the initiating user is logged in as
1.13      foxr      607: 
1.36      www       608: =item   $filename  - string [in]  Source filename.
1.12      foxr      609: 
                    610: =back
                    611: 
                    612: =cut
                    613: 
                    614: sub Delete1 {
1.55      albertel  615:     my ($request, $user, $domain, $fn) = @_;
1.12      foxr      616: 
1.55      albertel  617:     if( -e $fn) {
                    618: 	$request->print('<input type="hidden" name="newfilename" value="'.
1.95      bisitz    619: 			$fn.'" />');
1.70      raeburn   620:         if (-d $fn) {
                    621:             unless (&empty_directory($fn,'Delete1')) {
1.100     bisitz    622:                 $request->print('<p>'
                    623:                                .'<span class="LC_error">'
                    624:                                .&mt('Only empty directories may be deleted.')
                    625:                                .'</span><br />'
                    626:                                .&mt('You must delete the contents of the directory first.')
                    627:                                .'</p>'
                    628:                                .'<p><a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
                    629:                 );
1.70      raeburn   630:                 return;
                    631:             }
                    632:         } else { 
                    633: 	    unless (&obsolete_unpub($user,$domain,$fn)) {
1.100     bisitz    634:                 $request->print('<p><span class="LC_error">'
                    635:                                .&mt('Cannot delete non-obsolete published file.')
                    636:                                .'</span><br />'
                    637:                                .'<a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
                    638:                 );
1.70      raeburn   639: 	        return;
                    640: 	    }
                    641:         }
1.100     bisitz    642:         $request->print('<p>'
1.103     bisitz    643:                        .&mt('Delete [_1]?',
                    644:                             &display($fn))
1.100     bisitz    645:                        .'</p>'
                    646:         );
1.55      albertel  647: 	&CloseForm1($request, $fn);
                    648:     } else {
1.100     bisitz    649:         $request->print('<p class="LC_error">'
1.103     bisitz    650:                        .&mt('No such file: [_1]',
                    651:                             &display($fn))
1.100     bisitz    652:                        .'</p></form>'
                    653:         );
1.50      www       654:     }
1.12      foxr      655: }
                    656: 
                    657: =pod
                    658: 
                    659: =item Copy1($request, $user, $domain, $filename, $newfilename)
                    660: 
                    661:    Performs phase 1 processing of the construction space copy command.
1.17      harris41  662:    Ensure that the source file exists.  Ensure that a destination exists,
                    663:    also warn if the destination already exists.
1.12      foxr      664: 
                    665: Parameters:
                    666: 
                    667: =over 4
                    668: 
                    669: =item   $request   - Apache Request Object [in] request object for the current 
                    670:                 request.
                    671: 
                    672: =item   $user      - string [in]  Name of the user initiating the request.
                    673: 
                    674: =item   $domain    - string [in]  Domain the initiating user is logged in as
                    675: 
1.36      www       676: =item   $fn  - string [in]  Source filename.
1.12      foxr      677: 
                    678: =item   $newfilename-string [in]  Destination filename.
                    679: 
                    680: =back
                    681: 
                    682: =cut
                    683: 
                    684: sub Copy1 {
1.42      albertel  685:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.12      foxr      686: 
1.42      albertel  687:     if(-e $fn) {
                    688: 	# is dest a dir
                    689: 	if (-d $newfilename) {
                    690: 	    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    691: 	}
                    692: 	if ($newfilename =~ m|/[^\.]+$|) {
                    693: 	    #no extension add on original extension
                    694: 	    if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {	$newfilename.='.'.$1; }
                    695: 	} 
                    696: 	$newfilename=~s://+:/:g; # remove duplicate /
                    697: 	while ($newfilename=~m:/\.\./:) {
                    698: 	    $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    699: 	}
                    700: 	$request->print(&checksuffix($fn,$newfilename));
1.83      albertel  701: 	my ($type,$return)=&exists($user, $domain, $newfilename);
1.42      albertel  702: 	$request->print($return);
1.83      albertel  703: 	if ($type eq 'error') {
1.47      sakharuk  704: 	    $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.42      albertel  705: 	    return;
                    706: 	}
1.103     bisitz    707:     $request->print(
                    708:         '<input type="hidden" name="newfilename"'
                    709:        .' value="'.$newfilename.'" />'
                    710:        .'<p>'
                    711:        .&mt('Copy [_1] to [_2]?',
                    712:             &display($fn),
                    713:             &display($newfilename))
                    714:        .'</p>'
1.100     bisitz    715:         );
1.42      albertel  716: 	&CloseForm1($request, $fn);
                    717:     } else {
1.100     bisitz    718:         $request->print('<p class="LC_error">'
1.103     bisitz    719:                        .&mt('No such file: [_1]',
                    720:                             &display($fn))
1.100     bisitz    721:                        .'</p></form>'
                    722:         );
1.21      albertel  723:     }
1.19      albertel  724: }
                    725: 
                    726: =pod
                    727: 
1.12      foxr      728: =item NewDir1
                    729:  
                    730:   Does all phase 1 processing of directory creation:
                    731:   Ensures that the user provides a new directory name,
                    732:   and that the directory does not already exist.
                    733: 
                    734: Parameters:
                    735: 
                    736: =over 4
                    737: 
                    738: =item   $request  - Apache Request Object [in] - Server request object for the
1.17      harris41  739:                current url.
1.12      foxr      740: 
                    741: =item   $username - Name of the user that is requesting the directory creation.
                    742: 
1.36      www       743: =item $domain - Domain user is in
                    744: 
                    745: =item   $fn     - source file.
1.12      foxr      746: 
                    747: =item   $newdir   - Name of the directory to be created; path relative to the 
                    748:                top level of construction space.
                    749: =back
                    750: 
                    751: Side Effects:
                    752: 
                    753: =over 4
                    754: 
                    755: =item A new form is displayed.  Clicking on the confirmation button
                    756: causes the newdir operation to transition into phase 2.  The hidden field
                    757: "newfilename" is set with the construction space path to the new directory.
                    758: 
                    759: 
                    760: =back
                    761: 
                    762: =cut
                    763: 
                    764: 
1.55      albertel  765: sub NewDir1 {
                    766:     my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
                    767: 
1.85      albertel  768:     my ($type, $result)=&exists($username,$domain,$newfilename,'directory');
1.83      albertel  769:     $request->print($result);
1.85      albertel  770:     if ($type eq 'error') {
1.83      albertel  771: 	$request->print('</form>');
1.55      albertel  772:     } else {
1.104     raeburn   773: 	if (($mode eq 'testbank') || ($mode eq 'imsimport')) {
                    774: 	    $request->print('<input type="hidden" name="callingmode" value="'.$mode.'" />'."\n".
                    775:                             '<input type="hidden" name="inhibitmenu" value="yes" />');
1.55      albertel  776: 	}
1.100     bisitz    777:         $request->print('<input type="hidden" name="newfilename" value="'
                    778:                        .$newfilename.'" />'
                    779:                        .'<p>'
1.103     bisitz    780:                        .&mt('Make new directory [_1]?',
                    781:                             &display($newfilename))
1.100     bisitz    782:                        .'</p>'
                    783:         );
1.55      albertel  784: 	&CloseForm1($request, $fn);
                    785:     }
1.12      foxr      786: }
                    787: 
1.44      taceyjo1  788: 
                    789: sub Decompress1 {
1.55      albertel  790:     my ($request, $user, $domain, $fn) = @_;
                    791:     if( -e $fn) {
1.95      bisitz    792:    	$request->print('<input type="hidden" name="newfilename" value="'.$fn.'" />');
1.100     bisitz    793:    	$request->print('<p>'
1.103     bisitz    794:                    .&mt('Decompress [_1]?',
                    795:                         &display($fn))
1.100     bisitz    796:                    .'</p>'
                    797:     );
1.44      taceyjo1  798:    	&CloseForm1($request, $fn);
1.55      albertel  799:     } else {
1.100     bisitz    800:         $request->print('<p class="LC_error">'
1.103     bisitz    801:                        .&mt('No such file: [_1]',
                    802:                             &display($fn))
1.100     bisitz    803:                        .'</p></form>'
                    804:         );
1.55      albertel  805:     }
1.44      taceyjo1  806: }
1.55      albertel  807: 
1.12      foxr      808: =pod
                    809: 
1.22      albertel  810: =item NewFile1
                    811:  
                    812:   Does all phase 1 processing of file creation:
                    813:   Ensures that the user provides a new filename, adds proper extension
                    814:   if needed and that the file does not already exist, if it is a html,
                    815:   problem, page, or sequence, it then creates a form link to hand the
                    816:   actual creation off to the proper handler.
                    817: 
                    818: Parameters:
                    819: 
                    820: =over 4
                    821: 
                    822: =item   $request  - Apache Request Object [in] - Server request object for the
                    823:                current url.
                    824: 
                    825: =item   $username - Name of the user that is requesting the directory creation.
                    826: 
                    827: =item   $domain   - Name of the domain of the user
                    828: 
1.36      www       829: =item   $fn      - Source file name
1.22      albertel  830: 
                    831: =item   $newfilename
                    832:                   - Name of the file to be created; no path information
                    833: =back
                    834: 
                    835: Side Effects:
                    836: 
                    837: =over 4
                    838: 
                    839: =item 2 new forms are displayed.  Clicking on the confirmation button
                    840: causes the browser to attempt to load the specfied URL, allowing the
1.36      www       841: proper handler to take care of file creation. There is also a Cancel
1.22      albertel  842: button which returns you to the driectory listing you came from
                    843: 
                    844: =back
                    845: 
                    846: =cut
                    847: 
                    848: sub NewFile1 {
1.36      www       849:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.96      raeburn   850:     return if (&filename_check($newfilename) ne 'ok');
1.22      albertel  851: 
1.67      albertel  852:     if ($env{'form.action'} =~ /new(.+)file/) {
1.22      albertel  853: 	my $extension=$1;
                    854: 	if ($newfilename !~ /\Q.$extension\E$/) {
1.81      albertel  855: 	    if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
1.26      albertel  856: 		#already has an extension strip it and add in expected one
1.81      albertel  857: 		$newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
1.26      albertel  858: 	    }
1.22      albertel  859: 	    $newfilename.=".$extension";
                    860: 	}
1.31      albertel  861:     }
1.83      albertel  862:     my ($type, $result)=&exists($user,$domain,$newfilename);
                    863:     $request->print($result);
                    864:     if ($type eq 'error') {
                    865: 	$request->print('</form>');
1.39      www       866:     } else {
1.96      raeburn   867:         my $extension;
                    868: 
                    869:         if ($newfilename =~ m{[^/.]+\.([^/.]+)$}) {
                    870:             $extension = $1;
                    871:         }
                    872: 
1.102     raeburn   873:         my @okexts = qw(xml html xhtml htm xhtm problem page sequence rights sty task library js css txt);
1.96      raeburn   874:         if (($extension eq '') || (!grep(/^\Q$extension\E/,@okexts))) {
                    875:             my $validexts = '.'.join(', .',@okexts);
                    876:             $request->print('<p class="LC_warning">'.
                    877:                 &mt('Invalid filename: ').&display($newfilename).'</p><p>'.
                    878:                 &mt('The name of the new file needs to end with an appropriate file extension to indicate the type of file to create.').'<br />'.
                    879:                 &mt('The following are valid extensions: [_1].',$validexts).
                    880:                 '</p></form><p>'.
                    881: 		'<form name="fileaction" action="/adm/cfile" method="post">'.
                    882:                 '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
                    883: 		'<input type="hidden" name="action" value="newfile" />'.
                    884: 	        '<span class ="LC_nobreak">'.&mt('Enter a file name: ').'<input type="text" name="newfilename" value="Type Name Here" onfocus="if (this.value == '."'Type Name Here') this.value=''".'" />&nbsp;<input type="submit" value="Go" />'.
                    885:                 '</span></form></p>'.
                    886:                 '<p><form action="'.&url($fn).
1.97      bisitz    887:                 '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></form></p>');
1.96      raeburn   888:             return;
                    889:         }
                    890: 
1.47      sakharuk  891: 	$request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
1.22      albertel  892: 	$request->print('</form>');
1.96      raeburn   893: 
1.36      www       894: 	$request->print('<form action="'.&url($newfilename).
1.97      bisitz    895: 			'" method="post"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
1.36      www       896: 	$request->print('<form action="'.&url($fn).
1.97      bisitz    897: 			'" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.22      albertel  898:     }
1.96      raeburn   899:     return;
                    900: }
                    901: 
                    902: sub filename_check {
                    903:     my ($newfilename) = @_;
                    904:     ##Informs User (name).(number).(extension) not allowed
                    905:     if($newfilename =~ /\.(\d+)\.(\w+)$/){
                    906:         $r->print('<span class="LC_error">'.$newfilename.
                    907:                   ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
                    908:                   ' '.&mt('Not Allowed').'</span>');
                    909:         return;
                    910:     }
                    911:     if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
                    912:         $r->print('<span class="LC_error">'.$newfilename.
                    913:                   ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
                    914:                   ' '.&mt('Not Allowed').'</span>');
                    915:         return;
                    916:     }
                    917:     return 'ok'; 
1.22      albertel  918: }
                    919: 
                    920: =pod
                    921: 
1.12      foxr      922: =item phaseone($r, $fn, $uname, $udom)
                    923: 
                    924:   Peforms phase one processing of the request.  In phase one, error messages
                    925: are returned if the request cannot be performed (e.g. attempts to manipulate
                    926: files that are nonexistent).  If the operation can be performed, what is
                    927: about to be done will be presented to the user for confirmation.  If the
                    928: user confirms the request, then phase two is executed, the action 
                    929: performed and reported to the user.
                    930: 
                    931:  Parameters:
                    932: 
                    933: =over 4
                    934: 
                    935: =item $r  - request object [in] - The Apache request being executed.
                    936: 
                    937: =item $fn = string [in] - The filename being manipulated by the 
                    938:                              request.
                    939: 
                    940: =item $uname - string [in] Name of user logged in and doing this action.
                    941: 
1.17      harris41  942: =item $udom  - string [in] Domain name under which the user logged in. 
1.12      foxr      943: 
                    944: =back
                    945: 
                    946: =cut
1.8       albertel  947: 
1.1       www       948: sub phaseone {
1.55      albertel  949:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr      950:   
1.58      albertel  951:     my $doingdir=0;
1.67      albertel  952:     if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
1.106     raeburn   953:     my ($newfilename,$error) = 
                    954:         &cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname);
                    955:     unless ($error) {
                    956:         ($newfilename,$error)=&relativeDest($fn,$newfilename,$uname);
                    957:     }
                    958:     if ($error) {
                    959:         my $dirlist;
                    960:         if ($fn=~m{^(.*/)[^/]+$}) {
                    961:             $dirlist=$1;
                    962:         } else {
                    963:             $dirlist=$fn; 
                    964:         }
                    965:         $r->print('<div class="LC_error">'.$error.'</div>'.
                    966:                   '<h3><a href="'.&url($dirlist).'">'.&mt('Return to Directory').
                    967:                   '</a></h3>');
                    968:         return;
                    969:     }
1.55      albertel  970:     $r->print('<form action="/adm/cfile" method="post">'.
                    971: 	      '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
                    972: 	      '<input type="hidden" name="phase" value="two" />'.
1.67      albertel  973: 	      '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
1.12      foxr      974:   
1.67      albertel  975:     if ($env{'form.action'} eq 'rename') {
1.55      albertel  976: 	&Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
1.67      albertel  977:     } elsif ($env{'form.action'} eq 'move') {
1.55      albertel  978: 	&Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
1.67      albertel  979:     } elsif ($env{'form.action'} eq 'delete') { 
1.55      albertel  980: 	&Delete1($r, $uname, $udom, $fn);
1.67      albertel  981:     } elsif ($env{'form.action'} eq 'decompress') {
1.55      albertel  982: 	&Decompress1($r, $uname, $udom, $fn);
1.67      albertel  983:     } elsif ($env{'form.action'} eq 'copy') { 
1.55      albertel  984: 	if($newfilename) {
                    985: 	    &Copy1($r, $uname, $udom, $fn, $newfilename);
                    986: 	} else {
1.100     bisitz    987:             $r->print('<p class="LC_error">'
                    988:                      .&mt('No new filename specified.')
                    989:                      .'</p></form>'
                    990:             );
1.55      albertel  991: 	}
1.67      albertel  992:     } elsif ($env{'form.action'} eq 'newdir') {
1.55      albertel  993: 	my $mode = '';
1.67      albertel  994: 	if (exists($env{'form.callingmode'}) ) {
                    995: 	    $mode = $env{'form.callingmode'};
1.55      albertel  996: 	}   
                    997: 	&NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
1.67      albertel  998:     }  elsif ($env{'form.action'} eq 'newfile' ||
                    999: 	      $env{'form.action'} eq 'newhtmlfile' ||
                   1000: 	      $env{'form.action'} eq 'newproblemfile' ||
                   1001: 	      $env{'form.action'} eq 'newpagefile' ||
                   1002: 	      $env{'form.action'} eq 'newsequencefile' ||
                   1003: 	      $env{'form.action'} eq 'newrightsfile' ||
                   1004: 	      $env{'form.action'} eq 'newstyfile' ||
1.86      albertel 1005: 	      $env{'form.action'} eq 'newtaskfile' ||
1.67      albertel 1006:               $env{'form.action'} eq 'newlibraryfile' ||
                   1007: 	      $env{'form.action'} eq 'Select Action') {
1.65      www      1008:         my $empty=&mt('Type Name Here');
                   1009: 	if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
1.55      albertel 1010: 	    &NewFile1($r, $uname, $udom, $fn, $newfilename);
                   1011: 	} else {
1.100     bisitz   1012:             $r->print('<p class="LC_error">'
                   1013:                      .&mt('No new filename specified.')
                   1014:                      .'</p></form>'
                   1015:             );
1.55      albertel 1016: 	}
                   1017:     }
1.12      foxr     1018: }
                   1019: 
                   1020: =pod
                   1021: 
                   1022: =item Rename2($request, $user, $directory, $oldfile, $newfile)
                   1023: 
1.17      harris41 1024: Performs phase 2 processing of a rename reequest.   This is where the
1.12      foxr     1025: actual rename is performed.
                   1026: 
                   1027: Parameters
                   1028: 
                   1029: =over 4
                   1030: 
                   1031: =item $request - Apache request object [in] The request being processed.
                   1032: 
                   1033: =item $user  - string [in] The name of the user initiating the request.
                   1034: 
                   1035: =item $directory - string [in] The name of the directory relative to the
                   1036:                  construction space top level of the renamed file.
                   1037: 
                   1038: =item $oldfile - Name of the file.
                   1039: 
                   1040: =item $newfile - Name of the new file.
                   1041: 
                   1042: =back
                   1043: 
                   1044: Returns:
                   1045: 
                   1046: =over 4
                   1047: 
                   1048: =item 1 Success.
                   1049: 
                   1050: =item 0 Failure.
                   1051: 
                   1052: =cut
                   1053: 
                   1054: sub Rename2 {
                   1055: 
1.55      albertel 1056:     my ($request, $user, $directory, $oldfile, $newfile) = @_;
1.12      foxr     1057: 
1.55      albertel 1058:     &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
                   1059: 	   " new file ".$newfile."\n");
                   1060:     &Debug($request, "Target is: ".$directory.'/'.
                   1061: 	   $newfile);
                   1062:     if (-e $oldfile) {
                   1063: 
                   1064: 	my $oRN=$oldfile;
                   1065: 	my $nRN=$newfile;
                   1066: 	unless (rename($oldfile,$newfile)) {
1.83      albertel 1067: 	    $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1068: 	    return 0;
                   1069: 	}
                   1070: 	## If old name.(extension) exits, move under new name.
                   1071: 	## If it doesn't exist and a new.(extension) exists  
                   1072: 	## delete it (only concern when renaming over files)
                   1073: 	my $tmp1=$oRN.'.meta';
                   1074: 	my $tmp2=$nRN.'.meta';
                   1075: 	if(-e $tmp1){
                   1076: 	    unless(rename($tmp1,$tmp2)){ }
                   1077: 	} elsif(-e $tmp2){
                   1078: 	    unlink $tmp2;
                   1079: 	}
                   1080: 	$tmp1=$oRN.'.save';
                   1081: 	$tmp2=$nRN.'.save';
                   1082: 	if(-e $tmp1){
                   1083: 	    unless(rename($tmp1,$tmp2)){ }
                   1084: 	} elsif(-e $tmp2){
                   1085: 	    unlink $tmp2;
                   1086: 	}
                   1087: 	$tmp1=$oRN.'.log';
                   1088: 	$tmp2=$nRN.'.log';
                   1089: 	if(-e $tmp1){
                   1090: 	    unless(rename($tmp1,$tmp2)){ }
                   1091: 	} elsif(-e $tmp2){
                   1092: 	    unlink $tmp2;
                   1093: 	}
                   1094: 	$tmp1=$oRN.'.bak';
                   1095: 	$tmp2=$nRN.'.bak';
                   1096: 	if(-e $tmp1){
                   1097: 	    unless(rename($tmp1,$tmp2)){ }
                   1098: 	} elsif(-e $tmp2){
                   1099: 	    unlink $tmp2;
                   1100: 	}
                   1101:     } else {
1.103     bisitz   1102:         $request->print(
                   1103:             '<p>'
                   1104:            .&mt('No such file: [_1]',
                   1105:                 &display($oldfile))
                   1106:            .'</p></form>'
1.100     bisitz   1107:         );
1.55      albertel 1108: 	return 0;
                   1109:     }
                   1110:     return 1;
1.12      foxr     1111: }
1.55      albertel 1112: 
1.12      foxr     1113: =pod
                   1114: 
                   1115: =item Delete2($request, $user, $filename)
                   1116: 
                   1117:   Performs phase two of a delete.  The user has confirmed that they want 
                   1118: to delete the selected file.   The file is deleted and the results of the
                   1119: delete attempt are indicated.
                   1120: 
                   1121: Parameters:
                   1122: 
                   1123: =over 4
                   1124: 
                   1125: =item $request - Apache Request object [in] the request object for the current
                   1126:                  delete operation.
                   1127: 
                   1128: =item $user    - string [in]  The name of the user initiating the delete
                   1129:                  request.
                   1130: 
1.17      harris41 1131: =item $filename - string [in] The name of the file, relative to construction
                   1132:                   space, to delete.
1.12      foxr     1133: 
                   1134: =back
                   1135: 
                   1136: Returns:
                   1137:   1 - success.
                   1138:   0 - Failure.
                   1139: 
                   1140: =cut
                   1141: 
                   1142: sub Delete2 {
1.55      albertel 1143:     my ($request, $user, $filename) = @_;
1.70      raeburn  1144:     if (-d $filename) { 
                   1145: 	unless (&empty_directory($filename,'Delete2')) { 
1.83      albertel 1146: 	    $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>'); 
1.55      albertel 1147: 	    return 0;
                   1148: 	} else {   
                   1149: 	    if(-e $filename) {
                   1150: 		unless(rmdir($filename)) {
1.83      albertel 1151: 		    $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1152: 		    return 0;
                   1153: 		}
                   1154: 	    } else {
1.100     bisitz   1155:         	$request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1.55      albertel 1156: 		return 0;
                   1157: 	    }
                   1158: 	}
1.48      taceyjo1 1159:     } else {
1.55      albertel 1160: 	if(-e $filename) {
                   1161: 	    unless(unlink($filename)) {
1.83      albertel 1162: 		$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1163: 		return 0;
                   1164: 	    }
                   1165: 	} else {
1.100     bisitz   1166:             $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1.55      albertel 1167: 	    return 0;
1.46      taceyjo1 1168: 	}
1.55      albertel 1169:     }
                   1170:     return 1;
1.12      foxr     1171: }
                   1172: 
                   1173: =pod
                   1174: 
                   1175: =item Copy2($request, $username, $dir, $oldfile, $newfile)
                   1176: 
                   1177:    Performs phase 2 of a copy.  The file is copied and the status 
                   1178:    of that copy is reported back to the user.
                   1179: 
                   1180: =over 4
                   1181: 
                   1182: =item $request - Apache request object [in]; the apache request currently
                   1183:                  being executed.
                   1184: 
                   1185: =item $username - string [in] Name of the user who is requesting the copy.
                   1186: 
                   1187: =item $dir - string [in] Directory path relative to the construction space
                   1188:              of the destination file.
                   1189: 
                   1190: =item $oldfile - string [in] Name of the source file.
                   1191: 
                   1192: =item $newfile - string [in] Name of the destination file.
                   1193: 
                   1194: 
                   1195: =back
                   1196: 
1.71      raeburn  1197: Returns 0 failure, and 1 successs.
1.12      foxr     1198: 
                   1199: =cut
1.1       www      1200: 
1.12      foxr     1201: sub Copy2 {
                   1202:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
                   1203:     &Debug($request ,"Will try to copy $oldfile to $newfile");
                   1204:     if(-e $oldfile) {
1.71      raeburn  1205:         if ($oldfile eq $newfile) {
1.83      albertel 1206:             $request->print('<span class="LC_error">'.&mt('Warning').': '.&mt('Name of new file is the same as name of old file').' - '.&mt('no action taken').'.</span>');
1.71      raeburn  1207:             return 1;
                   1208:         }
1.12      foxr     1209: 	unless (copy($oldfile, $newfile)) {
1.83      albertel 1210: 	    $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1.12      foxr     1211: 	    return 0;
1.61      albertel 1212: 	} elsif (!chmod(0660, $newfile)) {
1.83      albertel 1213: 	    $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1.61      albertel 1214: 	    return 0;
                   1215: 	} elsif (-e $oldfile.'.meta' && 
                   1216: 		 !copy($oldfile.'.meta', $newfile.'.meta') &&
                   1217: 		 !chmod(0660, $newfile.'.meta')) {
1.83      albertel 1218: 	    $request->print('<span class="LC_error">'.&mt('copy metadata error').
                   1219: 			    ': '.$!.'</span>');
1.61      albertel 1220: 	    return 0;
1.12      foxr     1221: 	} else {
                   1222: 	    return 1;
                   1223: 	}
1.11      albertel 1224:     } else {
1.100     bisitz   1225:         $request->print('<p class="LC_error">'.&mt('No such file').'</p>');
1.12      foxr     1226: 	return 0;
1.11      albertel 1227:     }
1.12      foxr     1228:     return 1;
                   1229: }
1.55      albertel 1230: 
1.12      foxr     1231: =pod
                   1232: 
                   1233: =item NewDir2($request, $user, $newdirectory)
1.1       www      1234: 
1.12      foxr     1235: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
                   1236: 	reporting the results of that creation to the user.
                   1237: 	
                   1238: Parameters:
                   1239: =over 4
                   1240: 
                   1241: =item $request  - Apache request object [in].  Object representing the current HTTP request.
                   1242: 
                   1243: =item $user - string [in] The name of the user that is initiating the request.
                   1244: 
                   1245: =item $newdirectory - string [in] The full path of the directory being created.
                   1246: 
                   1247: =back
                   1248: 
                   1249: Returns 0 - failure 1 - success.
                   1250: 
                   1251: =cut
1.8       albertel 1252: 
1.12      foxr     1253: sub NewDir2 {
1.55      albertel 1254:     my ($request, $user, $newdirectory) = @_;
1.12      foxr     1255:   
1.55      albertel 1256:     unless(mkdir($newdirectory, 02770)) {
1.83      albertel 1257: 	$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1258: 	return 0;
                   1259:     }
                   1260:     unless(chmod(02770, ($newdirectory))) {
1.83      albertel 1261: 	$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1262: 	return 0;
                   1263:     }
                   1264:     return 1;
1.1       www      1265: }
1.55      albertel 1266: 
1.44      taceyjo1 1267: sub decompress2 {
1.55      albertel 1268:     my ($r, $user, $dir, $file) = @_;
1.88      raeburn  1269:     &Apache::lonnet::appenv({'cgi.file' => $file});
                   1270:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
1.55      albertel 1271:     my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   1272:     $r->print($result);
                   1273:     &Apache::lonnet::delenv('cgi.file');
                   1274:     &Apache::lonnet::delenv('cgi.dir');
                   1275:     return 1;
1.44      taceyjo1 1276: }
1.55      albertel 1277: 
1.12      foxr     1278: =pod
                   1279: 
                   1280: =item phasetwo($r, $fn, $uname, $udom)
                   1281: 
                   1282:    Controls the phase 2 processing of file management
                   1283:    requests for construction space.  In phase one, the user
                   1284:    was asked to confirm the operation.  In phase 2, the operation
                   1285:    is performed and the result is shown.
                   1286: 
                   1287:   The strategy is to break out the processing into specific action processors
                   1288:   named action2 where action is the requested action and the 2 denotes 
                   1289:   phase 2 processing.
                   1290: 
                   1291: Parameters:
                   1292: 
                   1293: =over 4
                   1294: 
                   1295: =item  $r     - Apache Request object [in] The request object for this httpd
                   1296:            transaction.
                   1297: 
                   1298: =item  $fn    - string [in]  A filename indicating the object that is being
                   1299:            manipulated.
                   1300: 
                   1301: =item  $uname - string [in] The name of the user initiating the file management
                   1302:            request.
                   1303: 
                   1304: =item  $udom  - string  [in] The login domain of the user initiating the
                   1305:            file management request.
                   1306: =back
                   1307: 
                   1308: =cut
                   1309: 
1.1       www      1310: sub phasetwo {
                   1311:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr     1312:     
1.9       foxr     1313:     &Debug($r, "loncfile - Entering phase 2 for $fn");
1.12      foxr     1314:     
1.78      banghart 1315:     # Break down the file into its component pieces.
1.12      foxr     1316:     
1.27      albertel 1317:     my $dir;		# Directory path
                   1318:     my $main;		# Filename.
                   1319:     my $suffix;		# Extension.
1.46      taceyjo1 1320:     if ($fn=~m:(.*)/([^/]+):) {
1.27      albertel 1321: 	$dir=$1;		# Directory path
                   1322: 	$main=$2;		# Filename.
1.54      albertel 1323:     }
                   1324:     if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1.68      albertel 1325: 	$suffix=$1; #This is the actually filename extension if it exists
1.66      albertel 1326: 	$main=~s/\.\w+$//; #strip the extension
1.54      albertel 1327:     }
1.79      banghart 1328:     my $dest;                       #
                   1329:     my $dest_dir;                   # On success this is where we'll go.
                   1330:     my $disp_newname;               #
                   1331:     my $dest_newname;               #
1.55      albertel 1332:     &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1.67      albertel 1333:     &Debug($r,"    newfilename = ".$env{'form.newfilename'});
1.9       foxr     1334: 
                   1335:     my $conspace=$fn;
1.12      foxr     1336:     
1.55      albertel 1337:     &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1.12      foxr     1338:     
1.67      albertel 1339:     &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1.12      foxr     1340:     
                   1341:     # Select the appropriate processing sub.
1.67      albertel 1342:     if ($env{'form.action'} eq 'decompress') { 
1.66      albertel 1343: 	$main .= '.'.$suffix;
1.55      albertel 1344: 	if(!&decompress2($r, $uname, $dir, $main)) {
                   1345: 	    return ;
                   1346: 	}
                   1347: 	$dest = $dir."/.";
1.67      albertel 1348:     } elsif ($env{'form.action'} eq 'rename' ||
                   1349: 	     $env{'form.action'} eq 'move') {
                   1350: 	if($env{'form.newfilename'}) {
1.27      albertel 1351: 	    if (!defined($dir)) {
                   1352: 		$fn=~m:^(.*)/:;
1.55      albertel 1353: 		$dir=$1; 
1.27      albertel 1354: 	    }
1.67      albertel 1355: 	    if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.12      foxr     1356: 		return;
                   1357: 	    }
1.78      banghart 1358: 	    $dest = $dir."/";
1.79      banghart 1359: 	    $dest_newname = $env{'form.newfilename'};
                   1360: 	    $env{'form.newfilename'} =~ /.+(\/.+$)/;
                   1361: 	    $disp_newname = $1;
                   1362: 	    $disp_newname =~ s/\///;
1.12      foxr     1363: 	}
1.67      albertel 1364:     } elsif ($env{'form.action'} eq 'delete') { 
                   1365: 	if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1.12      foxr     1366: 	    return ;
                   1367: 	}
                   1368: 	# Once a resource is deleted, we just list the directory that
                   1369: 	# previously held it.
                   1370: 	#
1.20      albertel 1371: 	$dest = $dir."/.";		# Parent dir.
1.67      albertel 1372:     } elsif ($env{'form.action'} eq 'copy') { 
                   1373: 	if($env{'form.newfilename'}) {
                   1374: 	    if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.44      taceyjo1 1375: 		return ;
1.55      albertel 1376: 	    }
1.67      albertel 1377: 	    $dest = $env{'form.newfilename'};
1.55      albertel 1378:      	} else {
1.100     bisitz   1379:             $r->print('<p class="LC_error">'.&mt('No New filename specified').'</p></form>');
1.12      foxr     1380: 	    return;
                   1381: 	}
                   1382: 	
1.67      albertel 1383:     } elsif ($env{'form.action'} eq 'newdir') {
                   1384:         my $newdir= $env{'form.newfilename'};
1.12      foxr     1385: 	if(!&NewDir2($r, $uname, $newdir)) {
                   1386: 	    return;
                   1387: 	}
1.55      albertel 1388: 	$dest = $newdir."/";
1.12      foxr     1389:     }
1.67      albertel 1390:     if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1.55      albertel 1391: 	$r->print('<h3><a href="javascript:self.close()">'.&mt('Done').'</a></h3>');
1.51      raeburn  1392:     } else {
1.79      banghart 1393:         if ($env{'form.action'} eq 'rename') {
                   1394:             $r->print('<h3><a href="'.&url($dest).'">'.&mt('Return to Directory').'</a></h3>');
                   1395:             $r->print('<h3><a href="'.&url($dest_newname).'">'.$disp_newname.'</a></h3>');
                   1396:         } else {
1.89      www      1397: 	    $r->print(&done(&url($dest)));
1.79      banghart 1398: 	}
1.51      raeburn  1399:     }
1.1       www      1400: }
                   1401: 
                   1402: sub handler {
                   1403: 
1.55      albertel 1404:     $r=shift;
1.1       www      1405: 
1.60      www      1406:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename']);
1.9       foxr     1407: 
1.55      albertel 1408:     &Debug($r, "loncfile.pm - handler entered");
1.67      albertel 1409:     &Debug($r, " filename: ".$env{'form.filename'});
                   1410:     &Debug($r, " newfilename: ".$env{'form.newfilename'});
1.36      www      1411: #
                   1412: # Determine the root filename
                   1413: # This could come in as "filename", which actually is a URL, or
                   1414: # as "qualifiedfilename", which is indeed a real filename in filesystem
                   1415: #
1.55      albertel 1416:     my $fn;
1.1       www      1417: 
1.67      albertel 1418:     if ($env{'form.filename'}) {
                   1419: 	&Debug($r, "test: $env{'form.filename'}");
1.77      www      1420: 	$fn=&unescape($env{'form.filename'});
1.55      albertel 1421: 	$fn=&URLToPath($fn);
1.67      albertel 1422:     }  elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {  
1.55      albertel 1423: 	#Just hijack the script only the first time around to inject the
                   1424: 	#correct information for further processing
1.77      www      1425: 	$fn=&unescape($env{'form.decompress'});
1.44      taceyjo1 1426: 	$fn=&URLToPath($fn);
1.67      albertel 1427: 	$env{'form.action'}="decompress";
                   1428:     } elsif ($env{'form.qualifiedfilename'}) {
                   1429: 	$fn=$env{'form.qualifiedfilename'};
1.55      albertel 1430:     } else {
                   1431: 	&Debug($r, "loncfile::handler - no form.filename");
1.67      albertel 1432: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.55      albertel 1433: 		       ' unspecified filename for cfile', $r->filename); 
                   1434: 	return HTTP_NOT_FOUND;
                   1435:     }
1.44      taceyjo1 1436: 
1.55      albertel 1437:     unless ($fn) { 
                   1438: 	&Debug($r, "loncfile::handler - doctored url is empty");
1.67      albertel 1439: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.55      albertel 1440: 		       ' trying to cfile non-existing file', $r->filename); 
                   1441: 	return HTTP_NOT_FOUND;
                   1442:     } 
1.1       www      1443: 
                   1444: # ----------------------------------------------------------- Start page output
1.55      albertel 1445:     my $uname;
                   1446:     my $udom;
                   1447: 
                   1448:     ($uname,$udom)=
                   1449: 	&Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                   1450:     &Debug($r, 
                   1451: 	   "loncfile::handler constructaccess uname = $uname domain = $udom");
                   1452:     unless (($uname) && ($udom)) {
                   1453: 	$r->log_reason($uname.' at '.$udom.
1.67      albertel 1454: 		       ' trying to manipulate file '.$env{'form.filename'}.
1.55      albertel 1455: 		       ' ('.$fn.') - not authorized', 
                   1456: 		       $r->filename); 
                   1457: 	return HTTP_NOT_ACCEPTABLE;
                   1458:     }
                   1459: 
1.1       www      1460: 
1.55      albertel 1461:     &Apache::loncommon::content_type($r,'text/html');
                   1462:     $r->send_http_header;
                   1463: 
1.75      albertel 1464:     my (%loaditem,$js);
                   1465: 
1.67      albertel 1466:     if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
                   1467: 	my $newdirname = $env{'form.newfilename'};
1.75      albertel 1468: 	$js = qq|
                   1469: <script type="text/javascript">
1.51      raeburn  1470: function writeDone() {
                   1471:     window.focus();
1.90      raeburn  1472:     opener.document.info.newdir.value = "$newdirname";
                   1473:     setTimeout("self.close()",10000);
1.51      raeburn  1474: }
                   1475:   </script>
1.75      albertel 1476: |;
1.76      albertel 1477: 	$loaditem{'onload'} = "writeDone()";
1.55      albertel 1478:     }
1.75      albertel 1479:     
1.99      bisitz   1480:     # Breadcrumbs
                   1481:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                   1482:     &Apache::lonhtmlcommon::add_breadcrumb({
                   1483:         'text'  => 'Construction Space',
1.101     bisitz   1484:         'href'  => &Apache::loncommon::authorspace(),
1.99      bisitz   1485:     });
                   1486:     &Apache::lonhtmlcommon::add_breadcrumb({
                   1487:         'text'  => 'File Operation',
                   1488:         'title' => 'Construction Space File Operation',
                   1489:         'href'  => '',
                   1490:     });
                   1491: 
1.75      albertel 1492:     $r->print(&Apache::loncommon::start_page('Construction Space File Operation',
                   1493: 					     $js,
1.99      bisitz   1494: 					     {'add_entries' => \%loaditem,})
                   1495:              .&Apache::lonhtmlcommon::breadcrumbs()
                   1496:              .&Apache::loncommon::head_subbox(
                   1497:                   &Apache::loncommon::CSTR_pageheader())
                   1498:     );
1.1       www      1499:   
1.55      albertel 1500:     $r->print('<h3>'.&mt('Location').': '.&display($fn).'</h3>');
1.1       www      1501:   
1.67      albertel 1502:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.105     www      1503:         $r->print('<p class="LC_info">'
1.98      bisitz   1504:                  .&mt('Co-Author [_1]',$uname.':'.$udom)
1.94      bisitz   1505:                  .'</p>'
                   1506:         );
1.55      albertel 1507:     }
                   1508: 
                   1509: 
1.67      albertel 1510:     &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
                   1511:     if ($env{'form.action'} eq 'delete') {
1.55      albertel 1512:       	$r->print('<h3>'.&mt('Delete').'</h3>');
1.67      albertel 1513:     } elsif ($env{'form.action'} eq 'rename') {
1.55      albertel 1514: 	$r->print('<h3>'.&mt('Rename').'</h3>');
1.67      albertel 1515:     } elsif ($env{'form.action'} eq 'move') {
1.55      albertel 1516: 	$r->print('<h3>'.&mt('Move').'</h3>');
1.67      albertel 1517:     } elsif ($env{'form.action'} eq 'newdir') {
1.55      albertel 1518: 	$r->print('<h3>'.&mt('New Directory').'</h3>');
1.67      albertel 1519:     } elsif ($env{'form.action'} eq 'decompress') {
1.55      albertel 1520: 	$r->print('<h3>'.&mt('Decompress').'</h3>');
1.67      albertel 1521:     } elsif ($env{'form.action'} eq 'copy') {
1.55      albertel 1522: 	$r->print('<h3>'.&mt('Copy').'</h3>');
1.67      albertel 1523:     } elsif ($env{'form.action'} eq 'newfile' ||
                   1524: 	     $env{'form.action'} eq 'newhtmlfile' ||
                   1525: 	     $env{'form.action'} eq 'newproblemfile' ||
                   1526: 	     $env{'form.action'} eq 'newpagefile' ||
                   1527: 	     $env{'form.action'} eq 'newsequencefile' ||
                   1528: 	     $env{'form.action'} eq 'newrightsfile' ||
                   1529: 	     $env{'form.action'} eq 'newstyfile' ||
1.86      albertel 1530: 	     $env{'form.action'} eq 'newtaskfile' ||
1.67      albertel 1531:              $env{'form.action'} eq 'newlibraryfile' ||
                   1532: 	     $env{'form.action'} eq 'Select Action' ) {
1.55      albertel 1533: 	$r->print('<h3>'.&mt('New Resource').'</h3>');
                   1534:     } else {
1.100     bisitz   1535:         $r->print('<p class="LC_error">'
                   1536:                  .&mt('Unknown Action').' '.$env{'form.action'}
                   1537:                  .'</p>'
                   1538:                  .&Apache::loncommon::end_page()
                   1539:         );
1.55      albertel 1540: 	return OK;  
                   1541:     }
1.67      albertel 1542:     if ($env{'form.phase'} eq 'two') {
1.55      albertel 1543: 	&Debug($r, "loncfile::handler  entering phase2");
                   1544: 	&phasetwo($r,$fn,$uname,$udom);
                   1545:     } else {
                   1546: 	&Debug($r, "loncfile::handler  entering phase1");
                   1547: 	&phaseone($r,$fn,$uname,$udom);
                   1548:     }
1.1       www      1549: 
1.75      albertel 1550:     $r->print(&Apache::loncommon::end_page());
1.55      albertel 1551:     return OK;  
1.1       www      1552: }
                   1553: 
                   1554: 1;
                   1555: __END__
1.9       foxr     1556: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>