#!/usr/bin/perl ############################################### # vote5.cgi # V2.0 (2005.7.12) # Copyright(C) CGI-design ############################################### require './cgi-lib.pl'; $script = 'vote5.cgi'; $base = './votedata'; #データ格納ディレクトリ $entfile = "$base/ent.txt"; #登録データ $nofile = "$base/no.txt"; #登録番号 $opfile = "$base/option.txt"; #オプション $lockfile = "$base/lock"; #ロック $cgi_lib'maxdata = 200000; #入力最大容量(byte) open (IN,"$opfile") || &error("OPEN ERROR"); $opdata = ; close IN; if (!$opdata) { $pass = &crypt('cgi'); chmod(0666,$opfile); open (OUT,">$opfile") || &error("OPEN ERROR"); print OUT "$pass<>画像投票<><><>$base<>$base<><><><>#fafaf5<>#000000<>#5A6D45<>#c00000<>#000000<>#5A6D45<>#CAD8CB<>140<>140<>4"; close OUT; chmod(0666,$entfile); chmod(0666,$nofile); } ### メイン処理 ### &ReadParse; $mode = $in{'mode'}; open (IN,"$opfile") || &error("OPEN ERROR"); ($pass,$title,$com_adm,$home,$savedir,$loaddir,$bg_img,$home_icon,$title_icon,$bg_color,$text_color,$title_color,$rank_color,$cnt_color,$sub_color,$ent_color,$max_w,$max_h,$imgcols) = split(/<>/,); close IN; if ($mode eq 'admin') {&admin;} else {&main;} print "\n"; exit; ### sub header { print "Content-type: text/html\n\n"; print "\n"; print "$title\n"; $head = 1; } ### sub main { if ($mode eq 'cnt') {&cnt;} &header; print "
\n"; print "
"; if ($home) {if ($home_icon) {print "";} else {print "[HOME]";}} print ""; if ($title_icon) {print "";} else {print "$title";} print "
\n"; print "
$com_adm
\n"; &ranking; print "
[編集]
\n"; # 次の行は著作権表示ですので削除しないで下さい。# print "CGI-design\n"; } ### sub cnt { $addr = $ENV{'REMOTE_ADDR'}; $cntfile = "$base/$in{'no'}.txt"; &lock; open (IN,"$cntfile") || &error("OPEN ERROR"); ($cnt,$paddr) = split(/<>/,); close IN; if ($addr ne $paddr) { $cnt++; open (OUT,">$cntfile") || &error("OPEN ERROR"); print OUT "$cnt<>$addr"; close OUT; } &unlock; } ### sub ranking { open (IN,"$entfile") || &error("OPEN ERROR"); while() { ($no) = split(/<>/); $data{$no} = $_; open (INC,"$base/$no.txt") || &error("OPEN ERROR"); ($rank{$no}) = split(/<>/,); close INC; } close IN; if ($imgcols == 0) {$imgcols = 4;} $perw = int(100 / $imgcols); $k=$n=$pcnt=0; print "\n"; foreach (sort{$rank{$b} <=> $rank{$a}} keys(%rank)) { $cnt = $rank{$_}; ($no,$img_type,$img_w,$img_h,$img_big,$sub) = split(/<>/,$data{$_}); if (!$k) {print "\n";} if ($cnt != $pcnt) {$rank = $n + 1; $pcnt = $cnt;} if ($mode eq 'admin' && $in{$no}) {print "\n"; $n++; $k++; if ($k == $imgcols) {print "\n"; $k = 0;} } if ($k) { for ($k+1 .. $imgcols) {print "";} print ""; } print "
";} else {print "";} print "\n"; if ($mode eq 'admin') {print "\n";} print "\n"; if ($mode ne 'admin') { print "\n"; } print "
$rank位 ($cnt票)
\n"; $imgfile = "$loaddir/$no.$img_type"; $imgsrc = ""; if ($img_big) {print "$imgsrc";} else {print $imgsrc;} print "
$sub
\n"; print "\n"; print "\n"; print "
\n"; } ### sub admin { &header; print "
\n"; $inpass = $in{'pass'}; if ($inpass eq '') { print "
[Return]
\n"; print "



パスワードを入力して下さい

\n"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
\n"; exit; } $mat = &decrypt($inpass,$pass); if (!$mat) {&error("パスワードが違います");} print "\n"; print "
 Return
\n"; print "\n"; print "\n"; print "\n"; print "

