File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.22: download - view: text, annotated - select for diffs
Tue Feb 4 21:54:17 2003 UTC (21 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added code for new file creation

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

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