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

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.84    ! albertel   12: # $Id: loncfile.pm,v 1.83 2007/04/26 21:17:16 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.84    ! albertel  343:     if ($dest =~ /\.(\d+)\.(\w+)$/){
        !           344: 	$request->print('<span class="LC_error">'
        !           345: 			.&mt('Bad filename [_1].<br /> <tt>(name).(number).(extension)</tt> not allowed. <br /> Removing the <tt>.number.</tt> from requested filename.',$dest)
        !           346: 			.'</span>');
        !           347: 	$dest =~ s/\.(\d+)(\.\w+)$/$2/;
        !           348:     }
1.58      albertel  349:     if ($foundbad) {
1.83      albertel  350: 	$request->print("<p><span class=\"LC_error\">".&mt('Invalid characters in requested name have been removed.')."</span></p>");
1.32      albertel  351:     }
                    352:     return $dest;
                    353: }
                    354: 
1.36      www       355: sub relativeDest {
                    356:     my ($fn,$newfilename,$uname)=@_;
                    357:     if ($newfilename=~/^\//) {
                    358: # absolute, simply add path
                    359: 	$newfilename='/home/'.$uname.'/public_html/';
                    360:     } else {
                    361: 	my $dir=$fn;
                    362: 	$dir=~s/\/[^\/]+$//;
                    363: 	$newfilename=$dir.'/'.$newfilename;
                    364:     }
                    365:     $newfilename=~s://+:/:g; # remove duplicate /
                    366:     while ($newfilename=~m:/\.\./:) {
                    367: 	$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    368:     }
                    369:     return $newfilename;
                    370: }
                    371: 
1.12      foxr      372: =pod
                    373: 
                    374: =item CloseForm1($request, $user, $file)
                    375: 
                    376:    Close of a form on the successful completion of phase 1 processing
                    377: 
                    378: Parameters:
                    379: 
                    380: =over 4
                    381: 
                    382: =item  $request - Apache Request Object [in] - Apache server request object.
                    383: 
