#!/usr/bin/perl

#┌─────────────────────────────────
#│ Web Forum : admin.cgi - 2011/10/07
#│ Copyright (c) KentWeb
#│ http://www.kent-web.com/
#└─────────────────────────────────

# モジュール宣言
use strict;
use CGI::Carp qw(fatalsToBrowser);
use lib "./lib";
use Jcode;

# 外部ファイル取込み
require './init.cgi';
my %cf = &init;

# データ受け取り
my %in = &parse_form;

# 認証
&check_passwd;

# 処理分岐
if ($in{mode} eq "edit" && $in{no}) { &edit_data; }
if ($in{mode} eq "dele" && $in{no}) { &dele_data; }
if ($in{list_data}) { &list_data; }

# メニュー画面
&menu_html;

#-----------------------------------------------------------
#  メニュー画面
#-----------------------------------------------------------
sub menu_html {
	&header("メニューTOP");

	print <<EOM;
<div align="center">
<p>選択ボタンを押してください。</p>
<form action="$cf{admin_cgi}" method="post">
<input type="hidden" name="pass" value="$in{pass}">
<table border="1" cellpadding="5" cellspacing="0">
<tr>
	<th bgcolor="#cccccc">選択</th>
	<th bgcolor="#cccccc" width="250">処理メニュー</th>
</tr><tr>
	<th><input type="submit" name="list_data" value="選択"></th>
	<td>記事リスト（修正/削除）</td>
</tr><tr>
	<th><input type="button" value="選択" onclick="javascript:window.location='$cf{bbs_cgi}'"></th>
	<td>掲示板へ戻る</td>
</tr><tr>
	<th><input type="button" value="選択" onclick="javascript:window.location='$cf{admin_cgi}'"></th>
	<td>ログアウト</td>
</tr>
</table>
</form>
</div>
</body>
</html>
EOM
	exit;
}

#-----------------------------------------------------------
#  記事リスト
#-----------------------------------------------------------
sub list_data {
	# ページ数
	my $page = $in{page} || '0';
	foreach ( keys(%in) ) {
		if (/^page:(\d+)$/) {
			$page = $1;
			last;
		}
	}

	&header("メニューTOP ＞ 記事リスト");
	&back_btn;
	print <<EOM;
<b class="ttl">■記事リスト</b>
<hr color="#00f078">
<ul>
<li>処理を選択して送信ボタンを押してください。
<li>ツリーの先頭記事を削除すると、ツリーごと一括削除されます。
</ul>
<form action="$cf{admin_cgi}" method="post">
<input type="hidden" name="pass" value="$in{pass}">
<input type="hidden" name="list_data" value="1">
処理 <select name="mode">
<option value="edit">修正
<option value="dele">削除
</select>
<input type="submit" value="送信する">
<dl>
EOM

	my $i = 0;
	my $x = 0;
	open(IN,"$cf{logfile}") || &error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$re,$lx,$sub,$eml,$url,$nam,$dat,$msg,$t,$hos,$pw,$wr,$oya) = split(/<>/);
		if ($re == 0) { $i++; }
		next if ($i < $page + 1);
		next if ($i > $page + $cf{p_tree});

		my $sp;
		if ($lx) { $sp = "&nbsp;&nbsp;&nbsp;" x $lx; }
		$msg =~ s/<br>/ /g;
		if (length($msg) > 60) { $msg = substr($msg,0,58); $msg .= '..'; }

		print "<dt>";
		print "<hr>" if ($no == $oya);
		print qq|$sp<input type="checkbox" name="no" value="$no">[$no] <b>$sub</b> - <B>$nam</B> $dat [$hos]\n|;
		print qq|<dd>$sp<span style="font-size:11px;color:$cf{no_color};">$msg</span>\n|;

	}
	close(IN);

	print "<dt><hr></dl>\n";

	my $next = $page + $cf{p_tree};
	my $back = $page - $cf{p_tree};

	if ($back >= 0) {
		print qq|<input type="submit" name="page:$back" value="前画面">\n|;
	}
	if ($next < $i) {
		print qq|<input type="submit" name="page:$next" value="次画面">\n|;
	}

	print <<EOM;
</form>
</body>
</html>
EOM
	exit;
}

#-----------------------------------------------------------
#  編集画面
#-----------------------------------------------------------
sub edit_data {
	# 修正実行
	if ($in{submit}) { &edit_log; }

	# ログを開く
	my ($no,$re,$x,$sub,$eml,$url,$nam,$dat,$msg,$t,$ho,$pw,$wr,$oya,$sml);
	open(IN,"$cf{logfile}") || &error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		($no,$re,$x,$sub,$eml,$url,$nam,$dat,$msg,$t,$ho,$pw,$wr,$oya,$sml) = split(/<>/);

		last if ($in{no} == $no);
	}
	close(IN);

	$msg =~ s/<br>/\n/g;

	# 編集フォームを出力
	&header("メニューTOP ＞ 記事リスト ＞ 編集画面");
	&back_btn('list_data');
	print <<EOM;
<b class="ttl">■記事リスト ＞ 編集画面</b>
<hr color="#00f078">
<p>▼変更したい部分のみ修正し送信ボタンを押してください。</p>
<form action="$cf{admin_cgi}" method="post">
<input type="hidden" name="mode" value="edit">
<input type="hidden" name="no" value="$in{no}">
<input type="hidden" name="pass" value="$in{pass}">
<table border="0">
<tr>
	<td><B>投稿者</B></td>
	<td><input type="text" name="name" value="$nam" size="28"></td>
</tr><tr>
	<td><B>Ｅメール</B></td>
	<td><input type="text" name="email" value="$eml" size="28">
		<select name="smail">
