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

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

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