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

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.43    ! albertel   12: # $Id: loncfile.pm,v 1.42 2003/09/11 21:02:38 albertel 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;
1.43    ! albertel  171:     $fn=~s-^/home/(\w+)/public_html-/priv/$1-;
1.36      www       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.42      albertel  390: 	    # is dest a dir
                    391: 	    if (-d $newfilename) {
                    392: 		if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    393: 	    }
1.29      albertel  394: 	    if ($newfilename =~ m|/[^\.]+$|) {
1.36      www       395: 		#no extension add on original extension
                    396: 		if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
1.24      albertel  397: 		    $newfilename.='.'.$1;
                    398: 		}
                    399: 	    }
1.36      www       400: 	    $request->print(&checksuffix($fn, $newfilename));
1.27      albertel  401: 	    #renaming a dir, delete the trailing /
1.39      www       402:             #remove second to last element for current dir
                    403: 	    if (-d $fn) {
                    404: 		$newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
1.27      albertel  405: 	    }
1.42      albertel  406: 	    $newfilename=~s://+:/:g; # remove duplicate /
                    407: 	    while ($newfilename=~m:/\.\./:) {
                    408: 		$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    409: 	    }
1.39      www       410: 	    my $return=&exists($user, $domain, $newfilename);
1.21      albertel  411: 	    $request->print($return);
                    412: 	    if ($return =~/^Error:/) {
1.36      www       413: 		$request->print('<br /><a href="'.&url($fn).'">Cancel</a>');
1.21      albertel  414: 		return;
                    415: 	    }
1.23      albertel  416: 	    $request->print('<input type="hidden" name="newfilename" value="'.
1.12      foxr      417: 			    $newfilename.
1.36      www       418: 			    '" /><p>Rename '.&display($fn).
                    419: 			    '</tt><br />to '.&display($newfilename).'?</p>');
                    420: 	    &CloseForm1($request, $fn);
1.12      foxr      421: 	} else {
1.36      www       422: 	    $request->print('<p>No new filename specified.</p></form>');
1.12      foxr      423: 	    return;
                    424: 	}
                    425:     } else {
1.36      www       426: 	$request->print('<p> No such file: '.&display($fn).'</p></form>');
1.12      foxr      427: 	return;
                    428:     }
                    429:     
                    430: }
                    431: =pod
                    432: 
                    433: =item Delete1
                    434: 
                    435:    Performs phase 1 processing of the delete operation.  In phase one
                    436:   we just check to be sure the file exists.
                    437: 
                    438: Parameters:
                    439: 
                    440: =over 4
                    441: 
1.36      www       442: =item   $request   - Apache Request Object [in] request object for the current 
1.12      foxr      443:                 request.
                    444: 
1.36      www       445: =item   $user      - string [in]  Name of the user initiating the request.
1.12      foxr      446: 
1.36      www       447: =item   $domain    - string [in]  Domain the initiating user is logged in as
1.13      foxr      448: 
1.36      www       449: =item   $filename  - string [in]  Source filename.
1.12      foxr      450: 
                    451: =back
                    452: 
                    453: =cut
                    454: 
                    455: sub Delete1 {
1.36      www       456:   my ($request, $user, $domain, $fn) = @_;
1.12      foxr      457: 
1.36      www       458:   if( -e $fn) {
1.23      albertel  459:     $request->print('<input type="hidden" name="newfilename" value="'.
1.36      www       460: 		    $fn.'"/>');
                    461:     $request->print('<p>Delete '.&display($fn).'?</p>');
                    462:     &CloseForm1($request, $fn);
1.12      foxr      463:   } else {
1.36      www       464:     $request->print('<p>No such file: '.&display($fn).'</p></form>');
1.12      foxr      465:   }
                    466: }
                    467: 
                    468: =pod
                    469: 
                    470: =item Copy1($request, $user, $domain, $filename, $newfilename)
                    471: 
                    472:    Performs phase 1 processing of the construction space copy command.
1.17      harris41  473:    Ensure that the source file exists.  Ensure that a destination exists,
                    474:    also warn if the destination already exists.
