Annotation of loncom/publisher/loncfile.pm, revision 1.40

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to rename files, etc, in construction space
                      3: #
1.9       foxr        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: #
1.24      albertel   11: #
1.40    ! www        12: # $Id: loncfile.pm,v 1.39 2003/08/04 20:34:19 www Exp $
1.7       albertel   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: #
1.12      foxr       36: =pod
                     37: 
                     38: =head1 NAME
                     39: 
                     40: Apache::loncfile - Construction space file management.
                     41: 
                     42: =head1 SYNOPSIS
                     43:  
                     44:  Content handler for buttons on the top frame of the construction space 
                     45: directory.
                     46: 
                     47: =head1 INTRODUCTION
                     48: 
                     49:   loncfile is invoked when buttons in the top frame of the construction 
1.17      harris41   50: space directory listing are clicked.   All operations proceed in two phases.
1.12      foxr       51: The first phase describes to the user exactly what will be done.  If the user
                     52: confirms the operation, the second phase commits the operation and indicates
                     53: completion.  When the user dismisses the output of phase2, they are returned to
                     54: an "appropriate" directory listing in general.
                     55: 
                     56:     This is part of the LearningOnline Network with CAPA project
                     57: described at http://www.lon-capa.org.
                     58: 
                     59: =head2 Subroutines
                     60: 
                     61: =cut
1.1       www        62: 
                     63: package Apache::loncfile;
                     64: 
                     65: use strict;
                     66: use Apache::File;
1.15      foxr       67: use File::Basename;
1.1       www        68: use File::Copy;
1.18      foxr       69: use HTML::Entities();
1.1       www        70: use Apache::Constants qw(:common :http :methods);
                     71: use Apache::loncacc;
1.9       foxr       72: use Apache::Log ();
1.15      foxr       73: use Apache::lonnet;
1.33      www        74: use Apache::loncommon();
1.9       foxr       75: 
1.10      foxr       76: my $DEBUG=0;
                     77: my $r;				# Needs to be global for some stuff RF.
1.12      foxr       78: 
                     79: =pod
                     80: 
                     81: =item Debug($request, $message)
                     82: 
1.17      harris41   83:   If debugging is enabled puts out a debugging message determined by the
1.12      foxr       84:   caller.  The debug message goes to the Apache error log file. Debugging
1.17      harris41   85:   is enabled by setting the module global DEBUG variable to nonzero (TRUE).
1.12      foxr       86: 
                     87:  Parameters:
                     88: 
                     89: =over 4
                     90:  
1.17      harris41   91: =item $request - The current request operation.
1.12      foxr       92: 
1.17      harris41   93: =item $message - The message to put in the log file.
1.12      foxr       94: 
                     95: =back
                     96:   
                     97:  Returns:
                     98:    nothing.
                     99: 
                    100: =cut
                    101: 
1.9       foxr      102: sub Debug {
1.12      foxr      103:   
                    104:   # Marshall the parameters.
                    105:   
                    106:   my $r       = shift;
                    107:   my $log     = $r->log;
                    108:   my $message = shift;
                    109:   
1.22      albertel  110:   # Put out the indicated message butonly if DEBUG is true.
1.12      foxr      111:   
                    112:   if ($DEBUG) {
                    113:     $log->debug($message);
                    114:   }
                    115: }
                    116: 
                    117: =pod
                    118: 
                    119: =item URLToPath($url)
                    120: 
                    121:   Convert a URL to a file system path.
                    122:   
                    123:   In order to manipulate the construction space objects, it is necessary
                    124:   to access url identified objects a filespace objects.  This function
                    125:   translates a construction space URL to a file system path.
                    126:  Parameters:
                    127: 
                    128: =over 4
                    129: 
                    130: =item  Url    - string [in] The url to convert.
                    131:   
                    132: =back
                    133:   
                    134:  Returns:
                    135: 
                    136: =over 4
                    137: 
1.16      harris41  138: =item  The corresponding file system path. 
1.12      foxr      139: 
                    140: =back
                    141: 
                    142: Global References
                    143: 
                    144: =over 4
                    145: 
                    146: =item  $r      - Request object [in] Referenced in the &Debug calls.
                    147: 
                    148: =back
                    149: 
                    150: =cut
                    151: 
                    152: sub URLToPath {
                    153:   my $Url = shift;
                    154:   &Debug($r, "UrlToPath got: $Url");
1.37      www       155:   $Url=~ s/\/+/\//g;
1.12      foxr      156:   $Url=~ s/^http\:\/\/[^\/]+//;
1.35      www       157:   $Url=~ s/^\///;
                    158:   $Url=~ s/(\~|priv\/)(\w+)\//\/home\/$2\/public_html\//;
1.12      foxr      159:   &Debug($r, "Returning $Url \n");
                    160:   return $Url;
                    161: }
                    162: 
1.36      www       163: sub url {
                    164:     my $fn=shift;
                    165:     $fn=~s/^\/home\/(\w+)\/public\_html/\/priv\/$1/;
                    166:     return $fn;
                    167: }
                    168: 
                    169: sub display {
                    170:     my $fn=shift;
                    171:     $fn=~s/^\/home\/(\w+)\/public\_html//;
                    172:     return '<tt>'.$fn.'</tt>';
                    173: }
                    174: 
1.12      foxr      175: =pod
                    176: 
