File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.111: download - view: text, annotated - select for diffs
Mon Oct 24 22:39:21 2011 UTC (12 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #1320

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

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