#!/usr/bin/perl

#Web Diary Professional (2004/05/01)
$ver = '2.27';
#
#Copyright(C) Knight 2002-2004
#Mail ... knight@big.or.jp
#Home ... http://www.big.or.jp/~knight/

#――――― 設定項目 ――――――――――――――――――――――――

#管理者用パスワード
$admin_pwd = 'kai';

#初期設定ファイル
require './diary.ini';

#――――― 設定項目終了 ――――――――――――――――――――――

### メイン処理
&mobile();
&decode();
if ($in{'mode'} eq 'res' && $res_mode == 1) {
	&res();
} elsif ($in{'mode'} eq 'icon' && $icon_mode == 1) {
	&icon();
} elsif ($in{'mode'} eq 'img' && $img_show_way == 2) {
	&img();
} elsif ($in{'mode'} eq 'loglist') {
	&loglist();
} elsif ($in{'mode'} eq 'view') {
	&view();
} elsif ($in{'mode'} eq 'search') {
	&search();
} elsif ($in{'mode'} eq 'userdel' && $res_mode == 1) {
	&userdel();
} elsif ($in{'mode'} eq 'past' && $make_past == 1) {
	&past();
} elsif ($in{'mode'} eq 'admin') {
	&admin();
} elsif ($in{'mode'} eq 'regist' && $res_mode == 1) {
	&regist();
}
if ($frame_mode == 1) {
	&frame();
} else {
	&html();
}

### HTML出力
sub html {
	if ($mini_counter != 0) {
		if ($ENV{'REQUEST_METHOD'} ne 'POST' && $ENV{'QUERY_STRING'} eq '') {
			&count_up();
		}

		open(COUNT, "$count_log") || &error("カウントログファイルを読み出せません");
		$data = <COUNT>;
		close(COUNT);

		($sum, $c_today, $c_yesterday, $key) = split(/\t/, $data);
	}

	open(FILE, "$log_file") || &error("ログファイルを読み出せません");
	@logs = <FILE>;
	close(FILE);

	&header();

	print <<"_HTML_";
<div class="link">
	<a href="$back_url">▲戻る</a> / 
	<a href="$script?mode=loglist">タイトル一覧</a> / 
	<a href="$script?mode=search">ログ検索</a> / 
_HTML_

	if ($res_mode == 1) {
		print qq|<a href="$script?mode=userdel">記事編集</a> / \n|;
	}
	if ($make_past == 1) {
		print qq|<a href="$script?mode=past">過去ログ</a> / \n|;
	}

	print <<"_HTML_";
	<a href="$script?mode=admin">管理者用</a>
</div>
<div align="right">
_HTML_

	if ($mini_counter == 1) {
		$counter = sprintf("%0${count_figure}d", $sum);
		print qq|Access $counter\n|;
	}

	print <<"_HTML_";
</div>
_HTML_

	if ($show_title == 1) {
		print <<"_HTML_";
<div class="title">
_HTML_
		if ($title_img ne '') {
			print qq|<br><img src="$title_img" alt="$title" width="$title_width" height="$title_height"><br><br>\n|;
		} else {
			print qq|<h1>$title</h1>\n|;
		}

		print <<"_HTML_";
	$top_message<br>
	<br>
</div>
_HTML_
	} else {
		print '<br><br>';
	}

	print <<"_HTML_";
<table align="center">
	<tr>
		<td align="center" valign="top">
_HTML_

	if ($layout == 0) {
		if ($show_calendar == 1) {
			&calendar(@logs);
		}

		if ($record_size != 0) {
			&record(@logs);
		}

		print <<"_HTML_";
		</td>
		<td align="center" valign="top">
_HTML_

		if ($show_index == 0 || $in{'date'} ne '' || $in{'year'} ne '' || $in{'month'} ne '' || $in{'no'} ne '') {
			&showlog(&extract(@logs));
		} else {
			&index_mesg();
		}
	} elsif ($layout == 1) {
		if ($show_calendar == 1) {
			&calendar(@logs);
		}

		if ($record_size != 0) {
			&record(@logs);
		}

		if ($show_index == 0 || $in{'date'} ne '' || $in{'year'} ne '' || $in{'month'} ne '' || $in{'no'} ne '') {
			&showlog(&extract(@logs));
		} else {
			&index_mesg();
		}
	} else {
		if ($show_index == 0 || $in{'date'} ne '' || $in{'year'} ne '' || $in{'month'} ne '' || $in{'no'} ne '') {
			&showlog(&extract(@logs));
		} else {
			&index_mesg();
		}

		print <<"_HTML_";
		</td>
		<td align="center" valign="top">
_HTML_

		if ($show_calendar == 1) {
			&calendar(@logs);
		}

		if ($record_size != 0) {
			&record(@logs);
		}
	}

	print <<"_HTML_";
		</td>
	</tr>
</table>
<hr>
_HTML_

	&footer();
}

### フレーム
sub frame {
	if ($in{'mode'} eq 'frame_calendar' || $in{'exec'} eq 'move') {
		open(FILE, "$log_file") || &error("ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		&header();

		print <<"_HTML_";
<div class="link">
	<a href="$back_url" target="_parent">▲戻る</a> / 
	<a href="$script?mode=loglist" target="_parent">タイトル一覧</a> / 
	<a href="$script?mode=search" target="_parent">ログ検索</a> / 
_HTML_

		if ($res_mode == 1) {
			print qq|<a href="$script?mode=userdel" target="_parent">記事編集</a> / \n|;
		}

		if ($make_past == 1) {
			print qq|<a href="$script?mode=past" target="_parent">過去ログ</a> / \n|;
		}

	print <<"_HTML_";
</div>
_HTML_

		if ($show_calendar == 1) {
			&calendar(@logs);
		}
		if ($record_size != 0) {
			&record(@logs);
		}

		print qq|<div align="center"><a href="$script?mode=admin" target="_parent">管理者用</a></div>\n|;

		&footer();
	} elsif ($in{'mode'} eq 'frame_log' || $in{'date'} ne '' || $in{'year'} ne '' || $in{'no'} ne '') {
		if ($mini_counter != 0) {
			open(COUNT, "$count_log") || &error("カウントログファイルを読み出せません");
			$data = <COUNT>;
			close(COUNT);

			($sum, $c_today, $c_yesterday, $key) = split(/\t/, $data);
		}

		open(FILE, "$log_file") || &error("ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		&header();

		print qq|<div align="right">\n|;

		if ($mini_counter == 1) {
			$counter = sprintf("%0${count_figure}d", $sum);
			print qq|Access $counter\n|;
		}

		print qq|</div>\n|;
		print qq|<div class="title">\n|;

		if ($show_title == 1) {
			if ($title_img ne '') {
				print qq|<br><img src="$title_img" alt="$title" width="$title_width" height="$title_height"><br><br>\n|;
			} else {
				print qq|<h1>$title</h1>\n|;
			}

			print <<"_HTML_";
	$top_message<br>
	<br>
</div>
_HTML_
		} else {
			print '<br><br>';
		}

		if ($show_index == 0 || $in{'date'} ne '' || $in{'year'} ne '' || $in{'month'} ne '' || $in{'no'} ne '') {
			&showlog(&extract(@logs));
		} else {
			&index_mesg();
		}

		print '<hr>';

		&footer();
	} else {
		if ($mini_counter != 0 && $ENV{'REQUEST_METHOD'} ne 'POST' && $ENV{'QUERY_STRING'} eq '') {
			&count_up();
		}

		print <<"_HTML_";
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html lang="ja">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>$title</title>
<link rel="stylesheet" href="$css_file" type="text/css">
</head>

<frameset cols="$frame_cols" border="0" frameborder="0" framespacing="0">
_HTML_

		if ($frame_layout == 0) {
			print <<"_HTML_";
	<frame src="$script?mode=frame_calendar" name="calendar">
	<frame src="$script?mode=frame_log" name="log">
_HTML_
		} else {
			print <<"_HTML_";
	<frame src="$script?mode=frame_log" name="log">
	<frame src="$script?mode=frame_calendar" name="calendar">
_HTML_
		}

		print <<"_HTML_";
</frameset>

</html>
_HTML_
	}

	exit();
}

### インデックス
sub index_mesg {
	print <<"_HTML_";
<div class="log">
	<table class="log_table">
		<tr>
			<td align="center" class="log_table_head"><strong>$title</strong></td>
		</tr>
		<tr>
			<td class="log_table_data">
				<br>
				<table width="100%">
					<tr>
						<td class="log_table_indent">&nbsp;</td>
						<td>
							$index_message
_HTML_

	if ($index_record_size != 0) {
		print "<br>■ <strong>最近の$index_record_size件</strong><br>";

		$i = 0;
		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

			if ($reno != 0) {
				next;
			}
			$i++;
			if ($i > $index_record_size) {
				last;
			}

			($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
			$today = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);
			if ($new_date != 0 && $today - $date < $new_date) {
				$new = $new_mark;
			} else {
				$new = '';
			}

			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				$date = "$1/$2/$3";
			}

			print qq|<a href="$script?no=$no">$date</a> … $subj $new<br>\n|;
		}
	}

	print <<"_HTML_";
						</td>
						<td class="log_table_indent">&nbsp;</td>
					</tr>
				</table>
				<br>
			</td>
		</tr>
	</table>
</div>
_HTML_
}

### カレンダー
sub calendar {
	local(@logs) = @_;

	if ($frame_mode == 1) {
		$target = ' target="log"';
	}

	%date = ();
	foreach (@logs) {
		($date, $icon) = (split(/\t/))[2, 9];
		if ($icon eq '') {
			$icon = '-';
		}
		$date{"$date"} .= "$icon<>";
	}

	($d, $m, $y) = (localtime(time()))[3, 4, 5];
	$y = sprintf("%04d", $y + 1900);
	$m = sprintf("%01d", $m + 1);
	$d = sprintf("%01d", $d);
	if ($in{'date'} ne '') {
		if ($in{'date'} =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
			$year  = sprintf("%04d", $1);
			$month = sprintf("%01d", $2);
		}
		if ($y == $year && $m == $month) {
			$today = $d;
		}
	} elsif ($in{'year'} eq '') {
		$year  = $y;
		$month = $m;
		$today = $d;
	} else {
		if ($in{'year'} == $y && $in{'month'} == $m) {
			$today = $d;
		}
		$year  = $in{'year'};
		$month = $in{'month'};
	}

	$head = qq|<a href="$script?year=$year&month=$month" title="$year年$month月の日記を一覧表\示"$target>$year年 $month月</a>|;

	$prev_month = $month - 1;
	$prev_year  = $year;
	if ($prev_month < 1) {
		$prev_month = 12;
		$prev_year--;
	}
	$prev = qq|<a href="$script?exec=move&year=$prev_year&month=$prev_month" title="$prev_month月に移動">&lt;&lt;</a>|;

	$next_month = $month + 1;
	$next_year  = $year;
	if ($next_month > 12) {
		$next_month = 1;
		$next_year++;
	}
	$next = qq|<a href="$script?exec=move&year=$next_year&month=$next_month" title="$next_month月に移動">&gt;&gt;</a>|;

	print <<"_HTML_";
<table border="1" class="table">
	<tr>
		<th colspan="7" height="30" class="table_head">$prev $head $next</th>
	</tr>
	<tr>
_HTML_

	foreach (0 .. 6) {
		$week = $week[$_];
		print qq|<td align="center" width="$cel_width" height="30" class="table_data">\n|;
		if ($_ == 0) {
			print qq|<span style="color:$sun_color;">$week</span>\n|;
		} elsif ($_ == 6) {
			print qq|<span style="color:$sat_color;">$week</span>\n|;
		} else {
			print qq|<span style="color:$day_color;">$week</span>\n|;
		}
		print qq|</td>\n|;
	}

	print '</tr>';

	$wkey = &getweek('1', $year, $month);
	$lastday = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$month - 1] + ($month == 2 && (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0));

	$i = 0;
	$day = 1;
	$flag = 0;
	$week_num = 0;
	$spend = 0;
	foreach (1 .. 42) {
		if ($wkey == $i) {
			$flag = 1;
		}
		if ($lastday < $day) {
			$flag = 0;
		}

		if ($i == 0) {
			print '<tr>';
		}

		if ($flag == 0) {
			print qq|<td align="center" height="$cel_height" class="table_data">-</td>\n|;
		} else {
			$month = sprintf("%02d", $month);
			$code  = sprintf("%02d", $day);
			if ($day == $today) {
				print qq|<td align="center" valign="top" height="$cel_height" class="table_data" style="background-color:$today_color;">|;
			} else {
				print qq|<td align="center" valign="top" height="$cel_height" class="table_data">|;
			}

			if ($i == 0) {
				$color = $sun_color;
			} elsif ($i == 6) {
				$color = $sat_color;
			} else {
				$color = $day_color;
			}

			if ($spend == 1) {
				$spend = 0;
				$color = $sun_color;
			} else {
				foreach $spend_day (@spend_day) {
					if ("$month$code" eq "$spend_day") {
						if ($i == 0) {
							$spend = 1;
						}
						$color = $sun_color;
						last;
					}
				}
				while (($m, $n) = each(%spend_day)) {
					if ($i == 1 && "$month" eq "$m" && "$week_num" eq "$n") {
						$color = $sun_color;
						last;
					}
				}
			}

			if ($written_mark eq '' && $date{"$year$month$code"} ne '') {
				print qq|<a href="$script?date=$year$month$code" title="日記を表\示"$target>|;
			}

			print qq|<span style="color:$color;">$day</span>\n|;

			if ($written_mark eq '' && $date{"$year$month$code"} ne '') {
				print qq|</a>|;
			}

			if ($written_mark ne '' && $date{"$year$month$code"} ne '') {
				$j = 1;
				foreach $diary_info (split(/<>/, $date{"$year$month$code"})) {
					if ($icon_display != 0) {
						if ($diary_info eq '-') {
							$written_icon = $written_mark
						} else {
							$written_icon = qq|<img src="$iconfile_dir$diary_info" alt="日記を表\示" border="0">|;
						}
					} else {
						$written_icon = $written_mark;
					}
					if ($j != 1) {
						$diary_no = "#$j";
					} else {
						$diary_no = '';
					}
					print qq|<br><a href="$script?date=$year$month$code$diary_no" title="日記を表\示"$target>$written_icon</a>\n|;

					if ($written_mark_multi == 1) {
						$j++;
					} else {
						last;
					}
				}
			}

			print qq|</td>|;
		}

		$i++;
		if ($i == 1 && $flag == 1) {
			$week_num++;
		}
		if ($i == 7) {
			print '</tr>';
			$i = 0;
		}
		if ($flag == 1) {
			$day++;
		}
	}

	print <<"_HTML_";
