############################################### Sub: Write Image File-Data
sub write_data
{
($loop, $ext, $cnt, $record, $file) = @_;
$photo_dir=$datapath.lc("/docs/templates/$sitebase/i");;
$count=''; $cnt>1?($count=$cnt-1):(); #used to increment for mutliple photos
$ext=lc($ext);
$Form{'image'}?():($Form{'image'}=$file);
if($Form{'mainbase'})
{
$photo_dir=$datapath.lc"/i/$Form{'mainbase'}/$Form{'base'}/$Form{'group'}";
}
elsif($Form{'destiny'})
{
$photo_dir=$datapath.lc"/i/creator/$Form{'destiny'}/$Form{'group'}";
}
elsif($Form{'folder'})
{
$photo_dir=$datapath.lc"/$Form{'folder'}"; mkdir($photo_dir,0755);
if($Form{'cat'})
{
$Form{'cat'}=~ s/\s/\_/g;
$photo_dir=$photo_dir.lc"/$Form{'cat'}"; mkdir($photo_dir,0755);
}
if($Form{'subcat'})
{
$Form{'subcat'}=~ s/\s/\_/g;
$photo_dir=$photo_dir.lc"/$Form{'subcat'}"; mkdir($photo_dir,0755);
}
$file=lc($Form{'image'})."$count";
$product_image = $photo_dir.lc"/$file";
}
elsif($Form{'ImgID'})
{
$photo_dir=$Form{'photo_dir'};
$count and $file=lc($Form{'ImgID'})."$Form{'seperator'}$count";
!$count and $file=lc($Form{'ImgID'});
$Form{'ext'} and $ext=$Form{'ext'};
}
elsif($Form{'image'}){ $file=lc($Form{'image'})."$count"; }
$thumbs_dir = "$photo_dir/thumbs";
$file =~ s!^.*(\\|\/)!!;
$file=~ s/\s/\_/g;
io_data('>',$photo_dir.lc"/$file.$ext",1,$record);
#print"$Form{'resize'}==>>$photo_dir/$file.$ext";
if(-e $photo_dir.lc"/$file.$ext")
{
$success.= ": $file.$ext : ";
if($Form{'resize'})
{
($imgmaxheight,$imgmaxwidth)=split('x',$Form{'resize'});
thumbs_convert_images(0,lc"$file.$ext");
$success.= "Resized $Form{'resize'} ";
}
if($Form{'makethumb'}){ create_icon(lc"$file.$ext"); }
}
else{ $success.= "Upload Failed $file.$ext "; }
}
#-############################################## Sub: Oops!
sub oops
{
my @err = @_;
local $_;
if (ref $txt{'Error'})
{
$txt{'Error'} = 'Error';
@err = split /\:/, $_[0];
shift @err;
@err = split /\
/, $err[0];
pop @err if $#err > 0;
print <
$txt{'Error'}
@err
$!
YOUR SET UP IS NOT CORRECT
Click
InterWer Script-Forums
if you need support
EO_HTML
}
else
{
@err = ((caller(2))[3]) unless $err[0];
print <
$txt{'Error'} @err
$txt{'Please, contact the'}
$txt{'Webmaster Administrator'}
$txt{'if you believe this to be a server problem'}.
EO_HTML
}
exit;
}
#-############################################## Sub: Check File-Data
sub check_data
{
my ($val, $ext) = @_;
$ext =~ /gif|jpg|bmp|png/i && length $$val > $maximagesize && return $success="Error - File exceeds maximum: @{[$maximagesize/1000]} Kb.
";
$ext =~ /flv/i && length($$val)/1000 > 40000 && return $success="Error - ".(length($$val)/1000)." File exceeds maximum: 40meg.
";
$ext =~ /gif|jpg|bmp|swf|pdf|doc|mp3/i && length($$val)/1000 > 20000 && return $success="Error - File exceeds maximum: 20meg..
";
$$val =~ /<[\s\/]*script\b[^>]*>/i && return q|Content not supported|;
return undef;
}
#-############################################## Sub: Read, Store and Write incoming Data
sub get_data
{
my $val = shift;
my $cnt = 1;
my ($file, $ext, $err, @val);
!$maximagesize?($maximagesize= 500000):(); #used here to set defaults
$success = '';
local $_;
$$val =~ s/Content\-Disposition\:\sform\-data\;\s//g;
$$val =~ s/^(.+)\r\n//;
@val = split /$1/, $$val;
pop @val;
@pairs=@val;
undef $$val;
undef $val;
while (defined($val = shift @val))
{
$val =~ /filename\=\"\"/oi && next;
$val !~ /filename/omi && do
{
$val =~ s/[\r\n]//omg;
$val =~ s/name\=\"(\w+)\"//oi;
($Form{$1} = $val) =~ s/\+/ /og;
$Form{$1} =~ s/\'/`/gi; #added to avoid apostrophy from crashing program.20080913
#print"$1 = $Form{$1}
\n";
if($val){ push(@Field_Order,$1); } #entered for all-auz-lib.pl send_mail
next;
};
($val =~ s/filename\=\"(.+)\"//oi) && do
{
$file = $1;
my (@imgname) = (split /\./o, $file); ##use this type of split in case source folder has dot included.
$val =~ s/name\=\"\w+\W*\w*\"\;|Content\-Type\:\s\w+\/\w+\W*\w*\s//oig;
($val = $') =~ s/^\r\n//og;
defined($err = check_data(\$val, $imgname[$#imgname]))
? do
{
($val = $file) =~ s/\\/\//og;
@val = split /\//, $val;
$val = pop @val;
undef @val;
$err = "$val\n$err";
last;
}
: do
{
write_data(1, $imgname[$#imgname], $cnt, \$val, $imgname[0]);
$cnt++;
};
};
}
undef $val;
$err;
}
############################################### Sub: Read Image-Data
sub read_img_data
{
my $file = shift;
my ($data, $ext);
local $_;
$ext = (split /\./, $file)[-1];
$data = io_data('<',"$img_dir/$file",1,undef );
return ($data, $ext);
}
#-############################################## Sub: Make Image-Size
sub img_size
{
my ($img_file, $max_xy, ) = @_;
my ($img_val, $ext, $x_size, $y_size, $rel);
($img_val, $ext) = read_img_data($img_file);
lc $ext eq 'gif'
? (($x_size, $y_size) = gif_size($img_val))
: (($x_size, $y_size) = jpg_size($img_val));
(defined $x_size and defined $y_size)
? $rel = $x_size / $y_size
: oops("Image.pm 3 : unable to read the image file $img_file");
for (0 .. 1)
{ # Flip-Flop switches are our friends :-)
if ($x_size > $max_xy)
{
$x_size = $max_xy;
$y_size = int($x_size / $rel);
}
if ($y_size > $max_xy)
{
$y_size = $max_xy;
$x_size = int($y_size * $rel);
}
}
return qq| width="$x_size" border=0|;
}
#-############################################## Sub: Get GIF-Size
sub gif_size
{
my $imgdata = shift;
my ($cmapsize, $buf, $h, $w, $x, $y, $type, $data,$read_in, $gif_blockskip, $last_pos);
$read_in = sub
{
my $buf = shift;
my ($length, $offset) = @_;
if (defined($offset) and ($offset != $last_pos))
{
$last_pos = $offset;
return '' if $last_pos > length($$imgdata);
}
$last_pos = 0 unless $last_pos;
$data = substr($$buf, $last_pos, $length);
$last_pos += length($data);
$data;
};
$gif_blockskip = sub
{
my ($skip, $type) = @_;
my $lbuf;
&$read_in($imgdata, $skip);
while (1) {
$lbuf = &$read_in($imgdata, 1); # Block size
last if ord($lbuf) == 0; # Block terminator
&$read_in($imgdata, ord($lbuf)); # Skip data
}
};
$type = &$read_in($imgdata, 6);
return (undef, undef, "Invalid/Corrupted GIF (bad header)")
if length($buf = &$read_in($imgdata, 7)) != 7;
($x) = unpack("x4 C", $buf);
if ($x & 0x80) {
$cmapsize = 3 * (2**(($x & 0x07) + 1));
return (undef, undef, "Invalid/Corrupted GIF (global color map too small?)")
unless &$read_in($imgdata, $cmapsize);
}
FINDIMG:
while (1) {
$buf = &$read_in($imgdata, 1);
($x) = unpack("C", $buf);
if ($x == 0x2c) { # Image Descriptor (GIF87a, GIF89a 20.c.i)
return (undef, undef, "Invalid/Corrupted GIF (missing image header?)")
if length($buf = &$read_in($imgdata, 8)) != 8;
($x, $w, $y, $h) = unpack("x4 C4", $buf);
$x += $w * 256;
$y += $h * 256;
return ($x, $y, 'GIF');
}
if ($x == 0x21) { # Extension Introducer (GIF89a 23.c.i, could also be in GIF87a)
$buf = &$read_in($imgdata, 1);
($x) = unpack("C", $buf);
if ($x == 0xF9) { # Graphic Control Extension (GIF89a 23.c.ii)
&$read_in($imgdata, 6); # Skip it
next FINDIMG;
}
elsif ($x == 0xFE) { # Comment Extension (GIF89a 24.c.ii)
&$gif_blockskip(0, "Comment");
next FINDIMG;
}
elsif ($x == 0x01) { # Plain Text Label (GIF89a 25.c.ii)
&$gif_blockskip(13, "text data");
next FINDIMG;
}
elsif ($x == 0xFF) { # Application Extension Label (GIF89a 26.c.ii)
&$gif_blockskip(12, "application data");
next FINDIMG;
}
else { return (undef, undef, sprintf("Invalid/Corrupted GIF (Unknown " . "extension %#x)", $x)) }
}
else { return (undef, undef, sprintf("Invalid/Corrupted GIF (Unknown code %#x)", $x)) }
}
}
#-############################################## Sub: Get JPEG-Size
sub jpg_size
{
my $imgdata = shift;
my $MARKER = "\xFF";
my $SIZE_FIRST = 0xC0;
my $SIZE_LAST = 0xC3;
my ($x, $y, $item) = (undef, undef, "could not determine JPEG size");
my ($marker, $code, $length, $last_pos, $data, $segheader, $read_data);
$read_data = sub {
my $buf = shift;
my ($length, $offset) = @_;
my $data;
if (defined($offset) && ($offset != $last_pos)) {
$last_pos = $offset;
return '' if $last_pos > length($$imgdata);
}
$last_pos = 0 unless $last_pos;
$data = substr($$buf, $last_pos, $length);
$last_pos += length($data);
$data;
};
&$read_data($imgdata, 2);
while (1) {
$length = 4;
$segheader = &$read_data($imgdata, $length);
($marker, $code, $length) = unpack("a a n", $segheader);
if ($marker ne $MARKER) {
$id = "JPEG marker not found";
last;
}
elsif (ord($code) >= $SIZE_FIRST and ord($code) <= $SIZE_LAST) {
$length = 5;
($y, $x) = unpack("xnn", &$read_data($imgdata, $length));
$id = 'JPG';
last;
}
else { &$read_data($imgdata, ($length - 2)) }
}
return ($x, $y, $id);
}
############################################### Sub: Read or Write File-Data
sub io_data
{
my ($option, $file_path, $bin_flag, $record) = @_;
my $data = "";
my $err = "";
my @data = ('ERROR');
local ($_, $|);
open IO_FILE, "$option $file_path" or return \@data;
$file_lock && do{ $option eq '<' ? (flock IO_FILE, 1) : (flock IO_FILE, 2) };
$| == 0 && ($| = 1);
$record ? do
{
$bin_flag ? do { binmode IO_FILE; $err = print IO_FILE $$record; } : ($err = print IO_FILE "$$record\n");
$err == 1 && (@data = ('1'));
close IO_FILE;
return \@data;
}
: do
{
$bin_flag ? do
{
binmode IO_FILE;
$data .= $_ while defined($_ = );
close IO_FILE;
return \$data;
}
: do
{
@_ = ; close IO_FILE;
@data = map { chomp; $_ } @_;
return \@data;
};
};
}
#-############################################## Sub: Get Form Data
sub get_form_data
{
my ($buffer,@data);
@pairs = ();
local $_;
($ENV{'REQUEST_METHOD'} eq 'POST' and $ENV{'CONTENT_LENGTH'}) ?
do
{
binmode STDIN;
read STDIN, $buffer, $ENV{'CONTENT_LENGTH'};
#print qq|Content-type: text/html\n\n|;
#print $buffer."
\n";
$buffer =~ /Content-Disposition/i && do
{
$Form{'error'} = get_data(\$buffer);
undef $buffer;
};
}
: $ENV{'QUERY_STRING'} && ($buffer = $ENV{'QUERY_STRING'});
$buffer && do
{
@pairs=split('&',$buffer);
foreach (split /\&/o, $buffer)
{
s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
@data= split /\=/o, $_, 2;
$data[1] =~ s/\+/ /og;
$Form{$data[0]} = $data[1];
$Form{$data[0]}=~ s/\'/`/gi;
$Form{$data[0]}=~ s/\'/\&\#39\;/gi;
@data = ();
}
};
}
1;