1.39      www       177: =item exists($user, $domain, $file)
1.12      foxr      178: 
1.17      harris41  179:    Determine if a resource file name has been published or exists
1.12      foxr      180:    in the construction space.
                    181: 
                    182:  Parameters:
                    183: 
                    184: =over 4
                    185: 
                    186: =item  $user   - string [in] - Name of the user for which to check.
                    187: 
                    188: =item  $domain - string [in] - Name of the domain in which the resource
                    189:                           might have been published.
                    190: 
                    191: =item  $file   - string [in] - Name of the file.
                    192: 
                    193: =back
                    194: 
                    195: Returns:
                    196: 
                    197: =over 4
                    198: 
                    199: =item  string - Either where the resource exists as an html string that can
                    200:            be embedded in a dialog or an empty string if the resource
                    201:            does not exist.
                    202:   
                    203: =back
                    204: 
                    205: =cut
                    206: 
1.8       albertel  207: sub exists {
1.39      www       208:   my ($user, $domain, $construct) = @_;
                    209:   my $published=$construct;
                    210:   $published=~
                    211: s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//;
                    212:   my $result='';    
1.21      albertel  213:   if ( -d $construct ) {
1.39      www       214:       return 'Error: destination for operation is an existing directory.';
1.21      albertel  215:   }
1.12      foxr      216:   if ( -e $published) {
1.23      albertel  217:       $result.='<p><font color="red">Warning: target file exists, and has been published!</font></p>';
1.36      www       218:   } elsif ( -e $construct) {
1.23      albertel  219:       $result.='<p><font color="red">Warning: target file exists!</font></p>';
1.21      albertel  220:   }
1.12      foxr      221:   return $result;
                    222: 
1.8       albertel  223: }
                    224: 
1.12      foxr      225: =pod
                    226: 
                    227: =item checksuffix($old, $new)
                    228:         
                    229:   Determine if a resource filename suffix (the stuff after the .) would change
                    230: as a result of this operation.
                    231: 
                    232:  Parameters:
                    233: 
                    234: =over 4
                    235: 
                    236: =item  $old   = string [in]  Previous filename.
                    237: 
                    238: =item  $new   = string [in]  Resultant filename.
                    239: 
                    240: =back
                    241: 
                    242:  Returns:
                    243: 
                    244: =over 4
                    245: 
1.17      harris41  246: =item    Empty string if everything worked.
1.12      foxr      247: 
                    248: =item    String containing an error message if there was a problem.
                    249: 
                    250: =back
                    251: 
                    252: =cut
                    253: 
1.8       albertel  254: sub checksuffix {
                    255:     my ($old,$new) = @_;
                    256:     my $result;
                    257:     my $oldsuffix;
                    258:     my $newsuffix;
                    259:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
                    260:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
                    261:     if ($oldsuffix ne $newsuffix) {
1.12      foxr      262: 	$result.=
1.23      albertel  263:             '<p><font color="red">Warning: change of MIME type!</font></p>';
1.8       albertel  264:     }
                    265:     return $result;
                    266: }
