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

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.94    ! bisitz     12: # $Id: loncfile.pm,v 1.93 2009/01/29 11:50:32 bisitz 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.80      albertel  178:     $fn=~s-^/home/($match_username)/public_html-/priv/$1-;
1.87      albertel  179:     return '<span class="LC_filename">'.$fn.'</span>';
1.36      www       180: }
                    181: 
1.50      www       182: 
                    183: # see if the file is
                    184: # a) published (return 0 if not)
                    185: # b) if, so obsolete (return 0 if not)
                    186: 
                    187: sub obsolete_unpub {
                    188:     my ($user,$domain,$construct)=@_;
                    189:     my $published=$construct;
                    190:     $published=~
                    191: 	s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//;
                    192:     if (-e $published) {
                    193: 	if (&Apache::lonnet::metadata($published,'obsolete')) {
                    194: 	    return 1;
                    195: 	}
                    196: 	return 0;
                    197:     } else {
                    198: 	return 1;
                    199:     }
                    200: }
                    201: 
1.70      raeburn   202: # see if directory is empty
1.74      www       203: # ignores any .meta, .save, .bak, and .log files created for a previously
1.70      raeburn   204: # published file, which has since been marked obsolete and deleted.
                    205: sub empty_directory {
                    206:     my ($dirname,$phase) = @_;
                    207:     if (opendir DIR, $dirname) {
                    208:         my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
                    209:         if (@files) { 
1.74      www       210:             my @orphans = grep(/\.(meta|save|log|bak)$/,@files);
1.70      raeburn   211:             if (scalar(@files) - scalar(@orphans) > 0) { 
                    212:                 return 0;
                    213:             } else {
                    214:                 if (($phase eq 'Delete2') && (@orphans > 0)) {
                    215:                     foreach my $file (@orphans) {
1.74      www       216:                         if ($file =~ /\.(meta|save|log|bak)$/) {
1.70      raeburn   217:                             unlink($dirname.$file);
                    218:                         }
                    219:                     }
                    220:                 }
                    221:             }
                    222:         }
                    223:         closedir(DIR);
                    224:         return 1;
                    225:     }
                    226:     return 0;
                    227: }
1.50      www       228: 
1.12      foxr      229: =pod
                    230: 
1.39      www       231: =item exists($user, $domain, $file)
1.12      foxr      232: 
1.17      harris41  233:    Determine if a resource file name has been published or exists
1.12      foxr      234:    in the construction space.
                    235: 
                    236:  Parameters:
                    237: 
                    238: =over 4
                    239: 
1.85      albertel  240: =item  $user     - string [in] - Name of the user for which to check.
1.12      foxr      241: 
1.85      albertel  242: =item  $domain   - string [in] - Name of the domain in which the resource
1.12      foxr      243:                           might have been published.
                    244: 
1.85      albertel  245: =item  $file     - string [in] - Name of the file.
                    246: 
                    247: =item  $creating - string [in] - optional, type of object being created,
                    248:                                either 'directory' or 'file'. Defaults to
                    249:                                'file' if unspecified.
1.12      foxr      250: 
                    251: =back
                    252: 
                    253: Returns:
                    254: 
                    255: =over 4
                    256: 
1.83      albertel  257: =item  string - Either undef, 'warning' or 'error' depending on the
                    258:                 type of problem
                    259: 
1.12      foxr      260: =item  string - Either where the resource exists as an html string that can
                    261:            be embedded in a dialog or an empty string if the resource
                    262:            does not exist.
                    263:   
                    264: =back
                    265: 
                    266: =cut
                    267: 
1.8       albertel  268: sub exists {
1.85      albertel  269:     my ($user, $domain, $construct, $creating) = @_;
                    270:     $creating ||= 'file';
                    271: 
1.55      albertel  272:     my $published=$construct;
                    273:     $published=~
1.83      albertel  274: 	s{^/home/$user/public_html/}{/home/httpd/html/res/$domain/$user/};
                    275:     my ($type,$result);
1.55      albertel  276:     if ( -d $construct ) {
1.83      albertel  277: 	return ('error','<p><span class="LC_error">'.&mt('Error: destination for operation is an existing directory.').'</span></p>');
                    278: 	
1.55      albertel  279:     }
1.83      albertel  280: 
1.55      albertel  281:     if ( -e $published) {
1.83      albertel  282: 	if ( -e $construct ) {
                    283: 	    $type = 'warning';
                    284: 	    $result.='<p><span class="LC_warning">'.&mt('Warning: target file exists, and has been published!').'</span></p>';
                    285: 	} else {
1.85      albertel  286: 	    my $published_type = (-d $published) ? 'directory' : 'file';
                    287: 
                    288: 	    if ($published_type eq $creating) {
                    289: 		$type = 'warning';
                    290: 		$result.='<p><span class="LC_warning">'.&mt("Warning: a published $published_type of this name exists.").'</span></p>';
                    291: 	    } else {
                    292: 		$type = 'error';
                    293: 		$result.='<p><span class="LC_error">'.&mt("Error: a published $published_type of this name exists.").'</span></p>';
                    294: 	    }
1.83      albertel  295: 	}
1.55      albertel  296:     } elsif ( -e $construct) {
1.83      albertel  297: 	$type = 'warning';
                    298: 	$result.='<p><span class="LC_warning">'.&mt('Warning: target file exists!').'</span></p>';
1.55      albertel  299:     }
1.83      albertel  300: 
                    301:     return ($type,$result);
1.8       albertel  302: }
                    303: 
1.12      foxr      304: =pod
                    305: 
                    306: =item checksuffix($old, $new)
                    307:         
                    308:   Determine if a resource filename suffix (the stuff after the .) would change
                    309: as a result of this operation.
                    310: 
                    311:  Parameters:
                    312: 
                    313: =over 4
                    314: 
                    315: =item  $old   = string [in]  Previous filename.
                    316: 
                    317: =item  $new   = string [in]  Resultant filename.
                    318: 
                    319: =back
                    320: 
                    321:  Returns:
                    322: 
                    323: =over 4
                    324: 
1.17      harris41  325: =item    Empty string if everything worked.
1.12      foxr      326: 
                    327: =item    String containing an error message if there was a problem.
                    328: 
                    329: =back
                    330: 
                    331: =cut
                    332: 
1.8       albertel  333: sub checksuffix {
                    334:     my ($old,$new) = @_;
                    335:     my $result;
                    336:     my $oldsuffix;
                    337:     my $newsuffix;
                    338:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
                    339:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
1.82      albertel  340:     if (lc($oldsuffix) ne lc($newsuffix)) {
1.12      foxr      341: 	$result.=
1.82      albertel  342:             '<p><span class="LC_warning">'.&mt('Warning: change of MIME type!').'</span></p>';
1.8       albertel  343:     }
                    344:     return $result;
                    345: }
