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

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

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