File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.127: download - view: text, annotated - select for diffs
Fri Jul 14 23:20:15 2023 UTC (9 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Remove trailing whitespace.  No code changes.

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

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