1.12      foxr      475: 
                    476: Parameters:
                    477: 
                    478: =over 4
                    479: 
                    480: =item   $request   - Apache Request Object [in] request object for the current 
                    481:                 request.
                    482: 
                    483: =item   $user      - string [in]  Name of the user initiating the request.
                    484: 
                    485: =item   $domain    - string [in]  Domain the initiating user is logged in as
                    486: 
1.36      www       487: =item   $fn  - string [in]  Source filename.
1.12      foxr      488: 
                    489: =item   $newfilename-string [in]  Destination filename.
                    490: 
                    491: =back
                    492: 
                    493: =cut
                    494: 
                    495: sub Copy1 {
1.42      albertel  496:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.12      foxr      497: 
1.42      albertel  498:     if(-e $fn) {
                    499: 	# is dest a dir
                    500: 	if (-d $newfilename) {
                    501: 	    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    502: 	}
                    503: 	if ($newfilename =~ m|/[^\.]+$|) {
                    504: 	    #no extension add on original extension
                    505: 	    if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {	$newfilename.='.'.$1; }
                    506: 	} 
                    507: 	$newfilename=~s://+:/:g; # remove duplicate /
                    508: 	while ($newfilename=~m:/\.\./:) {
                    509: 	    $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    510: 	}
                    511: 	$request->print(&checksuffix($fn,$newfilename));
                    512: 	my $return=&exists($user, $domain, $newfilename);
                    513: 	$request->print($return);
                    514: 	if ($return =~/^Error:/) {
                    515: 	    $request->print('<br /><a href="'.&url($fn).'">Cancel</a>');
                    516: 	    return;
                    517: 	}
                    518: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    519: 			$newfilename.
                    520: 			'" /><p>Copy '.&display($fn).'<br />to '.
                    521: 			&display($newfilename).'?</p>');
                    522: 	&CloseForm1($request, $fn);
                    523:     } else {
                    524: 	$request->print('<p>No such file: '.&display($fn).'</p></form>');
1.21      albertel  525:     }
1.19      albertel  526: }
                    527: 
                    528: =pod
                    529: 
1.12      foxr      530: =item NewDir1
                    531:  
                    532:   Does all phase 1 processing of directory creation:
                    533:   Ensures that the user provides a new directory name,
                    534:   and that the directory does not already exist.
                    535: 
                    536: Parameters:
                    537: 
                    538: =over 4
                    539: 
                    540: =item   $request  - Apache Request Object [in] - Server request object for the
1.17      harris41  541:                current url.
1.12      foxr      542: 
                    543: =item   $username - Name of the user that is requesting the directory creation.
                    544: 
1.36      www       545: =item $domain - Domain user is in
                    546: 
                    547: =item   $fn     - source file.
1.12      foxr      548: 
                    549: =item   $newdir   - Name of the directory to be created; path relative to the 
                    550:                top level of construction space.
                    551: =back
                    552: 
                    553: Side Effects:
                    554: 
                    555: =over 4
                    556: 
                    557: =item A new form is displayed.  Clicking on the confirmation button
                    558: causes the newdir operation to transition into phase 2.  The hidden field
                    559: "newfilename" is set with the construction space path to the new directory.
                    560: 
                    561: 
                    562: =back
                    563: 
                    564: =cut
                    565: 
                    566: 
                    567: sub NewDir1
                    568: {
1.36      www       569:   my ($request, $username, $domain, $fn, $newfilename) = @_;
1.12      foxr      570: 
1.39      www       571:   my $result=&exists($username,$domain,$newfilename);
                    572:   if ($result) {
                    573:     $request->print('<font color="red">'.$result.'</font></form>');
                    574:   } else {
1.23      albertel  575:     $request->print('<input type="hidden" name="newfilename" value="'.
1.36      www       576: 		    $newfilename.'" /><p>Make new directory '.
                    577: 		    &display($newfilename).'?</p>');
                    578:     &CloseForm1($request, $fn);
1.12      foxr      579:   }
                    580: }
                    581: 
                    582: =pod
                    583: 
