Annotation of loncom/publisher/lonupload.pm, revision 1.12

1.12    ! foxr        1: 
1.1       www         2: # The LearningOnline Network with CAPA
                      3: # Handler to upload files into construction space
                      4: #
1.12    ! foxr        5: # $Id: lonupload.pm,v 1.11 2002/08/05 02:22:56 foxr Exp $
1.8       matthew     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: #
1.1       www        29: # (Handler to retrieve an old version of a file
                     30: #
                     31: # (Publication Handler
                     32: # 
                     33: # (TeX Content Handler
                     34: #
1.10      harris41   35: # YEAR=2000
1.1       www        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
1.10      harris41   39: # YEAR=2001
1.1       www        40: # 03/23 Guy Albertelli
                     41: # 03/24,03/29 Gerd Kortemeyer)
                     42: #
                     43: # 03/31,04/03 Gerd Kortemeyer)
                     44: #
1.7       www        45: # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
1.8       matthew    46: # 11/29 Matthew Hall
1.10      harris41   47: # 12/16 Scott Harrison
                     48: #
                     49: ###
1.1       www        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);
1.3       www        57: use Apache::loncacc;
1.10      harris41   58: use Apache::loncommon();
1.12    ! foxr       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: }
1.1       www        77: 
1.2       www        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'});
1.1       www        85:   
1.2       www        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'};
1.1       www        92:     }
1.2       www        93:     return $datatoken;
                     94: }
                     95: 
                     96: 
                     97: sub phaseone {
1.5       www        98:    my ($r,$fn,$uname,$udom)=@_;
1.6       www        99:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
                    100:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
1.5       www       101:    if ($ENV{'form.upfile.filename'}) {
1.2       www       102:     $fn=~s/\/[^\/]+$//;
                    103:     $fn=~s/([^\/])$/$1\//;
                    104:     $fn.=$ENV{'form.upfile.filename'};
1.3       www       105:     $fn=~s/^\///;
                    106:     $fn=~s/(\/)+/\//g;
1.12    ! foxr      107:     $fn=~s/%20/ /g;
        !           108:     &Debug($r, "Filename for upload: $fn");
1.3       www       109:     if (($fn) && ($fn!~/\/$/)) {
                    110:       $r->print(
1.2       www       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 '.
1.3       www       115:  '<input type=text size=50 name=filename value="/priv/'.
                    116:   $uname.'/'.$fn.'"><br>'.
1.2       www       117:  '<input type=submit value="Store"></form>');
1.9       matthew   118:       # Check for bad extension and warn user
1.8       matthew   119:       if ($fn=~/\.(\w+)$/ && 
1.10      harris41  120: 	  (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.8       matthew   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>');
1.9       matthew   127:       } elsif($fn=~/\.(\w+)$/ && 
1.10      harris41  128: 	      !defined(&Apache::loncommon::fileembstyle($1))) {
1.9       matthew   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:       }
1.3       www       136:   } else {
                    137:       $r->print('<font color=red>Illegal filename.</font>');
                    138:   }
1.5       www       139:  } else {
                    140:      $r->print('<font color=red>No upload file specified.</font>');
                    141:  }
1.1       www       142: }
                    143: 
                    144: sub phasetwo {
1.4       www       145:    my ($r,$fn,$uname,$udom)=@_;
                    146:    if ($fn=~/^\/priv\/$uname\//) { 
1.3       www       147:     my $tfn=$fn;
                    148:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
                    149:     my $target='/home/'.$uname.'/public_html'.$tfn;
1.2       www       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';
1.9       matthew   163:            # Check for bad extension and disallow upload
1.8       matthew   164: 	   if ($fn=~/\.(\w+)$/ && 
1.10      harris41  165: 	       (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.8       matthew   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.'.
1.9       matthew   170:  '</font>');
                    171: 	   } elsif ($fn=~/\.(\w+)$/ && 
1.10      harris41  172: 		    !defined(&Apache::loncommon::fileembstyle($1))) {
1.9       matthew   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.'.
1.8       matthew   177:  '</font>');
                    178: 	   } elsif (copy($source,$target)) {
1.11      foxr      179: 	       chmod(0660, $target); # Set permissions to rw-rw---.
1.2       www       180: 	      $r->print('File copied.');
1.3       www       181:               $r->print('<p><font size=+2><a href="'.$fn.
1.2       www       182:                         '">View file</a></font>');
                    183: 	   } else {
                    184:               $r->print('Failed to copy: '.$!);
                    185: 	   }
                    186:        }
1.1       www       187:     } else {
                    188:        $r->print(
1.2       www       189:    '<font size=+1 color=red>Please pick a filename</font><p>');
1.1       www       190:        &phaseone($r,$fn,$uname,$udom);
                    191:     }
1.4       www       192:   } else {
                    193:     $r->print(
                    194:    '<font size=+1 color=red>Please pick a filename</font><p>');
                    195:     &phaseone($r,$fn,$uname,$udom);
                    196:   }
1.1       www       197: }
                    198: 
1.10      harris41  199: # ---------------------------------------------------------------- Main Handler
1.1       www       200: sub handler {
                    201: 
                    202:   my $r=shift;
                    203: 
1.3       www       204:   my $uname;
                    205:   my $udom;
                    206: 
1.5       www       207:   ($uname,$udom)=
1.3       www       208:     &Apache::loncacc::constructaccess(
1.5       www       209: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
                    210:   unless (($uname) && ($udom)) {
1.3       www       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: 
1.1       www       218:   my $fn;
                    219: 
                    220:   if ($ENV{'form.filename'}) {
                    221:       $fn=$ENV{'form.filename'};
1.3       www       222:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
1.1       www       223:   } else {
                    224:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       225:          ' unspecified filename for upload', $r->filename); 
1.1       www       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:   
1.2       www       241:   $r->print('<h1>Upload file to Construction Space</h1>');
1.3       www       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: 
1.1       www       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: }
1.7       www       258: 
                    259: 1;
                    260: __END__
1.10      harris41  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>