</table>
_HTML_

	$prev = qq|<a href="$script?exec=move&year=$prev_year&month=$prev_month">&lt;&lt; 前の月</a>\n|;
	$next = qq|<a href="$script?exec=move&year=$next_year&month=$next_month">次の月 &gt;&gt;</a>\n|;

	print qq|<p class="navi">$prev / $next</p>\n|;

	print <<"_HTML_";
<div align="center">
	<form action="$script" method="post" name="form" enctype="multipart/form-data">
		<input type="hidden" name="mode" value="$mode">
		<input type="hidden" name="exec" value="move">
_HTML_

			print qq|<select name="year">\n|;
			foreach (1 .. 3) {
				if ($_ == 1) {
					$prev_year = $year - 1;
					print qq|<option value="$prev_year">$prev_year年</option>\n|;
				} elsif ($_ == 2) {
					print qq|<option value="$year" selected>$year年</option>\n|;
				} else {
					$next_year = $year + 1;
					print qq|<option value="$next_year">$next_year年</option>\n|;
				}
			}
			print qq|</select>\n|;

			print qq|<select name="month">\n|;
			foreach (1 .. 12) {
				if ($_ == $month) {
					print qq|<option value="$_" selected>$_月</option>\n|;
				} else {
					print qq|<option value="$_">$_月</option>\n|;
				}
			}
			print qq|</select>\n|;

			print <<"_HTML_";
		<input type="submit" value="表\示" class="submit">
	</form>
</div>
_HTML_
}

### 更新履歴表示
sub record {
	local(@logs) = @_;

	if ($frame_mode == 1) {
		$target = ' target="log"';
	}

	if ($res_mode == 1) {
		$parent = 0;
		foreach (@logs) {
			if ((split(/\t/, $_))[1] == 0) {
				$parent++;
			}
		}
	} else {
		$parent = $#logs + 1;
	}

	if ($in{'record'} > 0) {
		$n = $in{'record'} - 1;
		$prev = qq|<a href="$script?exec=move&record=$n">↑新しい日記</a> / |;
	} else {
		$prev = '';
	}
	if (int(($parent - 1) / $record_size) > $in{'record'}) {
		$n = $in{'record'} + 1;
		$next = qq|<a href="$script?exec=move&record=$n">↓以前の日記</a>|;
	} else {
		$next = '';
	}

	print <<"_HTML_";
<table width="100%" border="1" class="table">
	<tr>
		<td class="table_head">■ <strong>更新履歴</strong></td>
	</tr>
	<tr>
		<td class="table_data">
_HTML_

	$start = $in{'record'} * $record_size;
	$end   = $start + $record_size;

	$i = 0;
	foreach (@logs) {
		($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($reno != 0) {
			next;
		}
		$i++;
		if ($i <= $start) {
			next;
		}
		if ($i > $end) {
			last;
		}

		($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
		$today = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);
		if ($new_date != 0 && $today - $date < $new_date) {
			$new = $new_mark;
		} else {
			$new = '';
		}

		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
			$date = "$1/$2/$3";
		}

		print qq|<a href="$script?no=$no"$target>$date</a> $new<br>$subj<br><br>\n|;
	}

	print <<"_HTML_";
			<br>
			<div class="navi">$prev$next</div>
		</td>
	</tr>
</table>
<br>
_HTML_
}

### 投稿記事表示
sub showlog {
	local(@logs) = @_;

	if ($frame_mode == 1) {
		$target = ' target="_parent"';
		$mode   = 'mode=frame_log&';
	}

	if ($res_mode == 1) {
		$parent = 0;
		foreach (@logs) {
			if ((split(/\t/, $_))[1] == 0) {
				$parent++;
			}
		}
	} else {
		$parent = $#logs + 1;
	}

	if ($in{'mode'} ne 'past' && $in{'date'} eq '' && $in{'year'} eq '' && $in{'month'} eq '' && $in{'no'} eq '') {
		$start = $in{'page'} * $default_show;
		$end   = $start + $default_show;
	} else {
		$start = 0;
		$end   = $#logs + 1;
	}

	if ($in{'page'} > 0) {
		$n = $in{'page'} - 1;
		$prev = qq|<a href="$script?${mode}page=$n">↑新しい日記</a> / |;
	} else {
		$prev = '';
	}
	if (int(($parent - 1) / $default_show) > $in{'page'}) {
		$n = $in{'page'} + 1;
		$next = qq|<a href="$script?${mode}page=$n">↓以前の日記</a>|;
	} else {
		$next = '';
	}

	$i = 0;
	$flag = 0;
	foreach (@logs) {
		($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($in{'mode'} eq 'search' || $reno == 0) {
			$i++;
		}
		if ($i <= $start) {
			next;
		}
		if ($i > $end) {
			last;
		}

		if ($date ne '') {
			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
				$today = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);
				if ($in{'preview'} ne 'on' && $new_date != 0 && $today - $date < $new_date) {
					$new = $new_mark;
				} else {
					$new = '';
				}

				$year  = sprintf("%04d", $1);
				$month = sprintf("%02d", $2);
				$day   = sprintf("%02d", $3);

				$week = $week[&getweek($day, $year, $month)];
				$date = qq|$year年$month月$day日（$week）|;
			} else {
				($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date);
				$date = sprintf("%04d年%02d月%02d日（%s）%02d:%02d", $year + 1900, $mon + 1, $day, $week[$week], $hour, $min);
			}
		}

		if ($mail ne '') {
			$mail = qq|/ <a href="mailto:$mail">Ｅメール</a>|;
		}
		if ($url ne '') {
			$url = qq|/ <a href="$url" target="_blank">ＵＲＬ</a>|;
		}

		if ($auto_link == 1) {
			$text = &auto_link($text);
		}
		if ($color_mode == 1) {
			if ($color eq '') {
				$color = $color[0];
			}
			$text = qq|<span style="color:$color;">$text</span>|;
		}
		if ($upload_mode == 1 && $file ne '' && -e "$upfile_dir$file") {
			$file =~ /[^\/\\]*\.([^.\/\\]*)$/;
			$ext = ".$1";
			$file_info = '';
			$file_byte = &comma((stat("$upfile_dir$file"))[7]);
			if ($ext eq '.gif' || $ext eq '.jpeg' || $ext eq '.jpg' || $ext eq '.jpe' || $ext eq '.png') {
				open(IMG, "$upfile_dir$file");
				binmode(IMG);
				if ($ext eq '.gif') {
					($img_width, $img_height) = &gifsize(*IMG);
				} elsif ($ext eq '.jpeg' || $ext eq '.jpg' || $ext eq '.jpe') {
					($img_width, $img_height) = &jpegsize(*IMG);
				} elsif ($ext eq '.png') {
					($img_width, $img_height) = &pngsize(*IMG);
				}
				if ($img_show_way == 2) {
					$org_img_width  = $img_width;
					$org_img_height = $img_height;
				}
				if ($img_width > $img_max_width) {
					$img_height = int($img_height / ($img_width / $img_max_width));
					$img_width  = $img_max_width;
				}
				if ($text_align == 0) {
					$align_left = '';
				} else {
					$align_left = ' align="left"';
				}
				if ($img_align == 0) {
					$file_info = qq|<img src="$upfile_url$file" alt="$file_byte byte"$align_left border="0" width="$img_width" height="$img_height">|;
				} elsif ($img_align == 1) {
					$file_info = qq|<div align="center"><img src="$upfile_url$file" alt="$file_byte byte" border="0" width="$img_width" height="$img_height"></div>|;
				} else {
					$file_info = qq|<img src="$upfile_url$file" alt="$file_byte byte" align="right" border="0" width="$img_width" height="$img_height">|;
				}
				if ($img_align == 1 || $text_align == 0) {
					$text_top_br = '<br><br>';
				}
				if ($img_show_way == 0) {
					$text = qq|<a href="$upfile_url$file">$file_info</a>$text_top_br$text\n|;
				} elsif ($img_show_way == 1) {
					$text = qq|<a href="$upfile_url$file" target="_blank">$file_info</a>$text_top_br$text\n|;
				} elsif ($img_show_way == 2) {
					$text = qq|<a href="javascript:void(0)" onClick="window.open('$script?mode=img&file=$file&width=$org_img_width&height=$org_img_height&byte=$file_byte', 'Img', 'width=$org_img_width,height=$org_img_height')">$file_info</a>$text_top_br$text\n|;
				} else {
					$text = qq|$file_info$text_top_br$text\n|;
				}
			} else {
				$text = qq|<a href="$upfile_url$file"><nobr>$file（$file_byte byte）</nobr></a><br><br>$text\n|;
			}
		}
		if ($icon_mode == 1 && $icon_display != 1 && $icon ne '') {
			$icon = qq|<img src="$iconfile_dir$icon" alt="$icon" hspace="10">|;
		} else {
			$icon = '&nbsp;';
		}

		chop($host);

		if ($in{'preview'} ne 'on') {
			$partition = '<hr>';
		}
		if ($res_mode == 1 && $logs[0] + 0 != 0 && ($in{'mode'} eq '' || $in{'mode'} eq 'frame_log')) {
			$res = qq|[ <a href="$script?mode=res&no=$no"$target>返信する</a> ]|;
		}

		if (($in{'mode'} eq 'search' && $in{'no'} eq '' && $flag == 1) || ($flag == 1 && $reno == 0)) {
			print <<"_HTML_";
	</table>
</div>
<br>
_HTML_
		}

		if (($in{'mode'} eq 'search' && $in{'no'} eq '') || $reno == 0) {
			$flag = 1;

			if ($in{'date'} ne '') {
				print qq|<a name="$i"></a>\n|;
			}

			print <<"_HTML_";
<div class="log">
	<table class="log_table">
_HTML_
		}

		if (($in{'mode'} eq 'search' && $in{'no'} eq '') || $reno == 0) {
			if ($res_mode == 1 && $in{'mode'} eq 'search' && $in{'no'} eq '') {
				if ($reno == 0) {
					$show = $no;
				} else {
					$show = $reno;
				}
				$all_show_info = qq|[<a href="$script?mode=search&obje=$in{'obje'}&no=$show">関連記事を表\示</a>]|;
			} else {
				$all_show_info = '';
			}

			print <<"_HTML_";
		<tr>
			<td class="log_table_head">■ <strong>$subj</strong> $new</td>
			<td align="right" class="log_table_head">$date</td>
		</tr>
		<tr>
			<td class="log_table_data" colspan="2">
				<br>
				<table width="100%">
					<tr>
						<td class="log_table_indent">$icon</td>
						<td>$text</td>
						<td class="log_table_indent">&nbsp;</td>
					</tr>
				</table>
				<br>
				<div align="right">$all_show_info $res</div>
			</td>
		</tr>
_HTML_
		} else {
			print <<"_HTML_";
		<tr>
			<td class="log_table_data" colspan="2">
				$partition□ $name $mail $url / $date $new<br>
				<br>
				<table width="100%">
					<tr>
						<td class="log_table_indent">$icon</td>
						<td>$text</td>
						<td class="log_table_indent">&nbsp;</td>
					</tr>
				</table>
				<br>
			</td>
		</tr>
_HTML_
		}
	}
	print <<"_HTML_";
	</table>
</div>
<br>
_HTML_

	if ($in{'mode'} ne 'search' && $in{'mode'} ne 'past' && $in{'date'} eq '' && $in{'year'} eq '' && $in{'month'} eq '' && $in{'no'} eq '') {
		print <<"_HTML_";
<p class="navi">$prev$next</p>
_HTML_
	}

	return;
}

### 表示ログ抽出
sub extract {
	local(@logs) = @_;

	$i = 0;
	@show = ();
	foreach (@logs) {
		($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($in{'date'} ne '') {
			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				$year  = $1;
				$month = $2;
				$day   = $3;
				if($in{'date'} =~ /^(\d\d\d\d)(\d\d)(\d\d)$/ && $year == $1 && $month == $2 && $day == $3) {
					push(@show, $_);
					$show_no = $no;
				}
			} elsif ($show_no ne '' && $show_no == $reno) {
				push(@show, $_);
			}
		} elsif ($in{'year'} ne '' || $in{'month'} ne '') {
			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/ && $in{'year'} == $1 && $in{'month'} == $2) {
				push(@show, $_);
				$show_no = $no;
			} elsif ($show_no ne '' && $show_no == $reno) {
				push(@show, $_);
			}
		} elsif ($in{'no'} ne '') {
			if ($in{'no'} == $no || $in{'no'} == $reno) {
				push(@show, $_);
			}
		} else {
			push(@show, $_);
		}
	}

	@logs = ();
	if ($show[0] ne '') {
		@logs = @show;
	} else {
		$logs[0] = "0\t0\t\t\t\t\tNo Data\t該当する日記はありません。\t\t\t\t\t";
	}

	return @logs;
}

