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

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

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