File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.18: download - view: text, annotated - select for diffs
Tue Sep 10 02:31:26 2002 UTC (21 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Correct ad hoc mapping of urls to use HTTP::Entities::encode.

    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.18 2002/09/10 02:31:26 foxr 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 false.
  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: 
  327:   my $published = &PublicationPath($domain, $user, $dir, $file);
  328:   my $construct = &ConstructionPath($user, $dir, $file);
  329: 
  330:   # If the resource exists in either space indicate this fact.
  331:   # Note that the check for existence in resource space is stricter.
  332: 
  333:   my $result;    
  334:   if ( -e $published) {
  335:     $result.='<p><font color=red>Warning: target file exists, and has been published!</font></p>';
  336:   }
  337:   elsif ( -e $construct) {
  338:     $result.='<p><font color=red>Warning: target file exists!</font></p>';
  339:    }
  340: 
  341:   return $result;
  342: 
  343: }
  344: 
  345: =pod
  346: 
  347: =item checksuffix($old, $new)
  348:         
  349:   Determine if a resource filename suffix (the stuff after the .) would change
  350: as a result of this operation.
  351: 
  352:  Parameters:
  353: 
  354: =over 4
  355: 
  356: =item  $old   = string [in]  Previous filename.
  357: 
  358: =item  $new   = string [in]  Resultant filename.
  359: 
  360: =back
  361: 
  362:  Returns:
  363: 
  364: =over 4
  365: 
  366: =item    Empty string if everything worked.
  367: 
  368: =item    String containing an error message if there was a problem.
  369: 
  370: =back
  371: 
  372: =cut
  373: 
  374: sub checksuffix {
  375:     my ($old,$new) = @_;
  376:     my $result;
  377:     my $oldsuffix;
  378:     my $newsuffix;
  379:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
  380:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
  381:     if ($oldsuffix ne $newsuffix) {
  382: 	$result.=
  383:             '<p><font color=red>Warning: change of MIME type!</font></p>';
  384:     }
  385:     return $result;
  386: }
  387: =pod
  388: 
  389: =item CloseForm1($request, $user, $file)
  390: 
  391:    Close of a form on the successful completion of phase 1 processing
  392: 
  393: Parameters:
  394: 
  395: =over 4
  396: 
  397: =item  $request - Apache Request Object [in] - Apache server request object.
  398: 
  399: =item  $cancelurl - the url to go to on cancel.
  400: 
  401: =back
  402: 
  403: =cut
  404: 
  405: sub CloseForm1 {
  406:    my ($request,  $cancelurl) = @_;
  407: 
  408: 
  409:    &Debug($request, "Cancel url is: ".$cancelurl);
  410:    $request->print('<p><input type=submit value=Continue></p></form>');
  411:    $request->print('<form action="'.$cancelurl.
  412: 		   '" method=GET"><p><input type=submit value=Cancel><p></form>');
  413: 
  414: }
  415: 
  416: 
  417: =pod
  418: 
  419: =item CloseForm2($request, $user, $directory)
  420: 
  421:    Successfully close off the phase 2 form.
  422: 
  423: Parameters:
  424: 
  425: =over 4
  426: 
  427: =item   $request    - Apache Request object [in] - The request that is being
  428:                  executed.
  429: 
  430: =item   $user       - string [in] - Name of the user that is initiating the
  431:                  request.
  432: 
  433: =item   $directory  - string [in] - Directory in which the operation is 
  434:                  being done relative to the top level construction space
  435:                  directory.
  436: 
  437: =back
  438: 
  439: =cut
  440: 
  441: sub CloseForm2 {
  442:   my ($request, $user, $directory) = @_;
  443: 
  444:   $request->print('<h3><a=href="/priv/'.$user.$directory.'/">Done </a> </h3>');
  445: }
  446: 
  447: =pod
  448: 
  449: =item Rename1($request, $filename, $user, $domain, $dir)
  450:  
  451:    Perform phase 1 processing of the file rename operation.
  452: 
  453: Parameters:
  454: 
  455: =over 4
  456: 
  457: =item  $request   - Apache Request Object [in] The request object for the 
  458: current request.
  459: 
  460: =item  $filename  - The filename relative to construction space.
  461: 
  462: =item  $user      - Name of the user making the request.
  463: 
  464: =item  $domain    - User login domain.
  465: 
  466: =item  $dir       - Directory specification of the path to the file.
  467: 
  468: =back
  469: 
  470: Side effects:
  471: 
  472: =over 4
  473: 
  474: =item A new form is displayed prompting for confirmation.  The newfilename
  475: hidden field of this form is loaded with
  476: new filename relative to the current directory ($dir).
  477: 
  478: =back
  479: 
  480: =cut  
  481: 
  482: sub Rename1 {
  483:     my ($request, $filename, $user, $domain, $dir) = @_;
  484:     &Debug($request, "Username - ".$user." filename: ".$filename."\n");
  485:     my $conspace = $filename;
  486: 
  487:     my $cancelurl = "/priv/".$filename;
  488:     $cancelurl    =~ s/\/home\///;
  489:     $cancelurl    =~ s/\/public_html//;
  490:     
  491:     if(-e $conspace) {
  492: 	if($ENV{'form.newfilename'}) {
  493: 	    my $newfilename = $ENV{'form.newfilename'};
  494: 	    $request->print(&checksuffix($filename, $newfilename));
  495: 	    $request->print(&exists($user, $domain, $dir, $newfilename));
  496: 	    $request->print('<input type=hidden name=newfilename value="'.
  497: 			    $newfilename.
  498: 			    '"><p>Rename <tt>'.$filename.'</tt> to <tt>'.
  499: 			    $dir.'/'.$newfilename.'</tt>?</p>');
  500: 	    &CloseForm1($request, $cancelurl);
  501: 	} else {
  502: 	    $request->print('<p>No new filename specified</p></form>');
  503: 	    return;
  504: 	}
  505:     } else {
  506: 	$request->print('<p> No such File </p> </form>');
  507: 	return;
  508:     }
  509:     
  510: }
  511: =pod
  512: 
  513: =item Delete1
  514: 
  515:    Performs phase 1 processing of the delete operation.  In phase one
  516:   we just check to be sure the file exists.
  517: 
  518: Parameters:
  519: 
  520: =over 4
  521: 
  522: =item   $request   - Apache Request Object [in] request object for the current
  523:                 request.
  524: 
  525: =item   $user      - string [in] Name of session user.
  526: 
  527: 
  528: =item   $filename  - string [in] Name fo the file to be deleted:
  529:                 Filename is the full filesystem path to the file.
  530: 
  531: =back
  532: 
  533: =cut
  534: 
  535: sub Delete1 {
  536:   my ($request, $user,  $filename) = @_;
  537: 
  538:   my $cancelurl = '/priv/'.$filename;
  539:   $cancelurl    =~ s/\/home\///;
  540:   $cancelurl    =~ s/\/public_html//;
  541:   
  542: 
  543:   if( -e $filename) {
  544:     $request->print('<input type=hidden name=newfilename value="'.
  545: 		    $filename.'">');
  546:     $request->print('<p> Delete <tt>'.$filename.'</tt>?</p>');
  547:     &CloseForm1($request, $cancelurl);
  548:   } else {
  549:     $request->print('<p> No Such file: <tt>'.$filename.'</tt></p></form>');
  550:   }
  551: }
  552: 
  553: =pod
  554: 
  555: =item Copy1($request, $user, $domain, $filename, $newfilename)
  556: 
  557:    Performs phase 1 processing of the construction space copy command.
  558:    Ensure that the source file exists.  Ensure that a destination exists,
  559:    also warn if the destination already exists.
  560: 
  561: Parameters:
  562: 
  563: =over 4
  564: 
  565: =item   $request   - Apache Request Object [in] request object for the current 
  566:                 request.
  567: 
  568: =item   $user      - string [in]  Name of the user initiating the request.
  569: 
  570: =item   $domain    - string [in]  Domain the initiating user is logged in as
  571: 
  572: =item   $dir       - string [in]  Directory path.
  573: 
  574: =item   $filename  - string [in]  Source filename.
  575: 
  576: =item   $newfilename-string [in]  Destination filename.
  577: 
  578: =back
  579: 
  580: =cut
  581: 
  582: sub Copy1 {
  583:   my ($request, $user, $domain, $dir, $filename, $newfilename) = @_;
  584: 
  585:   my $cancelurl = "/priv/".$filename;
  586:   $cancelurl    =~ s/\/home\///;
  587:   $cancelurl    =~ s/\/public_html//;
  588:     
  589: 
  590: 
  591:   if(-e $filename) {
  592:     $request->print(&checksuffix($filename,$newfilename));
  593:     $request->print(&exists($user, $domain, $dir, $newfilename));
  594:     $request->print('<input type = hidden name = newfilename value = "'.
  595: 		    $dir.'/'.$newfilename.
  596: 		    '"><p>Copy <tt>'.$filename.'</tt> to'.
  597: 		    '<tt>'.$dir.'/'.$newfilename.'</tt>/?</p>');
  598:     &CloseForm1($request, $cancelurl);
  599:   } else {
  600:     $request->print('<p>No such file <tt>'.$filename.'</p></form>');
  601:   }
  602: }
  603: 
  604: =pod
  605: 
  606: =item NewDir1
  607:  
  608:   Does all phase 1 processing of directory creation:
  609:   Ensures that the user provides a new directory name,
  610:   and that the directory does not already exist.
  611: 
  612: Parameters:
  613: 
  614: =over 4
  615: 
  616: =item   $request  - Apache Request Object [in] - Server request object for the
  617:                current url.
  618: 
  619: =item   $username - Name of the user that is requesting the directory creation.
  620: 
  621: =item   $path     - current directory relative to construction space.
  622: 
  623: =item   $newdir   - Name of the directory to be created; path relative to the 
  624:                top level of construction space.
  625: =back
  626: 
  627: Side Effects:
  628: 
  629: =over 4
  630: 
  631: =item A new form is displayed.  Clicking on the confirmation button
  632: causes the newdir operation to transition into phase 2.  The hidden field
  633: "newfilename" is set with the construction space path to the new directory.
  634: 
  635: 
  636: =back
  637: 
  638: =cut
  639: 
  640: 
  641: sub NewDir1
  642: {
  643:   my ($request, $username, $path, $newdir) = @_;
  644: 
  645:   my $fullpath = '/home/'.$username.'/public_html/'.
  646:     $path.'/'.$newdir;
  647: 
  648:   my $cancelurl = '/priv/'.$username.'/'.$path;
  649: 
  650:   &Debug($request, "Full path is : ".$fullpath);
  651: 
  652:   if(-e $fullpath) {
  653:     $request->print('<p>Directory exists.</p></form>');
  654:   }
  655:   else {
  656:     $request->print('<input type=hidden name=newfilename value="'.
  657: 		    $newdir.'"><p>Make new directory <tt>'.
  658: 		    $path."/".$newdir.'</tt>?</p>');
  659:     &CloseForm1($request, $cancelurl);
  660: 
  661:   }
  662: }
  663: 
  664: =pod
  665: 
  666: =item phaseone($r, $fn, $uname, $udom)
  667: 
  668:   Peforms phase one processing of the request.  In phase one, error messages
  669: are returned if the request cannot be performed (e.g. attempts to manipulate
  670: files that are nonexistent).  If the operation can be performed, what is
  671: about to be done will be presented to the user for confirmation.  If the
  672: user confirms the request, then phase two is executed, the action 
  673: performed and reported to the user.
  674: 
  675:  Parameters:
  676: 
  677: =over 4
  678: 
  679: =item $r  - request object [in] - The Apache request being executed.
  680: 
  681: =item $fn = string [in] - The filename being manipulated by the 
  682:                              request.
  683: 
  684: =item $uname - string [in] Name of user logged in and doing this action.
  685: 
  686: =item $udom  - string [in] Domain name under which the user logged in. 
  687: 
  688: =back
  689: 
  690: =cut
  691: 
  692: sub phaseone {
  693:   my ($r,$fn,$uname,$udom)=@_;
  694:   
  695:   $fn=~m:(.*)/([^/]+)\.(\w+)$:;
  696:   my $dir=$1;
  697:   my $main=$2;
  698:   my $suffix=$3;
  699:   
  700:   #  my $conspace=ConstructionPathFromRelative($uname, $fn);
  701:   
  702:   
  703:   $r->print('<form action=/adm/cfile method=post>'.
  704: 	    '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
  705: 	    '<input type=hidden name=phase value=two>'.
  706: 	    '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
  707:   
  708:   if ($ENV{'form.action'} eq 'rename') {
  709:     
  710:     &Rename1($r, $fn, $uname, $udom, $dir);
  711:     
  712:   } elsif ($ENV{'form.action'} eq 'delete') { 
  713:     
  714:     &Delete1($r, $uname, $fn);
  715:     
  716:   } elsif ($ENV{'form.action'} eq 'copy') { 
  717:     if($ENV{'form.newfilename'}) {
  718:       my $newfilename = $ENV{'form.newfilename'};
  719:       &Copy1($r, $uname, $udom, $dir, $fn, $newfilename);
  720:     }else {
  721:       $r->print('<p>No new filename specified.</p></form>');
  722:     }
  723:   } elsif ($ENV{'form.action'} eq 'newdir') {
  724:     &NewDir1($r, $uname, $dir, $ENV{'form.newfilename'});
  725:   }
  726:   
  727: }
  728: 
  729: =pod
  730: 
  731: =item Rename2($request, $user, $directory, $oldfile, $newfile)
  732: 
  733: Performs phase 2 processing of a rename reequest.   This is where the
  734: actual rename is performed.
  735: 
  736: Parameters
  737: 
  738: =over 4
  739: 
  740: =item $request - Apache request object [in] The request being processed.
  741: 
  742: =item $user  - string [in] The name of the user initiating the request.
  743: 
  744: =item $directory - string [in] The name of the directory relative to the
  745:                  construction space top level of the renamed file.
  746: 
  747: =item $oldfile - Name of the file.
  748: 
  749: =item $newfile - Name of the new file.
  750: 
  751: =back
  752: 
  753: Returns:
  754: 
  755: =over 4
  756: 
  757: =item 1 Success.
  758: 
  759: =item 0 Failure.
  760: 
  761: =cut
  762: 
  763: sub Rename2 {
  764: 
  765:   my ($request, $user, $directory, $oldfile, $newfile) = @_;
  766: 
  767:   &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
  768: 	 " new file ".$newfile."\n");
  769:   &Debug($request, "Target is: ".$directory.'/'.
  770: 	 $newfile);
  771: 
  772:   if(-e $oldfile) {
  773:       unless(rename($oldfile,
  774: 		    $directory.'/'.$newfile)) {
  775: 	  $request->print('<font color=red>Error: '.$!.'</font>');
  776: 	  return 0;
  777:       } else {}
  778:   } else {
  779:       $request->print("<p> No such file: /home".$user.'/public_html'.
  780: 		      $oldfile.' </p></form>');
  781:       return 0;
  782:   }
  783:   return 1;
  784: }
  785: =pod
  786: 
  787: =item Delete2($request, $user, $filename)
  788: 
  789:   Performs phase two of a delete.  The user has confirmed that they want 
  790: to delete the selected file.   The file is deleted and the results of the
  791: delete attempt are indicated.
  792: 
  793: Parameters:
  794: 
  795: =over 4
  796: 
  797: =item $request - Apache Request object [in] the request object for the current
  798:                  delete operation.
  799: 
  800: =item $user    - string [in]  The name of the user initiating the delete
  801:                  request.
  802: 
  803: =item $filename - string [in] The name of the file, relative to construction
  804:                   space, to delete.
  805: 
  806: =back
  807: 
  808: Returns:
  809:   1 - success.
  810:   0 - Failure.
  811: 
  812: =cut
  813: 
  814: sub Delete2 {
  815:   my ($request, $user, $filename) = @_;
  816: 
  817:   if(-e $filename) {
  818:     unless(unlink($filename)) {
  819:       $request->print('<font color=red>Error: '.$!.'</font>');
  820:       return 0;
  821:     }
  822:   } else {
  823:     $request->print('<p> No such file. </form');
  824:     return 0;
  825:   }
  826:   return 1;
  827: }
  828: 
  829: =pod
  830: 
  831: =item Copy2($request, $username, $dir, $oldfile, $newfile)
  832: 
  833:    Performs phase 2 of a copy.  The file is copied and the status 
  834:    of that copy is reported back to the user.
  835: 
  836: =over 4
  837: 
  838: =item $request - Apache request object [in]; the apache request currently
  839:                  being executed.
  840: 
  841: =item $username - string [in] Name of the user who is requesting the copy.
  842: 
  843: =item $dir - string [in] Directory path relative to the construction space
  844:              of the destination file.
  845: 
  846: =item $oldfile - string [in] Name of the source file.
  847: 
  848: =item $newfile - string [in] Name of the destination file.
  849: 
  850: 
  851: =back
  852: 
  853: Returns 0 failure, and 0 successs.
  854: 
  855: =cut
  856: 
  857: sub Copy2 {
  858:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
  859:     &Debug($request ,"Will try to copy $oldfile to $newfile");
  860:     if(-e $oldfile) {
  861: 	unless (copy($oldfile, $newfile)) {
  862: 	    $request->print('<font color=red> copy Error: '.$!.'</font>');
  863: 	    return 0;
  864: 	} else {
  865: 	    unless (chmod(0660, $newfile)) {
  866: 		$request->print('<font color=red> chmod error: '.$!.'</font>');
  867: 		return 0;
  868: 	    }
  869: 	    return 1;
  870: 	}
  871:     } else {
  872: 	$request->print('<p> No such file </p>');
  873: 	return 0;
  874:     }
  875:     return 1;
  876: }
  877: =pod
  878: 
  879: =item NewDir2($request, $user, $newdirectory)
  880: 
  881: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
  882: 	reporting the results of that creation to the user.
  883: 	
  884: Parameters:
  885: =over 4
  886: 
  887: =item $request  - Apache request object [in].  Object representing the current HTTP request.
  888: 
  889: =item $user - string [in] The name of the user that is initiating the request.
  890: 
  891: =item $newdirectory - string [in] The full path of the directory being created.
  892: 
  893: =back
  894: 
  895: Returns 0 - failure 1 - success.
  896: 
  897: =cut
  898: 
  899: sub NewDir2 {
  900:   my ($request, $user, $newdirectory) = @_;
  901:   
  902:   unless(mkdir($newdirectory, 02770)) {
  903:     $request->print('<font color=red>Error: '.$!.'</font>');
  904:     return 0;
  905:   }
  906:   unless(chmod(02770, ($newdirectory))) {
  907:       $request->print('<font color=red> Error: '.$!.'</font>');
  908:       return 0;
  909:   }
  910:   return 1;
  911: }
  912: 
  913: =pod
  914: 
  915: =item phasetwo($r, $fn, $uname, $udom)
  916: 
  917:    Controls the phase 2 processing of file management
  918:    requests for construction space.  In phase one, the user
  919:    was asked to confirm the operation.  In phase 2, the operation
  920:    is performed and the result is shown.
  921: 
  922:   The strategy is to break out the processing into specific action processors
  923:   named action2 where action is the requested action and the 2 denotes 
  924:   phase 2 processing.
  925: 
  926: Parameters:
  927: 
  928: =over 4
  929: 
  930: =item  $r     - Apache Request object [in] The request object for this httpd
  931:            transaction.
  932: 
  933: =item  $fn    - string [in]  A filename indicating the object that is being
  934:            manipulated.
  935: 
  936: =item  $uname - string [in] The name of the user initiating the file management
  937:            request.
  938: 
  939: =item  $udom  - string  [in] The login domain of the user initiating the
  940:            file management request.
  941: =back
  942: 
  943: =cut
  944: 
  945: sub phasetwo {
  946:     my ($r,$fn,$uname,$udom)=@_;
  947:     
  948:     &Debug($r, "loncfile - Entering phase 2 for $fn");
  949:     
  950:     # Break down the file into it's component pieces.
  951:     
  952:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
  953:     my $dir=$1;			# Directory path
  954:     my $main=$2;		# Filename.
  955:     my $suffix=$3;		# Extension.
  956:     
  957:     my $dest;                   # On success this is where we'll go.
  958:     
  959:     &Debug($r, 
  960: 	   "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
  961:     &Debug($r,
  962: 	   "    newfilename = ".$ENV{'form.newfilename'});
  963: 
  964:     my $conspace=$fn;
  965:     
  966:     &Debug($r, 
  967: 	   "loncfile::phase2 Full construction space name: $conspace");
  968:     
  969:     &Debug($r, 
  970: 	   "loncfie::phase2 action is $ENV{'form.action'}");
  971:     
  972:     # Select the appropriate processing sub.
  973:     
  974:     if ($ENV{'form.action'} eq 'rename') { # Rename.
  975: 	if($ENV{'form.newfilename'}) {
  976: 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
  977: 		return;
  978: 	    }
  979: 	    # Prepend the directory to the new name to form the basis of the
  980: 	    # url of the new resource.
  981: 	    #
  982: 	    $dest = $dir."/".$ENV{'form.newfilename'};
  983: 	}
  984:     } elsif ($ENV{'form.action'} eq 'delete') { 
  985: 	if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
  986: 	    return ;
  987: 	}
  988: 	# Once a resource is deleted, we just list the directory that
  989: 	# previously held it.
  990: 	#
  991: 	$dest = $dir."/";		# Parent dir.
  992:     } elsif ($ENV{'form.action'} eq 'copy') { 
  993: 	if($ENV{'form.newfilename'}) {
  994: 	    if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
  995: 		return
  996: 		}
  997: 	    $dest = $ENV{'form.newfilename'};
  998:      
  999: 	} else {
 1000: 	    $r->print('<p>No New filename specified</form>');
 1001: 	    return;
 1002: 	}
 1003: 	
 1004:     } elsif ($ENV{'form.action'} eq 'newdir') {
 1005: 	#
 1006: 	# Since the newfilename form field is construction space
 1007: 	# relative, ew need to prepend the current path; now in $fn.
 1008: 	#
 1009:         my $newdir= $fn.$ENV{'form.newfilename'};
 1010: 	if(!&NewDir2($r, $uname, $newdir)) {
 1011: 	    return;
 1012: 	}
 1013: 	$dest = $newdir."/"
 1014:     }
 1015:     #
 1016:     #  Substitute for priv for the first home in $dir to get our
 1017:     # construction space path.
 1018:     #
 1019:     &Debug($r, "Final url is: $dest");
 1020:     $dest =~ s/\/home\//\/priv\//;
 1021:     $dest =~ s/\/public_html//;
 1022:     
 1023:     my $base = &File::Basename::basename($dest);
 1024:     my $dpath= &File::Basename::dirname($dest);
 1025:     $dest = &HTML::Entities::encode($dpath.'/'.$base);
 1026: 
 1027: 
 1028:     &Debug($r, "Final url after rewrite: $dest");
 1029: 
 1030:     $r->print('<h3><a href="'.$dest.'">Done</a></h3>');
 1031: }
 1032: 
 1033: sub handler {
 1034: 
 1035:   $r=shift;
 1036: 
 1037: 
 1038:   &Debug($r, "loncfile.pm - handler entered");
 1039:   &Debug($r, " filename: ".$ENV{'form.filename'});
 1040:   &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
 1041: 
 1042:   my $fn;
 1043: 
 1044:   if ($ENV{'form.filename'}) {
 1045:       $fn=$ENV{'form.filename'};
 1046:       &Debug($r, "loncfile::handler - raw url: $fn");
 1047: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
 1048: #      $fn=~s/^http\:\/\/[^\/]+//;
 1049:       $fn=URLToPath($fn);
 1050:       &Debug($r, "loncfile::handler - doctored url: $fn");
 1051: 
 1052:   } else {
 1053:       &Debug($r, "loncfile::handler - no form.filename");
 1054:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1055:          ' unspecified filename for cfile', $r->filename); 
 1056:      return HTTP_NOT_FOUND;
 1057:   }
 1058: 
 1059:   unless ($fn) { 
 1060:       &Debug($r, "loncfile::handler - doctored url is empty");
 1061:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
 1062:          ' trying to cfile non-existing file', $r->filename); 
 1063:      return HTTP_NOT_FOUND;
 1064:   } 
 1065: 
 1066: # ----------------------------------------------------------- Start page output
 1067:   my $uname;
 1068:   my $udom;
 1069: 
 1070:   ($uname,$udom)=
 1071:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
 1072:   &Debug($r, 
 1073: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
 1074:   unless (($uname) && ($udom)) {
 1075:      $r->log_reason($uname.' at '.$udom.
 1076:          ' trying to manipulate file '.$ENV{'form.filename'}.
 1077:          ' ('.$fn.') - not authorized', 
 1078:          $r->filename); 
 1079:      return HTTP_NOT_ACCEPTABLE;
 1080:   }
 1081: 
 1082:   $fn=~s/\/\~(\w+)//;
 1083:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
 1084: 
 1085:   $r->content_type('text/html');
 1086:   $r->send_http_header;
 1087: 
 1088:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
 1089: 
 1090:   $r->print(
 1091:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
 1092: 
 1093:   
 1094:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
 1095:   
 1096:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
 1097:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
 1098:                '</font></h3>');
 1099:   }
 1100: 
 1101: 
 1102:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
 1103:   if ($ENV{'form.action'} eq 'delete') {
 1104:       
 1105:       $r->print('<h3>Delete</h3>');
 1106:   } elsif ($ENV{'form.action'} eq 'rename') {
 1107:       $r->print('<h3>Rename</h3>');
 1108:   } elsif ($ENV{'form.action'} eq 'newdir') {
 1109:       $r->print('<h3>New Directory</h3>');
 1110:   } elsif ($ENV{'form.action'} eq 'copy') {
 1111:       $r->print('<h3>Copy</h3>');
 1112:   } else {
 1113:      $r->print('<p>Unknown Action</body></html>');
 1114:      return OK;  
 1115:   }
 1116:   if ($ENV{'form.phase'} eq 'two') {
 1117:       &Debug($r, "loncfile::handler  entering phase2");
 1118:       &phasetwo($r,$fn,$uname,$udom);
 1119:   } else {
 1120:       &Debug($r, "loncfile::handler  entering phase1");
 1121:       &phaseone($r,$fn,$uname,$udom);
 1122:   }
 1123: 
 1124:   $r->print('</body></html>');
 1125:   return OK;  
 1126: }
 1127: 
 1128: 1;
 1129: __END__
 1130: 

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