1.32      albertel  267: 
                    268: sub cleanDest {
                    269:     my ($request,$dest)=@_;
                    270:     #remove bad characters
                    271:     if  ($dest=~/[\#\?&]/) {
                    272: 	$request->print("<p><font color=\"red\">Invalid characters in requested name have been removed.</font></p>");
                    273: 	$dest=~s/[\#\?&]//g;
                    274:     }
                    275:     return $dest;
                    276: }
                    277: 
1.36      www       278: sub relativeDest {
                    279:     my ($fn,$newfilename,$uname)=@_;
                    280:     if ($newfilename=~/^\//) {
                    281: # absolute, simply add path
                    282: 	$newfilename='/home/'.$uname.'/public_html/';
                    283:     } else {
                    284: 	my $dir=$fn;
                    285: 	$dir=~s/\/[^\/]+$//;
                    286: 	$newfilename=$dir.'/'.$newfilename;
                    287:     }
                    288:     $newfilename=~s://+:/:g; # remove duplicate /
                    289:     while ($newfilename=~m:/\.\./:) {
                    290: 	$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    291:     }
                    292:     return $newfilename;
                    293: }
                    294: 
1.12      foxr      295: =pod
                    296: 
                    297: =item CloseForm1($request, $user, $file)
                    298: 
                    299:    Close of a form on the successful completion of phase 1 processing
                    300: 
                    301: Parameters:
                    302: 
                    303: =over 4
                    304: 
                    305: =item  $request - Apache Request Object [in] - Apache server request object.
                    306: 
1.13      foxr      307: =item  $cancelurl - the url to go to on cancel.
1.12      foxr      308: 
                    309: =back
                    310: 
                    311: =cut
                    312: 
                    313: sub CloseForm1 {
1.36      www       314:    my ($request,  $fn) = @_;
1.23      albertel  315:    $request->print('<p><input type="submit" value="Continue" /></p></form>');
1.36      www       316:    $request->print('<form action="'.&url($fn).
                    317:      '" method="POST"><p><input type="submit" value="Cancel" /></p></form>');
1.12      foxr      318: }
                    319: 
                    320: 
                    321: =pod
                    322: 
                    323: =item CloseForm2($request, $user, $directory)
                    324: 
                    325:    Successfully close off the phase 2 form.
                    326: 
                    327: Parameters:
                    328: 
                    329: =over 4
                    330: 
                    331: =item   $request    - Apache Request object [in] - The request that is being
                    332:                  executed.
                    333: 
                    334: =item   $user       - string [in] - Name of the user that is initiating the
                    335:                  request.
                    336: 
                    337: =item   $directory  - string [in] - Directory in which the operation is 
                    338:                  being done relative to the top level construction space
                    339:                  directory.
                    340: 
                    341: =back
                    342: 
                    343: =cut
                    344: 
                    345: sub CloseForm2 {
1.36      www       346:   my ($request, $user, $fn) = @_;
                    347:   $request->print('<h3><a href="'.&url($fn).'/">Done</a></h3>');
1.12      foxr      348: }
                    349: 
                    350: =pod
                    351: 
                    352: =item Rename1($request, $filename, $user, $domain, $dir)
                    353:  
                    354:    Perform phase 1 processing of the file rename operation.
                    355: 
                    356: Parameters:
                    357: 
                    358: =over 4
                    359: 
                    360: =item  $request   - Apache Request Object [in] The request object for the 
                    361: current request.
                    362: 
                    363: =item  $filename  - The filename relative to construction space.
                    364: 
                    365: =item  $user      - Name of the user making the request.
                    366: 
                    367: =item  $domain    - User login domain.
                    368: 
                    369: =item  $dir       - Directory specification of the path to the file.
                    370: 
                    371: =back
                    372: 
                    373: Side effects:
                    374: 
                    375: =over 4
                    376: 
                    377: =item A new form is displayed prompting for confirmation.  The newfilename
                    378: hidden field of this form is loaded with
                    379: new filename relative to the current directory ($dir).
                    380: 
                    381: =back
                    382: 
                    383: =cut  
                    384: 
                    385: sub Rename1 {
1.36      www       386:     my ($request, $user, $domain, $fn, $newfilename) = @_;
                    387: 
                    388:     if(-e $fn) {
                    389: 	if($newfilename) {
1.29      albertel  390: 	    if ($newfilename =~ m|/[^\.]+$|) {
1.36      www       391: 		#no extension add on original extension
                    392: 		if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
1.24      albertel  393: 		    $newfilename.='.'.$1;
                    394: 		}
                    395: 	    }
1.36      www       396: 	    $request->print(&checksuffix($fn, $newfilename));
1.27      albertel  397: 	    #renaming a dir, delete the trailing /
1.39      www       398:             #remove second to last element for current dir
                    399: 	    if (-d $fn) {
                    400: 		$newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
1.27      albertel  401: 	    }
1.39      www       402: 	    my $return=&exists($user, $domain, $newfilename);
1.21      albertel  403: 	    $request->print($return);
                    404: 	    if ($return =~/^Error:/) {
1.36      www       405: 		$request->print('<br /><a href="'.&url($fn).'">Cancel</a>');
1.21      albertel  406: 		return;
                    407: 	    }
1.23      albertel  408: 	    $request->print('<input type="hidden" name="newfilename" value="'.
1.12      foxr      409: 			    $newfilename.
1.36      www       410: 			    '" /><p>Rename '.&display($fn).
                    411: 			    '</tt><br />to '.&display($newfilename).'?</p>');
                    412: 	    &CloseForm1($request, $fn);
1.12      foxr      413: 	} else {
1.36      www       414: 	    $request->print('<p>No new filename specified.</p></form>');
1.12      foxr      415: 	    return;
                    416: 	}
                    417:     } else {
1.36      www       418: 	$request->print('<p> No such file: '.&display($fn).'</p></form>');
1.12      foxr      419: 	return;
                    420:     }
                    421:     
                    422: }
                    423: =pod
                    424: 
                    425: =item Delete1
                    426: 
                    427:    Performs phase 1 processing of the delete operation.  In phase one
                    428:   we just check to be sure the file exists.
                    429: 
                    430: Parameters:
                    431: 
                    432: =over 4
                    433: 
1.36      www       434: =item   $request   - Apache Request Object [in] request object for the current 
1.12      foxr      435:                 request.
                    436: 
1.36      www       437: =item   $user      - string [in]  Name of the user initiating the request.
1.12      foxr      438: 
1.36      www       439: =item   $domain    - string [in]  Domain the initiating user is logged in as
1.13      foxr      440: 
1.36      www       441: =item   $filename  - string [in]  Source filename.
1.12      foxr      442: 
                    443: =back
                    444: 
                    445: =cut
                    446: 
                    447: sub Delete1 {
1.36      www       448:   my ($request, $user, $domain, $fn) = @_;
1.12      foxr      449: 
1.36      www       450:   if( -e $fn) {
1.23      albertel  451:     $request->print('<input type="hidden" name="newfilename" value="'.
1.36      www       452: 		    $fn.'"/>');
                    453:     $request->print('<p>Delete '.&display($fn).'?</p>');
                    454:     &CloseForm1($request, $fn);
1.12      foxr      455:   } else {
1.36      www       456:     $request->print('<p>No such file: '.&display($fn).'</p></form>');
1.12      foxr      457:   }
                    458: }
                    459: 
                    460: =pod
                    461: 
                    462: =item Copy1($request, $user, $domain, $filename, $newfilename)
                    463: 
                    464:    Performs phase 1 processing of the construction space copy command.
1.17      harris41  465:    Ensure that the source file exists.  Ensure that a destination exists,
                    466:    also warn if the destination already exists.
1.12      foxr      467: 
                    468: Parameters:
                    469: 
                    470: =over 4
                    471: 
                    472: =item   $request   - Apache Request Object [in] request object for the current 
                    473:                 request.
                    474: 
                    475: =item   $user      - string [in]  Name of the user initiating the request.
                    476: 
                    477: =item   $domain    - string [in]  Domain the initiating user is logged in as
                    478: 
1.36      www       479: =item   $fn  - string [in]  Source filename.
1.12      foxr      480: 
                    481: =item   $newfilename-string [in]  Destination filename.
                    482: 
                    483: =back
                    484: 
                    485: =cut
                    486: 
                    487: sub Copy1 {
1.36      www       488:   my ($request, $user, $domain, $fn, $newfilename) = @_;
1.12      foxr      489: 
1.36      www       490:   if(-e $fn) {
                    491:     $request->print(&checksuffix($fn,$newfilename));
1.39      www       492:     my $return=&exists($user, $domain, $newfilename);
1.21      albertel  493:     $request->print($return);
                    494:     if ($return =~/^Error:/) {
1.36      www       495: 	$request->print('<br /><a href="'.&url($fn).'">Cancel</a>');
1.21      albertel  496: 	return;
                    497:     }
1.23      albertel  498:     $request->print('<input type = "hidden" name = "newfilename" value = "'.
1.36      www       499: 		    $newfilename.
                    500: 		    '" /><p>Copy '.&display($fn).'<br />to '.
                    501: 		    &display($newfilename).'?</p>');
                    502:     &CloseForm1($request, $fn);
1.12      foxr      503:   } else {
1.36      www       504:     $request->print('<p>No such file: '.&display($fn).'</p></form>');
1.12      foxr      505:   }
1.19      albertel  506: }
                    507: 
                    508: =pod
                    509: 
1.12      foxr      510: =item NewDir1
                    511:  
                    512:   Does all phase 1 processing of directory creation:
                    513:   Ensures that the user provides a new directory name,
                    514:   and that the directory does not already exist.
                    515: 
                    516: Parameters:
                    517: 
                    518: =over 4
                    519: 
                    520: =item   $request  - Apache Request Object [in] - Server request object for the
1.17      harris41  521:                current url.
1.12      foxr      522: 
                    523: =item   $username - Name of the user that is requesting the directory creation.
                    524: 
1.36      www       525: =item $domain - Domain user is in
                    526: 
                    527: =item   $fn     - source file.
1.12      foxr      528: 
                    529: =item   $newdir   - Name of the directory to be created; path relative to the 
                    530:                top level of construction space.
                    531: =back
                    532: 
                    533: Side Effects:
                    534: 
                    535: =over 4
                    536: 
                    537: =item A new form is displayed.  Clicking on the confirmation button
                    538: causes the newdir operation to transition into phase 2.  The hidden field
                    539: "newfilename" is set with the construction space path to the new directory.
                    540: 
                    541: 
                    542: =back
                    543: 
                    544: =cut
                    545: 
                    546: 
                    547: sub NewDir1
                    548: {
1.36      www       549:   my ($request, $username, $domain, $fn, $newfilename) = @_;
1.12      foxr      550: 
1.39      www       551:   my $result=&exists($username,$domain,$newfilename);
                    552:   if ($result) {
                    553:     $request->print('<font color="red">'.$result.'</font></form>');
                    554:   } else {
1.23      albertel  555:     $request->print('<input type="hidden" name="newfilename" value="'.
1.36      www       556: 		    $newfilename.'" /><p>Make new directory '.
                    557: 		    &display($newfilename).'?</p>');
                    558:     &CloseForm1($request, $fn);
1.12      foxr      559:   }
                    560: }
                    561: 
                    562: =pod
                    563: 
1.22      albertel  564: =item NewFile1
                    565:  
                    566:   Does all phase 1 processing of file creation:
                    567:   Ensures that the user provides a new filename, adds proper extension
                    568:   if needed and that the file does not already exist, if it is a html,
                    569:   problem, page, or sequence, it then creates a form link to hand the
                    570:   actual creation off to the proper handler.
                    571: 
                    572: Parameters:
                    573: 
                    574: =over 4
                    575: 
                    576: =item   $request  - Apache Request Object [in] - Server request object for the
                    577:                current url.
                    578: 
                    579: =item   $username - Name of the user that is requesting the directory creation.
                    580: 
                    581: =item   $domain   - Name of the domain of the user
                    582: 
1.36      www       583: =item   $fn      - Source file name
1.22      albertel  584: 
                    585: =item   $newfilename
                    586:                   - Name of the file to be created; no path information
                    587: =back
                    588: 
                    589: Side Effects:
                    590: 
                    591: =over 4
                    592: 
                    593: =item 2 new forms are displayed.  Clicking on the confirmation button
                    594: causes the browser to attempt to load the specfied URL, allowing the
1.36      www       595: proper handler to take care of file creation. There is also a Cancel
1.22      albertel  596: button which returns you to the driectory listing you came from
                    597: 
                    598: =back
                    599: 
                    600: =cut
                    601: 
                    602: 
                    603: sub NewFile1 {
1.36      www       604:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.22      albertel  605: 
                    606:     if ($ENV{'form.action'} =~ /new(.+)file/) {
                    607: 	my $extension=$1;
1.40    ! www       608: 
        !           609:         ##Informs User (name).(number).(extension) not allowed 
        !           610: 	if($newfilename =~ /\.(\d+)\.(\w+)$/){
        !           611: 	    $r->print('<font color="red">'.$newfilename.
        !           612: 		      ' - Bad Filename<br />(name).(number).(extension)'.
        !           613: 		      ' Not Allowed</font>');
        !           614: 	    return;
        !           615: 	}
1.22      albertel  616: 	if ($newfilename !~ /\Q.$extension\E$/) {
1.26      albertel  617: 	    if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) {
                    618: 		#already has an extension strip it and add in expected one
                    619: 		$newfilename =~ s|.([^\.]+)$||;
                    620: 	    }
1.22      albertel  621: 	    $newfilename.=".$extension";
                    622: 	}
1.31      albertel  623:     }
1.39      www       624:     my $result=&exists($user,$domain,$newfilename);
                    625:     if($result) {
                    626: 	$request->print('<font color="red">'.$result.'</font></form>');
                    627:     } else {
1.36      www       628: 	$request->print('<p>Make new file '.&display($newfilename).'?</p>');
1.22      albertel  629: 	$request->print('</form>');
1.36      www       630: 	$request->print('<form action="'.&url($newfilename).
1.30      albertel  631: 			'" method="POST"><p><input type="submit" value="Continue" /></p></form>');
1.36      www       632: 	$request->print('<form action="'.&url($fn).
1.30      albertel  633: 			'" method="POST"><p><input type="submit" value="Cancel" /></p></form>');
1.22      albertel  634:     }
                    635: }
                    636: 
                    637: =pod
                    638: 
