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

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

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