1.32      albertel  346: 
                    347: sub cleanDest {
1.64      albertel  348:     my ($request,$dest,$subdir,$fn,$uname)=@_;
1.32      albertel  349:     #remove bad characters
1.58      albertel  350:     my $foundbad=0;
                    351:     if ($subdir && $dest =~/\./) {
                    352: 	$foundbad=1;
                    353: 	$dest=~s/\.//g;
                    354:     }
1.87      albertel  355:     $dest =~ s/(\s+$|^\s+)//g;
1.72      albertel  356:     if  ($dest=~/[\#\?&%\":]/) {
1.58      albertel  357: 	$foundbad=1;
1.72      albertel  358: 	$dest=~s/[\#\?&%\":]//g;
1.58      albertel  359:     }
1.63      albertel  360:     if ($dest=~m|/|) {
                    361: 	my ($newpath)=($dest=~m|(.*)/|);
1.64      albertel  362: 	$newpath=&relativeDest($fn,$newpath,$uname);
                    363: 	if (! -d "$newpath") {
1.93      bisitz    364: 	    $request->print('<p><span class="LC_error">'
1.92      bisitz    365:                            .&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."
                    366:                                ,'"'.&display($newpath).'"')
1.93      bisitz    367:                            .'</span></p>');
1.63      albertel  368: 	    $dest=~s|.*/||;
                    369: 	}
                    370:     }
1.84      albertel  371:     if ($dest =~ /\.(\d+)\.(\w+)$/){
                    372: 	$request->print('<span class="LC_error">'
1.93      bisitz    373: 			.&mt('Bad filename [_1]','<span class="LC_filename">'.&display($dest).'</span>')
                    374:                         .'<br />'
                    375:                         .&mt('[_1](name).(number).(extension)[_2] not allowed.','<tt>','</tt>')
                    376:                         .'<br />'
                    377:                         .&mt('Removing the [_1].number.[_2] from requested filename.','<tt>','</tt>')
1.84      albertel  378: 			.'</span>');
                    379: 	$dest =~ s/\.(\d+)(\.\w+)$/$2/;
                    380:     }
1.58      albertel  381:     if ($foundbad) {
1.83      albertel  382: 	$request->print("<p><span class=\"LC_error\">".&mt('Invalid characters in requested name have been removed.')."</span></p>");
1.32      albertel  383:     }
                    384:     return $dest;
                    385: }
                    386: 
1.36      www       387: sub relativeDest {
                    388:     my ($fn,$newfilename,$uname)=@_;
                    389:     if ($newfilename=~/^\//) {
                    390: # absolute, simply add path
                    391: 	$newfilename='/home/'.$uname.'/public_html/';
                    392:     } else {
                    393: 	my $dir=$fn;
                    394: 	$dir=~s/\/[^\/]+$//;
                    395: 	$newfilename=$dir.'/'.$newfilename;
                    396:     }
                    397:     $newfilename=~s://+:/:g; # remove duplicate /
                    398:     while ($newfilename=~m:/\.\./:) {
                    399: 	$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    400:     }
                    401:     return $newfilename;
                    402: }
                    403: 
1.12      foxr      404: =pod
                    405: 
                    406: =item CloseForm1($request, $user, $file)
                    407: 
                    408:    Close of a form on the successful completion of phase 1 processing
                    409: 
                    410: Parameters:
                    411: 
                    412: =over 4
                    413: 
                    414: =item  $request - Apache Request Object [in] - Apache server request object.
                    415: 
1.13      foxr      416: =item  $cancelurl - the url to go to on cancel.
1.12      foxr      417: 
                    418: =back
                    419: 
                    420: =cut
                    421: 
                    422: sub CloseForm1 {
1.55      albertel  423:     my ($request,  $fn) = @_;
                    424:     $request->print('<p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
                    425:     $request->print('<form action="'.&url($fn).
                    426: 		    '" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.12      foxr      427: }
                    428: 
                    429: 
                    430: =pod
                    431: 
                    432: =item CloseForm2($request, $user, $directory)
                    433: 
                    434:    Successfully close off the phase 2 form.
                    435: 
                    436: Parameters:
                    437: 
                    438: =over 4
                    439: 
                    440: =item   $request    - Apache Request object [in] - The request that is being
                    441:                  executed.
                    442: 
                    443: =item   $user       - string [in] - Name of the user that is initiating the
                    444:                  request.
                    445: 
                    446: =item   $directory  - string [in] - Directory in which the operation is 
                    447:                  being done relative to the top level construction space
                    448:                  directory.
                    449: 
                    450: =back
                    451: 
                    452: =cut
                    453: 
                    454: sub CloseForm2 {
1.55      albertel  455:     my ($request, $user, $fn) = @_;
1.89      www       456:     $request->print(&done(&url($fn)));
1.12      foxr      457: }
                    458: 
                    459: =pod
                    460: 
                    461: =item Rename1($request, $filename, $user, $domain, $dir)
                    462:  
                    463:    Perform phase 1 processing of the file rename operation.
                    464: 
                    465: Parameters:
                    466: 
                    467: =over 4
                    468: 
                    469: =item  $request   - Apache Request Object [in] The request object for the 
                    470: current request.
                    471: 
                    472: =item  $filename  - The filename relative to construction space.
                    473: 
                    474: =item  $user      - Name of the user making the request.
                    475: 
                    476: =item  $domain    - User login domain.
                    477: 
                    478: =item  $dir       - Directory specification of the path to the file.
                    479: 
                    480: =back
                    481: 
                    482: Side effects:
                    483: 
                    484: =over 4
                    485: 
                    486: =item A new form is displayed prompting for confirmation.  The newfilename
                    487: hidden field of this form is loaded with
                    488: new filename relative to the current directory ($dir).
                    489: 
                    490: =back
                    491: 
                    492: =cut  
                    493: 
                    494: sub Rename1 {
1.52      albertel  495:     my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
1.36      www       496: 
                    497:     if(-e $fn) {
                    498: 	if($newfilename) {
1.42      albertel  499: 	    # is dest a dir
1.52      albertel  500: 	    if ($style eq 'move') {
                    501: 		if (-d $newfilename) {
                    502: 		    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    503: 		}
1.42      albertel  504: 	    }
1.29      albertel  505: 	    if ($newfilename =~ m|/[^\.]+$|) {
1.36      www       506: 		#no extension add on original extension
                    507: 		if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
1.24      albertel  508: 		    $newfilename.='.'.$1;
                    509: 		}
                    510: 	    }
1.36      www       511: 	    $request->print(&checksuffix($fn, $newfilename));
1.27      albertel  512: 	    #renaming a dir, delete the trailing /
1.39      www       513:             #remove second to last element for current dir
                    514: 	    if (-d $fn) {
1.53      www       515: 		$newfilename=~/\.(\w+)$/;
                    516: 		if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
1.82      albertel  517: 		    $request->print('<br /><span classr="LC_warning">'.
1.55      albertel  518: 				    &mt('Cannot change MIME type of a directory').
1.82      albertel  519: 				    '</span>'.
1.55      albertel  520: 				    '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.53      www       521: 		    return;
                    522: 		}
1.39      www       523: 		$newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
1.27      albertel  524: 	    }
1.42      albertel  525: 	    $newfilename=~s://+:/:g; # remove duplicate /
                    526: 	    while ($newfilename=~m:/\.\./:) {
                    527: 		$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    528: 	    }
1.83      albertel  529: 	    my ($type, $return)=&exists($user, $domain, $newfilename);
1.21      albertel  530: 	    $request->print($return);
1.83      albertel  531: 	    if ($type eq 'error') {
1.47      sakharuk  532: 		$request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.21      albertel  533: 		return;
                    534: 	    }
1.50      www       535: 	    unless (&obsolete_unpub($user,$domain,$fn)) {
                    536: 		$request->print('<h3>'.&mt('Cannot rename or move non-obsolete published file').'</h3>'.
                    537: 				'<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
                    538: 		return;
                    539: 	    }
1.52      albertel  540: 	    my $action;
                    541: 	    if ($style eq 'rename') {
                    542: 		$action=&mt('Rename');
                    543: 	    } else {
                    544: 		$action=&mt('Move');
                    545: 	    }
1.23      albertel  546: 	    $request->print('<input type="hidden" name="newfilename" value="'.
1.12      foxr      547: 			    $newfilename.
1.52      albertel  548: 			    '" /><p>'.$action.' '.&display($fn).
1.87      albertel  549: 			    '</p><br />to '.&display($newfilename).'?</p>');
1.36      www       550: 	    &CloseForm1($request, $fn);
1.12      foxr      551: 	} else {
1.47      sakharuk  552: 	    $request->print('<p>'.&mt('No new filename specified.').'</p></form>');
1.12      foxr      553: 	    return;
                    554: 	}
                    555:     } else {
1.47      sakharuk  556: 	$request->print('<p> '.&mt('No such file').': '.&display($fn).'</p></form>');
1.12      foxr      557: 	return;
                    558:     }
                    559:     
                    560: }
1.55      albertel  561: 
1.12      foxr      562: =pod
                    563: 
                    564: =item Delete1
                    565: 
                    566:    Performs phase 1 processing of the delete operation.  In phase one
                    567:   we just check to be sure the file exists.
                    568: 
                    569: Parameters:
                    570: 
                    571: =over 4
                    572: 
1.36      www       573: =item   $request   - Apache Request Object [in] request object for the current 
1.12      foxr      574:                 request.
                    575: 
1.36      www       576: =item   $user      - string [in]  Name of the user initiating the request.
1.12      foxr      577: 
1.36      www       578: =item   $domain    - string [in]  Domain the initiating user is logged in as
1.13      foxr      579: 
1.36      www       580: =item   $filename  - string [in]  Source filename.
1.12      foxr      581: 
                    582: =back
                    583: 
                    584: =cut
                    585: 
                    586: sub Delete1 {
1.55      albertel  587:     my ($request, $user, $domain, $fn) = @_;
1.12      foxr      588: 
1.55      albertel  589:     if( -e $fn) {
                    590: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    591: 			$fn.'"/>');
1.70      raeburn   592:         if (-d $fn) {
                    593:             unless (&empty_directory($fn,'Delete1')) {
                    594:                 $request->print('<h3>'.&mt('Only empty directories may be deleted.').'</h3>'.
                    595:                             'You must delete the contents of the directory first.<br />'.
                    596:                             '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
                    597:                 return;
                    598:             }
                    599:         } else { 
                    600: 	    unless (&obsolete_unpub($user,$domain,$fn)) {
                    601: 	        $request->print('<h3>'.&mt('Cannot delete non-obsolete published file').'</h3>'.
1.55      albertel  602: 			    '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.70      raeburn   603: 	        return;
                    604: 	    }
                    605:         }
1.55      albertel  606: 	$request->print('<p>'.&mt('Delete').' '.&display($fn).'?</p>');
                    607: 	&CloseForm1($request, $fn);
                    608:     } else {
                    609: 	$request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
1.50      www       610:     }
1.12      foxr      611: }
                    612: 
                    613: =pod
                    614: 
                    615: =item Copy1($request, $user, $domain, $filename, $newfilename)
                    616: 
                    617:    Performs phase 1 processing of the construction space copy command.
1.17      harris41  618:    Ensure that the source file exists.  Ensure that a destination exists,
                    619:    also warn if the destination already exists.
1.12      foxr      620: 
                    621: Parameters:
                    622: 
                    623: =over 4
                    624: 
                    625: =item   $request   - Apache Request Object [in] request object for the current 
                    626:                 request.
                    627: 
                    628: =item   $user      - string [in]  Name of the user initiating the request.
                    629: 
                    630: =item   $domain    - string [in]  Domain the initiating user is logged in as
                    631: 
1.36      www       632: =item   $fn  - string [in]  Source filename.
1.12      foxr      633: 
                    634: =item   $newfilename-string [in]  Destination filename.
                    635: 
                    636: =back
                    637: 
                    638: =cut
                    639: 
                    640: sub Copy1 {
1.42      albertel  641:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.12      foxr      642: 
1.42      albertel  643:     if(-e $fn) {
                    644: 	# is dest a dir
                    645: 	if (-d $newfilename) {
                    646: 	    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    647: 	}
                    648: 	if ($newfilename =~ m|/[^\.]+$|) {
                    649: 	    #no extension add on original extension
                    650: 	    if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {	$newfilename.='.'.$1; }
                    651: 	} 
                    652: 	$newfilename=~s://+:/:g; # remove duplicate /
                    653: 	while ($newfilename=~m:/\.\./:) {
                    654: 	    $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    655: 	}
                    656: 	$request->print(&checksuffix($fn,$newfilename));
1.83      albertel  657: 	my ($type,$return)=&exists($user, $domain, $newfilename);
1.42      albertel  658: 	$request->print($return);
1.83      albertel  659: 	if ($type eq 'error') {
1.47      sakharuk  660: 	    $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.42      albertel  661: 	    return;
                    662: 	}
                    663: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    664: 			$newfilename.
1.47      sakharuk  665: 			'" /><p>'.&mt('Copy').' '.&display($fn).'<br />to '.
1.42      albertel  666: 			&display($newfilename).'?</p>');
                    667: 	&CloseForm1($request, $fn);
                    668:     } else {
1.47      sakharuk  669: 	$request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
1.21      albertel  670:     }
1.19      albertel  671: }
                    672: 
                    673: =pod
                    674: 
1.12      foxr      675: =item NewDir1
                    676:  
                    677:   Does all phase 1 processing of directory creation:
                    678:   Ensures that the user provides a new directory name,
                    679:   and that the directory does not already exist.
                    680: 
                    681: Parameters:
                    682: 
                    683: =over 4
                    684: 
                    685: =item   $request  - Apache Request Object [in] - Server request object for the
1.17      harris41  686:                current url.
1.12      foxr      687: 
                    688: =item   $username - Name of the user that is requesting the directory creation.
                    689: 
1.36      www       690: =item $domain - Domain user is in
                    691: 
                    692: =item   $fn     - source file.
1.12      foxr      693: 
                    694: =item   $newdir   - Name of the directory to be created; path relative to the 
                    695:                top level of construction space.
                    696: =back
                    697: 
                    698: Side Effects:
                    699: 
                    700: =over 4
                    701: 
                    702: =item A new form is displayed.  Clicking on the confirmation button
                    703: causes the newdir operation to transition into phase 2.  The hidden field
                    704: "newfilename" is set with the construction space path to the new directory.
                    705: 
                    706: 
                    707: =back
                    708: 
                    709: =cut
                    710: 
                    711: 
1.55      albertel  712: sub NewDir1 {
                    713:     my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
                    714: 
1.85      albertel  715:     my ($type, $result)=&exists($username,$domain,$newfilename,'directory');
1.83      albertel  716:     $request->print($result);
1.85      albertel  717:     if ($type eq 'error') {
1.83      albertel  718: 	$request->print('</form>');
1.55      albertel  719:     } else {
                    720: 	if ($mode eq 'testbank') {
                    721: 	    $request->print('<input type="hidden" name="callingmode" value="testbank">');
                    722: 	} elsif ($mode eq 'imsimport') {
                    723: 	    $request->print('<input type="hidden" name="callingmode" value="imsimport">');
                    724: 	}
                    725: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    726: 			$newfilename.'" /><p>'.&mt('Make new directory').' '.
                    727: 			&display($newfilename).'?</p>');
                    728: 	&CloseForm1($request, $fn);
                    729:     }
1.12      foxr      730: }
                    731: 
1.44      taceyjo1  732: 
                    733: sub Decompress1 {
1.55      albertel  734:     my ($request, $user, $domain, $fn) = @_;
                    735:     if( -e $fn) {
1.44      taceyjo1  736:    	$request->print('<input type="hidden" name="newfilename" value="'.$fn.'"/>');
1.47      sakharuk  737:    	$request->print('<p>'.&mt('Decompress').' '.&display($fn).'?</p>');
1.44      taceyjo1  738:    	&CloseForm1($request, $fn);
1.55      albertel  739:     } else {
                    740: 	$request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
                    741:     }
1.44      taceyjo1  742: }
1.55      albertel  743: 
1.12      foxr      744: =pod
                    745: 
1.22      albertel  746: =item NewFile1
                    747:  
                    748:   Does all phase 1 processing of file creation:
                    749:   Ensures that the user provides a new filename, adds proper extension
                    750:   if needed and that the file does not already exist, if it is a html,
                    751:   problem, page, or sequence, it then creates a form link to hand the
                    752:   actual creation off to the proper handler.
                    753: 
                    754: Parameters:
                    755: 
                    756: =over 4
                    757: 
                    758: =item   $request  - Apache Request Object [in] - Server request object for the
                    759:                current url.
                    760: 
                    761: =item   $username - Name of the user that is requesting the directory creation.
                    762: 
                    763: =item   $domain   - Name of the domain of the user
                    764: 
1.36      www       765: =item   $fn      - Source file name
1.22      albertel  766: 
                    767: =item   $newfilename
                    768:                   - Name of the file to be created; no path information
                    769: =back
                    770: 
                    771: Side Effects:
                    772: 
                    773: =over 4
                    774: 
                    775: =item 2 new forms are displayed.  Clicking on the confirmation button
                    776: causes the browser to attempt to load the specfied URL, allowing the
1.36      www       777: proper handler to take care of file creation. There is also a Cancel
1.22      albertel  778: button which returns you to the driectory listing you came from
                    779: 
                    780: =back
                    781: 
                    782: =cut
                    783: 
                    784: sub NewFile1 {
1.36      www       785:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.22      albertel  786: 
1.67      albertel  787:     if ($env{'form.action'} =~ /new(.+)file/) {
1.22      albertel  788: 	my $extension=$1;
1.40      www       789: 
                    790:         ##Informs User (name).(number).(extension) not allowed 
                    791: 	if($newfilename =~ /\.(\d+)\.(\w+)$/){
1.83      albertel  792: 	    $r->print('<span class="LC_error">'.$newfilename.
1.69      www       793: 		      ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
1.83      albertel  794: 		      ' '.&mt('Not Allowed').'</span>');
1.69      www       795: 	    return;
                    796: 	}
                    797: 	if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
1.83      albertel  798: 	    $r->print('<span class="LC_error">'.$newfilename.
1.69      www       799: 		      ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
1.83      albertel  800: 		      ' '.&mt('Not Allowed').'</span>');
1.40      www       801: 	    return;
                    802: 	}
1.22      albertel  803: 	if ($newfilename !~ /\Q.$extension\E$/) {
1.81      albertel  804: 	    if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
1.26      albertel  805: 		#already has an extension strip it and add in expected one
1.81      albertel  806: 		$newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
1.26      albertel  807: 	    }
1.22      albertel  808: 	    $newfilename.=".$extension";
                    809: 	}
1.31      albertel  810:     }
1.83      albertel  811:     my ($type, $result)=&exists($user,$domain,$newfilename);
                    812:     $request->print($result);
                    813:     if ($type eq 'error') {
                    814: 	$request->print('</form>');
1.39      www       815:     } else {
1.83      albertel  816: 	
1.47      sakharuk  817: 	$request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
1.22      albertel  818: 	$request->print('</form>');
1.36      www       819: 	$request->print('<form action="'.&url($newfilename).
1.47      sakharuk  820: 			'" method="POST"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
1.36      www       821: 	$request->print('<form action="'.&url($fn).
1.47      sakharuk  822: 			'" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.22      albertel  823:     }
                    824: }
                    825: 
                    826: =pod
                    827: 
1.12      foxr      828: =item phaseone($r, $fn, $uname, $udom)
                    829: 
                    830:   Peforms phase one processing of the request.  In phase one, error messages
                    831: are returned if the request cannot be performed (e.g. attempts to manipulate
                    832: files that are nonexistent).  If the operation can be performed, what is
                    833: about to be done will be presented to the user for confirmation.  If the
                    834: user confirms the request, then phase two is executed, the action 
                    835: performed and reported to the user.
                    836: 
                    837:  Parameters:
                    838: 
                    839: =over 4
                    840: 
                    841: =item $r  - request object [in] - The Apache request being executed.
                    842: 
                    843: =item $fn = string [in] - The filename being manipulated by the 
                    844:                              request.
                    845: 
                    846: =item $uname - string [in] Name of user logged in and doing this action.
                    847: 
1.17      harris41  848: =item $udom  - string [in] Domain name under which the user logged in. 
1.12      foxr      849: 
                    850: =back
                    851: 
                    852: =cut
1.8       albertel  853: 
1.1       www       854: sub phaseone {
1.55      albertel  855:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr      856:   
1.58      albertel  857:     my $doingdir=0;
1.67      albertel  858:     if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
                    859:     my $newfilename=&cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname);
1.55      albertel  860:     $newfilename=&relativeDest($fn,$newfilename,$uname);
                    861:     $r->print('<form action="/adm/cfile" method="post">'.
                    862: 	      '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
                    863: 	      '<input type="hidden" name="phase" value="two" />'.
1.67      albertel  864: 	      '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
1.12      foxr      865:   
1.67      albertel  866:     if ($env{'form.action'} eq 'rename') {
1.55      albertel  867: 	&Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
1.67      albertel  868:     } elsif ($env{'form.action'} eq 'move') {
1.55      albertel  869: 	&Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
1.67      albertel  870:     } elsif ($env{'form.action'} eq 'delete') { 
1.55      albertel  871: 	&Delete1($r, $uname, $udom, $fn);
1.67      albertel  872:     } elsif ($env{'form.action'} eq 'decompress') {
1.55      albertel  873: 	&Decompress1($r, $uname, $udom, $fn);
1.67      albertel  874:     } elsif ($env{'form.action'} eq 'copy') { 
1.55      albertel  875: 	if($newfilename) {
                    876: 	    &Copy1($r, $uname, $udom, $fn, $newfilename);
                    877: 	} else {
                    878: 	    $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
                    879: 	}
1.67      albertel  880:     } elsif ($env{'form.action'} eq 'newdir') {
1.55      albertel  881: 	my $mode = '';
1.67      albertel  882: 	if (exists($env{'form.callingmode'}) ) {
                    883: 	    $mode = $env{'form.callingmode'};
1.55      albertel  884: 	}   
                    885: 	&NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
1.67      albertel  886:     }  elsif ($env{'form.action'} eq 'newfile' ||
                    887: 	      $env{'form.action'} eq 'newhtmlfile' ||
                    888: 	      $env{'form.action'} eq 'newproblemfile' ||
                    889: 	      $env{'form.action'} eq 'newpagefile' ||
                    890: 	      $env{'form.action'} eq 'newsequencefile' ||
                    891: 	      $env{'form.action'} eq 'newrightsfile' ||
                    892: 	      $env{'form.action'} eq 'newstyfile' ||
1.86      albertel  893: 	      $env{'form.action'} eq 'newtaskfile' ||
1.67      albertel  894:               $env{'form.action'} eq 'newlibraryfile' ||
                    895: 	      $env{'form.action'} eq 'Select Action') {
1.65      www       896:         my $empty=&mt('Type Name Here');
                    897: 	if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
1.55      albertel  898: 	    &NewFile1($r, $uname, $udom, $fn, $newfilename);
                    899: 	} else {
                    900: 	    $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
                    901: 	}
                    902:     }
1.12      foxr      903: }
                    904: 
                    905: =pod
                    906: 
                    907: =item Rename2($request, $user, $directory, $oldfile, $newfile)
                    908: 
1.17      harris41  909: Performs phase 2 processing of a rename reequest.   This is where the
1.12      foxr      910: actual rename is performed.
                    911: 
                    912: Parameters
                    913: 
                    914: =over 4
                    915: 
                    916: =item $request - Apache request object [in] The request being processed.
                    917: 
                    918: =item $user  - string [in] The name of the user initiating the request.
                    919: 
                    920: =item $directory - string [in] The name of the directory relative to the
                    921:                  construction space top level of the renamed file.
                    922: 
                    923: =item $oldfile - Name of the file.
                    924: 
                    925: =item $newfile - Name of the new file.
                    926: 
                    927: =back
                    928: 
                    929: Returns:
                    930: 
                    931: =over 4
                    932: 
                    933: =item 1 Success.
                    934: 
                    935: =item 0 Failure.
                    936: 
                    937: =cut
                    938: 
                    939: sub Rename2 {
                    940: 
1.55      albertel  941:     my ($request, $user, $directory, $oldfile, $newfile) = @_;
1.12      foxr      942: 
1.55      albertel  943:     &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
                    944: 	   " new file ".$newfile."\n");
                    945:     &Debug($request, "Target is: ".$directory.'/'.
                    946: 	   $newfile);
                    947:     if (-e $oldfile) {
                    948: 
                    949: 	my $oRN=$oldfile;
                    950: 	my $nRN=$newfile;
                    951: 	unless (rename($oldfile,$newfile)) {
1.83      albertel  952: 	    $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel  953: 	    return 0;
                    954: 	}
                    955: 	## If old name.(extension) exits, move under new name.
                    956: 	## If it doesn't exist and a new.(extension) exists  
                    957: 	## delete it (only concern when renaming over files)
                    958: 	my $tmp1=$oRN.'.meta';
                    959: 	my $tmp2=$nRN.'.meta';
                    960: 	if(-e $tmp1){
                    961: 	    unless(rename($tmp1,$tmp2)){ }
                    962: 	} elsif(-e $tmp2){
                    963: 	    unlink $tmp2;
                    964: 	}
                    965: 	$tmp1=$oRN.'.save';
                    966: 	$tmp2=$nRN.'.save';
                    967: 	if(-e $tmp1){
                    968: 	    unless(rename($tmp1,$tmp2)){ }
                    969: 	} elsif(-e $tmp2){
                    970: 	    unlink $tmp2;
                    971: 	}
                    972: 	$tmp1=$oRN.'.log';
                    973: 	$tmp2=$nRN.'.log';
                    974: 	if(-e $tmp1){
                    975: 	    unless(rename($tmp1,$tmp2)){ }
                    976: 	} elsif(-e $tmp2){
                    977: 	    unlink $tmp2;
                    978: 	}
                    979: 	$tmp1=$oRN.'.bak';
                    980: 	$tmp2=$nRN.'.bak';
                    981: 	if(-e $tmp1){
                    982: 	    unless(rename($tmp1,$tmp2)){ }
                    983: 	} elsif(-e $tmp2){
                    984: 	    unlink $tmp2;
                    985: 	}
                    986:     } else {
                    987: 	$request->print("<p> ".&mt('No such file').": ".&display($oldfile).'</p></form>');
                    988: 	return 0;
                    989:     }
                    990:     return 1;
1.12      foxr      991: }
1.55      albertel  992: 
1.12      foxr      993: =pod
                    994: 
                    995: =item Delete2($request, $user, $filename)
                    996: 
                    997:   Performs phase two of a delete.  The user has confirmed that they want 
                    998: to delete the selected file.   The file is deleted and the results of the
                    999: delete attempt are indicated.
                   1000: 
                   1001: Parameters:
                   1002: 
                   1003: =over 4
                   1004: 
                   1005: =item $request - Apache Request object [in] the request object for the current
                   1006:                  delete operation.
                   1007: 
                   1008: =item $user    - string [in]  The name of the user initiating the delete
                   1009:                  request.
                   1010: 
1.17      harris41 1011: =item $filename - string [in] The name of the file, relative to construction
                   1012:                   space, to delete.
1.12      foxr     1013: 
                   1014: =back
                   1015: 
                   1016: Returns:
                   1017:   1 - success.
                   1018:   0 - Failure.
                   1019: 
                   1020: =cut
                   1021: 
                   1022: sub Delete2 {
1.55      albertel 1023:     my ($request, $user, $filename) = @_;
1.70      raeburn  1024:     if (-d $filename) { 
                   1025: 	unless (&empty_directory($filename,'Delete2')) { 
1.83      albertel 1026: 	    $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>'); 
1.55      albertel 1027: 	    return 0;
                   1028: 	} else {   
                   1029: 	    if(-e $filename) {
                   1030: 		unless(rmdir($filename)) {
1.83      albertel 1031: 		    $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1032: 		    return 0;
                   1033: 		}
                   1034: 	    } else {
                   1035: 		$request->print('<p> '.&mt('No such file').'. </p></form>');
                   1036: 		return 0;
                   1037: 	    }
                   1038: 	}
1.48      taceyjo1 1039:     } else {
1.55      albertel 1040: 	if(-e $filename) {
                   1041: 	    unless(unlink($filename)) {
1.83      albertel 1042: 		$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1043: 		return 0;
                   1044: 	    }
                   1045: 	} else {
                   1046: 	    $request->print('<p> '.&mt('No such file').'. </p></form>');
                   1047: 	    return 0;
1.46      taceyjo1 1048: 	}
1.55      albertel 1049:     }
                   1050:     return 1;
1.12      foxr     1051: }
                   1052: 
                   1053: =pod
                   1054: 
                   1055: =item Copy2($request, $username, $dir, $oldfile, $newfile)
                   1056: 
                   1057:    Performs phase 2 of a copy.  The file is copied and the status 
                   1058:    of that copy is reported back to the user.
                   1059: 
                   1060: =over 4
                   1061: 
                   1062: =item $request - Apache request object [in]; the apache request currently
                   1063:                  being executed.
                   1064: 
                   1065: =item $username - string [in] Name of the user who is requesting the copy.
                   1066: 
                   1067: =item $dir - string [in] Directory path relative to the construction space
                   1068:              of the destination file.
                   1069: 
                   1070: =item $oldfile - string [in] Name of the source file.
                   1071: 
                   1072: =item $newfile - string [in] Name of the destination file.
                   1073: 
                   1074: 
                   1075: =back
                   1076: 
1.71      raeburn  1077: Returns 0 failure, and 1 successs.
1.12      foxr     1078: 
                   1079: =cut
1.1       www      1080: 
1.12      foxr     1081: sub Copy2 {
                   1082:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
                   1083:     &Debug($request ,"Will try to copy $oldfile to $newfile");
                   1084:     if(-e $oldfile) {
1.71      raeburn  1085:         if ($oldfile eq $newfile) {
1.83      albertel 1086:             $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  1087:             return 1;
                   1088:         }
1.12      foxr     1089: 	unless (copy($oldfile, $newfile)) {
1.83      albertel 1090: 	    $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1.12      foxr     1091: 	    return 0;
1.61      albertel 1092: 	} elsif (!chmod(0660, $newfile)) {
1.83      albertel 1093: 	    $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1.61      albertel 1094: 	    return 0;
                   1095: 	} elsif (-e $oldfile.'.meta' && 
                   1096: 		 !copy($oldfile.'.meta', $newfile.'.meta') &&
                   1097: 		 !chmod(0660, $newfile.'.meta')) {
1.83      albertel 1098: 	    $request->print('<span class="LC_error">'.&mt('copy metadata error').
                   1099: 			    ': '.$!.'</span>');
1.61      albertel 1100: 	    return 0;
1.12      foxr     1101: 	} else {
                   1102: 	    return 1;
                   1103: 	}
1.11      albertel 1104:     } else {
1.47      sakharuk 1105: 	$request->print('<p> '.&mt('No such file').' </p>');
1.12      foxr     1106: 	return 0;
1.11      albertel 1107:     }
1.12      foxr     1108:     return 1;
                   1109: }
1.55      albertel 1110: 
1.12      foxr     1111: =pod
                   1112: 
                   1113: =item NewDir2($request, $user, $newdirectory)
1.1       www      1114: 
1.12      foxr     1115: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
                   1116: 	reporting the results of that creation to the user.
                   1117: 	
                   1118: Parameters:
                   1119: =over 4
                   1120: 
                   1121: =item $request  - Apache request object [in].  Object representing the current HTTP request.
                   1122: 
                   1123: =item $user - string [in] The name of the user that is initiating the request.
                   1124: 
                   1125: =item $newdirectory - string [in] The full path of the directory being created.
                   1126: 
                   1127: =back
                   1128: 
                   1129: Returns 0 - failure 1 - success.
                   1130: 
                   1131: =cut
1.8       albertel 1132: 
1.12      foxr     1133: sub NewDir2 {
1.55      albertel 1134:     my ($request, $user, $newdirectory) = @_;
1.12      foxr     1135:   
1.55      albertel 1136:     unless(mkdir($newdirectory, 02770)) {
1.83      albertel 1137: 	$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1138: 	return 0;
                   1139:     }
                   1140:     unless(chmod(02770, ($newdirectory))) {
1.83      albertel 1141: 	$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1142: 	return 0;
                   1143:     }
                   1144:     return 1;
1.1       www      1145: }
1.55      albertel 1146: 
1.44      taceyjo1 1147: sub decompress2 {
1.55      albertel 1148:     my ($r, $user, $dir, $file) = @_;
1.88      raeburn  1149:     &Apache::lonnet::appenv({'cgi.file' => $file});
                   1150:     &Apache::lonnet::appenv({'cgi.dir' => $dir});
1.55      albertel 1151:     my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   1152:     $r->print($result);
                   1153:     &Apache::lonnet::delenv('cgi.file');
                   1154:     &Apache::lonnet::delenv('cgi.dir');
                   1155:     return 1;
1.44      taceyjo1 1156: }
1.55      albertel 1157: 
1.12      foxr     1158: =pod
                   1159: 
                   1160: =item phasetwo($r, $fn, $uname, $udom)
                   1161: 
                   1162:    Controls the phase 2 processing of file management
                   1163:    requests for construction space.  In phase one, the user
                   1164:    was asked to confirm the operation.  In phase 2, the operation
                   1165:    is performed and the result is shown.
                   1166: 
                   1167:   The strategy is to break out the processing into specific action processors
                   1168:   named action2 where action is the requested action and the 2 denotes 
                   1169:   phase 2 processing.
                   1170: 
                   1171: Parameters:
                   1172: 
                   1173: =over 4
                   1174: 
                   1175: =item  $r     - Apache Request object [in] The request object for this httpd
                   1176:            transaction.
                   1177: 
                   1178: =item  $fn    - string [in]  A filename indicating the object that is being
                   1179:            manipulated.
                   1180: 
                   1181: =item  $uname - string [in] The name of the user initiating the file management
                   1182:            request.
                   1183: 
                   1184: =item  $udom  - string  [in] The login domain of the user initiating the
                   1185:            file management request.
                   1186: =back
                   1187: 
                   1188: =cut
                   1189: 
1.1       www      1190: sub phasetwo {
                   1191:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr     1192:     
1.9       foxr     1193:     &Debug($r, "loncfile - Entering phase 2 for $fn");
1.12      foxr     1194:     
1.78      banghart 1195:     # Break down the file into its component pieces.
1.12      foxr     1196:     
1.27      albertel 1197:     my $dir;		# Directory path
                   1198:     my $main;		# Filename.
                   1199:     my $suffix;		# Extension.
1.46      taceyjo1 1200:     if ($fn=~m:(.*)/([^/]+):) {
1.27      albertel 1201: 	$dir=$1;		# Directory path
                   1202: 	$main=$2;		# Filename.
1.54      albertel 1203:     }
                   1204:     if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1.68      albertel 1205: 	$suffix=$1; #This is the actually filename extension if it exists
1.66      albertel 1206: 	$main=~s/\.\w+$//; #strip the extension
1.54      albertel 1207:     }
1.79      banghart 1208:     my $dest;                       #
                   1209:     my $dest_dir;                   # On success this is where we'll go.
                   1210:     my $disp_newname;               #
                   1211:     my $dest_newname;               #
1.55      albertel 1212:     &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1.67      albertel 1213:     &Debug($r,"    newfilename = ".$env{'form.newfilename'});
1.9       foxr     1214: 
                   1215:     my $conspace=$fn;
1.12      foxr     1216:     
1.55      albertel 1217:     &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1.12      foxr     1218:     
1.67      albertel 1219:     &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1.12      foxr     1220:     
                   1221:     # Select the appropriate processing sub.
1.67      albertel 1222:     if ($env{'form.action'} eq 'decompress') { 
1.66      albertel 1223: 	$main .= '.'.$suffix;
1.55      albertel 1224: 	if(!&decompress2($r, $uname, $dir, $main)) {
                   1225: 	    return ;
                   1226: 	}
                   1227: 	$dest = $dir."/.";
1.67      albertel 1228:     } elsif ($env{'form.action'} eq 'rename' ||
                   1229: 	     $env{'form.action'} eq 'move') {
                   1230: 	if($env{'form.newfilename'}) {
1.27      albertel 1231: 	    if (!defined($dir)) {
                   1232: 		$fn=~m:^(.*)/:;
1.55      albertel 1233: 		$dir=$1; 
1.27      albertel 1234: 	    }
1.67      albertel 1235: 	    if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.12      foxr     1236: 		return;
                   1237: 	    }
1.78      banghart 1238: 	    $dest = $dir."/";
1.79      banghart 1239: 	    $dest_newname = $env{'form.newfilename'};
                   1240: 	    $env{'form.newfilename'} =~ /.+(\/.+$)/;
                   1241: 	    $disp_newname = $1;
                   1242: 	    $disp_newname =~ s/\///;
1.12      foxr     1243: 	}
1.67      albertel 1244:     } elsif ($env{'form.action'} eq 'delete') { 
                   1245: 	if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1.12      foxr     1246: 	    return ;
                   1247: 	}
                   1248: 	# Once a resource is deleted, we just list the directory that
                   1249: 	# previously held it.
                   1250: 	#
1.20      albertel 1251: 	$dest = $dir."/.";		# Parent dir.
1.67      albertel 1252:     } elsif ($env{'form.action'} eq 'copy') { 
                   1253: 	if($env{'form.newfilename'}) {
                   1254: 	    if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.44      taceyjo1 1255: 		return ;
1.55      albertel 1256: 	    }
1.67      albertel 1257: 	    $dest = $env{'form.newfilename'};
1.55      albertel 1258:      	} else {
1.47      sakharuk 1259: 	    $r->print('<p>'.&mt('No New filename specified').'</p></form>');
1.12      foxr     1260: 	    return;
                   1261: 	}
                   1262: 	
