File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.44: download - view: text, annotated - select for diffs
Wed Nov 19 14:57:32 2003 UTC (20 years, 6 months ago) by taceyjo1
Branches: MAIN
CVS tags: HEAD
loncfile includes the new decompression system integrated, a lot better than the old way lonconstruct and lonpubdir just support

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

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