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

1.1       www         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: #
1.7     ! www        18: # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
1.1       www        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);
1.3       www        26: use Apache::loncacc;
1.1       www        27: 
1.2       www        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'});
1.1       www        35:   
1.2       www        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'};
1.1       www        42:     }
1.2       www        43:     return $datatoken;
                     44: }
                     45: 
                     46: 
                     47: sub phaseone {
1.5       www        48:    my ($r,$fn,$uname,$udom)=@_;
1.6       www        49:    $ENV{'form.upfile.filename'}=~s/\\/\//g;
                     50:    $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
1.5       www        51:    if ($ENV{'form.upfile.filename'}) {
1.2       www        52:     $fn=~s/\/[^\/]+$//;
                     53:     $fn=~s/([^\/])$/$1\//;
                     54:     $fn.=$ENV{'form.upfile.filename'};
1.3       www        55:     $fn=~s/^\///;
                     56:     $fn=~s/(\/)+/\//g;
                     57:     if (($fn) && ($fn!~/\/$/)) {
                     58:       $r->print(
1.2       www        59:  '<form action=/adm/upload method=post>'.
                     60:  '<input type=hidden name=phase value=two>'.
                     61:  '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
                     62:  'Store uploaded file as '.
1.3       www        63:  '<input type=text size=50 name=filename value="/priv/'.
                     64:   $uname.'/'.$fn.'"><br>'.
1.2       www        65:  '<input type=submit value="Store"></form>');
1.3       www        66:   } else {
                     67:       $r->print('<font color=red>Illegal filename.</font>');
                     68:   }
1.5       www        69:  } else {
                     70:      $r->print('<font color=red>No upload file specified.</font>');
                     71:  }
1.1       www        72: }
                     73: 
                     74: sub phasetwo {
1.4       www        75:    my ($r,$fn,$uname,$udom)=@_;
                     76:    if ($fn=~/^\/priv\/$uname\//) { 
1.3       www        77:     my $tfn=$fn;
                     78:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
                     79:     my $target='/home/'.$uname.'/public_html'.$tfn;
1.2       www        80:     my $datatoken=$ENV{'form.datatoken'};
                     81:     if (($fn) && ($datatoken)) {
                     82: 	if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
                     83:            $r->print(
                     84:  '<form action=/adm/upload method=post>'.
                     85:  'File <tt>'.$fn.'</tt> exists. Overwrite? '.
                     86:  '<input type=hidden name=phase value=two>'.
                     87:  '<input type=hidden name=filename value="'.$fn.'">'.
                     88:  '<input type=hidden name=datatoken value="'.$datatoken.'">'.
                     89:  '<input type=submit name=override value="Yes"></form>');
                     90:        } else {
                     91:            my $source=$r->dir_config('lonDaemons').
                     92: 	                             '/tmp/'.$datatoken.'.tmp';
                     93:            if (copy($source,$target)) {
                     94: 	      $r->print('File copied.');
1.3       www        95:               $r->print('<p><font size=+2><a href="'.$fn.
1.2       www        96:                         '">View file</a></font>');
                     97: 	   } else {
                     98:               $r->print('Failed to copy: '.$!);
                     99: 	   }
                    100:        }
1.1       www       101:     } else {
                    102:        $r->print(
1.2       www       103:    '<font size=+1 color=red>Please pick a filename</font><p>');
1.1       www       104:        &phaseone($r,$fn,$uname,$udom);
                    105:     }
1.4       www       106:   } else {
                    107:     $r->print(
                    108:    '<font size=+1 color=red>Please pick a filename</font><p>');
                    109:     &phaseone($r,$fn,$uname,$udom);
                    110:   }
1.1       www       111: }
                    112: 
                    113: sub handler {
                    114: 
                    115:   my $r=shift;
                    116: 
1.3       www       117:   my $uname;
                    118:   my $udom;
                    119: 
1.5       www       120:   ($uname,$udom)=
1.3       www       121:     &Apache::loncacc::constructaccess(
1.5       www       122: 			 $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
                    123:   unless (($uname) && ($udom)) {
1.3       www       124:      $r->log_reason($uname.' at '.$udom.
                    125:          ' trying to publish file '.$ENV{'form.filename'}.
                    126:          ' - not authorized', 
                    127:          $r->filename); 
                    128:      return HTTP_NOT_ACCEPTABLE;
                    129:   }
                    130: 
1.1       www       131:   my $fn;
                    132: 
                    133:   if ($ENV{'form.filename'}) {
                    134:       $fn=$ENV{'form.filename'};
1.3       www       135:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
1.1       www       136:   } else {
                    137:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       138:          ' unspecified filename for upload', $r->filename); 
1.1       www       139:      return HTTP_NOT_FOUND;
                    140:   }
                    141: 
                    142: # ----------------------------------------------------------- Start page output
                    143: 
                    144: 
                    145:   $r->content_type('text/html');
                    146:   $r->send_http_header;
                    147: 
                    148:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    149: 
                    150:   $r->print(
                    151:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    152: 
                    153:   
1.2       www       154:   $r->print('<h1>Upload file to Construction Space</h1>');
1.3       www       155:   
                    156:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    157:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                    158:                '</font></h3>');
                    159:   }
                    160: 
1.1       www       161: 
                    162:   if ($ENV{'form.phase'} eq 'two') {
                    163:       &phasetwo($r,$fn,$uname,$udom);
                    164:   } else {
                    165:       &phaseone($r,$fn,$uname,$udom);
                    166:   }
                    167: 
                    168:   $r->print('</body></html>');
                    169:   return OK;  
                    170: }
1.7     ! www       171: 
        !           172: 1;
        !           173: __END__

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