### 投稿フォーム表示
sub form {
	if ($in{'mode'} eq 'res') {
		%cookie = &get_cookie($cookie_id);
		if ($cookie{'url'} eq '') {
			$cookie{'url'} = 'http://';
		}
		if ($cookie{'color'} eq '') {
			$cookie{'color'} = $color[0];
		}

		$subj   = 'Re:' . (split(/\t/, $logs[0]))[6];
		$text   = '';
		$mode   = 'regist';
		$submit = '返信する';
	} else {
		%cookie = &get_cookie($cookie_id);

		$in{'no'}        = $reno;
		$cookie{'name'}  = $name;
		$cookie{'mail'}  = $mail;
		$cookie{'url'}   = $url;
		$text            =~ s/<br>/\n/g;
		$cookie{'color'} = $color;
		$cookie{'pwd'}   = $in{'user_pwd'};
		$mode            = 'userdel';
		$submit          = '修正する';
	}

	print <<"_HTML_";
<br>
<div class="form">
	<form action="$script" method="post" enctype="multipart/form-data">
		<table class="form_table">
			<tr>
				<td class="form_table_head">名前</td>
				<td class="form_table_data"><input type="text" name="name" size="32" value="$cookie{'name'}"></td>
			</tr>
			<tr>
				<td class="form_table_head">Ｅメール</td>
				<td class="form_table_data"><input type="text" name="mail" size="32" value="$cookie{'mail'}"></td>
			</tr>
			<tr>
				<td class="form_table_head">ＵＲＬ</td>
				<td class="form_table_data">
					<input type="text" name="url" size="32" value="$cookie{'url'}">
					<input type="hidden" name="subj" value="$subj">
					<input type="hidden" name="mode" value="$mode">
					<input type="hidden" name="no" value="$no">
					<input type="hidden" name="date" value="$date">
					<input type="hidden" name="reno" value="$in{'no'}">
					<input type="hidden" name="user_pwd" value="$in{'user_pwd'}">
					<input type="submit" value="$submit" class="submit">
					<input type="reset" value="リセット" class="reset">
				</td>
			</tr>
			<tr>
				<td class="form_table_head">本文</td>
				<td class="form_table_data"><textarea name="text" cols="50" rows="6" wrap="soft">$text</textarea></td>
			</tr>
_HTML_

	if ($icon_mode == 1) {
		print <<"_HTML_";
			<tr>
				<td class="form_table_head">アイコン</td>
				<td class="form_table_data">
					<select name="icon">
						<option value="">なし</option>
_HTML_

		@new = ();
		foreach (@icon) {
			if ($_ ne '') {
				push(@new, $_);
			}
		}
		@icon = @new;

		while (scalar(@icon) != 0) {
			$icon_file = shift(@icon);
			$icon_name = shift(@icon);
			if ($cookie{'icon'} eq $icon_file) {
				print qq|<option value="$icon_file" selected>$icon_name</option>\n|;
			} else {
				print qq|<option value="$icon_file">$icon_name</option>\n|;
			}
		}

		print <<"_HTML_";
					</select>
					<a href="$script?mode=icon">アイコン一覧</a>
				</td>
			</tr>
_HTML_
	}

	if ($color_mode == 1) {
		print <<"_HTML_";
			<tr>
				<td class="form_table_head">本文の色</td>
				<td class="form_table_data">
_HTML_

		foreach (@color) {
			if ($_ eq '') {
				next;
			}

			if ($cookie{'color'} eq $_) {
				print qq|<input type="radio" name="color" id="radio_$_" value="$_" class="radio" checked><label for="radio_$_" style="color:$_;">■</label> / |;
			} else {
				print qq|<input type="radio" name="color" id="radio_$_" value="$_" class="radio"><label for="radio_$_" style="color:$_;">■</label> / |;
			}
		}

		print <<"_HTML_";
				</td>
			</tr>
_HTML_
	}

	print <<"_HTML_";
			<tr>
				<td class="form_table_head">削除キー</td>
				<td class="form_table_data"><input type="password" name="pwd" size="20" value="$cookie{'pwd'}"></td>
			</tr>
			<tr>
				<td align="right" colspan="2" class="form_table_data">
_HTML_

	if ($show_preview == 0 || $in{'mode'} eq 'userdel') {
		print qq|<input type="hidden" name="preview" value="off">\n|;
	} elsif ($show_preview == 1) {
		if ($cookie{'preview'} eq 'on') {
			print qq|<input type="checkbox" name="preview" id="checkbox_preview" value="on" class="checkbox" checked>|;
		} else {
			print qq|<input type="checkbox" name="preview" id="checkbox_preview" value="on" class="checkbox">|;
		}
		print ' <label for="checkbox_preview">プレビューを表示</label>';
	} else {
		print qq|<input type="hidden" name="preview" value="on">\n|;
	}

	print <<"_HTML_";
		</table>
	</form>
</div>
_HTML_

	return;
}

### 返信フォーム表示
sub res {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません");
	@logs = <FILE>;
	close(FILE);

	@res = ();
	foreach (@logs) {
		($no, $reno) = split(/\t/);
		if ($in{'no'} == $no || $in{'no'} == $reno) {
			push(@res, $_);
		}
	}
	@logs = @res;

	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>返信フォーム</h2>
_HTML_

	&showlog(@logs);
	&form();

	&footer();
}

### 投稿画像表示
sub img {
	print <<"_HTML_";
Content-Type: text/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 http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>IMAGE WINDOW</title>
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<a href="javascript:void(0)" onClick="window.close()">
  <img src="$upfile_url$in{'file'}" alt="クリックするとウインドウを閉じます" border="0" width="$in{'width'}" height="$in{'height'}">
</a>
</body>

</html>
_HTML_

	exit;
}