\n"; $wrt = $in{'wrt'}; if ($in{'set'}) {&setup;} else {&edtin;} } ### sub edtin { if ($in{'newwrt'}) {&newwrt;} elsif ($in{'edtwrt'}) {&edtwrt;} elsif ($in{'delwrt'}) {&delwrt;} &in_form; print "
\n"; print "\n"; print "\n"; print "\n"; print "
修正、削除する場合は[修正]をクリックして下さい。\n"; &ranking; print "
\n"; } ### sub in_form { print "
\n"; print "\n"; print "\n"; if ($in{'edt'}) { open (IN,"$entfile") || &error("OPEN ERROR"); while () { ($no,$img_type,$img_w,$img_h,$img_big,$sub) = split(/<>/); if ($in{$no}) {last;} } close IN; print "\n"; } else {$sub = '';} print "
\n"; print "\n"; print "\n"; print "
題名 
画像
"; if ($in{'edt'}) { print "\n"; print "
\n"; } else {print "";} print "
\n"; } ### sub newwrt { if (!$in{'img'}) {&error("画像を入力して下さい");} &lock; open (IN,"$nofile") || &error("OPEN ERROR"); $no = ; close IN; $no++; open (OUT,">$nofile") || &error("OPEN ERROR"); print OUT $no; close OUT; &img("$savedir/$no",'img'); open (OUT,">>$entfile") || &error("OPEN ERROR"); print OUT "$no<>$type<>$width<>$height<>$big<>$in{'sub'}<>\n"; close OUT; $cntfile = "$base/$no.txt"; open (OUT,">$cntfile") || &error("OPEN ERROR"); print OUT "0<>"; close OUT; chmod(0666,$cntfile); &unlock; } ### sub edtwrt { &img("$savedir/$in{'no'}",'img'); &lock; @new = (); open (IN,"$entfile") || &error("OPEN ERROR"); while () { ($no,$img_type,$img_w,$img_h,$img_big) = split(/<>/); if ($no eq $in{'no'}) { if ($type) {$img_type = $type; $img_w = $width; $img_h = $height; $img_big = $big;} push(@new,"$no<>$img_type<>$img_w<>$img_h<>$img_big<>$in{'sub'}<>\n"); } else {push(@new,$_);} } close IN; open (OUT,">$entfile") || &error("OPEN ERROR"); print OUT @new; close OUT; &unlock; } ### sub delwrt { &lock; @new = (); open (IN,"$entfile") || &error("OPEN ERROR"); while () { ($no,$img_type) = split(/<>/); if ($no eq $in{'no'}) { unlink "$savedir/$no.$img_type"; unlink "$base/$no.txt"; } else {push(@new,$_);} } close IN; open (OUT,">$entfile") || &error("OPEN ERROR"); print OUT @new; close OUT; &unlock; } ### sub setup { if ($wrt) { if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});} $title = $in{'title'}; $com_adm = $in{'com_adm'}; $com_adm =~ s/\r\n|\r|\n/
/g; $home = $in{'home'}; $savedir = $in{'savedir'}; $loaddir = $in{'loaddir'}; $icon = $in{'icon'}; if ($icon) { &img("$savedir/$icon",'img'); if ($type) {$icontype = "$icon.$type";} else {$icontype = '';} if ($icon eq 'wall') {$bg_img = $icontype;} elsif ($icon eq 'home') {$home_icon = $icontype;} elsif ($icon eq 'title') {$title_icon = $icontype;} } $bg_color = $in{'color0'}; $text_color = $in{'color1'}; $title_color = $in{'color2'}; $rank_color = $in{'color3'}; $cnt_color = $in{'color4'}; $sub_color = $in{'color5'}; $ent_color = $in{'color6'}; $max_w = $in{'max_w'}; $max_h = $in{'max_h'}; $imgcols = $in{'imgcols'}; open (OUT,">$opfile") || &error("OPEN ERROR"); print OUT "$pass<>$title<>$com_adm<>$home<>$savedir<>$loaddir<>$bg_img<>$home_icon<>$title_icon<>$bg_color<>$text_color<>$title_color<>$rank_color<>$cnt_color<>$sub_color<>$ent_color<>$max_w<>$max_h<>$imgcols"; close OUT; } print "下記に入力後、「設定する」を押して下さい。\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print "