1.12      foxr      639: =item phaseone($r, $fn, $uname, $udom)
                    640: 
                    641:   Peforms phase one processing of the request.  In phase one, error messages
                    642: are returned if the request cannot be performed (e.g. attempts to manipulate
                    643: files that are nonexistent).  If the operation can be performed, what is
                    644: about to be done will be presented to the user for confirmation.  If the
                    645: user confirms the request, then phase two is executed, the action 
                    646: performed and reported to the user.
                    647: 
                    648:  Parameters:
                    649: 
                    650: =over 4
                    651: 
                    652: =item $r  - request object [in] - The Apache request being executed.
                    653: 
                    654: =item $fn = string [in] - The filename being manipulated by the 
                    655:                              request.
                    656: 
                    657: =item $uname - string [in] Name of user logged in and doing this action.
                    658: 
1.17      harris41  659: =item $udom  - string [in] Domain name under which the user logged in. 
1.12      foxr      660: 
                    661: =back
                    662: 
                    663: =cut
1.8       albertel  664: 
1.1       www       665: sub phaseone {
1.12      foxr      666:   my ($r,$fn,$uname,$udom)=@_;
                    667:   
1.36      www       668:   my $newfilename=&cleanDest($r,$ENV{'form.newfilename'});
                    669:   $newfilename=&relativeDest($fn,$newfilename,$uname);
1.32      albertel  670: 
1.23      albertel  671:   $r->print('<form action="/adm/cfile" method="post">'.
1.36      www       672:       '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
                    673:       '<input type="hidden" name="phase" value="two" />'.
                    674:       '<input type="hidden" name="action" value="'.$ENV{'form.action'}.'" />');
1.12      foxr      675:   
                    676:   if ($ENV{'form.action'} eq 'rename') {
1.36      www       677:       &Rename1($r, $uname, $udom, $fn, $newfilename);
1.12      foxr      678:   } elsif ($ENV{'form.action'} eq 'delete') { 
1.36      www       679:       &Delete1($r, $uname, $udom, $fn);
1.12      foxr      680:   } elsif ($ENV{'form.action'} eq 'copy') { 
1.36      www       681:       if($newfilename) {
                    682: 	  &Copy1($r, $uname, $udom, $fn, $newfilename);
                    683:       } else {
                    684: 	  $r->print('<p>No new filename specified.</p></form>');
                    685:       }
1.12      foxr      686:   } elsif ($ENV{'form.action'} eq 'newdir') {
1.36      www       687:       &NewDir1($r, $uname, $udom, $fn, $newfilename);
1.22      albertel  688:   }  elsif ($ENV{'form.action'} eq 'newfile' ||
                    689: 	    $ENV{'form.action'} eq 'newhtmlfile' ||
1.28      www       690: 	    $ENV{'form.action'} eq 'newproblemfile' ||
                    691:             $ENV{'form.action'} eq 'newpagefile' ||
1.30      albertel  692:             $ENV{'form.action'} eq 'newsequencefile' ||
1.34      www       693:             $ENV{'form.action'} eq 'newrightsfile' ||
                    694:             $ENV{'form.action'} eq 'newstyfile' ||
1.30      albertel  695:             $ENV{'form.action'} eq 'Select Action') {
1.36      www       696:       if ($newfilename) {
                    697: 	  &NewFile1($r, $uname, $udom, $fn, $newfilename);
1.25      albertel  698:       } else {
                    699: 	  $r->print('<p>No new filename specified.</p></form>');
1.22      albertel  700:       }
1.12      foxr      701:   }
                    702: }
                    703: 
                    704: =pod
                    705: 
                    706: =item Rename2($request, $user, $directory, $oldfile, $newfile)
                    707: 
