#! /usr/bin/perl


############################################################
#
#   名  前：春香コラム
#   制  作：2005年 7月 5日 修正
#
#
############################################################


#======初期設定==================================================

#このCGIファイル
$column = './column.cgi';

#ログファイル
$log_dat = './haruka.cgi';

#一覧表で１ページに表示する件数
$def = 10;

#ロックモード（0=ロックしない/1=symlink関数/2=mkdir関数）
$lockkey = 1;

#ロックファイル
$lockfile = '../lock/column.lock';

# インクルードファイル
$inc_foot = '../files/footer2.txt';

#JCODEパス
require '../jcode.pl';

#================================================================


if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
else { $buffer = $ENV{'QUERY_STRING'}; }


# フォーム内容を分析
@pairs = split(/&/,$buffer);
foreach $pair (@pairs) {

        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        &jcode'convert(*value,'sjis');

	$value =~ s/\,//g;
	$value =~ s/\n//g;
	$value =~ s/\<//g;
	$value =~ s/\>//g;
        $value =~ s/\"//g;

	if ($value =~ /\D/) { $errorcheck = "<small>●不正なアクセスです</small>"; &error; }

	$FORM{$name} = $value;

}


#ロック開始
&lock;

if (!open(LOG,"$log_dat")) { $errorcheck = "<small>●データベースが開けません</small>"; &error; }
@lines = <LOG>;
close(LOG);

#ロック解除
&unlock;

#フッタファイルの読み込み
&inc_file;


if ($FORM{'page'} eq '') { $page = 0; } else { $page = $FORM{'page'}; }

$accesses = @lines; $accesses--;
$page_end = $page + $def - 1;
if ($page_end > $accesses) { $page_end = $accesses; }


print "Content-type: text/html\n\n";

&html_head;


foreach ($page .. $page_end) {

	($no,$title,$msg,$t_date,$host) = split(/\,/,$lines[$_]);

	chop($page) if $page =~ /\n/;


	print "<table border=\"0\" width=\"560\" cellspacing=\"5\" cellpadding=\"0\" class=\"column_list\">\n";
	print "<tr>\n";
	print "<th><small><img src=\"../image/m_head1.gif\" alt=\"\" width=\"11\" height=\"13\" align=\"top\"> $title</small></th>\n";
	print "<td align=\"right\"><small>$t_date</small></td>\n";
	print "</tr>\n";
	print "<tr>\n";
	print "<td colspan=\"2\"><img src=\"../image/line_dot.gif\" alt=\"\" width=\"550\" height=\"1\"></td>\n";
	print "</tr>\n";
	print "<tr>\n";
	print "<td colspan=\"2\"><small><br></small></td>\n";
	print "</tr>\n";
	print "<tr>\n";
	print "<td colspan=\"2\"><small class=\"column_text\">\n";
	print "$msg\n";
	print "</small></td>\n";
	print "</tr>\n";
	print "</table>\n";

}


# 改ページの処理-------------------------------------------
$page_next = $page_end + 1;
$page_back = $page - $def;

$i = $page + 1; $j = $page_end + 1;

if ($page_end ne $accesses) {

	if ($i > $def) {

		print "<table border=\"0\" width=\"560\" cellspacing=\"5\" cellpadding=\"0\">\n";
		print "<tr>\n";
		print "<td colspan=\"2\"><img src=\"../image/line_dot.gif\" alt=\"\" width=\"550\" height=\"1\"></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<form method=\"POST\" action=\"$column\">\n";
		print "<input type=\"hidden\" name=\"page\" value=\"$page_back\">\n";
		print "<td align=\"right\" width=\"50%\"><input type=\"image\" src=\"image/previous.gif\" alt=\"前のページ\" border=\"0\" width=\"107\" height=\"17\"></td>\n";
		print "</form>\n";
		print "<form method=\"POST\" action=\"$column\">\n";
		print "<input type=\"hidden\" name=\"page\" value=\"$page_next\">\n";
		print "<td width=\"50%\"><input type=\"image\" src=\"image/next.gif\" alt=\"次のページ\" border=\"0\" width=\"86\" height=\"17\"></td>\n";
		print "</form>\n";
		print "</tr>\n";
		print "</table>\n";

	}

	else {

		print "<table border=\"0\" width=\"560\" cellspacing=\"5\" cellpadding=\"0\">\n";
		print "<tr>\n";
		print "<td><img src=\"../image/line_dot.gif\" alt=\"\" width=\"550\" height=\"1\"></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<form method=\"POST\" action=\"$column\">\n";
		print "<input type=\"hidden\" name=\"page\" value=\"$page_next\">\n";
		print "<td align=\"center\"><input type=\"image\" src=\"image/next.gif\" alt=\"次のページ\" border=\"0\" width=\"86\" height=\"17\"></td>\n";
		print "</form>\n";
		print "</tr>\n";
		print "</table>\n";

	}

}