1.67      albertel 1263:     } elsif ($env{'form.action'} eq 'newdir') {
                   1264:         my $newdir= $env{'form.newfilename'};
1.12      foxr     1265: 	if(!&NewDir2($r, $uname, $newdir)) {
                   1266: 	    return;
                   1267: 	}
1.55      albertel 1268: 	$dest = $newdir."/";
1.12      foxr     1269:     }
1.67      albertel 1270:     if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1.55      albertel 1271: 	$r->print('<h3><a href="javascript:self.close()">'.&mt('Done').'</a></h3>');
1.51      raeburn  1272:     } else {
1.79      banghart 1273:         if ($env{'form.action'} eq 'rename') {
                   1274:             $r->print('<h3><a href="'.&url($dest).'">'.&mt('Return to Directory').'</a></h3>');
                   1275:             $r->print('<h3><a href="'.&url($dest_newname).'">'.$disp_newname.'</a></h3>');
                   1276:         } else {
1.89      www      1277: 	    $r->print(&done(&url($dest)));
1.79      banghart 1278: 	}
1.51      raeburn  1279:     }
1.1       www      1280: }
                   1281: 
                   1282: sub handler {
                   1283: 
1.55      albertel 1284:     $r=shift;
1.1       www      1285: 
1.60      www      1286:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename']);
1.9       foxr     1287: 
1.55      albertel 1288:     &Debug($r, "loncfile.pm - handler entered");
1.67      albertel 1289:     &Debug($r, " filename: ".$env{'form.filename'});
                   1290:     &Debug($r, " newfilename: ".$env{'form.newfilename'});
1.36      www      1291: #
                   1292: # Determine the root filename
                   1293: # This could come in as "filename", which actually is a URL, or
                   1294: # as "qualifiedfilename", which is indeed a real filename in filesystem
                   1295: #
1.55      albertel 1296:     my $fn;
1.1       www      1297: 
1.67      albertel 1298:     if ($env{'form.filename'}) {
                   1299: 	&Debug($r, "test: $env{'form.filename'}");
1.77      www      1300: 	$fn=&unescape($env{'form.filename'});
1.55      albertel 1301: 	$fn=&URLToPath($fn);
1.67      albertel 1302:     }  elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {  
1.55      albertel 1303: 	#Just hijack the script only the first time around to inject the
                   1304: 	#correct information for further processing
1.77      www      1305: 	$fn=&unescape($env{'form.decompress'});
1.44      taceyjo1 1306: 	$fn=&URLToPath($fn);
1.67      albertel 1307: 	$env{'form.action'}="decompress";
                   1308:     } elsif ($env{'form.qualifiedfilename'}) {
                   1309: 	$fn=$env{'form.qualifiedfilename'};
1.55      albertel 1310:     } else {
                   1311: 	&Debug($r, "loncfile::handler - no form.filename");
1.67      albertel 1312: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.55      albertel 1313: 		       ' unspecified filename for cfile', $r->filename); 
                   1314: 	return HTTP_NOT_FOUND;
                   1315:     }