1.17      harris41  708: Performs phase 2 processing of a rename reequest.   This is where the
1.12      foxr      709: actual rename is performed.
                    710: 
                    711: Parameters
                    712: 
                    713: =over 4
                    714: 
                    715: =item $request - Apache request object [in] The request being processed.
                    716: 
                    717: =item $user  - string [in] The name of the user initiating the request.
                    718: 
                    719: =item $directory - string [in] The name of the directory relative to the
                    720:                  construction space top level of the renamed file.
                    721: 
                    722: =item $oldfile - Name of the file.
                    723: 
                    724: =item $newfile - Name of the new file.
                    725: 
                    726: =back
                    727: 
                    728: Returns:
                    729: 
                    730: =over 4
                    731: 
                    732: =item 1 Success.
                    733: 
                    734: =item 0 Failure.
                    735: 
                    736: =cut
                    737: 
                    738: sub Rename2 {
                    739: 
                    740:   my ($request, $user, $directory, $oldfile, $newfile) = @_;
                    741: 
                    742:   &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
                    743: 	 " new file ".$newfile."\n");
                    744:   &Debug($request, "Target is: ".$directory.'/'.
                    745: 	 $newfile);
1.38      www       746:   if (-e $oldfile) {
1.40    ! www       747: 
        !           748:       my $oRN=$oldfile;
        !           749:       my $nRN=$newfile;
1.38      www       750:       unless (rename($oldfile,$newfile)) {
1.23      albertel  751: 	  $request->print('<font color="red">Error: '.$!.'</font>');
1.12      foxr      752: 	  return 0;
1.40    ! www       753:       }
        !           754:       ## If old name.(extension) exits, move under new name.
        !           755:       ## If it doesn't exist and a new.(extension) exists  
        !           756:       ## delete it (only concern when renaming over files)
        !           757:       my $tmp1=$oRN.'.meta';
        !           758:       my $tmp2=$nRN.'.meta';
        !           759:       if(-e $tmp1){
        !           760: 	  unless(rename($tmp1,$tmp2)){ }
        !           761:       }else if(-e $tmp2){
        !           762: 	  unlink $tmp2;
        !           763:       }
        !           764:       $tmp1=$oRN.'.save';
        !           765:       $tmp2=$nRN.'.save';
        !           766:       if(-e $tmp1){
        !           767: 	  unless(rename($tmp1,$tmp2)){ }
        !           768:       }else if(-e $tmp2){
        !           769: 	  unlink $tmp2;
        !           770:       }
        !           771:       $tmp1=$oRN.'.log';
        !           772:       $tmp2=$nRN.'.log';
        !           773:       if(-e $tmp1){
        !           774: 	  unless(rename($tmp1,$tmp2)){ }
        !           775:       }else if(-e $tmp2){
        !           776: 	  unlink $tmp2;
        !           777:       }
        !           778:       $tmp1=$oRN.'.bak';
        !           779:       $tmp2=$nRN.'.bak';
        !           780:       if(-e $tmp1){
        !           781: 	  unless(rename($tmp1,$tmp2)){ }
        !           782:       }else if(-e $tmp2){
        !           783: 	  unlink $tmp2;
1.38      www       784:       }
1.12      foxr      785:   } else {
1.38      www       786:       $request->print("<p> No such file: ".&display($oldfile).'</p></form>');
1.12      foxr      787:       return 0;
                    788:   }
                    789:   return 1;
                    790: }
                    791: =pod
                    792: 
                    793: =item Delete2($request, $user, $filename)
                    794: 
                    795:   Performs phase two of a delete.  The user has confirmed that they want 
                    796: to delete the selected file.   The file is deleted and the results of the
                    797: delete attempt are indicated.
                    798: 
                    799: Parameters:
                    800: 
                    801: =over 4
                    802: 
                    803: =item $request - Apache Request object [in] the request object for the current
                    804:                  delete operation.
                    805: 
                    806: =item $user    - string [in]  The name of the user initiating the delete
                    807:                  request.
                    808: 
