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

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

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