File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.101: download - view: text, annotated - select for diffs
Thu May 28 17:08:38 2009 UTC (14 years, 11 months ago) by bisitz
Branches: MAIN
CVS tags: bz5969, bz2851, HEAD, BZ5971-printing-apage
Changes related to modification of LON-CAPA screen header.
Added dynamic link to Construction Space to breadcrumbs

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

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