1.22      albertel  584: =item NewFile1
                    585:  
                    586:   Does all phase 1 processing of file creation:
                    587:   Ensures that the user provides a new filename, adds proper extension
                    588:   if needed and that the file does not already exist, if it is a html,
                    589:   problem, page, or sequence, it then creates a form link to hand the
                    590:   actual creation off to the proper handler.
                    591: 
                    592: Parameters:
                    593: 
                    594: =over 4
                    595: 
                    596: =item   $request  - Apache Request Object [in] - Server request object for the
                    597:                current url.
                    598: 
                    599: =item   $username - Name of the user that is requesting the directory creation.
                    600: 
                    601: =item   $domain   - Name of the domain of the user
                    602: 
1.36      www       603: =item   $fn      - Source file name
1.22      albertel  604: 
                    605: =item   $newfilename
                    606:                   - Name of the file to be created; no path information
                    607: =back
                    608: 
                    609: Side Effects:
                    610: 
                    611: =over 4
                    612: 
                    613: =item 2 new forms are displayed.  Clicking on the confirmation button
                    614: causes the browser to attempt to load the specfied URL, allowing the
1.36      www       615: proper handler to take care of file creation. There is also a Cancel
1.22      albertel  616: button which returns you to the driectory listing you came from
                    617: 
                    618: =back
                    619: 
                    620: =cut
                    621: 
                    622: 
                    623: sub NewFile1 {
1.36      www       624:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.22      albertel  625: 
                    626:     if ($ENV{'form.action'} =~ /new(.+)file/) {
                    627: 	my $extension=$1;
1.40      www       628: 
                    629:         ##Informs User (name).(number).(extension) not allowed 
                    630: 	if($newfilename =~ /\.(\d+)\.(\w+)$/){
                    631: 	    $r->print('<font color="red">'.$newfilename.
                    632: 		      ' - Bad Filename<br />(name).(number).(extension)'.
                    633: 		      ' Not Allowed</font>');
                    634: 	    return;
                    635: 	}
1.22      albertel  636: 	if ($newfilename !~ /\Q.$extension\E$/) {
1.26      albertel  637: 	    if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) {
                    638: 		#already has an extension strip it and add in expected one
                    639: 		$newfilename =~ s|.([^\.]+)$||;
                    640: 	    }
1.22      albertel  641: 	    $newfilename.=".$extension";
                    642: 	}
1.31      albertel  643:     }
1.39      www       644:     my $result=&exists($user,$domain,$newfilename);
                    645:     if($result) {
                    646: 	$request->print('<font color="red">'.$result.'</font></form>');
                    647:     } else {
1.36      www       648: 	$request->print('<p>Make new file '.&display($newfilename).'?</p>');
1.22      albertel  649: 	$request->print('</form>');
1.36      www       650: 	$request->print('<form action="'.&url($newfilename).
1.30      albertel  651: 			'" method="POST"><p><input type="submit" value="Continue" /></p></form>');
1.36      www       652: 	$request->print('<form action="'.&url($fn).
1.30      albertel  653: 			'" method="POST"><p><input type="submit" value="Cancel" /></p></form>');
1.22      albertel  654:     }
                    655: }
                    656: 
                    657: =pod
                    658: 
1.12      foxr      659: =item phaseone($r, $fn, $uname, $udom)
                    660: 
                    661:   Peforms phase one processing of the request.  In phase one, error messages
                    662: are returned if the request cannot be performed (e.g. attempts to manipulate
                    663: files that are nonexistent).  If the operation can be performed, what is
                    664: about to be done will be presented to the user for confirmation.  If the
                    665: user confirms the request, then phase two is executed, the action 
                    666: performed and reported to the user.
                    667: 
                    668:  Parameters:
                    669: 
                    670: =over 4
                    671: 
                    672: =item $r  - request object [in] - The Apache request being executed.
                    673: 
                    674: =item $fn = string [in] - The filename being manipulated by the 
                    675:                              request.
                    676: 
                    677: =item $uname - string [in] Name of user logged in and doing this action.
                    678: 
1.17      harris41  679: =item $udom  - string [in] Domain name under which the user logged in. 
1.12      foxr      680: 
                    681: =back
                    682: 
                    683: =cut