### アイコン一覧
sub icon {
	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>アイコン一覧</h2>
<p>
	■ 選択できるアイコンは以下のとおりです。<br>
	<br>
	<table align="center" border="1" class="table">
_HTML_

	@new = ();
	foreach (@icon) {
		if ($_ ne '') {
			push(@new, $_);
		}
	}
	@icon = @new;

	$icon_no = ($#icon + 1) / 2;
	foreach (1 .. $icon_no) {
		if (($_ % 5) == 1) {
			print "<tr>\n";
		}

		$icon_file = shift(@icon);
		$icon_name = shift(@icon);
		print qq|<th valign="bottom" width="20%" class="table_data"><img src="$iconfile_dir$icon_file" alt="$icon_name"><br><br>$icon_name</th>\n|;

		if (($_ % 5) == 0) {
			print "</tr>\n";
		}

		if ($_ == $icon_no) {
			foreach (1 .. (5 - ($_ % 5))) {
				print qq|<th class="table_data">-</th>\n|;
			}
			print "</tr>\n";
		}
	}

	print <<"_HTML_";
	</table>
</p>
<hr>
_HTML_

	&footer();
}

### タイトル一覧
sub loglist {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません");
	@logs = <FILE>;
	close(FILE);

	$parent = 0;
	foreach (@logs) {
		if ((split(/\t/))[1] == 0) {
			$parent++;
		}
	}

	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>タイトル一覧</h2>
<p>
_HTML_

	if ($icon_display != 0) {
		print qq|<table>\n|;
	}

	$start = $in{'page'} * $list_size;
	$end   = $start + $list_size;

	$i = 0;
	foreach (@logs) {
		($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($reno == 0) {
			$i++;
		}
		if ($reno != 0 || $i <= $start) {
			next;
		}
		if ($i > $end) {
			last;
		}

		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
			($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
			$today = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);
			if ($new_date != 0 && $today - $date < $new_date) {
				$new = $new_mark;
			} else {
				$new = '';
			}

			$year  = sprintf("%04d", $1);
			$month = sprintf("%02d", $2);
			$day   = sprintf("%02d", $3);

			$week = $week[&getweek($day, $year, $month)];
			$date = qq|$year年$month月$day日（$week）|;
		}

		if ($icon_display != 0) {
			print qq|<tr><td width="1%">|;
			if ($icon ne '') {
				print qq|<img src="$iconfile_dir$icon" alt="$icon">|;
			}
			print qq|</td><td>|;
		}

		print qq|<a href="$script?mode=view&no=$i">$date</a> &nbsp; $subj $new<br>\n|;

		if ($icon_display != 0) {
			print qq|</td></tr>|;
		}
	}

	if ($icon_display != 0) {
		print qq|</table>\n|;
	}

	print '</p>';

	print qq|<div class="link">ページ移動 /|;
	foreach (0 .. int(($parent - 1) / $list_size)) {
		$page_info = $_ + 1;
		if ($in{'page'} == $_) {
			print qq| [$page_info]|;
		} else {
			print qq| [<a href="$script?mode=loglist&page=$_">$page_info</a>]|;
		}
	}
	print qq|</div><hr>\n|;

	&footer();
}

### 個別表示
sub view {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません");
	@logs = <FILE>;
	close(FILE);

	&header();

	@show = ();
	$i = 1;
	$pno = 0;
	foreach (@logs) {
		($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($in{'no'} == $i || ($reno != 0 && $reno == $pno)) {
			push(@show, $_);
			if ($in{'no'} == $i) {
				$pno = $no;
			}
		}
		if ($reno == 0) {
			$i++;
		}
	}

	if ($in{'mode'} eq '') {
		$view = qq|<a href="$script?mode=new">以前の日記を表\示</a>|;
	} else {
		$prev_view = '↓前の日記';
		$next_view = '次の日記↑';
		$view = '/';

		if ($in{'no'} != $i - 1) {
			$prev_no = $in{'no'} + 1;
			$prev_view = qq|<a href="$script?mode=view&no=$prev_no">$prev_view</a>\n|;
		}
		if ($in{'no'} != 1) {
			$next_no = $in{'no'} - 1;
			$next_view = qq|<a href="$script?mode=view&no=$next_no">$next_view</a>\n|;
		}
	}

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>個別表\示</h2>
_HTML_

	&showlog(@show);

	print qq|<p class="navi">$prev_view $view $next_view</p><hr>\n|;

	&footer();
}

### ログ検索
sub search {
	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>ログ検索</h2>
<p>
	■ 検索したい<strong>キーワード</strong>を入力し、<strong>検索</strong>ボタンを押してください。<br>
	■ キーワードは<strong>半角スペース</strong>で区切って複数指定することができます。<br>
	<br>
	<form action="$script" method="get" enctype="multipart/form-data">
		<table>
			<tr>
				<td>キーワード：</td>
				<td><input type="text" name="word" size="30" value="$in{'word'}"></td>
			</tr>
			<tr>
				<td>検索条件：</td>
				<td>
					<select name="cond">
_HTML_

	if ($in{'cond'} eq '' || $in{'cond'} eq 'and') {
		print qq|<option value="and" selected>AND検索</option><option value="or">OR検索</option>|;
	} else {
		print qq|<option value="and">AND検索</option><option value="or" selected>OR検索</option>|;
	}

	print <<"_HTML_";
					</select>
				</td>
			</tr>
_HTML_

	if ($make_past == 1) {
		print <<"_HTML_";
			<tr>
				<td>検索対象：</td>
				<td>
					<select name="obje">
_HTML_

			if ($in{'obje'} eq 'now' || $in{'obje'} eq '') {
				print qq|<option value="now" selected>現行ログ</option><option value="past">過去ログ</option><option value="all">すべてのログ</option>\n|;
			} elsif ($in{'obje'} eq 'past') {
				print qq|<option value="now">現行ログ</option><option value="past" selected>過去ログ</option><option value="all">すべてのログ</option>\n|;
			} else {
				print qq|<option value="now">現行ログ</option><option value="past">過去ログ</option><option value="all" selected>すべてのログ</option>\n|;
			}

			print <<"_HTML_";
					</select>
				</td>
			</tr>
_HTML_
	}

	print <<"_HTML_";
			<tr>
				<td colspan="2">
					<label for="checkbox_dis">大文字、小文字を区別しない</label>
_HTML_

	if ($in{'string'} eq 'dis') {
		print qq|<input type="checkbox" name="string" id="checkbox_dis" value="dis" class="checkbox" checked>\n|;
	} else {
		print qq|<input type="checkbox" name="string" id="checkbox_dis" value="dis" class="checkbox">\n|;
	}

	print <<"_HTML_";
				</td>
			</tr>
_HTML_

	print <<"_HTML_";
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="hidden" name="mode" value="search">
					<input type="submit" value="検索する" class="submit">
					<input type="reset" value="リセット" class="reset">
				</td>
			</tr>
		</table>
	</form>
</p>
_HTML_

	if ($in{'word'} ne '' || $in{'no'} ne '') {
		if ($make_past == 0 || $in{'obje'} ne 'past') {
			open(FILE, "$log_file") || &error("ログファイルを読み出せません");
			@search = <FILE>;
			close(FILE);
		}
		if ($make_past == 1 && $in{'obje'} ne 'now') {
			opendir(DIR, "$past_dir") || &error("過去ログディレクトリを読み出せません");
			@dir = readdir(DIR);
			closedir(DIR);

			@dir = sort {$b <=> $a} (@dir);

			@past = ();
			foreach (@dir) {
				if ($_ =~ /^\d+(\.\w+)$/ && $1 eq $past_ext) {
					open(FILE, "$past_dir$_") || &error("過去ログファイルを読み出せません");
					@file = <FILE>;
					close(FILE);

					push(@past, @file);
				}
			}

			if ($in{'obje'} eq 'all') {
				push(@search, @past);
			} elsif($in{'obje'} eq 'past') {
				@search = @past;
			}
		}

		if ($in{'word'} ne '') {
			$in{'word'} =~ s/　/ /g;
			@pairs = split(/\s+/, $in{'word'});

			@logs = ();
			foreach (@search) {
				$string = $_;
				$flag = 0;
				foreach $pair (@pairs) {
					if ($in{'string'} eq 'dis') {
						$string =~ tr/A-Z/a-z/;
						$pair   =~ tr/A-Z/a-z/;
					}
					if (index($string, $pair) >= 0) {
						$flag = 1;
						if ($in{'cond'} eq 'or') { last; }
					} else {
						if ($in{'cond'} eq 'and') { $flag = 0; last; }
					}
				}
				if ($flag != 0) { push(@logs, $_); }
			}

			print qq|<div class="link">ページ移動 /|;
			foreach (0 .. int($#logs / $page_size)) {
				$page_info = $_ + 1;
				if ($in{'page'} == $_) {
					print qq| [$page_info]|;
				} else {
					$inword = $in{'word'};
					$inword =~ s/(\W)/'%' . unpack('H2',$1)/ge;
					print qq| [<a href="$script?mode=search&word=$inword&obje=$in{'obje'}&page=$_">$page_info</a>]|;
				}
			}
			print qq|</div><hr><br>\n|;
		} elsif ($in{'no'} ne '') {
			@logs = ();
			foreach (@search) {
				($no, $reno) = split(/\t/);
				if (($reno == 0 && $in{'no'} == $no) || ($reno != 0 && $in{'no'} == $reno)) {
					push(@logs, $_);
				}
			}
			print qq|<hr><br>\n|;
		}

		$default_show = $page_size;
		&showlog(@logs);
	}

	print '<hr>';

	&footer();
}

### ユーザー記事削除
sub userdel {
	if ($in{'prev'} ne '') { $in{'page'}--; }
	if ($in{'next'} ne '') { $in{'page'}++; }

	if ($in{'delete'} ne '') {
		if ($ENV{'REQUEST_METHOD'} ne 'POST') {
			&error("不正なアクセスです");
		}

		if ($ENV{'HTTP_REFERER'} ne '' && $base_url ne '' && $ENV{'HTTP_REFERER'} !~ /^$base_url/i) {
			&error("不正なアクセスです");
		}

		if ($in{'user_pwd'} eq '') {
			&error("削除キーを入力してください");
		}
		if ($in{'del'} eq '') {
			&error("削除したい記事を選択してください");
		}

		&lock($lock_file);

		open(FILE, "$log_file") || &error("ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		@new = ();
		$flag = 0;
		$del_list = '';
		foreach (@logs) {
			($no, $reno, $pwd) = (split(/\t/))[0, 1, 11];

			if ($flag == 0 && $in{'del'} == $no) {
				&decrypt($in{'user_pwd'}, $pwd);
				$flag = 1;
			} elsif ($flag == 1 && $in{'del'} == $reno) {
				$flag = 1;
			} else {
				$flag = 0;
			}
			if ($flag == 0) {
				push(@new, $_);
			} else {
				$del_list .= " [$no]";
			}
		}
		$work_mesg = "$del_list番の記事を削除しました";

		open(FILE, ">$log_file") || &error("ログファイルに書き込めません");
		print FILE @new;
		close(FILE);

		if ($js_mode != 0) {
			&output_jsfile(@new);
		}

		&unlock($lock_file);
	}
	if ($edit_mode == 1 && $in{'modify'} ne '') {
		if ($ENV{'REQUEST_METHOD'} ne 'POST') {
			&error("不正なアクセスです");
		}

		if ($ENV{'HTTP_REFERER'} ne '' && $base_url ne '' && $ENV{'HTTP_REFERER'} !~ /^$base_url/i) {
			&error("不正なアクセスです");
		}

		if ($in{'user_pwd'} eq '') {
			&error("削除キーを入力してください");
		}
		if ($in{'modi'} eq '') {
			&error("修正したい記事を選択してください");
		}

		open(FILE, "$log_file") || &error("ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		$flag = 0;
		foreach (@logs) {
			chop($_);
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);
			if ($in{'modi'} == $no) {
				&decrypt($in{'user_pwd'}, $pwd);
				$flag = 1;
				last;
			}
		}
		if ($flag != 1) {
			&error("該当する記事が見つかりません");
		}

		&header();

		print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>$page_info</h2>
<p>
	■ 記事を修正して、<strong>修正</strong>ボタンを押してください。<br>
</p>
_HTML_

		&form();

		&footer();
	}
	if ($edit_mode == 1 && $in{'no'} ne '') {
		&regist_check();

		&lock($lock_file);

		open(FILE, "$log_file") || &error("ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		if ($in{'subj'} eq '') {
			$in{'subj'} = '無題';
		}
		if ($in{'url'} eq 'http://') {
			$in{'url'} = '';
		}
		if ($in{'pwd'} ne '') {
			$in{'pwd'} = &encrypt($in{'pwd'});
		}

		$line = "$in{'no'}\t$in{'reno'}\t$in{'date'}\t$in{'name'}\t$in{'mail'}\t$in{'url'}\t$in{'subj'}\t$in{'text'}\t$in{'color'}\t$in{'icon'}\t$in{'file'}\t$in{'pwd'}\t$host\n";

		@new = ();
		foreach (@logs) {
			if ($in{'no'} == ($_ + 0)) {
				push(@new, $line);
			} else {
				push(@new, $_);
			}
		}

		$work_mesg = "[$in{'no'}] 番の記事を修正しました";

		open(FILE, ">$log_file") || &error("ログファイルに書き込めません");
		print FILE @new;
		close(FILE);

		if ($js_mode != 0) {
			&output_jsfile(@new);
		}

		&unlock($lock_file);
	}

	open(FILE, "$log_file") || &error("ログファイルを読み出せません");
	@logs = <FILE>;
	close(FILE);

	$parent = 0;
	foreach (@logs) {
		if ((split(/\t/))[1] == 0) {
			$parent++;
		}
	}

	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>記事編集</h2>
<p>
	■ <strong>削除キー</strong>を入力して削除したい記事にチェックを入れ、<strong>削除</strong>ボタンを押してください。<br>
_HTML_

	if ($edit_mode == 1) {
		print qq|■ <strong>削除キー</strong>を入力して修正したい記事にチェックを入れ、<strong>修正</strong>ボタンを押してください。<br>\n|;
	}

	print <<"_HTML_";
	■ 投稿時に削除キーを設定していなければ記事を削除することはできません。<br>
	<br>
	<form action="$script" method="post" enctype="multipart/form-data">
		<input type="hidden" name="mode" value="userdel">
		<input type="hidden" name="page" value="$in{'page'}">
		<input type="password" name="user_pwd" size="15">
		<input type="submit" name="delete" value="削除する" class="submit">
_HTML_

	if ($edit_mode == 1) {
		print qq|<input type="submit" name="modify" value="修正する" class="submit">\n|;
	}

	print <<"_HTML_";
		<input type="reset" value="リセット" class="reset"><br>
		<br>
		スレッド数 <strong>$parent</strong>件 / $work_mesg<br>
		<br>
		<table border="1" class="table">
			<tr>
				<th class="table_head">削除</th>
_HTML_

	if ($edit_mode == 1) {
		print <<"_HTML_";
				<th class="table_head">修正</th>
_HTML_
	}

	print <<"_HTML_";
				<th class="table_head">No</th>
				<th class="table_head">投稿者</th>
				<th class="table_head">題名</th>
				<th class="table_head">本文</th>
			</tr>
_HTML_

	$start = $in{'page'} * $page_size;
	$end   = $start + $page_size;

	$i = 0;
	foreach (@logs) {
		($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($reno == 0) {
			$i++;
		}
		if ($i <= $start) {
			next;
		}
		if ($i > $end) {
			last;
		}

		if ($reno == 0) {
			$name = '管理者';
			$subj = "<strong>$subj</strong>";
		}
		$text =~ s/<br>//gi;
		$text =~ s/</&lt;/g;
		$text =~ s/>/&gt;/g;
		if (length($text) > 60) {
			$text = substr($text, 0, 60) . ' …';
		}

		if ($reno == 0) {
			print <<"_HTML_";
			<tr>
				<th class="table_data">-</th>
_HTML_
		} else {
			print <<"_HTML_";
			<tr>
				<th class="table_data"><input type="radio" name="del" value="$no" class="radio"></th>
_HTML_
		}

		if ($edit_mode == 1) {
			if ($reno == 0) {
				print <<"_HTML_";
				<th class="table_data">-</th>
_HTML_
			} else {
				print <<"_HTML_";
				<th class="table_data"><input type="radio" name="modi" value="$no" class="radio"></th>
_HTML_
			}
		}

		print <<"_HTML_";
				<td class="table_data">$no</td>
				<td class="table_data">$name</td>
				<td class="table_data">$subj</td>
				<td class="table_data">$text</td>
			</tr>
_HTML_
	}

	print <<"_HTML_";
		</table>
		<br>
		<div class="navi">
_HTML_

	if ($in{'page'} > 0) {
		print qq|<input type="submit" name="prev" value="↑前のページ" class="submit">\n|;
	}
	if (int(($parent - 1) / $page_size) > $in{'page'}) {
		print qq|<input type="submit" name="next" value="↓次のページ" class="submit">\n|;
	}

	print <<"_HTML_";
		</div>
	</form>
</p>
<hr>
_HTML_

	&footer();
}

### 過去ログ表示
sub past {
	opendir(DIR, "$past_dir") || &error("過去ログディレクトリを読み出せません");
	@dir = readdir(DIR);
	closedir(DIR);

	@dir = sort {$a <=> $b} (@dir);

	$past_byte = 0;
	foreach (@dir) {
		if ($_ =~ /^\d+(\.\w+)$/ && $1 eq $past_ext) {
			$past_byte += (stat("$past_dir$_"))[7];
		}
	}

	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>過去ログ</h2>
<p>
	■ 表\示したい過去ログを選択し、<strong>表\示</strong>ボタンを押してください。<br>
	■ <a href="$script?mode=search">ログ検索</a>によって過去ログを検索することができます。<br>
	<br>
	<form action="$script" method="post" enctype="multipart/form-data">
		<select name="view">
			<option value="">過去ログを選択してください</option>
_HTML_

	if ($past_byte == 0) {
		print qq|<option value="">過去ログはありません</option>\n|;
	} else {
		$i = 1;
		foreach (@dir) {
			if ($_ =~ /^\d+(\.\w+)$/ && $1 eq $past_ext) {
				if ($in{'view'} eq $_) {
					print qq|<option value="$_" selected>過去ログ$iページ</option>\n|;
				} else {
					print qq|<option value="$_">過去ログ$iページ</option>\n|;
				}
				$i++;
			}
		}
	}

	print <<"_HTML_";
		</select>
		<input type="hidden" name="mode" value="past">
		<input type="submit" value="表\示する" class="submit">
	</form>
</p>
_HTML_

	if ($in{'view'} ne '' && $in{'view'} =~ /^\d+\.\w+$/ && -e "$past_dir$in{'view'}") {
		open(FILE, "$past_dir$in{'view'}") || &error("過去ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		if ($past_show == 1) {
			if ($in{'no'} ne '') {
				@show = ();
				foreach (@logs) {
					($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

					if ($in{'no'} == $no) {
						push(@show, $_);
						last;
					}
				}

				print '<hr><br>';
				&showlog(@show);
			} else {
				print '<hr><p>';

				if ($icon_display != 0) {
					print qq|<table>\n|;
				}

				foreach (@logs) {
					($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

					if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
						($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());

						$year  = sprintf("%04d", $1);
						$month = sprintf("%02d", $2);
						$day   = sprintf("%02d", $3);

						$week = $week[&getweek($day, $year, $month)];
						$date = qq|$year年$month月$day日（$week）|;
					}

					if ($icon_display != 0) {
						print qq|<tr><td width="1%">|;
						if ($icon ne '') {
							print qq|<img src="$iconfile_dir$icon" alt="$icon">|;
						}
						print qq|</td><td>|;
					}

					print qq|<a href="$script?mode=past&view=$in{'view'}&no=$no">$date</a> &nbsp; $subj<br>\n|;

					if ($icon_display != 0) {
						print qq|</td></tr>|;
					}
				}

				if ($icon_display != 0) {
					print qq|</table>\n|;
				}

				print '</p>';
			}
		} else {
			print '<hr><br>';
			&showlog(@logs);
		}
	}

	print '<hr>';

	&footer();
}

### 管理者用
sub admin {
	if ($in{'admin_pwd'} eq '') {
		&header();

		print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<p>
	<div align="center">
		↓パスワードは「kai」です↓<br>
		<br>
		<form action="$script" method="post" enctype="multipart/form-data">
			<input type="password" name="admin_pwd" size="15">
			<input type="hidden" name="mode" value="admin">
			<input type="submit" value="認証" class="submit">
		</form>
	</div>
</p>
_HTML_

		&footer();
	} else {
		if ($ENV{'REQUEST_METHOD'} ne 'POST') {
			&error("不正なアクセスです");
		}

		if ($ENV{'HTTP_REFERER'} ne '' && $base_url ne '' && $ENV{'HTTP_REFERER'} !~ /^$base_url/i) {
			&error("不正なアクセスです");
		}

		if ($in{'admin_pwd'} ne $admin_pwd) {
			&error("パスワードが違います");
		}

		if ($in{'prev'} ne '') { $in{'page'}--; }
		if ($in{'next'} ne '') { $in{'page'}++; }

		if ($make_past == 1 && ($in{'create_log'} ne '' || $in{'read_log'} ne '')) {
			&lock($lock_file);

			opendir(DIR, "$past_dir") || &error("過去ログディレクトリを読み出せません");
			@dir = readdir(DIR);
			closedir(DIR);

			@dir = sort {$a <=> $b} (@dir);

			open(FILE, "$log_file") || &error("ログファイルを読み出せません");
			@logs = <FILE>;
			close(FILE);

 			if ($in{'create_log'} ne '') {
				$dir[$#dir] =~ /([^\/\\]*)\.([^.\/\\]*)$/;
				$past_file = ($1 + 1) . $past_ext;
				@past = ();

				(*logs, *past) = &move_log(*logs, *past, $past_size);

				open(FILE, ">$past_dir$past_file") || &error("過去ログファイルを作成できません");
				print FILE @past;
				close(FILE);

				chmod(0666, "$past_dir$past_file");

				$work_mesg = "スレッド$past_size件を過去ログに移動しました";
			}
			if ($in{'read_log'} ne '') {
				$past_file = $dir[$#dir];

				open(FILE, "$past_dir$past_file") || &error("過去ログファイルを読み出せません");
				@past = <FILE>;
				close(FILE);

				(*logs, *past) = &move_log(*logs, *past, 're');

				unlink("$past_dir$past_file");

				$work_mesg = "スレッド$past_size件を過去ログから読み戻しました";
			}

			open(FILE, ">$log_file") || &error("ログファイルに書き込めません");
			print FILE @logs;
			close(FILE);

			if ($js_mode != 0) {
				&output_jsfile(@logs);
			}

			&unlock($lock_file);
		}

		if ($in{'delete'} ne '') {
			if ($delete[0] eq '') {
				&error("削除したい日記を選択してください");
			}

			&lock($lock_file);

			open(FILE, "$log_file") || &error("ログファイルを読み出せません");
			@logs = <FILE>;
			close(FILE);

			@new = ();
			$del_list = '';
			foreach (@logs) {
				($no, $reno, $file) = (split(/\t/))[0, 1, 10];
				$flag = 0;
				foreach $del (@delete) {
					if ($no == $del || $reno == $del) { $flag = 1; last; }
				}
				if ($flag == 0) {
					push(@new, $_);
				} else {
					$del_list .= " [$no]";
					if (-e "$upfile_dir$file") {
						unlink("$upfile_dir$file");
					}
				}
			}
			$work_mesg = "$del_list番の日記を削除しました";

			open(FILE, ">$log_file") || &error("ログファイルに書き込めません");
			print FILE @new;
			close(FILE);

			if ($js_mode != 0) {
				&output_jsfile(@new);
			}

			&unlock($lock_file);
		}

		if ($in{'download'} ne '') {
			open(FILE, "$log_file") || &error("ログファイルを読み出せません");
			@logs = <FILE>;
			close(FILE);

			print "Content-type: text/download\n\n";
			print @logs;
			exit;
		}

		if ($in{'form'} ne '' || $in{'modify'} ne '') {
			if ($in{'form'} ne '') {
				%cookie = &get_cookie($admin_cookie_id);
				if ($cookie{'color'} eq '') {
					$cookie{'color'} = $color[0];
				}

				$page_info  = '日記を書く';
				$form_info  = '書き込む';
				$input_mode = 'regist';

				($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
				$this_year  = sprintf("%04d", $year + 1900);
				$this_month = sprintf("%02d", $mon + 1);
				$this_day   = sprintf("%02d", $day);
			} else {
				if ($in{'modi'} eq '') {
					&error("修正したい日記を選択してください");
				}

				open(FILE, "$log_file") || &error("ログファイルを読み出せません");
				@logs = <FILE>;
				close(FILE);

				$flag = 0;
				foreach (@logs) {
					chop($_);
					($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);
					if ($in{'modi'} == $no) { $flag = 1; last; }
				}
				if ($flag != 1) {
					&error("該当する日記が見つかりません");
				}

				$page_info  = '日記を修正する';
				$form_info  = '修正する';
				$input_mode = 'modi';

				if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
					$this_year  = sprintf("%04d", $1);
					$this_month = sprintf("%02d", $2);
					$this_day   = sprintf("%02d", $3);
				} else {
					($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
					$this_year  = sprintf("%04d", $year + 1900);
				}

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

				$cookie{'color'} = $color;
				$cookie{'icon'}  = $icon;

				$file =~ /([^\/\\]*)\.([^.\/\\]*)$/;
				$ext = $2;
				$ext =~ tr/A-Z/a-z/;
			}

			&header();

			print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>$page_info</h2>
<p class="form">
_HTML_

			if ($upload_mode == 1 && $in{'mode'} eq 'admin' && $in{'form'} ne '') {
				$insert_data = qq| onSubmit="return insertData()"|;
			}

			if ($file ne '') {
				print <<"_HTML_";
	■ <strong>ファイル</strong>の項目を入力しなければ、以前の内容が引き続いて使用されます。<br>
	<br>
_HTML_
			}

			print <<"_HTML_";
	<form action="$script" method="post" enctype="multipart/form-data" name="form"$insert_data>
		<table class="form_table">
			<tr>
				<td class="form_table_head">題名</td>
				<td class="form_table_data"><input type="text" name="subj" size="32" value="$subj"></td>
			</tr>
			<tr>
				<td class="form_table_head">日付</td>
				<td class="form_table_data">
_HTML_

			if ($year_mode == 1) {
				print qq|<input type="text" name="year" size="6" value="$this_year">\n|;
			} else {
				print qq|<select name="year">\n|;
				foreach (1 .. 5) {
					if ($_ == 1) {
						$show_year = $this_year - 2;
						print qq|<option value="$show_year">$show_year年</option>\n|;
					} elsif ($_ == 2) {
						$show_year = $this_year - 1;
						print qq|<option value="$show_year">$show_year年</option>\n|;
					} elsif ($_ == 3) {
						print qq|<option value="$this_year" selected>$this_year年</option>\n|;
					} elsif ($_ == 4) {
						$show_year = $this_year + 1;
						print qq|<option value="$show_year">$show_year年</option>\n|;
					} elsif ($_ == 5) {
						$show_year = $this_year + 2;
						print qq|<option value="$show_year">$show_year年</option>\n|;
					}
				}
				print qq|</select>\n|;
			}

			print qq|<select name="month">\n|;
			foreach (1 .. 12) {
				$month = sprintf("%02d", $_);
				if ($month == $this_month) {
					print qq|<option value="$month" selected>$_月</option>\n|;
				} else {
					print qq|<option value="$month">$_月</option>\n|;
				}
			}
			print qq|</select>\n|;

			print qq|<select name="day">\n|;
			foreach (1 .. 31) {
				$day = sprintf("%02d", $_);
				if ($day == $this_day) {
					print qq|<option value="$day" selected>$_日</option>\n|;
				} else {
					print qq|<option value="$day">$_日</option>\n|;
				}
			}
			print qq|</select>\n|;

			print <<"_HTML_";
				</td>
			</tr>
_HTML_

			if ($icon_mode == 1) {
				print <<"_HTML_";
			<tr>
				<td class="form_table_head">アイコン</td>
				<td colspan="2" class="form_table_data">
					<select name="icon">
						<option value="">なし</option>
_HTML_

				if ($admin_icon_mode == 1) {
					push(@icon, @admin_icon);
				}

				@new = ();
				foreach (@icon) {
					if ($_ ne '') {
						push(@new, $_);
					}
				}
				@icon = @new;

				while (scalar(@icon) != 0) {
					$icon_file = shift(@icon);
					$icon_name = shift(@icon);
					if ($cookie{'icon'} eq $icon_file) {
						print qq|<option value="$icon_file" selected>$icon_name</option>\n|;
					} else {
						print qq|<option value="$icon_file">$icon_name</option>\n|;
					}
				}

				print <<"_HTML_";
					</select>
					<a href="$script?mode=icon">アイコン一覧</a>
				</td>
			</tr>
_HTML_
			}

			if ($color_mode == 1) {
				print <<"_HTML_";
			<tr>
				<td class="form_table_head">本文の色</td>
				<td colspan="2" class="form_table_data">
_HTML_

				foreach (@color) {
					if ($_ eq '') {
						next;
					}

					if ($cookie{'color'} eq $_) {
						print qq|<input type="radio" name="color" id="radio_$_" value="$_" class="radio" checked><label for="radio_$_" style="color:$_;">■</label> / |;
					} else {
						print qq|<input type="radio" name="color" id="radio_$_" value="$_" class="radio"><label for="radio_$_" style="color:$_;">■</label> / |;
					}
				}

				print <<"_HTML_";
				</td>
			</tr>
_HTML_
			}

			if ($upload_mode == 1) {
				print <<"_HTML_";
			<tr>
				<td class="form_table_head">ファイル</td>
				<td class="form_table_data"><input type="file" name="file" size="32"></td>
			</tr>
_HTML_
			}

			print <<"_HTML_";
			<tr>
				<td class="form_table_head">本文</td>
				<td class="form_table_data"><textarea name="text" cols="50" rows="17" wrap="soft">$text</textarea></td>
			</tr>
			<tr>
				<td colspan="2" class="form_table_data">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="2" class="form_table_data">
					<input type="hidden" name="mode" value="admin">
					<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
					<input type="hidden" name="no" value="$no">
					<input type="hidden" name="orgfile" value="$file">
					<input type="hidden" name="ext" value="$file">
					<input type="hidden" name="host" value="$host">
					<input type="submit" name="$input_mode" value="$form_info" class="submit">
					<input type="reset" value="リセット" class="reset">
				</td>
			</tr>
			<tr>
				<td align="right" colspan="2" class="form_table_data">
_HTML_

			if ($show_preview == 0 || $in{'modify'} ne '') {
				print qq|<input type="hidden" name="preview" value="off">\n|;
			} elsif ($show_preview == 1) {
				if ($cookie{'preview'} eq 'on') {
					print qq|<input type="checkbox" name="preview" id="checkbox_preview" value="on" class="checkbox" checked>|;
				} else {
					print qq|<input type="checkbox" name="preview" id="checkbox_preview" value="on" class="checkbox">|;
				}
				print ' <label for="checkbox_preview">プレビューを表示</label>';
			} else {
				print qq|<input type="hidden" name="preview" value="on">\n|;
			}

			print <<"_HTML_";
				</td>
			</tr>
_HTML_

			if ($file ne '') {
				print <<"_HTML_";
			<tr>
				<td align="right" colspan="2" class="form_table_data">
					<input type="checkbox" name="delfile" id="checkbox_delfile" value="on" class="checkbox"> <label for="checkbox_delfile">添付ファイルを削除</label>
				</td>
			</tr>
_HTML_
			}

			print <<"_HTML_";
		</table>
	</form>
</p>
<hr>
_HTML_

			&footer();
		}

		if ($in{'regist'} ne '' || $in{'modi'} ne '') {
			$in{'subj'} = &create_line($in{'subj'});
			$in{'text'} = &create_text($in{'text'});

			$in{'text'} =~ s/&amp;/&/g;
			$in{'text'} =~ s/&lt;/</g;
			$in{'text'} =~ s/&gt;/>/g;
			$in{'text'} =~ s/&quot;/"/g;

			if ($in{'date'} eq '') {
				$in{'date'} = "$in{'year'}$in{'month'}$in{'day'}";
			}

			if ($in{'date'} !~ /^\d\d\d\d\d\d\d\d$/) {
				&error("日付が不正です");
			}
			if ($in{'subj'} eq '') {
				&error("題名が入力されていません");
			}
			if ($in{'text'} eq '') {
				&error("本文が入力されていません");
			}

			if ($in{'preview'} eq 'on') {
				&preview();
			}

			$cookie{'color'}   = $in{'color'};
			$cookie{'icon'}    = $in{'icon'};
			$cookie{'preview'} = $in{'c_preview'};

			&set_cookie(*cookie, $admin_cookie_id, $hold_days);

			open(FILE, "$log_file") || &error("ログファイルを読み出せません");
			@logs = <FILE>;
			close(FILE);

			if ($in{'regist'} ne '') {
				$host = $ENV{'REMOTE_HOST'};
				$addr = $ENV{'REMOTE_ADDR'};
				if ($host eq "" || $host eq $addr) {
					if ($gethostbyaddr == 1) {
						$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2) || $addr;
					}
				}

				$no     = 0;
				$parent = 0;
				$subj = '';
				$text = '';
				foreach (@logs) {
					if ($_ + 0 > $no) {
						($no, $date, $subj, $text) = (split(/\t/))[0, 2, 6, 7];
					}
					if ((split(/\t/))[1] == 0) {
						$parent++;
					}
				}
				$no++;

				if ($in{'subj'} eq $subj && $in{'text'} eq $text) {
					&error("すでに書き込まれています");
				}

				if ($in{'file'} ne '') {
					$in{'file'} = &upfile($in{'file'}, $in{'filename'}, $no);
				} elsif ($in{'ext'} ne '' && -e "$upfile_dir$temporary_file") {
					$in{'file'} = "$no.$in{'ext'}";
					rename("$upfile_dir$temporary_file", "$upfile_dir$in{'file'}");
				}

				$line = "$no\t0\t$in{'date'}\t\t\t\t$in{'subj'}\t$in{'text'}\t$in{'color'}\t$in{'icon'}\t$in{'file'}\t\t$host\n";

				if ($make_past == 0) {
					foreach (0 .. ($parent - $max_log)) {
						($no, $reno) = split(/\t/, $logs[$#logs]);
						if ($reno == 0) {
							($file) = (split(/\t/, pop(@logs)))[10];
							if (-e "$upfile_dir$file") {
								unlink("$upfile_dir$file");
							}
						} else {
							while ((split(/\t/, $logs[$#logs]))[1] != 0) {
								($file) = (split(/\t/, pop(@logs)))[10];
								if (-e "$upfile_dir$file") {
									unlink("$upfile_dir$file");
								}
							}
							($file) = (split(/\t/, pop(@logs)))[10];
							if (-e "$upfile_dir$file") {
								unlink("$upfile_dir$file");
							}
						}
					}
				}

				@new  = ();
				$flag = 0;
				foreach (@logs) {
					($no, $reno, $date) = split(/\t/);
					if ($flag == 0 && $reno == 0 && $in{'date'} + 0 >= $date) {
						$flag = 1;
						push(@new, $line);
					}
					push(@new, $_);
				}
				if ($flag == 0) {
					push(@new, $line);
				}
				@logs = @new;

				$work_mesg = "日記を新規に書き込みました"
			} else {
				if ($in{'file'} ne '') {
					if (-e "$upfile_dir$in{'orgfile'}") {
						unlink("$upfile_dir$in{'orgfile'}");
					}
					$in{'file'} = &upfile($in{'file'}, $in{'filename'}, $in{'no'});
				} elsif ($in{'orgfile'} ne '' && $in{'delfile'} eq 'on') {
					if (-e "$upfile_dir$in{'orgfile'}") {
						unlink("$upfile_dir$in{'orgfile'}");
					}
					$in{'file'} = '';
				} elsif ($in{'orgfile'} ne '') {
					$in{'file'} = $in{'orgfile'};
				}

				$line = "$in{'no'}\t0\t$in{'date'}\t\t\t\t$in{'subj'}\t$in{'text'}\t$in{'color'}\t$in{'icon'}\t$in{'file'}\t\t$in{'host'}\n";

				@new = ();
				@tmp = ();
				foreach (@logs) {
					($no, $reno) = split(/\t/);
					if ($in{'no'} == $no) {
						push(@tmp, $line);
					} elsif ($in{'no'} == $reno) {
						push(@tmp, $_);
					} else {
						push(@new, $_);
					}
				}
				@logs = ();
				$flag = 0;
				foreach (@new) {
					($no, $reno, $date) = split(/\t/);
					if ($flag == 0 && $reno == 0 && $in{'date'} + 0 >= $date) {
						$flag = 1;
						push(@logs, @tmp);
					}
					push(@logs, $_);
				}
				if ($flag == 0) {
					push(@logs, @tmp);
				}

				$work_mesg = "$in{'no'}の日記を修正しました"
			}

			open(FILE, ">$log_file") || &error("ログファイルに書き込めません");
			print FILE @logs;
			close(FILE);

			if ($js_mode != 0) {
				&output_jsfile(@logs);
			}

			&unlock($lock_file);
		}

		if ($in{'download'} ne '') {
			open(FILE, "$log_file") || &error("ログファイルを読み出せません");
			@logs = <FILE>;
			close(FILE);

			print "Content-type: text/download\n\n";
			print @logs;
			exit;
		}

		open(FILE, "$log_file") || &error("ログファイルを読み出せません");
		@logs = <FILE>;
		close(FILE);

		$log_byte  = &comma(int((stat($log_file))[7] / 1024));
		if ($logs[0] ne '' && $log_byte == 0) {
			$log_byte = 1;
		}

		if ($make_past == 1) {
			opendir(DIR, "$past_dir") || &error("過去ログディレクトリを読み出せません");
			@dir = readdir(DIR);
			closedir(DIR);

			$past_byte = 0;
			foreach (@dir) {
				if ($_ =~ /^\d+(\.\w+)$/ && $1 eq $past_ext) {
					$past_byte += (stat("$past_dir$_"))[7];
				}
			}

			$past_byte = &comma(int($past_byte / 1024));
		}

		$parent = 0;
		foreach (@logs) {
			if ((split(/\t/))[1] == 0) {
				$parent++;
			}
		}

		if ($mini_counter != 0) {
			open(COUNT, "$count_log") || &error("カウントログファイルを読み出せません");
			$data = <COUNT>;
			close(COUNT);

			($sum, $today, $yesterday, $key) = split(/\t/, $data);
		}

		&header();

		print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>管理者用</h2>
<p>
	■ 削除したい日記にチェックを入れ、<strong>削除</strong>ボタンを押してください。<br>
	■ 修正したい日記にチェックを入れ、<strong>修正</strong>ボタンを押してください。<BR>
	■ <strong>ダウンロード</strong>ボタンを押すと、現在のログをダウンロードします。<br>
_HTML_

		if ($make_past == 1) {
			print qq|■ 日記が<strong>$past_size</strong>件以上になると<a href="$script?mode=past">過去ログ</a>を作成することができます。<br>\n|;
		} else {
			print qq|■ 日記が<strong>$max_log</strong>件以上になると古い日記から削除されます。<br>\n|;
		}

		print <<"_HTML_";
	<br>
	<form action="$script" method="post" enctype="multipart/form-data">
		<input type="hidden" name="mode" value="admin">
		<input type="hidden" name="page" value="$in{'page'}">
		<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
		<input type="submit" name="delete" value="削除する" class="submit">
		<input type="submit" name="modify" value="修正する" class="submit">
		<input type="reset" value="リセット" class="reset">
_HTML_

		if ($make_past == 1 && $parent > $past_size) {
			print qq|<input type="submit" name="create_log" value="過去ログ作成" class="submit">\n|;
		}
		if ($make_past == 1 && $past_byte > 0) {
			print qq|<input type="submit" name="read_log" value="過去ログ読戻" class="submit">\n|;
		}

		print <<"_HTML_";
		<input type="submit" name="download" value="ダウンロード" class="submit">
		<input type="submit" name="form" value="日記を書く" class="submit"><br>
		<br>
		ログサイズ <strong>$log_byte</strong>KB / 
_HTML_

		if ($make_past == 1) {
			print qq|過去ログサイズ <strong>$past_byte</strong>KB / \n|;
		}

		print <<"_HTML_";
		日記数 <strong>$parent</strong>件 / $work_mesg<br>
_HTML_

		if ($mini_counter != 0) {
			printf("総アクセス <strong>%0${count_figure}d</strong> / 今日 <strong>%0${count_figure}d</strong> / 昨日 <strong>%0${count_figure}d</strong><br>", $sum, $today, $yesterday);
		}

		print <<"_HTML_";
		<br>
		<table border="1" class="table">
			<tr>
				<th class="table_head">削除</th>
				<th class="table_head">修正</th>
				<th class="table_head">No</th>
				<th class="table_head">日付</th>
_HTML_

		if ($res_mode == 1) {
			print <<"_HTML_";
				<th class="table_head">投稿者</th>
_HTML_
		}

		print <<"_HTML_";
				<th class="table_head">題名</th>
				<th class="table_head">本文</th>
_HTML_

		if ($upload_mode == 1) {
			print <<"_HTML_";
				<th class="table_head">ファイル</th>
_HTML_
		}

		if ($res_mode == 1) {
			print <<"_HTML_";
				<th class="table_head">ホスト</th>
_HTML_
		}

		print <<"_HTML_";
			</tr>
_HTML_

		$start = $in{'page'} * $page_size;
		$end   = $start + $page_size;

		$i = 0;
		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

			if ($reno == 0) {
				$i++;
			}
			if ($i <= $start) {
				next;
			}
			if ($i > $end) {
				last;
			}

			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				$year  = sprintf("%04d", $1);
				$month = sprintf("%02d", $2);
				$day   = sprintf("%02d", $3);

				$week = $week[&getweek($day, $year, $month)];
				$date = qq|$year年$month月$day日（$week）|;
			} else {
				($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date);
				$date = sprintf("%04d年%02d月%02d日（%s）", $year + 1900, $mon + 1, $day, $week[$week]);
			}

			if ($reno == 0) {
				$name = '管理者';
				$modi = qq|<input type="radio" name="modi" value="$no" class="radio">|;
			} else {
				$modi = '-';
			}
			$text =~ s/<br>//gi;
			$text =~ s/</&lt;/g;
			$text =~ s/>/&gt;/g;
			if (length($text) > 60) {
				$text = substr($text, 0, 60) . ' …';
			}
			if ($file eq '') {
				$file = '-';
			} else {
				$file = qq|<a href="$upfile_dir$file" target="_blank">$file</a>|;
			}

			chop($host);

			print <<"_HTML_";
			<tr>
				<th class="table_data"><input type="checkbox" name="del" value="$no" class="checkbox"></th>
				<th class="table_data">$modi</th>
				<td class="table_data">$no</td>
				<td class="table_data">$date</td>
_HTML_
			if ($res_mode == 1) {
				print <<"_HTML_";
				<td class="table_data">$name</td>
_HTML_
			}

			print <<"_HTML_";
				<td class="table_data">$subj</td>
				<td class="table_data">$text</td>
_HTML_

			if ($upload_mode == 1) {
				print <<"_HTML_";
				<td class="table_data">$file</td>
_HTML_
			}

			if ($res_mode == 1) {
				print <<"_HTML_";
				<td class="table_data">$host</td>
_HTML_
			}

			print <<"_HTML_";
			</tr>
_HTML_
		}

		print <<"_HTML_";
		</table>
		<br>
		<div class="navi">
_HTML_

		if ($in{'page'} > 0) {
			print qq|<input type="submit" name="prev" value="↑前のページ" class="submit">\n|;
		}
		if (int(($parent - 1) / $page_size) > $in{'page'}) {
			print qq|<input type="submit" name="next" value="↓次のページ" class="submit">\n|;
		}

		print <<"_HTML_";
		</div>
	</form>
</p>
<hr>
_HTML_

		&footer();
	}
}

### 記事登録
sub regist {
	&regist_check();

	if ($in{'preview'} eq 'on') {
		&preview();
	}

	if ($mobile == 0) {
		$cookie{'name'}    = $in{'name'};
		$cookie{'mail'}    = $in{'mail'};
		$cookie{'url'}     = $in{'url'};
		$cookie{'icon'}    = $in{'icon'};
		$cookie{'color'}   = $in{'color'};
		$cookie{'pwd'}     = $in{'pwd'};
		$cookie{'preview'} = $in{'c_preview'};

		&set_cookie(*cookie, $cookie_id, $hold_days);
	}

	&lock($lock_file);

	open(FILE, "$log_file") || &error("ログファイルを読み出せません");
	@logs = <FILE>;
	close(FILE);

	$no     = 0;
	$parent = 0;
	$name = '';
	$text = '';
	foreach (@logs) {
		if ($_ + 0 > $no) {
			($no, $date, $name, $text, $ip) = (split(/\t/))[0, 2, 3, 7, 12];
		}
		if ((split(/\t/))[1] == 0) {
			$parent++;
		}
	}
	$no++;

	if ($in{'name'} eq $name && $in{'text'} eq $text) {
		&unlock($lock_file);
		&error("二重投稿は禁止です");
	}
	chop($ip);
	if (time() - $date < $wait_time && $host eq $ip) {
		&unlock($lock_file);
		&error("連続投稿は$wait_time秒以上時間をあけてください");
	}

	$date = time();

	if ($in{'subj'} eq '') {
		$in{'subj'} = '無題';
	}
	if ($in{'url'} eq 'http://') {
		$in{'url'} = '';
	}
	if ($in{'pwd'} ne '') {
		$in{'pwd'} = &encrypt($in{'pwd'});
	}
	if ($in{'file'} ne '') {
		$in{'file'} = &upfile($in{'file'}, $in{'filename'}, $no);
	}

	$line = "$no\t$in{'reno'}\t$date\t$in{'name'}\t$in{'mail'}\t$in{'url'}\t$in{'subj'}\t$in{'text'}\t$in{'color'}\t$in{'icon'}\t\t$in{'pwd'}\t$host\n";

	@new  = ();
	$flag = 0;
	foreach (@logs) {
		($no, $reno) = split(/\t/);
		if ($flag == 0 && $in{'reno'} == $no) {
			$flag = 1;
		} elsif ($flag == 1 && $in{'reno'} != $reno) {
			$flag = 2;
			push(@new, $line);
		}
		push(@new, $_);
	}
	if ($flag == 1) {
		push(@new, $line);
	}
	@logs = @new;

	open(FILE, ">$log_file") || &error("ログファイルに書き込めません");
	print FILE @logs;
	close(FILE);

	if ($js_mode != 0) {
		&output_jsfile(@logs);
	}

	&unlock($lock_file);

	$in{'mode'} = '';
	$in{'no'}   = '';
	$in{'date'} = '';

	return;
}

### 投稿内容のチェック
sub regist_check {
	if ($ENV{'REQUEST_METHOD'} ne 'POST' && $mobile == 0) {
		&error("不正なアクセスです");
	}

	if ($ENV{'HTTP_REFERER'} ne '' && $base_url ne '' && $ENV{'HTTP_REFERER'} !~ /^$base_url/i) {
		&error("不正なアクセスです");
	}

	$host = $ENV{'REMOTE_HOST'};
	$addr = $ENV{'REMOTE_ADDR'};
	if ($host eq "" || $host eq $addr) {
		if ($gethostbyaddr == 1) {
			$host = gethostbyaddr(pack("C4",split(/\./,$addr)),2) || $addr;
		}
	}
	$flag = 0;
	foreach $list (@blacklist) {
		if ($list ne '' && $host =~ /$list/i) {
			$flag = 1; last;
		}
	}
	if ($proxy_check == 1) {
		if ($ENV{'HTTP_VIA'} ne '' || $ENV{'HTTP_FORWARDED'} ne '' || $ENV{'HTTP_X_FORWARDED_FOR'} ne '') {
			$flag = 1;
		}
	}
	if ($flag == 1) {
		&error("アクセスを許可されていません");
	}

	$in{'name'} = &create_line($in{'name'});
	$in{'mail'} = &create_line($in{'mail'});
	$in{'url'}  = &create_line($in{'url'});
	$in{'subj'} = &create_line($in{'subj'});
	$in{'pwd'}  = &create_line($in{'pwd'});
	$in{'text'} = &create_text($in{'text'});

	if ($in{'name'} eq '') {
		&error("名前が入力されていません");
	}
	if (length($in{'name'}) > $name_length * 2) {
		&error("名前の長さは全角$name_length文字までにしてください");
	}
	if (($in{'mail'} ne '' && $in{'mail'} =~ /[^\w\.\@\d-_]/ && $in{'mail'} !~ /(.*)\@(.*)\.(.*)/) || length($in{'mail'}) > 50) {
		&error("Ｅメールの入力内容が正しくありません");
	}
	if (length($in{'url'}) > 70) {
		&error("URLの入力内容が正しくありません");
	}
	if ($need_subj == 1 && $in{'subj'} eq '') {
		&error("題名が入力されていません");
	}
	if (length($in{'subj'}) > ($subj_length + 2) * 2) {
		&error("題名の長さは全角" . ($subj_length + 2) . "文字までにしてください");
	}
	if ($in{'text'} eq '') {
		&error("本文が入力されていません");
	}
	if (length($in{'text'}) > $text_length * 2) {
		&error("本文の長さは全角$text_length文字までにしてください");
	}
	if (($in{'text'} =~ s/<br>/<br>/g) + 1 > $text_line) {
		&error("本文は$text_line行までにしてください");
	}
	if ($admin_icon_mode == 1 && $in{'icon'} eq $admin_icon[0]) {
		&error("管理者専用のアイコンは使用できません");
	}
	if ($need_pwd == 1 && $in{'pwd'} eq '') {
		&error("削除キーが設定されていません");
	}
	if (length($in{'pwd'}) > 10) {
		&error("削除キーの長さは半角10文字までにしてください");
	}

	return;
}

### プレビュー表示
sub preview {
	if ($in{'mode'} eq 'admin') {
		$date       = $in{'date'};
		$input_mode = ' name="regist"';
	} else {
		$date = time();
	}

	if ($in{'subj'} eq '') {
		$in{'subj'} = '無題';
	}
	if ($in{'url'} eq 'http://') {
		$in{'url'} = '';
	}
	$preview_text = $in{'text'};
	$in{'text'} =~ s/"/&quot;/g;
	$in{'text'} =~ s/<br>/\n/g;

	if ($upload_mode == 1 && $in{'mode'} eq 'admin' && ($in{'file'} ne '' || $in{'orgfile'} ne '')) {
		if ($in{'orgfile'} ne '') {
			$in{'filename'} = $in{'orgfile'};
		}
		$in{'filename'} =~ /([^\/\\]*)\.([^.\/\\]*)$/;
		$ext = $2;
		$ext =~ tr/A-Z/a-z/;

		if ($in{'file'} ne '') {
			open(FILE, ">$upfile_dir$temporary_file") || &error("テンポラリファイルが作成できません");
			binmode(FILE);
			print FILE $in{'file'};
			close(FILE);
		}
	}

	&header();

	print <<"_HTML_";
<div class="link"><a href="$script">▲日記帳に戻る</a></div>
<h2>プレビュー</h2>
_HTML_

	$logs[0] = "XXX\t0\t$date\t$in{'name'}\t$in{'mail'}\t$in{'url'}\t$in{'subj'}\t$preview_text\t$in{'color'}\t$in{'icon'}\t$in{'file'}\t\t$in{'pwd'}\t$host\n";
	&showlog(@logs);

	print <<"_HTML_";
<p align="center">
	<form action="$script" method="post" enctype="multipart/form-data">
		<input type="hidden" name="mode" value="$in{'mode'}">
		<input type="hidden" name="preview" value="off">
		<input type="hidden" name="c_preview" value="$in{'preview'}">
		<input type="hidden" name="date" value="$in{'date'}">
		<input type="hidden" name="reno" value="$in{'reno'}">
		<input type="hidden" name="name" value="$in{'name'}">
		<input type="hidden" name="mail" value="$in{'mail'}">
		<input type="hidden" name="url" value="$in{'url'}">
		<input type="hidden" name="subj" value="$in{'subj'}">
		<input type="hidden" name="text" value="$in{'text'}">
		<input type="hidden" name="color" value="$in{'color'}">
		<input type="hidden" name="icon" value="$in{'icon'}">
		<input type="hidden" name="pwd" value="$in{'pwd'}">
		<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
		<input type="hidden" name="ext" value="$ext">
		<input type="submit"$input_mode value="この記事を投稿する" class="submit">
	</form>
</p>
<hr>
_HTML_

	&footer();
}

### デコード
sub decode {
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		if ($ENV{'CONTENT_LENGTH'} > 150000) {
			&error("投稿量が大きすぎます<br>現在の投稿量：$ENV{'CONTENT_LENGTH'} / 最大投稿量：150000");
		}
		$length = $ENV{'CONTENT_LENGTH'};
		binmode(STDIN);
		while ($length > 0) {
			$length  -= sysread(STDIN, $buffer, $length);
			$alldata .= $buffer;
		}
		$in{'file_size'} = 0;
		foreach $data (split(/\r\n/, $alldata)) {
			if ($interval eq '') {
				$interval = $data;
			} else {
				if ($data !~ /^$interval/i) {
					if ($data =~ /^Content-Disposition: form-data; name="([^"]*)"/i && $key eq '') {
						$key = $1;
						if ($data =~ /filename="([^"]*)"/i) {
							$in{'filename'} = $1;
						}
					} elsif ($data =~ /^Content-type: (\S+)/i) {
						$in{'mimetype'} = $1;
					} else {
						$in{"$key"} .= "$data\r\n";
						if ($key eq 'file') {
							$in{'file_size'} += length($data);
						}
					}
				} else {
					$key = '';
				}
			}
		}
	} else {
		foreach $data (split(/&/, $ENV{'QUERY_STRING'})) {
			($key, $value) = split(/=/, $data);
			$value =~ tr/+/ /;
			$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack('C', hex($1))/eg;
			$value =~ s/\t//g;
			$in{"$key"} = $value;
		}
	}
	while (($key, $value) = each(%in)) {
		$value =~ s/^\r\n//;
		$value =~ s/\r\n$//;

		if ($key ne 'file') {
			&jcode'convert(*value, 'sjis', '', 'z');

			$value =~ s/&/&amp;/g;
			$value =~ s/</&lt;/g;
			$value =~ s/>/&gt;/g;
			$value =~ s/"/&quot;/g;
		}

		if ($key eq 'del') {
			foreach $del (split(/\r\n\r\n/, $value)) {
				push(@delete, $del);
			}
		}

		$in{"$key"} = $value;
	}
	$in{'page'}   += 0;
	$in{'record'} += 0;

	return;
}

### HTMLヘッダ
sub header {
	print <<"_HTML_";
Content-Type: text/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 http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>$title</title>
<link rel="stylesheet" href="$css_file" type="text/css">
_HTML_

	if ($upload_mode == 1 && $in{'mode'} eq 'admin' && $in{'form'} ne '') {
		print <<"_HTML_";
<script type="text/javascript">
<!--

//アップロードファイル情報の保持
function insertData() {
	if (document.form.file.value != '') {
		document.form.orgfile.value = document.form.file.value;
	}
}

//-->
</script>
_HTML_
	}

	print <<"_HTML_";
</head>

<body>
<div class="document">
_HTML_

	return;
}

### HTMLフッタ
sub footer {
	print <<"_HTML_";
<!-- Copyright -->
<div align="center">- <a href="http://www.big.or.jp/~knight/" target="_blank">Web Diary Professional ver $ver</a> -</div>
</div>
</body>

</html>
_HTML_

	exit();
}

### エラー出力
sub error {
	local($_) = @_;

	if ($mobile == 1) {
		print <<"_HTML_";
<p>
	ｴﾗｰ:<br>
	$_<br>
</p>
</body>

</html>
_HTML_

		exit();
	}

	&header();

	print <<"_HTML_";
<p>
	<div align="center">
		<br>
		<hr width="70%">
		<br>
		<span class="warning">エラーが発生しました</span><br>
		<br>
		$_<br>
		<br>
		<hr width="70%">
		<br>
	</div>
</p>
_HTML_

	&footer();
}

### モバイル表示
sub mobile {
	$agent = $ENV{'HTTP_USER_AGENT'};

	if ($agent !~ /DoCoMo/i && $agent !~ /J-PHONE/i && $agent !~ /UP\.Browser/i) {
		return;
	} else {
		$mobile = 1;
	}

	print <<"_HTML_";
Content-Type: text/html

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>$title</title>
</head>

<body text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#0000FF">
_HTML_

	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		if ($ENV{'CONTENT_LENGTH'} > 15000) {
			&error("投稿量が大きすぎます<br>現在の投稿量：$ENV{'CONTENT_LENGTH'} / 最大投稿量：15000");
		}
		read(STDIN, $alldata, $ENV{'CONTENT_LENGTH'});
	} else {
		$alldata = $ENV{'QUERY_STRING'};
	}
	foreach $data (split(/&/, $alldata)) {
		($key, $value) = split(/=/, $data);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack('C', hex($1))/eg;
		$value =~ s/\t//g;

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

		$value =~ s/&/&amp;/g;
		$value =~ s/</&lt;/g;
		$value =~ s/>/&gt;/g;
		$value =~ s/"/&quot;/g;

		if ($key eq 'del') {
			push(@delete, $value);
		}

		$in{"$key"} = $value;
	}
	$in{'page'}   += 0;

	if ($in{'mode'} eq 'regist') {
		if ($in{'adminpwd'} ne $admin_pwd) {
			&error("ﾊﾟｽﾜｰﾄﾞが違います");
		}

		$in{'subj'} = &create_line($in{'subj'});
		$in{'text'} = &create_text($in{'text'});

		$in{'text'} =~ s/&amp;/&/g;
		$in{'text'} =~ s/&lt;/</g;
		$in{'text'} =~ s/&gt;/>/g;
		$in{'text'} =~ s/&quot;/"/g;

		if ($in{'subj'} eq '') {
			&error("題名が入力されていません");
		}
		if ($in{'date'} !~ /^\d\d\d\d\d\d\d\d$/) {
			&error("日付の値が不正です");
		}
		if ($in{'text'} eq '') {
			&error("本文が入力されていません");
		}

		&lock($lock_file);

		open(FILE, "$log_file") || &error("ﾛｸﾞﾌｧｲﾙを読み出せません");
		@logs = <FILE>;
		close(FILE);

		$host = $ENV{'REMOTE_HOST'};
		$addr = $ENV{'REMOTE_ADDR'};
		if ($host eq "" || $host eq $addr) {
			if ($gethostbyaddr == 1) {
				$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2) || $addr;
			}
		}

		$no     = 0;
		$parent = 0;
		$subj = '';
		$text = '';
		foreach (@logs) {
			if ($_ + 0 > $no) {
				($no, $date, $subj, $text) = (split(/\t/))[0, 2, 6, 7];
			}
			if ((split(/\t/))[1] == 0) {
				$parent++;
			}
		}
		$no++;

		if ($in{'subj'} eq $subj && $in{'text'} eq $text) {
			&error("すでに書き込まれています");
		}

		$line = "$no\t0\t$in{'date'}\t\t\t\t$in{'subj'}\t$in{'text'}\t\t\t\t\t$host\n";

		if ($make_past == 0 && $parent >= $max_log) {
			foreach (0 .. ($parent - $max_log)) {
				($no, $reno) = split(/\t/, $logs[$#logs]);
				if ($reno == 0) {
					pop(@logs);
				} else {
					while ((split(/\t/, $logs[$#logs]))[1] != 0) {
						pop(@logs)
					}
					pop(@logs);
				}
			}
		}

		@new  = ();
		$flag = 0;
		foreach (@logs) {
			($no, $reno, $date) = split(/\t/);
			if ($flag == 0 && $reno == 0 && $in{'date'} + 0 >= $date) {
				$flag = 1;
				push(@new, $line);
			}
			push(@new, $_);
		}
		if ($flag == 0) {
			push(@new, $line);
		}
		@logs = @new;

		open(FILE, ">$log_file") || &error("ﾛｸﾞﾌｧｲﾙに書き込めません");
		print FILE @logs;
		close(FILE);

		if ($js_mode != 0) {
			&output_jsfile(@logs);
		}

		&unlock($lock_file);

		$in{'mode'} = '';
	} elsif ($in{'mode'} eq 'responce') {
		&regist();
	}

	open(FILE, "$log_file") || &error("ﾛｸﾞﾌｧｲﾙを読み出せません");
	@logs = <FILE>;
	close(FILE);

	$parent = 0;
	foreach (@logs) {
		if ((split(/\t/))[1] == 0) {
			$parent++;
		}
	}

	if ($in{'mode'} eq 'form') {
		if ($agent =~ /DoCoMo/i) {
			$method = 'post';
		} else {
			$method = 'get';
		}

		if ($in{'adminpwd'} eq '') {
			print <<"_HTML_";
<p>
	ﾊﾟｽﾜｰﾄﾞを入力してください<br>
	<form action="$script" method="$method">
		<input type="password" name="adminpwd" size="6"><br>
		<input type="hidden" name="mode" value="form">
		<input type="submit" value="認証">
	</form>
</p>
<p>
	<a href="$script">戻る</a>
</p>
_HTML_
		} else {
			if ($in{'adminpwd'} ne $admin_pwd) {
				&error("ﾊﾟｽﾜｰﾄﾞが違います");
			}

			($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
			$date = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);

			print <<"_HTML_";
<p>
	■ 日記投稿<br>
	<hr>
	<form action="$script" method="$method">
		題名<br>
		<input type="text" name="subj" size="12" value="$subj"><br>
		日付<br>
		<input type="text" name="date" size="12" value="$date"><br>
		本文<br>
		<textarea name="text" cols="15" rows="5"></textarea><br>
		<br>
		<input type="submit" value="書き込む">
		<input type="hidden" name="mode" value="regist">
		<input type="hidden" name="adminpwd" value="$in{'adminpwd'}">
	</form>
</p>
<p>
	<a href="$script">戻る</a>
</p>
_HTML_
		}
	} elsif ($in{'mode'} eq 'res') {
		if ($agent =~ /DoCoMo/i) {
			$method = 'post';
		} else {
			$method = 'get';
		}

		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj) = split(/\t/);

			if ($in{'no'} == $no) {
				$subj = 'Re:' . $subj;
				last;
			}
		}

		print <<"_HTML_";
<p>
	■ 記事投稿<br>
	<hr>
	<form action="$script" method="$method">
		名前<br>
		<input type="text" name="name" size="12"><br>
		Eﾒｰﾙ<br>
		<input type="text" name="mail" size="12"><br>
		URL<br>
		<input type="text" name="url" size="12"><br>
		本文<br>
		<textarea name="text" cols="15" rows="4"></textarea><br>
		削除ｷｰ<br>
		<input type="password" name="pwd" size="6"><br>
		<br>
		<input type="submit" value="返信する">
		<input type="hidden" name="subj" value="$subj">
		<input type="hidden" name="mode" value="responce">
		<input type="hidden" name="reno" value="$in{'no'}">
	</form>
</p>
<p>
	<a href="$script">戻る</a>
</p>
_HTML_
	} elsif ($in{'mode'} eq 'delete') {
		if ($in{'del'} ne '') {
			if ($in{'user_pwd'} eq '') {
				&error("削除ｷｰを入力してください");
			}
			if ($in{'del'} eq '') {
				&error("削除したい記事を指定してください");
			}

			&lock($lock_file);

			open(FILE, "$log_file") || &error("ﾛｸﾞﾌｧｲﾙを読み出せません");
			@logs = <FILE>;
			close(FILE);

			@new = ();
			$flag = 0;
			$del_list = '';
			foreach (@logs) {
				($no, $reno, $pwd) = (split(/\t/))[0, 1, 11];

				if ($flag == 0 && $in{'del'} == $no) {
					if ($in{'user_pwd'} ne $admin_pwd) {
						&decrypt($in{'user_pwd'}, $pwd);
					}
					$flag = 1;
				} elsif ($flag == 1 && $in{'del'} == $reno) {
					$flag = 1;
				} else {
					$flag = 0;
				}
				if ($flag == 0) {
					push(@new, $_);
				} else {
					$del_list .= " [$no]";
				}
			}
			if ($del_list eq '') {
				&error("該当記事がありません");
			}
			$work_mesg = "$del_list番の記事を削除しました";

			open(FILE, ">$log_file") || &error("ﾛｸﾞﾌｧｲﾙに書き込めません");
			print FILE @new;
			close(FILE);

			if ($js_mode != 0) {
				&output_jsfile(@new);
			}

			&unlock($lock_file);
		}

		if ($agent =~ /DoCoMo/i) {
			$method = 'post';
		} else {
			$method = 'get';
		}

		print <<"_HTML_";
<p>
	■ 記事削除<br>
	<hr>
	$work_mesg<br>
	<form action="$script" method="$method">
		削除する記事の番号<br>
		<input type="text" name="del" size="6"><br>
		削除ｷｰ<br>
		<input type="password" name="user_pwd" size="6"><br>
		<br>
		<input type="submit" value="削除する">
		<input type="hidden" name="mode" value="delete">
	</form>
</p>
<p>
	<a href="$script">戻る</a>
</p>
_HTML_
	} elsif ($in{'mode'} eq 'all') {
		print '<p>■ 日記表示<br>';

		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host) = split(/\t/);

			if ($in{'no'} != $no && $in{'no'} != $reno) {
				next;
			}

			&jcode'convert(*subj, 'sjis', '', 'h');
			&jcode'convert(*text, 'sjis', '', 'h');

			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
				$today = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);
				if ($new_date != 0 && $today - $date < $new_date) {
					$new = $new_mark;
				} else {
					$new = '';
				}

				$year  = sprintf("%04d", $1);
				$month = sprintf("%02d", $2);
				$day   = sprintf("%02d", $3);

				$week = $week[&getweek($day, $year, $month)];
				$date = qq|$year年$month月$day日（$week）|;
			} else {
				($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date);
				$date = sprintf("%02d月%02d日%02d:%02d", $mon + 1, $day, $hour, $min);
			}

			if ($mail ne '') {
				$mail = qq|<a href="mailto:$mail">Eﾒｰﾙ</a>|;
			}
			if ($url ne '') {
				$url = qq|<a href="$url" target="_blank">URL</a>|;
			}

			if ($auto_link == 1) {
				$text = &auto_link($text);
			}

			if ($file ne '' && -e "$upfile_dir$file") {
				$file = qq|<a href="$upfile_dir$file">ﾌｧｲﾙ</a>|;
			}
			if ($res_mode == 1 && $logs[0] + 0 != 0) {
				$res = qq|<a href="$script?mode=res&no=$in{'no'}">返信する</a><br>|;
			}

			chop($host);

			if ($reno == 0) {
				print <<"_HTML_";
<hr>
[$no] $subj $file<br>
<br>
$text<br>
<br>
$date<br>
_HTML_
			} else {
				print <<"_HTML_";
<hr>
[$no] $subj<br>
<br>
$name $mail $url $file<br>
<br>
$text<br>
<br>
$date<br>
_HTML_
			}
		}

		print <<"_HTML_";
<hr>
$res
</p>
<p>
	<a href="$script">戻る</a>
</p>
_HTML_
	} else {
		print '<p>';

		$start = $in{'page'} * $page_size;
		$end   = $start + $page_size;

		$i = 0;
		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host) = split(/\t/);

			if ($reno == 0) {
				$i++;
			} else {
				next;
			}
			if ($i <= $start) {
				next;
			}
			if ($i > $end) {
				last;
			}

			&jcode'convert(*subj, 'sjis', '', 'h');

			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				$year  = sprintf("%04d", $1);
				$month = sprintf("%02d", $2);
				$day   = sprintf("%02d", $3);

				$week = $week[&getweek($day, $year, $month)];
				$date = qq|$month月$day日（$week）|;
			}

			chop($host);

			print qq|■ <a href="$script?mode=all&no=$no">$subj</a> - $date<br>\n|;
		}

		print '<br>';

		if ($in{'page'} > 0) {
			$n = $in{'page'} - 1;
			print qq|<a href="$script?page=$n">前のﾍﾟｰｼﾞ</a><br>\n|;
		}
		if (int(($parent - 1) / $page_size) > $in{'page'}) {
			$n = $in{'page'} + 1;
			print qq|<a href="$script?page=$n">次のﾍﾟｰｼﾞ</a><br>\n|;
		}

		print <<"_HTML_";
</p>
<p>
	<a href="$script?mode=form">日記を書く</a><br>
	<a href="$script?mode=delete">記事削除</a><br>
	<br>
	<a href="$mobile_back_url">戻る</a><br>
</p>
_HTML_
	}

	print <<"_HTML_";
<hr>
<div align="center"><a href="http://www.big.or.jp/~knight/mobile/" target="_blank">Web Liberty</a></div>
</body>

</html>
_HTML_

	exit();
}

