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

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

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