1.8       albertel  684: 
1.1       www       685: sub phaseone {
1.12      foxr      686:   my ($r,$fn,$uname,$udom)=@_;
                    687:   
1.36      www       688:   my $newfilename=&cleanDest($r,$ENV{'form.newfilename'});
                    689:   $newfilename=&relativeDest($fn,$newfilename,$uname);
1.32      albertel  690: 
1.23      albertel  691:   $r->print('<form action="/adm/cfile" method="post">'.
1.36      www       692:       '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
                    693:       '<input type="hidden" name="phase" value="two" />'.
                    694:       '<input type="hidden" name="action" value="'.$ENV{'form.action'}.'" />');
1.12      foxr      695:   
                    696:   if ($ENV{'form.action'} eq 'rename') {
1.36      www       697:       &Rename1($r, $uname, $udom, $fn, $newfilename);
1.12      foxr      698:   } elsif ($ENV{'form.action'} eq 'delete') { 
1.36      www       699:       &Delete1($r, $uname, $udom, $fn);
1.12      foxr      700:   } elsif ($ENV{'form.action'} eq 'copy') { 
1.36      www       701:       if($newfilename) {
                    702: 	  &Copy1($r, $uname, $udom, $fn, $newfilename);
                    703:       } else {
                    704: 	  $r->print('<p>No new filename specified.</p></form>');
                    705:       }
1.12      foxr      706:   } elsif ($ENV{'form.action'} eq 'newdir') {
1.36      www       707:       &NewDir1($r, $uname, $udom, $fn, $newfilename);
1.22      albertel  708:   }  elsif ($ENV{'form.action'} eq 'newfile' ||
                    709: 	    $ENV{'form.action'} eq 'newhtmlfile' ||
1.28      www       710: 	    $ENV{'form.action'} eq 'newproblemfile' ||
                    711:             $ENV{'form.action'} eq 'newpagefile' ||
1.30      albertel  712:             $ENV{'form.action'} eq 'newsequencefile' ||
1.34      www       713:             $ENV{'form.action'} eq 'newrightsfile' ||
                    714:             $ENV{'form.action'} eq 'newstyfile' ||
1.30      albertel  715:             $ENV{'form.action'} eq 'Select Action') {
1.36      www       716:       if ($newfilename) {
                    717: 	  &NewFile1($r, $uname, $udom, $fn, $newfilename);
1.25      albertel  718:       } else {
                    719: 	  $r->print('<p>No new filename specified.</p></form>');
1.22      albertel  720:       }
1.12      foxr      721:   }
                    722: }
                    723: 
                    724: =pod
                    725: 
                    726: =item Rename2($request, $user, $directory, $oldfile, $newfile)
                    727: 
1.17      harris41  728: Performs phase 2 processing of a rename reequest.   This is where the
1.12      foxr      729: actual rename is performed.
                    730: 
                    731: Parameters
                    732: 
                    733: =over 4
                    734: 
                    735: =item $request - Apache request object [in] The request being processed.
                    736: 
                    737: =item $user  - string [in] The name of the user initiating the request.
                    738: 
                    739: =item $directory - string [in] The name of the directory relative to the
                    740:                  construction space top level of the renamed file.
                    741: 
                    742: =item $oldfile - Name of the file.
                    743: 
                    744: =item $newfile - Name of the new file.
                    745: 
                    746: =back
                    747: 
                    748: Returns:
                    749: 
                    750: =over 4
                    751: 
                    752: =item 1 Success.
                    753: 
                    754: =item 0 Failure.
                    755: 
                    756: =cut
                    757: 
                    758: sub Rename2 {
                    759: 
                    760:   my ($request, $user, $directory, $oldfile, $newfile) = @_;
                    761: 
                    762:   &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
                    763: 	 " new file ".$newfile."\n");
                    764:   &Debug($request, "Target is: ".$directory.'/'.
                    765: 	 $newfile);
