File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.32: download - view: text, annotated - select for diffs
Thu Jun 19 21:04:37 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- better solution to BUG#1242, cleans onall actions now

    1: # The LearningOnline Network with CAPA
    2: # Handler to rename files, etc, in construction space
    3: #
    4: #  This file responds to the various buttons and events
    5: #  in the top frame of the construction space directory.
    6: #  Each event is processed in two phases.  The first phase
    7: #  presents a page that describes the proposed action to the user
    8: #  and requests confirmation.  The second phase commits the action
    9: #  and displays a page showing the results of the action.
   10: #
   11: #
   12: # $Id: loncfile.pm,v 1.32 2003/06/19 21:04:37 albertel Exp $
   13: #
   14: # Copyright Michigan State University Board of Trustees
   15: #
   16: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   17: #
   18: # LON-CAPA is free software; you can redistribute it and/or modify
   19: # it under the terms of the GNU General Public License as published by
   20: # the Free Software Foundation; either version 2 of the License, or
   21: # (at your option) any later version.
   22: #
   23: # LON-CAPA is distributed in the hope that it will be useful,
   24: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   25: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   26: # GNU General Public License for more details.
   27: #
   28: # You should have received a copy of the GNU General Public License
   29: # along with LON-CAPA; if not, write to the Free Software
   30: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   31: #
   32: # /home/httpd/html/adm/gpl.txt
   33: #
   34: # http://www.lon-capa.org/
   35: #
   36: #
   37: # (Handler to retrieve an old version of a file
   38: #
   39: # (Publication Handler
   40: # 
   41: # (TeX Content Handler
   42: #
   43: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   44: #
   45: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   46: # 03/23 Guy Albertelli
   47: # 03/24,03/29 Gerd Kortemeyer)
   48: #
   49: # 03/31,04/03,05/02,05/09,06/23,06/24 Gerd Kortemeyer)
   50: #
   51: # 06/23 Gerd Kortemeyer
   52: # 05/07/02 Ron Fox:
   53: #           - Added Debug log output so that I can trace what the heck this
   54: #             undocumented thingy does.
   55: # 05/28/02  Ron Fox:
   56: #           - Started putting in pod in standard format.
   57: =pod
   58: 
   59: =head1 NAME
   60: 
   61: Apache::loncfile - Construction space file management.
   62: 
   63: =head1 SYNOPSIS
   64:  
   65:  Content handler for buttons on the top frame of the construction space 
   66: directory.
   67: 
   68: =head1 INTRODUCTION
   69: 
   70:   loncfile is invoked when buttons in the top frame of the construction 
   71: space directory listing are clicked.   All operations proceed in two phases.
   72: The first phase describes to the user exactly what will be done.  If the user
   73: confirms the operation, the second phase commits the operation and indicates
   74: completion.  When the user dismisses the output of phase2, they are returned to
   75: an "appropriate" directory listing in general.
   76: 
   77:     This is part of the LearningOnline Network with CAPA project
   78: described at http://www.lon-capa.org.
   79: 
   80: =head2 Subroutines
   81: 
   82: =cut
   83: 
   84: package Apache::loncfile;
   85: 
   86: use strict;
   87: use Apache::File;
   88: use File::Basename;
   89: use File::Copy;
   90: use HTML::Entities();
   91: use Apache::Constants qw(:common :http :methods);
   92: use Apache::loncacc;
   93: use Apache::Log ();
   94: use Apache::lonnet;
   95: 
   96: my $DEBUG=0;
   97: my $r;				# Needs to be global for some stuff RF.
   98: 
   99: =pod
  100: 
  101: =item Debug($request, $message)
  102: 
  103:   If debugging is enabled puts out a debugging message determined by the
  104:   caller.  The debug message goes to the Apache error log file. Debugging
  105:   is enabled by setting the module global DEBUG variable to nonzero (TRUE).
  106: 
  107:  Parameters:
  108: 
  109: =over 4
  110:  
  111: =item $request - The current request operation.
  112: 
  113: =item $message - The message to put in the log file.
  114: 
  115: =back
  116:   
  117:  Returns:
  118:    nothing.
  119: 
  120: =cut
  121: 
  122: sub Debug {
  123:   
  124:   # Marshall the parameters.
  125:   
  126:   my $r       = shift;
  127:   my $log     = $r->log;
  128:   my $message = shift;
  129:   
  130:   # Put out the indicated message butonly if DEBUG is true.
  131:   
  132:   if ($DEBUG) {
  133:     $log->debug($message);
  134:   }
  135: }
  136: 
  137: =pod
  138: 
  139: =item URLToPath($url)
  140: 
  141:   Convert a URL to a file system path.
  142:   
  143:   In order to manipulate the construction space objects, it is necessary
  144:   to access url identified objects a filespace objects.  This function
  145:   translates a construction space URL to a file system path.
  146:  Parameters:
  147: 
  148: =over 4
  149: 
  150: =item  Url    - string [in] The url to convert.
  151:   
  152: =back
  153:   
  154:  Returns:
  155: 
  156: =over 4
  157: 
  158: =item  The corresponding file system path. 
  159: 
  160: =back
  161: 
  162: Global References
  163: 
  164: =over 4
  165: 
  166: =item  $r      - Request object [in] Referenced in the &Debug calls.
  167: 
  168: =back
  169: 
  170: =cut
  171: 
  172: sub URLToPath {
  173:   my $Url = shift;
  174:   &Debug($r, "UrlToPath got: $Url");
  175:   $Url=~ s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
  176:   $Url=~ s/^http\:\/\/[^\/]+//;
  177:   &Debug($r, "Returning $Url \n");
  178:   return $Url;
  179: }
  180: 
  181: =pod
  182: 
  183: =item PublicationPath($domain, $user, $dir, $file)
  184: 
  185:    Determines the filesystem path corresponding to a published resource
  186:    specification.  The returned value is the path.
  187: Parameters:
  188: 
  189: =over 4
  190: 
  191: =item   $domain - string [in] Name of the domain within which the resource is 
  192:              stored.
  193: 
  194: =item   $user   - string [in] Name of the user asking about the resource.
  195: 
  196: =item   $dir    - Directory path relative to the top of the resource space.
  197: 
  198: =item   $file   - name of the resource file itself without path info.
  199: 
  200: =back
  201: 
  202: =over 4
  203: 
  204: Returns:
  205: 
  206: =item  string - full path to the file if it exists in publication space.
  207: 
  208: =back
  209:      
  210: =cut
  211: 
  212: sub PublicationPath
  213: {
  214:   my ($domain, $user, $dir, $file)=@_;
  215: 
  216:   return '/home/httpd/html/res/'.$domain.'/'.$user.'/'.$dir.'/'.
  217: 	$file;
  218: }
  219: 
  220: =pod
  221: 
  222: =item ConstructionPath($domain, $user, $dir, $file)
  223: 
  224:    Determines the filesystem path corresponding to a construction space
  225:    resource specification.  The returned value is the path
  226: Parameters:
  227: 
  228: =over 4
  229: 
  230: =item   $user   - string [in] Name of the user asking about the resource.
  231: 
  232: =item   $dir    - Directory path relative to the top of the resource space.
  233: 
  234: =item   $file   - name of the resource file itself without path info.
  235: 
  236: Returns:
  237: 
  238: =item  string - full path to the file if it exists in Construction space.
  239: 
  240: =back
  241:      
  242: =cut
  243: 
  244: sub ConstructionPath {
  245:   my ($user, $dir, $file) = @_;
  246: 
  247:   return '/home/'.$user.'/public_html/'.$dir.'/'.$file;
  248: 
  249: }
  250: =pod
  251: 
  252: =item  ConstructionPathFromRelative($user, $relname)
  253: 
  254:    Determines the path to a construction space file given
  255: the username and the path relative to the root of construction space.
  256: 
  257: Parameters:
  258: 
  259: =over 4
  260: 
  261: =item  $user  - string [in] Name of the user in whose construction space the
  262:            file [will] live.
  263: 
  264: =item  $relname - string[in] Path to the file relative to the root of the
  265:             construction space.
  266: 
  267: =back
  268: 
  269: Returns:
  270: 
  271: =over 4   
  272: 
  273: =item  string - Full path to the file.
  274: 
  275: =back
  276: 
  277: =cut
  278: 
  279: sub ConstructionPathFromRelative {
  280: 
  281:   my ($user, $relname) = @_;
  282:   return '/home/'.$user.'/public_html'.$relname;
  283: 
  284: }
  285: 
  286: =pod
  287: 
  288: =item exists($user, $domain, $directory, $file)
  289: 
  290:    Determine if a resource file name has been published or exists
  291:    in the construction space.
  292: 
  293:  Parameters:
  294: 
  295: =over 4
  296: 
  297: =item  $user   - string [in] - Name of the user for which to check.
  298: 
  299: =item  $domain - string [in] - Name of the domain in which the resource
  300:                           might have been published.
  301: 
  302: =item  $dir    - string [in] - Path relative to construction or resource space
  303:                           in which the resource might live.
  304: 
  305: =item  $file   - string [in] - Name of the file.
  306: 
  307: =back
  308: 
  309: Returns:
  310: 
  311: =over 4
  312: 
  313: =item  string - Either where the resource exists as an html string that can
  314:            be embedded in a dialog or an empty string if the resource
  315:            does not exist.
  316:   
  317: =back
  318: 
  319: =cut
  320: 
  321: sub exists {
  322:   my ($user, $domain, $dir, $file) = @_;
  323: 
  324:   # Create complete paths in publication and construction space.
  325:   my $relativedir=$dir;
  326:   $relativedir=s|/home/\Q$user\E/public_html||;
  327:   my $published = &PublicationPath($domain, $user, $relativedir, $file);
  328:   my $construct = &ConstructionPath($user, $relativedir, $file);
  329: 
  330:   # If the resource exists in either space indicate this fact.
  331:   # Note that the check for existence in resource space is stricter.
  332: 
  333:   my $result;    
  334:   if ( -d $construct ) {
  335:       return 'Error: destination for operation is a directory.';
  336:   }
  337:   if ( -e $published) {
  338:       $result.='<p><font color="red">Warning: target file exists, and has been published!</font></p>';
  339:   }
  340:   elsif ( -e $construct) {
  341:       $result.='<p><font color="red">Warning: target file exists!</font></p>';
  342:   }
  343: 
  344:   return $result;
  345: 
  346: }
  347: 
  348: =pod
  349: 
  350: =item checksuffix($old, $new)
  351:         
  352:   Determine if a resource filename suffix (the stuff after the .) would change
  353: as a result of this operation.
  354: 
  355:  Parameters:
  356: 
  357: =over 4
  358: 
  359: =item  $old   = string [in]  Previous filename.
  360: 
  361: =item  $new   = string [in]  Resultant filename.
  362: 
  363: =back
  364: 
  365:  Returns:
  366: 
  367: =over 4
  368: 
  369: =item    Empty string if everything worked.
  370: 
  371: =item    String containing an error message if there was a problem.
  372: 
  373: =back
  374: 
  375: =cut
  376: 
  377: sub checksuffix {
  378:     my ($old,$new) = @_;
  379:     my $result;
  380:     my $oldsuffix;
  381:     my $newsuffix;
  382:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
  383:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
  384:     if ($oldsuffix ne $newsuffix) {
  385: 	$result.=
  386:             '<p><font color="red">Warning: change of MIME type!</font></p>';
  387:     }
  388:     return $result;
  389: }
  390: 
  391: sub cleanDest {
  392:     my ($request,$dest)=@_;
  393:     #remove bad characters
  394:     if  ($dest=~/[\#\?&]/) {
  395: 	$request->print("<p><font color=\"red\">Invalid characters in requested name have been removed.</font></p>");
  396: 	$dest=~s/[\#\?&]//g;
  397:     }
  398:     return $dest;
  399: }
  400: 
  401: =pod
  402: 
  403: =item CloseForm1($request, $user, $file)
  404: 
  405:    Close of a form on the successful completion of phase 1 processing
  406: 
  407: Parameters:
  408: 
  409: =over 4
  410: 
  411: =item  $request - Apache Request Object [in] - Apache server request object.
  412: 
  413: =item  $cancelurl - the url to go to on cancel.
  414: 
  415: =back
  416: 
  417: =cut
  418: 
  419: sub CloseForm1 {
  420:    my ($request,  $cancelurl) = @_;
  421: 
  422: 
  423:    &Debug($request, "Cancel url is: ".$cancelurl);
  424:    $request->print('<p><input type="submit" value="Continue" /></p></form>');
  425:    $request->print('<form action="'.$cancelurl.
  426: 		   '" method="POST"><p><input type="submit" value="Cancel" /></p></form>');
  427: 
  428: }
  429: 
  430: 
  431: =pod
  432: 
  433: =item CloseForm2($request, $user, $directory)
  434: 
  435:    Successfully close off the phase 2 form.
  436: 
  437: Parameters:
  438: 
  439: =over 4
  440: 
  441: =item   $request    - Apache Request object [in] - The request that is being
  442:                  executed.
  443: 
  444: =item   $user       - string [in] - Name of the user that is initiating the
  445:                  request.
  446: 
  447: =item   $directory  - string [in] - Directory in which the operation is 
  448:                  being done relative to the top level construction space
  449:                  directory.
  450: 
  451: =back
  452: 
  453: =cut
  454: 
  455: sub CloseForm2 {
  456:   my ($request, $user, $directory) = @_;
  457: 
  458:   $request->print('<h3><a href="/priv/'.$user.$directory.'/">Done </a> </h3>');
  459: }
  460: 
  461: =pod
  462: 
  463: =item Rename1($request, $filename, $user, $domain, $dir)
  464:  
  465:    Perform phase 1 processing of the file rename operation.
  466: 
  467: Parameters:
  468: 
  469: =over 4
  470: 
  471: =item  $request   - Apache Request Object [in] The request object for the 
  472: current request.
  473: 
  474: =item  $filename  - The filename relative to construction space.
  475: 
  476: =item  $user      - Name of the user making the request.
  477: 
  478: =item  $domain    - User login domain.
  479: 
  480: =item  $dir       - Directory specification of the path to the file.
  481: 
  482: =back
  483: 
  484: Side effects:
  485: 
  486: =over 4
  487: 
  488: =item A new form is displayed prompting for confirmation.  The newfilename
  489: hidden field of this form is loaded with
  490: new filename relative to the current directory ($dir).
  491: 
  492: =back
  493: 
  494: =cut  
  495: 
  496: sub Rename1 {
  497:     my ($request, $filename, $user, $domain, $dir) = @_;
  498:     &Debug($request, "Username - ".$user." filename: ".$filename."\n");
  499:     my $conspace = $filename;
  500: 
  501:     my $cancelurl = "/priv/".$filename;
  502:     $cancelurl    =~ s/\/home\///;
  503:     $cancelurl    =~ s/\/public_html//;
  504:     
  505:     if(-e $conspace) {
  506: 	if($ENV{'form.newfilename'}) {
  507: 	    my $newfilename = $ENV{'form.newfilename'};
  508: 	    if ($newfilename =~ m|/[^\.]+$|) {
  509: 		#no extension add on orignal extension
  510: 		if ($filename =~ m|/[^\.]*\.([^\.]+)$|) {
  511: 		    $newfilename.='.'.$1;
  512: 		}
  513: 	    }
  514: 	    $request->print(&checksuffix($filename, $newfilename));
  515: 	    #renaming a dir, delete the trailing /
  516:             #remove last element for current dir
  517: 	    if ($filename =~ m|/$|) {
  518: 		$filename =~ s|/$||;
  519: 		$dir =~ s|/[^/]*$||;
  520: 	    }
  521: 	    my $return=&exists($user, $domain, $dir, $newfilename);
  522: 	    $request->print($return);
  523: 	    if ($return =~/^Error:/) {
  524: 		$request->print('<br /><a href="'.$cancelurl.'">Cancel</a>');
  525: 		return;
  526: 	    }
  527: 	    my $dest=&SimplifyDir($dir,$newfilename);
  528: 	    $request->print('<input type="hidden" name="newfilename" value="'.
  529: 			    $newfilename.
  530: 			    '" /><p>Rename <tt>'.$filename.
  531: 			    '</tt><br /> to <tt>'.
  532: 			    $dest.'</tt>?</p>');
  533: 	    &CloseForm1($request, $cancelurl);
  534: 	} else {
  535: 	    $request->print('<p>No new filename specified</p></form>');
  536: 	    return;
  537: 	}
  538:     } else {
  539: 	$request->print('<p> No such File </p> </form>');
  540: 	return;
  541:     }
  542:     
  543: }
  544: =pod
  545: 
  546: =item Delete1
  547: 
  548:    Performs phase 1 processing of the delete operation.  In phase one
  549:   we just check to be sure the file 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 session user.
  559: 
  560: 
  561: =item   $filename  - string [in] Name fo the file to be deleted:
  562:                 Filename is the full filesystem path to the file.
  563: 
  564: =back
  565: 
  566: =cut
  567: 
  568: sub Delete1 {
  569:   my ($request, $user,  $filename) = @_;
  570: 
  571:   my $cancelurl = '/priv/'.$filename;
  572:   $cancelurl    =~ s/\/home\///;
  573:   $cancelurl    =~ s/\/public_html//;
  574:   
  575: 
  576:   if( -e $filename) {
  577:     $request->print('<input type="hidden" name="newfilename" value="'.
  578: 		    $filename.'"/>');
  579:     $request->print('<p> Delete <tt>'.$filename.'</tt>?</p>');
  580:     &CloseForm1($request, $cancelurl);
  581:   } else {
  582:     $request->print('<p> No Such file: <tt>'.$filename.'</tt></p></form>');
  583:   }
  584: }
  585: 
  586: =pod
  587: 
  588: =item Copy1($request, $user, $domain, $filename, $newfilename)
  589: 
  590:    Performs phase 1 processing of the construction space copy command.
  591:    Ensure that the source file exists.  Ensure that a destination exists,
  592:    also warn if the destination already exists.
  593: 
  594: Parameters:
  595: 
  596: =over 4
  597: 
  598: =item   $request   - Apache Request Object [in] request object for the current 
  599:                 request.
  600: 
  601: =item   $user      - string [in]  Name of the user initiating the request.
  602: 
  603: =item   $domain    - string [in]  Domain the initiating user is logged in as
  604: 
  605: =item   $dir       - string [in]  Directory path.
  606: 
  607: =item   $filename  - string [in]  Source filename.
  608: 
  609: =item   $newfilename-string [in]  Destination filename.
  610: 
  611: =back
  612: 
  613: =cut
  614: 
  615: sub Copy1 {
  616:   my ($request, $user, $domain, $dir, $filename, $newfilename) = @_;
  617: 
  618:   my $cancelurl = "/priv/".$filename;
  619:   $cancelurl    =~ s/\/home\///;
  620:   $cancelurl    =~ s/\/public_html//;
  621:     
  622: 
  623:   if(-e $filename) {
  624:     $request->print(&checksuffix($filename,$newfilename));
  625:     my $return=&exists($user, $domain, $dir, $newfilename);
  626:     $request->print($return);
  627:     if ($return =~/^Error:/) {
  628: 	$request->print('<br /><a href="'.$cancelurl.'">Cancel</a>');
  629: 	return;
  630:     }
  631:     my $dest=&SimplifyDir($dir,$newfilename);
  632:     $request->print('<input type = "hidden" name = "newfilename" value = "'.
  633: 		    $dir.'/'.$newfilename.
  634: 		    '" /><p>Copy <tt>'.$filename.'</tt><br />  to '.
  635: 		    '<tt>'.$dest.'</tt>?</p>');
  636:     &CloseForm1($request, $cancelurl);
  637:   } else {
  638:     $request->print('<p>No such file <tt>'.$filename.'</p></form>');
  639:   }
  640: }
  641: 
  642: =pod
  643: 
  644: =item SimplifyDir
  645: 
  646:   Removes all extra / and all .. references
  647: 
  648: Parameters:
  649: 
  650: =over 4
  651: 
  652: =item $dir - string [in] a directory name
  653: 
  654: =item $file - string [in] a file reference relative to $dir
  655: 
  656: =back
  657: 
  658: Results: the concatenated path.
  659: 
  660: =cut
  661: 
  662: sub SimplifyDir {
  663:     my ($dir,$file) = @_;
  664:     my $location = $dir. '/'.$file;
  665:     $location=~s://+:/:g; # remove duplicate /
  666:     while ($location=~m:/\.\./:) {$location=~s:/[^/]+/\.\./:/:g;}#remove dir/..
  667:     return $location;
  668: }
  669: 
  670: =pod
  671: 
  672: =item NewDir1
  673:  
  674:   Does all phase 1 processing of directory creation:
  675:   Ensures that the user provides a new directory name,
  676:   and that the directory does not already exist.
  677: 
  678: Parameters:
  679: 
  680: =over 4
  681: 
  682: =item   $request  - Apache Request Object [in] - Server request object for the
  683:                current url.
  684: 
  685: =item   $username - Name of the user that is requesting the directory creation.
  686: 
  687: =item   $path     - current directory relative to construction space.
  688: 
  689: =item   $newdir   - Name of the directory to be created; path relative to the 
  690:                top level of construction space.
  691: =back
  692: 
  693: Side Effects:
  694: 
  695: =over 4
  696: 
  697: =item A new form is displayed.  Clicking on the confirmation button
  698: causes the newdir operation to transition into phase 2.  The hidden field
  699: "newfilename" is set with the construction space path to the new directory.
  700: 
  701: 
  702: =back
  703: 
  704: =cut
  705: 
  706: 
  707: sub NewDir1
  708: {
  709:   my ($request, $username, $path, $newdir) = @_;
  710: 
  711:   my $fullpath = '/home/'.$username.'/public_html/'.
  712:     $path.'/'.$newdir;
  713: 
  714:   my $cancelurl = '/priv/'.$username.'/'.$path;
  715: 
  716:   &Debug($request, "Full path is : ".$fullpath);
  717: 
  718:   if(-e $fullpath) {
  719:     $request->print('<p>Directory exists.</p></form>');
  720:   }
  721:   else {
  722:     $request->print('<input type="hidden" name="newfilename" value="'.
  723: 		    $newdir.'" /><p>Make new directory <tt>'.
  724: 		    $path."/".$newdir.'</tt>?</p>');
  725:     &CloseForm1($request, $cancelurl);
  726: 
  727:   }
  728: }
  729: 
  730: =pod
  731: 
  732: =item NewFile1
  733:  
  734:   Does all phase 1 processing of file creation:
  735:   Ensures that the user provides a new filename, adds proper extension
  736:   if needed and that the file does not already exist, if it is a html,
  737:   problem, page, or sequence, it then creates a form link to hand the
  738:   actual creation off to the proper handler.
  739: 
  740: Parameters:
  741: 
  742: =over 4
  743: 
  744: =item   $request  - Apache Request Object [in] - Server request object for the
  745:                current url.
  746: 
  747: =item   $username - Name of the user that is requesting the directory creation.
  748: 
  749: =item   $domain   - Name of the domain of the user
  750: 
  751: =item   $dir      - current absolute diretory
  752: 
  753: =item   $newfilename
  754:                   - Name of the file to be created; no path information
  755: =back
  756: 
  757: Side Effects:
  758: 
  759: =over 4
  760: 
  761: =item 2 new forms are displayed.  Clicking on the confirmation button
  762: causes the browser to attempt to load the specfied URL, allowing the
  763: proper handler to take care of file creation. There is also a Cancle
  764: button which returns you to the driectory listing you came from
  765: 
  766: =back
  767: 
  768: =cut
  769: 
  770: 
  771: sub NewFile1 {
  772:     my ($request, $user, $domain, $dir, $newfilename) = @_;
  773: 
  774:     &Debug($request, "Dir is : ".$dir);
  775:     &Debug($request, "Newfile is : ".$newfilename);
  776: 
  777:     my $cancelurl = "/priv/".$dir;
  778:     $cancelurl    =~ s/\/home\///;
  779:     $cancelurl    =~ s/\/public_html//;
  780: 
  781:     if ($ENV{'form.action'} =~ /new(.+)file/) {
  782: 	my $extension=$1;
  783: 	if ($newfilename !~ /\Q.$extension\E$/) {
  784: 	    if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) {
  785: 		#already has an extension strip it and add in expected one
  786: 		$newfilename =~ s|.([^\.]+)$||;
  787: 	    }
  788: 	    $newfilename.=".$extension";
  789: 	}
  790:     }
  791: 
  792:     my $fullpath = $dir.'/'.$newfilename;
  793: 
  794:     &Debug($request, "Full path is : ".$fullpath);
  795: 
  796:     if(-e $fullpath) {
  797: 	$request->print('<p>File exists.</p></form>');
  798:     }
  799:     else {
  800: 	$request->print('<p>Make new file <tt>'.$dir.'/'.$newfilename.'</tt>?</p>');
  801: 	my $dest=&MakeFinalUrl($request,$fullpath);
  802: 	&Debug($request, "Cancel url is: ".$cancelurl);
  803: 	&Debug($request, "Dest url is: ".$dest);
  804: 	$request->print('</form>');
  805: 	$request->print('<form action="'.$dest.
  806: 			'" method="POST"><p><input type="submit" value="Continue" /></p></form>');
  807: 	$request->print('<form action="'.$cancelurl.
  808: 			'" method="POST"><p><input type="submit" value="Cancel" /></p></form>');
  809:     }
  810: }
  811: 
  812: =pod
  813: 
  814: =item phaseone($r, $fn, $uname, $udom)
  815: 
  816:   Peforms phase one processing of the request.  In phase one, error messages
  817: are returned if the request cannot be performed (e.g. attempts to manipulate
  818: files that are nonexistent).  If the operation can be performed, what is
  819: about to be done will be presented to the user for confirmation.  If the
  820: user confirms the request, then phase two is executed, the action 
  821: performed and reported to the user.
  822: 
  823:  Parameters:
  824: 
  825: =over 4
  826: 
  827: =item $r  - request object [in] - The Apache request being executed.
  828: 
  829: =item $fn = string [in] - The filename being manipulated by the 
  830:                              request.
  831: 
  832: =item $uname - string [in] Name of user logged in and doing this action.
  833: 
  834: =item $udom  - string [in] Domain name under which the user logged in. 
  835: 
  836: =back
  837: 
  838: =cut
  839: 
  840: sub phaseone {
  841:   my ($r,$fn,$uname,$udom)=@_;
  842:   
  843:   $fn=~m:(.*)/([^/]+)\.(\w+)$:;
  844:   my $dir=$1;
  845:   my $main=$2;
  846:   my $suffix=$3;
  847:   
  848:   #  my $conspace=ConstructionPathFromRelative($uname, $fn);
  849:   
  850:   $ENV{'form.newfilename'}=&cleanDest($r,$ENV{'form.newfilename'});
  851: 
  852:   $r->print('<form action="/adm/cfile" method="post">'.
  853: 	    '<input type="hidden" name="filename" value="/~'.$uname.$fn.'" />'.
  854: 	    '<input type="hidden" name="phase" value="two" />'.
  855: 	    '<input type="hidden" name="action" value="'.$ENV{'form.action'}.'" />');
  856:   
  857:   if ($ENV{'form.action'} eq 'rename') {
  858:       if (!defined($dir)) {
  859: 	  $fn=~m:(.*)/:;
  860: 	  $dir=$1;
  861:       }
  862:       &Rename1($r, $fn, $uname, $udom, $dir);
  863:   } elsif ($ENV{'form.action'} eq 'delete') { 
  864:     
  865:     &Delete1($r, $uname, $fn);
  866:     
  867:   } elsif ($ENV{'form.action'} eq 'copy') { 
  868:     if($ENV{'form.newfilename'}) {
  869:       my $newfilename = $ENV{'form.newfilename'};
  870:       &Copy1($r, $uname, $udom, $dir, $fn, $newfilename);
  871:     }else {
  872:       $r->print('<p>No new filename specified.</p></form>');
  873:     }
  874:   } elsif ($ENV{'form.action'} eq 'newdir') {
  875:     &NewDir1($r, $uname, $dir, $ENV{'form.newfilename'});
  876:   }  elsif ($ENV{'form.action'} eq 'newfile' ||
  877: 	    $ENV{'form.action'} eq 'newhtmlfile' ||
  878: 	    $ENV{'form.action'} eq 'newproblemfile' ||
  879:             $ENV{'form.action'} eq 'newpagefile' ||
  880:             $ENV{'form.action'} eq 'newsequencefile' ||
  881:             $ENV{'form.action'} eq 'Select Action') {
  882:       if($ENV{'form.newfilename'}) {
  883: 	  my $newfilename = $ENV{'form.newfilename'};
  884: 	  if (!defined($dir)) {
  885: 	      $fn=~m:(.*)/:;
  886: 	      $dir=$1;
  887: 	  }
  888: 	  &NewFile1($r, $uname, $udom, $dir, $newfilename);
  889:       } else {
  890: 	  $r->print('<p>No new filename specified.</p></form>');
  891:       }
  892:   }
  893: }
  894: 
  895: =pod
  896: 
  897: =item Rename2($request, $user, $directory, $oldfile, $newfile)
  898: 
  899: Performs phase 2 processing of a rename reequest.   This is where the
  900: actual rename is performed.
  901: 
  902: Parameters
  903: 
  904: =over 4
  905: 
  906: =item $request - Apache request object [in] The request being processed.
  907: 
  908: =item $user  - string [in] The name of the user initiating the request.
  909: 
  910: =item $directory - string [in] The name of the directory relative to the
  911:                  construction space top level of the renamed file.
  912: 
  913: =item $oldfile - Name of the file.
  914: 
  915: =item $newfile - Name of the new file.
  916: 
  917: =back
  918: 
  919: Returns:
  920: 
  921: =over 4
  922: 
  923: =item 1 Success.
  924: 
  925: =item 0 Failure.
  926: 
  927: =cut
  928: 
  929: sub Rename2 {
  930: 
  931:   my ($request, $user, $directory, $oldfile, $newfile) = @_;
  932: 
  933:   &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
  934: 	 " new file ".$newfile."\n");
  935:   &Debug($request, "Target is: ".$directory.'/'.
  936: 	 $newfile);
  937: 
  938:   if(-e $oldfile) {
  939:       my $dest;
  940: 
  941:       if ($oldfile =~ m|/$|) {
  942: 	  #renaming a dir
  943: 	  $oldfile =~ s|/$||;
  944: 	  $dest=$directory;
  945: 	  $dest=~s|(/)([^/]*)$|$1|;
  946: 	  $dest.='/'.$newfile;
  947:       } else {
  948: 	  $dest=$directory.'/'.$newfile;
  949:       }
  950: 
  951:       unless(rename($oldfile,$dest)) {
  952: 	  $request->print('<font color="red">Error: '.$!.'</font>');
  953: 	  return 0;
  954:       } else {}
  955:   } else {
  956:       $request->print("<p> No such file: /home".$user.'/public_html'.
  957: 		      $oldfile.' </p></form>');
  958:       return 0;
  959:   }
  960:   return 1;
  961: }
  962: =pod
  963: 
  964: =item Delete2($request, $user, $filename)
  965: 
  966:   Performs phase two of a delete.  The user has confirmed that they want 
  967: to delete the selected file.   The file is deleted and the results of the
  968: delete attempt are indicated.
  969: 
  970: Parameters:
  971: 
  972: =over 4
  973: 
  974: =item $request - Apache Request object [in] the request object for the current
  975:                  delete operation.
  976: 
  977: =item $user    - string [in]  The name of the user initiating the delete
  978:                  request.
  979: 
  980: =item $filename - string [in] The name of the file, relative to construction
  981:                   space, to delete.
  982: 
  983: =back
  984: 
  985: Returns:
  986:   1 - success.
  987:   0 - Failure.
  988: 
  989: =cut
  990: 
  991: sub Delete2 {
  992:   my ($request, $user, $filename) = @_;
  993: 
  994:   if(-e $filename) {
  995:     unless(unlink($filename)) {
  996:       $request->print('<font color="red">Error: '.$!.'</font>');
  997:       return 0;
  998:     }
  999:   } else {
 1000:     $request->print('<p> No such file. </p></form');
 1001:     return 0;
 1002:   }
 1003:   return 1;
 1004: }
 1005: 
 1006: =pod
 1007: 
 1008: =item Copy2($request, $username, $dir, $oldfile, $newfile)
 1009: 
 1010:    Performs phase 2 of a copy.  The file is copied and the status 
 1011:    of that copy is reported back to the user.
 1012: 
 1013: =over 4
 1014: 
 1015: =item $request - Apache request object [in]; the apache request currently
 1016:                  being executed.
 1017: 
 1018: =item $username - string [in] Name of the user who is requesting the copy.
 1019: 
 1020: =item $dir - string [in] Directory path relative to the construction space
 1021:              of the destination file.
 1022: 
 1023: =item $oldfile - string [in] Name of the source file.
 1024: 
 1025: =item $newfile - string [in] Name of the destination file.
 1026: 
 1027: 
 1028: =back
 1029: 
 1030: Returns 0 failure, and 0 successs.
 1031: 
 1032: =cut
 1033: 
 1034: sub Copy2 {
 1035:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
 1036:     &Debug($request ,"Will try to copy $oldfile to $newfile");
 1037:     if(-e $oldfile) {
 1038: 	unless (copy($oldfile, $newfile)) {
 1039: 	    $request->print('<font color="red"> copy Error: '.$!.'</font>');
 1040: 	    return 0;
 1041: 	} else {
 1042: 	    unless (chmod(0660, $newfile)) {
 1043: 		$request->print('<font color="red"> chmod error: '.$!.'</font>');
 1044: 		return 0;
 1045: 	    }
 1046: 	    return 1;
 1047: 	}
 1048:     } else {
 1049: 	$request->print('<p> No such file </p>');
 1050: 	return 0;
 1051:     }
 1052:     return 1;
 1053: }
 1054: =pod
 1055: 
 1056: =item NewDir2($request, $user, $newdirectory)
 1057: 
 1058: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
 1059: 	reporting the results of that creation to the user.
 1060: 	
 1061: Parameters:
 1062: =over 4
 1063: 
 1064: =item $request  - Apache request object [in].  Object representing the current HTTP request.
 1065: 
 1066: =item $user - string [in] The name of the user that is initiating the request.
 1067: 
 1068: =item $newdirectory - string [in] The full path of the directory being created.
 1069: 
 1070: =back
 1071: 
 1072: Returns 0 - failure 1 - success.
 1073: 
 1074: =cut
 1075: 
 1076: sub NewDir2 {
 1077:   my ($request, $user, $newdirectory) = @_;
 1078:   
 1079:   unless(mkdir($newdirectory, 02770)) {
 1080:     $request->print('<font color="red">Error: '.$!.'</font>');
 1081:     return 0;
 1082:   }
 1083:   unless(chmod(02770, ($newdirectory))) {
 1084:       $request->print('<font color="red"> Error: '.$!.'</font>');
 1085:       return 0;
 1086:   }
 1087:   return 1;
 1088: }
 1089: 
 1090: =pod
 1091: 
 1092: =item phasetwo($r, $fn, $uname, $udom)
 1093: 
 1094:    Controls the phase 2 processing of file management
 1095:    requests for construction space.  In phase one, the user
 1096:    was asked to confirm the operation.  In phase 2, the operation
 1097:    is performed and the result is shown.
 1098: 
 1099:   The strategy is to break out the processing into specific action processors
 1100:   named action2 where action is the requested action and the 2 denotes 
 1101:   phase 2 processing.
 1102: 
 1103: Parameters:
 1104: 
 1105: =over 4
 1106: 
 1107: =item  $r     - Apache Request object [in] The request object for this httpd
 1108:            transaction.
 1109: 
 1110: =item  $fn    - string [in]  A filename indicating the object that is being
 1111:            manipulated.
 1112: 
 1113: =item  $uname - string [in] The name of the user initiating the file management
 1114:            request.
 1115: 
 1116: =item  $udom  - string  [in] The login domain of the user initiating the
 1117:            file management request.
 1118: =back
 1119: 
 1120: =cut
 1121: 
 1122: sub phasetwo {
 1123:     my ($r,$fn,$uname,$udom)=@_;
 1124:     
 1125:     &Debug($r, "loncfile - Entering phase 2 for $fn");
 1126:     
 1127:     # Break down the file into it's component pieces.
 1128:     
 1129:     my $dir;		# Directory path
 1130:     my $main;		# Filename.
 1131:     my $suffix;		# Extension.
 1132: 
 1133:     if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) {
 1134: 	$dir=$1;		# Directory path
 1135: 	$main=$2;		# Filename.
 1136: 	$suffix=$3;		# Extension.
 1137:     }
 1138:         
 1139:     my $dest;                   # On success this is where we'll go.
 1140:     
 1141:     &Debug($r, 
 1142: 	   "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
 1143:     &Debug($r,
 1144: 	   "    newfilename = ".$ENV{'form.newfilename'});
 1145: 
 1146:     my $conspace=$fn;
 1147:     
 1148:     &Debug($r, 
 1149: 	   "loncfile::phase2 Full construction space name: $conspace");
 1150:     
 1151:     &Debug($r, 
 1152: 	   "loncfie::phase2 action is $ENV{'form.action'}");
 1153:     
 1154:     # Select the appropriate processing sub.
 1155:     
 1156:     if ($ENV{'form.action'} eq 'rename') { # Rename.
 1157: 	if($ENV{'form.newfilename'}) {
 1158: 	    if (!defined($dir)) {
 1159: 		$fn=~m:^(.*)/:;
 1160: 		$dir=$1;
 1161: 	    }
 1162: 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
 1163: 		return;
 1164: 	    }
 1165: 	    # Prepend the directory to the new name to form the basis of the
 1166: 	    # url of the new resource.
 1167: 	    #
 1168: 	    #renaming a dir
 1169:             #remove last element for current dir
 1170: 	    if ($fn =~ m|/$|) { $dir =~ s|/[^/]*$||; }
 1171: 	    $dest = $dir."/".$ENV{'form.newfilename'};
 1172: 	}
 1173:     } elsif ($ENV{'form.action'} eq 'delete') { 
 1174: 	if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
 1175: 	    return ;
 1176: 	}
 1177: 	# Once a resource is deleted, we just list the directory that
 1178: 	# previously held it.
 1179: 	#
 1180: 	$dest = $dir."/.";		# Parent dir.
 1181:     } elsif ($ENV{'form.action'} eq 'copy') { 
 1182: 	if($ENV{'form.newfilename'}) {
 1183: 	    if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
 1184: 		return
 1185: 		}
 1186: 	    $dest = $ENV{'form.newfilename'};
 1187:      
 1188: 	} else {
 1189: 	    $r->print('<p>No New filename specified</p></form>');
 1190: 	    return;
 1191: 	}
 1192: 	
 1193:     } elsif ($ENV{'form.action'} eq 'newdir') {
 1194: 	#
 1195: 	# Since the newfilename form field is construction space
 1196: 	# relative, ew need to prepend the current path; now in $fn.
 1197: 	#
 1198:         my $newdir= $fn.$ENV{'form.newfilename'};
 1199: 	if(!&NewDir2($r, $uname, $newdir)) {
 1200: 	    return;
 1201: 	}
 1202: 	$dest = $newdir."/"
 1203:     }
 1204:     #
 1205:     #  Substitute for priv for the first home in $dir to get our
 1206:     # construction space path.
 1207:     #
 1208:     $dest=&MakeFinalUrl($r,$dest);
 1209: 
 1210:     $r->print('<h3><a href="'.$dest.'">Done</a></h3>');
 1211: }
 1212: 
 1213: sub MakeFinalUrl {
 1214:     my($r,$dest)=@_;
 1215:     &Debug($r, "Final url is: $dest");
 1216:     $dest =~ s|/home/|/priv/|;
 1217:     $dest =~ s|/public_html||;
 1218: 
 1219:     my $base = &File::Basename::basename($dest);
 1220:     my $dpath= &File::Basename::dirname($dest);
 1221:     if ($base eq '.') { $base=''; }
 1222:     $dest = &HTML::Entities::encode($dpath.'/'.$base);
 1223: 
 1224:     &Debug($r, "Final url after rewrite: $dest");
 1225:     return $dest;
 1226: }
 1227: 
 1228: sub handler {
 1229: 
 1230:   $r=shift;
 1231: 
 1232: 
 1233:   &Debug($r, "loncfile.pm - handler entered");
 1234:   &Debug($r, " filename: ".$ENV{'form.filename'});
 1235:   &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
 1236: 
 1237:   my $fn;
 1238: 
 1239:   if ($ENV{'form.filename'}) {
 1240:       $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
 1241:       &Debug($r, "loncfile::handler - raw url: $fn");
 1242: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
 1243: #      $fn=~s/^http\:\/\/[^\/]+//;
 1244:       $fn=URLToPath($fn);
 1245:       &Debug($r, "loncfile::handler - doctored url: $fn");
 1246: 
 1247:   } else {
 1248:       &Debug($r, "loncfile::handler - no form.filename");
 1249:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1250:          ' unspecified filename for cfile', $r->filename); 
 1251:      return HTTP_NOT_FOUND;
 1252:   }
 1253: 
 1254:   unless ($fn) { 
 1255:       &Debug($r, "loncfile::handler - doctored url is empty");
 1256:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1257:          ' trying to cfile non-existing file', $r->filename); 
 1258:      return HTTP_NOT_FOUND;
 1259:   } 
 1260: 
 1261: # ----------------------------------------------------------- Start page output
 1262:   my $uname;
 1263:   my $udom;
 1264: 
 1265:   ($uname,$udom)=
 1266:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
 1267:   &Debug($r, 
 1268: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
 1269:   unless (($uname) && ($udom)) {
 1270:      $r->log_reason($uname.' at '.$udom.
 1271:          ' trying to manipulate file '.$ENV{'form.filename'}.
 1272:          ' ('.$fn.') - not authorized', 
 1273:          $r->filename); 
 1274:      return HTTP_NOT_ACCEPTABLE;
 1275:   }
 1276: 
 1277:   $fn=~s/\/\~(\w+)//;
 1278:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
 1279: 
 1280:   $r->content_type('text/html');
 1281:   $r->send_http_header;
 1282: 
 1283:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
 1284: 
 1285:   $r->print(
 1286:    '<body bgcolor="#FFFFFF"><img align="right" src="/adm/lonIcons/lonlogos.gif" />');
 1287: 
 1288:   
 1289:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
 1290:   
 1291:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
 1292:           $r->print('<h3><font color="red">Co-Author: '.$uname.' at '.$udom.
 1293:                '</font></h3>');
 1294:   }
 1295: 
 1296: 
 1297:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
 1298:   if ($ENV{'form.action'} eq 'delete') {
 1299:       
 1300:       $r->print('<h3>Delete</h3>');
 1301:   } elsif ($ENV{'form.action'} eq 'rename') {
 1302:       $r->print('<h3>Rename</h3>');
 1303:   } elsif ($ENV{'form.action'} eq 'newdir') {
 1304:       $r->print('<h3>New Directory</h3>');
 1305:   } elsif ($ENV{'form.action'} eq 'copy') {
 1306:       $r->print('<h3>Copy</h3>');
 1307:   } elsif ($ENV{'form.action'} eq 'newfile' ||
 1308: 	   $ENV{'form.action'} eq 'newhtmlfile' ||
 1309: 	   $ENV{'form.action'} eq 'newproblemfile' ||
 1310:            $ENV{'form.action'} eq 'newpagefile' ||
 1311:            $ENV{'form.action'} eq 'newsequencefile' ||
 1312:            $ENV{'form.action'} eq 'Select Action' ) {
 1313:       $r->print('<h3>New Resource</h3>');
 1314:   } else {
 1315:      $r->print('<p>Unknown Action '.$ENV{'form.action'}.' </p></body></html>');
 1316:      return OK;  
 1317:   }
 1318:   if ($ENV{'form.phase'} eq 'two') {
 1319:       &Debug($r, "loncfile::handler  entering phase2");
 1320:       &phasetwo($r,$fn,$uname,$udom);
 1321:   } else {
 1322:       &Debug($r, "loncfile::handler  entering phase1");
 1323:       &phaseone($r,$fn,$uname,$udom);
 1324:   }
 1325: 
 1326:   $r->print('</body></html>');
 1327:   return OK;  
 1328: }
 1329: 
 1330: 1;
 1331: __END__
 1332: 

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