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

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

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