File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.10: download - view: text, annotated - select for diffs
Mon May 27 03:18:46 2002 UTC (22 years ago) by foxr
Branches: MAIN
CVS tags: HEAD
Add a sub for URL -> Path conversion, use it in handler...
  still must look for applications eleswhere in the module.

    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: #
   13: # $Id: loncfile.pm,v 1.10 2002/05/27 03:18:46 foxr Exp $
   14: #
   15: # Copyright Michigan State University Board of Trustees
   16: #
   17: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   18: #
   19: # LON-CAPA is free software; you can redistribute it and/or modify
   20: # it under the terms of the GNU General Public License as published by
   21: # the Free Software Foundation; either version 2 of the License, or
   22: # (at your option) any later version.
   23: #
   24: # LON-CAPA is distributed in the hope that it will be useful,
   25: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   26: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   27: # GNU General Public License for more details.
   28: #
   29: # You should have received a copy of the GNU General Public License
   30: # along with LON-CAPA; if not, write to the Free Software
   31: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   32: #
   33: # /home/httpd/html/adm/gpl.txt
   34: #
   35: # http://www.lon-capa.org/
   36: #
   37: #
   38: # (Handler to retrieve an old version of a file
   39: #
   40: # (Publication Handler
   41: # 
   42: # (TeX Content Handler
   43: #
   44: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   45: #
   46: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   47: # 03/23 Guy Albertelli
   48: # 03/24,03/29 Gerd Kortemeyer)
   49: #
   50: # 03/31,04/03,05/02,05/09,06/23,06/24 Gerd Kortemeyer)
   51: #
   52: # 06/23 Gerd Kortemeyer
   53: # 05/07/02 Ron Fox:
   54: #           - Added Debug log output so that I can trace what the heck this
   55: #             undocumented thingy does.
   56: 
   57: package Apache::loncfile;
   58: 
   59: use strict;
   60: use Apache::File;
   61: use File::Copy;
   62: use Apache::Constants qw(:common :http :methods);
   63: use Apache::loncacc;
   64: use Apache::Log ();
   65: 
   66: my $DEBUG=0;
   67: my $r;				# Needs to be global for some stuff RF.
   68: #
   69: #  Debug
   70: #    If debugging is enabled puts out a debuggin message determined by the
   71: #    caller.  The debug message goes to the Apache error log file.
   72: #
   73: #  Parameters:
   74: #     r       - Apache request [in]
   75: #     message - String [in] 
   76: # Returns:
   77: #    nothing.
   78: sub Debug {
   79:     my $r       = shift;
   80:     my $log     = $r->log;
   81:     my $message = shift;
   82:     if ($DEBUG) {
   83: 	$log->debug($message);    
   84:     }
   85: }
   86: #
   87: #   URLToPath
   88: #     Convert a URL to a file system path.
   89: #
   90: #   In order to manipulate the construction space objects, it's necessary
   91: #   to access url identified objects a filespace objects.  This function
   92: #   translates a construction space URL to a file system path.
   93: # Parameters:
   94: #    Url    - string [in] The url to convert.
   95: # Returns:
   96: #    The corresponing file system path.
   97: sub URLToPath
   98: {
   99:     my $Url = shift;
  100:     &Debug($r, "UrlToPath got: $Url");
  101:     $Url=~ s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
  102:     $Url=~ s/^http\:\/\/[^\/]+//;
  103:     &Debug($r, "Returning $Url \n");
  104:     return $Url;
  105: }
  106: sub exists {
  107:     my ($uname,$udom,$dir,$newfile)=@_;
  108:     my $published='/home/httpd/html/res/'.$udom.'/'.$uname.'/'.$dir.'/'.
  109: 	$ENV{'form.newfilename'};
  110:     my $construct='/home/'.$uname.'/public_html/'.$dir.'/'.
  111: 	$ENV{'form.newfilename'};
  112:     my $result;
  113:     if (-e $published) {
  114: 	$result.='<p><font color=red>Warning: target file exists, and has been published!</font></p>';
  115:     } elsif ( -e $construct ) {
  116: 	$result.='<p><font color=red>Warning: target file exists!</font></p>';
  117:     }
  118:     return $result;
  119: }
  120: 
  121: sub checksuffix {
  122:     my ($old,$new) = @_;
  123:     my $result;
  124:     my $oldsuffix;
  125:     my $newsuffix;
  126:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
  127:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
  128:     if ($oldsuffix ne $newsuffix) {
  129: 	$result.='<p><font color=red>Warning: change of MIME type!</font></p>';
  130:     }
  131:     return $result;
  132: }
  133: 
  134: sub phaseone {
  135:     my ($r,$fn,$uname,$udom)=@_;
  136: 
  137:     $fn=~m:(.*)/([^/]+)\.(\w+)$:;
  138:     my $dir=$1;
  139:     my $main=$2;
  140:     my $suffix=$3;
  141: 
  142:     my $conspace='/home/'.$uname.'/public_html'.$fn;
  143: 
  144:     $r->print('<form action=/adm/cfile method=post>'.
  145: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
  146:               '<input type=hidden name=phase value=two>'.
  147:               '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
  148: 
  149:     if ($ENV{'form.action'} eq 'rename') {
  150: 	if (-e $conspace) {
  151: 	    if ($ENV{'form.newfilename'}) {
  152: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
  153: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
  154: 	       $r->print('<input type=hidden name=newfilename value="'.
  155:                          $ENV{'form.newfilename'}.
  156:                          '"><p>Rename <tt>'.$fn.'</tt> to <tt>'.
  157:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
  158: 	    } else {
  159: 	       $r->print('<p>No new filename specified.</p></form>');
  160:                return;
  161: 	    }
  162:         } else {
  163: 	    $r->print('<p>No such file.</p></form>');
  164:             return;
  165:         }
  166:     } elsif ($ENV{'form.action'} eq 'delete') { 
  167: 	if (-e $conspace) {
  168:             $r->print('<p>Delete <tt>'.$fn.'</tt>?</p>');
  169:         } else {
  170: 	    $r->print('<p>No such file.</p></form>');
  171:             return;
  172:         }
  173:     } elsif ($ENV{'form.action'} eq 'copy') { 
  174: 	if (-e $conspace) {
  175: 	    if ($ENV{'form.newfilename'}) {
  176: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
  177: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
  178: 	       $r->print('<input type=hidden name=newfilename value="'.
  179:                          $ENV{'form.newfilename'}.
  180:                          '"><p>Copy <tt>'.$fn.'</tt> to <tt>'.
  181:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
  182: 	    } else {
  183: 	       $r->print('<p>No new filename specified.</p></form>');
  184:                return;
  185: 	    }
  186:         } else {
  187: 	    $r->print('<p>No such file.</p></form>');
  188:             return;
  189:         }
  190:     } elsif ($ENV{'form.action'} eq 'newdir') {
  191:         my $newdir='/home/'.$uname.'/public_html/'.
  192:                    $fn.$ENV{'form.newfilename'};
  193: 	if (-e $newdir) {
  194:             $r->print('<p>Directory exists.</p></form>');
  195:             return;
  196:         }
  197: 	$r->print('<input type=hidden name=newfilename value="'.
  198:                   $ENV{'form.newfilename'}.
  199:                   '"><p>Make new directory <tt>'.
  200:                   $fn.$ENV{'form.newfilename'}.'</tt>?</p>');
  201:        
  202:     }
  203:     $r->print('<p><input type=submit value=Continue></p></form>');
  204:     $r->print('<form action="/priv/'.$uname.$fn.
  205: 	      '" method="GET"><p><input type=submit value=Cancel></p></form>');
  206: 
  207: }
  208: 
  209: sub phasetwo {
  210:     my ($r,$fn,$uname,$udom)=@_;
  211: 
  212:     &Debug($r, "loncfile - Entering phase 2 for $fn");
  213: 
  214:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
  215:     my $dir=$1;
  216:     my $main=$2;
  217:     my $suffix=$3;
  218:     
  219:     
  220:     &Debug($r, "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
  221: 
  222:     my $conspace=$fn;
  223: 
  224:     &Debug($r, "loncfile::phase2 Full construction space name: $conspace");
  225: 
  226:     &Debug($r, "loncfie::phase2 action is $ENV{'form.action'}");
  227: 
  228:     if ($ENV{'form.action'} eq 'rename') {
  229: 	if (-e $conspace) {
  230: 	    if ($ENV{'form.newfilename'}) {
  231:                unless (rename('/home/'.$uname.'/public_html'.$fn,
  232:           '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
  233: 	    $r->print('<font color=red>Error: '.$!.'</font>');
  234:                }
  235:             }
  236:         } else {
  237: 	    $r->print('<p>No such file.</form>');
  238:             return;
  239:         }
  240:     } elsif ($ENV{'form.action'} eq 'delete') { 
  241: 	if (-e $conspace) {
  242:             unless (unlink('/home/'.$uname.'/public_html'.$fn)) {
  243: 	       $r->print('<font color=red>Error: '.$!.'</font>');
  244:             }
  245:         } else {
  246: 	    $r->print('<p>No such file.</form>');
  247:             return;
  248:         }
  249:     } elsif ($ENV{'form.action'} eq 'copy') { 
  250: 	if (-e $conspace) {
  251: 	    if ($ENV{'form.newfilename'}) {
  252:                unless (copy('/home/'.$uname.'/public_html'.$fn,
  253:            '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
  254: 	          $r->print('<font color=red>Error: '.$!.'</font>');
  255:                }
  256: 	    } else {
  257: 	       $r->print('<p>No new filename specified.</form>');
  258:                return;
  259: 	    }
  260:         } else {
  261: 	    $r->print('<p>No such file.</form>');
  262:             return;
  263:         }
  264:     } elsif ($ENV{'form.action'} eq 'newdir') {
  265:         my $newdir= $fn.$ENV{'form.newfilename'};
  266: 
  267: 	&Debug($r, "loncfile::phasetwo - new directory name: $newdir");
  268: 
  269:         unless (mkdir($newdir,0770)) {
  270: 	    $r->print('<font color=red>Error: '.$!.'</font>');
  271: 	    &Debug($r, "loncfile::phasetwo - mkdir failed $!");
  272:         }
  273: 	&Debug($r, "Done button: uname = $uname, dir = $dir, fn = $fn");
  274: 	my $url = '/priv/'.$uname.$newdir.'/';
  275: 	&Debug($r, "URL[1] = ".$url);
  276: 	$url =~ s/\/home\/$uname\/public_html//o;
  277:         &Debug($r, "URL = ".$url);
  278: 
  279:         $r->print('<h3><a href="'.$url.'">Done</a></h3>');
  280:         return;
  281:     }
  282:     $r->print('<h3><a href="/priv/'.$uname.$dir.'/">Done</a></h3>');
  283: }
  284: 
  285: sub handler {
  286: 
  287:   $r=shift;
  288: 
  289: 
  290:   &Debug($r, "loncfile.pm - handler entered");
  291: 
  292:   my $fn;
  293: 
  294:   if ($ENV{'form.filename'}) {
  295:       $fn=$ENV{'form.filename'};
  296:       &Debug($r, "loncfile::handler - raw url: $fn");
  297: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
  298: #      $fn=~s/^http\:\/\/[^\/]+//;
  299:       $fn=URLToPath($fn);
  300:       &Debug($r, "loncfile::handler - doctored url: $fn");
  301: 
  302:   } else {
  303:       &Debug($r, "loncfile::handler - no form.filename");
  304:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  305:          ' unspecified filename for cfile', $r->filename); 
  306:      return HTTP_NOT_FOUND;
  307:   }
  308: 
  309:   unless ($fn) { 
  310:       &Debug($r, "loncfile::handler - doctored url is empty");
  311:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  312:          ' trying to cfile non-existing file', $r->filename); 
  313:      return HTTP_NOT_FOUND;
  314:   } 
  315: 
  316: # ----------------------------------------------------------- Start page output
  317:   my $uname;
  318:   my $udom;
  319: 
  320:   ($uname,$udom)=
  321:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  322:   &Debug($r, 
  323: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
  324:   unless (($uname) && ($udom)) {
  325:      $r->log_reason($uname.' at '.$udom.
  326:          ' trying to manipulate file '.$ENV{'form.filename'}.
  327:          ' ('.$fn.') - not authorized', 
  328:          $r->filename); 
  329:      return HTTP_NOT_ACCEPTABLE;
  330:   }
  331: 
  332:   $fn=~s/\/\~(\w+)//;
  333:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
  334: 
  335:   $r->content_type('text/html');
  336:   $r->send_http_header;
  337: 
  338:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  339: 
  340:   $r->print(
  341:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  342: 
  343:   
  344:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
  345:   
  346:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  347:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  348:                '</font></h3>');
  349:   }
  350: 
  351: 
  352:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
  353:   if ($ENV{'form.action'} eq 'delete') {
  354:       
  355:       $r->print('<h3>Delete</h3>');
  356:   } elsif ($ENV{'form.action'} eq 'rename') {
  357:       $r->print('<h3>Rename</h3>');
  358:   } elsif ($ENV{'form.action'} eq 'newdir') {
  359:       $r->print('<h3>New Directory</h3>');
  360:   } elsif ($ENV{'form.action'} eq 'copy') {
  361:       $r->print('<h3>Copy</h3>');
  362:   } else {
  363:      $r->print('<p>Unknown Action</body></html>');
  364:      return OK;  
  365:   }
  366:   if ($ENV{'form.phase'} eq 'two') {
  367:       &Debug($r, "loncfile::handler  entering phase2");
  368:       &phasetwo($r,$fn,$uname,$udom);
  369:   } else {
  370:       &Debug($r, "loncfile::handler  entering phase1");
  371:       &phaseone($r,$fn,$uname,$udom);
  372:   }
  373: 
  374:   $r->print('</body></html>');
  375:   return OK;  
  376: }
  377: 
  378: 1;
  379: __END__
  380: 

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