1.13      foxr      384: =item  $cancelurl - the url to go to on cancel.
1.12      foxr      385: 
                    386: =back
                    387: 
                    388: =cut
                    389: 
                    390: sub CloseForm1 {
1.55      albertel  391:     my ($request,  $fn) = @_;
                    392:     $request->print('<p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
                    393:     $request->print('<form action="'.&url($fn).
                    394: 		    '" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.12      foxr      395: }
                    396: 
                    397: 
                    398: =pod
                    399: 
                    400: =item CloseForm2($request, $user, $directory)
                    401: 
                    402:    Successfully close off the phase 2 form.
                    403: 
                    404: Parameters:
                    405: 
                    406: =over 4
                    407: 
                    408: =item   $request    - Apache Request object [in] - The request that is being
                    409:                  executed.
                    410: 
                    411: =item   $user       - string [in] - Name of the user that is initiating the
                    412:                  request.
                    413: 
                    414: =item   $directory  - string [in] - Directory in which the operation is 
                    415:                  being done relative to the top level construction space
                    416:                  directory.
                    417: 
                    418: =back
                    419: 
                    420: =cut
                    421: 
                    422: sub CloseForm2 {
1.55      albertel  423:     my ($request, $user, $fn) = @_;
                    424:     $request->print('<h3><a href="'.&url($fn).'/">'.&mt('Done').'</a></h3>');
1.12      foxr      425: }
                    426: 
                    427: =pod
                    428: 
                    429: =item Rename1($request, $filename, $user, $domain, $dir)
                    430:  
                    431:    Perform phase 1 processing of the file rename operation.
                    432: 
                    433: Parameters:
                    434: 
                    435: =over 4
                    436: 
                    437: =item  $request   - Apache Request Object [in] The request object for the 
                    438: current request.
                    439: 
                    440: =item  $filename  - The filename relative to construction space.
                    441: 
                    442: =item  $user      - Name of the user making the request.
                    443: 
                    444: =item  $domain    - User login domain.
                    445: 
                    446: =item  $dir       - Directory specification of the path to the file.
                    447: 
                    448: =back
                    449: 
                    450: Side effects:
                    451: 
                    452: =over 4
                    453: 
                    454: =item A new form is displayed prompting for confirmation.  The newfilename
                    455: hidden field of this form is loaded with
                    456: new filename relative to the current directory ($dir).
                    457: 
                    458: =back
                    459: 
                    460: =cut  
                    461: 
                    462: sub Rename1 {
1.52      albertel  463:     my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
1.36      www       464: 
                    465:     if(-e $fn) {
                    466: 	if($newfilename) {
1.42      albertel  467: 	    # is dest a dir
1.52      albertel  468: 	    if ($style eq 'move') {
                    469: 		if (-d $newfilename) {
                    470: 		    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    471: 		}
1.42      albertel  472: 	    }
1.29      albertel  473: 	    if ($newfilename =~ m|/[^\.]+$|) {
1.36      www       474: 		#no extension add on original extension
                    475: 		if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
1.24      albertel  476: 		    $newfilename.='.'.$1;
                    477: 		}
                    478: 	    }
1.36      www       479: 	    $request->print(&checksuffix($fn, $newfilename));
1.27      albertel  480: 	    #renaming a dir, delete the trailing /
1.39      www       481:             #remove second to last element for current dir
                    482: 	    if (-d $fn) {
1.53      www       483: 		$newfilename=~/\.(\w+)$/;
                    484: 		if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
1.82      albertel  485: 		    $request->print('<br /><span classr="LC_warning">'.
1.55      albertel  486: 				    &mt('Cannot change MIME type of a directory').
1.82      albertel  487: 				    '</span>'.
1.55      albertel  488: 				    '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.53      www       489: 		    return;
                    490: 		}
1.39      www       491: 		$newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
1.27      albertel  492: 	    }
1.42      albertel  493: 	    $newfilename=~s://+:/:g; # remove duplicate /
                    494: 	    while ($newfilename=~m:/\.\./:) {
                    495: 		$newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    496: 	    }
1.83      albertel  497: 	    my ($type, $return)=&exists($user, $domain, $newfilename);
1.21      albertel  498: 	    $request->print($return);
1.83      albertel  499: 	    if ($type eq 'error') {
1.47      sakharuk  500: 		$request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.21      albertel  501: 		return;
                    502: 	    }
1.50      www       503: 	    unless (&obsolete_unpub($user,$domain,$fn)) {
                    504: 		$request->print('<h3>'.&mt('Cannot rename or move non-obsolete published file').'</h3>'.
                    505: 				'<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
                    506: 		return;
                    507: 	    }
1.52      albertel  508: 	    my $action;
                    509: 	    if ($style eq 'rename') {
                    510: 		$action=&mt('Rename');
                    511: 	    } else {
                    512: 		$action=&mt('Move');
                    513: 	    }
1.23      albertel  514: 	    $request->print('<input type="hidden" name="newfilename" value="'.
1.12      foxr      515: 			    $newfilename.
1.52      albertel  516: 			    '" /><p>'.$action.' '.&display($fn).
1.36      www       517: 			    '</tt><br />to '.&display($newfilename).'?</p>');
                    518: 	    &CloseForm1($request, $fn);
1.12      foxr      519: 	} else {
1.47      sakharuk  520: 	    $request->print('<p>'.&mt('No new filename specified.').'</p></form>');
1.12      foxr      521: 	    return;
                    522: 	}
                    523:     } else {
1.47      sakharuk  524: 	$request->print('<p> '.&mt('No such file').': '.&display($fn).'</p></form>');
1.12      foxr      525: 	return;
                    526:     }
                    527:     
                    528: }
1.55      albertel  529: 
1.12      foxr      530: =pod
                    531: 
                    532: =item Delete1
                    533: 
                    534:    Performs phase 1 processing of the delete operation.  In phase one
                    535:   we just check to be sure the file exists.
                    536: 
                    537: Parameters:
                    538: 
                    539: =over 4
                    540: 
1.36      www       541: =item   $request   - Apache Request Object [in] request object for the current 
1.12      foxr      542:                 request.
                    543: 
1.36      www       544: =item   $user      - string [in]  Name of the user initiating the request.
1.12      foxr      545: 
1.36      www       546: =item   $domain    - string [in]  Domain the initiating user is logged in as
1.13      foxr      547: 
1.36      www       548: =item   $filename  - string [in]  Source filename.
1.12      foxr      549: 
                    550: =back
                    551: 
                    552: =cut
                    553: 
                    554: sub Delete1 {
1.55      albertel  555:     my ($request, $user, $domain, $fn) = @_;
1.12      foxr      556: 
1.55      albertel  557:     if( -e $fn) {
                    558: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    559: 			$fn.'"/>');
1.70      raeburn   560:         if (-d $fn) {
                    561:             unless (&empty_directory($fn,'Delete1')) {
                    562:                 $request->print('<h3>'.&mt('Only empty directories may be deleted.').'</h3>'.
                    563:                             'You must delete the contents of the directory first.<br />'.
                    564:                             '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
                    565:                 return;
                    566:             }
                    567:         } else { 
                    568: 	    unless (&obsolete_unpub($user,$domain,$fn)) {
                    569: 	        $request->print('<h3>'.&mt('Cannot delete non-obsolete published file').'</h3>'.
1.55      albertel  570: 			    '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.70      raeburn   571: 	        return;
                    572: 	    }
                    573:         }
1.55      albertel  574: 	$request->print('<p>'.&mt('Delete').' '.&display($fn).'?</p>');
                    575: 	&CloseForm1($request, $fn);
                    576:     } else {
                    577: 	$request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
1.50      www       578:     }
1.12      foxr      579: }
                    580: 
                    581: =pod
                    582: 
                    583: =item Copy1($request, $user, $domain, $filename, $newfilename)
                    584: 
                    585:    Performs phase 1 processing of the construction space copy command.
1.17      harris41  586:    Ensure that the source file exists.  Ensure that a destination exists,
                    587:    also warn if the destination already exists.
1.12      foxr      588: 
                    589: Parameters:
                    590: 
                    591: =over 4
                    592: 
                    593: =item   $request   - Apache Request Object [in] request object for the current 
                    594:                 request.
                    595: 
                    596: =item   $user      - string [in]  Name of the user initiating the request.
                    597: 
                    598: =item   $domain    - string [in]  Domain the initiating user is logged in as
                    599: 
1.36      www       600: =item   $fn  - string [in]  Source filename.
1.12      foxr      601: 
                    602: =item   $newfilename-string [in]  Destination filename.
                    603: 
                    604: =back
                    605: 
                    606: =cut
                    607: 
                    608: sub Copy1 {
1.42      albertel  609:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.12      foxr      610: 
1.42      albertel  611:     if(-e $fn) {
                    612: 	# is dest a dir
                    613: 	if (-d $newfilename) {
                    614: 	    if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
                    615: 	}
                    616: 	if ($newfilename =~ m|/[^\.]+$|) {
                    617: 	    #no extension add on original extension
                    618: 	    if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {	$newfilename.='.'.$1; }
                    619: 	} 
                    620: 	$newfilename=~s://+:/:g; # remove duplicate /
                    621: 	while ($newfilename=~m:/\.\./:) {
                    622: 	    $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
                    623: 	}
                    624: 	$request->print(&checksuffix($fn,$newfilename));
1.83      albertel  625: 	my ($type,$return)=&exists($user, $domain, $newfilename);
1.42      albertel  626: 	$request->print($return);
1.83      albertel  627: 	if ($type eq 'error') {
1.47      sakharuk  628: 	    $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.42      albertel  629: 	    return;
                    630: 	}
                    631: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    632: 			$newfilename.
1.47      sakharuk  633: 			'" /><p>'.&mt('Copy').' '.&display($fn).'<br />to '.
1.42      albertel  634: 			&display($newfilename).'?</p>');
                    635: 	&CloseForm1($request, $fn);
                    636:     } else {
1.47      sakharuk  637: 	$request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
1.21      albertel  638:     }
1.19      albertel  639: }
                    640: 
                    641: =pod
                    642: 
1.12      foxr      643: =item NewDir1
                    644:  
                    645:   Does all phase 1 processing of directory creation:
                    646:   Ensures that the user provides a new directory name,
                    647:   and that the directory does not already exist.
                    648: 
                    649: Parameters:
                    650: 
                    651: =over 4
                    652: 
                    653: =item   $request  - Apache Request Object [in] - Server request object for the
1.17      harris41  654:                current url.
1.12      foxr      655: 
                    656: =item   $username - Name of the user that is requesting the directory creation.
                    657: 
1.36      www       658: =item $domain - Domain user is in
                    659: 
                    660: =item   $fn     - source file.
1.12      foxr      661: 
                    662: =item   $newdir   - Name of the directory to be created; path relative to the 
                    663:                top level of construction space.
                    664: =back
                    665: 
                    666: Side Effects:
                    667: 
                    668: =over 4
                    669: 
                    670: =item A new form is displayed.  Clicking on the confirmation button
                    671: causes the newdir operation to transition into phase 2.  The hidden field
                    672: "newfilename" is set with the construction space path to the new directory.
                    673: 
                    674: 
                    675: =back
                    676: 
                    677: =cut
                    678: 
                    679: 
1.55      albertel  680: sub NewDir1 {
                    681:     my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
                    682: 
1.83      albertel  683:     my ($type, $result)=&exists($username,$domain,$newfilename);
                    684:     $request->print($result);
1.55      albertel  685:     if ($result) {
1.83      albertel  686: 	$request->print('</form>');
1.55      albertel  687:     } else {
                    688: 	if ($mode eq 'testbank') {
                    689: 	    $request->print('<input type="hidden" name="callingmode" value="testbank">');
                    690: 	} elsif ($mode eq 'imsimport') {
                    691: 	    $request->print('<input type="hidden" name="callingmode" value="imsimport">');
                    692: 	}
                    693: 	$request->print('<input type="hidden" name="newfilename" value="'.
                    694: 			$newfilename.'" /><p>'.&mt('Make new directory').' '.
                    695: 			&display($newfilename).'?</p>');
                    696: 	&CloseForm1($request, $fn);
                    697:     }
1.12      foxr      698: }
                    699: 
1.44      taceyjo1  700: 
                    701: sub Decompress1 {
1.55      albertel  702:     my ($request, $user, $domain, $fn) = @_;
                    703:     if( -e $fn) {
1.44      taceyjo1  704:    	$request->print('<input type="hidden" name="newfilename" value="'.$fn.'"/>');
1.47      sakharuk  705:    	$request->print('<p>'.&mt('Decompress').' '.&display($fn).'?</p>');
1.44      taceyjo1  706:    	&CloseForm1($request, $fn);
1.55      albertel  707:     } else {
                    708: 	$request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
                    709:     }
1.44      taceyjo1  710: }
1.55      albertel  711: 
1.12      foxr      712: =pod
                    713: 
1.22      albertel  714: =item NewFile1
                    715:  
                    716:   Does all phase 1 processing of file creation:
                    717:   Ensures that the user provides a new filename, adds proper extension
                    718:   if needed and that the file does not already exist, if it is a html,
                    719:   problem, page, or sequence, it then creates a form link to hand the
                    720:   actual creation off to the proper handler.
                    721: 
                    722: Parameters:
                    723: 
                    724: =over 4
                    725: 
                    726: =item   $request  - Apache Request Object [in] - Server request object for the
                    727:                current url.
                    728: 
                    729: =item   $username - Name of the user that is requesting the directory creation.
                    730: 
                    731: =item   $domain   - Name of the domain of the user
                    732: 
1.36      www       733: =item   $fn      - Source file name
1.22      albertel  734: 
                    735: =item   $newfilename
                    736:                   - Name of the file to be created; no path information
                    737: =back
                    738: 
                    739: Side Effects:
                    740: 
                    741: =over 4
                    742: 
                    743: =item 2 new forms are displayed.  Clicking on the confirmation button
                    744: causes the browser to attempt to load the specfied URL, allowing the
1.36      www       745: proper handler to take care of file creation. There is also a Cancel
1.22      albertel  746: button which returns you to the driectory listing you came from
                    747: 
                    748: =back
                    749: 
                    750: =cut
                    751: 
                    752: sub NewFile1 {
1.36      www       753:     my ($request, $user, $domain, $fn, $newfilename) = @_;
1.22      albertel  754: 
1.67      albertel  755:     if ($env{'form.action'} =~ /new(.+)file/) {
1.22      albertel  756: 	my $extension=$1;
1.40      www       757: 
                    758:         ##Informs User (name).(number).(extension) not allowed 
                    759: 	if($newfilename =~ /\.(\d+)\.(\w+)$/){
1.83      albertel  760: 	    $r->print('<span class="LC_error">'.$newfilename.
1.69      www       761: 		      ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
1.83      albertel  762: 		      ' '.&mt('Not Allowed').'</span>');
1.69      www       763: 	    return;
                    764: 	}
                    765: 	if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
1.83      albertel  766: 	    $r->print('<span class="LC_error">'.$newfilename.
1.69      www       767: 		      ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
1.83      albertel  768: 		      ' '.&mt('Not Allowed').'</span>');
1.40      www       769: 	    return;
                    770: 	}
1.22      albertel  771: 	if ($newfilename !~ /\Q.$extension\E$/) {
1.81      albertel  772: 	    if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
1.26      albertel  773: 		#already has an extension strip it and add in expected one
1.81      albertel  774: 		$newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
1.26      albertel  775: 	    }
1.22      albertel  776: 	    $newfilename.=".$extension";
                    777: 	}
1.31      albertel  778:     }
1.83      albertel  779:     my ($type, $result)=&exists($user,$domain,$newfilename);
                    780:     $request->print($result);
                    781:     if ($type eq 'error') {
                    782: 	$request->print('</form>');
1.39      www       783:     } else {
1.83      albertel  784: 	
1.47      sakharuk  785: 	$request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
1.22      albertel  786: 	$request->print('</form>');
1.36      www       787: 	$request->print('<form action="'.&url($newfilename).
1.47      sakharuk  788: 			'" method="POST"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
1.36      www       789: 	$request->print('<form action="'.&url($fn).
1.47      sakharuk  790: 			'" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.22      albertel  791:     }
                    792: }
                    793: 
                    794: =pod
                    795: 
1.12      foxr      796: =item phaseone($r, $fn, $uname, $udom)
                    797: 
                    798:   Peforms phase one processing of the request.  In phase one, error messages
                    799: are returned if the request cannot be performed (e.g. attempts to manipulate
                    800: files that are nonexistent).  If the operation can be performed, what is
                    801: about to be done will be presented to the user for confirmation.  If the
                    802: user confirms the request, then phase two is executed, the action 
                    803: performed and reported to the user.
                    804: 
                    805:  Parameters:
                    806: 
                    807: =over 4
                    808: 
                    809: =item $r  - request object [in] - The Apache request being executed.
                    810: 
                    811: =item $fn = string [in] - The filename being manipulated by the 
                    812:                              request.
                    813: 
                    814: =item $uname - string [in] Name of user logged in and doing this action.
                    815: 
1.17      harris41  816: =item $udom  - string [in] Domain name under which the user logged in. 
1.12      foxr      817: 
                    818: =back
                    819: 
                    820: =cut
1.8       albertel  821: 
1.1       www       822: sub phaseone {
1.55      albertel  823:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr      824:   
1.58      albertel  825:     my $doingdir=0;
1.67      albertel  826:     if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
                    827:     my $newfilename=&cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname);
1.55      albertel  828:     $newfilename=&relativeDest($fn,$newfilename,$uname);
                    829:     $r->print('<form action="/adm/cfile" method="post">'.
                    830: 	      '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
                    831: 	      '<input type="hidden" name="phase" value="two" />'.
1.67      albertel  832: 	      '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
1.12      foxr      833:   
1.67      albertel  834:     if ($env{'form.action'} eq 'rename') {
1.55      albertel  835: 	&Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
1.67      albertel  836:     } elsif ($env{'form.action'} eq 'move') {
1.55      albertel  837: 	&Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
1.67      albertel  838:     } elsif ($env{'form.action'} eq 'delete') { 
1.55      albertel  839: 	&Delete1($r, $uname, $udom, $fn);
1.67      albertel  840:     } elsif ($env{'form.action'} eq 'decompress') {
1.55      albertel  841: 	&Decompress1($r, $uname, $udom, $fn);
1.67      albertel  842:     } elsif ($env{'form.action'} eq 'copy') { 
1.55      albertel  843: 	if($newfilename) {
                    844: 	    &Copy1($r, $uname, $udom, $fn, $newfilename);
                    845: 	} else {
                    846: 	    $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
                    847: 	}
1.67      albertel  848:     } elsif ($env{'form.action'} eq 'newdir') {
1.55      albertel  849: 	my $mode = '';
1.67      albertel  850: 	if (exists($env{'form.callingmode'}) ) {
                    851: 	    $mode = $env{'form.callingmode'};
1.55      albertel  852: 	}   
                    853: 	&NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
1.67      albertel  854:     }  elsif ($env{'form.action'} eq 'newfile' ||
                    855: 	      $env{'form.action'} eq 'newhtmlfile' ||
                    856: 	      $env{'form.action'} eq 'newproblemfile' ||
                    857: 	      $env{'form.action'} eq 'newpagefile' ||
                    858: 	      $env{'form.action'} eq 'newsequencefile' ||
                    859: 	      $env{'form.action'} eq 'newrightsfile' ||
                    860: 	      $env{'form.action'} eq 'newstyfile' ||
                    861:               $env{'form.action'} eq 'newlibraryfile' ||
                    862: 	      $env{'form.action'} eq 'Select Action') {
1.65      www       863:         my $empty=&mt('Type Name Here');
                    864: 	if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
1.55      albertel  865: 	    &NewFile1($r, $uname, $udom, $fn, $newfilename);
                    866: 	} else {
                    867: 	    $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
                    868: 	}
                    869:     }
1.12      foxr      870: }
                    871: 
                    872: =pod
                    873: 
                    874: =item Rename2($request, $user, $directory, $oldfile, $newfile)
                    875: 
1.17      harris41  876: Performs phase 2 processing of a rename reequest.   This is where the
1.12      foxr      877: actual rename is performed.
                    878: 
                    879: Parameters
                    880: 
                    881: =over 4
                    882: 
                    883: =item $request - Apache request object [in] The request being processed.
                    884: 
                    885: =item $user  - string [in] The name of the user initiating the request.
                    886: 
                    887: =item $directory - string [in] The name of the directory relative to the
                    888:                  construction space top level of the renamed file.
                    889: 
                    890: =item $oldfile - Name of the file.
                    891: 
                    892: =item $newfile - Name of the new file.
                    893: 
                    894: =back
                    895: 
                    896: Returns:
                    897: 
                    898: =over 4
                    899: 
                    900: =item 1 Success.
                    901: 
                    902: =item 0 Failure.
                    903: 
                    904: =cut
                    905: 
                    906: sub Rename2 {
                    907: 
1.55      albertel  908:     my ($request, $user, $directory, $oldfile, $newfile) = @_;
1.12      foxr      909: 
1.55      albertel  910:     &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
                    911: 	   " new file ".$newfile."\n");
                    912:     &Debug($request, "Target is: ".$directory.'/'.
                    913: 	   $newfile);
                    914:     if (-e $oldfile) {
                    915: 
                    916: 	my $oRN=$oldfile;
                    917: 	my $nRN=$newfile;
                    918: 	unless (rename($oldfile,$newfile)) {
1.83      albertel  919: 	    $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel  920: 	    return 0;
                    921: 	}
                    922: 	## If old name.(extension) exits, move under new name.
                    923: 	## If it doesn't exist and a new.(extension) exists  
                    924: 	## delete it (only concern when renaming over files)
                    925: 	my $tmp1=$oRN.'.meta';
                    926: 	my $tmp2=$nRN.'.meta';
                    927: 	if(-e $tmp1){
                    928: 	    unless(rename($tmp1,$tmp2)){ }
                    929: 	} elsif(-e $tmp2){
                    930: 	    unlink $tmp2;
                    931: 	}
                    932: 	$tmp1=$oRN.'.save';
                    933: 	$tmp2=$nRN.'.save';
                    934: 	if(-e $tmp1){
                    935: 	    unless(rename($tmp1,$tmp2)){ }
                    936: 	} elsif(-e $tmp2){
                    937: 	    unlink $tmp2;
                    938: 	}
                    939: 	$tmp1=$oRN.'.log';
                    940: 	$tmp2=$nRN.'.log';
                    941: 	if(-e $tmp1){
                    942: 	    unless(rename($tmp1,$tmp2)){ }
                    943: 	} elsif(-e $tmp2){
                    944: 	    unlink $tmp2;
                    945: 	}
                    946: 	$tmp1=$oRN.'.bak';
                    947: 	$tmp2=$nRN.'.bak';
                    948: 	if(-e $tmp1){
                    949: 	    unless(rename($tmp1,$tmp2)){ }
                    950: 	} elsif(-e $tmp2){
                    951: 	    unlink $tmp2;
                    952: 	}
                    953:     } else {
                    954: 	$request->print("<p> ".&mt('No such file').": ".&display($oldfile).'</p></form>');
                    955: 	return 0;
                    956:     }
                    957:     return 1;
1.12      foxr      958: }
1.55      albertel  959: 
1.12      foxr      960: =pod
                    961: 
                    962: =item Delete2($request, $user, $filename)
                    963: 
                    964:   Performs phase two of a delete.  The user has confirmed that they want 
                    965: to delete the selected file.   The file is deleted and the results of the
                    966: delete attempt are indicated.
                    967: 
                    968: Parameters:
                    969: 
                    970: =over 4
                    971: 
                    972: =item $request - Apache Request object [in] the request object for the current
                    973:                  delete operation.
                    974: 
                    975: =item $user    - string [in]  The name of the user initiating the delete
                    976:                  request.
                    977: 
1.17      harris41  978: =item $filename - string [in] The name of the file, relative to construction
                    979:                   space, to delete.
1.12      foxr      980: 
                    981: =back
                    982: 
                    983: Returns:
                    984:   1 - success.
                    985:   0 - Failure.
                    986: 
                    987: =cut
                    988: 
                    989: sub Delete2 {
1.55      albertel  990:     my ($request, $user, $filename) = @_;
1.70      raeburn   991:     if (-d $filename) { 
                    992: 	unless (&empty_directory($filename,'Delete2')) { 
1.83      albertel  993: 	    $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>'); 
1.55      albertel  994: 	    return 0;
                    995: 	} else {   
                    996: 	    if(-e $filename) {
                    997: 		unless(rmdir($filename)) {
1.83      albertel  998: 		    $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel  999: 		    return 0;
                   1000: 		}
                   1001: 	    } else {
                   1002: 		$request->print('<p> '.&mt('No such file').'. </p></form>');
                   1003: 		return 0;
                   1004: 	    }
                   1005: 	}
1.48      taceyjo1 1006:     } else {
1.55      albertel 1007: 	if(-e $filename) {
                   1008: 	    unless(unlink($filename)) {
1.83      albertel 1009: 		$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1010: 		return 0;
                   1011: 	    }
                   1012: 	} else {
                   1013: 	    $request->print('<p> '.&mt('No such file').'. </p></form>');
                   1014: 	    return 0;
1.46      taceyjo1 1015: 	}
1.55      albertel 1016:     }
                   1017:     return 1;
1.12      foxr     1018: }
                   1019: 
                   1020: =pod
                   1021: 
                   1022: =item Copy2($request, $username, $dir, $oldfile, $newfile)
                   1023: 
                   1024:    Performs phase 2 of a copy.  The file is copied and the status 
                   1025:    of that copy is reported back to the user.
                   1026: 
                   1027: =over 4
                   1028: 
                   1029: =item $request - Apache request object [in]; the apache request currently
                   1030:                  being executed.
                   1031: 
                   1032: =item $username - string [in] Name of the user who is requesting the copy.
                   1033: 
                   1034: =item $dir - string [in] Directory path relative to the construction space
                   1035:              of the destination file.
                   1036: 
                   1037: =item $oldfile - string [in] Name of the source file.
                   1038: 
                   1039: =item $newfile - string [in] Name of the destination file.
                   1040: 
                   1041: 
                   1042: =back
                   1043: 
1.71      raeburn  1044: Returns 0 failure, and 1 successs.
1.12      foxr     1045: 
                   1046: =cut
1.1       www      1047: 
1.12      foxr     1048: sub Copy2 {
                   1049:     my ($request, $username, $dir, $oldfile, $newfile) = @_;
                   1050:     &Debug($request ,"Will try to copy $oldfile to $newfile");
                   1051:     if(-e $oldfile) {
1.71      raeburn  1052:         if ($oldfile eq $newfile) {
1.83      albertel 1053:             $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  1054:             return 1;
                   1055:         }
1.12      foxr     1056: 	unless (copy($oldfile, $newfile)) {
1.83      albertel 1057: 	    $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1.12      foxr     1058: 	    return 0;
1.61      albertel 1059: 	} elsif (!chmod(0660, $newfile)) {
1.83      albertel 1060: 	    $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1.61      albertel 1061: 	    return 0;
                   1062: 	} elsif (-e $oldfile.'.meta' && 
                   1063: 		 !copy($oldfile.'.meta', $newfile.'.meta') &&
                   1064: 		 !chmod(0660, $newfile.'.meta')) {
1.83      albertel 1065: 	    $request->print('<span class="LC_error">'.&mt('copy metadata error').
                   1066: 			    ': '.$!.'</span>');
1.61      albertel 1067: 	    return 0;
1.12      foxr     1068: 	} else {
                   1069: 	    return 1;
                   1070: 	}
1.11      albertel 1071:     } else {
1.47      sakharuk 1072: 	$request->print('<p> '.&mt('No such file').' </p>');
1.12      foxr     1073: 	return 0;
1.11      albertel 1074:     }
1.12      foxr     1075:     return 1;
                   1076: }
1.55      albertel 1077: 
1.12      foxr     1078: =pod
                   1079: 
                   1080: =item NewDir2($request, $user, $newdirectory)
1.1       www      1081: 
1.12      foxr     1082: 	Performs phase 2 processing of directory creation.  This involves creating the directory and
                   1083: 	reporting the results of that creation to the user.
                   1084: 	
                   1085: Parameters:
                   1086: =over 4
                   1087: 
                   1088: =item $request  - Apache request object [in].  Object representing the current HTTP request.
                   1089: 
                   1090: =item $user - string [in] The name of the user that is initiating the request.
                   1091: 
                   1092: =item $newdirectory - string [in] The full path of the directory being created.
                   1093: 
                   1094: =back
                   1095: 
                   1096: Returns 0 - failure 1 - success.
                   1097: 
                   1098: =cut
1.8       albertel 1099: 
1.12      foxr     1100: sub NewDir2 {
1.55      albertel 1101:     my ($request, $user, $newdirectory) = @_;
1.12      foxr     1102:   
1.55      albertel 1103:     unless(mkdir($newdirectory, 02770)) {
1.83      albertel 1104: 	$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1105: 	return 0;
                   1106:     }
                   1107:     unless(chmod(02770, ($newdirectory))) {
1.83      albertel 1108: 	$request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55      albertel 1109: 	return 0;
                   1110:     }
                   1111:     return 1;
1.1       www      1112: }
1.55      albertel 1113: 
1.44      taceyjo1 1114: sub decompress2 {
1.55      albertel 1115:     my ($r, $user, $dir, $file) = @_;
                   1116:     &Apache::lonnet::appenv('cgi.file' => $file);
                   1117:     &Apache::lonnet::appenv('cgi.dir' => $dir);
                   1118:     my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
                   1119:     $r->print($result);
                   1120:     &Apache::lonnet::delenv('cgi.file');
                   1121:     &Apache::lonnet::delenv('cgi.dir');
                   1122:     return 1;
1.44      taceyjo1 1123: }
1.55      albertel 1124: 
1.12      foxr     1125: =pod
                   1126: 
                   1127: =item phasetwo($r, $fn, $uname, $udom)
                   1128: 
                   1129:    Controls the phase 2 processing of file management
                   1130:    requests for construction space.  In phase one, the user
                   1131:    was asked to confirm the operation.  In phase 2, the operation
                   1132:    is performed and the result is shown.
                   1133: 
                   1134:   The strategy is to break out the processing into specific action processors
                   1135:   named action2 where action is the requested action and the 2 denotes 
                   1136:   phase 2 processing.
                   1137: 
                   1138: Parameters:
                   1139: 
                   1140: =over 4
                   1141: 
                   1142: =item  $r     - Apache Request object [in] The request object for this httpd
                   1143:            transaction.
                   1144: 
                   1145: =item  $fn    - string [in]  A filename indicating the object that is being
                   1146:            manipulated.
                   1147: 
                   1148: =item  $uname - string [in] The name of the user initiating the file management
                   1149:            request.
                   1150: 
                   1151: =item  $udom  - string  [in] The login domain of the user initiating the
                   1152:            file management request.
                   1153: =back
                   1154: 
                   1155: =cut
                   1156: 
1.1       www      1157: sub phasetwo {
                   1158:     my ($r,$fn,$uname,$udom)=@_;
1.12      foxr     1159:     
1.9       foxr     1160:     &Debug($r, "loncfile - Entering phase 2 for $fn");
1.12      foxr     1161:     
1.78      banghart 1162:     # Break down the file into its component pieces.
1.12      foxr     1163:     
1.27      albertel 1164:     my $dir;		# Directory path
                   1165:     my $main;		# Filename.
                   1166:     my $suffix;		# Extension.
1.46      taceyjo1 1167:     if ($fn=~m:(.*)/([^/]+):) {
1.27      albertel 1168: 	$dir=$1;		# Directory path
                   1169: 	$main=$2;		# Filename.
1.54      albertel 1170:     }
                   1171:     if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1.68      albertel 1172: 	$suffix=$1; #This is the actually filename extension if it exists
1.66      albertel 1173: 	$main=~s/\.\w+$//; #strip the extension
1.54      albertel 1174:     }
1.79      banghart 1175:     my $dest;                       #
                   1176:     my $dest_dir;                   # On success this is where we'll go.
                   1177:     my $disp_newname;               #
                   1178:     my $dest_newname;               #
1.55      albertel 1179:     &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1.67      albertel 1180:     &Debug($r,"    newfilename = ".$env{'form.newfilename'});
1.9       foxr     1181: 
                   1182:     my $conspace=$fn;
1.12      foxr     1183:     
1.55      albertel 1184:     &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1.12      foxr     1185:     
1.67      albertel 1186:     &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1.12      foxr     1187:     
                   1188:     # Select the appropriate processing sub.
1.67      albertel 1189:     if ($env{'form.action'} eq 'decompress') { 
1.66      albertel 1190: 	$main .= '.'.$suffix;
1.55      albertel 1191: 	if(!&decompress2($r, $uname, $dir, $main)) {
                   1192: 	    return ;
                   1193: 	}
                   1194: 	$dest = $dir."/.";
1.67      albertel 1195:     } elsif ($env{'form.action'} eq 'rename' ||
                   1196: 	     $env{'form.action'} eq 'move') {
                   1197: 	if($env{'form.newfilename'}) {
1.27      albertel 1198: 	    if (!defined($dir)) {
                   1199: 		$fn=~m:^(.*)/:;
1.55      albertel 1200: 		$dir=$1; 
1.27      albertel 1201: 	    }
1.67      albertel 1202: 	    if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.12      foxr     1203: 		return;
                   1204: 	    }
1.78      banghart 1205: 	    $dest = $dir."/";
1.79      banghart 1206: 	    $dest_newname = $env{'form.newfilename'};
                   1207: 	    $env{'form.newfilename'} =~ /.+(\/.+$)/;
                   1208: 	    $disp_newname = $1;
                   1209: 	    $disp_newname =~ s/\///;
1.12      foxr     1210: 	}
1.67      albertel 1211:     } elsif ($env{'form.action'} eq 'delete') { 
                   1212: 	if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1.12      foxr     1213: 	    return ;
                   1214: 	}
                   1215: 	# Once a resource is deleted, we just list the directory that
                   1216: 	# previously held it.
                   1217: 	#
1.20      albertel 1218: 	$dest = $dir."/.";		# Parent dir.
1.67      albertel 1219:     } elsif ($env{'form.action'} eq 'copy') { 
                   1220: 	if($env{'form.newfilename'}) {
                   1221: 	    if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.44      taceyjo1 1222: 		return ;
1.55      albertel 1223: 	    }
1.67      albertel 1224: 	    $dest = $env{'form.newfilename'};
1.55      albertel 1225:      	} else {
1.47      sakharuk 1226: 	    $r->print('<p>'.&mt('No New filename specified').'</p></form>');
1.12      foxr     1227: 	    return;
                   1228: 	}
                   1229: 	