1.44      taceyjo1 1316: 
1.55      albertel 1317:     unless ($fn) { 
                   1318: 	&Debug($r, "loncfile::handler - doctored url is empty");
1.67      albertel 1319: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.55      albertel 1320: 		       ' trying to cfile non-existing file', $r->filename); 
                   1321: 	return HTTP_NOT_FOUND;
                   1322:     } 
1.1       www      1323: 
                   1324: # ----------------------------------------------------------- Start page output
1.55      albertel 1325:     my $uname;
                   1326:     my $udom;
                   1327: 
                   1328:     ($uname,$udom)=
                   1329: 	&Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                   1330:     &Debug($r, 
                   1331: 	   "loncfile::handler constructaccess uname = $uname domain = $udom");
                   1332:     unless (($uname) && ($udom)) {
                   1333: 	$r->log_reason($uname.' at '.$udom.
1.67      albertel 1334: 		       ' trying to manipulate file '.$env{'form.filename'}.
1.55      albertel 1335: 		       ' ('.$fn.') - not authorized', 
                   1336: 		       $r->filename); 
                   1337: 	return HTTP_NOT_ACCEPTABLE;
                   1338:     }
                   1339: 
1.1       www      1340: 
1.55      albertel 1341:     &Apache::loncommon::content_type($r,'text/html');
                   1342:     $r->send_http_header;
                   1343: 
