File:  [LON-CAPA] / loncom / publisher / lonupload.pm
Revision 1.10: download - view: text, annotated - select for diffs
Mon Dec 17 00:57:59 2001 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: version_0_4, stable_2002_spring, stable_2002_july, stable_2002_april, STABLE, HEAD
adding in POD documentation; changing void context map statements to
foreach statements; removing references to Apache::lonnet::fileembstyle
and using Apache::loncommon::fileembstyle -Scott Harrison

    1: # The LearningOnline Network with CAPA
    2: # Handler to upload files into construction space
    3: #
    4: # $Id: lonupload.pm,v 1.10 2001/12/17 00:57:59 harris41 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: # YEAR=2000
   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
   38: # YEAR=2001
   39: # 03/23 Guy Albertelli
   40: # 03/24,03/29 Gerd Kortemeyer)
   41: #
   42: # 03/31,04/03 Gerd Kortemeyer)
   43: #
   44: # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
   45: # 11/29 Matthew Hall
   46: # 12/16 Scott Harrison
   47: #
   48: ###
   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);
   56: use Apache::loncacc;
   57: use Apache::loncommon();
   58: 
   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'});
   66:   
   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'};
   73:     }
   74:     return $datatoken;
   75: }
   76: 
   77: 
   78: sub phaseone {
   79:    my ($r,$fn,$uname,$udom)=@_;
   80:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
   81:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
   82:    if ($ENV{'form.upfile.filename'}) {
   83:     $fn=~s/\/[^\/]+$//;
   84:     $fn=~s/([^\/])$/$1\//;
   85:     $fn.=$ENV{'form.upfile.filename'};
   86:     $fn=~s/^\///;
   87:     $fn=~s/(\/)+/\//g;
   88: 
   89:     if (($fn) && ($fn!~/\/$/)) {
   90:       $r->print(
   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 '.
   95:  '<input type=text size=50 name=filename value="/priv/'.
   96:   $uname.'/'.$fn.'"><br>'.
   97:  '<input type=submit value="Store"></form>');
   98:       # Check for bad extension and warn user
   99:       if ($fn=~/\.(\w+)$/ && 
  100: 	  (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
  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>');
  107:       } elsif($fn=~/\.(\w+)$/ && 
  108: 	      !defined(&Apache::loncommon::fileembstyle($1))) {
  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:       }
  116:   } else {
  117:       $r->print('<font color=red>Illegal filename.</font>');
  118:   }
  119:  } else {
  120:      $r->print('<font color=red>No upload file specified.</font>');
  121:  }
  122: }
  123: 
  124: sub phasetwo {
  125:    my ($r,$fn,$uname,$udom)=@_;
  126:    if ($fn=~/^\/priv\/$uname\//) { 
  127:     my $tfn=$fn;
  128:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
  129:     my $target='/home/'.$uname.'/public_html'.$tfn;
  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';
  143:            # Check for bad extension and disallow upload
  144: 	   if ($fn=~/\.(\w+)$/ && 
  145: 	       (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
  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.'.
  150:  '</font>');
  151: 	   } elsif ($fn=~/\.(\w+)$/ && 
  152: 		    !defined(&Apache::loncommon::fileembstyle($1))) {
  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.'.
  157:  '</font>');
  158: 	   } elsif (copy($source,$target)) {
  159: 	      $r->print('File copied.');
  160:               $r->print('<p><font size=+2><a href="'.$fn.
  161:                         '">View file</a></font>');
  162: 	   } else {
  163:               $r->print('Failed to copy: '.$!);
  164: 	   }
  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:   } else {
  172:     $r->print(
  173:    '<font size=+1 color=red>Please pick a filename</font><p>');
  174:     &phaseone($r,$fn,$uname,$udom);
  175:   }
  176: }
  177: 
  178: # ---------------------------------------------------------------- Main Handler
  179: sub handler {
  180: 
  181:   my $r=shift;
  182: 
  183:   my $uname;
  184:   my $udom;
  185: 
  186:   ($uname,$udom)=
  187:     &Apache::loncacc::constructaccess(
  188: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
  189:   unless (($uname) && ($udom)) {
  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: 
  197:   my $fn;
  198: 
  199:   if ($ENV{'form.filename'}) {
  200:       $fn=$ENV{'form.filename'};
  201:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
  202:   } else {
  203:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  204:          ' unspecified filename for upload', $r->filename); 
  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:   
  220:   $r->print('<h1>Upload file to Construction Space</h1>');
  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: 
  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: }
  237: 
  238: 1;
  239: __END__
  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>