1.67      albertel 1230:     } elsif ($env{'form.action'} eq 'newdir') {
                   1231:         my $newdir= $env{'form.newfilename'};
1.12      foxr     1232: 	if(!&NewDir2($r, $uname, $newdir)) {
                   1233: 	    return;
                   1234: 	}
1.55      albertel 1235: 	$dest = $newdir."/";
1.12      foxr     1236:     }
1.67      albertel 1237:     if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1.55      albertel 1238: 	$r->print('<h3><a href="javascript:self.close()">'.&mt('Done').'</a></h3>');
1.51      raeburn  1239:     } else {
1.79      banghart 1240:         if ($env{'form.action'} eq 'rename') {
                   1241:             $r->print('<h3><a href="'.&url($dest).'">'.&mt('Return to Directory').'</a></h3>');
                   1242:             $r->print('<h3><a href="'.&url($dest_newname).'">'.$disp_newname.'</a></h3>');
                   1243:         } else {
                   1244: 	    $r->print('<h3><a href="'.&url($dest).'">'.&mt('Done').'</a></h3>');
                   1245: 	}
1.51      raeburn  1246:     }
1.1       www      1247: }
                   1248: 
                   1249: sub handler {
                   1250: 
1.55      albertel 1251:     $r=shift;
1.1       www      1252: 
1.60      www      1253:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename']);
1.9       foxr     1254: 
1.55      albertel 1255:     &Debug($r, "loncfile.pm - handler entered");
1.67      albertel 1256:     &Debug($r, " filename: ".$env{'form.filename'});
                   1257:     &Debug($r, " newfilename: ".$env{'form.newfilename'});
1.36      www      1258: #
                   1259: # Determine the root filename
                   1260: # This could come in as "filename", which actually is a URL, or
                   1261: # as "qualifiedfilename", which is indeed a real filename in filesystem
                   1262: #
1.55      albertel 1263:     my $fn;
1.1       www      1264: 
1.67      albertel 1265:     if ($env{'form.filename'}) {
                   1266: 	&Debug($r, "test: $env{'form.filename'}");
1.77      www      1267: 	$fn=&unescape($env{'form.filename'});
1.55      albertel 1268: 	$fn=&URLToPath($fn);
1.67      albertel 1269:     }  elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {  
1.55      albertel 1270: 	#Just hijack the script only the first time around to inject the
                   1271: 	#correct information for further processing
1.77      www      1272: 	$fn=&unescape($env{'form.decompress'});
1.44      taceyjo1 1273: 	$fn=&URLToPath($fn);
1.67      albertel 1274: 	$env{'form.action'}="decompress";
                   1275:     } elsif ($env{'form.qualifiedfilename'}) {
                   1276: 	$fn=$env{'form.qualifiedfilename'};
1.55      albertel 1277:     } else {
                   1278: 	&Debug($r, "loncfile::handler - no form.filename");
1.67      albertel 1279: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.55      albertel 1280: 		       ' unspecified filename for cfile', $r->filename); 
                   1281: 	return HTTP_NOT_FOUND;
                   1282:     }