### ミニアクセスカウンタカウントアップ
sub count_up {
	if ($no_count_list[0] ne '') {
		$host = $ENV{'REMOTE_HOST'};
		$addr = $ENV{'REMOTE_ADDR'};
		if ($host eq "" || $host eq $addr) {
			if ($gethostbyaddr == 1) {
				$host = gethostbyaddr(pack("C4",split(/\./,$addr)),2) || $addr;
			} else {
				$host = $addr;
			}
		}
		$flag = 0;
		foreach $list (@no_count_list) {
			if ($list ne '' && $host =~ /$list/i) {
				$flag = 1; last;
			}
		}
		if ($flag == 1) {
			return;
		}
	}

	&lock($lock_file);

	open(COUNT, "$count_log") || &error("カウントログファイルを読み出せません");
	$data = <COUNT>;
	close(COUNT);

	local($sum, $today, $yesterday, $key) = split(/\t/, $data);

	local($sec, $min, $hour, $day) = localtime(time());

	if ($key == $day) {
		$today++;
	} else {
		$yesterday = $today;
		$today     = 1;
	}
	$sum++;

	$data = "$sum\t$today\t$yesterday\t$day";

	open(COUNT, ">$count_log") || &error("カウントログファイルに書き込めません");
	print COUNT $data;
	close(COUNT);

	&unlock($lock_file);

	return;
}

