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

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

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