1.44      taceyjo1 1283: 
1.55      albertel 1284:     unless ($fn) { 
                   1285: 	&Debug($r, "loncfile::handler - doctored url is empty");
1.67      albertel 1286: 	$r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.55      albertel 1287: 		       ' trying to cfile non-existing file', $r->filename); 
                   1288: 	return HTTP_NOT_FOUND;
                   1289:     } 
1.1       www      1290: 
                   1291: # ----------------------------------------------------------- Start page output
1.55      albertel 1292:     my $uname;
                   1293:     my $udom;
                   1294: 
                   1295:     ($uname,$udom)=
                   1296: 	&Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                   1297:     &Debug($r, 
                   1298: 	   "loncfile::handler constructaccess uname = $uname domain = $udom");
                   1299:     unless (($uname) && ($udom)) {
                   1300: 	$r->log_reason($uname.' at '.$udom.
1.67      albertel 1301: 		       ' trying to manipulate file '.$env{'form.filename'}.
1.55      albertel 1302: 		       ' ('.$fn.') - not authorized', 
                   1303: 		       $r->filename); 
                   1304: 	return HTTP_NOT_ACCEPTABLE;
                   1305:     }
                   1306: 
1.1       www      1307: 
1.55      albertel 1308:     &Apache::loncommon::content_type($r,'text/html');
                   1309:     $r->send_http_header;
                   1310: 