### JSファイル出力
sub output_jsfile {
	local(@logs) = @_;

	if ($js_mode == 1 || $js_mode == 3) {
		@js_logs = ();
		$i = 0;
		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

			if ($reno != 0) {
				next;
			}
			if ($i >= $js_output_text) {
				last;
			}

			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				$date = "$1/$2/$3";
			}

			$subj =~ s/\'/\\\'/g;
			$text =~ s/\'/\\\'/g;

			$line = qq|document.write('■ $subj ... $date<br><br>$text<hr>');\n|;
			push(@js_logs, $line);

			$i++;
		}

		$line = qq|document.write('<div align="right"><a href="http://www.big.or.jp/~knight/" target="_blank">Web Diary Professional</a></div>');\n|;
		push(@js_logs, $line);

		open(FILE, ">$js_text_file") || &error("JSファイルに書き込めません");
		print FILE @js_logs;
		close(FILE);
	}
	if ($js_mode == 2 || $js_mode == 3) {
		@js_logs = ();
		$i = 0;
		foreach (@logs) {
			($no, $reno, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

			if ($reno != 0) {
				next;
			}
			if ($i >= $js_output_title) {
				last;
			}

			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
				$date = "$1/$2/$3";
			}
			if ($js_title_url ne '') {
				$subj = qq|<a href="$js_title_url?no=$no">$subj</a>|;
			}

			$subj =~ s/\'/\\\'/g;
			$text =~ s/\'/\\\'/g;

			$line = qq|document.write('$date ... $subj<br>');\n|;
			push(@js_logs, $line);

			$i++;
		}

		open(FILE, ">$js_title_file") || &error("JSファイルに書き込めません");
		print FILE @js_logs;
		close(FILE);
	}

	return;
}

