2
# FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
# Copyright (C) 2003-2010 Frederico Caldeira Knabben
7
# Licensed under the terms of any of the following licenses at your
10
# - GNU General Public License Version 2 or later (the "GPL")
11
# http://www.gnu.org/licenses/gpl.html
13
# - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
# http://www.gnu.org/licenses/lgpl.html
16
# - Mozilla Public License Version 1.1 or later (the "MPL")
17
# http://www.mozilla.org/MPL/MPL-1.1.html
21
# This is the File Manager Connector for Perl.
28
# File size max(unit KB)
29
$MAX_CONTENT_SIZE = 30000;
31
# After file is uploaded, sometimes it is required to change its permissions
32
# so that it was possible to access it at the later time.
33
# If possible, it is recommended to set more restrictive permissions, like 0755.
34
# Set to 0 to disable this feature.
35
$CHMOD_ON_UPLOAD = 0777;
38
# Used when creating folders that does not exist.
39
$CHMOD_ON_FOLDER_CREATE = 0755;
41
# Filelock (1=use,0=not use)
45
# upload Content-Type list
46
my %UPLOAD_CONTENT_TYPE_LIST = (
47
'image/(x-)?png' => 'png', # PNG image
48
'image/p?jpe?g' => 'jpg', # JPEG image
49
'image/gif' => 'gif', # GIF image
50
'image/x-xbitmap' => 'xbm', # XBM image
52
'image/(x-(MS-)?)?bmp' => 'bmp', # Windows BMP image
53
'image/pict' => 'pict', # Macintosh PICT image
54
'image/tiff' => 'tif', # TIFF image
55
'application/pdf' => 'pdf', # PDF image
56
'application/x-shockwave-flash' => 'swf', # Shockwave Flash
58
'video/(x-)?msvideo' => 'avi', # Microsoft Video
59
'video/quicktime' => 'mov', # QuickTime Video
60
'video/mpeg' => 'mpeg', # MPEG Video
61
'video/x-mpeg2' => 'mpv2', # MPEG2 Video
63
'audio/(x-)?midi?' => 'mid', # MIDI Audio
64
'audio/(x-)?wav' => 'wav', # WAV Audio
65
'audio/basic' => 'au', # ULAW Audio
66
'audio/mpeg' => 'mpga', # MPEG Audio
68
'application/(x-)?zip(-compressed)?' => 'zip', # ZIP Compress
70
'text/html' => 'html', # HTML
71
'text/plain' => 'txt', # TEXT
72
'(?:application|text)/(?:rtf|richtext)' => 'rtf', # RichText
74
'application/msword' => 'doc', # Microsoft Word
75
'application/vnd.ms-excel' => 'xls', # Microsoft Excel
80
# Upload is permitted.
81
# A regular expression is possible.
82
my %UPLOAD_EXT_LIST = (
84
'p?jpe?g|jpe|jfif|pjp' => 'JPEG image',
88
'bmp|dib|rle' => 'Windows BMP image',
89
'pi?ct' => 'Macintosh PICT image',
90
'tiff?' => 'TIFF image',
92
'swf' => 'Shockwave Flash',
94
'avi' => 'Microsoft Video',
95
'moo?v|qt' => 'QuickTime Video',
96
'm(p(e?gv?|e|v)|1v)' => 'MPEG Video',
97
'mp(v2|2v)' => 'MPEG2 Video',
99
'midi?|kar|smf|rmi|mff' => 'MIDI Audio',
100
'wav' => 'WAVE Audio',
101
'au|snd' => 'ULAW Audio',
102
'mp(e?ga|2|a|3)|abs' => 'MPEG Audio',
104
'zip' => 'ZIP Compress',
105
'lzh' => 'LZH Compress',
106
'cab' => 'CAB Compress',
109
'rtf|rtx' => 'RichText',
110
'txt|text' => 'Text',
117
my $CHARCODE = 'sjis';
119
$TRANS_2BYTE_CODE = 0;
121
##############################################################################
129
##############################################################################
132
eval("use File::Copy;");
133
eval("use File::Path;");
137
if (defined $CHMOD_ON_FOLDER_CREATE && !$CHMOD_ON_FOLDER_CREATE) {
142
if (defined $CHMOD_ON_FOLDER_CREATE) {
143
mkdir("$img_dir",$CHMOD_ON_FOLDER_CREATE);
146
mkdir("$img_dir",0777);
150
undef $img_data_exists;
152
undef @NEWFNAME_DATA;
154
if($ENV{'CONTENT_LENGTH'} > 10000000 || $ENV{'CONTENT_LENGTH'} > $MAX_CONTENT_SIZE * 1024) {
158
"Transmitting size is too large.MAX <strong>%d KB</strong> Now Size <strong>%d KB</strong>(<strong>%d bytes</strong> Over)",
160
int($ENV{'CONTENT_LENGTH'} / 1024),
161
$ENV{'CONTENT_LENGTH'} - $MAX_CONTENT_SIZE * 1024
167
if($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data/) {
169
return unless($ENV{'CONTENT_LENGTH'});
172
# STDIN A pause character is detected.'(MacIE3.0 boundary of $ENV{'CONTENT_TYPE'} cannot be trusted.)
173
my $Boundary = <STDIN>;
174
$Boundary =~ s/\x0D\x0A//;
175
$Boundary = quotemeta($Boundary);
177
if(/^\s*Content-Disposition:/i) {
178
my($name,$ContentType,$FileName);
180
if(/\bname="([^"]+)"/i || /\bname=([^\s:;]+)/i) {
183
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
186
if(/\bfilename="([^"]*)"/i || /\bfilename=([^\s:;]*)/i) {
187
$FileName = $1 || 'unknown';
192
if(/^\s*Content-Type:\s*"([^"]+)"/i || /^\s*Content-Type:\s*([^\s:;]+)/i) {
199
last if(/^$Boundary/o);
203
$value =~s /\x0D\x0A$//;
205
if($FileName || $ContentType) {
206
$img_data_exists = 1;
214
) = &CheckContentType(\$value,$FileName,$ContentType);
216
$FORM{$name} = $FileName;
217
$new_fname = $FileName;
218
push(@NEWFNAME_DATA,"$FileName\t$Ext\t$Length\t$ImageWidth\t$ImageHeight\t$ContentName");
220
# Multi-upload correspondence
221
push(@NEWFNAMES,$new_fname);
222
open(OUT,">$img_dir/$new_fname");
224
eval "flock(OUT,2);" if($PM{'flock'} == 1);
226
eval "flock(OUT,8);" if($PM{'flock'} == 1);
231
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
232
&Encode(\$value,'trans');
233
$FORM{$name} .= "\0" if(defined($FORM{$name}));
234
$FORM{$name} .= $value;
238
last if($lastline =~ /^$Boundary\-\-/o);
240
} elsif($ENV{'CONTENT_LENGTH'}) {
241
read(STDIN,$Buffer,$ENV{'CONTENT_LENGTH'});
243
foreach(split(/&/,$Buffer),split(/&/,$ENV{'QUERY_STRING'})) {
244
my($name, $value) = split(/=/);
246
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
248
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
251
&Encode(\$value,'trans');
252
$FORM{$name} .= "\0" if(defined($FORM{$name}));
253
$FORM{$name} .= $value;
259
##############################################################################
267
##############################################################################
271
my($DATA,$FileName,$ContentType) = @_;
272
my($Ext,$ImageWidth,$ImageHeight,$ContentName,$Infomation);
273
my $DataLength = length($$DATA);
275
# An unknown file type
280
|| /^application\/(x-)?macbinary$/i
281
|| /^application\/applefile$/i
282
|| /^application\/octet-stream$/i
284
|| /^x-unknown-content-type/i
287
# MacBinary(Mac Unnecessary data are deleted.)
288
if($UnknownType || $ENV{'HTTP_USER_AGENT'} =~ /Macintosh|Mac_/) {
289
if($DataLength > 128 && !unpack("C",substr($$DATA,0,1)) && !unpack("C",substr($$DATA,74,1)) && !unpack("C",substr($$DATA,82,1)) ) {
290
my $MacBinary_ForkLength = unpack("N", substr($$DATA, 83, 4)); # ForkLength Get
291
my $MacBinary_FileName = quotemeta(substr($$DATA, 2, unpack("C",substr($$DATA, 1, 1))));
292
if($MacBinary_FileName && $MacBinary_ForkLength && $DataLength >= $MacBinary_ForkLength + 128
293
&& ($FileName =~ /$MacBinary_FileName/i || substr($$DATA,102,4) eq 'mBIN')) { # DATA TOP 128byte MacBinary!!
294
$$DATA = substr($$DATA,128,$MacBinary_ForkLength);
295
my $ResourceLength = $DataLength - $MacBinary_ForkLength - 128;
296
$DataLength = $MacBinary_ForkLength;
301
# A file name is changed into EUC.
302
# &jcode::convert(\$FileName,'euc',$FormCodeDefault);
303
# &jcode::h2z_euc(\$FileName);
304
$FileName =~ s/^.*\\//; # Windows, Mac
305
$FileName =~ s/^.*\///; # UNIX
306
$FileName =~ s/&/&/g;
307
$FileName =~ s/"/"/g;
308
$FileName =~ s/</</g;
309
$FileName =~ s/>/>/g;
311
# if($CHARCODE ne 'euc') {
312
# &jcode::convert(\$FileName,$CHARCODE,'euc');
315
# An extension is extracted and it changes into a small letter.
317
if($FileName =~ /\.(\w+)$/) {
319
$FileExt =~ tr/A-Z/a-z/;
322
# Executable file detection (ban on upload)
323
if($$DATA =~ /^MZ/) {
327
if(!$Ext && ($UnknownType || $ContentType =~ /^text\//i || $ContentType =~ /^application\/(?:rtf|richtext)$/i || $ContentType =~ /^image\/x-xbitmap$/i)
328
&& ! $$DATA =~ /[\000-\006\177\377]/) {
329
# $$DATA =~ s/\x0D\x0A/\n/g;
330
# $$DATA =~ tr/\x0D\x0A/\n\n/;
333
# $$DATA =~ /<\s*SCRIPT(?:.|\n)*?>/i
334
# || $$DATA =~ /<\s*(?:.|\n)*?\bONLOAD\s*=(?:.|\n)*?>/i
335
# || $$DATA =~ /<\s*(?:.|\n)*?\bONCLICK\s*=(?:.|\n)*?>/i
337
# $Infomation = '(JavaScript contains)';
339
# if($$DATA =~ /<\s*TABLE(?:.|\n)*?>/i
340
# || $$DATA =~ /<\s*BLINK(?:.|\n)*?>/i
341
# || $$DATA =~ /<\s*MARQUEE(?:.|\n)*?>/i
342
# || $$DATA =~ /<\s*OBJECT(?:.|\n)*?>/i
343
# || $$DATA =~ /<\s*EMBED(?:.|\n)*?>/i
344
# || $$DATA =~ /<\s*FRAME(?:.|\n)*?>/i
345
# || $$DATA =~ /<\s*APPLET(?:.|\n)*?>/i
346
# || $$DATA =~ /<\s*FORM(?:.|\n)*?>/i
347
# || $$DATA =~ /<\s*(?:.|\n)*?\bSRC\s*=(?:.|\n)*?>/i
348
# || $$DATA =~ /<\s*(?:.|\n)*?\bDYNSRC\s*=(?:.|\n)*?>/i
350
# $Infomation = '(the HTML tag which is not safe is included)';
353
if($FileExt =~ /^txt$/i || $FileExt =~ /^cgi$/i || $FileExt =~ /^pl$/i) { # Text File
355
} elsif($ContentType =~ /^text\/html$/i || $FileExt =~ /html?/i || $$DATA =~ /<\s*HTML(?:.|\n)*?>/i) { # HTML File
357
} elsif($ContentType =~ /^image\/x-xbitmap$/i || $FileExt =~ /^xbm$/i) { # XBM(x-BitMap) Image
359
my ($XbmWidth, $XbmHeight);
360
if($$DATA =~ /\#define\s*$XbmName\_width\s*(\d+)/i) {
363
if($$DATA =~ /\#define\s*$XbmName\_height\s*(\d+)/i) {
366
if($XbmWidth && $XbmHeight) {
368
$ImageWidth = $XbmWidth;
369
$ImageHeight = $XbmHeight;
377
if(!$Ext && ($UnknownType || $ContentType =~ /^image\//i)) {
379
if($$DATA =~ /^\x89PNG\x0D\x0A\x1A\x0A/) {
380
if(substr($$DATA, 12, 4) eq 'IHDR') {
382
($ImageWidth, $ImageHeight) = unpack("N2", substr($$DATA, 16, 8));
384
} elsif($$DATA =~ /^GIF8(?:9|7)a/) { # GIF89a(modified), GIF89a, GIF87a
386
($ImageWidth, $ImageHeight) = unpack("v2", substr($$DATA, 6, 4));
387
} elsif($$DATA =~ /^II\x2a\x00\x08\x00\x00\x00/ || $$DATA =~ /^MM\x00\x2a\x00\x00\x00\x08/) { # TIFF
389
} elsif($$DATA =~ /^BM/) { # BMP
391
} elsif($$DATA =~ /^\xFF\xD8\xFF/ || $$DATA =~ /JFIF/) { # JPEG
392
my $HeaderPoint = index($$DATA, "\xFF\xD8\xFF", 0);
393
my $Point = $HeaderPoint + 2;
394
while($Point < $DataLength) {
395
my($Maker, $MakerType, $MakerLength) = unpack("C2n",substr($$DATA,$Point,4));
396
if($Maker != 0xFF || $MakerType == 0xd9 || $MakerType == 0xda) {
398
} elsif($MakerType >= 0xC0 && $MakerType <= 0xC3) {
400
($ImageHeight, $ImageWidth) = unpack("n2", substr($$DATA, $Point + 5, 4));
401
if($HeaderPoint > 0) {
402
$$DATA = substr($$DATA, $HeaderPoint);
403
$DataLength = length($$DATA);
407
$Point += $MakerLength + 2;
414
if(!$Ext && ($UnknownType || $ContentType =~ /^audio\//i)) {
416
if($$DATA =~ /^MThd/) {
418
} elsif($$DATA =~ /^\x2esnd/) { # ULAW Audio
420
} elsif($$DATA =~ /^RIFF/ || $$DATA =~ /^ID3/ && $$DATA =~ /RIFF/) {
421
my $HeaderPoint = index($$DATA, "RIFF", 0);
422
$_ = substr($$DATA, $HeaderPoint + 8, 8);
425
if(unpack("V",substr($$DATA, $HeaderPoint + 16, 4)) == 16) {
427
} else { # RIFF WAVE MP3
430
} elsif(/^RMIDdata$/) { # RIFF MIDI
432
} elsif(/^RMP3data$/) { # RIFF MP3
435
if($ContentType =~ /^audio\//i) {
436
$Infomation .= '(RIFF '. substr($$DATA, $HeaderPoint + 8, 4). ')';
444
if($$DATA =~ /^\%PDF/) {
445
# Picture size is not measured.
447
} elsif($$DATA =~ /^FWS/) { # Shockwave Flash
449
} elsif($$DATA =~ /^RIFF/ || $$DATA =~ /^ID3/ && $$DATA =~ /RIFF/) {
450
my $HeaderPoint = index($$DATA, "RIFF", 0);
451
$_ = substr($$DATA,$HeaderPoint + 8, 8);
456
if($ContentType =~ /^video\//i) {
457
$Infomation .= '(RIFF '. substr($$DATA, $HeaderPoint + 8, 4). ')';
459
} elsif($$DATA =~ /^PK/) { # ZIP Compress File
461
} elsif($$DATA =~ /^MSCF/) { # CAB Compress File
463
} elsif($$DATA =~ /^Rar\!/) { # RAR Compress File
465
} elsif(substr($$DATA, 2, 5) =~ /^\-lh(\d+|d)\-$/) { # LHA Compress File
466
$Infomation .= "(lh$1)";
468
} 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") {
474
# Header analysis failure
476
# It will be followed if it applies for the MIME type from the browser.
477
foreach (keys %UPLOAD_CONTENT_TYPE_LIST) {
479
if($ContentType =~ /^$_$/i) {
480
$Ext = $UPLOAD_CONTENT_TYPE_LIST{$_};
481
$ContentName = &CheckContentExt($Ext);
505
$Infomation .= ' / Header analysis failure';
507
if($Ext ne $FileExt && &CheckContentExt($FileExt) eq $ContentName) {
513
# a MIME type is unknown--It judges from an extension.
515
$ContentName = &CheckContentExt($FileExt);
518
$Infomation .= ' / MIME type is unknown('. $ContentType. ')';
524
# $ContentName = &CheckContentExt($Ext) unless($ContentName);
525
# if($Ext && $ContentName) {
526
# $ContentName .= $Infomation;
530
# "$FileName A not corresponding extension ($Ext)<BR>The extension which can be responded ". join(',', sort values(%UPLOAD_EXT_LIST))
535
# if($Ext =~ /.?html?/ && $$DATA =~ /<\!/) {
544
# $$DATA =~ s/\#\s*$_/\&\#35\;$_/ig
551
int($DataLength / 1024 + 1),
558
##############################################################################
561
# Extension discernment
566
##############################################################################
573
foreach (keys %UPLOAD_EXT_LIST) {
575
if($_ && $Ext =~ /^$_$/) {
576
$ContentName = $UPLOAD_EXT_LIST{$_};
584
##############################################################################
592
##############################################################################
596
my($value,$Trans) = @_;
598
# my $FormCode = &jcode::getcode($value) || $FormCodeDefault;
599
# $FormCodeDefault ||= $FormCode;
601
# if($Trans && $TRANS_2BYTE_CODE) {
602
# if($FormCode ne 'euc') {
603
# &jcode::convert($value, 'euc', $FormCode);
607
# "\xA3\xB0-\xA3\xB9\xA3\xC1-\xA3\xDA\xA3\xE1-\xA3\xFA",
610
# if($CHARCODE ne 'euc') {
611
# &jcode::convert($value,$CHARCODE,'euc');
614
# if($CHARCODE ne $FormCode) {
615
# &jcode::convert($value,$CHARCODE,$FormCode);
618
# if($CHARCODE eq 'euc') {
619
# &jcode::h2z_euc($value);
620
# } elsif($CHARCODE eq 'sjis') {
621
# &jcode::h2z_sjis($value);
626
##############################################################################
634
##############################################################################
639
local($error_message) = $_[0];
640
local($error_message2) = $_[1];
642
print "Content-type: text/html\n\n";
646
<TITLE>Error Message</TITLE></HEAD>
648
<table border="1" cellspacing="10" cellpadding="10">
649
<TR bgcolor="#0000B0">
650
<TD bgcolor="#0000B0" NOWRAP><font size="-1" color="white"><B>Error Message</B></font></TD>
654
<H4> $error_message </H4>
660
&rm_tmp_uploaded_files; # Image Temporary deletion
664
##############################################################################
667
# Image Temporary deletion
672
##############################################################################
674
sub rm_tmp_uploaded_files
676
if($img_data_exists == 1){
678
foreach $fname_list(@NEWFNAMES) {
679
if(-e "$img_dir/$fname_list") {
680
unlink("$img_dir/$fname_list");