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

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

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