~alexharrington/xibo/pyclient-1.1.0a22

« back to all changes in this revision

Viewing changes to server/3rdparty/fckeditor/editor/filemanager/connectors/perl/upload_fck.pl

  • Committer: Dan Garner
  • Date: 2008-12-14 14:42:52 UTC
  • mto: (1.1.80 Xibo)
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: mail@dangarner.co.uk-20081214144252-8dosaegtfwvv0dsl
Moved 3rd Party libraries to their own folder.
Updated jQuery to the latest revision and now use jQuery UI instead of individual plugins.

Tabs are not currently working

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#####
2
 
#  FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
 
#  Copyright (C) 2003-2007 Frederico Caldeira Knabben
4
 
#
5
 
#  == BEGIN LICENSE ==
6
 
#
7
 
#  Licensed under the terms of any of the following licenses at your
8
 
#  choice:
9
 
#
10
 
#   - GNU General Public License Version 2 or later (the "GPL")
11
 
#     http://www.gnu.org/licenses/gpl.html
12
 
#
13
 
#   - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
 
#     http://www.gnu.org/licenses/lgpl.html
15
 
#
16
 
#   - Mozilla Public License Version 1.1 or later (the "MPL")
17
 
#     http://www.mozilla.org/MPL/MPL-1.1.html
18
 
#
19
 
#  == END LICENSE ==
20
 
#
21
 
#  This is the File Manager Connector for Perl.
22
 
#####
23
 
 
24
 
# image data save dir
25
 
$img_dir        = './temp/';
26
 
 
27
 
 
28
 
# File size max(unit KB)
29
 
$MAX_CONTENT_SIZE =  30000;
30
 
 
31
 
# Filelock (1=use,0=not use)
32
 
$PM{'flock'}            = '1';
33
 
 
34
 
 
35
 
# upload Content-Type list
36
 
my %UPLOAD_CONTENT_TYPE_LIST = (
37
 
        'image/(x-)?png'                                                =>      'png',  # PNG image
38
 
        'image/p?jpe?g'                                                 =>      'jpg',  # JPEG image
39
 
        'image/gif'                                                             =>      'gif',  # GIF image
40
 
        'image/x-xbitmap'                                               =>      'xbm',  # XBM image
41
 
 
42
 
        'image/(x-(MS-)?)?bmp'                                  =>      'bmp',  # Windows BMP image
43
 
        'image/pict'                                                    =>      'pict', # Macintosh PICT image
44
 
        'image/tiff'                                                    =>      'tif',  # TIFF image
45
 
        'application/pdf'                                               =>      'pdf',  # PDF image
46
 
        'application/x-shockwave-flash'                 =>      'swf',  # Shockwave Flash
47
 
 
48
 
        'video/(x-)?msvideo'                                    =>      'avi',  # Microsoft Video
49
 
        'video/quicktime'                                               =>      'mov',  # QuickTime Video
50
 
        'video/mpeg'                                                    =>      'mpeg', # MPEG Video
51
 
        'video/x-mpeg2'                                                 =>      'mpv2', # MPEG2 Video
52
 
 
53
 
        'audio/(x-)?midi?'                                              =>      'mid',  # MIDI Audio
54
 
        'audio/(x-)?wav'                                                =>      'wav',  # WAV Audio
55
 
        'audio/basic'                                                   =>      'au',   # ULAW Audio
56
 
        'audio/mpeg'                                                    =>      'mpga', # MPEG Audio
57
 
 
58
 
        'application/(x-)?zip(-compressed)?'    =>      'zip',  # ZIP Compress
59
 
 
60
 
        'text/html'                                                             =>      'html', # HTML
61
 
        'text/plain'                                                    =>      'txt',  # TEXT
62
 
        '(?:application|text)/(?:rtf|richtext)' =>      'rtf',  # RichText
63
 
 
64
 
        'application/msword'                                    =>      'doc',  # Microsoft Word
65
 
        'application/vnd.ms-excel'                              =>      'xls',  # Microsoft Excel
66
 
 
67
 
        ''
68
 
);
69
 
 
70
 
# Upload is permitted.
71
 
# A regular expression is possible.
72
 
my %UPLOAD_EXT_LIST = (
73
 
        'png'                                   =>      'PNG image',
74
 
        'p?jpe?g|jpe|jfif|pjp'  =>      'JPEG image',
75
 
        'gif'                                   =>      'GIF image',
76
 
        'xbm'                                   =>      'XBM image',
77
 
 
78
 
        'bmp|dib|rle'                   =>      'Windows BMP image',
79
 
        'pi?ct'                                 =>      'Macintosh PICT image',
80
 
        'tiff?'                                 =>      'TIFF image',
81
 
        'pdf'                                   =>      'PDF image',
82
 
        'swf'                                   =>      'Shockwave Flash',
83
 
 
84
 
        'avi'                                   =>      'Microsoft Video',
85
 
        'moo?v|qt'                              =>      'QuickTime Video',
86
 
        'm(p(e?gv?|e|v)|1v)'    =>      'MPEG Video',
87
 
        'mp(v2|2v)'                             =>      'MPEG2 Video',
88
 
 
89
 
        'midi?|kar|smf|rmi|mff' =>      'MIDI Audio',
90
 
        'wav'                                   =>      'WAVE Audio',
91
 
        'au|snd'                                =>      'ULAW Audio',
92
 
        'mp(e?ga|2|a|3)|abs'    =>      'MPEG Audio',
93
 
 
94
 
        'zip'                                   =>      'ZIP Compress',
95
 
        'lzh'                                   =>      'LZH Compress',
96
 
        'cab'                                   =>      'CAB Compress',
97
 
 
98
 
        'd?html?'                               =>      'HTML',
99
 
        'rtf|rtx'                               =>      'RichText',
100
 
        'txt|text'                              =>      'Text',
101
 
 
102
 
        ''
103
 
);
104
 
 
105
 
 
106
 
# sjis or euc
107
 
my $CHARCODE = 'sjis';
108
 
 
109
 
$TRANS_2BYTE_CODE = 0;
110
 
 
111
 
##############################################################################
112
 
# Summary
113
 
#
114
 
# Form Read input
115
 
#
116
 
# Parameters
117
 
# Returns
118
 
# Memo
119
 
##############################################################################
120
 
sub read_input
121
 
{
122
 
eval("use File::Copy;");
123
 
eval("use File::Path;");
124
 
 
125
 
        my ($FORM) = @_;
126
 
 
127
 
 
128
 
        mkdir($img_dir,0777);
129
 
        chmod(0777,$img_dir);
130
 
 
131
 
        undef $img_data_exists;
132
 
        undef @NEWFNAMES;
133
 
        undef @NEWFNAME_DATA;
134
 
 
135
 
        if($ENV{'CONTENT_LENGTH'} > 10000000 || $ENV{'CONTENT_LENGTH'} > $MAX_CONTENT_SIZE * 1024) {
136
 
                &upload_error(
137
 
                        'Size Error',
138
 
                        sprintf(
139
 
                                "Transmitting size is too large.MAX <strong>%d KB</strong> Now Size <strong>%d KB</strong>(<strong>%d bytes</strong> Over)",
140
 
                                $MAX_CONTENT_SIZE,
141
 
                                int($ENV{'CONTENT_LENGTH'} / 1024),
142
 
                                $ENV{'CONTENT_LENGTH'} - $MAX_CONTENT_SIZE * 1024
143
 
                        )
144
 
                );
145
 
        }
146
 
 
147
 
        my $Buffer;
148
 
        if($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data/) {
149
 
                # METHOD POST only
150
 
                return  unless($ENV{'CONTENT_LENGTH'});
151
 
 
152
 
                binmode(STDIN);
153
 
                # STDIN A pause character is detected.'(MacIE3.0 boundary of $ENV{'CONTENT_TYPE'} cannot be trusted.)
154
 
                my $Boundary = <STDIN>;
155
 
                $Boundary =~ s/\x0D\x0A//;
156
 
                $Boundary = quotemeta($Boundary);
157
 
                while(<STDIN>) {
158
 
                        if(/^\s*Content-Disposition:/i) {
159
 
                                my($name,$ContentType,$FileName);
160
 
                                # form data get
161
 
                                if(/\bname="([^"]+)"/i || /\bname=([^\s:;]+)/i) {
162
 
                                        $name = $1;
163
 
                                        $name   =~ tr/+/ /;
164
 
                                        $name   =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
165
 
                                        &Encode(\$name);
166
 
                                }
167
 
                                if(/\bfilename="([^"]*)"/i || /\bfilename=([^\s:;]*)/i) {
168
 
                                        $FileName = $1 || 'unknown';
169
 
                                }