1.75      albertel 1344:     my (%loaditem,$js);
                   1345: 
1.67      albertel 1346:     if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
                   1347: 	my $newdirname = $env{'form.newfilename'};
1.75      albertel 1348: 	$js = qq|
                   1349: <script type="text/javascript">
1.51      raeburn  1350: function writeDone() {
                   1351:     window.focus();
1.90      raeburn  1352:     opener.document.info.newdir.value = "$newdirname";
                   1353:     setTimeout("self.close()",10000);
1.51      raeburn  1354: }
                   1355:   </script>
1.75      albertel 1356: |;
1.76      albertel 1357: 	$loaditem{'onload'} = "writeDone()";
1.55      albertel 1358:     }
1.75      albertel 1359:     
                   1360:     $r->print(&Apache::loncommon::start_page('Construction Space File Operation',
                   1361: 					     $js,
                   1362: 					     {'add_entries' => \%loaditem,}));
1.1       www      1363:   
1.55      albertel 1364:     $r->print('<h3>'.&mt('Location').': '.&display($fn).'</h3>');
1.1       www      1365:   
1.67      albertel 1366:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.94    ! bisitz   1367:         $r->print('<p class="LC_warning">'
        !          1368:                  .&mt('Co-Author [_1]:[_2]',$uname,$udom)
        !          1369:                  .'</p>'
        !          1370:         );
