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

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

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