File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue Feb 4 22:01:38 2003 UTC (21 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- xhtmlize

    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.23 2003/02/04 22:01:38 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.
  509: 			    '</tt><br /> to <tt>'.
  510: 			    $dest.'</tt>?</p>');
  511: 	    &CloseForm1($request, $cancelurl);
  512: 	} else {
  513: 	    $request->print('<p>No new filename specified</p></form>');
  514: 	    return;
  515: 	}
  516:     } else {
  517: 	$request->print('<p> No such File </p> </form>');
  518: 	return;
  519:     }
  520:     
  521: }
  522: =pod
  523: 
  524: =item Delete1
  525: 
  526:    Performs phase 1 processing of the delete operation.  In phase one
  527:   we just check to be sure the file exists.
  528: 
  529: Parameters:
  530: 
  531: =over 4
  532: 
  533: =item   $request   - Apache Request Object [in] request object for the current
  534:                 request.
  535: 
  536: =item   $user      - string [in] Name of session user.
  537: 
  538: 
  539: =item   $filename  - string [in] Name fo the file to be deleted:
  540:                 Filename is the full filesystem path to the file.
  541: 
  542: =back
  543: 
  544: =cut
  545: 
  546: sub Delete1 {
  547:   my ($request, $user,  $filename) = @_;
  548: 
  549:   my $cancelurl = '/priv/'.$filename;
  550:   $cancelurl    =~ s/\/home\///;
  551:   $cancelurl    =~ s/\/public_html//;
  552:   
  553: 
  554:   if( -e $filename) {
  555:     $request->print('<input type="hidden" name="newfilename" value="'.
  556: 		    $filename.'"/>');
  557:     $request->print('<p> Delete <tt>'.$filename.'</tt>?</p>');
  558:     &CloseForm1($request, $cancelurl);
  559:   } else {
  560:     $request->print('<p> No Such file: <tt>'.$filename.'</tt></p></form>');
  561:   }
  562: }
  563: 
  564: =pod
  565: 
  566: =item Copy1($request, $user, $domain, $filename, $newfilename)
  567: 
  568:    Performs phase 1 processing of the construction space copy command.
  569:    Ensure that the source file exists.  Ensure that a destination exists,
  570:    also warn if the destination already exists.
  571: 
  572: Parameters:
  573: 
  574: =over 4
  575: 
  576: =item   $request   - Apache Request Object [in] request object for the current 
  577:                 request.
  578: 
  579: =item   $user      - string [in]  Name of the user initiating the request.
  580: 
  581: =item   $domain    - string [in]  Domain the initiating user is logged in as
  582: 
  583: =item   $dir       - string [in]  Directory path.
  584: 
  585: =item   $filename  - string [in]  Source filename.
  586: 
  587: =item   $newfilename-string [in]  Destination filename.
  588: 
  589: =back
  590: 
  591: =cut
  592: 
  593: sub Copy1 {
  594:   my ($request, $user, $domain, $dir, $filename, $newfilename) = @_;
  595: 
  596:   my $cancelurl = "/priv/".$filename;
  597:   $cancelurl    =~ s/\/home\///;
  598:   $cancelurl    =~ s/\/public_html//;
  599:     
  600: 
  601:   if(-e $filename) {
  602:     $request->print(&checksuffix($filename,$newfilename));
  603:     my $return=&exists($user, $domain, $dir, $newfilename);
  604:     $request->print($return);
  605:     if ($return =~/^Error:/) {
  606: 	$request->print('<br /><a href="'.$cancelurl.'">Cancel</a>');
  607: 	return;
  608:     }
  609:     my $dest=&SimplifyDir($dir,$newfilename);
  610:     $request->print('<input type = "hidden" name = "newfilename" value = "'.
  611: 		    $dir.'/'.$newfilename.
  612: 		    '" /><p>Copy <tt>'.$filename.'</tt><br />  to '.
  613: 		    '<tt>'.$dest.'</tt>?</p>');
  614:     &CloseForm1($request, $cancelurl);
  615:   } else {
  616:     $request->print('<p>No such file <tt>'.$filename.'</p></form>');
  617:   }
  618: }
  619: 
  620: =pod
  621: 
  622: =item SimplifyDir
  623: 
  624:   Removes all extra / and all .. references
  625: 
  626: Parameters:
  627: 
  628: =over 4
  629: 
  630: =item $dir - string [in] a directory name
  631: 
  632: =item $file - string [in] a file reference relative to $dir
  633: 
  634: =back
  635: 
  636: Results: the concatenated path.
  637: 
  638: =cut
  639: 
  640: sub SimplifyDir {
  641:     my ($dir,$file) = @_;
  642:     my $location = $dir. '/'.$file;
  643:     $location=~s://+:/:g; # remove duplicate /
  644:     while ($location=~m:/\.\./:) {$location=~s:/[^/]+/\.\./:/:g;}#remove dir/..
  645:     return $location;
  646: }
  647: 
  648: =pod
  649: 
  650: =item NewDir1
  651:  
  652:   Does all phase 1 processing of directory creation:
  653:   Ensures that the user provides a new directory name,
  654:   and that the directory does not already exist.
  655: 
  656: Parameters:
  657: 
  658: =over 4
  659: 
  660: =item   $request  - Apache Request Object [in] - Server request object for the
  661:                current url.
  662: 
  663: =item   $username - Name of the user that is requesting the directory creation.
  664: 
  665: =item   $path     - current directory relative to construction space.
  666: 
  667: =item   $newdir   - Name of the directory to be created; path relative to the 
  668:                top level of construction space.
  669: =back
  670: 
  671: Side Effects:
  672: 
  673: =over 4
  674: 
  675: =item A new form is displayed.  Clicking on the confirmation button
  676: causes the newdir operation to transition into phase 2.  The hidden field
  677: "newfilename" is set with the construction space path to the new directory.
  678: 
  679: 
  680: =back
  681: 
  682: =cut
  683: 
  684: 
  685: sub NewDir1
  686: {
  687:   my ($request, $username, $path, $newdir) = @_;
  688: 
  689:   my $fullpath = '/home/'.$username.'/public_html/'.
  690:     $path.'/'.$newdir;
  691: 
  692:   my $cancelurl = '/priv/'.$username.'/'.$path;
  693: 
  694:   &Debug($request, "Full path is : ".$fullpath);
  695: 
  696:   if(-e $fullpath) {
  697:     $request->print('<p>Directory exists.</p></form>');
  698:   }
  699:   else {
  700:     $request->print('<input type="hidden" name="newfilename" value="'.
  701: 		    $newdir.'" /><p>Make new directory <tt>'.
  702: 		    $path."/".$newdir.'</tt>?</p>');
  703:     &CloseForm1($request, $cancelurl);
  704: 
  705:   }
  706: }
  707: 
  708: =pod
  709: 
  710: =item NewFile1
  711:  
  712:   Does all phase 1 processing of file creation:
  713:   Ensures that the user provides a new filename, adds proper extension
  714:   if needed and that the file does not already exist, if it is a html,
  715:   problem, page, or sequence, it then creates a form link to hand the
  716:   actual creation off to the proper handler.
  717: 
  718: Parameters:
  719: 
  720: =over 4
  721: 
  722: =item   $request  - Apache Request Object [in] - Server request object for the
  723:                current url.
  724: 
  725: =item   $username - Name of the user that is requesting the directory creation.
  726: 
  727: =item   $domain   - Name of the domain of the user
  728: 
  729: =item   $dir      - current absolute diretory
  730: 
  731: =item   $newfilename
  732:                   - Name of the file to be created; no path information
  733: =back
  734: 
  735: Side Effects:
  736: 
  737: =over 4
  738: 
  739: =item 2 new forms are displayed.  Clicking on the confirmation button
  740: causes the browser to attempt to load the specfied URL, allowing the
  741: proper handler to take care of file creation. There is also a Cancle
  742: button which returns you to the driectory listing you came from
  743: 
  744: =back
  745: 
  746: =cut
  747: 
  748: 
  749: sub NewFile1 {
  750:     my ($request, $user, $domain, $dir, $newfilename) = @_;
  751: 
  752:     &Debug($request, "Dir is : ".$dir);
  753:     &Debug($request, "Newfile is : ".$newfilename);
  754: 
  755:     my $cancelurl = "/priv/".$dir;
  756:     $cancelurl    =~ s/\/home\///;
  757:     $cancelurl    =~ s/\/public_html//;
  758: 
  759:     if ($ENV{'form.action'} =~ /new(.+)file/) {
  760: 	my $extension=$1;
  761: 	if ($newfilename !~ /\Q.$extension\E$/) {
  762: 	    $newfilename.=".$extension";
  763: 	}
  764:     }
  765: 
  766:     my $fullpath = $dir.'/'.$newfilename;
  767: 
  768:     &Debug($request, "Full path is : ".$fullpath);
  769: 
  770:     if(-e $fullpath) {
  771: 	$request->print('<p>File exists.</p></form>');
  772:     }
  773:     else {
  774: 	$request->print('<p>Make new file <tt>'.$newfilename.'</tt>?</p>');
  775: 	my $dest=&MakeFinalUrl($request,$fullpath);
  776: 	&Debug($request, "Cancel url is: ".$cancelurl);
  777: 	&Debug($request, "Dest url is: ".$dest);
  778: 	$request->print('</form>');
  779: 	$request->print('<form action="'.$dest.
  780: 			'" method="GET"><p><input type="submit" value="Continue" /></p></form>');
  781: 	$request->print('<form action="'.$cancelurl.
  782: 			'" method="GET"><p><input type="submit" value="Cancel" /></p></form>');
  783:     }
  784: }
  785: 
  786: =pod
  787: 
  788: =item phaseone($r, $fn, $uname, $udom)
  789: 
  790:   Peforms phase one processing of the request.  In phase one, error messages
  791: are returned if the request cannot be performed (e.g. attempts to manipulate
  792: files that are nonexistent).  If the operation can be performed, what is
  793: about to be done will be presented to the user for confirmation.  If the
  794: user confirms the request, then phase two is executed, the action 
  795: performed and reported to the user.
  796: 
  797:  Parameters:
  798: 
  799: =over 4
  800: 
  801: =item $r  - request object [in] - The Apache request being executed.
  802: 
  803: =item $fn = string [in] - The filename being manipulated by the 
  804:                              request.
  805: 
  806: =item $uname - string [in] Name of user logged in and doing this action.
  807: 
  808: =item $udom  - string [in] Domain name under which the user logged in. 
  809: 
  810: =back
  811: 
  812: =cut
  813: 
  814: sub phaseone {
  815:   my ($r,$fn,$uname,$udom)=@_;
  816:   
  817:   $fn=~m:(.*)/([^/]+)\.(\w+)$:;
  818:   my $dir=$1;
  819:   my $main=$2;
  820:   my $suffix=$3;
  821:   
  822:   #  my $conspace=ConstructionPathFromRelative($uname, $fn);
  823:   
  824:   
  825:   $r->print('<form action="/adm/cfile" method="post">'.
  826: 	    '<input type="hidden" name="filename" value="/~'.$uname.$fn.'" />'.
  827: 	    '<input type="hidden" name="phase" value="two" />'.
  828: 	    '<input type="hidden" name="action" value="'.$ENV{'form.action'}.'" />');
  829:   
  830:   if ($ENV{'form.action'} eq 'rename') {
  831:     
  832:     &Rename1($r, $fn, $uname, $udom, $dir);
  833:     
  834:   } elsif ($ENV{'form.action'} eq 'delete') { 
  835:     
  836:     &Delete1($r, $uname, $fn);
  837:     
  838:   } elsif ($ENV{'form.action'} eq 'copy') { 
  839:     if($ENV{'form.newfilename'}) {
  840:       my $newfilename = $ENV{'form.newfilename'};
  841:       &Copy1($r, $uname, $udom, $dir, $fn, $newfilename);
  842:     }else {
  843:       $r->print('<p>No new filename specified.</p></form>');
  844:     }
  845:   } elsif ($ENV{'form.action'} eq 'newdir') {
  846:     &NewDir1($r, $uname, $dir, $ENV{'form.newfilename'});
  847:   }  elsif ($ENV{'form.action'} eq 'newfile' ||
  848: 	    $ENV{'form.action'} eq 'newhtmlfile' ||
  849: 	    $ENV{'form.action'} eq 'newproblemfile') {
  850:     if($ENV{'form.newfilename'}) {
  851:       my $newfilename = $ENV{'form.newfilename'};
  852:       if (!defined($dir)) {
  853: 	  $fn=~m:(.*)/:;
  854: 	  $dir=$1;
  855:       }
  856:       &NewFile1($r, $uname, $udom, $dir, $fn, $newfilename);
  857:     }else {
  858:       $r->print('<p>No new filename specified.</p></form>');
  859:     }
  860:   }
  861: }
  862: 
  863: =pod
  864: 
  865: =item Rename2($request, $user, $directory, $oldfile, $newfile)
  866: 
  867: Performs phase 2 processing of a rename reequest.   This is where the
  868: actual rename is performed.
  869: 
  870: Parameters
  871: 
  872: =over 4
  873: 
  874: =item $request - Apache request object [in] The request being processed.
  875: 
  876: =item $user  - string [in] The name of the user initiating the request.
  877: 
  878: =item $directory - string [in] The name of the directory relative to the
  879:                  construction space top level of the renamed file.
  880: 
  881: =item $oldfile - Name of the file.
  882: 
  883: =item $newfile - Name of the new file.
  884: 
  885: =back
  886: 
  887: Returns:
  888: 
  889: =over 4
  890: 
  891: =item 1 Success.
  892: 
  893: =item 0 Failure.
  894: 
  895: =cut
  896: 
  897: sub Rename2 {
  898: 
  899:   my ($request, $user, $directory, $oldfile, $newfile) = @_;
  900: 
  901:   &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
  902: 	 " new file ".$newfile."\n");
  903:   &Debug($request, "Target is: ".$directory.'/'.
  904: 	 $newfile);
  905: 
  906:   if(-e $oldfile) {
  907:       unless(rename($oldfile,
  908: 		    $directory.'/'.$newfile)) {
  909: 	  $request->print('<font color="red">Error: '.$!.'</font>');
  910: 	  return 0;
  911:       } else {}
  912:   } else {
  913:       $request->print("<p> No such file: /home".$user.'/public_html'.
  914: 		      $oldfile.' </p></form>');
  915:       return 0;
  916:   }
  917:   return 1;
  918: }
  919: =pod
  920: 
  921: =item Delete2($request, $user, $filename)
  922: 
  923:   Performs phase two of a delete.  The user has confirmed that they want 
  924: to delete the selected file.   The file is deleted and the results of the
  925: delete attempt are indicated.
  926: 
  927: Parameters:
  928: 
  929: =over 4
  930: 
  931: =item $request - Apache Request object [in] the request object for the current
  932:                  delete operation.
  933: 
  934: =item $user    - string [in]  The name of the user initiating the delete
  935:                  request.
  936: 
  937: =item $filename - string [in] The name of the file, relative to construction
  938:                   space, to delete.
  939: 
  940: =back
  941: 
  942: Returns:
  943:   1 - success.
  944:   0 - Failure.
  945: 
  946: =cut
  947: 
  948: sub Delete2 {
  949:   my ($request, $user, $filename) = @_;
  950: 
  951:   if(-e $filename) {
  952:     unless(unlink($filename)) {
  953:       $request->print('<font color="red">Error: '.$!.'</font>');
  954:       return 0;
  955:     }
  956:   } else {
  957:     $request->print('<p> No such file. </p></form');
  958:     return 0;
  959:   }
  960:   return 1;
  961: }
  962: 
  963: =pod
  964: 
  965: =item Copy2($request, $username, $dir, $oldfile, $newfile)
  966: 
  967:    Performs phase 2 of a copy.  The file is copied and the status 
  968:    of that copy is reported back to the user.
  969: 
  970: =over 4
  971: 
  972: =item $request - Apache request object [in]; the apache request currently
  973:                  being executed.
  974: 
  975: =item $username - string [in] Name of the user who is requesting the copy.
  976: 
  977: =item $dir - string [in] Directory path relative to the construction space
  978:              of the destination file.
  979: 
  980: =item $oldfile - string [in] Name of the source file.
  981: 
  982: =item $newfile - string [in] Name of the destination file.
  983: 
  984: 
  985: =back
  986: 
  987: Returns 0 failure, and 0 successs.
  988: 
  989: =cut
  990: 
  991: sub Copy2 {
  992:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
  993:     &Debug($request ,"Will try to copy $oldfile to $newfile");
  994:     if(-e $oldfile) {
  995: 	unless (copy($oldfile, $newfile)) {
  996: 	    $request->print('<font color="red"> copy Error: '.$!.'</font>');
  997: 	    return 0;
  998: 	} else {
  999: 	    unless (chmod(0660, $newfile)) {
 1000: 		$request->print('<font color="red"> chmod error: '.$!.'</font>');
 1001: 		return 0;
 1002: 	    }
 1003: 	    return 1;
 1004: 	}
 1005:     } else {
 1006: 	$request->print('<p> No such file </p>');
 1007: 	return 0;
 1008:     }
 1009:     return 1;
 1010: }
 1011: =pod
 1012: 
 1013: =item NewDir2($request, $user, $newdirectory)
 1014: 
 1015: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
 1016: 	reporting the results of that creation to the user.
 1017: 	
 1018: Parameters:
 1019: =over 4
 1020: 
 1021: =item $request  - Apache request object [in].  Object representing the current HTTP request.
 1022: 
 1023: =item $user - string [in] The name of the user that is initiating the request.
 1024: 
 1025: =item $newdirectory - string [in] The full path of the directory being created.
 1026: 
 1027: =back
 1028: 
 1029: Returns 0 - failure 1 - success.
 1030: 
 1031: =cut
 1032: 
 1033: sub NewDir2 {
 1034:   my ($request, $user, $newdirectory) = @_;
 1035:   
 1036:   unless(mkdir($newdirectory, 02770)) {
 1037:     $request->print('<font color="red">Error: '.$!.'</font>');
 1038:     return 0;
 1039:   }
 1040:   unless(chmod(02770, ($newdirectory))) {
 1041:       $request->print('<font color="red"> Error: '.$!.'</font>');
 1042:       return 0;
 1043:   }
 1044:   return 1;
 1045: }
 1046: 
 1047: =pod
 1048: 
 1049: =item phasetwo($r, $fn, $uname, $udom)
 1050: 
 1051:    Controls the phase 2 processing of file management
 1052:    requests for construction space.  In phase one, the user
 1053:    was asked to confirm the operation.  In phase 2, the operation
 1054:    is performed and the result is shown.
 1055: 
 1056:   The strategy is to break out the processing into specific action processors
 1057:   named action2 where action is the requested action and the 2 denotes 
 1058:   phase 2 processing.
 1059: 
 1060: Parameters:
 1061: 
 1062: =over 4
 1063: 
 1064: =item  $r     - Apache Request object [in] The request object for this httpd
 1065:            transaction.
 1066: 
 1067: =item  $fn    - string [in]  A filename indicating the object that is being
 1068:            manipulated.
 1069: 
 1070: =item  $uname - string [in] The name of the user initiating the file management
 1071:            request.
 1072: 
 1073: =item  $udom  - string  [in] The login domain of the user initiating the
 1074:            file management request.
 1075: =back
 1076: 
 1077: =cut
 1078: 
 1079: sub phasetwo {
 1080:     my ($r,$fn,$uname,$udom)=@_;
 1081:     
 1082:     &Debug($r, "loncfile - Entering phase 2 for $fn");
 1083:     
 1084:     # Break down the file into it's component pieces.
 1085:     
 1086:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
 1087:     my $dir=$1;			# Directory path
 1088:     my $main=$2;		# Filename.
 1089:     my $suffix=$3;		# Extension.
 1090:     
 1091:     my $dest;                   # On success this is where we'll go.
 1092:     
 1093:     &Debug($r, 
 1094: 	   "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
 1095:     &Debug($r,
 1096: 	   "    newfilename = ".$ENV{'form.newfilename'});
 1097: 
 1098:     my $conspace=$fn;
 1099:     
 1100:     &Debug($r, 
 1101: 	   "loncfile::phase2 Full construction space name: $conspace");
 1102:     
 1103:     &Debug($r, 
 1104: 	   "loncfie::phase2 action is $ENV{'form.action'}");
 1105:     
 1106:     # Select the appropriate processing sub.
 1107:     
 1108:     if ($ENV{'form.action'} eq 'rename') { # Rename.
 1109: 	if($ENV{'form.newfilename'}) {
 1110: 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
 1111: 		return;
 1112: 	    }
 1113: 	    # Prepend the directory to the new name to form the basis of the
 1114: 	    # url of the new resource.
 1115: 	    #
 1116: 	    $dest = $dir."/".$ENV{'form.newfilename'};
 1117: 	}
 1118:     } elsif ($ENV{'form.action'} eq 'delete') { 
 1119: 	if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
 1120: 	    return ;
 1121: 	}
 1122: 	# Once a resource is deleted, we just list the directory that
 1123: 	# previously held it.
 1124: 	#
 1125: 	$dest = $dir."/.";		# Parent dir.
 1126:     } elsif ($ENV{'form.action'} eq 'copy') { 
 1127: 	if($ENV{'form.newfilename'}) {
 1128: 	    if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
 1129: 		return
 1130: 		}
 1131: 	    $dest = $ENV{'form.newfilename'};
 1132:      
 1133: 	} else {
 1134: 	    $r->print('<p>No New filename specified</p></form>');
 1135: 	    return;
 1136: 	}
 1137: 	
 1138:     } elsif ($ENV{'form.action'} eq 'newdir') {
 1139: 	#
 1140: 	# Since the newfilename form field is construction space
 1141: 	# relative, ew need to prepend the current path; now in $fn.
 1142: 	#
 1143:         my $newdir= $fn.$ENV{'form.newfilename'};
 1144: 	if(!&NewDir2($r, $uname, $newdir)) {
 1145: 	    return;
 1146: 	}
 1147: 	$dest = $newdir."/"
 1148:     }
 1149:     #
 1150:     #  Substitute for priv for the first home in $dir to get our
 1151:     # construction space path.
 1152:     #
 1153:     $dest=&MakeFinalUrl($r,$dest);
 1154: 
 1155:     $r->print('<h3><a href="'.$dest.'">Done</a></h3>');
 1156: }
 1157: 
 1158: sub MakeFinalUrl {
 1159:     my($r,$dest)=@_;
 1160:     &Debug($r, "Final url is: $dest");
 1161:     $dest =~ s|/home/|/priv/|;
 1162:     $dest =~ s|/public_html||;
 1163: 
 1164:     my $base = &File::Basename::basename($dest);
 1165:     my $dpath= &File::Basename::dirname($dest);
 1166:     if ($base eq '.') { $base=''; }
 1167:     $dest = &HTML::Entities::encode($dpath.'/'.$base);
 1168: 
 1169:     &Debug($r, "Final url after rewrite: $dest");
 1170:     return $dest;
 1171: }
 1172: 
 1173: sub handler {
 1174: 
 1175:   $r=shift;
 1176: 
 1177: 
 1178:   &Debug($r, "loncfile.pm - handler entered");
 1179:   &Debug($r, " filename: ".$ENV{'form.filename'});
 1180:   &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
 1181: 
 1182:   my $fn;
 1183: 
 1184:   if ($ENV{'form.filename'}) {
 1185:       $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
 1186:       &Debug($r, "loncfile::handler - raw url: $fn");
 1187: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
 1188: #      $fn=~s/^http\:\/\/[^\/]+//;
 1189:       $fn=URLToPath($fn);
 1190:       &Debug($r, "loncfile::handler - doctored url: $fn");
 1191: 
 1192:   } else {
 1193:       &Debug($r, "loncfile::handler - no form.filename");
 1194:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1195:          ' unspecified filename for cfile', $r->filename); 
 1196:      return HTTP_NOT_FOUND;
 1197:   }
 1198: 
 1199:   unless ($fn) { 
 1200:       &Debug($r, "loncfile::handler - doctored url is empty");
 1201:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1202:          ' trying to cfile non-existing file', $r->filename); 
 1203:      return HTTP_NOT_FOUND;
 1204:   } 
 1205: 
 1206: # ----------------------------------------------------------- Start page output
 1207:   my $uname;
 1208:   my $udom;
 1209: 
 1210:   ($uname,$udom)=
 1211:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
 1212:   &Debug($r, 
 1213: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
 1214:   unless (($uname) && ($udom)) {
 1215:      $r->log_reason($uname.' at '.$udom.
 1216:          ' trying to manipulate file '.$ENV{'form.filename'}.
 1217:          ' ('.$fn.') - not authorized', 
 1218:          $r->filename); 
 1219:      return HTTP_NOT_ACCEPTABLE;
 1220:   }
 1221: 
 1222:   $fn=~s/\/\~(\w+)//;
 1223:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
 1224: 
 1225:   $r->content_type('text/html');
 1226:   $r->send_http_header;
 1227: 
 1228:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
 1229: 
 1230:   $r->print(
 1231:    '<body bgcolor="#FFFFFF"><img align="right" src="/adm/lonIcons/lonlogos.gif" />');
 1232: 
 1233:   
 1234:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
 1235:   
 1236:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
 1237:           $r->print('<h3><font color="red">Co-Author: '.$uname.' at '.$udom.
 1238:                '</font></h3>');
 1239:   }
 1240: 
 1241: 
 1242:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
 1243:   if ($ENV{'form.action'} eq 'delete') {
 1244:       
 1245:       $r->print('<h3>Delete</h3>');
 1246:   } elsif ($ENV{'form.action'} eq 'rename') {
 1247:       $r->print('<h3>Rename</h3>');
 1248:   } elsif ($ENV{'form.action'} eq 'newdir') {
 1249:       $r->print('<h3>New Directory</h3>');
 1250:   } elsif ($ENV{'form.action'} eq 'copy') {
 1251:       $r->print('<h3>Copy</h3>');
 1252:   } elsif ($ENV{'form.action'} eq 'newfile' ||
 1253: 	   $ENV{'form.action'} eq 'newhtmlfile' ||
 1254: 	   $ENV{'form.action'} eq 'newproblemfile') {
 1255:       $r->print('<h3>New Resource</h3>');
 1256:   } else {
 1257:      $r->print('<p>Unknown Action</p></body></html>');
 1258:      return OK;  
 1259:   }
 1260:   if ($ENV{'form.phase'} eq 'two') {
 1261:       &Debug($r, "loncfile::handler  entering phase2");
 1262:       &phasetwo($r,$fn,$uname,$udom);
 1263:   } else {
 1264:       &Debug($r, "loncfile::handler  entering phase1");
 1265:       &phaseone($r,$fn,$uname,$udom);
 1266:   }
 1267: 
 1268:   $r->print('</body></html>');
 1269:   return OK;  
 1270: }
 1271: 
 1272: 1;
 1273: __END__
 1274: 

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