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

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.2     ! www        18: # 04/05,04/09 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);
                     26: 
                     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'};
        !            52:     $r->print(
        !            53:  '<form action=/adm/upload method=post>'.
        !            54:  '<input type=hidden name=phase value=two>'.
        !            55:  '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
        !            56:  'Store uploaded file as '.
        !            57:  '<input type=text size=50 name=filename value="'.$fn.'"><br>'.
        !            58:  '<input type=submit value="Store"></form>');
1.1       www        59: }
                     60: 
                     61: sub phasetwo {
                     62:     my ($r,$fn,$uname,$udom)=@_;
1.2     ! www        63:     my $target='/home/'.$uname.'/public_html'.$fn;
        !            64:     my $datatoken=$ENV{'form.datatoken'};
        !            65:     if (($fn) && ($datatoken)) {
        !            66: 	if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
        !            67:            $r->print(
        !            68:  '<form action=/adm/upload method=post>'.
        !            69:  'File <tt>'.$fn.'</tt> exists. Overwrite? '.
        !            70:  '<input type=hidden name=phase value=two>'.
        !            71:  '<input type=hidden name=filename value="'.$fn.'">'.
        !            72:  '<input type=hidden name=datatoken value="'.$datatoken.'">'.
        !            73:  '<input type=submit name=override value="Yes"></form>');
        !            74:        } else {
        !            75:            my $source=$r->dir_config('lonDaemons').
        !            76: 	                             '/tmp/'.$datatoken.'.tmp';
        !            77:            if (copy($source,$target)) {
        !            78: 	      $r->print('File copied.');
        !            79:               $r->print('<p><font size=+2><a href="/priv/'.$uname.$fn.
        !            80:                         '">View file</a></font>');
        !            81: 	   } else {
        !            82:               $r->print('Failed to copy: '.$!);
        !            83: 	   }
        !            84:        }
1.1       www        85:     } else {
                     86:        $r->print(
1.2     ! www        87:    '<font size=+1 color=red>Please pick a filename</font><p>');
1.1       www        88:        &phaseone($r,$fn,$uname,$udom);
                     89:     }
                     90: }
                     91: 
                     92: sub handler {
                     93: 
                     94:   my $r=shift;
                     95: 
                     96:   my $fn;
                     97: 
                     98:   if ($ENV{'form.filename'}) {
                     99:       $fn=$ENV{'form.filename'};
                    100:       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//;
                    101:   } else {
                    102:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2     ! www       103:          ' unspecified filename for upload', $r->filename); 
1.1       www       104:      return HTTP_NOT_FOUND;
                    105:   }
                    106: 
                    107: # ----------------------------------------------------------- Start page output
                    108: 
                    109:   my $uname=$ENV{'user.name'};
                    110:   my $udom=$ENV{'user.domain'};
                    111: 
                    112:   $r->content_type('text/html');
                    113:   $r->send_http_header;
                    114: 
                    115:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    116: 
                    117:   $r->print(
                    118:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    119: 
                    120:   
1.2     ! www       121:   $r->print('<h1>Upload file to Construction Space</h1>');
1.1       www       122: 
                    123:   if ($ENV{'form.phase'} eq 'two') {
                    124:       &phasetwo($r,$fn,$uname,$udom);
                    125:   } else {
                    126:       &phaseone($r,$fn,$uname,$udom);
                    127:   }
                    128: 
                    129:   $r->print('</body></html>');
                    130:   return OK;  
                    131: }

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