1.75      albertel 1311:     my (%loaditem,$js);
                   1312: 
1.67      albertel 1313:     if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
                   1314: 	my $newdirname = $env{'form.newfilename'};
1.75      albertel 1315: 	$js = qq|
                   1316: <script type="text/javascript">
1.51      raeburn  1317: function writeDone() {
                   1318:     var winName = window.opener
                   1319:     window.focus();
                   1320:     winName.document.dataForm.newdir.value = "$newdirname"
                   1321:     setTimeout("self.close()",10000)
                   1322: }
                   1323:   </script>
1.75      albertel 1324: |;
1.76      albertel 1325: 	$loaditem{'onload'} = "writeDone()";
1.55      albertel 1326:     }
1.75      albertel 1327:     
                   1328:     $r->print(&Apache::loncommon::start_page('Construction Space File Operation',
                   1329: 					     $js,
                   1330: 					     {'add_entries' => \%loaditem,}));
1.1       www      1331:   
1.55      albertel 1332:     $r->print('<h3>'.&mt('Location').': '.&display($fn).'</h3>');
1.1       www      1333:   
1.67      albertel 1334:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.83      albertel 1335: 	$r->print('<h3><span class="LC_error">'.&mt('Co-Author').': '.$uname.' at '.$udom.
                   1336: 		  '</span></h3>');