1.17      harris41  809: =item $filename - string [in] The name of the file, relative to construction
                    810:                   space, to delete.
1.12      foxr      811: 
                    812: =back
                    813: 
                    814: Returns:
                    815:   1 - success.
                    816:   0 - Failure.
                    817: 
                    818: =cut
                    819: 
                    820: sub Delete2 {
                    821:   my ($request, $user, $filename) = @_;
                    822: 
                    823:   if(-e $filename) {
                    824:     unless(unlink($filename)) {
1.23      albertel  825:       $request->print('<font color="red">Error: '.$!.'</font>');
1.12      foxr      826:       return 0;
                    827:     }
                    828:   } else {
1.23      albertel  829:     $request->print('<p> No such file. </p></form');
1.12      foxr      830:     return 0;
                    831:   }
                    832:   return 1;
                    833: }
                    834: 
                    835: =pod
                    836: 
                    837: =item Copy2($request, $username, $dir, $oldfile, $newfile)
                    838: 
                    839:    Performs phase 2 of a copy.  The file is copied and the status 
                    840:    of that copy is reported back to the user.
                    841: 
                    842: =over 4
                    843: 
                    844: =item $request - Apache request object [in]; the apache request currently
                    845:                  being executed.
                    846: 
                    847: =item $username - string [in] Name of the user who is requesting the copy.
                    848: 
                    849: =item $dir - string [in] Directory path relative to the construction space
                    850:              of the destination file.
                    851: 
                    852: =item $oldfile - string [in] Name of the source file.
                    853: 
                    854: =item $newfile - string [in] Name of the destination file.
                    855: 
                    856: 
                    857: =back
                    858: 
                    859: Returns 0 failure, and 0 successs.
                    860: 
                    861: =cut
1.1       www       862: 
1.12      foxr      863: sub Copy2 {
                    864:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
                    865:     &Debug($request ,"Will try to copy $oldfile to $newfile");
                    866:     if(-e $oldfile) {
                    867: 	unless (copy($oldfile, $newfile)) {
1.23      albertel  868: 	    $request->print('<font color="red"> copy Error: '.$!.'</font>');
1.12      foxr      869: 	    return 0;
                    870: 	} else {
1.14      foxr      871: 	    unless (chmod(0660, $newfile)) {
1.23      albertel  872: 		$request->print('<font color="red"> chmod error: '.$!.'</font>');
1.14      foxr      873: 		return 0;
                    874: 	    }
1.12      foxr      875: 	    return 1;
                    876: 	}
1.11      albertel  877:     } else {
1.12      foxr      878: 	$request->print('<p> No such file </p>');
                    879: 	return 0;
1.11      albertel  880:     }
1.12      foxr      881:     return 1;
                    882: }
                    883: =pod
                    884: 
                    885: =item NewDir2($request, $user, $newdirectory)
