| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # ----------------------------------------------------------------------------- | ||
| 4 | # | ||
| 5 | # Nes by Skriptke | ||
| 6 | # Copyright 2009 - 2010 Enrique F. Castañón Barbero | ||
| 7 | # Licensed under the GNU GPL. | ||
| 8 | # | ||
| 9 | # CPAN: | ||
| 10 | # http://search.cpan.org/dist/Nes/ | ||
| 11 | # | ||
| 12 | # Sample: | ||
| 13 | # http://nes.sourceforge.net/ | ||
| 14 | # | ||
| 15 | # Repository: | ||
| 16 | # http://github.com/Skriptke/nes | ||
| 17 | # | ||
| 18 | # Version 1.03 | ||
| 19 | # | ||
| 20 | # file_upload.pl | ||
| 21 | # | ||
| 22 | # ----------------------------------------------------------------------------- | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use Nes; | ||
| 26 | |||
| 27 | my $nes = Nes::Singleton->new(); | ||
| 28 | my $query = $nes->{'query'}; | ||
| 29 | my $cfg = $nes->{'CFG'}; | ||
| 30 | my $data = '('.$query->{'q'}{'file_upload_param_1'}.')'; | ||
| 31 | my %vars = eval "$data"; | ||
| 32 | my @thefiles; | ||
| 33 | my $error = 0; | ||
| 34 | |||
| 35 | $vars{'modal_loading'} = 'file_upload_loading.nhtml' if !exists $vars{'modal_loading'}; | ||
| 36 | $vars{'msg_loading'} = 'Uploading file...' if !$vars{'msg_loading'}; | ||
| 37 | $vars{'msg_wait'} = 'Please wait.' if !$vars{'msg_wait'}; | ||
| 38 | $vars{'msg_cancel'} = 'Cancel' if !$vars{'msg_cancel'}; | ||
| 39 | $vars{'msg_error_max'} = '<img src="'.$cfg->{'img_dir'}.'/error_i.gif"> A file or whole post exceeding the size limit.' if !$vars{'msg_error_max'}; | ||
| 40 | $vars{'cancel_page'} = 'http://'.$ENV{'SERVER_NAME'}.$ENV{'REQUEST_URI'} if !$vars{'cancel_page'}; | ||
| 41 | $vars{'p_loading'} = '<p>'.$vars{'msg_loading'}.'<br><img src="/cgi-bin/nes/images/f_loading.gif"><br>'.$vars{'msg_wait'}.'</p>' if !$vars{'p_loading'}; | ||
| 42 | $vars{'p_cancel'} = '<p><a href="'.$vars{'cancel_page'}.'"><small>'.$vars{'msg_cancel'}.'</small></a></p>' if !$vars{'p_cancel'}; | ||
| 43 | |||
| 44 | foreach my $this ( @{ $vars{'uploads'} } ) { | ||
| 45 | $this->{'size'} = 10 if !$this->{'size'}; | ||
| 46 | $error = $vars{'error_max'} = $vars{'msg_error_max'} if $query->upload_max_size || $query->post_max_size; | ||
| 47 | push(@thefiles, $this) if $query->{'q'}{$this->{'field_name'}}; | ||
| 48 | } | ||
| 49 | |||
| 50 | if ( @thefiles && !$error ) { | ||
| 51 | |||
| 52 | foreach my $this ( @thefiles ) { | ||
| 53 | $this->{'filename'} = $query->get_upload_name($this->{'field_name'}); | ||
| 54 | to_dir($this) if $vars{'to_dir'}; | ||
| 55 | } | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | $nes->out(%vars); | ||
| 60 | |||
| 61 | sub to_dir { | ||
| 62 | my $file = shift; | ||
| 63 | |||
| 64 | my $filename = $vars{'to_dir'}.'/'.$file->{'filename'}; | ||
| 65 | |||
| 66 | open(my $fh,'>',$filename) or warn "Can't write file : $file"; | ||
| 67 | binmode $fh; | ||
| 68 | if ( $query->upload_is_tmp($file->{'field_name'}) ) { | ||
| 69 | my $buffer; | ||
| 70 | while ( $query->get_upload_buffer($file->{'field_name'},\$buffer) ) { | ||
| 71 | print $fh $buffer; | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | print $fh $query->{'q'}{$file->{'field_name'}}; | ||
| 75 | } | ||
| 76 | close $fh; | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | # don't forget to return a true value from the file | ||
| 82 | 1; |