File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.124: download - view: text, annotated - select for diffs
Sun May 22 01:09:54 2016 UTC (7 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_uiuc, version_2_11_2_educog, version_2_11_2, HEAD
- Eliminate some <span> tags and include LC_error class in <p> tag instead.

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

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