File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.45: download - view: text, annotated - select for diffs
Wed Nov 19 15:06:33 2003 UTC (20 years, 6 months ago) by taceyjo1
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
Whoops, forgot to remove some debugging information, it pollutes the log less now

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

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