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

1.12      foxr        1: 
1.1       www         2: # The LearningOnline Network with CAPA
                      3: # Handler to upload files into construction space
                      4: #
1.17    ! www         5: # $Id: lonupload.pm,v 1.16 2003/06/23 21:56:31 albertel 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: #
                     48: ###
1.1       www        49: 
                     50: package Apache::lonupload;
                     51: 
                     52: use strict;
                     53: use Apache::File;
                     54: use File::Copy;
1.13      foxr       55: use File::Basename;
1.1       www        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();
1.13      foxr       60: use Apache::lonnet;
1.14      foxr       61: use HTML::Entities();
1.12      foxr       62: 
                     63: my $DEBUG=0;
                     64: 
                     65: sub Debug {
                     66:   
                     67:   # Marshall the parameters.
                     68:   
                     69:   my $r       = shift;
                     70:   my $log     = $r->log;
                     71:   my $message = shift;
                     72:   
                     73:   # Put out the indicated message butonly if DEBUG is false.
                     74:   
                     75:   if ($DEBUG) {
                     76:     $log->debug($message);
                     77:   }
                     78: }
1.1       www        79: 
1.2       www        80: sub upfile_store {
                     81:     my $r=shift;
                     82: 	
                     83:     my $fname=$ENV{'form.upfile.filename'};
                     84:     $fname=~s/\W//g;
                     85:     
                     86:     chop($ENV{'form.upfile'});
1.1       www        87:   
1.2       www        88:     my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
                     89: 		  '_upload_'.$fname.'_'.time.'_'.$$;
                     90:     {
                     91:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
                     92:                                    '/tmp/'.$datatoken.'.tmp');
                     93:        print $fh $ENV{'form.upfile'};
1.1       www        94:     }
1.2       www        95:     return $datatoken;
                     96: }
                     97: 
                     98: 
                     99: sub phaseone {
1.5       www       100:    my ($r,$fn,$uname,$udom)=@_;
1.6       www       101:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
                    102:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
1.5       www       103:    if ($ENV{'form.upfile.filename'}) {
1.2       www       104:     $fn=~s/\/[^\/]+$//;
                    105:     $fn=~s/([^\/])$/$1\//;
                    106:     $fn.=$ENV{'form.upfile.filename'};
1.3       www       107:     $fn=~s/^\///;
                    108:     $fn=~s/(\/)+/\//g;
1.13      foxr      109: 
                    110: #    Fn is the full path to the destination filename.
                    111: #    
                    112: 
1.12      foxr      113:     &Debug($r, "Filename for upload: $fn");
1.3       www       114:     if (($fn) && ($fn!~/\/$/)) {
                    115:       $r->print(
1.2       www       116:  '<form action=/adm/upload method=post>'.
                    117:  '<input type=hidden name=phase value=two>'.
                    118:  '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
                    119:  'Store uploaded file as '.
1.3       www       120:  '<input type=text size=50 name=filename value="/priv/'.
                    121:   $uname.'/'.$fn.'"><br>'.
1.2       www       122:  '<input type=submit value="Store"></form>');
1.9       matthew   123:       # Check for bad extension and warn user
1.8       matthew   124:       if ($fn=~/\.(\w+)$/ && 
1.10      harris41  125: 	  (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.8       matthew   126: 	  $r->print(
                    127:  '<font color=red>'.
                    128:  'The extension on this file, "'.$1.
                    129:  '", is reserved internally by LON-CAPA. <br \>'.
                    130:  'Please change the extension.'.
                    131:  '</font>');
1.9       matthew   132:       } elsif($fn=~/\.(\w+)$/ && 
1.10      harris41  133: 	      !defined(&Apache::loncommon::fileembstyle($1))) {
1.9       matthew   134: 	  $r->print(
                    135:  '<font color=red>'.
                    136:  'The extension on this file, "'.$1.
                    137:  '", is not recognized by LON-CAPA. <br \>'.
                    138:  'Please change the extension.'.
                    139:  '</font>');
                    140:       }
1.3       www       141:   } else {
                    142:       $r->print('<font color=red>Illegal filename.</font>');
                    143:   }
1.5       www       144:  } else {
                    145:      $r->print('<font color=red>No upload file specified.</font>');
                    146:  }
1.1       www       147: }
                    148: 
                    149: sub phasetwo {
1.4       www       150:    my ($r,$fn,$uname,$udom)=@_;
1.13      foxr      151:    &Debug($r, "Filename is ".$fn);
                    152:    if ($fn=~/^\/priv\/$uname\//) {
                    153:     &Debug($r, "Filename after priv substitution: ".$fn);
1.3       www       154:     my $tfn=$fn;
                    155:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
1.13      foxr      156:     &Debug($r, "Filename for tfn = ".$tfn);
1.3       www       157:     my $target='/home/'.$uname.'/public_html'.$tfn;
1.13      foxr      158:     &Debug($r, "target -> ".$target);
                    159: #     target is the full filesystem path of the destination file.
                    160:     my $base = &File::Basename::basename($fn);
                    161:     my $path = &File::Basename::dirname($fn);
1.14      foxr      162:     $base    = &HTML::Entities::encode($base);
1.13      foxr      163:     my $url  = $path."/".$base; 
                    164:     &Debug($r, "URL is now ".$url);
1.2       www       165:     my $datatoken=$ENV{'form.datatoken'};
                    166:     if (($fn) && ($datatoken)) {
                    167: 	if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
                    168:            $r->print(
                    169:  '<form action=/adm/upload method=post>'.
                    170:  'File <tt>'.$fn.'</tt> exists. Overwrite? '.
                    171:  '<input type=hidden name=phase value=two>'.
1.13      foxr      172:  '<input type=hidden name=filename value="'."$url".'">'.
1.2       www       173:  '<input type=hidden name=datatoken value="'.$datatoken.'">'.
                    174:  '<input type=submit name=override value="Yes"></form>');
                    175:        } else {
                    176:            my $source=$r->dir_config('lonDaemons').
                    177: 	                             '/tmp/'.$datatoken.'.tmp';
1.9       matthew   178:            # Check for bad extension and disallow upload
1.8       matthew   179: 	   if ($fn=~/\.(\w+)$/ && 
1.10      harris41  180: 	       (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.8       matthew   181: 	       $r->print(
                    182:  'File <tt>'.$fn.'</tt> could not be copied.<br />'.
                    183:  '<font color=red>'.
                    184:  'The extension on this file is reserved internally by LON-CAPA.'.
1.9       matthew   185:  '</font>');
                    186: 	   } elsif ($fn=~/\.(\w+)$/ && 
1.10      harris41  187: 		    !defined(&Apache::loncommon::fileembstyle($1))) {
1.9       matthew   188: 	       $r->print(
                    189:  'File <tt>'.$fn.'</tt> could not be copied.<br />'.
                    190:  '<font color=red>'.
                    191:  'The extension on this file is not recognized by LON-CAPA.'.
1.8       matthew   192:  '</font>');
                    193: 	   } elsif (copy($source,$target)) {
1.11      foxr      194: 	       chmod(0660, $target); # Set permissions to rw-rw---.
1.2       www       195: 	      $r->print('File copied.');
1.13      foxr      196:               $r->print('<p><font size=+2><a href="'.$url.
1.2       www       197:                         '">View file</a></font>');
1.16      albertel  198:               $r->print('<p><font size=+2><a href="'.$path.
                    199:                         '">Back to Directory</a></font>');
1.2       www       200: 	   } else {
                    201:               $r->print('Failed to copy: '.$!);
                    202: 	   }
                    203:        }
1.1       www       204:     } else {
                    205:        $r->print(
1.2       www       206:    '<font size=+1 color=red>Please pick a filename</font><p>');
1.1       www       207:        &phaseone($r,$fn,$uname,$udom);
                    208:     }
1.4       www       209:   } else {
                    210:     $r->print(
                    211:    '<font size=+1 color=red>Please pick a filename</font><p>');
                    212:     &phaseone($r,$fn,$uname,$udom);
                    213:   }
1.1       www       214: }
                    215: 
1.10      harris41  216: # ---------------------------------------------------------------- Main Handler
1.1       www       217: sub handler {
                    218: 
                    219:   my $r=shift;
                    220: 
1.3       www       221:   my $uname;
                    222:   my $udom;
                    223: 
1.5       www       224:   ($uname,$udom)=
1.3       www       225:     &Apache::loncacc::constructaccess(
1.5       www       226: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
                    227:   unless (($uname) && ($udom)) {
1.3       www       228:      $r->log_reason($uname.' at '.$udom.
                    229:          ' trying to publish file '.$ENV{'form.filename'}.
                    230:          ' - not authorized', 
                    231:          $r->filename); 
                    232:      return HTTP_NOT_ACCEPTABLE;
                    233:   }
                    234: 
1.1       www       235:   my $fn;
                    236: 
                    237:   if ($ENV{'form.filename'}) {
                    238:       $fn=$ENV{'form.filename'};
1.3       www       239:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
1.1       www       240:   } else {
                    241:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       242:          ' unspecified filename for upload', $r->filename); 
1.1       www       243:      return HTTP_NOT_FOUND;
                    244:   }
                    245: 
                    246: # ----------------------------------------------------------- Start page output
                    247: 
                    248: 
                    249:   $r->content_type('text/html');
                    250:   $r->send_http_header;
                    251: 
                    252:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    253: 
1.17    ! www       254:   $r->print(&Apache::loncommon::bodytag('Upload file to Construction Space'));
1.3       www       255:   
                    256:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    257:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                    258:                '</font></h3>');
                    259:   }
                    260: 