1.38      www       766:   if (-e $oldfile) {
1.40      www       767: 
                    768:       my $oRN=$oldfile;
                    769:       my $nRN=$newfile;
1.38      www       770:       unless (rename($oldfile,$newfile)) {
1.23      albertel  771: 	  $request->print('<font color="red">Error: '.$!.'</font>');
1.12      foxr      772: 	  return 0;
1.40      www       773:       }
                    774:       ## If old name.(extension) exits, move under new name.
                    775:       ## If it doesn't exist and a new.(extension) exists  
                    776:       ## delete it (only concern when renaming over files)
                    777:       my $tmp1=$oRN.'.meta';
                    778:       my $tmp2=$nRN.'.meta';
                    779:       if(-e $tmp1){
                    780: 	  unless(rename($tmp1,$tmp2)){ }
1.41      matthew   781:       } elsif(-e $tmp2){
1.40      www       782: 	  unlink $tmp2;
                    783:       }
                    784:       $tmp1=$oRN.'.save';
                    785:       $tmp2=$nRN.'.save';
                    786:       if(-e $tmp1){
                    787: 	  unless(rename($tmp1,$tmp2)){ }
1.41      matthew   788:       } elsif(-e $tmp2){
1.40      www       789: 	  unlink $tmp2;
                    790:       }
                    791:       $tmp1=$oRN.'.log';
                    792:       $tmp2=$nRN.'.log';
                    793:       if(-e $tmp1){
                    794: 	  unless(rename($tmp1,$tmp2)){ }
1.41      matthew   795:       } elsif(-e $tmp2){
1.40      www       796: 	  unlink $tmp2;
                    797:       }
                    798:       $tmp1=$oRN.'.bak';
                    799:       $tmp2=$nRN.'.bak';
                    800:       if(-e $tmp1){
                    801: 	  unless(rename($tmp1,$tmp2)){ }
1.41      matthew   802:       } elsif(-e $tmp2){
1.40      www       803: 	  unlink $tmp2;
1.38      www       804:       }
1.12      foxr      805:   } else {
1.38      www       806:       $request->print("<p> No such file: ".&display($oldfile).'</p></form>');
1.12      foxr      807:       return 0;
                    808:   }
                    809:   return 1;
                    810: }
                    811: =pod
                    812: 
                    813: =item Delete2($request, $user, $filename)
                    814: 
                    815:   Performs phase two of a delete.  The user has confirmed that they want 
                    816: to delete the selected file.   The file is deleted and the results of the
                    817: delete attempt are indicated.
                    818: 
                    819: Parameters:
                    820: 
                    821: =over 4
                    822: 
                    823: =item $request - Apache Request object [in] the request object for the current
                    824:                  delete operation.
                    825: 
                    826: =item $user    - string [in]  The name of the user initiating the delete
                    827:                  request.
                    828: 
1.17      harris41  829: =item $filename - string [in] The name of the file, relative to construction
                    830:                   space, to delete.
1.12      foxr      831: 
                    832: =back
                    833: 
                    834: Returns:
                    835:   1 - success.
                    836:   0 - Failure.
                    837: 
                    838: =cut
                    839: 
                    840: sub Delete2 {
                    841:   my ($request, $user, $filename) = @_;
                    842: 
                    843:   if(-e $filename) {
                    844:     unless(unlink($filename)) {
1.23      albertel  845:       $request->print('<font color="red">Error: '.$!.'</font>');
1.12      foxr      846:       return 0;
                    847:     }
                    848:   } else {
1.23      albertel  849:     $request->print('<p> No such file. </p></form');
1.12      foxr      850:     return 0;
                    851:   }
                    852:   return 1;
                    853: }
                    854: 
                    855: =pod
                    856: 
                    857: =item Copy2($request, $username, $dir, $oldfile, $newfile)
                    858: 
                    859:    Performs phase 2 of a copy.  The file is copied and the status 
                    860:    of that copy is reported back to the user.
                    861: 
                    862: =over 4
                    863: 
                    864: =item $request - Apache request object [in]; the apache request currently
                    865:                  being executed.
                    866: 
                    867: =item $username - string [in] Name of the user who is requesting the copy.
                    868: 
                    869: =item $dir - string [in] Directory path relative to the construction space
                    870:              of the destination file.
                    871: 
                    872: =item $oldfile - string [in] Name of the source file.
                    873: 
                    874: =item $newfile - string [in] Name of the destination file.
                    875: 
                    876: 
                    877: =back
                    878: 
                    879: Returns 0 failure, and 0 successs.
                    880: 
                    881: =cut