EOM

	my @sm = ('表示', '非表示');
	foreach (0, 1) {
		if ($sml == $_) {
			print "<option value=\"$_\" selected>$sm[$_]\n";
		} else {
			print "<option value=\"$_\">$sm[$_]\n";
		}
	}

	print <<EOM;
</select></td>
</tr><tr>
	<td><B>タイトル</B></td>
	<td><input type="text" name="sub" value="$sub" size="38"></td>
</tr><tr>
	<td colspan="2"><B>メッセージ</B><br>
<textarea name="message" cols="64" rows="10">$msg</textarea></td>
</tr><tr>
	<td><B>参照先</B></td>
	<td><input type="text" name="url" value="$url" size="55"></td>
</tr>
</table>
<br>
<input type="submit" name="submit" value=" 送信する ">
</form>
</body>
</html>
EOM
	exit;
}

#-----------------------------------------------------------
#  編集実行
#-----------------------------------------------------------
sub edit_log {
	# ログを開く
	my @data;
	open(DAT,"+< $cf{logfile}") || &error("open err: $cf{logfile}");
	eval "flock(DAT, 2);";
	my $top = <DAT>;
	while (<DAT>) {
		chomp;
		my ($no,$re,$x,$sub,$eml,$url,$nam,$dat,$msg,$t,$ho,$pw,$wr,$oya,$sml,$res) = split(/<>/);

		if ($in{no} == $no) {
			$_ = "$no<>$re<>$x<>$in{sub}<>$in{email}<>$in{url}<>$in{name}<>$dat<>$in{message}<>$t<>$ho<>$pw<><>$oya<>$in{smail}<>$res<>";
		}
		push(@data,"$_\n");
	}

	# ログを更新
	unshift(@data,$top);
	seek(DAT, 0, 0);
	print DAT @data;
	truncate(DAT, tell(DAT));
	close(DAT);

	# 完了
	&message("修正を完了しました");
}

#-----------------------------------------------------------
#  削除処理
#-----------------------------------------------------------
sub dele_data {
	my %del;
	foreach ( split(/\0/, $in{no}) ) {
		$del{$_}++;
	}

	# ログを開く
	my @data;
	open(DAT,"+< $cf{logfile}") or &error("open err: $cf{logfile}");
	eval "flock(DAT, 2);";
	my $top = <DAT>;
	while (<DAT>) {
		my ($no,$re,$x,$sub,$eml,$url,$nam,$dat,$msg,$t,$ho,$pw,$wr,$oya) = split(/<>/);

		next if (defined($del{$no}));
		next if (defined($del{$oya}));

		push(@data,$_);
	}

	# ログを更新
	unshift(@data,$top);
	seek(DAT, 0, 0);
	print DAT @data;
	truncate(DAT, tell(DAT));
	close(DAT);

	# 初期画面に戻る
	&list_data;
}

#-----------------------------------------------------------
#  パスワード認証
#-----------------------------------------------------------
sub check_passwd {
	# パスワードが未入力の場合は入力フォーム画面
	if ($in{pass} eq "") {
		&enter_form;

	# パスワード認証
	} elsif ($in{pass} ne $cf{password}) {
		&err("認証できません");
	}
}

#-----------------------------------------------------------
#  入室画面
#-----------------------------------------------------------
sub enter_form {
	&header("入室画面");
	print <<EOM;
<div align="center">
<form action="$cf{admin_cgi}" method="post">
<table width="380" style="margin-top:50px">
<tr>
	<td height="40" align="center">
		<fieldset><legend>管理パスワード入力</legend>
		<br>
		<input type="password" name="pass" value="" size="20">
		<input type="submit" value=" 認証 ">
		<br><br>
		</fieldset>
	</td>
</tr>
</table>
</form>
<script language="javascript">
<!--
self.document.forms[0].pass.focus();
//-->
</script>
</div>
</body>
</html>
EOM
	exit;
}

#-----------------------------------------------------------
#  HTMLヘッダー
#-----------------------------------------------------------
sub header {
	my $ttl = shift;

	print "Content-type: text/html\n\n";
	print <<EOM;
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=shift_jis">
<meta http-equiv="content-style-type" content="text/css">
<style type="text/css">
<!--
body,td,th { font-size: 13px; }
.ttl { color: #004040; }
-->
</style>
<title>$ttl</title>
</head>
<body>
EOM
}

#-----------------------------------------------------------
#  戻りボタン
#-----------------------------------------------------------
sub back_btn {
	my ($mode) = @_;

	print <<EOM;
<div align="right">
<form action="$cf{admin_cgi}" method="post">
<input type="hidden" name="pass" value="$in{pass}">
@{[ $mode ? qq|<input type="submit" name="$mode" value="&lt; 前画面">| : "" ]}
<input type="submit" value="&lt; メニュー">
</form>
</div>
EOM
}

#-----------------------------------------------------------
#  エラー
#-----------------------------------------------------------
sub err {
	my $msg = shift;

	&header("ERROR!");
	print <<EOM;
<div align="center">
<hr width="350">
<h3>ERROR!</h3>
<p style="color:#dd0000">$msg</p>
<hr width="350">
<form>
<input type="button" value="前画面に戻る" onclick="history.back()">
</form>
</div>
</body>
</html>
EOM
	exit;
}

#-----------------------------------------------------------
#  メッセージ表示
#-----------------------------------------------------------
sub message {
	my ($msg,$btn) = @_;

	&header("処理完了");
	print <<EOM;
<b class="ttl">■処理完了</b>
<hr color="#00f078">
<p>$msg</p>
<form action="$cf{admin_cgi}" method="post">
<input type="hidden" name="pass" value="$in{pass}">
<input type="hidden" name="list_data" value="1">
<input type="submit" value="メニューに戻る">
</form>
</body>
</html>
EOM
	exit;
}