1.1       www       261: 
                    262:   if ($ENV{'form.phase'} eq 'two') {
                    263:       &phasetwo($r,$fn,$uname,$udom);
                    264:   } else {
                    265:       &phaseone($r,$fn,$uname,$udom);
                    266:   }
                    267: 
                    268:   $r->print('</body></html>');
                    269:   return OK;  
                    270: }
1.7       www       271: 
                    272: 1;
                    273: __END__
1.10      harris41  274: 
                    275: =head1 NAME
                    276: 
                    277: Apache::lonupload - upload files into construction space
                    278: 
                    279: =head1 SYNOPSIS
                    280: 
                    281: Invoked by /etc/httpd/conf/srm.conf:
                    282: 
                    283:  <Location /adm/upload>
                    284:  PerlAccessHandler       Apache::lonacc
                    285:  SetHandler perl-script
                    286:  PerlHandler Apache::lonupload
                    287:  ErrorDocument     403 /adm/login
                    288:  ErrorDocument     404 /adm/notfound.html
                    289:  ErrorDocument     406 /adm/unauthorized.html
                    290:  ErrorDocument	  500 /adm/errorhandler
                    291:  </Location>
                    292: 
                    293: =head1 INTRODUCTION
                    294: 
                    295: This module uploads a file sitting on a client computer into 
                    296: library server construction space.
                    297: 
                    298: This is part of the LearningOnline Network with CAPA project
                    299: described at http://www.lon-capa.org.
                    300: 
                    301: =head1 HANDLER SUBROUTINE
                    302: 
                    303: This routine is called by Apache and mod_perl.
                    304: 
                    305: =over 4
                    306: 
                    307: =item *
                    308: 
                    309: Initialize variables
                    310: 
                    311: =item *
                    312: 
                    313: Start page output
                    314: 
                    315: =item *
                    316: 
                    317: output relevant interface phase (phaseone or phasetwo)
                    318: 
                    319: =item *
                    320: 
                    321: (phase one is to specify upload file; phase two is to handle conditions
                    322: subsequent to specification--like overwriting an existing file)
                    323: 
                    324: =back
                    325: 
                    326: =head1 OTHER SUBROUTINES
                    327: 
                    328: =over 4
                    329: 
                    330: =item *
                    331: 
                    332: phaseone() : Interface for specifying file to upload.
                    333: 
                    334: =item *
                    335: 
                    336: phasetwo() : Interface for handling post-conditions about uploading (such
                    337: as overwriting an existing file).
                    338: 
                    339: =item *
                    340: 
                    341: upfile_store() : Store contents of uploaded file into temporary space.  Invoked
                    342: by phaseone subroutine.
                    343: 
                    344: =back
                    345: 
                    346: =cut

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