File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.112: download - view: text, annotated - select for diffs
Sun Oct 30 19:27:27 2011 UTC (12 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- use lonDocRoot perlvar in place of static string: '/home/httpd/html'
- coding style: eliminate leaning toothpicks.

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

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