1.1       www       882: 
1.12      foxr      883: sub Copy2 {
                    884:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
                    885:     &Debug($request ,"Will try to copy $oldfile to $newfile");
                    886:     if(-e $oldfile) {
                    887: 	unless (copy($oldfile, $newfile)) {
1.23      albertel  888: 	    $request->print('<font color="red"> copy Error: '.$!.'</font>');
1.12      foxr      889: 	    return 0;
                    890: 	} else {
1.14      foxr      891: 	    unless (chmod(0660, $newfile)) {
1.23      albertel  892: 		$request->print('<font color="red"> chmod error: '.$!.'</font>');
1.14      foxr      893: 		return 0;
                    894: 	    }
1.12      foxr      895: 	    return 1;
                    896: 	}
1.11      albertel  897:     } else {
1.12      foxr      898: 	$request->print('<p> No such file </p>');
                    899: 	return 0;
1.11      albertel  900:     }
1.12      foxr      901:     return 1;
                    902: }
                    903: =pod
                    904: 
                    905: =item NewDir2($request, $user, $newdirectory)
1.1       www       906: 
1.12      foxr      907: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
                    908: 	reporting the results of that creation to the user.
                    909: 	
                    910: Parameters:
                    911: =over 4
                    912: 
                    913: =item $request  - Apache request object [in].  Object representing the current HTTP request.
                    914: 
                    915: =item $user - string [in] The name of the user that is initiating the request.
                    916: 
                    917: =item $newdirectory - string [in] The full path of the directory being created.
                    918: 
                    919: =back
                    920: 
                    921: Returns 0 - failure 1 - success.
                    922: 
                    923: =cut
1.8       albertel  924: 
1.12      foxr      925: sub NewDir2 {
                    926:   my ($request, $user, $newdirectory) = @_;
                    927:   
                    928:   unless(mkdir($newdirectory, 02770)) {
1.23      albertel  929:     $request->print('<font color="red">Error: '.$!.'</font>');
1.12      foxr      930:     return 0;
                    931:   }
                    932:   unless(chmod(02770, ($newdirectory))) {
1.23      albertel  933:       $request->print('<font color="red"> Error: '.$!.'</font>');
1.12      foxr      934:       return 0;
                    935:   }
                    936:   return 1;
1.1       www       937: }
                    938: 
1.12      foxr      939: =pod
                    940: 
                    941: =item phasetwo($r, $fn, $uname, $udom)
                    942: 
                    943:    Controls the phase 2 processing of file management
                    944:    requests for construction space.  In phase one, the user
                    945:    was asked to confirm the operation.  In phase 2, the operation
                    946:    is performed and the result is shown.
                    947: 
                    948:   The strategy is to break out the processing into specific action processors
                    949:   named action2 where action is the requested action and the 2 denotes 
                    950:   phase 2 processing.
                    951: 
                    952: Parameters:
                    953: 
                    954: =over 4
                    955: 
                    956: =item  $r     - Apache Request object [in] The request object for this httpd
                    957:            transaction.
                    958: 
                    959: =item  $fn    - string [in]  A filename indicating the object that is being
                    960:            manipulated.
                    961: 
                    962: =item  $uname - string [in] The name of the user initiating the file management
                    963:            request.
                    964: 
                    965: =item  $udom  - string  [in] The login domain of the user initiating the
                    966:            file management request.
                    967: =back
                    968: 
                    969: =cut
                    970: 
1.1       www       971: sub phasetwo {
                    972:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr      973:     
1.9       foxr      974:     &Debug($r, "loncfile - Entering phase 2 for $fn");
1.12      foxr      975:     
                    976:     # Break down the file into it's component pieces.
                    977:     
1.27      albertel  978:     my $dir;		# Directory path
                    979:     my $main;		# Filename.
                    980:     my $suffix;		# Extension.
                    981: 
                    982:     if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) {
                    983: 	$dir=$1;		# Directory path
                    984: 	$main=$2;		# Filename.
                    985: 	$suffix=$3;		# Extension.
                    986:     }
                    987:         
