File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.109: download - view: text, annotated - select for diffs
Sun Oct 23 01:27:34 2011 UTC (12 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
... bug 1320 ...

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

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