#!/usr/local/bin/perl
###################################################################################
#
# <一覧処理>
#
###################################################################################

require "./setup.cgi";

$locationR = $base_cgi . 'regist.cgi';
$location  = $base_cgi . 'bbs.cgi';

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
@wday_array = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
@wday_array2 = ('日','月','火','水','木','金','土');
@mon_array = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$timeStamp = sprintf("%04d/%s/%01d %s %02d:%02d",$year +1900,$mon_array[$mon],$mday,$wday_array[$wday],$hour,$min);
$timeStamp2 = sprintf("%04d%02d%02d%02d%02d%02d",$year +1900,$mon +1,$mday,$hour,$min,$sec);
$time = time;

foreach (@category) {

	($catekey,$catename) = split(/:/,$_,2);
	$category{$catekey} = $catename;
}

$key = &decCookie('key'); # クッキーに記録したキーを取得
open(f,$login); # ファイルに記録したキーを取得
$login_key = <f>;
close(f);

if ($ENV{'PATH_INFO'} =~ /^\/(\d\d\d\d\d\d\d\d\d\d\d\d\d\d)$/) { $target = $1; &view_goods(); exit; }

($ret) = &ReadParse;

while (($key,$val) = each %in) {

	if ($key eq "upfile") { next; }
	if ($key eq "upfile_s") { next; }

	$val =~ s/\r\n/\n/g;
	$val =~ s/\r/\n/g;

	$val =~ s/\f//g;
	$val =~ s/\t//g;
	$val =~ s/\0//g;

	&jcode'h2z_sjis(\$val);
	&jcode'convert(\$val,'sjis');

	$in{$key} = $val;
}

&search();
&form();

#
### 個別表示
#

