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

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

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