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

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

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