1.12      foxr      988:     my $dest;                   # On success this is where we'll go.
1.9       foxr      989:     
1.12      foxr      990:     &Debug($r, 
                    991: 	   "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
                    992:     &Debug($r,
                    993: 	   "    newfilename = ".$ENV{'form.newfilename'});
1.9       foxr      994: 
                    995:     my $conspace=$fn;
1.12      foxr      996:     
                    997:     &Debug($r, 
                    998: 	   "loncfile::phase2 Full construction space name: $conspace");
                    999:     
                   1000:     &Debug($r, 
                   1001: 	   "loncfie::phase2 action is $ENV{'form.action'}");
                   1002:     
                   1003:     # Select the appropriate processing sub.
                   1004:     
                   1005:     if ($ENV{'form.action'} eq 'rename') { # Rename.
                   1006: 	if($ENV{'form.newfilename'}) {
1.27      albertel 1007: 	    if (!defined($dir)) {
                   1008: 		$fn=~m:^(.*)/:;
                   1009: 		$dir=$1;
                   1010: 	    }
1.12      foxr     1011: 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
                   1012: 		return;
                   1013: 	    }
1.38      www      1014: 	    $dest = &url($ENV{'form.newfilename'});
1.12      foxr     1015: 	}
1.5       www      1016:     } elsif ($ENV{'form.action'} eq 'delete') { 
1.12      foxr     1017: 	if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
                   1018: 	    return ;
                   1019: 	}
                   1020: 	# Once a resource is deleted, we just list the directory that
                   1021: 	# previously held it.
                   1022: 	#
1.20      albertel 1023: 	$dest = $dir."/.";		# Parent dir.
1.5       www      1024:     } elsif ($ENV{'form.action'} eq 'copy') { 
1.12      foxr     1025: 	if($ENV{'form.newfilename'}) {
                   1026: 	    if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
                   1027: 		return
                   1028: 		}
                   1029: 	    $dest = $ENV{'form.newfilename'};
                   1030:      
                   1031: 	} else {
1.23      albertel 1032: 	    $r->print('<p>No New filename specified</p></form>');
1.12      foxr     1033: 	    return;
                   1034: 	}
                   1035: 	
1.5       www      1036:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.38      www      1037:         my $newdir= $ENV{'form.newfilename'};
1.12      foxr     1038: 	if(!&NewDir2($r, $uname, $newdir)) {
                   1039: 	    return;
                   1040: 	}
                   1041: 	$dest = $newdir."/"
                   1042:     }
