File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.31: download - view: text, annotated - select for diffs
Thu Jun 19 20:49:13 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove bad characters from file names(BUG#1242)

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

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