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

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