sub view_goods
{

if (!open(f,$data)) { &error("エラー","データがありません。"); }
while ($i=<f>) {

	if ($i =~ /^#/) { next; }

	@csv = &DecodeCSV($i);
	if ($target eq $csv[0]) { $hit = 1; last; }
}
close(f);

if (!$hit) { &error("エラー","記事がありません。"); }

if ($csv[0] =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/) { $y = $1; $m = $2 + 0; $d = $3 + 0; $hour = $4; $min = $5; }
$view{$csv[0]}{'time'} = "$y/$m/$d $hour:$min";
$view{$csv[0]}{'category'} = $csv[1];
$view{$csv[0]}{'subject'} = $csv[2];
$view{$csv[0]}{'writer'} = $csv[3];
$view{$csv[0]}{'body'} = $csv[4];
$view{$csv[0]}{'body2'} = $csv[5];
$view{$csv[0]}{'email'} = $csv[6];
$view{$csv[0]}{'url'} = $csv[7];
$view{$csv[0]}{'image'} = $csv[8];

$BODY = $view{$csv[0]}{'body'} . "<p>" . $view{$csv[0]}{'body2'};
$BODY =~ s/&#10;/\n/g;

(@images) = split(/\|/,$view{$csv[0]}{'image'});
foreach $i (@images) {

	if ($i =~ /(.+)\.s.+\((\d+)\,(\d+)\)/) { ($j,$k) = split(/\(/,$i,2); $img{$1} = "$j/$2/$3"; }
	elsif ($i =~ /(.+)\..+\((\d+)\,(\d+)\)/) {

		$img_big{$1} = "$2/$3";
		if (! $img{$1}) { ($j,$k) = split(/\(/,$i,2); $img{$1} = "$j/$2/$3"; }
	}
	elsif ($i =~ /(.+)\..+/) {

		if (! $img{$1}) { $img{$i} = $def_height; }
	}
}

print <<"EOF";
Content-type: text/html\n
<html>
<head>
<title>$title</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="imagetoolbar" content="no">
<meta name=keywords content="$view{$csv[0]}{'subject'}">

<SCRIPT language="JavaScript">
<!--
function ViewImage(imgsrc,yoko,tate)
{
wintop = 100;
winleft = 200;
imgtemp = "";
if (imgtemp!=imgsrc || window.win.closed) {

	imgtemp = imgsrc;
	win = window.open("","img","resizable=yes,width="+yoko+",height="+tate+",left="+winleft+",top="+wintop);
	writetext = "<html><head><title>画像</title></head>\\n";
	writetext += "<body style='margin:0;'onclick='window.close();' onblur='self.focus()'>\\n";
	writetext += "<img src='"+imgsrc+"' galleryimg='no' alt='クリックで閉じる'>";
	win.document.open();
	win.document.write(writetext);
	win.document.close();
	win.focus();
}
else { win.close(); imgtemp=""; } //参考 himajin.moo.jp
}
onunload = function() { if (window.win) win.close(); }

function kPcheck(e)
{
	if (navigator.appName == 'Netscape')
	chrCode=e.which
	else
	chrCode=event.keyCode
	if (chrCode==13) { return false; }
}
//-->
</SCRIPT>
</head>
$body
$TITLE

<table cellpadding=0 width=100% style="border:2px #AFAFBF dashed; background-color:#F2F2F3; padding:8; margin:0px 0px 0px 0px;"><tr>
<td><font size=+1><b>$view{$csv[0]}{'subject'}</b></font><br>
<font size=-1>投稿日時 $view{$csv[0]}{'time'} 
EOF

if ($view{$csv[0]}{'category'}) { print " <a href=\"$location\?category=$view{$csv[0]}{'category'}\">$category{$view{$csv[0]}{'category'}}</a> | "; }

if ($view{$csv[0]}{'email'} && $view{$csv[0]}{'writer'}) {

	($pre,$suf) = split(/\@/,$view{$csv[0]}{'email'},2);

print <<"EOF";
<Script language="JavaScript">
<!--
document.write('by <a href="mai' + 'lto:$pre&#64;$suf">$view{$csv[0]}{'writer'}</a> | ');
// -->
</Script>
EOF

}
elsif ($view{$csv[0]}{'email'}) {

	($pre,$suf) = split(/\@/,$view{$csv[0]}{'email'},2);

# メールアドレス自動収拾防止
print <<"EOF";
<Script language="JavaScript">
<!--
document.write('<a href="mai' + 'lto:$pre&#64;$suf">メール送信</a> | ');
// -->
</Script>
EOF

}
elsif ($view{$csv[0]}{'writer'}) { print "by $view{$csv[0]}{'writer'} | "; }

print <<"EOF";
<a href="$location/$csv[0]">固定リンク</a></font>
</td>
<td align=right><a href="$location">一覧表\示</a>
EOF

if ($key eq $login_key && $login_key ne '') { print " | <a href=\"$locationR\?edit=$csv[0]\">編集</a> | <a href=\"$locationR\?cmd=logout&redirect=$csv[0]\">ログアウト</a>"; }
else { print " | <a href=\"$locationR\?cmd=login&redirect=$csv[0]&mode=1\">ログイン</a>"; }

print <<"EOF";
</td>
</tr></table>

<table width=100% cellpadding=0 style="border-left:2px #AFAFBF dashed; border-right:2px #AFAFBF dashed; border-bottom:2px #AFAFBF dashed; background-color:#F2F2F3; padding:8; margin: 0px 0px 0px 0px;">
<tr><td>
<table cellpadding=10>
<tr><td vAlign=top colspan=2>
EOF

@images = (); $img = "";
if ($view{$csv[0]}{'image'}) {

	$add_width = 0;
	foreach $key (sort keys %img) {

		if ($add_width == 0) { print "<table width=100% cellpadding=5><tr>\n"; }

		if ($img{$key} =~ /(.+)\/(\d+)\/(\d+)/) {

			$fn = $fn2 = $1; $width = $2; $height = $3;
			$fn =~ s/\.s//;
			($dir,$etc) = split(/\_/,$fn,2);

			if ($img_big{$key} =~ /(\d+)\/(\d+)/) {

				$img = "<img src=\"$img_url$dir/$fn2\" width=\"$width\" height=\"$height\" alt=\"クリックで表\示\" border=0 onClick=\"ViewImage('$img_url$dir/$fn',$1,$2,event)\" style='cursor:pointer'>";
			}
			else { $img = "<a href=\"$img_url$dir/$fn\" target=_blank><img src=\"$img_url$dir/$fn2\" width=\"$width\" height=\"$height\" alt=\"\" border=0></a>"; }

			$add_width += $width;
		}
		else {
			($dir,$etc) = split(/\_/,$key,2);

			$height = $img{$key};
			$img = "<a href=\"$img_url$dir/$key\" target=_blank><img src=\"$img_url$dir/$key\" height=\"$height\" alt=\"\" border=0></a>";
			$add_width += 300;
		}

		print "<td vAlign=top>$img</td>\n";

		if ($max_width <= $add_width) { $add_width = 0; print "</tr></table><p>\n"; }
	}

	if ($add_width != 0) { print "</tr></table>\n"; }
}
else { print "</td>\n"; }

print <<"EOF";
</td>
<td vAlign=top><tt>
$BODY
</tt>
<p>
EOF

if ($comment_mode) {

	if ($comment_list_mode) { print "<a name=comment></a>\n"; }

print <<"EOF";
<form method="post" action="$locationR" name="comForm">
<input type=hidden name="cmd" value="comment">
<input type=hidden name="base" value="$target">

<table style="border-left:3px #AFAFBF solid;" cellspacing=10 cellpadding=0>
EOF

	if ($comment_list_mode) {

print <<"EOF";
<tr>
<td noWrap align=right><tt>$comment_type</tt></td>
<td><textarea name="comment" cols=40 rows=3 wrap=soft tabindex=1></textarea></td>
</tr>
<tr>
<td noWrap align=right><tt>お名前</tt></td>
<td><input type="text" name="uname" size=40 maxlength=255 onKeypress="return kPcheck(event)" tabindex=2></td>
</tr>
<tr>
<td noWrap align=right><tt>URL</tt></td>
<td><input type="text" name="url" value="http://" size=60 maxlength=255 onKeypress="return kPcheck(event)" tabindex=3 style="ime-mode:disabled"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="投稿" tabindex=4 onClick="f=confirm('管理人のチェック後に掲載されます。\\nよろしいですか？');return f">
</form></td>
</tr>
EOF
	}

print <<"EOF";
<tr><td colspan=2>
EOF
	if (open(f,"$img_dir$target\/comment.cgi")) {

		@comments = <f>;
		close(f);
	}

	if ($comment_list_mode) { @comments = reverse @comments; }

	foreach $i (@comments) {

		@csv = &DecodeCSV($i);
		$csv[3] =~ s/ /&nbsp;/g;
		$csv[3] =~ s/&#10;/<br>\n/g;

		if ($csv[5]) { $uname = "<a href=\"$csv[5]\" target=_blank>$csv[4]</a>"; }
		else { $uname = $csv[4]; }

		if (!$csv[1] && $key eq $login_key && $login_key ne '') { # 検閲

			print "<a name=$csv[0]></a><hr size=1><font size=-1 color=red>$csv[2] by $uname &nbsp;[<a href=\"$locationR\?cmd=check&cmd2=ok&base=$target&target=$csv[0]\"><b>掲載</b></a>] [<a href=\"$locationR\?cmd=check&cmd2=ng&base=$target&target=$csv[0]\" onClick=\"f=confirm('削除しますか？');return f\">削除</a>]</font><br>\n";
			print "<font color=red><tt>$csv[3]</tt></font><p>\n";
		}
		elsif ($csv[1] && $key eq $login_key && $login_key ne '') { # 検閲済みの削除

			print "<a name=$csv[0]></a><hr size=1><font size=-1>$csv[2] by $uname &nbsp;[<a href=\"$locationR\?cmd=check&cmd2=ng&base=$target&target=$csv[0]\" onClick=\"f=confirm('削除しますか？');return f\">削除</a>]</font><br>\n";
			print "<tt>$csv[3]</tt><p>\n";
		}
		elsif ($csv[1]) {

			print "<a name=$csv[0]></a><hr size=1><font size=-1>$csv[2] by $uname</font><br>\n";
			print "<tt>$csv[3]</tt><p>\n";
		}
	}

print <<"EOF";
</td>
</tr>
EOF
	if (!$comment_list_mode) {

print <<"EOF";
<tr>
<td noWrap align=right><tt>$comment_type</tt></td>
<td><textarea name="comment" cols=40 rows=3 wrap=soft tabindex=1></textarea></td>
</tr>
<tr>
<td noWrap align=right><tt>お名前</tt></td>
<td><input type="text" name="uname" size=40 maxlength=255 onKeypress="return kPcheck(event)" tabindex=2></td>
</tr>
<tr>
<td noWrap align=right><tt>URL</tt></td>
<td><input type="text" name="url" value="http://" size=60 maxlength=255 onKeypress="return kPcheck(event)" tabindex=3 style="ime-mode:disabled"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="投稿" tabindex=4 onClick="f=confirm('管理人のチェック後に掲載されます。\\nよろしいですか？');return f">
</form></td>
</tr>
EOF
	}

	print "</table>\n";
}

print <<"EOF";
</td></tr>
</table>
</td></tr></table>
EOF
	if (!$comment_list_mode) { print "<a name=comment></a>\n"; }

print <<"EOF";
$TITLE2
</body>
</html>
EOF

exit;

}

#
### 検索処理と抽出処理
#

sub search
{

if ($in{'search'} ne "") {

	$pattern = $in{'search'};
	&jcode'convert(\$pattern,'euc');
}

if ($in{'vp'} eq "") { $FF = 1; $in{'vp'} = 1; } else { $FF = ($in{'vp'} - 1) * $page + 1; }

$next_data = 0;
$c = $n = 0;
$allhits = 0;

if (!open(f,$data)) { &error("エラー","商品データがありません。"); }
while ($i=<f>) {

	if ($in{'category'} ne "") {

		@csv = &DecodeCSV($i);
		if ($in{'category'} eq $csv[1]) {

			if ($in{'search'} ne "") {

				$str = "$csv[2] $csv[3] $csv[4] $csv[5] $csv[6]";
				&jcode'convert(\$str,'euc','sjis');

				if ($str =~ /^(?:[\x00-\x7F]|[\x8E\xA1-\xFE][\xA1-\xFE]|\x8F[\xA1-\xFE][\xA1-\xFE])*?(?:$pattern)/) { $allhits++; } # 参考 http://www.din.or.jp/~ohzaki/regex.htm
				else { next; }
			}
			else { $allhits++; }
		}
		else { next; }
	}
	else {
		if ($in{'search'} ne "") {

			@csv = &DecodeCSV($i);
			$str = "$csv[2] $csv[3] $csv[4] $csv[5] $csv[6]";
			&jcode'convert(\$str,'euc','sjis');

			if ($str =~ /^(?:[\x00-\x7F]|[\x8E\xA1-\xFE][\xA1-\xFE]|\x8F[\xA1-\xFE][\xA1-\xFE])*?(?:$pattern)/) { $allhits++; }
			else { next; }
		}
		else { $allhits++; }
	}

	$c++;
	if ($FF > $c) { next; }

	$n++;
	if ($n > $page) { last; }

	if (!$in{'category'}) { @csv = &DecodeCSV($i); }

	if ($csv[0] =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/) { $y = $1; $m = $2 + 0; $d = $3 + 0; $hour = $4; $min = $5; }
	$view{$csv[0]}{'time'} = "$y/$m/$d $hour:$min";
	$view{$csv[0]}{'category'} = $csv[1];
	$view{$csv[0]}{'subject'} = $csv[2];
	$view{$csv[0]}{'writer'} = $csv[3];
	$view{$csv[0]}{'body'} = $csv[4];
	$view{$csv[0]}{'body2'} = $csv[5];
	$view{$csv[0]}{'email'} = $csv[6];
	$view{$csv[0]}{'url'} = $csv[7];
	$view{$csv[0]}{'image'} = $csv[8];

	push(@number,$csv[0]);
}
close(f);

for ($jump = $page; $jump < $allhits + $page; $jump += $page) { $last_page++; }
$start = $in{'vp'} - 4;
if ($start < 1) { $start = 1; }
$end = $in{'vp'} + 4;
if ($end >= $last_page) { $end = $last_page; }
$ffv = ($last_page - 1) * $page;
$vp = 0;
foreach $vp ($start .. $end) { if ($vp == $in{'vp'}) { $page_now = $vp; last; } }
$FROM = $page_now * $page - ($page - 1);
$LAST = $FF + $page - 1;
if ($LAST > $allhits) { $LAST = $allhits; }

}

#
### 検索フォーム・一覧
#

sub form
{

$key = &decCookie('key'); # クッキーに記録したキーを取得
open(f,$login); # ファイルに記録したキーを取得
$login_key = <f>;
close(f);

if ($_[0]) { $body =~ s/<body/<body onLoad="alert('$_[0]')"/i; }

print <<"EOF";
Content-type: text/html\n
<html>
<head>
<title>$title</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="imagetoolbar" content="no">

<SCRIPT language="JavaScript">
<!--
function ViewImage(imgsrc,yoko,tate)
{
wintop = 100;
winleft = 200;
imgtemp = "";
if (imgtemp!=imgsrc || window.win.closed) {

	imgtemp = imgsrc;
	win = window.open("","img","resizable=yes,width="+yoko+",height="+tate+",left="+winleft+",top="+wintop);
	writetext = "<html><head><title>画像</title></head>\\n";
	writetext += "<body style='margin:0;'onclick='window.close();' onblur='self.focus()'>\\n";
	writetext += "<img src='"+imgsrc+"' galleryimg='no' alt='クリックで閉じる'>";
	win.document.open();
	win.document.write(writetext);
	win.document.close();
	win.focus();
}
else { win.close(); imgtemp=""; } //参考 himajin.moo.jp
}
onunload = function() { if (window.win) win.close(); }
//-->
</SCRIPT>

</head>
$body
EOF

if ($key eq $login_key && $login_key ne '') {

print <<"EOF";
<div align=right><a href="$locationR\?init">投稿</a> | <a href="$locationR\?logout">ログアウト</a>&nbsp;&nbsp;</div>
EOF

}
else {

print <<"EOF";
<div align=right><a href="$locationR\?redirect=list&mode=1&cmd=login">ログイン</a>&nbsp;&nbsp;</div>
EOF

}

print <<"EOF";
$TITLE

<table width=100%><tr><td>
<table cellpadding=2 cellspacing=0><tr>
EOF

if (!$in{'category'}) {

	if ($in{'search'} ne "") { print "<td bgcolor=\"$inset\" style=\"border-style: inset;\"><b><a href=\"$location\" title=\"検索状態の解除\">$group</a></b></td>\n"; }
	else { print "<td bgcolor=\"$inset\" style=\"border-style: inset;\"><b>$group</b></td>\n"; }
}
else { print "<td bgcolor=\"$outset\" style=\"border-style: outset;\"><font size=-1><a href=\"$location\">$group</a></font></td>\n"; }

foreach (@category) {

	($catekey,$catename) = split(/:/,$_,2);
	if ($in{'category'} eq $catekey) {

		if ($in{'search'} ne "") { print "<td bgcolor=\"$inset\" style=\"border-style: inset;\"><b><a href=\"$location\?category=$catekey\" title=\"検索状態の解除\">$catename</a></b></td>\n"; }
		else { print "<td bgcolor=\"$inset\" style=\"border-style: inset;\"><b>$catename</b></td>\n"; }
	}
	else { print "<td bgcolor=\"$outset\" style=\"border-style: outset;\"><font size=-1><a href=\"$location\?category=$catekey\">$catename</a></font></td>\n"; }
}

print <<"EOF";
</tr></table>
EOF

print <<"EOF";
</td>
<form method="post" action="$location">
<td align=right>
<input type="hidden" name="category" value="$in{'category'}">
<input type="text" name="search" size="12" value="$in{'search'}">
<input type="submit" value="単語検索">
</td>
</form>
EOF

print <<"EOF";
</tr></table>
<hr size=1><p>
EOF

foreach $i (@number) {

	if ($comment_mode) {

		@comments = (); $com = $com2 = $info = ""; $count = $not_yet = 0;

		if (open(f,"$img_dir$i\/comment.cgi")) {

			@comments = <f>;
			close(f);
		}

		foreach $j (@comments) {

			@csv = &DecodeCSV($j);
			if ($csv[1]) { $count++; }
		}

		if ($key eq $login_key && $login_key ne '') {

			$all = @comments;
			$not_yet = $all - $count;

			if ($not_yet > 0) { $info = " | <a href=\"$locationR\?cmd=login&mode=1&redirect=$i\"><b>確認待ち($not_yet)</b></a>"; }
			else { $info = " | <a href=\"$locationR\?cmd=login&mode=1&redirect=$i\">確認待ち(0)</a>"; }
		}

		if ($count > 0) { $com = " | <a href=\"$location\/$i#comment\"><b>$comment_type($count)</b></a>"; $com2 = "<a href=\"$location\/$i#comment\">$comment_typeを読む</a>"; }
		else { $com = " | <a href=\"$location\/$i#comment\">$comment_type(0)</a>"; $com2 = ""; }
	}

print <<"EOF";
<table cellpadding=0 width=100% style="border:2px #AFAFBF dashed; background-color:#F2F2F3; padding:8; margin:0px 0px 0px 0px;"><tr>
<td><font size=+1><b>$view{$i}{'subject'}</b></font><br>
<font size=-1>投稿日時 $view{$i}{'time'}
EOF

if ($view{$i}{'category'}) { print " <a href=\"$location\?category=$view{$i}{'category'}\">$category{$view{$i}{'category'}}</a> | \n"; }

if ($view{$i}{'email'} && $view{$i}{'writer'}) {

	($pre,$suf) = split(/\@/,$view{$i}{'email'},2);

print <<"EOF";
<Script language="JavaScript">
<!--
document.write('by <a href="mai' + 'lto:$pre&#64;$suf">$view{$i}{'writer'}</a> | ');
// -->
</Script>
EOF

}
elsif ($view{$i}{'email'}) {

	($pre,$suf) = split(/\@/,$view{$i}{'email'},2);

print <<"EOF";
<Script language="JavaScript">
<!--
document.write('<a href="mai' + 'lto:$pre&#64;$suf">メール送信</a> | ');
// -->
</Script>
EOF

}
elsif ($view{$i}{'writer'}) { print "by $view{$i}{'writer'} | "; }

print <<"EOF";
<a href="$location/$i">固定リンク</a>$com$info</font>
</td>
EOF

if ($key eq $login_key && $login_key ne '') { print "<td align=right><a href=\"$locationR\?edit=$i\">編集・削除</a></td>"; }

print <<"EOF";
</tr></table>

<table width=100% cellpadding=10 style="border-left:2px #AFAFBF dashed; border-right:2px #AFAFBF dashed; border-bottom:2px #AFAFBF dashed; background-color:#F2F2F3; padding:8; margin: 0px 0px 0px 0px;">
<tr><td>
<table cellpadding=10>
<tr><td vAlign=top align=center>
EOF

@images = %img = (); $img = $img_count = "";
if ($view{$i}{'image'}) {

	(@images) = split(/\|/,$view{$i}{'image'});
	foreach $i (@images) {

		if ($i =~ /(.+)\.s.+\((\d+)\,(\d+)\)/) { ($j,$k) = split(/\(/,$i,2); $img{$1} = "$j/$2/$3"; }
		elsif ($i =~ /(.+)\..+\((\d+)\,(\d+)\)/) {

			$img_big{$1} = "$2/$3";
			if (! $img{$1}) { ($j,$k) = split(/\(/,$i,2); $img{$1} = "$j/$2/$3"; }
		}
		elsif ($i =~ /(.+)\..+/) {

			if (! $img{$1}) { $img{$i} = $def_height; }
		}
	}

	@key = sort keys %img;
	$img_count = @key;

	if ($img{$key[0]} =~ /(.+)\/(\d+)\/(\d+)/) {

		$fn = $fn2 = $1; $width = $2; $height = $3;
		$fn =~ s/\.s//;
		($dir,$etc) = split(/\_/,$fn,2);

		if ($img_big{$key[0]} =~ /(\d+)\/(\d+)/) {

			$img = "<img src=\"$img_url$dir/$fn2\" width=\"$width\" height=\"$height\" alt=\"クリックで表\示\" border=0 onClick=\"ViewImage('$img_url$dir/$fn',$1,$2)\" style='cursor:pointer'>";
		}
		else { $img = "<a href=\"$img_url$dir/$fn\" target=_blank><img src=\"$img_url$dir/$fn2\" width=\"$width\" height=\"$height\" alt=\"\" border=0></a>"; }
	}
	else {
		($dir,$etc) = split(/\_/,$key[0],2);

		$height = $img{$key[0]};
		$img = "<a href=\"$img_url$dir/$key[0]\" target=_blank><img src=\"$img_url$dir/$key[0]\" height=\"$height\" alt=\"\" border=0></a>";
	}

	print "$img";

	if ($img_count > 1) { print "<p><font size=-1><a href=\"$location\/$i\">もっと写真を見る</a></font>\n"; }
}

if ($view{$i}{'body2'} ne "") { $more = "<p><font size=-1><a href=\"$location\/$i\">続きを読む</a> $com2</font>\n"; }
else { $more = "<p><font size=-1>$com2</font>\n"; }

print <<"EOF";
</td>
<td valign=top>
<tt>
$view{$i}{'body'}
</tt>
$more</td></tr>
</table>
</td></tr></table>
<p>
EOF

}

if ($in{'vp'} eq "") { $in{'vp'} = 1; }
$start = $in{'vp'} - 5;

if ($start < 1) { $start = 1; }
elsif ($start == 1) { ; }
elsif ($start > 2) { push(@link,"<font size=+1><a href=\"$location\?category=$in{'category'}\" title=\"最初のページ\">1</a></font> .."); }
else { push(@link,"<font size=+1><a href=\"$location\?category=$in{'category'}\" title=\"最初のページ\">1</a></font>"); }

$end = $in{'vp'} + 5;
if ($end >= $last_page) { $end = $last_page; }
elsif ($end < $last_page - 1) { $last_link = " .. <font size=+1><a href=\"$location\?category=$in{'category'}&vp=$last_page\">$last_page</a></font>\n"; }
else { $last_link = "<font size=+1><a href=\"$location\?category=$in{'category'}&vp=$last_page\">$last_page</a></font>\n"; }

$n = $vp = 0;
foreach $vp ($start .. $end) {

	if ($vp == $in{'vp'}) { push(@link,"<b><font size=+1>$vp</font></b>"); $n = 1; $page_now = $vp; }
	elsif ($vp == 1) { push(@link,"<font size=+1><a href=\"$location\?category=$in{'category'}\">1</a></font> "); }
	else {
		if ($n) {
			$n = 0;
			$Next_link = "[ <a href=\"$location\?category=$in{'category'}&vp=$vp\"><b>次のページ</b></a> ]";
			$next = $vp;
		}
		else { $next = $vp; }

		push(@link,"<font size=+1><a href=\"$location\?category=$in{'category'}&vp=$vp\">$next</a></font>");
	}
}

$link = join ' | ' , @link;
print "<hr size=1><table width=100%><tr><td>$link</td><td align=right vAlign=top>$Next_link</td></tr>\n";

if (!$allhits) { print "<tr><td><font size=-1>このカテゴリに記事はありません.</font></td></tr></table>\n"; }
else { print "<tr><td><font size=-1><b>$page_now</b>ページ目を表\示 ($allhits件抽出)</font></td></tr></table>\n"; }

print <<"EOF";
$TITLE2

<div align=right>
<font size=1>Powered by <a href="http://www.rescue.ne.jp/" target=_blank title="設計/著作 CGI-RESCUE">miniBLOG</a></font>&nbsp;&nbsp;
</div>
</BODY>
</HTML>
EOF

# ↑著作リンクを削除することは利用規定違反となります。

exit;

}

#
###
#

sub setCookie
{

local($set,$key,$val) = @_;

if ($set) {

	if ($in{'rec'}) {

		($secg,$ming,$hourg,$mdayg,$mong,$yearg,$wdayg,$ydayg,$isdstg) = gmtime(time + 1*24*60*60); # 1日保存
		@monthg = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
		@youbi = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
		$date_gmt = sprintf("%s\, %02d\-%s\-%04d %02d:%02d:%02d GMT",$youbi[$wdayg],$mdayg,$monthg[$mong],$yearg +1900,$hourg,$ming,$secg);
		$rec = "; expires=$date_gmt";
	}
	else { $rec = ""; }

	$val =~ s/([^0-9A-Za-z_])/"%" . unpack("H2",$1)/ge; # URLエンコード
	$val =~ tr/ /+/;

	print "P3P: CP=\"NON DSP COR CURa ADMa DEVa CUSa TAIa OUR SAMa IND\"\n";
	print "Set-Cookie: $key=$val$rec\n";
}
else {
	$clear_gmt = "Sun, 01-Jan-1995 01:00:00 GMT";
	print "P3P: CP=\"ADM NOI\"\n";
	print "Set-Cookie: $key=\n";
}

}

#
###
#

sub decCookie
{

local($name) = @_;
local($key,$val,$key2,$val2);
local($cookies) = $ENV{'HTTP_COOKIE'};

@pairs = split(/;/,$cookies);
foreach $pair (@pairs) {

	($key,$val) = split(/=/,$pair,2);
	$key =~ s/ //g;

	if ($key eq $name) {

		$val =~ tr/+/ /;
		$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
		last;
	}
}

return $val;

}

#
###
#

sub DecodeCSV {

	# 参考 http://www.din.or.jp/~ohzaki/regex.htm

	local($tmp) = @_;
	local(@values) = ();

	$tmp =~ s/\n//;
	if ($tmp eq '') { return (); }

	$tmp =~ s/(?:\x0D\x0A|[\x0D\x0A])?$/,/;
	@values = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_}
	($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);

	@values;
}

#
###
#

sub error
{

local (@msg) = @_;
local ($i);

print <<"EOF";
Content-type: text/html\n
<html>
<head>
<title>$title</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</HEAD>
$body
<h1>$_[0]<hr size=1></h1>
EOF

foreach $i (1 .. $#msg) { print "$msg[$i]<br>\n"; }

print <<"EOF";
</body>
</html>
EOF

exit;

}
