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

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

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