### INPUT情報正規化
sub create_line {
	local($_) = @_;
	$_ =~ s/^\s+//;
	$_ =~ s/\s+$//;
	return $_;
}

### TEXTAREA情報正規化
sub create_text {
	local($_) = @_;
	$_ =~ s/\r?\n/\r/g;
	$_ =~ s/^\r+//;
	$_ =~ s/\r+$//;
	$_ =~ s/\r/<br>/g;
	return $_;
}

### オートリンク
sub auto_link {
	local($_) = @_;
	$_ =~ s/(https?:\/\/[\w\.\~\-\/\?\&\#\+\=\:\;\@\%]+)/<a href="$1" target="_blank">$1<\/a>/gi;
	$_ =~ s/([\w\-]+\@[\w\-\.]+)/<a href="mailto:$1">$1<\/a>/gi;
	return $_;
}

### コンマの付加
sub comma {
	local($_) = @_;
	while ($_ =~ s/^([-+]?\d+)(\d\d\d)/$1,$2/) {}
	return $_;
}

### 削除キー暗号化
sub encrypt {
	local($_) = @_;
	srand(time + $$);
	local $salt = pack('CC', int(rand(26) + 65), int(rand(10) + 48));
	return crypt($_, $salt);
}

### 削除キー照合
sub decrypt {
	local($inpwd, $pwd) = @_;
	if ($pwd eq '') {
		&error("該当記事には削除キーが設定されていません");
	}
	if (crypt($inpwd, $pwd) ne $pwd) {
		&error("削除キーが違います");
	}
	return;
}

### 曜日を取得
sub getweek {
	local($day, $year, $month) = @_;

	if ($month == 1 || $month == 2) {
		$year--;
		$month += 12;
	}
	return int($year + int($year / 4) - int($year / 100) + int($year / 400) + int ((13 * $month + 8) / 5) + $day) % 7;
}

### ファイルアップロード
sub upfile {
	local($file, $filename, $no) = @_;

	$filename =~ /([^\/\\]*)\.([^.\/\\]*)$/;
	$ext = $2;
	$ext =~ tr/A-Z/a-z/;

	open(FILE, ">$upfile_dir$no.$ext") || &error("アップロードファイルが保存できません");
	binmode(FILE);
	print FILE $file;
	close(FILE);

	return "$no.$ext";
}

### ログ移動処理
sub move_log {
	local(*logs, *past, $size) = @_;

	if ($size ne 're') {
		foreach (1 .. $size) {
			while ((split(/\t/, $logs[$#logs]))[1] != 0) {
				unshift(@past, pop(@logs));
			}
			unshift(@past, pop(@logs));
		}
	} else {
		foreach (@past) {
			push(@logs, $_);
		}
	}

	return (*logs, *past);
}

### ファイルロック
sub lock {
	local($lock_file) = @_;
	local $flag = 0;
	foreach (1 .. 5) {
		if (-e $lock_file) {
			if (time() > (stat($lock_file))[9] + 60) {
				&unlock($lock_file);
				next;
			}
			sleep(1);
		} else {
			open(LOCK, ">$lock_file") || &error("ロックファイルが作成できません");
			close(LOCK);
			$flag = 1;
			last;
		}
	}
	if ($flag == 0) {
		&error("ファイルがロックされています<br>時間をおいてもう一度投稿してください");
	}
	return;
}

### ファイルロック解除
sub unlock {
	local($_) = @_;
	unlink($_);
	return;
}

### GIF,JPEG,PNGサイズ取得(参考：WWWimagesize ... http://www.bloodyeck.com/wwwis/)
sub gifsize{
	local($GIF) = @_;
	local($type, $a, $b, $c, $d, $s) = (0, 0, 0, 0, 0, 0);

	if (defined($GIF) && read($GIF, $type, 6) && $type =~ /GIF8[7,9]a/ && read($GIF, $s, 4) == 4) {
		($a, $b, $c, $d) = unpack("C"x4, $s);
		return ($b << 8 | $a, $d << 8 | $c);
	}
	return (0, 0);
}
sub jpegsize {
	local($JPEG) = @_;
	local($done) = 0;
	local($c1, $c2, $ch, $s, $length, $dummy) = (0, 0, 0, 0, 0, 0);
	local($a, $b, $c, $d);

	if (defined($JPEG) && read($JPEG, $c1, 1) && read($JPEG, $c2, 1) && ord($c1) == 0xFF && ord($c2) == 0xD8) {
		while (ord($ch) != 0xDA && !$done) {
			while (ord($ch) != 0xFF) {
				return(0, 0) unless read($JPEG, $ch, 1);
			}
			while (ord($ch) == 0xFF) {
				return(0, 0) unless read($JPEG, $ch, 1);
			}
			if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
				return(0, 0) unless read($JPEG, $dummy, 3);
				return(0, 0) unless read($JPEG, $s, 4);
				($a, $b, $c, $d) = unpack("C"x4, $s);
				return ($c << 8 | $d, $a << 8 | $b);
			} else {
				return(0, 0) unless read ($JPEG, $s, 2);
				($c1, $c2) = unpack("C"x2, $s);
				$length = $c1 << 8 | $c2;
				last if (!defined($length) || $length < 2);
				read($JPEG, $dummy, $length - 2);
			}
		}
	}
	return (0, 0);
}
sub pngsize {
	local($PNG) = @_;
	local($head) = "";
	local($a, $b, $c, $d, $e, $f, $g, $h) = 0;

	if (defined($PNG) && read($PNG, $head, 8) == 8 && $head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" && read($PNG, $head, 4) == 4 && read($PNG, $head, 4) == 4 && $head eq "IHDR" && read($PNG, $head, 8) == 8){
		($a, $b, $c, $d, $e, $f, $g, $h) = unpack("C"x8, $head);
		return ($a << 24 | $b << 16 | $c << 8 | $d, $e << 24 | $f << 16 | $g << 8 | $h);
	}
	return (0, 0);
}

### クッキー関連の処理
sub get_cookie {
	local($cookie_id) = @_;
	local %all_cookies = ();
	foreach (split(/; /, $ENV{'HTTP_COOKIE'})) {
		local($key, $value) = split(/=/);
		$all_cookies{"$key"} = $value;
	}
	foreach (split(/&/, $all_cookies{"$cookie_id"})) {
		local ($key, $value) = split(/:/);
		$value =~ s/&/&amp;/g;
		$value =~ s/</&lt;/g;
		$value =~ s/>/&gt;/g;
		$value =~ s/"/&quot;/g;
		$cookie{&unescape($key)} = &unescape($value);
	}
	return %cookie;
}
sub set_cookie {
	local(*cookie, $cookie_id, $hold_days) = @_;
	if (time() > $cookie{'refresh'} + 60 * 60 * 24 * 30) {
		$cookie{'refresh'} = time();
	}
	local @pairs = ();
	foreach (sort keys %cookie) {
		push(@pairs, &escape($_) . ':' . &escape($cookie{"$_"}));
	}
	local $new_cookie = join('&', @pairs);
	local $date = &gmt_date(time + 60 * 60 * 24 * $hold_days);
	if ($new_cookie ne $all_cookies{"$cookie_id"}) {
		print "Set-Cookie: $cookie_id=$new_cookie; expires=$date\n";
	}
	return;
}
sub escape {
	local($_) = @_;
	$_ =~ s/([&:;=%\x00-\x21])/sprintf("%%%02X", unpack('C',$1))/ge;
	return $_;
}
sub unescape {
	local($_) = @_;
	$_ =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack('C', hex($1))/ge;
	return $_;
}
sub gmt_date {
	local($_) = @_;
	local($sec, $min, $hour, $day, $mon, $year, $wday) = gmtime($_);
	local @week  = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	local @month = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", $week[$wday], $day, $month[$mon], $year + 1900, $hour, $min, $sec);
}
