File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.35: download - view: text, annotated - select for diffs
Fri Aug 1 20:32:05 2003 UTC (20 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Looking better for bug #2018, needs testing.

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