File:  [LON-CAPA] / loncom / publisher / lonupload.pm
Revision 1.9: download - view: text, annotated - select for diffs
Tue Dec 4 18:13:06 2001 UTC (22 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Disallow uploads of files that are marked 'hdn' in filetypes.tab or not listed
in filetypes.tab.

    1: # The LearningOnline Network with CAPA
    2: # Handler to upload files into construction space
    3: #
    4: # $Id: lonupload.pm,v 1.9 2001/12/04 18:13:06 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (Handler to retrieve an old version of a file
   29: #
   30: # (Publication Handler
   31: # 
   32: # (TeX Content Handler
   33: #
   34: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   35: #
   36: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   37: # 03/23 Guy Albertelli
   38: # 03/24,03/29 Gerd Kortemeyer)
   39: #
   40: # 03/31,04/03 Gerd Kortemeyer)
   41: #
   42: # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
   43: # 11/29 Matthew Hall
   44: 
   45: package Apache::lonupload;
   46: 
   47: use strict;
   48: use Apache::File;
   49: use File::Copy;
   50: use Apache::Constants qw(:common :http :methods);
   51: use Apache::loncacc;
   52: use Apache::lonnet;
   53: 
   54: sub upfile_store {
   55:     my $r=shift;
   56: 	
   57:     my $fname=$ENV{'form.upfile.filename'};
   58:     $fname=~s/\W//g;
   59:     
   60:     chop($ENV{'form.upfile'});
   61:   
   62:     my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
   63: 		  '_upload_'.$fname.'_'.time.'_'.$$;
   64:     {
   65:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
   66:                                    '/tmp/'.$datatoken.'.tmp');
   67:        print $fh $ENV{'form.upfile'};
   68:     }
   69:     return $datatoken;
   70: }
   71: 
   72: 
   73: sub phaseone {
   74:    my ($r,$fn,$uname,$udom)=@_;
   75:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
   76:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
   77:    if ($ENV{'form.upfile.filename'}) {
   78:     $fn=~s/\/[^\/]+$//;
   79:     $fn=~s/([^\/])$/$1\//;
   80:     $fn.=$ENV{'form.upfile.filename'};
   81:     $fn=~s/^\///;
   82:     $fn=~s/(\/)+/\//g;
   83: 
   84:     if (($fn) && ($fn!~/\/$/)) {
   85:       $r->print(
   86:  '<form action=/adm/upload method=post>'.
   87:  '<input type=hidden name=phase value=two>'.
   88:  '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
   89:  'Store uploaded file as '.
   90:  '<input type=text size=50 name=filename value="/priv/'.
   91:   $uname.'/'.$fn.'"><br>'.
   92:  '<input type=submit value="Store"></form>');
   93:       # Check for bad extension and warn user
   94:       if ($fn=~/\.(\w+)$/ && 
   95: 	  (&Apache::lonnet::fileembstyle($1) eq 'hdn')) {
   96: 	  $r->print(
   97:  '<font color=red>'.
   98:  'The extension on this file, "'.$1.
   99:  '", is reserved internally by LON-CAPA. <br \>'.
  100:  'Please change the extension.'.
  101:  '</font>');
  102:       } elsif($fn=~/\.(\w+)$/ && 
  103: 	      !defined(&Apache::lonnet::fileembstyle($1))) {
  104: 	  $r->print(
  105:  '<font color=red>'.
  106:  'The extension on this file, "'.$1.
  107:  '", is not recognized by LON-CAPA. <br \>'.
  108:  'Please change the extension.'.
  109:  '</font>');
  110:       }
  111:   } else {
  112:       $r->print('<font color=red>Illegal filename.</font>');
  113:   }
  114:  } else {
  115:      $r->print('<font color=red>No upload file specified.</font>');
  116:  }
  117: }
  118: 
  119: sub phasetwo {
  120:    my ($r,$fn,$uname,$udom)=@_;
  121:    if ($fn=~/^\/priv\/$uname\//) { 
  122:     my $tfn=$fn;
  123:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
  124:     my $target='/home/'.$uname.'/public_html'.$tfn;
  125:     my $datatoken=$ENV{'form.datatoken'};
  126:     if (($fn) && ($datatoken)) {
  127: 	if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
  128:            $r->print(
  129:  '<form action=/adm/upload method=post>'.
  130:  'File <tt>'.$fn.'</tt> exists. Overwrite? '.
  131:  '<input type=hidden name=phase value=two>'.
  132:  '<input type=hidden name=filename value="'.$fn.'">'.
  133:  '<input type=hidden name=datatoken value="'.$datatoken.'">'.
  134:  '<input type=submit name=override value="Yes"></form>');
  135:        } else {
  136:            my $source=$r->dir_config('lonDaemons').
  137: 	                             '/tmp/'.$datatoken.'.tmp';
  138:            # Check for bad extension and disallow upload
  139: 	   if ($fn=~/\.(\w+)$/ && 
  140: 	       (&Apache::lonnet::fileembstyle($1) eq 'hdn')) {
  141: 	       $r->print(
  142:  'File <tt>'.$fn.'</tt> could not be copied.<br />'.
  143:  '<font color=red>'.
  144:  'The extension on this file is reserved internally by LON-CAPA.'.
  145:  '</font>');
  146: 	   } elsif ($fn=~/\.(\w+)$/ && 
  147: 		    !defined(&Apache::lonnet::fileembstyle($1))) {
  148: 	       $r->print(
  149:  'File <tt>'.$fn.'</tt> could not be copied.<br />'.
  150:  '<font color=red>'.
  151:  'The extension on this file is not recognized by LON-CAPA.'.
  152:  '</font>');
  153: 	   } elsif (copy($source,$target)) {
  154: 	      $r->print('File copied.');
  155:               $r->print('<p><font size=+2><a href="'.$fn.
  156:                         '">View file</a></font>');
  157: 	   } else {
  158:               $r->print('Failed to copy: '.$!);
  159: 	   }
  160:        }
  161:     } else {
  162:        $r->print(
  163:    '<font size=+1 color=red>Please pick a filename</font><p>');
  164:        &phaseone($r,$fn,$uname,$udom);
  165:     }
  166:   } else {
  167:     $r->print(
  168:    '<font size=+1 color=red>Please pick a filename</font><p>');
  169:     &phaseone($r,$fn,$uname,$udom);
  170:   }
  171: }
  172: 
  173: sub handler {
  174: 
  175:   my $r=shift;
  176: 
  177:   my $uname;
  178:   my $udom;
  179: 
  180:   ($uname,$udom)=
  181:     &Apache::loncacc::constructaccess(
  182: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
  183:   unless (($uname) && ($udom)) {
  184:      $r->log_reason($uname.' at '.$udom.
  185:          ' trying to publish file '.$ENV{'form.filename'}.
  186:          ' - not authorized', 
  187:          $r->filename); 
  188:      return HTTP_NOT_ACCEPTABLE;
  189:   }
  190: 
  191:   my $fn;
  192: 
  193:   if ($ENV{'form.filename'}) {
  194:       $fn=$ENV{'form.filename'};
  195:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
  196:   } else {
  197:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  198:          ' unspecified filename for upload', $r->filename); 
  199:      return HTTP_NOT_FOUND;
  200:   }
  201: 
  202: # ----------------------------------------------------------- Start page output
  203: 
  204: 
  205:   $r->content_type('text/html');
  206:   $r->send_http_header;
  207: 
  208:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  209: 
  210:   $r->print(
  211:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  212: 
  213:   
  214:   $r->print('<h1>Upload file to Construction Space</h1>');
  215:   
  216:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  217:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  218:                '</font></h3>');
  219:   }
  220: 
  221: 
  222:   if ($ENV{'form.phase'} eq 'two') {
  223:       &phasetwo($r,$fn,$uname,$udom);
  224:   } else {
  225:       &phaseone($r,$fn,$uname,$udom);
  226:   }
  227: 
  228:   $r->print('</body></html>');
  229:   return OK;  
  230: }
  231: 
  232: 1;
  233: __END__

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