File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.39: download - view: text, annotated - select for diffs
Mon Aug 4 20:34:19 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
Bug #2018: fix &exists() and use it.

    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.39 2003/08/04 20:34:19 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 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: 	    if ($newfilename =~ m|/[^\.]+$|) {
  391: 		#no extension add on original extension
  392: 		if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
  393: 		    $newfilename.='.'.$1;
  394: 		}
  395: 	    }
  396: 	    $request->print(&checksuffix($fn, $newfilename));
  397: 	    #renaming a dir, delete the trailing /
  398:             #remove second to last element for current dir
  399: 	    if (-d $fn) {
  400: 		$newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
  401: 	    }
  402: 	    my $return=&exists($user, $domain, $newfilename);
  403: 	    $request->print($return);
  404: 	    if ($return =~/^Error:/) {
  405: 		$request->print('<br /><a href="'.&url($fn).'">Cancel</a>');
  406: 		return;
  407: 	    }
  408: 	    $request->print('<input type="hidden" name="newfilename" value="'.
  409: 			    $newfilename.
  410: 			    '" /><p>Rename '.&display($fn).
  411: 			    '</tt><br />to '.&display($newfilename).'?</p>');
  412: 	    &CloseForm1($request, $fn);
  413: 	} else {
  414: 	    $request->print('<p>No new filename specified.</p></form>');
  415: 	    return;
  416: 	}
  417:     } else {
  418: 	$request->print('<p> No such file: '.&display($fn).'</p></form>');
  419: 	return;
  420:     }
  421:     
  422: }
  423: =pod
  424: 
  425: =item Delete1
  426: 
  427:    Performs phase 1 processing of the delete operation.  In phase one
  428:   we just check to be sure the file exists.
  429: 
  430: Parameters:
  431: 
  432: =over 4
  433: 
  434: =item   $request   - Apache Request Object [in] request object for the current 
  435:                 request.
  436: 
  437: =item   $user      - string [in]  Name of the user initiating the request.
  438: 
  439: =item   $domain    - string [in]  Domain the initiating user is logged in as
  440: 
  441: =item   $filename  - string [in]  Source filename.
  442: 
  443: =back
  444: 
  445: =cut
  446: 
  447: sub Delete1 {
  448:   my ($request, $user, $domain, $fn) = @_;
  449: 
  450:   if( -e $fn) {
  451:     $request->print('<input type="hidden" name="newfilename" value="'.
  452: 		    $fn.'"/>');
  453:     $request->print('<p>Delete '.&display($fn).'?</p>');
  454:     &CloseForm1($request, $fn);
  455:   } else {
  456:     $request->print('<p>No such file: '.&display($fn).'</p></form>');
  457:   }
  458: }
  459: 
  460: =pod
  461: 
  462: =item Copy1($request, $user, $domain, $filename, $newfilename)
  463: 
  464:    Performs phase 1 processing of the construction space copy command.
  465:    Ensure that the source file exists.  Ensure that a destination exists,
  466:    also warn if the destination already exists.
  467: 
  468: Parameters:
  469: 
  470: =over 4
  471: 
  472: =item   $request   - Apache Request Object [in] request object for the current 
  473:                 request.
  474: 
  475: =item   $user      - string [in]  Name of the user initiating the request.
  476: 
  477: =item   $domain    - string [in]  Domain the initiating user is logged in as
  478: 
  479: =item   $fn  - string [in]  Source filename.
  480: 
  481: =item   $newfilename-string [in]  Destination filename.
  482: 
  483: =back
  484: 
  485: =cut
  486: 
  487: sub Copy1 {
  488:   my ($request, $user, $domain, $fn, $newfilename) = @_;
  489: 
  490:   if(-e $fn) {
  491:     $request->print(&checksuffix($fn,$newfilename));
  492:     my $return=&exists($user, $domain, $newfilename);
  493:     $request->print($return);
  494:     if ($return =~/^Error:/) {
  495: 	$request->print('<br /><a href="'.&url($fn).'">Cancel</a>');
  496: 	return;
  497:     }
  498:     $request->print('<input type = "hidden" name = "newfilename" value = "'.
  499: 		    $newfilename.
  500: 		    '" /><p>Copy '.&display($fn).'<br />to '.
  501: 		    &display($newfilename).'?</p>');
  502:     &CloseForm1($request, $fn);
  503:   } else {
  504:     $request->print('<p>No such file: '.&display($fn).'</p></form>');
  505:   }
  506: }
  507: 
  508: =pod
  509: 
  510: =item NewDir1
  511:  
  512:   Does all phase 1 processing of directory creation:
  513:   Ensures that the user provides a new directory name,
  514:   and that the directory does not already exist.
  515: 
  516: Parameters:
  517: 
  518: =over 4
  519: 
  520: =item   $request  - Apache Request Object [in] - Server request object for the
  521:                current url.
  522: 
  523: =item   $username - Name of the user that is requesting the directory creation.
  524: 
  525: =item $domain - Domain user is in
  526: 
  527: =item   $fn     - source file.
  528: 
  529: =item   $newdir   - Name of the directory to be created; path relative to the 
  530:                top level of construction space.
  531: =back
  532: 
  533: Side Effects:
  534: 
  535: =over 4
  536: 
  537: =item A new form is displayed.  Clicking on the confirmation button
  538: causes the newdir operation to transition into phase 2.  The hidden field
  539: "newfilename" is set with the construction space path to the new directory.
  540: 
  541: 
  542: =back
  543: 
  544: =cut
  545: 
  546: 
  547: sub NewDir1
  548: {
  549:   my ($request, $username, $domain, $fn, $newfilename) = @_;
  550: 
  551:   my $result=&exists($username,$domain,$newfilename);
  552:   if ($result) {
  553:     $request->print('<font color="red">'.$result.'</font></form>');
  554:   } else {
  555:     $request->print('<input type="hidden" name="newfilename" value="'.
  556: 		    $newfilename.'" /><p>Make new directory '.
  557: 		    &display($newfilename).'?</p>');
  558:     &CloseForm1($request, $fn);
  559:   }
  560: }
  561: 
  562: =pod
  563: 
  564: =item NewFile1
  565:  
  566:   Does all phase 1 processing of file creation:
  567:   Ensures that the user provides a new filename, adds proper extension
  568:   if needed and that the file does not already exist, if it is a html,
  569:   problem, page, or sequence, it then creates a form link to hand the
  570:   actual creation off to the proper handler.
  571: 
  572: Parameters:
  573: 
  574: =over 4
  575: 
  576: =item   $request  - Apache Request Object [in] - Server request object for the
  577:                current url.
  578: 
  579: =item   $username - Name of the user that is requesting the directory creation.
  580: 
  581: =item   $domain   - Name of the domain of the user
  582: 
  583: =item   $fn      - Source file name
  584: 
  585: =item   $newfilename
  586:                   - Name of the file to be created; no path information
  587: =back
  588: 
  589: Side Effects:
  590: 
  591: =over 4
  592: 
  593: =item 2 new forms are displayed.  Clicking on the confirmation button
  594: causes the browser to attempt to load the specfied URL, allowing the
  595: proper handler to take care of file creation. There is also a Cancel
  596: button which returns you to the driectory listing you came from
  597: 
  598: =back
  599: 
  600: =cut
  601: 
  602: 
  603: sub NewFile1 {
  604:     my ($request, $user, $domain, $fn, $newfilename) = @_;
  605: 
  606:     if ($ENV{'form.action'} =~ /new(.+)file/) {
  607: 	my $extension=$1;
  608: 	if ($newfilename !~ /\Q.$extension\E$/) {
  609: 	    if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) {
  610: 		#already has an extension strip it and add in expected one
  611: 		$newfilename =~ s|.([^\.]+)$||;
  612: 	    }
  613: 	    $newfilename.=".$extension";
  614: 	}
  615:     }
  616:     my $result=&exists($user,$domain,$newfilename);
  617:     if($result) {
  618: 	$request->print('<font color="red">'.$result.'</font></form>');
  619:     } else {
  620: 	$request->print('<p>Make new file '.&display($newfilename).'?</p>');
  621: 	$request->print('</form>');
  622: 	$request->print('<form action="'.&url($newfilename).
  623: 			'" method="POST"><p><input type="submit" value="Continue" /></p></form>');
  624: 	$request->print('<form action="'.&url($fn).
  625: 			'" method="POST"><p><input type="submit" value="Cancel" /></p></form>');
  626:     }
  627: }
  628: 
  629: =pod
  630: 
  631: =item phaseone($r, $fn, $uname, $udom)
  632: 
  633:   Peforms phase one processing of the request.  In phase one, error messages
  634: are returned if the request cannot be performed (e.g. attempts to manipulate
  635: files that are nonexistent).  If the operation can be performed, what is
  636: about to be done will be presented to the user for confirmation.  If the
  637: user confirms the request, then phase two is executed, the action 
  638: performed and reported to the user.
  639: 
  640:  Parameters:
  641: 
  642: =over 4
  643: 
  644: =item $r  - request object [in] - The Apache request being executed.
  645: 
  646: =item $fn = string [in] - The filename being manipulated by the 
  647:                              request.
  648: 
  649: =item $uname - string [in] Name of user logged in and doing this action.
  650: 
  651: =item $udom  - string [in] Domain name under which the user logged in. 
  652: 
  653: =back
  654: 
  655: =cut
  656: 
  657: sub phaseone {
  658:   my ($r,$fn,$uname,$udom)=@_;
  659:   
  660:   my $newfilename=&cleanDest($r,$ENV{'form.newfilename'});
  661:   $newfilename=&relativeDest($fn,$newfilename,$uname);
  662: 
  663:   $r->print('<form action="/adm/cfile" method="post">'.
  664:       '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
  665:       '<input type="hidden" name="phase" value="two" />'.
  666:       '<input type="hidden" name="action" value="'.$ENV{'form.action'}.'" />');
  667:   
  668:   if ($ENV{'form.action'} eq 'rename') {
  669:       &Rename1($r, $uname, $udom, $fn, $newfilename);
  670:   } elsif ($ENV{'form.action'} eq 'delete') { 
  671:       &Delete1($r, $uname, $udom, $fn);
  672:   } elsif ($ENV{'form.action'} eq 'copy') { 
  673:       if($newfilename) {
  674: 	  &Copy1($r, $uname, $udom, $fn, $newfilename);
  675:       } else {
  676: 	  $r->print('<p>No new filename specified.</p></form>');
  677:       }
  678:   } elsif ($ENV{'form.action'} eq 'newdir') {
  679:       &NewDir1($r, $uname, $udom, $fn, $newfilename);
  680:   }  elsif ($ENV{'form.action'} eq 'newfile' ||
  681: 	    $ENV{'form.action'} eq 'newhtmlfile' ||
  682: 	    $ENV{'form.action'} eq 'newproblemfile' ||
  683:             $ENV{'form.action'} eq 'newpagefile' ||
  684:             $ENV{'form.action'} eq 'newsequencefile' ||
  685:             $ENV{'form.action'} eq 'newrightsfile' ||
  686:             $ENV{'form.action'} eq 'newstyfile' ||
  687:             $ENV{'form.action'} eq 'Select Action') {
  688:       if ($newfilename) {
  689: 	  &NewFile1($r, $uname, $udom, $fn, $newfilename);
  690:       } else {
  691: 	  $r->print('<p>No new filename specified.</p></form>');
  692:       }
  693:   }
  694: }
  695: 
  696: =pod
  697: 
  698: =item Rename2($request, $user, $directory, $oldfile, $newfile)
  699: 
  700: Performs phase 2 processing of a rename reequest.   This is where the
  701: actual rename is performed.
  702: 
  703: Parameters
  704: 
  705: =over 4
  706: 
  707: =item $request - Apache request object [in] The request being processed.
  708: 
  709: =item $user  - string [in] The name of the user initiating the request.
  710: 
  711: =item $directory - string [in] The name of the directory relative to the
  712:                  construction space top level of the renamed file.
  713: 
  714: =item $oldfile - Name of the file.
  715: 
  716: =item $newfile - Name of the new file.
  717: 
  718: =back
  719: 
  720: Returns:
  721: 
  722: =over 4
  723: 
  724: =item 1 Success.
  725: 
  726: =item 0 Failure.
  727: 
  728: =cut
  729: 
  730: sub Rename2 {
  731: 
  732:   my ($request, $user, $directory, $oldfile, $newfile) = @_;
  733: 
  734:   &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
  735: 	 " new file ".$newfile."\n");
  736:   &Debug($request, "Target is: ".$directory.'/'.
  737: 	 $newfile);
  738:   if (-e $oldfile) {
  739:       unless (rename($oldfile,$newfile)) {
  740: 	  $request->print('<font color="red">Error: '.$!.'</font>');
  741: 	  return 0;
  742:       }
  743:   } else {
  744:       $request->print("<p> No such file: ".&display($oldfile).'</p></form>');
  745:       return 0;
  746:   }
  747:   return 1;
  748: }
  749: =pod
  750: 
  751: =item Delete2($request, $user, $filename)
  752: 
  753:   Performs phase two of a delete.  The user has confirmed that they want 
  754: to delete the selected file.   The file is deleted and the results of the
  755: delete attempt are indicated.
  756: 
  757: Parameters:
  758: 
  759: =over 4
  760: 
  761: =item $request - Apache Request object [in] the request object for the current
  762:                  delete operation.
  763: 
  764: =item $user    - string [in]  The name of the user initiating the delete
  765:                  request.
  766: 
  767: =item $filename - string [in] The name of the file, relative to construction
  768:                   space, to delete.
  769: 
  770: =back
  771: 
  772: Returns:
  773:   1 - success.
  774:   0 - Failure.
  775: 
  776: =cut
  777: 
  778: sub Delete2 {
  779:   my ($request, $user, $filename) = @_;
  780: 
  781:   if(-e $filename) {
  782:     unless(unlink($filename)) {
  783:       $request->print('<font color="red">Error: '.$!.'</font>');
  784:       return 0;
  785:     }
  786:   } else {
  787:     $request->print('<p> No such file. </p></form');
  788:     return 0;
  789:   }
  790:   return 1;
  791: }
  792: 
  793: =pod
  794: 
  795: =item Copy2($request, $username, $dir, $oldfile, $newfile)
  796: 
  797:    Performs phase 2 of a copy.  The file is copied and the status 
  798:    of that copy is reported back to the user.
  799: 
  800: =over 4
  801: 
  802: =item $request - Apache request object [in]; the apache request currently
  803:                  being executed.
  804: 
  805: =item $username - string [in] Name of the user who is requesting the copy.
  806: 
  807: =item $dir - string [in] Directory path relative to the construction space
  808:              of the destination file.
  809: 
  810: =item $oldfile - string [in] Name of the source file.
  811: 
  812: =item $newfile - string [in] Name of the destination file.
  813: 
  814: 
  815: =back
  816: 
  817: Returns 0 failure, and 0 successs.
  818: 
  819: =cut
  820: 
  821: sub Copy2 {
  822:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
  823:     &Debug($request ,"Will try to copy $oldfile to $newfile");
  824:     if(-e $oldfile) {
  825: 	unless (copy($oldfile, $newfile)) {
  826: 	    $request->print('<font color="red"> copy Error: '.$!.'</font>');
  827: 	    return 0;
  828: 	} else {
  829: 	    unless (chmod(0660, $newfile)) {
  830: 		$request->print('<font color="red"> chmod error: '.$!.'</font>');
  831: 		return 0;
  832: 	    }
  833: 	    return 1;
  834: 	}
  835:     } else {
  836: 	$request->print('<p> No such file </p>');
  837: 	return 0;
  838:     }
  839:     return 1;
  840: }
  841: =pod
  842: 
  843: =item NewDir2($request, $user, $newdirectory)
  844: 
  845: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
  846: 	reporting the results of that creation to the user.
  847: 	
  848: Parameters:
  849: =over 4
  850: 
  851: =item $request  - Apache request object [in].  Object representing the current HTTP request.
  852: 
  853: =item $user - string [in] The name of the user that is initiating the request.
  854: 
  855: =item $newdirectory - string [in] The full path of the directory being created.
  856: 
  857: =back
  858: 
  859: Returns 0 - failure 1 - success.
  860: 
  861: =cut
  862: 
  863: sub NewDir2 {
  864:   my ($request, $user, $newdirectory) = @_;
  865:   
  866:   unless(mkdir($newdirectory, 02770)) {
  867:     $request->print('<font color="red">Error: '.$!.'</font>');
  868:     return 0;
  869:   }
  870:   unless(chmod(02770, ($newdirectory))) {
  871:       $request->print('<font color="red"> Error: '.$!.'</font>');
  872:       return 0;
  873:   }
  874:   return 1;
  875: }
  876: 
  877: =pod
  878: 
  879: =item phasetwo($r, $fn, $uname, $udom)
  880: 
  881:    Controls the phase 2 processing of file management
  882:    requests for construction space.  In phase one, the user
  883:    was asked to confirm the operation.  In phase 2, the operation
  884:    is performed and the result is shown.
  885: 
  886:   The strategy is to break out the processing into specific action processors
  887:   named action2 where action is the requested action and the 2 denotes 
  888:   phase 2 processing.
  889: 
  890: Parameters:
  891: 
  892: =over 4
  893: 
  894: =item  $r     - Apache Request object [in] The request object for this httpd
  895:            transaction.
  896: 
  897: =item  $fn    - string [in]  A filename indicating the object that is being
  898:            manipulated.
  899: 
  900: =item  $uname - string [in] The name of the user initiating the file management
  901:            request.
  902: 
  903: =item  $udom  - string  [in] The login domain of the user initiating the
  904:            file management request.
  905: =back
  906: 
  907: =cut
  908: 
  909: sub phasetwo {
  910:     my ($r,$fn,$uname,$udom)=@_;
  911:     
  912:     &Debug($r, "loncfile - Entering phase 2 for $fn");
  913:     
  914:     # Break down the file into it's component pieces.
  915:     
  916:     my $dir;		# Directory path
  917:     my $main;		# Filename.
  918:     my $suffix;		# Extension.
  919: 
  920:     if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) {
  921: 	$dir=$1;		# Directory path
  922: 	$main=$2;		# Filename.
  923: 	$suffix=$3;		# Extension.
  924:     }
  925:         
  926:     my $dest;                   # On success this is where we'll go.
  927:     
  928:     &Debug($r, 
  929: 	   "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
  930:     &Debug($r,
  931: 	   "    newfilename = ".$ENV{'form.newfilename'});
  932: 
  933:     my $conspace=$fn;
  934:     
  935:     &Debug($r, 
  936: 	   "loncfile::phase2 Full construction space name: $conspace");
  937:     
  938:     &Debug($r, 
  939: 	   "loncfie::phase2 action is $ENV{'form.action'}");
  940:     
  941:     # Select the appropriate processing sub.
  942:     
  943:     if ($ENV{'form.action'} eq 'rename') { # Rename.
  944: 	if($ENV{'form.newfilename'}) {
  945: 	    if (!defined($dir)) {
  946: 		$fn=~m:^(.*)/:;
  947: 		$dir=$1;
  948: 	    }
  949: 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
  950: 		return;
  951: 	    }
  952: 	    $dest = &url($ENV{'form.newfilename'});
  953: 	}
  954:     } elsif ($ENV{'form.action'} eq 'delete') { 
  955: 	if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
  956: 	    return ;
  957: 	}
  958: 	# Once a resource is deleted, we just list the directory that
  959: 	# previously held it.
  960: 	#
  961: 	$dest = $dir."/.";		# Parent dir.
  962:     } elsif ($ENV{'form.action'} eq 'copy') { 
  963: 	if($ENV{'form.newfilename'}) {
  964: 	    if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
  965: 		return
  966: 		}
  967: 	    $dest = $ENV{'form.newfilename'};
  968:      
  969: 	} else {
  970: 	    $r->print('<p>No New filename specified</p></form>');
  971: 	    return;
  972: 	}
  973: 	
  974:     } elsif ($ENV{'form.action'} eq 'newdir') {
  975:         my $newdir= $ENV{'form.newfilename'};
  976: 	if(!&NewDir2($r, $uname, $newdir)) {
  977: 	    return;
  978: 	}
  979: 	$dest = $newdir."/"
  980:     }
  981:     $r->print('<h3><a href="'.&url($dest).'">Done</a></h3>');
  982: }
  983: 
  984: sub handler {
  985: 
  986:   $r=shift;
  987: 
  988: 
  989:   &Debug($r, "loncfile.pm - handler entered");
  990:   &Debug($r, " filename: ".$ENV{'form.filename'});
  991:   &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
  992: #
  993: # Determine the root filename
  994: # This could come in as "filename", which actually is a URL, or
  995: # as "qualifiedfilename", which is indeed a real filename in filesystem
  996: #
  997:   my $fn;
  998: 
  999:   if ($ENV{'form.filename'}) {
 1000:       $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
 1001:       $fn=&URLToPath($fn);
 1002:   } elsif ($ENV{'form.qualifiedfilename'}) {
 1003:       $fn=$ENV{'form.qualifiedfilename'};
 1004:   } else {
 1005:       &Debug($r, "loncfile::handler - no form.filename");
 1006:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1007:          ' unspecified filename for cfile', $r->filename); 
 1008:      return HTTP_NOT_FOUND;
 1009:   }
 1010: 
 1011:   unless ($fn) { 
 1012:       &Debug($r, "loncfile::handler - doctored url is empty");
 1013:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1014:          ' trying to cfile non-existing file', $r->filename); 
 1015:      return HTTP_NOT_FOUND;
 1016:   } 
 1017: 
 1018: # ----------------------------------------------------------- Start page output
 1019:   my $uname;
 1020:   my $udom;
 1021: 
 1022:   ($uname,$udom)=
 1023:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
 1024:   &Debug($r, 
 1025: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
 1026:   unless (($uname) && ($udom)) {
 1027:      $r->log_reason($uname.' at '.$udom.
 1028:          ' trying to manipulate file '.$ENV{'form.filename'}.
 1029:          ' ('.$fn.') - not authorized', 
 1030:          $r->filename); 
 1031:      return HTTP_NOT_ACCEPTABLE;
 1032:   }
 1033: 
 1034: 
 1035:   $r->content_type('text/html');
 1036:   $r->send_http_header;
 1037: 
 1038:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
 1039: 
 1040:   $r->print(&Apache::loncommon::bodytag('Construction Space File Operation'));
 1041: 
 1042:   
 1043:   $r->print('<h3>Location: '.&display($fn).'</h3>');
 1044:   
 1045:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
 1046:           $r->print('<h3><font color="red">Co-Author: '.$uname.' at '.$udom.
 1047:                '</font></h3>');
 1048:   }
 1049: 
 1050: 
 1051:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
 1052:   if ($ENV{'form.action'} eq 'delete') {
 1053:       
 1054:       $r->print('<h3>Delete</h3>');
 1055:   } elsif ($ENV{'form.action'} eq 'rename') {
 1056:       $r->print('<h3>Rename</h3>');
 1057:   } elsif ($ENV{'form.action'} eq 'newdir') {
 1058:       $r->print('<h3>New Directory</h3>');
 1059:   } elsif ($ENV{'form.action'} eq 'copy') {
 1060:       $r->print('<h3>Copy</h3>');
 1061:   } elsif ($ENV{'form.action'} eq 'newfile' ||
 1062: 	   $ENV{'form.action'} eq 'newhtmlfile' ||
 1063: 	   $ENV{'form.action'} eq 'newproblemfile' ||
 1064:            $ENV{'form.action'} eq 'newpagefile' ||
 1065:            $ENV{'form.action'} eq 'newsequencefile' ||
 1066: 	   $ENV{'form.action'} eq 'newrightsfile' ||
 1067: 	   $ENV{'form.action'} eq 'newstyfile' ||
 1068:            $ENV{'form.action'} eq 'Select Action' ) {
 1069:       $r->print('<h3>New Resource</h3>');
 1070:   } else {
 1071:      $r->print('<p>Unknown Action '.$ENV{'form.action'}.' </p></body></html>');
 1072:      return OK;  
 1073:   }
 1074:   if ($ENV{'form.phase'} eq 'two') {
 1075:       &Debug($r, "loncfile::handler  entering phase2");
 1076:       &phasetwo($r,$fn,$uname,$udom);
 1077:   } else {
 1078:       &Debug($r, "loncfile::handler  entering phase1");
 1079:       &phaseone($r,$fn,$uname,$udom);
 1080:   }
 1081: 
 1082:   $r->print('</body></html>');
 1083:   return OK;  
 1084: }
 1085: 
 1086: 1;
 1087: __END__
 1088: 

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