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

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.3     ! www        18: # 04/05,04/09,05/25 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 {
                     48:     my ($r,$fn,$uname,$udom)=@_;
                     49:     $fn=~s/\/[^\/]+$//;
                     50:     $fn=~s/([^\/])$/$1\//;
                     51:     $fn.=$ENV{'form.upfile.filename'};
1.3     ! www        52:     $fn=~s/^\///;
        !            53:     $fn=~s/(\/)+/\//g;
        !            54: 
        !            55:     if (($fn) && ($fn!~/\/$/)) {
        !            56:       $r->print(
1.2       www        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 '.
1.3     ! www        61:  '<input type=text size=50 name=filename value="/priv/'.
        !            62:   $uname.'/'.$fn.'"><br>'.
1.2       www        63:  '<input type=submit value="Store"></form>');
1.3     ! www        64:   } else {
        !            65:       $r->print('<font color=red>Illegal filename.</font>');
        !            66:   }
1.1       www        67: }
                     68: 
                     69: sub phasetwo {
                     70:     my ($r,$fn,$uname,$udom)=@_;
1.3     ! www        71:     my $tfn=$fn;
        !            72:     $tfn=~s/^\/(\~|priv)\/(\w+)//;
        !            73:     my $target='/home/'.$uname.'/public_html'.$tfn;
1.2       www        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.');
1.3     ! www        89:               $r->print('<p><font size=+2><a href="'.$fn.
1.2       www        90:                         '">View file</a></font>');
                     91: 	   } else {
                     92:               $r->print('Failed to copy: '.$!);
                     93: 	   }
                     94:        }
1.1       www        95:     } else {
                     96:        $r->print(
1.2       www        97:    '<font size=+1 color=red>Please pick a filename</font><p>');
1.1       www        98:        &phaseone($r,$fn,$uname,$udom);
                     99:     }
                    100: }
                    101: 
                    102: sub handler {
                    103: 
                    104:   my $r=shift;
                    105: 
1.3     ! www       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: 
1.1       www       119:   my $fn;
                    120: 
                    121:   if ($ENV{'form.filename'}) {
                    122:       $fn=$ENV{'form.filename'};
1.3     ! www       123:       $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
1.1       www       124:   } else {
                    125:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       126:          ' unspecified filename for upload', $r->filename); 
1.1       www       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:   
1.2       www       142:   $r->print('<h1>Upload file to Construction Space</h1>');
1.3     ! www       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: 
1.1       www       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>