1.55      albertel 1337:     }
                   1338: 
                   1339: 
1.67      albertel 1340:     &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
                   1341:     if ($env{'form.action'} eq 'delete') {
1.55      albertel 1342:       	$r->print('<h3>'.&mt('Delete').'</h3>');
1.67      albertel 1343:     } elsif ($env{'form.action'} eq 'rename') {
1.55      albertel 1344: 	$r->print('<h3>'.&mt('Rename').'</h3>');
1.67      albertel 1345:     } elsif ($env{'form.action'} eq 'move') {
1.55      albertel 1346: 	$r->print('<h3>'.&mt('Move').'</h3>');
1.67      albertel 1347:     } elsif ($env{'form.action'} eq 'newdir') {
1.55      albertel 1348: 	$r->print('<h3>'.&mt('New Directory').'</h3>');
1.67      albertel 1349:     } elsif ($env{'form.action'} eq 'decompress') {
1.55      albertel 1350: 	$r->print('<h3>'.&mt('Decompress').'</h3>');
1.67      albertel 1351:     } elsif ($env{'form.action'} eq 'copy') {
1.55      albertel 1352: 	$r->print('<h3>'.&mt('Copy').'</h3>');
1.67      albertel 1353:     } elsif ($env{'form.action'} eq 'newfile' ||
                   1354: 	     $env{'form.action'} eq 'newhtmlfile' ||
                   1355: 	     $env{'form.action'} eq 'newproblemfile' ||
                   1356: 	     $env{'form.action'} eq 'newpagefile' ||
                   1357: 	     $env{'form.action'} eq 'newsequencefile' ||
                   1358: 	     $env{'form.action'} eq 'newrightsfile' ||
                   1359: 	     $env{'form.action'} eq 'newstyfile' ||
                   1360:              $env{'form.action'} eq 'newlibraryfile' ||
                   1361: 	     $env{'form.action'} eq 'Select Action' ) {
1.55      albertel 1362: 	$r->print('<h3>'.&mt('New Resource').'</h3>');
                   1363:     } else {
1.75      albertel 1364: 	$r->print('<p>'.&mt('Unknown Action').' '.$env{'form.action'}.' </p>'.
                   1365: 		  &Apache::loncommon::end_page());
1.55      albertel 1366: 	return OK;  
                   1367:     }
1.67      albertel 1368:     if ($env{'form.phase'} eq 'two') {
1.55      albertel 1369: 	&Debug($r, "loncfile::handler  entering phase2");
                   1370: 	&phasetwo($r,$fn,$uname,$udom);
                   1371:     } else {
                   1372: 	&Debug($r, "loncfile::handler  entering phase1");
                   1373: 	&phaseone($r,$fn,$uname,$udom);
                   1374:     }
1.1       www      1375: 
1.75      albertel 1376:     $r->print(&Apache::loncommon::end_page());
1.55      albertel 1377:     return OK;  
1.1       www      1378: }
                   1379: 
                   1380: 1;
                   1381: __END__
1.9       foxr     1382: 

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