170
 
                                # head read
171
 
                                while(<STDIN>) {
172
 
                                        last    if(! /\w/);
173
 
                                        if(/^\s*Content-Type:\s*"([^"]+)"/i || /^\s*Content-Type:\s*([^\s:;]+)/i) {
174
 
                                                $ContentType = $1;
175
 
                                        }
176
 
                                }
177
 
                                # body read
178
 
                                $value = "";
179
 
                                while(<STDIN>) {
180
 
                                        last    if(/^$Boundary/o);
181
 
                                        $value .= $_;
182
 
                                };
183
 
                                $lastline = $_;
184
 
                                $value =~s /\x0D\x0A$//;
185
 
                                if($value ne '') {
186
 
                                        if($FileName || $ContentType) {
187
 
                                                $img_data_exists = 1;
188
 
                                                (
189
 
                                                        $FileName,              #
190
 
                                                        $Ext,                   #
191
 
                                                        $Length,                #
192
 
                                                        $ImageWidth,    #
193
 
                                                        $ImageHeight,   #
194
 
                                                        $ContentName    #
195
 
                                                ) = &CheckContentType(\$value,$FileName,$ContentType);
196
 
 
197
 
                                                $FORM{$name}    = $FileName;
198
 
                                                $new_fname              = $FileName;
199
 
                                                push(@NEWFNAME_DATA,"$FileName\t$Ext\t$Length\t$ImageWidth\t$ImageHeight\t$ContentName");
200
 
 
201
 
                                                # Multi-upload correspondence
202
 
                                                push(@NEWFNAMES,$new_fname);
203
 
                                                open(OUT,">$img_dir/$new_fname");
204
 
                                                binmode(OUT);
205
 
                                                eval "flock(OUT,2);" if($PM{'flock'} == 1);
206
 
                                                print OUT $value;
207
 
                                                eval "flock(OUT,8);" if($PM{'flock'} == 1);
208
 
                                                close(OUT);
209
 
 
210
 
                                        } elsif($name) {
211
 
                                                $value  =~ tr/+/ /;
212
 
                                                $value  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
213
 
                                                &Encode(\$value,'trans');
214
 
                                                $FORM{$name} .= "\0"                    if(defined($FORM{$name}));
215
 
                                                $FORM{$name} .= $value;
216
 
                                        }
217
 
                                }
218
 
                        };
219
 
                        last if($lastline =~ /^$Boundary\-\-/o);
220
 
                }
221
 
        } elsif($ENV{'CONTENT_LENGTH'}) {
222
 
                read(STDIN,$Buffer,$ENV{'CONTENT_LENGTH'});
223
 
        }
224
 
        foreach(split(/&/,$Buffer),split(/&/,$ENV{'QUERY_STRING'})) {
225
 
                my($name, $value) = split(/=/);
226
 
                $name   =~ tr/+/ /;
227
 
                $name   =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
228
 
                $value  =~ tr/+/ /;
229
 
                $value  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
230
 
 
231
 
                &Encode(\$name);
232
 
                &Encode(\$value,'trans');
233
 
                $FORM{$name} .= "\0"                    if(defined($FORM{$name}));
234
 
                $FORM{$name} .= $value;
235
 
 
236
 
        }
237
 
 
238
 
}
239
 
 
240
 
##############################################################################
241
 
# Summary
242
 
#
243
 
#       CheckContentType
244
 
#
245
 
# Parameters
246
 
# Returns
247
 
# Memo
248
 
##############################################################################
249
 
sub CheckContentType
250
 