1.38      www      1043:     $r->print('<h3><a href="'.&url($dest).'">Done</a></h3>');
1.1       www      1044: }
                   1045: 
                   1046: sub handler {
                   1047: 
1.10      foxr     1048:   $r=shift;
1.1       www      1049: 
1.9       foxr     1050: 
                   1051:   &Debug($r, "loncfile.pm - handler entered");
1.12      foxr     1052:   &Debug($r, " filename: ".$ENV{'form.filename'});
                   1053:   &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
1.36      www      1054: #
                   1055: # Determine the root filename
                   1056: # This could come in as "filename", which actually is a URL, or
                   1057: # as "qualifiedfilename", which is indeed a real filename in filesystem
                   1058: #
1.1       www      1059:   my $fn;
                   1060: 
                   1061:   if ($ENV{'form.filename'}) {
1.22      albertel 1062:       $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
1.35      www      1063:       $fn=&URLToPath($fn);
1.36      www      1064:   } elsif ($ENV{'form.qualifiedfilename'}) {
                   1065:       $fn=$ENV{'form.qualifiedfilename'};
1.1       www      1066:   } else {
1.9       foxr     1067:       &Debug($r, "loncfile::handler - no form.filename");
1.1       www      1068:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   1069:          ' unspecified filename for cfile', $r->filename); 
                   1070:      return HTTP_NOT_FOUND;
                   1071:   }
                   1072: 
                   1073:   unless ($fn) { 
1.9       foxr     1074:       &Debug($r, "loncfile::handler - doctored url is empty");
1.1       www      1075:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   1076:          ' trying to cfile non-existing file', $r->filename); 
                   1077:      return HTTP_NOT_FOUND;
                   1078:   } 
                   1079: 
                   1080: # ----------------------------------------------------------- Start page output
                   1081:   my $uname;
                   1082:   my $udom;
                   1083: 
                   1084:   ($uname,$udom)=
                   1085:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1.9       foxr     1086:   &Debug($r, 
                   1087: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
1.1       www      1088:   unless (($uname) && ($udom)) {
                   1089:      $r->log_reason($uname.' at '.$udom.
1.4       www      1090:          ' trying to manipulate file '.$ENV{'form.filename'}.
1.1       www      1091:          ' ('.$fn.') - not authorized', 
                   1092:          $r->filename); 
                   1093:      return HTTP_NOT_ACCEPTABLE;
                   1094:   }
                   1095: 
                   1096: 
                   1097:   $r->content_type('text/html');
                   1098:   $r->send_http_header;
                   1099: 
                   1100:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                   1101: 
1.36      www      1102:   $r->print(&Apache::loncommon::bodytag('Construction Space File Operation'));
1.1       www      1103: 
                   1104:   
1.36      www      1105:   $r->print('<h3>Location: '.&display($fn).'</h3>');
1.1       www      1106:   
                   1107:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
1.23      albertel 1108:           $r->print('<h3><font color="red">Co-Author: '.$uname.' at '.$udom.
1.1       www      1109:                '</font></h3>');
                   1110:   }
                   1111: 
1.9       foxr     1112: 
                   1113:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
1.2       www      1114:   if ($ENV{'form.action'} eq 'delete') {
1.9       foxr     1115:       
1.2       www      1116:       $r->print('<h3>Delete</h3>');
                   1117:   } elsif ($ENV{'form.action'} eq 'rename') {
                   1118:       $r->print('<h3>Rename</h3>');
                   1119:   } elsif ($ENV{'form.action'} eq 'newdir') {
                   1120:       $r->print('<h3>New Directory</h3>');
                   1121:   } elsif ($ENV{'form.action'} eq 'copy') {
                   1122:       $r->print('<h3>Copy</h3>');
1.22      albertel 1123:   } elsif ($ENV{'form.action'} eq 'newfile' ||
                   1124: 	   $ENV{'form.action'} eq 'newhtmlfile' ||
1.28      www      1125: 	   $ENV{'form.action'} eq 'newproblemfile' ||
                   1126:            $ENV{'form.action'} eq 'newpagefile' ||
1.30      albertel 1127:            $ENV{'form.action'} eq 'newsequencefile' ||
1.34      www      1128: 	   $ENV{'form.action'} eq 'newrightsfile' ||
                   1129: 	   $ENV{'form.action'} eq 'newstyfile' ||
1.30      albertel 1130:            $ENV{'form.action'} eq 'Select Action' ) {
1.22      albertel 1131:       $r->print('<h3>New Resource</h3>');
1.2       www      1132:   } else {
1.30      albertel 1133:      $r->print('<p>Unknown Action '.$ENV{'form.action'}.' </p></body></html>');
1.2       www      1134:      return OK;  
                   1135:   }
1.1       www      1136:   if ($ENV{'form.phase'} eq 'two') {
1.9       foxr     1137:       &Debug($r, "loncfile::handler  entering phase2");
1.4       www      1138:       &phasetwo($r,$fn,$uname,$udom);
1.1       www      1139:   } else {
1.9       foxr     1140:       &Debug($r, "loncfile::handler  entering phase1");
1.3       www      1141:       &phaseone($r,$fn,$uname,$udom);
1.1       www      1142:   }
                   1143: 
                   1144:   $r->print('</body></html>');
                   1145:   return OK;  
                   1146: }
                   1147: 
                   1148: 1;
                   1149: __END__
1.9       foxr     1150: 

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