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

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

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