File:  [LON-CAPA] / loncom / publisher / loncfile.pm
Revision 1.11: download - view: text, annotated - select for diffs
Fri Jun 7 01:35:48 2002 UTC (22 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_0_4, stable_2002_july, STABLE, HEAD
- $fn seems to be set to the full path (/home/$uname/public_html) but was treated as if to was relative to the public_html directory.

- tested for filesystem authenticated, but not internal users

    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.11 2002/06/07 01:35:48 albertel 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;
  143:     if ($fn =~ m-^/home/-) {
  144:         $conspace=$fn;
  145:     } else {
  146:         $conspace='/home/'.$uname.'/public_html'.$fn;
  147:     }
  148: 
  149:     $r->print('<form action=/adm/cfile method=post>'.
  150: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
  151:               '<input type=hidden name=phase value=two>'.
  152:               '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
  153: 
  154:     if ($ENV{'form.action'} eq 'rename') {
  155: 	if (-e $conspace) {
  156: 	    if ($ENV{'form.newfilename'}) {
  157: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
  158: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
  159: 	       $r->print('<input type=hidden name=newfilename value="'.
  160:                          $ENV{'form.newfilename'}.
  161:                          '"><p>Rename <tt>'.$fn.'</tt> to <tt>'.
  162:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
  163: 	    } else {
  164: 	       $r->print('<p>No new filename specified.</p></form>');
  165:                return;
  166: 	    }
  167:         } else {
  168: 	    $r->print('<p>No such file.</p></form>');
  169:             return;
  170:         }
  171:     } elsif ($ENV{'form.action'} eq 'delete') { 
  172: 	if (-e $conspace) {
  173:             $r->print('<p>Delete <tt>'.$fn.'</tt>?</p>');
  174:         } else {
  175: 	    $r->print('<p>No such file.</p></form>');
  176:             return;
  177:         }
  178:     } elsif ($ENV{'form.action'} eq 'copy') { 
  179: 	if (-e $conspace) {
  180: 	    if ($ENV{'form.newfilename'}) {
  181: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
  182: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
  183: 	       $r->print('<input type=hidden name=newfilename value="'.
  184:                          $ENV{'form.newfilename'}.
  185:                          '"><p>Copy <tt>'.$fn.'</tt> to <tt>'.
  186:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
  187: 	    } else {
  188: 	       $r->print('<p>No new filename specified.</p></form>');
  189:                return;
  190: 	    }
  191:         } else {
  192: 	    $r->print('<p>No such file.</p></form>');
  193:             return;
  194:         }
  195:     } elsif ($ENV{'form.action'} eq 'newdir') {
  196:         my $newdir='/home/'.$uname.'/public_html/'.
  197:                    $fn.$ENV{'form.newfilename'};
  198: 	if (-e $newdir) {
  199:             $r->print('<p>Directory exists.</p></form>');
  200:             return;
  201:         }
  202: 	$r->print('<input type=hidden name=newfilename value="'.
  203:                   $ENV{'form.newfilename'}.
  204:                   '"><p>Make new directory <tt>'.
  205:                   $fn.$ENV{'form.newfilename'}.'</tt>?</p>');
  206:        
  207:     }
  208:     $r->print('<p><input type=submit value=Continue></p></form>');
  209:     $r->print('<form action="/priv/'.$uname.$fn.
  210: 	      '" method="GET"><p><input type=submit value=Cancel></p></form>');
  211: 
  212: }
  213: 
  214: sub phasetwo {
  215:     my ($r,$fn,$uname,$udom)=@_;
  216: 
  217:     &Debug($r, "loncfile - Entering phase 2 for $fn");
  218: 
  219:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
  220:     my $dir=$1;
  221:     my $main=$2;
  222:     my $suffix=$3;
  223:     $dir =~ s-^/[^/]*/[^/]*/[^/]*--;
  224:     
  225:     
  226:     &Debug($r, "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
  227: 
  228:     my $conspace=$fn;
  229: 
  230:     &Debug($r, "loncfile::phase2 Full construction space name: $conspace");
  231: 
  232:     &Debug($r, "loncfie::phase2 action is $ENV{'form.action'}");
  233: 
  234:     if ($ENV{'form.action'} eq 'rename') {
  235: 	if (-e $conspace) {
  236: 	    if ($ENV{'form.newfilename'}) {
  237:                unless (rename($fn,
  238:           '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
  239: 	    $r->print('<font color=red>Error: '.$!.'</font>');
  240:                }
  241:             }
  242:         } else {
  243: 	    $r->print('<p>No such file.</form>');
  244:             return;
  245:         }
  246:     } elsif ($ENV{'form.action'} eq 'delete') { 
  247: 	if (-e $conspace) {
  248:             unless (unlink($fn)) {
  249: 	       $r->print('<font color=red>Error: '.$!.'</font>');
  250:             }
  251:         } else {
  252: 	    $r->print('<p>No such file.</form>');
  253:             return;
  254:         }
  255:     } elsif ($ENV{'form.action'} eq 'copy') { 
  256: 	if (-e $conspace) {
  257: 	    if ($ENV{'form.newfilename'}) {
  258:                unless (copy($fn,
  259:            '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
  260: 	          $r->print('<font color=red>Error: '.$!.'</font>');
  261:                }
  262: 	    } else {
  263: 	       $r->print('<p>No new filename specified.</form>');
  264:                return;
  265: 	    }
  266:         } else {
  267: 	    $r->print('<p>No such file.</form>');
  268:             return;
  269:         }
  270:     } elsif ($ENV{'form.action'} eq 'newdir') {
  271:         my $newdir= $fn.$ENV{'form.newfilename'};
  272: 
  273: 	&Debug($r, "loncfile::phasetwo - new directory name: $newdir");
  274: 
  275:         unless (mkdir($newdir,0770)) {
  276: 	    $r->print('<font color=red>Error: '.$!.'</font>');
  277: 	    &Debug($r, "loncfile::phasetwo - mkdir failed $!");
  278:         }
  279: 	&Debug($r, "Done button: uname = $uname, dir = $dir, fn = $fn");
  280: 	my $url = '/priv/'.$uname.$newdir.'/';
  281: 	&Debug($r, "URL[1] = ".$url);
  282: 	$url =~ s/\/home\/$uname\/public_html//o;
  283:         &Debug($r, "URL = ".$url);
  284: 
  285:         $r->print('<h3><a href="'.$url.'">Done</a></h3>');
  286:         return;
  287:     }
  288:     $r->print('<h3><a href="/priv/'.$uname.$dir.'/">Done</a></h3>');
  289: }
  290: 
  291: sub handler {
  292: 
  293:   $r=shift;
  294: 
  295: 
  296:   &Debug($r, "loncfile.pm - handler entered");
  297: 
  298:   my $fn;
  299: 
  300:   if ($ENV{'form.filename'}) {
  301:       $fn=$ENV{'form.filename'};
  302:       &Debug($r, "loncfile::handler - raw url: $fn");
  303: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
  304: #      $fn=~s/^http\:\/\/[^\/]+//;
  305:       $fn=URLToPath($fn);
  306:       &Debug($r, "loncfile::handler - doctored url: $fn");
  307: 
  308:   } else {
  309:       &Debug($r, "loncfile::handler - no form.filename");
  310:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  311:          ' unspecified filename for cfile', $r->filename); 
  312:      return HTTP_NOT_FOUND;
  313:   }
  314: 
  315:   unless ($fn) { 
  316:       &Debug($r, "loncfile::handler - doctored url is empty");
  317:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  318:          ' trying to cfile non-existing file', $r->filename); 
  319:      return HTTP_NOT_FOUND;
  320:   } 
  321: 
  322: # ----------------------------------------------------------- Start page output
  323:   my $uname;
  324:   my $udom;
  325: 
  326:   ($uname,$udom)=
  327:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  328:   &Debug($r, 
  329: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
  330:   unless (($uname) && ($udom)) {
  331:      $r->log_reason($uname.' at '.$udom.
  332:          ' trying to manipulate file '.$ENV{'form.filename'}.
  333:          ' ('.$fn.') - not authorized', 
  334:          $r->filename); 
  335:      return HTTP_NOT_ACCEPTABLE;
  336:   }
  337: 
  338:   $fn=~s/\/\~(\w+)//;
  339:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
  340: 
  341:   $r->content_type('text/html');
  342:   $r->send_http_header;
  343: 
  344:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  345: 
  346:   $r->print(
  347:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  348: 
  349:   
  350:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
  351:   
  352:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  353:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  354:                '</font></h3>');
  355:   }
  356: 
  357: 
  358:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
  359:   if ($ENV{'form.action'} eq 'delete') {
  360:       
  361:       $r->print('<h3>Delete</h3>');
  362:   } elsif ($ENV{'form.action'} eq 'rename') {
  363:       $r->print('<h3>Rename</h3>');
  364:   } elsif ($ENV{'form.action'} eq 'newdir') {
  365:       $r->print('<h3>New Directory</h3>');
  366:   } elsif ($ENV{'form.action'} eq 'copy') {
  367:       $r->print('<h3>Copy</h3>');
  368:   } else {
  369:      $r->print('<p>Unknown Action</body></html>');
  370:      return OK;  
  371:   }
  372:   if ($ENV{'form.phase'} eq 'two') {
  373:       &Debug($r, "loncfile::handler  entering phase2");
  374:       &phasetwo($r,$fn,$uname,$udom);
  375:   } else {
  376:       &Debug($r, "loncfile::handler  entering phase1");
  377:       &phaseone($r,$fn,$uname,$udom);
  378:   }
  379: 
  380:   $r->print('</body></html>');
  381:   return OK;  
  382: }
  383: 
  384: 1;
  385: __END__
  386: 

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