File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.114: download - view: text, annotated - select for diffs
Mon Nov 14 00:20:31 2011 UTC (12 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- &authorspace() routine now takes an argument: $uri
  so "Construction Space" breadcrumb points at appropriate author space
  for resource/directory being viewed or acted om.
  - current role could be different or ("no role" - i.e., cm), so is only
    used to determine authorspace when $uri is unavailable.

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

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