1.1       www       886: 
1.12      foxr      887: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
                    888: 	reporting the results of that creation to the user.
                    889: 	
                    890: Parameters:
                    891: =over 4
                    892: 
                    893: =item $request  - Apache request object [in].  Object representing the current HTTP request.
                    894: 
                    895: =item $user - string [in] The name of the user that is initiating the request.
                    896: 
                    897: =item $newdirectory - string [in] The full path of the directory being created.
                    898: 
                    899: =back
                    900: 
                    901: Returns 0 - failure 1 - success.
                    902: 
                    903: =cut
1.8       albertel  904: 
1.12      foxr      905: sub NewDir2 {
                    906:   my ($request, $user, $newdirectory) = @_;
                    907:   
                    908:   unless(mkdir($newdirectory, 02770)) {
1.23      albertel  909:     $request->print('<font color="red">Error: '.$!.'</font>');
1.12      foxr      910:     return 0;
                    911:   }
                    912:   unless(chmod(02770, ($newdirectory))) {
1.23      albertel  913:       $request->print('<font color="red"> Error: '.$!.'</font>');
1.12      foxr      914:       return 0;
                    915:   }
                    916:   return 1;
1.1       www       917: }
                    918: 
1.12      foxr      919: =pod
                    920: 
                    921: =item phasetwo($r, $fn, $uname, $udom)
                    922: 
                    923:    Controls the phase 2 processing of file management
                    924:    requests for construction space.  In phase one, the user
                    925:    was asked to confirm the operation.  In phase 2, the operation
                    926:    is performed and the result is shown.
                    927: 
                    928:   The strategy is to break out the processing into specific action processors
                    929:   named action2 where action is the requested action and the 2 denotes 
                    930:   phase 2 processing.
                    931: 
                    932: Parameters:
                    933: 
                    934: =over 4
                    935: 
                    936: =item  $r     - Apache Request object [in] The request object for this httpd
                    937:            transaction.
                    938: 
                    939: =item  $fn    - string [in]  A filename indicating the object that is being
                    940:            manipulated.
                    941: 
                    942: =item  $uname - string [in] The name of the user initiating the file management
                    943:            request.
                    944: 
                    945: =item  $udom  - string  [in] The login domain of the user initiating the
                    946:            file management request.
                    947: =back
                    948: 
                    949: =cut
                    950: 
1.1       www       951: sub phasetwo {
                    952:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr      953:     
1.9       foxr      954:     &Debug($r, "loncfile - Entering phase 2 for $fn");
1.12      foxr      955:     
                    956:     # Break down the file into it's component pieces.
                    957:     
1.27      albertel  958:     my $dir;		# Directory path
                    959:     my $main;		# Filename.
                    960:     my $suffix;		# Extension.
                    961: 
                    962:     if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) {
                    963: 	$dir=$1;		# Directory path
                    964: 	$main=$2;		# Filename.
                    965: 	$suffix=$3;		# Extension.
                    966:     }
                    967:         
1.12      foxr      968:     my $dest;                   # On success this is where we'll go.
1.9       foxr      969:     
1.12      foxr      970:     &Debug($r, 
                    971: 	   "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
                    972:     &Debug($r,
                    973: 	   "    newfilename = ".$ENV{'form.newfilename'});
1.9       foxr      974: 
                    975:     my $conspace=$fn;
1.12      foxr      976:     
                    977:     &Debug($r, 
                    978: 	   "loncfile::phase2 Full construction space name: $conspace");
                    979:     
                    980:     &Debug($r, 
                    981: 	   "loncfie::phase2 action is $ENV{'form.action'}");
                    982:     
                    983:     # Select the appropriate processing sub.
                    984:     
                    985:     if ($ENV{'form.action'} eq 'rename') { # Rename.
                    986: 	if($ENV{'form.newfilename'}) {
1.27      albertel  987: 	    if (!defined($dir)) {
                    988: 		$fn=~m:^(.*)/:;
                    989: 		$dir=$1;
                    990: 	    }
1.12      foxr      991: 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
                    992: 		return;
                    993: 	    }
1.38      www       994: 	    $dest = &url($ENV{'form.newfilename'});
1.12      foxr      995: 	}
1.5       www       996:     } elsif ($ENV{'form.action'} eq 'delete') { 
1.12      foxr      997: 	if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
                    998: 	    return ;
                    999: 	}
                   1000: 	# Once a resource is deleted, we just list the directory that
                   1001: 	# previously held it.
                   1002: 	#
1.20      albertel 1003: 	$dest = $dir."/.";		# Parent dir.
1.5       www      1004:     } elsif ($ENV{'form.action'} eq 'copy') { 
1.12      foxr     1005: 	if($ENV{'form.newfilename'}) {
                   1006: 	    if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
                   1007: 		return
                   1008: 		}
                   1009: 	    $dest = $ENV{'form.newfilename'};
                   1010:      
                   1011: 	} else {
1.23      albertel 1012: 	    $r->print('<p>No New filename specified</p></form>');
1.12      foxr     1013: 	    return;
                   1014: 	}
                   1015: 	
1.5       www      1016:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.38      www      1017:         my $newdir= $ENV{'form.newfilename'};
1.12      foxr     1018: 	if(!&NewDir2($r, $uname, $newdir)) {
                   1019: 	    return;
                   1020: 	}
                   1021: 	$dest = $newdir."/"
                   1022:     }