\n"; print "
\n"; print "\n"; $com_adm =~ s/
/\r/g; print "\n"; print "\n"; print "\n"; print "\n"; @name = ('壁紙','ホーム','タイトル'); @data = ($bg_img,$home_icon,$title_icon); @icon = ('wall','home','title'); print "\n"; print "\n"; @name = ('基本背景色','基本文字色','タイトル色','順位色','得票色','題名色','画像背景色'); @data = ($bg_color,$text_color,$title_color,$rank_color,$cnt_color,$sub_color,$ent_color); for (0 .. $#name) { print "\n"; } print "\n"; print "\n"; print "
タイトル

コメント
ホームURL
画像格納ディレクトリ
画像読出ディレクトリ

壁紙、アイコン
\n"; for (0 .. $#name) { print "$name[$_]"; if ($data[$_]) { if ($_) {$w = '';} else {$w = ' width=30';} print "   "; } else {print "(無)  ";} } print "
カラーコード
$name[$_]\n"; print "\n"; print "
画像表\示横maxpx  縦maxpx\n"; print "  
パスワード変更 (英数8文字以内)
\n"; } ### sub img { $type=$width=$height=$big=$mac=''; $imgdata = $in{"$_[1]"}; if (!$imgdata) {return;} foreach (@in) { if (/$_[1]/ and /Content-Type:(.+)/i) { if ($1 =~ /image\/.*jpeg/i) {$type = 'jpg';} elsif ($1 =~ /image\/gif/i) {$type = 'gif';} elsif ($1 =~ /image\/.*png/i) {$type = 'png';} } if (/application\/x-macbinary/i) {$mac = 1;} } if (!$type) {&error("このファイルはアップロードできません");} if ($mac) { $leng = substr($imgdata,83,4); $leng = unpack("%N",$leng); $imgdata = substr($imgdata,128,$leng); } $img_file = "$_[0].$type"; open (IMG,">$img_file") || &error("$img_fileファイルを作成できません"); binmode IMG; print IMG $imgdata; close IMG; chmod (0666,$img_file); ($t,$width,$height) = &getImageSize("$img_file"); if (!$width || !$height) {&error("ファイルを認識できません");} $big = 0; if ($max_w && $max_w < $width) {$rate_w = $max_w / $width; $big = 1;} else {$rate_w = 1;} if ($max_h && $max_h < $height) {$rate_h = $max_h / $height; $big = 1;} else {$rate_h = 1;} if ($big) { if ($rate_w < $rate_h) {$rate = $rate_w;} else {$rate = $rate_h;} $width = int($width * $rate); $height = int($height * $rate); } } #========================================= # Get Image Pixel Size.(出典:stdio-902) #========================================= sub getImageSize { local($file_name) = @_; local($head); return if (!open IMG, $file_name); binmode IMG; read IMG, $head, 8; if ($head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a") { local($width, $height); if (read(IMG, $head, 4) != 4 || read(IMG, $head, 4) != 4 || $head ne 'IHDR') { close IMG; return "PNG", 0; } read IMG, $head, 8; close IMG; $width = unpack "N", substr($head, 0, 4); $height = unpack "N", substr($head, 4, 4); return "PNG", $width, $height; } $head = substr $head, 0, 3; if ($head eq "\x47\x49\x46") { local($head, $width, $height); seek IMG, 6, 0; read IMG, $head, 4; close IMG; ($width, $height) = unpack "vv", $head; return "GIF", $width, $height; } $head = substr $head, 0, 2; if ($head eq "\xff\xd8") { local($head, $width, $height, $w1, $w2, $h1, $h2, $l1, $l2, $length); seek IMG, 2, 0; while (read IMG, $head, 1) { last if ($head eq ""); if ($head eq "\xff") { $head = getc IMG; if ($head =~ /^[\xc0-\xc3\xc5-\xcf]$/) { seek IMG, 3, 1; last if (read(IMG, $head, 4) != 4); close IMG; ($h1, $h2, $w1, $w2) = unpack "C4", $head; $height = $h1 * 256 + $h2; $width = $w1 * 256 + $w2; return "JPG", $width, $height; } elsif ($head eq "\xd9" || $head eq "\xda") { last; } else { last if (read(IMG, $head, 2) != 2); ($l1, $l2) = unpack "CC", $head; $length = $l1 * 256 + $l2; seek IMG, $length - 2, 1; } } } close IMG; return "JPG", 0; } return 0; } ### sub lock { $retry = 3; if (-e $lockfile) { $locktime = (stat($lockfile))[9]; if ($locktime < time - 60) {&unlock;} } while (!mkdir($lockfile, 0755)) { if (--$retry < 0) {&error("busy!");} sleep(1); } } ### sub unlock {rmdir($lockfile);} ### sub crypt { @salt = ('a' .. 'z','A' .. 'Z','0' .. '9'); srand; $salt = "$salt[int(rand($#salt))]$salt[int(rand($#salt))]"; return crypt($_[0],$salt); } ### sub decrypt { $salt = $_[1] =~ /^\$1\$(.*)\$/ && $1 || substr($_[1],0,2); if (crypt($_[0],$salt) eq $_[1] || crypt($_[0],'$1$' . $salt) eq $_[1]) {return 1;} return 0; } ### sub error { if (!$head) {&header; print "
\n";} print "



ERROR !!

$_[0]\n"; print "
\n"; exit; }