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

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

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