1.38      www      1023:     $r->print('<h3><a href="'.&url($dest).'">Done</a></h3>');
1.1       www      1024: }
                   1025: 
                   1026: sub handler {
                   1027: 
1.10      foxr     1028:   $r=shift;
1.1       www      1029: 
1.9       foxr     1030: 
                   1031:   &Debug($r, "loncfile.pm - handler entered");
1.12      foxr     1032:   &Debug($r, " filename: ".$ENV{'form.filename'});
                   1033:   &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
1.36      www      1034: #
                   1035: # Determine the root filename
                   1036: # This could come in as "filename", which actually is a URL, or
                   1037: # as "qualifiedfilename", which is indeed a real filename in filesystem
                   1038: #
1.1       www      1039:   my $fn;
                   1040: 
                   1041:   if ($ENV{'form.filename'}) {
1.22      albertel 1042:       $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
1.35      www      1043:       $fn=&URLToPath($fn);
1.36      www      1044:   } elsif ($ENV{'form.qualifiedfilename'}) {
                   1045:       $fn=$ENV{'form.qualifiedfilename'};
1.1       www      1046:   } else {
1.9       foxr     1047:       &Debug($r, "loncfile::handler - no form.filename");
1.1       www      1048:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   1049:          ' unspecified filename for cfile', $r->filename); 
                   1050:      return HTTP_NOT_FOUND;
                   1051:   }
                   1052: 
                   1053:   unless ($fn) { 
1.9       foxr     1054:       &Debug($r, "loncfile::handler - doctored url is empty");
1.1       www      1055:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   1056:          ' trying to cfile non-existing file', $r->filename); 
                   1057:      return HTTP_NOT_FOUND;
                   1058:   } 
                   1059: 
                   1060: # ----------------------------------------------------------- Start page output
                   1061:   my $uname;
                   1062:   my $udom;
                   1063: 
                   1064:   ($uname,$udom)=
                   1065:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1.9       foxr     1066:   &Debug($r, 
                   1067: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
1.1       www      1068:   unless (($uname) && ($udom)) {
                   1069:      $r->log_reason($uname.' at '.$udom.
1.4       www      1070:          ' trying to manipulate file '.$ENV{'form.filename'}.
1.1       www      1071:          ' ('.$fn.') - not authorized', 
                   1072:          $r->filename); 
                   1073:      return HTTP_NOT_ACCEPTABLE;
                   1074:   }
                   1075: 
                   1076: 
                   1077:   $r->content_type('text/html');
                   1078:   $r->send_http_header;
                   1079: 
                   1080:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                   1081: 
1.36      www      1082:   $r->print(&Apache::loncommon::bodytag('Construction Space File Operation'));
1.1       www      1083: 
                   1084:   
1.36      www      1085:   $r->print('<h3>Location: '.&display($fn).'</h3>');
1.1       www      1086:   
                   1087:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
1.23      albertel 1088:           $r->print('<h3><font color="red">Co-Author: '.$uname.' at '.$udom.
1.1       www      1089:                '</font></h3>');
                   1090:   }
                   1091: 
1.9       foxr     1092: 
                   1093:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
1.2       www      1094:   if ($ENV{'form.action'} eq 'delete') {
1.9       foxr     1095:       
1.2       www      1096:       $r->print('<h3>Delete</h3>');
                   1097:   } elsif ($ENV{'form.action'} eq 'rename') {
                   1098:       $r->print('<h3>Rename</h3>');
                   1099:   } elsif ($ENV{'form.action'} eq 'newdir') {
                   1100:       $r->print('<h3>New Directory</h3>');
                   1101:   } elsif ($ENV{'form.action'} eq 'copy') {
                   1102:       $r->print('<h3>Copy</h3>');
1.22      albertel 1103:   } elsif ($ENV{'form.action'} eq 'newfile' ||
                   1104: 	   $ENV{'form.action'} eq 'newhtmlfile' ||
1.28      www      1105: 	   $ENV{'form.action'} eq 'newproblemfile' ||
                   1106:            $ENV{'form.action'} eq 'newpagefile' ||
1.30      albertel 1107:            $ENV{'form.action'} eq 'newsequencefile' ||
1.34      www      1108: 	   $ENV{'form.action'} eq 'newrightsfile' ||
                   1109: 	   $ENV{'form.action'} eq 'newstyfile' ||
1.30      albertel 1110:            $ENV{'form.action'} eq 'Select Action' ) {
1.22      albertel 1111:       $r->print('<h3>New Resource</h3>');
1.2       www      1112:   } else {
1.30      albertel 1113:      $r->print('<p>Unknown Action '.$ENV{'form.action'}.' </p></body></html>');
1.2       www      1114:      return OK;  
                   1115:   }
1.1       www      1116:   if ($ENV{'form.phase'} eq 'two') {
1.9       foxr     1117:       &Debug($r, "loncfile::handler  entering phase2");
1.4       www      1118:       &phasetwo($r,$fn,$uname,$udom);
1.1       www      1119:   } else {
1.9       foxr     1120:       &Debug($r, "loncfile::handler  entering phase1");
1.3       www      1121:       &phaseone($r,$fn,$uname,$udom);
1.1       www      1122:   }
                   1123: 
                   1124:   $r->print('</body></html>');
                   1125:   return OK;  
                   1126: }
                   1127: 
                   1128: 1;
                   1129: __END__
1.9       foxr     1130: 

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