File:  [LON-CAPA] / loncom / publisher / lonupload.pm
Revision 1.3: download - view: text, annotated - select for diffs
Fri May 25 16:36:36 2001 UTC (23 years ago) by www
Branches: MAIN
CVS tags: HEAD
Bugfix

    1: # The LearningOnline Network with CAPA
    2: # Handler to upload files into construction space
    3: #
    4: # (Handler to retrieve an old version of a file
    5: #
    6: # (Publication Handler
    7: # 
    8: # (TeX Content Handler
    9: #
   10: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   11: #
   12: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   13: # 03/23 Guy Albertelli
   14: # 03/24,03/29 Gerd Kortemeyer)
   15: #
   16: # 03/31,04/03 Gerd Kortemeyer)
   17: #
   18: # 04/05,04/09,05/25 Gerd Kortemeyer
   19: 
   20: package Apache::lonupload;
   21: 
   22: use strict;
   23: use Apache::File;
   24: use File::Copy;
   25: use Apache::Constants qw(:common :http :methods);
   26: use Apache::loncacc;
   27: 
   28: sub upfile_store {
   29:     my $r=shift;
   30: 	
   31:     my $fname=$ENV{'form.upfile.filename'};
   32:     $fname=~s/\W//g;
   33:     
   34:     chop($ENV{'form.upfile'});
   35:   
   36:     my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
   37: 		  '_upload_'.$fname.'_'.time.'_'.$$;
   38:     {
   39:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
   40:                                    '/tmp/'.$datatoken.'.tmp');
   41:        print $fh $ENV{'form.upfile'};
   42:     }
   43:     return $datatoken;
   44: }
   45: 
   46: 
   47: sub phaseone {
   48:     my ($r,$fn,$uname,$udom)=@_;
   49:     $fn=~s/\/[^\/]+$//;
   50:     $fn=~s/([^\/])$/$1\//;
   51:     $fn.=$ENV{'form.upfile.filename'};
   52:     $fn=~s/^\///;
   53:     $fn=~s/(\/)+/\//g;
   54: 
   55:     if (($fn) && ($fn!~/\/$/)) {
   56:       $r->print(
   57:  '<form action=/adm/upload method=post>'.
   58:  '<input type=hidden name=phase value=two>'.
   59:  '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
   60:  'Store uploaded file as '.
   61:  '<input type=text size=50 name=filename value="/priv/'.
   62:   $uname.'/'.$fn.'"><br>'.
   63:  '<input type=submit value="Store"></form>');
   64:   } else {
   65:       $r->print('<font color=red>Illegal filename.</font>');
   66:   }
   67: }
   68: 
   69: sub phasetwo {
   70:     my ($r,$fn,$uname,$udom)=@_;
   71:     my $tfn=$fn;
   72:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
   73:     my $target='/home/'.$uname.'/public_html'.$tfn;
   74:     my $datatoken=$ENV{'form.datatoken'};
   75:     if (($fn) && ($datatoken)) {
   76: 	if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
   77:            $r->print(
   78:  '<form action=/adm/upload method=post>'.
   79:  'File <tt>'.$fn.'</tt> exists. Overwrite? '.
   80:  '<input type=hidden name=phase value=two>'.
   81:  '<input type=hidden name=filename value="'.$fn.'">'.
   82:  '<input type=hidden name=datatoken value="'.$datatoken.'">'.
   83:  '<input type=submit name=override value="Yes"></form>');
   84:        } else {
   85:            my $source=$r->dir_config('lonDaemons').
   86: 	                             '/tmp/'.$datatoken.'.tmp';
   87:            if (copy($source,$target)) {
   88: 	      $r->print('File copied.');
   89:               $r->print('<p><font size=+2><a href="'.$fn.
   90:                         '">View file</a></font>');
   91: 	   } else {
   92:               $r->print('Failed to copy: '.$!);
   93: 	   }
   94:        }
   95:     } else {
   96:        $r->print(
   97:    '<font size=+1 color=red>Please pick a filename</font><p>');
   98:        &phaseone($r,$fn,$uname,$udom);
   99:     }
  100: }
  101: 
  102: sub handler {
  103: 
  104:   my $r=shift;
  105: 
  106:   my $uname;
  107:   my $udom;
  108: 
  109:   unless (($uname,$udom)=
  110:     &Apache::loncacc::constructaccess(
  111:              $ENV{'form.filename'},$r->dir_config('lonDefDomain'))) {
  112:      $r->log_reason($uname.' at '.$udom.
  113:          ' trying to publish file '.$ENV{'form.filename'}.
  114:          ' - not authorized', 
  115:          $r->filename); 
  116:      return HTTP_NOT_ACCEPTABLE;
  117:   }
  118: 
  119:   my $fn;
  120: 
  121:   if ($ENV{'form.filename'}) {
  122:       $fn=$ENV{'form.filename'};
  123:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
  124:   } else {
  125:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  126:          ' unspecified filename for upload', $r->filename); 
  127:      return HTTP_NOT_FOUND;
  128:   }
  129: 
  130: # ----------------------------------------------------------- Start page output
  131: 
  132: 
  133:   $r->content_type('text/html');
  134:   $r->send_http_header;
  135: 
  136:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  137: 
  138:   $r->print(
  139:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  140: 
  141:   
  142:   $r->print('<h1>Upload file to Construction Space</h1>');
  143:   
  144:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  145:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  146:                '</font></h3>');
  147:   }
  148: 
  149: 
  150:   if ($ENV{'form.phase'} eq 'two') {
  151:       &phasetwo($r,$fn,$uname,$udom);
  152:   } else {
  153:       &phaseone($r,$fn,$uname,$udom);
  154:   }
  155: 
  156:   $r->print('</body></html>');
  157:   return OK;  
  158: }

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