{
251
 
 
252
 
        my($DATA,$FileName,$ContentType) = @_;
253
 
        my($Ext,$ImageWidth,$ImageHeight,$ContentName,$Infomation);
254
 
        my $DataLength = length($$DATA);
255
 
 
256
 
        # An unknown file type
257
 
 
258
 
        $_ = $ContentType;
259
 
        my $UnknownType = (
260
 
                !$_
261
 
                || /^application\/(x-)?macbinary$/i
262
 
                || /^application\/applefile$/i
263
 
                || /^application\/octet-stream$/i
264
 
                || /^text\/plane$/i
265
 
                || /^x-unknown-content-type/i
266
 
        );
267
 
 
268
 
        # MacBinary(Mac Unnecessary data are deleted.)
269
 
        if($UnknownType || $ENV{'HTTP_USER_AGENT'} =~ /Macintosh|Mac_/) {
270
 
                if($DataLength > 128 && !unpack("C",substr($$DATA,0,1)) && !unpack("C",substr($$DATA,74,1)) && !unpack("C",substr($$DATA,82,1)) ) {
271
 
                        my $MacBinary_ForkLength = unpack("N", substr($$DATA, 83, 4));          # ForkLength Get
272
 
                        my $MacBinary_FileName = quotemeta(substr($$DATA, 2, unpack("C",substr($$DATA, 1, 1))));
273
 
                        if($MacBinary_FileName && $MacBinary_ForkLength && $DataLength >= $MacBinary_ForkLength + 128
274
 
                                        && ($FileName =~ /$MacBinary_FileName/i || substr($$DATA,102,4) eq 'mBIN')) {   # DATA TOP 128byte MacBinary!!
275
 
                                $$DATA                          = substr($$DATA,128,$MacBinary_ForkLength);
276
 
                                my $ResourceLength      = $DataLength - $MacBinary_ForkLength - 128;
277
 
                                $DataLength                     = $MacBinary_ForkLength;
278
 
                        }
279
 
                }
280
 
        }
281
 
 
282
 
        # A file name is changed into EUC.
283
 
#       &jcode::convert(\$FileName,'euc',$FormCodeDefault);
284
 
#       &jcode::h2z_euc(\$FileName);
285
 
        $FileName =~ s/^.*\\//;                                 # Windows, Mac
286
 
        $FileName =~ s/^.*\///;                                 # UNIX
287
 
        $FileName =~ s/&/&amp;/g;
288
 
        $FileName =~ s/"/&quot;/g;
289
 
        $FileName =~ s/</&lt;/g;
290
 
        $FileName =~ s/>/&gt;/g;
291
 
#
292
 
#       if($CHARCODE ne 'euc') {
293
 
#               &jcode::convert(\$FileName,$CHARCODE,'euc');
294
 
#       }
295
 
 
296
 
        # An extension is extracted and it changes into a small letter.
297
 
        my $FileExt;
298
 
        if($FileName =~ /\.(\w+)$/) {
299
 
                $FileExt = $1;
300
 
                $FileExt =~ tr/A-Z/a-z/;
301
 
        }
302
 
 
303
 
        # Executable file detection (ban on upload)
304
 
        if($$DATA =~ /^MZ/) {
305
 
                $Ext = 'exe';
306
 
        }
307
 
        # text
308
 
        if(!$Ext && ($UnknownType || $ContentType =~ /^text\//i || $ContentType =~ /^application\/(?:rtf|richtext)$/i || $ContentType =~ /^image\/x-xbitmap$/i)
309
 
                                && ! $$DATA =~ /[\000-\006\177\377]/) {
310
 
#               $$DATA =~ s/\x0D\x0A/\n/g;
311
 
#               $$DATA =~ tr/\x0D\x0A/\n\n/;
312
 
#
313
 
#               if(
314
 
#                       $$DATA =~ /<\s*SCRIPT(?:.|\n)*?>/i
315
 
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bONLOAD\s*=(?:.|\n)*?>/i
316
 
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bONCLICK\s*=(?:.|\n)*?>/i
317
 
#                               ) {
318
 
#                       $Infomation = '(JavaScript contains)';
319
 
#               }
320
 
#               if($$DATA =~ /<\s*TABLE(?:.|\n)*?>/i
321
 
#                               || $$DATA =~ /<\s*BLINK(?:.|\n)*?>/i
322
 
#                               || $$DATA =~ /<\s*MARQUEE(?:.|\n)*?>/i
323
 
#                               || $$DATA =~ /<\s*OBJECT(?:.|\n)*?>/i
324
 
#                               || $$DATA =~ /<\s*EMBED(?:.|\n)*?>/i
325
 
#                               || $$DATA =~ /<\s*FRAME(?:.|\n)*?>/i
326
 
#                               || $$DATA =~ /<\s*APPLET(?:.|\n)*?>/i
327
 
#                               || $$DATA =~ /<\s*FORM(?:.|\n)*?>/i
328
 
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bSRC\s*=(?:.|\n)*?>/i
329
 
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bDYNSRC\s*=(?:.|\n)*?>/i
330
 
#                               ) {
331
 
#                       $Infomation = '(the HTML tag which is not safe is included)';
332
 
#               }
333
 
 
334
 
                if($FileExt =~ /^txt$/i || $FileExt =~ /^cgi$/i || $FileExt =~ /^pl$/i) {                                                               # Text File
335
 
                        $Ext = 'txt';
336
 
                } elsif($ContentType =~ /^text\/html$/i || $FileExt =~ /html?/i || $$DATA =~ /<\s*HTML(?:.|\n)*?>/i) {  # HTML File
337
 
                        $Ext = 'html';
338
 
                } elsif($ContentType =~ /^image\/x-xbitmap$/i || $FileExt =~ /^xbm$/i) {                                                                # XBM(x-BitMap) Image
339
 
                        my $XbmName = $1;
340
 
                        my ($XbmWidth, $XbmHeight);
341
 
                        if($$DATA =~ /\#define\s*$XbmName\_width\s*(\d+)/i) {
342
 
                                $XbmWidth = $1;
343
 
                        }
344
 
                        if($$DATA =~ /\#define\s*$XbmName\_height\s*(\d+)/i) {
345
 
                                $XbmHeight = $1;
346
 
                        }
347
 
                        if($XbmWidth && $XbmHeight) {
348
 
                                $Ext = 'xbm';
349
 
                                $ImageWidth             = $XbmWidth;
350
 
                                $ImageHeight    = $XbmHeight;
351
 
                        }
352
 
                } else {                #
353
 
                        $Ext = 'txt';
354
 
                }
355
 
        }
356
 
 
357
 
        # image
358
 
        if(!$Ext && ($UnknownType || $ContentType =~ /^image\//i)) {
359
 
                # PNG
360
 
                if($$DATA =~ /^\x89PNG\x0D\x0A\x1A\x0A/) {
361
 
                        if(substr($$DATA, 12, 4) eq 'IHDR') {
362
 
                                $Ext = 'png';
363
 
                                ($ImageWidth, $ImageHeight) = unpack("N2", substr($$DATA, 16, 8));
364
 
                        }
365
 
                } elsif($$DATA =~ /^GIF8(?:9|7)a/) {                                                                                                                    # GIF89a(modified), GIF89a, GIF87a
366
 
                        $Ext = 'gif';
367
 
                        ($ImageWidth, $ImageHeight) = unpack("v2", substr($$DATA, 6, 4));
368
 
                } elsif($$DATA =~ /^II\x2a\x00\x08\x00\x00\x00/ || $$DATA =~ /^MM\x00\x2a\x00\x00\x00\x08/) {   # TIFF
369
 
                        $Ext = 'tif';
370
 
                } elsif($$DATA =~ /^BM/) {                                                                                                                                              # BMP
371
 
                        $Ext = 'bmp';
372
 
                } elsif($$DATA =~ /^\xFF\xD8\xFF/ || $$DATA =~ /JFIF/) {                                                                                # JPEG
373
 
                        my $HeaderPoint = index($$DATA, "\xFF\xD8\xFF", 0);
374
 
                        my $Point = $HeaderPoint + 2;
375
 
                        while($Point < $DataLength) {
376
 
                                my($Maker, $MakerType, $MakerLength) = unpack("C2n",substr($$DATA,$Point,4));
377
 
                                if($Maker != 0xFF || $MakerType == 0xd9 || $MakerType == 0xda) {
378
 
                                        last;
379
 
                                } elsif($MakerType >= 0xC0 && $MakerType <= 0xC3) {
380
 
                                        $Ext = 'jpg';
381
 
                                        ($ImageHeight, $ImageWidth) = unpack("n2", substr($$DATA, $Point + 5, 4));
382
 
                                        if($HeaderPoint > 0) {
383
 
                                                $$DATA = substr($$DATA, $HeaderPoint);
384
 
                                                $DataLength = length($$DATA);
385
 
                                        }
386
 
                                        last;
387
 
                                } else {
388
 
                                        $Point += $MakerLength + 2;
389
 
                                }
390
 
                        }
391
 
                }
392
 
        }
393
 
 
394
 
        # audio
395
 
        if(!$Ext && ($UnknownType || $ContentType =~ /^audio\//i)) {
396
 
                # MIDI Audio
397
 
                if($$DATA =~ /^MThd/) {
398
 
                        $Ext = 'mid';
399
 
                } elsif($$DATA =~ /^\x2esnd/) {         # ULAW Audio
400
 
                        $Ext = 'au';
401
 
                } elsif($$DATA =~ /^RIFF/ || $$DATA =~ /^ID3/ && $$DATA =~ /RIFF/) {
402
 
                        my $HeaderPoint = index($$DATA, "RIFF", 0);
403
 
                        $_ = substr($$DATA, $HeaderPoint + 8, 8);
404
 
                        if(/^WAVEfmt $/) {
405
 
                                # WAVE
406
 
                                if(unpack("V",substr($$DATA, $HeaderPoint + 16, 4)) == 16) {
407
 
                                        $Ext = 'wav';
408
 
                                } else {                                        # RIFF WAVE MP3
409
 
                                        $Ext = 'mp3';
410
 
                                }
411
 
                        } elsif(/^RMIDdata$/) {                 # RIFF MIDI
412
 
                                $Ext = 'rmi';
413
 
                        } elsif(/^RMP3data$/) {                 # RIFF MP3
414
 
                                $Ext = 'rmp';
415
 
                        }
416
 
                        if($ContentType =~ /^audio\//i) {
417
 
                                $Infomation .= '(RIFF '. substr($$DATA, $HeaderPoint + 8, 4). ')';
418
 
                        }
419
 
                }
420
 
        }
421
 
 
422
 
        # a binary file
423
 
        unless ($Ext) {
424
 
                # PDF image
425
 
                if($$DATA =~ /^\%PDF/) {
426
 
                        # Picture size is not measured.
427
 
                        $Ext = 'pdf';
428
 
                } elsif($$DATA =~ /^FWS/) {             # Shockwave Flash
429
 
                        $Ext = 'swf';
430
 
                } elsif($$DATA =~ /^RIFF/ || $$DATA =~ /^ID3/ && $$DATA =~ /RIFF/) {
431
 
                        my $HeaderPoint = index($$DATA, "RIFF", 0);
432
 
                        $_ = substr($$DATA,$HeaderPoint + 8, 8);
433
 
                        # AVI
434
 
                        if(/^AVI LIST$/) {
435
 
                                $Ext = 'avi';
436
 
                        }
437
 
                        if($ContentType =~ /^video\//i) {
438
 
                                $Infomation .= '(RIFF '. substr($$DATA, $HeaderPoint + 8, 4). ')';
439
 
                        }
440
 
                } elsif($$DATA =~ /^PK/) {                      # ZIP Compress File
441
 
                        $Ext = 'zip';
442
 
                } elsif($$DATA =~ /^MSCF/) {            # CAB Compress File
443
 
                        $Ext = 'cab';
444
 
                } elsif($$DATA =~ /^Rar\!/) {           # RAR Compress File
445
 
                        $Ext = 'rar';
446
 
                } elsif(substr($$DATA, 2, 5) =~ /^\-lh(\d+|d)\-$/) {            # LHA Compress File
447
 
                        $Infomation .= "(lh$1)";
448
 
                        $Ext = 'lzh';
449
 
                } elsif(substr($$DATA, 325, 25) eq "Apple Video Media Handler" || substr($$DATA, 325, 30) eq "Apple \x83\x72\x83\x66\x83\x49\x81\x45\x83\x81\x83\x66\x83\x42\x83\x41\x83\x6E\x83\x93\x83\x68\x83\x89") {
450
 
                        # QuickTime
451
 
                        $Ext = 'mov';
452
 
                }
453
 
        }
454
 
 
455
 
        # Header analysis failure
456
 
        unless ($Ext) {
457
 
                # It will be followed if it applies for the MIME type from the browser.
458
 
                foreach (keys %UPLOAD_CONTENT_TYPE_LIST) {
459
 
                        next unless ($_);
460
 
                        if($ContentType =~ /^$_$/i) {
461
 
                                $Ext = $UPLOAD_CONTENT_TYPE_LIST{$_};
462
 
                                $ContentName = &CheckContentExt($Ext);
463
 
                                if(
464
 
                                        grep {$_ eq $Ext;} (
465
 
                                                'png',
466
 
                                                'gif',
467
 
                                                'jpg',
468
 
                                                'xbm',
469
 
                                                'tif',
470
 
                                                'bmp',
471
 
                                                'pdf',
472
 
                                                'swf',
473
 
                                                'mov',
474
 
                                                'zip',
475
 
                                                'cab',
476
 
                                                'lzh',
477
 
                                                'rar',
478
 
                                                'mid',
479
 
                                                'rmi',
480
 
                                                'au',
481
 
                                                'wav',
482
 
                                                'avi',
483
 
                                                'exe'
484
 
                                        )
485
 
                                ) {
486
 
                                        $Infomation .= ' / Header analysis failure';
487
 
                                }
488
 
                                if($Ext ne $FileExt && &CheckContentExt($FileExt) eq $ContentName) {
489
 
                                        $Ext = $FileExt;
490
 
                                }
491
 
                                last;
492
 
                        }
493
 
                }
494
 
                # a MIME type is unknown--It judges from an extension.
495
 
                unless ($Ext) {
496
 
                        $ContentName = &CheckContentExt($FileExt);
497
 
                        if($ContentName) {
498
 
                                $Ext = $FileExt;
499
 
                                $Infomation .= ' /      MIME type is unknown('. $ContentType. ')';
500
 
                                last;
501
 
                        }
502
 
                }
503
 
        }
504
 
 
505
 
#       $ContentName = &CheckContentExt($Ext)   unless($ContentName);
506
 
#       if($Ext && $ContentName) {
507
 
#               $ContentName .=  $Infomation;
508
 
#       } else {
509
 
#               &upload_error(
510
 
#                       'Extension Error',
511
 
#                       "$FileName A not corresponding extension ($Ext)<BR>The extension which can be responded ". join(',', sort values(%UPLOAD_EXT_LIST))
512
 
#               );
513
 
#       }
514
 
 
515
 
#       # SSI Tag Deletion
516
 
#       if($Ext =~ /.?html?/ && $$DATA =~ /<\!/) {
517
 
#               foreach (
518
 
#                       'config',
519
 
#                       'echo',
520
 
#                       'exec',
521
 
#                       'flastmod',
522
 
#                       'fsize',
523
 
#                       'include'
524
 
#               ) {
525
 
#                       $$DATA =~ s/\#\s*$_/\&\#35\;$_/ig
526
 
#               }
527
 
#       }
528
 
 
529
 
        return (
530
 
                $FileName,
531
 
                $Ext,
532
 
                int($DataLength / 1024 + 1),
533
 
                $ImageWidth,
534
 
                $ImageHeight,
535
 
                $ContentName
536
 
        );
537
 
}
538
 
 
539
 
##############################################################################
540
 
# Summary
541
 
#
542
 
# Extension discernment
543
 
#
544
 
# Parameters
545
 
# Returns
546
 
# Memo
547
 
##############################################################################
548
 
 
549
 
sub CheckContentExt
550
 
{
551
 
 
552
 
        my($Ext) = @_;
553
 
        my $ContentName;
554
 
        foreach (keys %UPLOAD_EXT_LIST) {
555
 
                next    unless ($_);
556
 
                if($_ && $Ext =~ /^$_$/) {
557
 
                        $ContentName = $UPLOAD_EXT_LIST{$_};
558
 
                        last;
559
 
                }
560
 
        }
561
 
        return $ContentName;
562
 
 
563
 
}
564
 
 
565
 
##############################################################################
566
 
# Summary
567
 
#
568
 
# Form decode
569
 
#
570
 
# Parameters
571
 
# Returns
572
 
# Memo
573
 
##############################################################################
574
 
sub Encode
575
 
{
576
 
 
577
 
        my($value,$Trans) = @_;
578
 
 
579
 
#       my $FormCode = &jcode::getcode($value) || $FormCodeDefault;
580
 
#       $FormCodeDefault ||= $FormCode;
581
 
#
582
 
#       if($Trans && $TRANS_2BYTE_CODE) {
583
 
#               if($FormCode ne 'euc') {
584
 
#                       &jcode::convert($value, 'euc', $FormCode);
585
 
#               }
586
 
#               &jcode::tr(
587
 
#                       $value,
588
 
#                       "\xA3\xB0-\xA3\xB9\xA3\xC1-\xA3\xDA\xA3\xE1-\xA3\xFA",
589
 
#                       '0-9A-Za-z'
590
 
#               );
591
 
#               if($CHARCODE ne 'euc') {
592
 
#                       &jcode::convert($value,$CHARCODE,'euc');
593
 
#               }
594
 
#       } else {
595
 
#               if($CHARCODE ne $FormCode) {
596
 
#                       &jcode::convert($value,$CHARCODE,$FormCode);
597
 
#               }
598
 
#       }
599
 
#       if($CHARCODE eq 'euc') {
600
 
#               &jcode::h2z_euc($value);
601
 
#       } elsif($CHARCODE eq 'sjis') {
602
 
#               &jcode::h2z_sjis($value);
603
 
#       }
604
 
 
605
 
}
606
 
 
607
 
##############################################################################
608
 
# Summary
609
 
#
610
 
# Error Msg
611
 
#
612
 
# Parameters
613
 
# Returns
614
 
# Memo
615
 
##############################################################################
616
 
 
617
 
sub upload_error
618
 
{
619
 
 
620
 
        local($error_message)   = $_[0];
621
 
        local($error_message2)  = $_[1];
622
 
 
623
 
        print "Content-type: text/html\n\n";
624
 
        print<<EOF;
625
 
<HTML>
626
 
<HEAD>
627
 
<TITLE>Error Message</TITLE></HEAD>
628
 
<BODY>
629
 
<table border="1" cellspacing="10" cellpadding="10">
630
 
        <TR bgcolor="#0000B0">
631
 
        <TD bgcolor="#0000B0" NOWRAP><font size="-1" color="white"><B>Error Message</B></font></TD>
632
 
        </TR>
633
 
</table>
634
 
<UL>
635
 
<H4> $error_message </H4>
636
 
$error_message2 <BR>
637
 
</UL>
638
 
</BODY>
639
 
</HTML>
640
 
EOF
641
 
        &rm_tmp_uploaded_files;                 # Image Temporary deletion
642
 
        exit;
643
 
}
644
 
 
645
 
##############################################################################
646
 
# Summary
647
 
#
648
 
# Image Temporary deletion
649
 
#
650
 
# Parameters
651
 
# Returns
652
 
# Memo
653
 
##############################################################################
654
 
 
655
 
sub rm_tmp_uploaded_files
656
 
{
657
 
        if($img_data_exists == 1){
658
 
                sleep 1;
659
 
                foreach $fname_list(@NEWFNAMES) {
660
 
                        if(-e "$img_dir/$fname_list") {
661
 
                                unlink("$img_dir/$fname_list");
662
 
                        }
663
 
                }
664
 
        }
665
 
 
666
 
}
667
 
1;
 
1
#####
 
2
#  FCKeditor - The text editor for Internet - http://www.fckeditor.net
 
3
#  Copyright (C) 2003-2007 Frederico Caldeira Knabben
 
4
#
 
5
#  == BEGIN LICENSE ==
 
6
#
 
7
#  Licensed under the terms of any of the following licenses at your
 
8
#  choice:
 
9
#
 
10
#   - GNU General Public License Version 2 or later (the "GPL")
 
11
#     http://www.gnu.org/licenses/gpl.html
 
12
#
 
13
#   - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 
14
#     http://www.gnu.org/licenses/lgpl.html
 
15
#
 
16
#   - Mozilla Public License Version 1.1 or later (the "MPL")
 
17
#     http://www.mozilla.org/MPL/MPL-1.1.html
 
18
#
 
19
#  == END LICENSE ==
 
20
#
 
21
#  This is the File Manager Connector for Perl.
 
22
#####
 
23
 
 
24
# image data save dir
 
25
$img_dir        = './temp/';
 
26
 
 
27
 
 
28
# File size max(unit KB)
 
29
$MAX_CONTENT_SIZE =  30000;
 
30
 
 
31
# Filelock (1=use,0=not use)
 
32
$PM{'flock'}            = '1';
 
33
 
 
34
 
 
35
# upload Content-Type list
 
36
my %UPLOAD_CONTENT_TYPE_LIST = (
 
37
        'image/(x-)?png'                                                =>      'png',  # PNG image
 
38
        'image/p?jpe?g'                                                 =>      'jpg',  # JPEG image
 
39
        'image/gif'                                                             =>      'gif',  # GIF image
 
40
        'image/x-xbitmap'                                               =>      'xbm',  # XBM image
 
41
 
 
42
        'image/(x-(MS-)?)?bmp'                                  =>      'bmp',  # Windows BMP image
 
43
        'image/pict'                                                    =>      'pict', # Macintosh PICT image
 
44
        'image/tiff'                                                    =>      'tif',  # TIFF image
 
45
        'application/pdf'                                               =>      'pdf',  # PDF image
 
46
        'application/x-shockwave-flash'                 =>      'swf',  # Shockwave Flash
 
47
 
 
48
        'video/(x-)?msvideo'                                    =>      'avi',  # Microsoft Video
 
49
        'video/quicktime'                                               =>      'mov',  # QuickTime Video
 
50
        'video/mpeg'                                                    =>      'mpeg', # MPEG Video
 
51
        'video/x-mpeg2'                                                 =>      'mpv2', # MPEG2 Video
 
52
 
 
53
        'audio/(x-)?midi?'                                              =>      'mid',  # MIDI Audio
 
54
        'audio/(x-)?wav'                                                =>      'wav',  # WAV Audio
 
55
        'audio/basic'                                                   =>      'au',   # ULAW Audio
 
56
        'audio/mpeg'                                                    =>      'mpga', # MPEG Audio
 
57
 
 
58
        'application/(x-)?zip(-compressed)?'    =>      'zip',  # ZIP Compress
 
59
 
 
60
        'text/html'                                                             =>      'html', # HTML
 
61
        'text/plain'                                                    =>      'txt',  # TEXT
 
62
        '(?:application|text)/(?:rtf|richtext)' =>      'rtf',  # RichText
 
63
 
 
64
        'application/msword'                                    =>      'doc',  # Microsoft Word
 
65
        'application/vnd.ms-excel'                              =>      'xls',  # Microsoft Excel
 
66
 
 
67
        ''
 
68
);
 
69
 
 
70
# Upload is permitted.
 
71
# A regular expression is possible.
 
72
my %UPLOAD_EXT_LIST = (
 
73
        'png'                                   =>      'PNG image',
 
74
        'p?jpe?g|jpe|jfif|pjp'  =>      'JPEG image',
 
75
        'gif'                                   =>      'GIF image',
 
76
        'xbm'                                   =>      'XBM image',
 
77
 
 
78
        'bmp|dib|rle'                   =>      'Windows BMP image',
 
79
        'pi?ct'                                 =>      'Macintosh PICT image',
 
80
        'tiff?'                                 =>      'TIFF image',
 
81
        'pdf'                                   =>      'PDF image',
 
82
        'swf'                                   =>      'Shockwave Flash',
 
83
 
 
84
        'avi'                                   =>      'Microsoft Video',
 
85
        'moo?v|qt'                              =>      'QuickTime Video',
 
86
        'm(p(e?gv?|e|v)|1v)'    =>      'MPEG Video',
 
87
        'mp(v2|2v)'                             =>      'MPEG2 Video',
 
88
 
 
89
        'midi?|kar|smf|rmi|mff' =>      'MIDI Audio',
 
90
        'wav'                                   =>      'WAVE Audio',
 
91
        'au|snd'                                =>      'ULAW Audio',
 
92
        'mp(e?ga|2|a|3)|abs'    =>      'MPEG Audio',
 
93
 
 
94
        'zip'                                   =>      'ZIP Compress',
 
95
        'lzh'                                   =>      'LZH Compress',
 
96
        'cab'                                   =>      'CAB Compress',
 
97
 
 
98
        'd?html?'                               =>      'HTML',
 
99
        'rtf|rtx'                               =>      'RichText',
 
100
        'txt|text'                              =>      'Text',
 
101
 
 
102
        ''
 
103
);
 
104
 
 
105
 
 
106
# sjis or euc
 
107
my $CHARCODE = 'sjis';
 
108
 
 
109
$TRANS_2BYTE_CODE = 0;
 
110
 
 
111
##############################################################################
 
112
# Summary
 
113
#
 
114
# Form Read input
 
115
#
 
116
# Parameters
 
117
# Returns
 
118
# Memo
 
119
##############################################################################
 
120
sub read_input
 
121
{
 
122
eval("use File::Copy;");
 
123
eval("use File::Path;");
 
124
 
 
125
        my ($FORM) = @_;
 
126
 
 
127
 
 
128
        mkdir($img_dir,0777);
 
129
        chmod(0777,$img_dir);
 
130
 
 
131
        undef $img_data_exists;
 
132
        undef @NEWFNAMES;
 
133
        undef @NEWFNAME_DATA;
 
134
 
 
135
        if($ENV{'CONTENT_LENGTH'} > 10000000 || $ENV{'CONTENT_LENGTH'} > $MAX_CONTENT_SIZE * 1024) {
 
136
                &upload_error(
 
137
                        'Size Error',
 
138
                        sprintf(
 
139
                                "Transmitting size is too large.MAX <strong>%d KB</strong> Now Size <strong>%d KB</strong>(<strong>%d bytes</strong> Over)",
 
140
                                $MAX_CONTENT_SIZE,
 
141
                                int($ENV{'CONTENT_LENGTH'} / 1024),
 
142
                                $ENV{'CONTENT_LENGTH'} - $MAX_CONTENT_SIZE * 1024
 
143
                        )
 
144
                );
 
145
        }
 
146
 
 
147
        my $Buffer;
 
148
        if($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data/) {
 
149
                # METHOD POST only
 
150
                return  unless($ENV{'CONTENT_LENGTH'});
 
151
 
 
152
                binmode(STDIN);
 
153
                # STDIN A pause character is detected.'(MacIE3.0 boundary of $ENV{'CONTENT_TYPE'} cannot be trusted.)
 
154
                my $Boundary = <STDIN>;
 
155
                $Boundary =~ s/\x0D\x0A//;
 
156
                $Boundary = quotemeta($Boundary);
 
157
                while(<STDIN>) {
 
158
                        if(/^\s*Content-Disposition:/i) {
 
159
                                my($name,$ContentType,$FileName);
 
160
                                # form data get
 
161
                                if(/\bname="([^"]+)"/i || /\bname=([^\s:;]+)/i) {
 
162
                                        $name = $1;
 
163
                                        $name   =~ tr/+/ /;
 
164
                                        $name   =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 
165
                                        &Encode(\$name);
 
166
                                }
 
167
                                if(/\bfilename="([^"]*)"/i || /\bfilename=([^\s:;]*)/i) {
 
168
                                        $FileName = $1 || 'unknown';
 
169
                                }
 
170
                                # head read
 
171
                                while(<STDIN>) {
 
172
                                        last    if(! /\w/);
 
173
                                        if(/^\s*Content-Type:\s*"([^"]+)"/i || /^\s*Content-Type:\s*([^\s:;]+)/i) {
 
174
                                                $ContentType = $1;
 
175
                                        }
 
176
                                }
 
177
                                # body read
 
178
                                $value = "";
 
179
                                while(<STDIN>) {
 
180
                                        last    if(/^$Boundary/o);
 
181
                                        $value .= $_;
 
182
                                };
 
183
                                $lastline = $_;
 
184
                                $value =~s /\x0D\x0A$//;
 
185
                                if($value ne '') {
 
186
                                        if($FileName || $ContentType) {
 
187
                                                $img_data_exists = 1;
 
188
                                                (
 
189
                                                        $FileName,              #
 
190
                                                        $Ext,                   #
 
191
                                                        $Length,                #
 
192
                                                        $ImageWidth,    #
 
193
                                                        $ImageHeight,   #
 
194
                                                        $ContentName    #
 
195
                                                ) = &CheckContentType(\$value,$FileName,$ContentType);
 
196
 
 
197
                                                $FORM{$name}    = $FileName;
 
198
                                                $new_fname              = $FileName;
 
199
                                                push(@NEWFNAME_DATA,"$FileName\t$Ext\t$Length\t$ImageWidth\t$ImageHeight\t$ContentName");
 
200
 
 
201
                                                # Multi-upload correspondence
 
202
                                                push(@NEWFNAMES,$new_fname);
 
203
                                                open(OUT,">$img_dir/$new_fname");
 
204
                                                binmode(OUT);
 
205
                                                eval "flock(OUT,2);" if($PM{'flock'} == 1);
 
206
                                                print OUT $value;
 
207
                                                eval "flock(OUT,8);" if($PM{'flock'} == 1);
 
208
                                                close(OUT);
 
209
 
 
210
                                        } elsif($name) {
 
211
                                                $value  =~ tr/+/ /;
 
212
                                                $value  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 
213
                                                &Encode(\$value,'trans');
 
214
                                                $FORM{$name} .= "\0"                    if(defined($FORM{$name}));
 
215
                                                $FORM{$name} .= $value;
 
216
                                        }
 
217
                                }
 
218
                        };
 
