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

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