File:  [LON-CAPA] / loncom / publisher / lonupload.pm
Revision 1.12: download - view: text, annotated - select for diffs
Thu Aug 8 02:30:39 2002 UTC (21 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: version_0_5_1, version_0_5, HEAD
BUG 442 - map %20 to ' ' in filenames.

    1: 
    2: # The LearningOnline Network with CAPA
    3: # Handler to upload files into construction space
    4: #
    5: # $Id: lonupload.pm,v 1.12 2002/08/08 02:30:39 foxr Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: # (Handler to retrieve an old version of a file
   30: #
   31: # (Publication Handler
   32: # 
   33: # (TeX Content Handler
   34: #
   35: # YEAR=2000
   36: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   37: #
   38: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   39: # YEAR=2001
   40: # 03/23 Guy Albertelli
   41: # 03/24,03/29 Gerd Kortemeyer)
   42: #
   43: # 03/31,04/03 Gerd Kortemeyer)
   44: #
   45: # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
   46: # 11/29 Matthew Hall
   47: # 12/16 Scott Harrison
   48: #
   49: ###
   50: 
   51: package Apache::lonupload;
   52: 
   53: use strict;
   54: use Apache::File;
   55: use File::Copy;
   56: use Apache::Constants qw(:common :http :methods);
   57: use Apache::loncacc;
   58: use Apache::loncommon();
   59: use Apache::Log();
   60: 
   61: my $DEBUG=0;
   62: 
   63: sub Debug {
   64:   
   65:   # Marshall the parameters.
   66:   
   67:   my $r       = shift;
   68:   my $log     = $r->log;
   69:   my $message = shift;
   70:   
   71:   # Put out the indicated message butonly if DEBUG is false.
   72:   
   73:   if ($DEBUG) {
   74:     $log->debug($message);
   75:   }
   76: }
   77: 
   78: sub upfile_store {
   79:     my $r=shift;
   80: 	
   81:     my $fname=$ENV{'form.upfile.filename'};
   82:     $fname=~s/\W//g;
   83:     
   84:     chop($ENV{'form.upfile'});
   85:   
   86:     my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
   87: 		  '_upload_'.$fname.'_'.time.'_'.$$;
   88:     {
   89:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
   90:                                    '/tmp/'.$datatoken.'.tmp');
   91:        print $fh $ENV{'form.upfile'};
   92:     }
   93:     return $datatoken;
   94: }
   95: 
   96: 
   97: sub phaseone {
   98:    my ($r,$fn,$uname,$udom)=@_;
   99:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
  100:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
  101:    if ($ENV{'form.upfile.filename'}) {
  102:     $fn=~s/\/[^\/]+$//;
  103:     $fn=~s/([^\/])$/$1\//;
  104:     $fn.=$ENV{'form.upfile.filename'};
  105:     $fn=~s/^\///;
  106:     $fn=~s/(\/)+/\//g;
  107:     $fn=~s/%20/ /g;
  108:     &Debug($r, "Filename for upload: $fn");
  109:     if (($fn) && ($fn!~/\/$/)) {
  110:       $r->print(
  111:  '<form action=/adm/upload method=post>'.
  112:  '<input type=hidden name=phase value=two>'.
  113:  '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
  114:  'Store uploaded file as '.
  115:  '<input type=text size=50 name=filename value="/priv/'.
  116:   $uname.'/'.$fn.'"><br>'.
  117:  '<input type=submit value="Store"></form>');
  118:       # Check for bad extension and warn user
  119:       if ($fn=~/\.(\w+)$/ && 
  120: 	  (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
  121: 	  $r->print(
  122:  '<font color=red>'.
  123:  'The extension on this file, "'.$1.
  124:  '", is reserved internally by LON-CAPA. <br \>'.
  125:  'Please change the extension.'.
  126:  '</font>');
  127:       } elsif($fn=~/\.(\w+)$/ && 
  128: 	      !defined(&Apache::loncommon::fileembstyle($1))) {
  129: 	  $r->print(
  130:  '<font color=red>'.
  131:  'The extension on this file, "'.$1.
  132:  '", is not recognized by LON-CAPA. <br \>'.
  133:  'Please change the extension.'.
  134:  '</font>');
  135:       }
  136:   } else {
  137:       $r->print('<font color=red>Illegal filename.</font>');
  138:   }
  139:  } else {
  140:      $r->print('<font color=red>No upload file specified.</font>');
  141:  }
  142: }
  143: 
  144: sub phasetwo {
  145:    my ($r,$fn,$uname,$udom)=@_;
  146:    if ($fn=~/^\/priv\/$uname\//) { 
  147:     my $tfn=$fn;
  148:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
  149:     my $target='/home/'.$uname.'/public_html'.$tfn;
  150:     my $datatoken=$ENV{'form.datatoken'};
  151:     if (($fn) && ($datatoken)) {
  152: 	if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
  153:            $r->print(
  154:  '<form action=/adm/upload method=post>'.
  155:  'File <tt>'.$fn.'</tt> exists. Overwrite? '.
  156:  '<input type=hidden name=phase value=two>'.
  157:  '<input type=hidden name=filename value="'.$fn.'">'.
  158:  '<input type=hidden name=datatoken value="'.$datatoken.'">'.
  159:  '<input type=submit name=override value="Yes"></form>');
  160:        } else {
  161:            my $source=$r->dir_config('lonDaemons').
  162: 	                             '/tmp/'.$datatoken.'.tmp';
  163:            # Check for bad extension and disallow upload
  164: 	   if ($fn=~/\.(\w+)$/ && 
  165: 	       (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
  166: 	       $r->print(
  167:  'File <tt>'.$fn.'</tt> could not be copied.<br />'.
  168:  '<font color=red>'.
  169:  'The extension on this file is reserved internally by LON-CAPA.'.
  170:  '</font>');
  171: 	   } elsif ($fn=~/\.(\w+)$/ && 
  172: 		    !defined(&Apache::loncommon::fileembstyle($1))) {
  173: 	       $r->print(
  174:  'File <tt>'.$fn.'</tt> could not be copied.<br />'.
  175:  '<font color=red>'.
  176:  'The extension on this file is not recognized by LON-CAPA.'.
  177:  '</font>');
  178: 	   } elsif (copy($source,$target)) {
  179: 	       chmod(0660, $target); # Set permissions to rw-rw---.
  180: 	      $r->print('File copied.');
  181:               $r->print('<p><font size=+2><a href="'.$fn.
  182:                         '">View file</a></font>');
  183: 	   } else {
  184:               $r->print('Failed to copy: '.$!);
  185: 	   }
  186:        }
  187:     } else {
  188:        $r->print(
  189:    '<font size=+1 color=red>Please pick a filename</font><p>');
  190:        &phaseone($r,$fn,$uname,$udom);
  191:     }
  192:   } else {
  193:     $r->print(
  194:    '<font size=+1 color=red>Please pick a filename</font><p>');
  195:     &phaseone($r,$fn,$uname,$udom);
  196:   }
  197: }
  198: 
  199: # ---------------------------------------------------------------- Main Handler
  200: sub handler {
  201: 
  202:   my $r=shift;
  203: 
  204:   my $uname;
  205:   my $udom;
  206: 
  207:   ($uname,$udom)=
  208:     &Apache::loncacc::constructaccess(
  209: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
  210:   unless (($uname) && ($udom)) {
  211:      $r->log_reason($uname.' at '.$udom.
  212:          ' trying to publish file '.$ENV{'form.filename'}.
  213:          ' - not authorized', 
  214:          $r->filename); 
  215:      return HTTP_NOT_ACCEPTABLE;
  216:   }
  217: 
  218:   my $fn;
  219: 
  220:   if ($ENV{'form.filename'}) {
  221:       $fn=$ENV{'form.filename'};
  222:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
  223:   } else {
  224:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  225:          ' unspecified filename for upload', $r->filename); 
  226:      return HTTP_NOT_FOUND;
  227:   }
  228: 
  229: # ----------------------------------------------------------- Start page output
  230: 
  231: 
  232:   $r->content_type('text/html');
  233:   $r->send_http_header;
  234: 
  235:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  236: 
  237:   $r->print(
  238:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  239: 
  240:   
  241:   $r->print('<h1>Upload file to Construction Space</h1>');
  242:   
  243:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  244:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  245:                '</font></h3>');
  246:   }
  247: 
  248: 
  249:   if ($ENV{'form.phase'} eq 'two') {
  250:       &phasetwo($r,$fn,$uname,$udom);
  251:   } else {
  252:       &phaseone($r,$fn,$uname,$udom);
  253:   }
  254: 
  255:   $r->print('</body></html>');
  256:   return OK;  
  257: }
  258: 
  259: 1;
  260: __END__
  261: 
  262: =head1 NAME
  263: 
  264: Apache::lonupload - upload files into construction space
  265: 
  266: =head1 SYNOPSIS
  267: 
  268: Invoked by /etc/httpd/conf/srm.conf:
  269: 
  270:  <Location /adm/upload>
  271:  PerlAccessHandler       Apache::lonacc
  272:  SetHandler perl-script
  273:  PerlHandler Apache::lonupload
  274:  ErrorDocument     403 /adm/login
  275:  ErrorDocument     404 /adm/notfound.html
  276:  ErrorDocument     406 /adm/unauthorized.html
  277:  ErrorDocument	  500 /adm/errorhandler
  278:  </Location>
  279: 
  280: =head1 INTRODUCTION
  281: 
  282: This module uploads a file sitting on a client computer into 
  283: library server construction space.
  284: 
  285: This is part of the LearningOnline Network with CAPA project
  286: described at http://www.lon-capa.org.
  287: 
  288: =head1 HANDLER SUBROUTINE
  289: 
  290: This routine is called by Apache and mod_perl.
  291: 
  292: =over 4
  293: 
  294: =item *
  295: 
  296: Initialize variables
  297: 
  298: =item *
  299: 
  300: Start page output
  301: 
  302: =item *
  303: 
  304: output relevant interface phase (phaseone or phasetwo)
  305: 
  306: =item *
  307: 
  308: (phase one is to specify upload file; phase two is to handle conditions
  309: subsequent to specification--like overwriting an existing file)
  310: 
  311: =back
  312: 
  313: =head1 OTHER SUBROUTINES
  314: 
  315: =over 4
  316: 
  317: =item *
  318: 
  319: phaseone() : Interface for specifying file to upload.
  320: 
  321: =item *
  322: 
  323: phasetwo() : Interface for handling post-conditions about uploading (such
  324: as overwriting an existing file).
  325: 
  326: =item *
  327: 
  328: upfile_store() : Store contents of uploaded file into temporary space.  Invoked
  329: by phaseone subroutine.
  330: 
  331: =back
  332: 
  333: =cut

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