#!/usr/bin/perl #!/usr/bin/perl -w #use strict; #no strict 'vars'; #------------------------------- # count.cgi v2.1 (ShiftJIS) # 1999. 8. 3 MIYAMORI Yoshimasa # (kan_ta@amcsys.com) # (RXE02744@nifty.ne.jp) #------------------------------- # # fly: http://www.unimelb.edu.au/fly/ # # 使い方. # など. # 0 は データファイルの番号. 0〜9まで. # 5 は 表示桁数. 1〜8まで. $myFilePath = '../../../sample/counter/'; $flyProg = '../../../fly/fly'; $dstX = 12; # 出力する数字1つの大きさ. $dstY = 20; $fileMode = '666'; # ログのパーミッション. $logLimit = 2000; # ログの行数. $countLimit = 99999999; # カウントの最大値. $myEnv = 1; # 環境変数の出力. 0:無し. 1:一部. 2:すべて. &main; exit; sub main { local($num, $i); for ($i = 0; $i < 10; $i++) { $countFile[$i] = join('', $myFilePath, 'count', $i, '.dat'); $imgFile[$i] = join('', $myFilePath, $i, '.gif'); } $errImgFile = $imgFile[0]; $envFile = $myFilePath . 'count.env'; $flyFile = $myFilePath . 'fly.gif'; $lockFile0 = $myFilePath . 'count0.lck'; $lockFile1 = $myFilePath . 'count1.lck'; $tmpFile = $myFilePath . 'count.tmp'; $qry0 = 0; $qry1 = 0; if ($ENV{'REQUEST_METHOD'} ne 'HEAD') { &exitCgi if (length($ENV{'QUERY_STRING'}) < 2); $qry0 = substr($ENV{'QUERY_STRING'}, 0, 1); # データファイル. $qry1 = substr($ENV{'QUERY_STRING'}, 1, 1); # 表示桁数. &exitCgi if (($qry0 < 0) || ($#countFile < $qry0)); } open(LOCK0, ">$lockFile0") || &exitCgi; flock(LOCK0, 2); open(LOCK1, ">$lockFile1") || &exitCgi; flock(LOCK1, 2); if ($ENV{'REQUEST_METHOD'} ne 'HEAD') { $num = &getCount; &putEnv; &putCount($num); &dispCount(1); } else { &dispCount(0); } flock(LOCK1, 8); close LOCK1; flock(LOCK0, 8); close LOCK0; chmod oct($fileMode), $countFile[$qry0]; chmod oct($fileMode), $envFile; chmod oct($fileMode), $flyFile; chmod oct($fileMode), $lockFile0; chmod oct($fileMode), $lockFile1; } sub getCount { local($num); if (-f $countFile[$qry0]) { open(FILE, "<$countFile[$qry0]") || &exitCgi; $num = ; close FILE; } else { $num = 1; } $num; } sub putCount { local($num) = @_; local($s, $n, $m, $x1, $x2); open(FLY,"| $flyProg -q -o $flyFile") || &exitCgi; print FLY 'new', "\n"; print FLY 'size ', $dstX * $qry1, ',', $dstY, "\n"; $s = sprintf("%0${qry1}d", $num); $n = 0; while ($n < $qry1) { $x1 = $n * $dstX; $x2 = $x1 + $dstX + 1; $m = substr($s, $n, 1); print FLY "copyresized -1,-1,-1,-1,$x1,0,$x2,$dstY,$imgFile[$m]\n"; $n++; } close FLY; $num++; $num = 0 if ($num > $countLimit); open(FILE, ">$countFile[$qry0]") || &exitCgi; print FILE $num; close FILE; } sub dispCount { local($n) = @_; if ($n) { print 'Content-length: ', (stat($flyFile))[7], "\n"; print 'Content-type: image/gif', "\n", "\n"; open(FLY, "<$flyFile") || &exitCgi; print ; close FLY; } else { print 'Last-Modified: ', &getDate((stat($flyFile))[9], 0), "\n"; print 'Content-length: ', (stat($flyFile))[7], "\n"; print 'Content-type: image/gif', "\n", "\n"; } } # 1: Tue Aug 3 00:00:00 JST 1999 # 0: Tue, 03 Aug 1999 00:00:00 GMT sub getDate { local($gmt, $n) = @_; local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst); if ($n) { $gmt += 9 * 3600; } ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($gmt); $hour = '0' . $hour if ($hour < 10); $min = '0' . $min if ($min < 10); $sec = '0' . $sec if ($sec < 10); $mon = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$mon]; $wday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday]; $year += 1900; if ($n) { $mday = ' ' . $mday if ($mday < 10); join('',$wday,' ',$mon,' ',$mday,' ',$hour,':',$min,':',$sec,' JST ',$year); } else { $mday = '0' . $mday if ($mday < 10); join('',$wday,', ',$mday,' ',$mon,' ',$year,' ',$hour,':',$min,':',$sec,' GMT'); } } sub getEnv { local($key, $text); $text = join('', $num, "\n", &getDate(time, 1), "\n"); if ($myEnv == 1) { $text = join('', $text, 'HTTP_REFERER', ' = ', $ENV{'HTTP_REFERER'}, "\n"); $text = join('', $text, 'HTTP_USER_AGENT', ' = ', $ENV{'HTTP_USER_AGENT'}, "\n"); $text = join('', $text, 'QUERY_STRING', ' = ', $ENV{'QUERY_STRING'}, "\n"); $text = join('', $text, 'REMOTE_ADDR', ' = ', $ENV{'REMOTE_ADDR'}, "\n"); $text = join('', $text, 'REMOTE_HOST', ' = ', $ENV{'REMOTE_HOST'}, "\n"); } else { foreach $key (sort(keys %ENV)) { $text = join('', $text, $key, ' = ', $ENV{$key}, "\n"); } } join('', $text, "\n"); } sub putEnv { local($text, $i); return unless ($myEnv); $text = &getEnv; open(TMP, ">$tmpFile") || &exitCgi; if (-f $envFile) { open(FILE, "<$envFile") || &exitCgi; print TMP $text; $i = 0; while () { print TMP $_; $i++; last if ($i > $logLimit); } close FILE; } else { print TMP $text; } close TMP; rename($tmpFile, $envFile); } sub exitCgi { print 'Content-length: ', (stat($errImgFile))[7], "\n"; print 'Content-type: image/gif', "\n", "\n"; open(ERRIMG, "<$errImgFile"); print ; close ERRIMG; exit; }