else {

	if ($accesses > $def) {

		print "<table border=\"0\" width=\"560\" cellspacing=\"5\" cellpadding=\"0\">\n";
		print "<tr>\n";
		print "<td><img src=\"../image/line_dot.gif\" alt=\"\" width=\"550\" height=\"1\"></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<form method=\"POST\" action=\"$column\">\n";
		print "<input type=\"hidden\" name=\"page\" value=\"$page_back\">\n";
		print "<td align=\"center\"><input type=\"image\" src=\"image/previous.gif\" alt=\"前のページ\" border=\"0\" width=\"107\" height=\"17\"></td>\n";
		print "</form>\n";
		print "</tr>\n";
		print "</table>\n";

	}

	else{

		print "<table border=\"0\" width=\"560\" cellspacing=\"5\" cellpadding=\"0\">\n";
		print "<tr>\n";
		print "<td><img src=\"../image/line_dot.gif\" alt=\"\" width=\"550\" height=\"1\"></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td><small><br></small></td>\n";
		print "</tr>\n";
		print "</table>\n";

	}

}

&html_foot;

exit;



sub html_head {

print <<"HTML";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>

<meta http-equiv="CONTENT-TYPE" content="text/html; CHARSET=Shift_JIS">
<meta name="keywords" content="春香,モデル,ホームページ,サイト,haruka,はるか,ハルカ,タレント,ファッション,女優,写真集,癒し,コマーシャル,CM">
<meta name="description" content="春香コラム。春香自身の書き込みによるコラムをご覧いただけます。">

<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" type="text/css" href="../files/page.css">

<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript" src="../files/page.js"></script>

<title>春香コラム - 春香オフィシャルサイト</title>

</head>
<body>

<table border="0" width="100%" cellspacing="0" cellpadding="0" id="head_area">
<tr>
<td><img src="../image/top_logo.gif" alt="Haruka Web" border="0" width="124" height="23"></td>
<td width="651" nowrap>&nbsp;</td>
</tr>
</table>

<table border="0" width="100%" height="500" cellspacing="5" cellpadding="0" id="column_main">
<tr valign="top">
<td width="200" nowrap>&nbsp;</td>
<td align="center">

<!-- <p id="page_menu"><img src="../image/link_head.gif" alt="" width="15" height="12" align="top"><a href="../index.html" title="トップページ" class="mb">Top</a>　<a href="../news/news.cgi" title="最新ニュース" class="mb">News</a>　<a href="../profile/index.html" title="春香プロフィール" class="mb">Profile</a>　<a href="../gallery/index.html" title="ギャラリー" class="mb">Gallery</a>　<a href="../poster_cover/index.html" title="ポスター&amp;カバー" class="mb">Poster&amp;Cover</a>　<a href="../haruka_column/column.cgi" title="春香コラム" class="mba">Column</a>　<a href="../fan_letter/index.html" title="ファンレター" class="mb">FanLetter</a>　<a href="../link/index.html" title="リンク" class="mb">Link</a></p> -->

<p id="page_title"><img src="image/title.gif" alt="Column" width="111" height="28"></p>

<p class="line"><img src="../image/line.gif" alt="" width="390" height="1"></p>

<table border="0" width="560" cellspacing="0" cellpadding="0" class="md1">
<tr>
<td><img src="image/sub_title.gif" alt="Haruka Column" width="210" height="27"></td>
</tr>
</table>

HTML

}



sub html_foot {

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

HTML

print @incdata_foot;

print <<"HTML";

</body>
</html>
HTML

}
sub inc_file {

	if (!open(INC,"$inc_foot")) { $errorcheck = "<small>●Footerファイルが開けません</small>"; &error; }
	@incdata_foot = <INC>;
	close(INC);

}



# エラーメッセージ-------------------------------------------
sub error {

if ($lockflag == 1) { &unlock; }

print "Content-type: text/html\n\n";

&html_head;

print <<"HTML";
<table border="0" width="560" cellspacing="0" cellpadding="0" id="fan_cmt">
<tr>
<td><small>
<span style="color: #CC0000; font-weight: bold;">エラーが発生しました</span>
</small></td>
</tr>
</table>

<table border="0" width="560" cellspacing="0" cellpadding="0">
<tr>
<td>$errorcheck</td>
</tr>
</table>

<p style="margin-top: 40px; margin-bottom: 30px;">
<a href="javascript:history.back();"><small>前のページにもどる</small></a>
</p>
HTML

&html_foot;

exit;

}



#ロック処理------------------------------
sub lock {

	local($retry) = 5;

	#古いロックを削除する
	if (-e $lockfile) {

		local($mtime) = (stat($lockfile))[9];
		if ($mtime < time - 60) { &unlock; }

	}
	#symlink関数
	if ($lockkey == 1) {

		while (!symlink(".", $lockfile)) {

			if (--$retry <= 0) { $errorcheck = "ロックBUSY"; &error; }
			sleep(1);

		}

	}
	#mkdir関数
	elsif ($lockkey == 2) {

		while (!mkdir($lockfile, 0755)) {

			if (--$retry <= 0) { $errorcheck = "ロックBUSY"; &error; }
			sleep(1);

		}

	}

	$lockflag = 1;

}

sub unlock {

	if ($lockkey == 1) { unlink($lockfile); }
	elsif ($lockkey == 2) { rmdir($lockfile); }

	$lockflag = 0;

}
