File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.37: download - view: text, annotated - select for diffs
Mon Aug 4 17:45:06 2003 UTC (21 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Working on bug #2018: could not handle absolute vs. relative URLs

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

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