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

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

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