File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.51: download - view: text, annotated - select for diffs
Tue Feb 17 22:06:10 2004 UTC (20 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Modified to permit processing of testbank uploads and IMS package uploads.

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

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