1.55      albertel 1371:     }
                   1372: 
                   1373: 
1.67      albertel 1374:     &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
                   1375:     if ($env{'form.action'} eq 'delete') {
1.55      albertel 1376:       	$r->print('<h3>'.&mt('Delete').'</h3>');
1.67      albertel 1377:     } elsif ($env{'form.action'} eq 'rename') {
1.55      albertel 1378: 	$r->print('<h3>'.&mt('Rename').'</h3>');
1.67      albertel 1379:     } elsif ($env{'form.action'} eq 'move') {
1.55      albertel 1380: 	$r->print('<h3>'.&mt('Move').'</h3>');
1.67      albertel 1381:     } elsif ($env{'form.action'} eq 'newdir') {
1.55      albertel 1382: 	$r->print('<h3>'.&mt('New Directory').'</h3>');
1.67      albertel 1383:     } elsif ($env{'form.action'} eq 'decompress') {
1.55      albertel 1384: 	$r->print('<h3>'.&mt('Decompress').'</h3>');
1.67      albertel 1385:     } elsif ($env{'form.action'} eq 'copy') {
1.55      albertel 1386: 	$r->print('<h3>'.&mt('Copy').'</h3>');
1.67      albertel 1387:     } elsif ($env{'form.action'} eq 'newfile' ||
                   1388: 	     $env{'form.action'} eq 'newhtmlfile' ||
                   1389: 	     $env{'form.action'} eq 'newproblemfile' ||
                   1390: 	     $env{'form.action'} eq 'newpagefile' ||
                   1391: 	     $env{'form.action'} eq 'newsequencefile' ||
                   1392: 	     $env{'form.action'} eq 'newrightsfile' ||
                   1393: 	     $env{'form.action'} eq 'newstyfile' ||
1.86      albertel 1394: 	     $env{'form.action'} eq 'newtaskfile' ||
1.67      albertel 1395:              $env{'form.action'} eq 'newlibraryfile' ||
                   1396: 	     $env{'form.action'} eq 'Select Action' ) {
1.55      albertel 1397: 	$r->print('<h3>'.&mt('New Resource').'</h3>');
                   1398:     } else {
1.75      albertel 1399: 	$r->print('<p>'.&mt('Unknown Action').' '.$env{'form.action'}.' </p>'.
                   1400: 		  &Apache::loncommon::end_page());
1.55      albertel 1401: 	return OK;  
                   1402:     }
1.67      albertel 1403:     if ($env{'form.phase'} eq 'two') {
1.55      albertel 1404: 	&Debug($r, "loncfile::handler  entering phase2");
                   1405: 	&phasetwo($r,$fn,$uname,$udom);
                   1406:     } else {
                   1407: 	&Debug($r, "loncfile::handler  entering phase1");
                   1408: 	&phaseone($r,$fn,$uname,$udom);
                   1409:     }
1.1       www      1410: 
1.75      albertel 1411:     $r->print(&Apache::loncommon::end_page());
1.55      albertel 1412:     return OK;  
1.1       www      1413: }
                   1414: 
                   1415: 1;
                   1416: __END__
1.9       foxr     1417: 

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