File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.86: download - view: text, annotated - select for diffs
Wed Jul 25 19:56:57 2007 UTC (16 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD
- add task to creation menu

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

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