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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to upload files into construction space
                      3: #
1.9     ! matthew     4: # $Id: lonupload.pm,v 1.8 2001/11/29 21:51:40 matthew Exp $
1.8       matthew     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: #
1.1       www        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: #
1.7       www        42: # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
1.8       matthew    43: # 11/29 Matthew Hall
1.1       www        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);
1.3       www        51: use Apache::loncacc;
1.8       matthew    52: use Apache::lonnet;
1.1       www        53: 
1.2       www        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'});
1.1       www        61:   
1.2       www        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'};
1.1       www        68:     }
1.2       www        69:     return $datatoken;
                     70: }
                     71: 
                     72: 
                     73: sub phaseone {
1.5       www        74:    my ($r,$fn,$uname,$udom)=@_;
1.6       www        75:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
                     76:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
1.5       www        77:    if ($ENV{'form.upfile.filename'}) {
1.2       www        78:     $fn=~s/\/[^\/]+$//;
                     79:     $fn=~s/([^\/])$/$1\//;
                     80:     $fn.=$ENV{'form.upfile.filename'};
1.3       www        81:     $fn=~s/^\///;
                     82:     $fn=~s/(\/)+/\//g;
1.8       matthew    83: 
1.3       www        84:     if (($fn) && ($fn!~/\/$/)) {
                     85:       $r->print(
1.2       www        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 '.
1.3       www        90:  '<input type=text size=50 name=filename value="/priv/'.
                     91:   $uname.'/'.$fn.'"><br>'.
1.2       www        92:  '<input type=submit value="Store"></form>');
1.9     ! matthew    93:       # Check for bad extension and warn user
1.8       matthew    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>');
1.9     ! matthew   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:       }
1.3       www       111:   } else {
                    112:       $r->print('<font color=red>Illegal filename.</font>');
                    113:   }
1.5       www       114:  } else {
                    115:      $r->print('<font color=red>No upload file specified.</font>');
                    116:  }
1.1       www       117: }
                    118: 
                    119: sub phasetwo {
1.4       www       120:    my ($r,$fn,$uname,$udom)=@_;
                    121:    if ($fn=~/^\/priv\/$uname\//) { 
1.3       www       122:     my $tfn=$fn;
                    123:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
                    124:     my $target='/home/'.$uname.'/public_html'.$tfn;
1.2       www       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';
1.9     ! matthew   138:            # Check for bad extension and disallow upload
1.8       matthew   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.'.
1.9     ! matthew   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.'.
1.8       matthew   152:  '</font>');
                    153: 	   } elsif (copy($source,$target)) {
1.2       www       154: 	      $r->print('File copied.');
1.3       www       155:               $r->print('<p><font size=+2><a href="'.$fn.
1.2       www       156:                         '">View file</a></font>');
                    157: 	   } else {
                    158:               $r->print('Failed to copy: '.$!);
                    159: 	   }
                    160:        }
1.1       www       161:     } else {
                    162:        $r->print(
1.2       www       163:    '<font size=+1 color=red>Please pick a filename</font><p>');
1.1       www       164:        &phaseone($r,$fn,$uname,$udom);
                    165:     }
1.4       www       166:   } else {
                    167:     $r->print(
                    168:    '<font size=+1 color=red>Please pick a filename</font><p>');
                    169:     &phaseone($r,$fn,$uname,$udom);
                    170:   }
1.1       www       171: }
                    172: 
                    173: sub handler {
                    174: 
                    175:   my $r=shift;
                    176: 
1.3       www       177:   my $uname;
                    178:   my $udom;
                    179: 
1.5       www       180:   ($uname,$udom)=
1.3       www       181:     &Apache::loncacc::constructaccess(
1.5       www       182: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
                    183:   unless (($uname) && ($udom)) {
1.3       www       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: 
1.1       www       191:   my $fn;
                    192: 
                    193:   if ($ENV{'form.filename'}) {
                    194:       $fn=$ENV{'form.filename'};
1.3       www       195:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
1.1       www       196:   } else {
                    197:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       198:          ' unspecified filename for upload', $r->filename); 
1.1       www       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:   
1.2       www       214:   $r->print('<h1>Upload file to Construction Space</h1>');
1.3       www       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: 
1.1       www       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: }
1.7       www       231: 
                    232: 1;
                    233: __END__

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