219
                        last if($lastline =~ /^$Boundary\-\-/o);
 
220
                }
 
221
        } elsif($ENV{'CONTENT_LENGTH'}) {
 
222
                read(STDIN,$Buffer,$ENV{'CONTENT_LENGTH'});
 
223
        }
 
224
        foreach(split(/&/,$Buffer),split(/&/,$ENV{'QUERY_STRING'})) {
 
225
                my($name, $value) = split(/=/);
 
226
                $name   =~ tr/+/ /;
 
227
                $name   =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 
228
                $value  =~ tr/+/ /;
 
229
                $value  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 
230
 
 
231
                &Encode(\$name);
 
232
                &Encode(\$value,'trans');
 
233
                $FORM{$name} .= "\0"                    if(defined($FORM{$name}));
 
234
                $FORM{$name} .= $value;
 
235
 
 
236
        }
 
237
 
 
238
}
 
239
 
 
240
##############################################################################
 
241
# Summary
 
242
#
 
243
#       CheckContentType
 
244
#
 
245
# Parameters
 
246
# Returns
 
247
# Memo
 
248
##############################################################################
 
249
sub CheckContentType
 
250
{
 
251
 
 
252
        my($DATA,$FileName,$ContentType) = @_;
 
253
        my($Ext,$ImageWidth,$ImageHeight,$ContentName,$Infomation);
 
254
        my $DataLength = length($$DATA);
 
255
 
 
256
        # An unknown file type
 
257
 
 
258
        $_ = $ContentType;
 
259
        my $UnknownType = (
 
260
                !$_
 
261
                || /^application\/(x-)?macbinary$/i
 
262
                || /^application\/applefile$/i
 
263
                || /^application\/octet-stream$/i
 
264
                || /^text\/plane$/i
 
265
                || /^x-unknown-content-type/i
 
266
        );
 
267
 
 
268
        # MacBinary(Mac Unnecessary data are deleted.)
 
269
        if($UnknownType || $ENV{'HTTP_USER_AGENT'} =~ /Macintosh|Mac_/) {
 
270
                if($DataLength > 128 && !unpack("C",substr($$DATA,0,1)) && !unpack("C",substr($$DATA,74,1)) && !unpack("C",substr($$DATA,82,1)) ) {
 
271
                        my $MacBinary_ForkLength = unpack("N", substr($$DATA, 83, 4));          # ForkLength Get
 
272
                        my $MacBinary_FileName = quotemeta(substr($$DATA, 2, unpack("C",substr($$DATA, 1, 1))));
 
273
                        if($MacBinary_FileName && $MacBinary_ForkLength && $DataLength >= $MacBinary_ForkLength + 128
 
274
                                        && ($FileName =~ /$MacBinary_FileName/i || substr($$DATA,102,4) eq 'mBIN')) {   # DATA TOP 128byte MacBinary!!
 
275
                                $$DATA                          = substr($$DATA,128,$MacBinary_ForkLength);
 
276
                                my $ResourceLength      = $DataLength - $MacBinary_ForkLength - 128;
 
277
                                $DataLength                     = $MacBinary_ForkLength;
 
278
                        }
 
279
                }
 
280
        }
 
281
 
 
282
        # A file name is changed into EUC.
 
283
#       &jcode::convert(\$FileName,'euc',$FormCodeDefault);
 
284
#       &jcode::h2z_euc(\$FileName);
 
285
        $FileName =~ s/^.*\\//;                                 # Windows, Mac
 
286
        $FileName =~ s/^.*\///;                                 # UNIX
 
287
        $FileName =~ s/&/&amp;/g;
 
288
        $FileName =~ s/"/&quot;/g;
 
289
        $FileName =~ s/</&lt;/g;
 
290
        $FileName =~ s/>/&gt;/g;
 
291
#
 
292
#       if($CHARCODE ne 'euc') {
 
293
#               &jcode::convert(\$FileName,$CHARCODE,'euc');
 
294
#       }
 
295
 
 
296
        # An extension is extracted and it changes into a small letter.
 
297
        my $FileExt;
 
298
        if($FileName =~ /\.(\w+)$/) {
 
299
                $FileExt = $1;
 
300
                $FileExt =~ tr/A-Z/a-z/;
 
301
        }
 
302
 
 
303
        # Executable file detection (ban on upload)
 
304
        if($$DATA =~ /^MZ/) {
 
305
                $Ext = 'exe';
 
306
        }
 
307
        # text
 
308
        if(!$Ext && ($UnknownType || $ContentType =~ /^text\//i || $ContentType =~ /^application\/(?:rtf|richtext)$/i || $ContentType =~ /^image\/x-xbitmap$/i)
 
309
                                && ! $$DATA =~ /[\000-\006\177\377]/) {
 
310
#               $$DATA =~ s/\x0D\x0A/\n/g;
 
311
#               $$DATA =~ tr/\x0D\x0A/\n\n/;
 
312
#
 
313
#               if(
 
314
#                       $$DATA =~ /<\s*SCRIPT(?:.|\n)*?>/i
 
315
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bONLOAD\s*=(?:.|\n)*?>/i
 
316
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bONCLICK\s*=(?:.|\n)*?>/i
 
317
#                               ) {
 
318
#                       $Infomation = '(JavaScript contains)';
 
319
#               }
 
320
#               if($$DATA =~ /<\s*TABLE(?:.|\n)*?>/i
 
321
#                               || $$DATA =~ /<\s*BLINK(?:.|\n)*?>/i
 
322
#                               || $$DATA =~ /<\s*MARQUEE(?:.|\n)*?>/i
 
323
#                               || $$DATA =~ /<\s*OBJECT(?:.|\n)*?>/i
 
324
#                               || $$DATA =~ /<\s*EMBED(?:.|\n)*?>/i
 
325
#                               || $$DATA =~ /<\s*FRAME(?:.|\n)*?>/i
 
326
#                               || $$DATA =~ /<\s*APPLET(?:.|\n)*?>/i
 
327
#                               || $$DATA =~ /<\s*FORM(?:.|\n)*?>/i
 
328
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bSRC\s*=(?:.|\n)*?>/i
 
329
#                               || $$DATA =~ /<\s*(?:.|\n)*?\bDYNSRC\s*=(?:.|\n)*?>/i
 
330
#                               ) {
 
331
#                       $Infomation = '(the HTML tag which is not safe is included)';
 
332
#               }
 
333
 
 
334
                if($FileExt =~ /^txt$/i || $FileExt =~ /^cgi$/i || $FileExt =~ /^pl$/i) {                                                               # Text File
 
335
                        $Ext = 'txt';
 
336
                } elsif($ContentType =~ /^text\/html$/i || $FileExt =~ /html?/i || $$DATA =~ /<\s*HTML(?:.|\n)*?>/i) {  # HTML File
 
337
                        $Ext = 'html';
 
338
                } elsif($ContentType =~ /^image\/x-xbitmap$/i || $FileExt =~ /^xbm$/i) {                                                                # XBM(x-BitMap) Image
 
339
                        my $XbmName = $1;
 
340
                        my ($XbmWidth, $XbmHeight);
 
341
                        if($$DATA =~ /\#define\s*$XbmName\_width\s*(\d+)/i) {
 
342
                                $XbmWidth = $1;
 
343
                        }
 
344
                        if($$DATA =~ /\#define\s*$XbmName\_height\s*(\d+)/i) {
 
345
                                $XbmHeight = $1;
 
346
                        }
 
347
                        if($XbmWidth && $XbmHeight) {
 
348
                                $Ext = 'xbm';
 
349
                                $ImageWidth             = $XbmWidth;
 
350
                                $ImageHeight    = $XbmHeight;
 
351
                        }
 
352
                } else {                #
 
353
                        $Ext = 'txt';
 
354
                }
 
355
        }
 
356
 
 
357
        # image
 
358
        if(!$Ext && ($UnknownType || $ContentType =~ /^image\//i)) {
 
359
                # PNG
 
360
                if($$DATA =~ /^\x89PNG\x0D\x0A\x1A\x0A/) {
 
361
                        if(substr($$DATA, 12, 4) eq 'IHDR') {
 
362
                                $Ext = 'png';
 
363
                                ($ImageWidth, $ImageHeight) = unpack("N2", substr($$DATA, 16, 8));
 
364
                        }
 
365
                } elsif($$DATA =~ /^GIF8(?:9|7)a/) {                                                                                                                    # GIF89a(modified), GIF89a, GIF87a
 
366
                        $Ext = 'gif';
 
367
                        ($ImageWidth, $ImageHeight) = unpack("v2", substr($$DATA, 6, 4));
 
368
                } elsif($$DATA =~ /^II\x2a\x00\x08\x00\x00\x00/ || $$DATA =~ /^MM\x00\x2a\x00\x00\x00\x08/) {   # TIFF
 
369
                        $Ext = 'tif';
 
370
                } elsif($$DATA =~ /^BM/) {                                                                                                                                              # BMP
 
371
                        $Ext = 'bmp';
 
372
                } elsif($$DATA =~ /^\xFF\xD8\xFF/ || $$DATA =~ /JFIF/) {                                                                                # JPEG
 
373
                        my $HeaderPoint = index($$DATA, "\xFF\xD8\xFF", 0);
 
374
                        my $Point = $HeaderPoint + 2;
 
375
                        while($Point < $DataLength) {
 
376
                                my($Maker, $MakerType, $MakerLength) = unpack("C2n",substr($$DATA,$Point,4));
 
377
                                if($Maker != 0xFF || $MakerType == 0xd9 || $MakerType == 0xda) {
 
378
                                        last;
 
379
                                } elsif($MakerType >= 0xC0 && $MakerType <= 0xC3) {
 
380
                                        $Ext = 'jpg';
 
381
                                        ($ImageHeight, $ImageWidth) = unpack("n2", substr($$DATA, $Point + 5, 4));
 
382
                                        if($HeaderPoint > 0) {
 
383
                                                $$DATA = substr($$DATA, $HeaderPoint);
 
384
                                                $DataLength = length($$DATA);
 
385
                                        }
 
386
                                        last;
 
387
                                } else {
 
388
                                        $Point += $MakerLength + 2;
 
389
                                }
 
390
                        }
 
391
                }
 
392
        }
 
393
 
 
394
        # audio
 
395
        if(!$Ext && ($UnknownType || $ContentType =~ /^audio\//i)) {
 
396
                # MIDI Audio
 
397
                if($$DATA =~ /^MThd/) {
 
398
                        $Ext = 'mid';
 
399
                } elsif($$DATA =~ /^\x2esnd/) {         # ULAW Audio
 
400
                        $Ext = 'au';
 
401
                } elsif($$DATA =~ /^RIFF/ || $$DATA =~ /^ID3/ && $$DATA =~ /RIFF/) {
 
402
                        my $HeaderPoint = index($$DATA, "RIFF", 0);
 
403
                        $_ = substr($$DATA, $HeaderPoint + 8, 8);
 
404
                        if(/^WAVEfmt $/) {
 
405
                                # WAVE
 
406
                                if(unpack("V",substr($$DATA, $HeaderPoint + 16, 4)) == 16) {
 
407
                                        $Ext = 'wav';
 
408
                                } else {                                        # RIFF WAVE MP3
 
409
                                        $Ext = 'mp3';
 
410
                                }
 
411
                        } elsif(/^RMIDdata$/) {                 # RIFF MIDI
 
412
                                $Ext = 'rmi';
 
413
                        } elsif(/^RMP3data$/) {                 # RIFF MP3
 
414
                                $Ext = 'rmp';
 
415
                        }
 
416
                        if($ContentType =~ /^audio\//i) {
 
417
                                $Infomation .= '(RIFF '. substr($$DATA, $HeaderPoint + 8, 4). ')';
 
418
                        }
 
419
                }
 
420
        }
 
421
 
 
422
        # a binary file
 
423
        unless ($Ext) {
 
424
                # PDF image
 
425
                if($$DATA =~ /^\%PDF/) {
 
426
                        # Picture size is not measured.
 
427
                        $Ext = 'pdf';
 
428
                } elsif($$DATA =~ /^FWS/) {             # Shockwave Flash
 
429
                        $Ext = 'swf';
 
430
                } elsif($$DATA =~ /^RIFF/ || $$DATA =~ /^ID3/ && $$DATA =~ /RIFF/) {
 
431
                        my $HeaderPoint = index($$DATA, "RIFF", 0);
 
432
                        $_ = substr($$DATA,$HeaderPoint + 8, 8);
 
433
                        # AVI
 
434
                        if(/^AVI LIST$/) {
 
435
                                $Ext = 'avi';
 
436
                        }
 
437
                        if($ContentType =~ /^video\//i) {
 
438
                                $Infomation .= '(RIFF '. substr($$DATA, $HeaderPoint + 8, 4). ')';
 
439
                        }
 
440
                } elsif($$DATA =~ /^PK/) {                      # ZIP Compress File
 
441
                        $Ext = 'zip';
 
442
                } elsif($$DATA =~ /^MSCF/) {            # CAB Compress File
 
443
                        $Ext = 'cab';
 
444
                } elsif($$DATA =~ /^Rar\!/) {           # RAR Compress File
 
445
                        $Ext = 'rar';
 
446
                } elsif(substr($$DATA, 2, 5) =~ /^\-lh(\d+|d)\-$/) {            # LHA Compress File
 
447
                        $Infomation .= "(lh$1)";
 
448
                        $Ext = 'lzh';
 
449
                } elsif(substr($$DATA, 325, 25) eq "Apple Video Media Handler" || substr($$DATA, 325, 30) eq "Apple \x83\x72\x83\x66\x83\x49\x81\x45\x83\x81\x83\x66\x83\x42\x83\x41\x83\x6E\x83\x93\x83\x68\x83\x89") {
 
450
                        # QuickTime
 
451
                        $Ext = 'mov';
 
452
                }
 
453
        }
 
454
 
 
455
        # Header analysis failure
 
456
        unless ($Ext) {
 
457
                # It will be followed if it applies for the MIME type from the browser.
 
458
                foreach (keys %UPLOAD_CONTENT_TYPE_LIST) {
 
459
                        next unless ($_);
 
460
                        if($ContentType =~ /^$_$/i) {
 
461
                                $Ext = $UPLOAD_CONTENT_TYPE_LIST{$_};
 
462
                                $ContentName = &CheckContentExt($Ext);
 
463
                                if(
 
464
                                        grep {$_ eq $Ext;} (
 
465
                                                'png',
 
466
                                                'gif',
 
467
                                                'jpg',
 
468
                                                'xbm',
 
469
                                                'tif',
 
470
                                                'bmp',
 
471
                                                'pdf',
 
472
                                                'swf',
 
473
                                                'mov',
 
474
                                                'zip',
 
475
                                                'cab',
 
476
                                                'lzh',
 
477
                                                'rar',
 
478
                                                'mid',
 
479
                                                'rmi',
 
480
                                                'au',
 
481
                                                'wav',
 
482
                                                'avi',
 
483
                                                'exe'
 
484
                                        )
 
485
                                ) {
 
486
                                        $Infomation .= ' / Header analysis failure';
 
487
                                }
 
488
                                if($Ext ne $FileExt && &CheckContentExt($FileExt) eq $ContentName) {
 
489
                                        $Ext = $FileExt;
 
490
                                }
 
491
                                last;
 
492
                        }
 
493
                }
 
494
                # a MIME type is unknown--It judges from an extension.
 
495
                unless ($Ext) {
 
496
                        $ContentName = &CheckContentExt($FileExt);
 
497
                        if($ContentName) {
 
498
                                $Ext = $FileExt;
 
499
                                $Infomation .= ' /      MIME type is unknown('. $ContentType. ')';
 
500
                                last;
 
501
                        }
 
502
                }
 
503
        }
 
504
 
 
505
#       $ContentName = &CheckContentExt($Ext)   unless($ContentName);
 
506
#       if($Ext && $ContentName) {
 
507
#               $ContentName .=  $Infomation;
 
508
#       } else {
 
509
#               &upload_error(
 
510
#                       'Extension Error',
 
511
#                       "$FileName A not corresponding extension ($Ext)<BR>The extension which can be responded ". join(',', sort values(%UPLOAD_EXT_LIST))
 
512
#               );
 
513
#       }
 
514
 
 
515
#       # SSI Tag Deletion
 
516
#       if($Ext =~ /.?html?/ && $$DATA =~ /<\!/) {
 
517
#               foreach (
 
518
#                       'config',
 
519
#                       'echo',
 
520
#                       'exec',
 
521
#                       'flastmod',
 
522
#                       'fsize',
 
523
#                       'include'
 
524
#               ) {
 
525
#                       $$DATA =~ s/\#\s*$_/\&\#35\;$_/ig
 
526
#               }
 
527
#       }
 
528
 
 
529
        return (
 
530
                $FileName,
 
531
                $Ext,
 
532
                int($DataLength / 1024 + 1),
 
533
                $ImageWidth,
 
534
                $ImageHeight,
 
535
                $ContentName
 
536
        );
 
537
}
 
538
 
 
539
##############################################################################
 
540
# Summary
 
541
#
 
542
# Extension discernment
 
543
#
 
544
# Parameters
 
545
# Returns
 
546
# Memo
 
547
##############################################################################
 
548
 
 
549
sub CheckContentExt
 
550
{
 
551
 
 
552
        my($Ext) = @_;
 
553
        my $ContentName;
 
554
        foreach (keys %UPLOAD_EXT_LIST) {
 
555
                next    unless ($_);
 
556
                if($_ && $Ext =~ /^$_$/) {
 
557
                        $ContentName = $UPLOAD_EXT_LIST{$_};
 
558
                        last;
 
559
                }
 
560
        }
 
561
        return $ContentName;
 
562
 
 
563
}
 
564
 
 
565
##############################################################################
 
566
# Summary
 
567
#
 
568
# Form decode
 
569
#
 
570
# Parameters
 
571
# Returns
 
572
# Memo
 
573
##############################################################################
 
574
sub Encode
 
575
{
 
576
 
 
577
        my($value,$Trans) = @_;
 
578
 
 
579
#       my $FormCode = &jcode::getcode($value) || $FormCodeDefault;
 
580
#       $FormCodeDefault ||= $FormCode;
 
581
#
 
582
#       if($Trans && $TRANS_2BYTE_CODE) {
 
583
#               if($FormCode ne 'euc') {
 
584
#                       &jcode::convert($value, 'euc', $FormCode);
 
585
#               }
 
586
#               &jcode::tr(
 
587
#                       $value,
 
588
#                       "\xA3\xB0-\xA3\xB9\xA3\xC1-\xA3\xDA\xA3\xE1-\xA3\xFA",
 
589
#                       '0-9A-Za-z'
 
590
#               );
 
591
#               if($CHARCODE ne 'euc') {
 
592
#                       &jcode::convert($value,$CHARCODE,'euc');
 
593
#               }
 
594
#       } else {
 
595
#               if($CHARCODE ne $FormCode) {
 
596
#                       &jcode::convert($value,$CHARCODE,$FormCode);
 
597
#               }
 
598
#       }
 
599
#       if($CHARCODE eq 'euc') {
 
600
#               &jcode::h2z_euc($value);
 
601
#       } elsif($CHARCODE eq 'sjis') {
 
602
#               &jcode::h2z_sjis($value);
 
603
#       }
 
604
 
 
605
}
 
606
 
 
607
##############################################################################
 
608
# Summary
 
609
#
 
610
# Error Msg
 
611
#
 
612
# Parameters
 
613
# Returns
 
614
# Memo
 
615
##############################################################################
 
616
 
 
617
sub upload_error
 
618
{
 
619
 
 
620
        local($error_message)   = $_[0];
 
621
        local($error_message2)  = $_[1];
 
622
 
 
623
        print "Content-type: text/html\n\n";
 
624
        print<<EOF;
 
625
<HTML>
 
626
<HEAD>
 
627
<TITLE>Error Message</TITLE></HEAD>
 
628
<BODY>
 
629
<table border="1" cellspacing="10" cellpadding="10">
 
630
        <TR bgcolor="#0000B0">
 
631
        <TD bgcolor="#0000B0" NOWRAP><font size="-1" color="white"><B>Error Message</B></font></TD>
 
632
        </TR>
 
633
</table>
 
634
<UL>
 
635
<H4> $error_message </H4>
 
636
$error_message2 <BR>
 
637
</UL>
 
638
</BODY>
 
639
</HTML>
 
640
EOF
 
641
        &rm_tmp_uploaded_files;                 # Image Temporary deletion
 
642
        exit;
 
643
}
 
644
 
 
645
##############################################################################
 
646
# Summary
 
647
#
 
648
# Image Temporary deletion
 
649
#
 
650
# Parameters
 
651
# Returns
 
652
# Memo
 
653
##############################################################################
 
654
 
 
655
sub rm_tmp_uploaded_files
 
656
{
 
657
        if($img_data_exists == 1){
 
658
                sleep 1;
 
659
                foreach $fname_list(@NEWFNAMES) {
 
660
                        if(-e "$img_dir/$fname_list") {
 
661
                                unlink("$img_dir/$fname_list");
 
662
                        }
 
663
                }
 
664
        }
 
665
 
 
666
}
 
667
1;