#!/usr/bin/perl

#Web Diary Professional (2005/08/04)
$ver = '3.19';
#
#Copyright(C) Knight 2002-2005
#Mail ... support@web-liberty.net
#Home ... http://www.web-liberty.net/

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

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

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

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

### メイン処理
if ($require_mode == 1) {
	return 1;
}
&mobile();
&skin();

&decode();
if ($in{'mode'} eq 'res' && $res_mode == 1) {
	&res();
} elsif ($in{'mode'} eq 'rss' && $rss_mode == 1) {
	&rss();
} elsif ($in{'mode'} eq 'trackback' && $trackback_mode == 1) {
	&trackback();
} 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();
}
&html();

### スキン読み込み
sub skin {
	open(SKIN, "$skin_file") || &skin_error();
	local @skins = <SKIN>;
	close(SKIN);

	%skin = ();
	@skin = ();
	local $skin_info = '';
	foreach (@skins) {
		if ($_ =~ /<!--SKIN_(\w+)_START-->/) {
			$skin_info = $1;
			$skin_info =~ tr/A-Z/a-z/;

			if ($skin_info eq 'navigation_head' || $skin_info eq 'navigation_foot' || $skin_info eq 'information_head' || $skin_info eq 'information_foot' || $skin_info eq 'diary_head' || $skin_info eq 'diary_foot') {
				$skin_label = $skin_info;
			} else {
				$skin_label = (split(/_/, $skin_info, 2))[0];
			}
			if ($skin_label ne $skin[$#skin] && ($skin_label eq 'navigation_head' || $skin_label eq 'navigation_foot' || $skin_label eq 'information_head' || $skin_label eq 'information_foot' || $skin_label eq 'calendar' || $skin_label eq 'counter' || $skin_label eq 'contents' || $skin_label eq 'search' || $skin_label eq 'record' || $skin_label eq 'field' || $skin_label eq 'author' || $skin_label eq 'trackbacklist' || $skin_label eq 'reslist' || $skin_label eq 'linklist' || $skin_label eq 'diary_head' || $skin_label eq 'diary_foot' || $skin_label eq 'subtitle' || $skin_label eq 'logs')) {
				push(@skin, $skin_label);
			}
			
			next;
		} elsif ($_ =~ /<!--SKIN_(\w+)_END-->/) {
			$skin_info = '';
			next;
		}

		if ($skin_info ne '') {
			$skin{"$skin_info"} .= $_;
		}
	}

	return;
}

### 基本変数
sub skin_basic {
	local($_) = @_;

	$_ =~ s/\$INFO_TITLE/$title/g;
	$_ =~ s/\$INFO_MESSAGE/$top_message/g;
	$_ =~ s/\$INFO_SCRIPT/$script/g;

	$_ =~ s/\$INFO_COUNTER_SUM/$sum_count/g;
	$_ =~ s/\$INFO_COUNTER_TODAY/$today_count/g;
	$_ =~ s/\$INFO_COUNTER_YESTERDAY/$yesterday_count/g;

	$_ =~ s/\$LINK_BACK/$back_url/g;
	$_ =~ s/\$LINK_LOGLIST/$script?mode=loglist/g;
	$_ =~ s/\$LINK_RSS/$script?mode=rss/g;
	$_ =~ s/\$LINK_SEARCH/$script?mode=search/g;
	$_ =~ s/\$LINK_USERDEL/$script?mode=userdel/g;
	$_ =~ s/\$LINK_PAST/$script?mode=past/g;
	$_ =~ s/\$LINK_ADMIN/$script?mode=admin/g;
	$_ =~ s/\$LINK_ICON/$script?mode=icon/g;

	if ($rss_mode == 1) {
		$_ =~ s/<!--INFO_RSS_START-->//g;
		$_ =~ s/<!--INFO_RSS_END-->//g;

		$_ =~ s/<!--LINK_RSS_START-->//g;
		$_ =~ s/<!--LINK_RSS_END-->//g;
	} else {
		$_ =~ s/<!--INFO_RSS_START-->/<!--/g;
		$_ =~ s/<!--INFO_RSS_END-->/-->/g;

		$_ =~ s/<!--LINK_RSS_START-->/<!--/g;
		$_ =~ s/<!--LINK_RSS_END-->/-->/g;
	}

	if ($res_mode == 1) {
		$_ =~ s/<!--LINK_USERDEL_START-->//g;
		$_ =~ s/<!--LINK_USERDEL_END-->//g;
	} else {
		$_ =~ s/<!--LINK_USERDEL_START-->/<!--/g;
		$_ =~ s/<!--LINK_USERDEL_END-->/-->/g;
	}

	if ($make_past == 1) {
		$_ =~ s/<!--LINK_PAST_START-->//g;
		$_ =~ s/<!--LINK_PAST_END-->//g;
	} else {
		$_ =~ s/<!--LINK_PAST_START-->/<!--/g;
		$_ =~ s/<!--LINK_PAST_END-->/-->/g;
	}

	if ($in{'page'} > 0) {
		local $prev = $in{'page'} - 1;

		$_ =~ s/<!--LINK_PREV_START-->/<a href="$script?page=$prev">/g;
		$_ =~ s/<!--LINK_PREV_END-->/<\/a>/g;
	} else {
		$_ =~ s/<!--LINK_PREV_START-->//g;
		$_ =~ s/<!--LINK_PREV_END-->//g;
	}
	if (int(($parent - 1) / $page_size) > $in{'page'}) {
		local $next = $in{'page'} + 1;

		$_ =~ s/<!--LINK_NEXT_START-->/<a href="$script?page=$next">/g;
		$_ =~ s/<!--LINK_NEXT_END-->/<\/a>/g;
	} else {
		$_ =~ s/<!--LINK_NEXT_START-->//g;
		$_ =~ s/<!--LINK_NEXT_END-->//g;
	}

	return $_;
}

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

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

		($sum, $c_today, $c_yesterday, $key) = split(/\t/, $data);
	}
	if ($counter_mode == 1) {
		$sum_count       = sprintf("%0${count_figure}d", $sum);
		$today_count     = sprintf("%0${count_figure}d", $c_today);
		$yesterday_count = sprintf("%0${count_figure}d", $c_yesterday);
	}

	local @profile = ();
	if ($in{'profile'} ne '') {
		open(PROFILE, "$profile_file") || &error("プロフィール保存ファイルを読み出せません。");
		@profile = <PROFILE>;
		close(PROFILE);
	}

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

	$html{'navigation_head'} = &skin_basic($skin{'navigation_head'});
	$html{'navigation_foot'} = &skin_basic($skin{'navigation_foot'});

	$html{'information_head'} = &skin_basic($skin{'information_head'});
	$html{'information_foot'} = &skin_basic($skin{'information_foot'});

	$html{'diary_head'} = &skin_basic($skin{'diary_head'});
	$html{'diary_foot'} = &skin_basic($skin{'diary_foot'});

	if ($show_calendar == 1) {
		&calendar(@logs);
	}
	if ($counter_mode == 1) {
		$html{'counter'} = &skin_basic($skin{'counter'});
	}
	if ($contents_mode == 1) {
		&contents();
	}
	if ($show_search_form == 1) {
		$html{'search'} = &skin_basic($skin{'search'});
	}
	if ($record_size != 0) {
		&record(@logs);
	}
	if ($show_field_list == 1 && $fields[1] ne '') {
		&field(@logs);
	}
	if ($show_author_list == 1 && $author_mode == 1) {
		&author(@logs);
	}
	if ($show_trackback_list != 0 && $trackback_mode) {
		&trackback_list();
	}
	if ($show_res_list != 0 && $res_mode == 1) {
		&res_list(@logs);
	}
	if ($link_mode == 1) {
		&link_list();
	}

	local $diary_info = '';
	if ($in{'date'} =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
		$diary_info = "$1年$2月$3日";
	} elsif ($in{'year'} ne '' || $in{'month'} ne '') {
		$diary_info = "$in{'year'}年$in{'month'}月";
	} elsif ($in{'no'} ne '') {
		$diary_info = "No.$in{'no'}の記事";
	} elsif ($in{'field'} ne '') {
		$diary_info = $fields[$in{'field'}];
	} elsif ($in{'profile'} ne '') {
		$diary_info = '投稿者情報';
	} elsif ($index_mode == 1) {
		$diary_info = $index_title;
	}
	if ($diary_info ne '') {
		$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/$diary_info<!--/g;
		$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;
	}
	$html{'subtitle'} = &skin_basic($skin{'subtitle'});

	if ($index_mode == 1 && $ENV{'QUERY_STRING'} eq '') {
		&index_mesg(@logs);
	} else {
		if ($in{'profile'} ne '') {
			local $flag = 0;
			foreach (@profile) {
				chop($_);
				($author, $text) = split(/\t/);

				if ($author[$in{'profile'}] eq $author) {
					$flag = 1;
					last;
				}
			}
			if ($flag == 0) {
				$author = $author[$in{'profile'}];
				$text   = 'プロフィールは登録されていません。';
			}

			&showlog("0\t0\t1\t\t\t$author\t\t\tプロフィール\t<em>$author</em><br><br>$text\t\t\t\t\t");
		} else {
			&showlog(&extract(@logs));
		}
	}

	&header();
	foreach (@skin) {
		print $html{"$_"};
	}
	&footer();
}

### インデックスページ
sub index_mesg {
	local(@logs) = @_;

	$skin{'log_head'} =~ s/\$LOG_HEAD_ID//g;

	$html{'logs'} = &skin_basic($skin{'logs_head'});
	$html{'logs'} .= &skin_basic($skin{'log_head'});
	$html{'logs'} .= $index_message;

	if ($index_record_size != 0) {
		$html{'logs'} .= qq|<p><em>最近の$index_record_size件</em></p>\n<ul>\n|;

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

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

			if ($display == 0) {
				next;
			}

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

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

				local $year  = sprintf("%04d", $1);
				local $month = sprintf("%02d", $2);
				local $day   = sprintf("%02d", $3);
				local $week  = $week[&getweek($day, $year, $month)];

				$date = "$year年$month月$day日（$week）";
			}

			$html{'logs'} .= qq|<li><a href="$script?no=$no">$date</a> &nbsp; $subj$new</li>\n|;
		}

		$html{'logs'} .= qq|</ul>\n|;
	}

	$html{'logs'} .= &skin_basic($skin{'log_foot'});
	$html{'logs'} .= &skin_basic($skin{'logs_foot'});

	return;
}

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

	local %date = ();
	foreach (@logs) {
		local($display, $date, $icon) = (split(/\t/))[2, 3, 11];

		if ($display == 0) {
			next;
		}

		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$date = "$1$2$3";
		}
		if ($icon eq '') {
			$icon = '-';
		}
		$date{"$date"} .= "$icon<>";
	}

	local @icon_info = @icon;
	local %icon_info;
	if ($icon_mode == 1 && $icon_calendar != 0) {
		if ($admin_icon_mode == 1) {
			push(@icon_info, @admin_icon);
		}
		while (scalar(@icon_info) != 0) {
			local $icon_file = shift(@icon_info);
			local $icon_name = shift(@icon_info);

			$icon_info{"$icon_file"} = $icon_name;
		}
	}

	local($d, $m, $y) = (localtime(time()))[3, 4, 5];
	$y = sprintf("%04d", $y + 1900);
	$m = sprintf("%01d", $m + 1);
	$d = sprintf("%01d", $d);

	local $this_year  = '';
	local $this_month = '';
	local $this_day   = '';
	if ($in{'date'} ne '') {
		if ($in{'date'} =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
			$this_year  = sprintf("%04d", $1);
			$this_month = sprintf("%01d", $2);
		}
		if ($y == $this_year && $m == $this_month) {
			$this_day = $d;
		}
	} elsif ($in{'year'} eq '') {
		$this_year  = $y;
		$this_month = $m;
		$this_day = $d;
	} else {
		if ($in{'year'} == $y && $in{'month'} == $m) {
			$this_day = $d;
		}
		$this_year  = $in{'year'};
		$this_month = $in{'month'};
	}

	local $prev_month = $this_month - 1;
	local $prev_year  = $this_year;
	if ($prev_month < 1) {
		$prev_month = 12;
		$prev_year--;
	}

	local $next_month = $this_month + 1;
	local $next_year  = $this_year;
	if ($next_month > 12) {
		$next_month = 1;
		$next_year++;
	}

	$skin{'calendar_head'} =~ s/\$CALENDAR_PREV_LINK/$script?year=$prev_year&amp;month=$prev_month/g;
	$skin{'calendar_head'} =~ s/\$CALENDAR_NEXT_LINK/$script?year=$next_year&amp;month=$next_month/g;
	$skin{'calendar_head'} =~ s/\$CALENDAR_ALL_LINK/$script?year=$this_year&amp;month=$this_month/g;

	$skin{'calendar_head'} =~ s/\$CALENDAR_PREV_YEAR/$prev_year/g;
	$skin{'calendar_head'} =~ s/\$CALENDAR_PREV_MONTH/$prev_month/g;

	$skin{'calendar_head'} =~ s/\$CALENDAR_NEXT_YEAR/$next_year/g;
	$skin{'calendar_head'} =~ s/\$CALENDAR_NEXT_MONTH/$next_month/g;

	$skin{'calendar_head'} =~ s/\$CALENDAR_THIS_YEAR/$this_year/g;
	$skin{'calendar_head'} =~ s/\$CALENDAR_THIS_MONTH/$this_month/g;

	$html{'calendar'} = &skin_basic($skin{'calendar_head'});

	$skin_calendar_celhead = &skin_basic($skin{'calendar_celhead'});
	$skin_calendar_celfoot = &skin_basic($skin{'calendar_celfoot'});

	$skin_calendar_todayhead = &skin_basic($skin{'calendar_todayhead'});
	$skin_calendar_todayfoot = &skin_basic($skin{'calendar_todayfoot'});

	$skin_calendar_sunday = &skin_basic($skin{'calendar_sunday'});

	local $week_key = &getweek('1', $this_year, $this_month);
	local $last_day = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$this_month - 1] + ($this_month == 2 && (($this_month % 4 == 0 && $this_month % 100 != 0) || $this_month % 400 == 0));

	local $i = 0;
	local $day = 1;
	local $week_number = 0;
	local $show_flag  = 0;
	local $spend_flag = 0;
	foreach (1 .. 42) {
		if ($week_key == $i) {
			$show_flag = 1;
		}
		if ($last_day < $day) {
			$show_flag = 0;
		}

		if ($show_voidrow == 0 && $show_flag == 0 && $day > 1 && $i == 0) {
			last;
		}

		if ($i == 0) {
			$skin_calendar_weekhead = $skin{'calendar_weekhead'};
			$html{'calendar'} .= &skin_basic($skin_calendar_weekhead);
		}

		if ($show_flag == 0) {
			$html{'calendar'} .= $skin_calendar_celhead;
			$html{'calendar'} .= $skin{'calendar_void'};
			$html{'calendar'} .= $skin_calendar_celfoot;
		} else {
			$this_month = sprintf("%02d", $this_month);
			local $code = sprintf("%02d", $day);
			if ($day == $this_day) {
				$html{'calendar'} .= $skin_calendar_todayhead;
			} else {
				$html{'calendar'} .= $skin_calendar_celhead;
			}

			local $day_style = '';
			if ($i == 0) {
				$day_style = 'sunday';
			} elsif ($i == 6) {
				$day_style = 'satday';
			} else {
				$day_style = 'day';
			}

			if ($spend_flag == 1) {
				$spend_flag = 0;
				$day_style = 'sunday';
			} else {
				foreach $spend_day (@spend_day) {
					if ("$this_month$code" eq "$spend_day") {
						if ($i == 0) {
							$spend_flag = 1;
						}
						$day_style = 'sunday';
						last;
					}
				}
				while (($m, $n) = each(%spend_day)) {
					if ($i == 1 && $this_month eq $m && $week_number eq $n) {
						$day_style = 'sunday';
						last;
					}
				}
			}

			if ($day_style eq 'sunday') {
				$skin_calendar_day = $skin{'calendar_sunday'};
			} elsif ($day_style eq 'satday') {
				$skin_calendar_day = $skin{'calendar_satday'};
			} else {
				$skin_calendar_day = $skin{'calendar_day'};
			}
			$skin_calendar_day =~ s/\$CALENDAR_DAY/$day/g;

			if ($date{"$this_year$this_month$code"} ne '') {
				if ($skin_calendar_day =~ /^(\s*)((.|\s)+)(\s*)$/) {
					local $day_string = $2;
					chop($day_string);

					if ($icon_calendar != 0) {
						local $icon_info = '';
						local $icon_no   = '';
						local $j         = 1;
						foreach $diary_info (split(/<>/, $date{"$this_year$this_month$code"})) {
							if ($j != 1) {
								$icon_no = "#diary$j";
							} else {
								$icon_no = '';
							}
							if ($diary_info eq '-') {
								$icon_info .= qq|<br><a href="$script?date=$this_year$this_month$code$icon_no">$written_mark</a>|;
							} else {
								$icon_info .= qq|<br><a href="$script?date=$this_year$this_month$code$icon_no"><img src="$icon_dir$diary_info" alt="$icon_info{$diary_info}"></a>|;
							}
							if ($icon_calendar == 1) {
								last;
							} else {
								$j++;
							}
						}

						$skin_calendar_day = qq|$1$day_string$icon_info$3|;
					} else {
						$skin_calendar_day = qq|$1<a href="$script?date=$this_year$this_month$code">$day_string</a>$3|;
					}
				}
			}

			$html{'calendar'} .= $skin_calendar_day;
			$html{'calendar'} .= $skin_calendar_celfoot;
		}

		$i++;
		if ($i == 1 && $show_flag == 1) {
			$week_number++;
		}
		if ($i == 7) {
			$skin_calendar_weekfoot = $skin{'calendar_weekfoot'};
			$html{'calendar'} .= &skin_basic($skin_calendar_weekfoot);
			$i = 0;
		}
		if ($show_flag == 1) {
			$day++;
		}
	}

	local $year_select = '<select name="year">';
	foreach ($this_year - 2 .. $this_year + 2) {
		if ($this_year == $_) {
			$year_select .= qq|<option value="$_" selected>$_年</option>|;
		} else {
			$year_select .= qq|<option value="$_">$_年</option>|;
		}
	}
	$year_select .= '</select>';

	local $month_select = '<select name="month">';
	foreach (1 .. 12) {
		if ($_ == $this_month) {
			$month_select .= qq|<option value="$_" selected>$_月</option>|;
		} else {
			$month_select .= qq|<option value="$_">$_月</option>|;
		}
	}
	$month_select .= '</select>';

	$skin{'calendar_foot'} =~ s/\$CALENDAR_YEAR_SELECT/$year_select/g;
	$skin{'calendar_foot'} =~ s/\$CALENDAR_MONTH_SELECT/$month_select/g;
	$skin{'calendar_foot'} =~ s/\$CALENDAR_PREV_LINK/$script?year=$prev_year&amp;month=$prev_month/g;
	$skin{'calendar_foot'} =~ s/\$CALENDAR_NEXT_LINK/$script?year=$next_year&amp;month=$next_month/g;

	$html{'calendar'} .= &skin_basic($skin{'calendar_foot'});

	return;
}

### 最近の記事表示
sub record {
	local(@logs) = @_;

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

	if ($in{'record'} > 0) {
		local $n = $in{'record'} - 1;

		$skin{'record_foot'} =~ s/<!--RECORD_PREV_START-->/<a href="$script?record=$n">/g;
		$skin{'record_foot'} =~ s/<!--RECORD_PREV_END-->/<\/a>/g;
	} else {
		$skin{'record_foot'} =~ s/<!--RECORD_PREV_START-->//g;
		$skin{'record_foot'} =~ s/<!--RECORD_PREV_END-->//g;
	}
	if (int(($parent - 1) / $record_size) > $in{'record'}) {
		local $n = $in{'record'} + 1;

		$skin{'record_foot'} =~ s/<!--RECORD_NEXT_START-->/<a href="$script?record=$n">/g;
		$skin{'record_foot'} =~ s/<!--RECORD_NEXT_END-->/<\/a>/g;
	} else {
		$skin{'record_foot'} =~ s/<!--RECORD_NEXT_START-->//g;
		$skin{'record_foot'} =~ s/<!--RECORD_NEXT_END-->//g;
	}

	$html{'record'} = &skin_basic($skin{'record_head'});

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

	local $i = 0;
	foreach (@logs) {
		local($no, $reno, $display, $date, $field, $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;
		}

		if ($display == 0) {
			next;
		}

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

			($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_flag = 1;
			} else {
				$new_flag = 0;
			}

			$date = "$1/$2/$3";
		}

		$skin_record = $skin{'record'};
		$skin_record =~ s/\$RECORD_NO/$no/g;
		$skin_record =~ s/\$RECORD_RENO/$reno/g;
		$skin_record =~ s/\$RECORD_DATE/$date/g;
		$skin_record =~ s/\$RECORD_FIELD/$field/g;
		$skin_record =~ s/\$RECORD_NAME/$name/g;
		$skin_record =~ s/\$RECORD_MAIL/$mail/g;
		$skin_record =~ s/\$RECORD_URL/$url/g;
		$skin_record =~ s/\$RECORD_SUBJ/$subj/g;
		$skin_record =~ s/\$RECORD_TEXT/$text/g;
		$skin_record =~ s/\$RECORD_COLOR/$color/g;
		$skin_record =~ s/\$RECORD_ICON/$icon/g;
		$skin_record =~ s/\$RECORD_HOST/$host/g;
		$skin_record =~ s/\$RECORD_LINK/$script?no=$no/g;

		if ($new_flag == 1) {
			$skin_record =~ s/<!--RECORD_NEW_START-->//g;
			$skin_record =~ s/<!--RECORD_NEW_END-->//g;
		} else {
			$skin_record =~ s/<!--RECORD_NEW_START-->/<!--/g;
			$skin_record =~ s/<!--RECORD_NEW_END-->/-->/g;
		}

		$html{'record'} .= &skin_basic($skin_record);
	}

	$html{'record'} .= &skin_basic($skin{'record_foot'});

	return;
}

### コンテンツリスト
sub contents {
	open(CONTENTS, "$contents_file") || &error("コンテンツリスト保存ファイルを読み出せません。");
	local @contents = <CONTENTS>;
	close(CONTENTS);

	$html{'contents'} = &skin_basic($skin{'contents_head'});

	if ($contents_fields[0] ne '') {
		local %contents = ();
		foreach (@contents) {
			chop($_);
			local($name, $url, $field) = split(/\t/);

			if ($field eq '') {
				$field = '未分類';
			}

			$contents{"$field"} .= "$name\t$url\t$field\n";
		}
		if ($contents{'未分類'} ne '') {
			push(@contents_fields, '未分類');
		}

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

			$skin_contents_field_head = $skin{'contents_field_head'};
			$skin_contents_field_head =~ s/\$CONTENTS_FIELD/$_/g;

			$html{'contents'} .= &skin_basic($skin_contents_field_head);

			foreach $contents (split(/\n/, $contents{$_})) {
				local($name, $url, $field) = split(/\t/, $contents);

				$skin_contents = $skin{'contents'};
				$skin_contents =~ s/\$CONTENTS_NAME/$name/g;
				$skin_contents =~ s/\$CONTENTS_URL/$url/g;
				$skin_contents =~ s/\$CONTENTS_FIELD/$field/g;

				$html{'contents'} .= &skin_basic($skin_contents);
			}

			$skin_contents_field_foot = $skin{'contents_field_foot'};
			$html{'contents'} .= &skin_basic($skin_contents_field_foot);
		}
	} else {
		foreach (@contents) {
			chop($_);
			local($name, $url, $field) = split(/\t/);

			$skin_contents = $skin{'contents'};
			$skin_contents =~ s/\$CONTENTS_NAME/$name/g;
			$skin_contents =~ s/\$CONTENTS_URL/$url/g;
			$skin_contents =~ s/\$CONTENTS_FIELD/$field/g;

			$html{'contents'} .= &skin_basic($skin_contents);
		}
	}

	$html{'contents'} .= &skin_basic($skin{'contents_foot'});

	return;
}

### 分類一覧
sub field {
	local(@logs) = @_;

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

		if ((split(/\t/, $_))[2] == 1) {
			$fields{(split(/\t/, $_))[4]}++;
		}
	}

	$html{'field'} = &skin_basic($skin{'field_head'});

	local $i = 0;
	foreach (@fields) {
		if ($_ eq '') {
			next;
		}

		if ($fields{$_} eq '') {
			$fields{$_} = 0;
		}

		$skin_field = $skin{'field'};
		$skin_field =~ s/\$FIELD_NAME/$_/g;
		$skin_field =~ s/\$FIELD_NO/$fields{$_}/g;
		$skin_field =~ s/\$FIELD_LINK/$script?field=$i/g;

		$html{'field'} .= &skin_basic($skin_field);

		$i++;
	}

	$html{'field'} .= &skin_basic($skin{'field_foot'});

	return;
}

### 投稿者名一覧
sub author {
	local(@logs) = @_;

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

		if ((split(/\t/, $_))[2] == 1) {
			$author{(split(/\t/, $_))[5]}++;
		}
	}

	$html{'author'} = &skin_basic($skin{'author_head'});

	local $i = 0;
	foreach (@author) {
		if ($_ eq '') {
			next;
		}

		if ($author{$_} eq '') {
			$author{$_} = 0;
		}

		$skin_author = $skin{'author'};
		$skin_author =~ s/\$AUTHOR_NAME/$_/g;
		$skin_author =~ s/\$AUTHOR_NO/$author{$_}/g;
		$skin_author =~ s/\$AUTHOR_PROFILE_LINK/$script?profile=$i/g;
		$skin_author =~ s/\$AUTHOR_DIARY_LINK/$script?author=$i/g;

		$html{'author'} .= &skin_basic($skin_author);

		$i++;
	}

	$html{'author'} .= &skin_basic($skin{'author_foot'});

	return;
}

### 最近の受信トラックバック一覧
sub trackback_list {
	local @trackback = ();
	if (-e "${reserved_trackback_dir}new$reserved_trackback_ext") {
		open(TB, "${reserved_trackback_dir}new$reserved_trackback_ext") || &error_trackback("Read error");
		@trackback = <TB>;
		close(TB);
	}

	$html{'trackbacklist'} = &skin_basic($skin{'trackbacklist_head'});

	local $i = 0;
	foreach (@trackback) {
		if ($i >= $show_trackback_list) {
			last;
		}

		local($no, $time, $blog_name, $title, $url, $excerpt) = split(/\t/);

		($sec, $min, $hour, $day, $mon, $year, $week) = localtime($time);
		$time = sprintf("%02d/%02d-%02d:%02d", $mon + 1, $day, $hour, $min);

		$skin_trackbacklist = $skin{'trackbacklist'};
		$skin_trackbacklist =~ s/\$TRACKBACKLIST_NO/$no/g;
		$skin_trackbacklist =~ s/\$TRACKBACKLIST_TIME/$time/g;
		$skin_trackbacklist =~ s/\$TRACKBACKLIST_BLOGNAME/$blog_name/g;
		$skin_trackbacklist =~ s/\$TRACKBACKLIST_TITLE/$title/g;
		$skin_trackbacklist =~ s/\$TRACKBACKLIST_URL/$url/g;
		$skin_trackbacklist =~ s/\$TRACKBACKLIST_EXCERPT/$excerpt/g;

		$html{'trackbacklist'} .= &skin_basic($skin_trackbacklist);

		$i++;
	}

	$html{'trackbacklist'} .= &skin_basic($skin{'trackbacklist_foot'});

	return;
}

### 最近のコメント一覧
sub res_list {
	local(@logs) = @_;

	local %comment_list = ();
	foreach (@logs) {
		chop($_);
		local($no, $reno, $display, $date, $field, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($reno != 0) {
			$comment_list{"$date"} = "$no\t$reno\t$display\t$date\t$field\t$name\t$mail\t$url\t$subj\t$text\t$color\t$icon\t$file\t$pwd\t$host";
		}
	}

	$html{'reslist'} = &skin_basic($skin{'reslist_head'});

	local $i = 0;
	foreach (sort { $b <=> $a } keys %comment_list) {
		local($no, $reno, $display, $date, $field, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/, $comment_list{$_});

		$i++;
		if ($i > $show_res_list) {
			last;
		}

		($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date);
		$date = sprintf("%02d/%02d-%02d:%02d", $mon + 1, $day, $hour, $min);

		$skin_reslist = $skin{'reslist'};
		$skin_reslist =~ s/\$RESLIST_NO/$no/g;
		$skin_reslist =~ s/\$RESLIST_RENO/$reno/g;
		$skin_reslist =~ s/\$RESLIST_DATE/$date/g;
		$skin_reslist =~ s/\$RESLIST_FIELD/$field/g;
		$skin_reslist =~ s/\$RESLIST_NAME/$name/g;
		$skin_reslist =~ s/\$RESLIST_MAIL/$mail/g;
		$skin_reslist =~ s/\$RESLIST_URL/$url/g;
		$skin_reslist =~ s/\$RESLIST_SUBJ/$subj/g;
		$skin_reslist =~ s/\$RESLIST_TEXT/$text/g;
		$skin_reslist =~ s/\$RESLIST_COLOR/$color/g;
		$skin_reslist =~ s/\$RESLIST_ICON/$icon/g;
		$skin_reslist =~ s/\$RESLIST_HOST/$host/g;
		$skin_reslist =~ s/\$RESLIST_LINK/$script?mode=res&amp;no=$reno/g;

		$html{'reslist'} .= &skin_basic($skin_reslist);
	}

	$html{'reslist'} .= &skin_basic($skin{'reslist_foot'});

	return;
}

### ミニリンク集
sub link_list {
	open(LINK, "$link_file") || &error("ミニリンク集保存ファイルを読み出せません。");
	local @link = <LINK>;
	close(LINK);

	$html{'linklist'} = &skin_basic($skin{'linklist_head'});

	if ($link_fields[0] ne '') {
		local %link = ();
		foreach (@link) {
			chop($_);
			local($name, $url, $field) = split(/\t/);

			if ($field eq '') {
				$field = '未分類';
			}

			$link{"$field"} .= "$name\t$url\t$field\n";
		}
		if ($link{'未分類'} ne '') {
			push(@link_fields, '未分類');
		}

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

			$skin_linklist_field_head = $skin{'linklist_field_head'};
			$skin_linklist_field_head =~ s/\$LINKLIST_FIELD/$_/g;

			$html{'linklist'} .= &skin_basic($skin_linklist_field_head);

			foreach $link (split(/\n/, $link{$_})) {
				local($name, $url, $field) = split(/\t/, $link);

				$skin_linklist = $skin{'linklist'};
				$skin_linklist =~ s/\$LINKLIST_NAME/$name/g;
				$skin_linklist =~ s/\$LINKLIST_URL/$url/g;
				$skin_linklist =~ s/\$LINKLIST_FIELD/$field/g;

				$html{'linklist'} .= &skin_basic($skin_linklist);
			}

			$skin_linklist_field_foot = $skin{'linklist_field_foot'};
			$html{'linklist'} .= &skin_basic($skin_linklist_field_foot);
		}
	} else {
		foreach (@link) {
			chop($_);
			local($name, $url, $field) = split(/\t/);

			$skin_linklist = $skin{'linklist'};
			$skin_linklist =~ s/\$LINKLIST_NAME/$name/g;
			$skin_linklist =~ s/\$LINKLIST_URL/$url/g;
			$skin_linklist =~ s/\$LINKLIST_FIELD/$field/g;

			$html{'linklist'} .= &skin_basic($skin_linklist);
		}
	}

	$html{'linklist'} .= &skin_basic($skin{'linklist_foot'});

	return;
}

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

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

	local @icon_info = @icon;
	local %icon_info;
	if ($icon_mode == 1 && $icon_display != 1) {
		if ($admin_icon_mode == 1) {
			push(@icon_info, @admin_icon);
		}
		while (scalar(@icon_info) != 0) {
			local $icon_file = shift(@icon_info);
			local $icon_name = shift(@icon_info);

			$icon_info{"$icon_file"} = $icon_name;
		}
	}

	local $start = 0;
	local $end   = 0;
	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;
	}

	local $field_query  = '';
	local $author_query = '';
	if ($in{'page'} > 0) {
		if ($in{'field'} ne '') {
			$field_query = "field=$in{'field'}&amp;";
		} else {
			$field_query = '';
		}
		if ($in{'author'} ne '') {
			$author_query = "author=$in{'author'}&amp;";
		} else {
			$author_query = '';
		}
		local $n = $in{'page'} - 1;

		$skin{'navi'} =~ s/<!--LINK_PREV_START-->/<a href="$script?$field_query$author_query${mode}page=$n">/g;
		$skin{'navi'} =~ s/<!--LINK_PREV_END-->/<\/a>/g;
	} else {
		$skin{'navi'} =~ s/<!--LINK_PREV_START-->//g;
		$skin{'navi'} =~ s/<!--LINK_PREV_END-->//g;
	}
	if (int(($parent - 1) / $default_show) > $in{'page'}) {
		if ($in{'field'} ne '') {
			$field_query = "field=$in{'field'}&amp;";
		} else {
			$field_query = '';
		}
		if ($in{'author'} ne '') {
			$author_query = "author=$in{'author'}&amp;";
		} else {
			$author_query = '';
		}
		local $n = $in{'page'} + 1;

		$skin{'navi'} =~ s/<!--LINK_NEXT_START-->/<a href="$script?$field_query$author_query${mode}page=$n">/g;
		$skin{'navi'} =~ s/<!--LINK_NEXT_END-->/<\/a>/g;
	} else {
		$skin{'navi'} =~ s/<!--LINK_NEXT_START-->//g;
		$skin{'navi'} =~ s/<!--LINK_NEXT_END-->//g;
	}

	$html{'logs'} = &skin_basic($skin{'logs_head'});

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

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

		if ($display == 0) {
			next;
		}

		if ($subj eq '' || $in{'profile'} ne '') {
			$diary_flag = 0;
		}
		if ($subj eq '') {
			$subj = 'No Data';
		}

		if ($color_mode == 1 && $diary_flag == 1) {
			if ($color eq '') {
				$color = $color[0];
			}
			$color_start = qq|<span style="color:$color;">|;
			$color_end   = qq|</span>|;

			$text = "$color_start$text$color_end";
		} else {
			$color_start = '';
			$color_end   = '';
		}

		foreach $html (@no_paragrath_html_a) {
			if ($html eq '') {
				next;
			}
			$text =~ s/(<$html.*>)/$color_end<\/p>$1/gi;
			$text =~ s/(<\/$html>)/$1<p>$color_start/gi;
		}
		foreach $html (@no_paragrath_html_b) {
			if ($html eq '') {
				next;
			}
			$text =~ s/(<${html}[^>]*>)/$color_end<\/p>$1<p>$color_start/gi;
		}

		if ($paragraph_mode == 1) {
			$text =~ s/<br><br>/$color_end<\/p><p>$color_start/g;
		}
		if ($auto_link == 1) {
			$text = &auto_link($text);
		}

		if ($icon_mode == 1 && $icon_display != 1 && $icon ne '') {
			$icon = qq|<img src="$icon_dir$icon" alt="$icon_info{$icon}" class="icon">|;
		} else {
			$icon = '';
		}

		if (($in{'mode'} eq 'search' && $in{'no'} eq '' && $flag == 1) || ($flag == 1 && $reno == 0)) {
			$skin_log_foot = $skin{'log_foot'};
			$html{'logs'} .= &skin_basic($skin_log_foot);
		}

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

			$skin_log_head = $skin{'log_head'};
			if ($in{'date'} ne '') {
				$skin_log_head =~ s/\$LOG_HEAD_ID/ id="diary$i"/g;
			} else {
				$skin_log_head =~ s/\$LOG_HEAD_ID//g;
			}

			$html{'logs'} .= &skin_basic($skin_log_head);
		}

		if (($in{'mode'} eq 'search' && $in{'no'} eq '') || $reno == 0) {
			$skin_log = $skin{'log'};

			$skin_log =~ s/\$LOG_NAME/$name/g;
			$skin_log =~ s/\$LOG_SUBJ/$subj/g;
			$skin_log =~ s/\$LOG_ICON/$icon/g;
			$skin_log =~ s/\$LOG_HOST/$host/g;

			($file_info, $text) = &set_upfile($text, $file);
			$skin_log =~ s/\$LOG_FILE/$file_info/g;

			if ($pause_string ne '' && $reno == 0 && $text =~ /$pause_string/) {
				if ($in{'no'} eq '') {
					$text = (split(/$pause_string/, $text, 2))[0];
					$text .= qq|... <a href="$script?no=$no#continue">続きを読む</a>|;
				} else {
					$text =~ s/$pause_string/<a name="continue"><\/a>/;
				}
			}

			$skin_log =~ s/\$LOG_TEXT/$text/g;

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

				local $year   = sprintf("%04d", $1);
				local $month  = sprintf("%02d", $2);
				local $day    = sprintf("%02d", $3);
				local $hour   = sprintf("%02d", $4);
				local $minute = sprintf("%02d", $5);
				local $week   = $week[&getweek($day, $year, $month)];

				$date = "$year年$month月$day日（$week）";

				if ($time_mode == 1) {
					$date .= "$hour:$minute";
				}
			}

			if ($in{'mode'} eq 'past') {
				$skin_log =~ s/\$LOG_DATE_LINK/$script?mode=past&amp;view=$in{'view'}&amp;no=$in{'no'}/g;
			} else {
				$skin_log =~ s/\$LOG_DATE_LINK/$script?no=$no/g;
			}
			if ($diary_flag == 1) {
				$skin_log =~ s/\$LOG_DATE/$date/g;

				$skin_log =~ s/\$LOG_FIXED_LINK/$script?no=$no/g;

				$skin_log =~ s/<!--LOG_DATE_START-->//g;
				$skin_log =~ s/<!--LOG_DATE_END-->//g;

				$skin_log =~ s/<!--LOG_FIXED_START-->//g;
				$skin_log =~ s/<!--LOG_FIXED_END-->//g;
			} else {
				$skin_log =~ s/<!--LOG_DATE_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_DATE_END-->/-->/g;

				$skin_log =~ s/<!--LOG_FIXED_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_FIXED_END-->/-->/g;
			}

			local $field_no = 0;
			foreach $field_name (@fields) {
				if ($field eq $field_name) {
					last;
				}
				$field_no++;
			}

			local $author_no = 0;
			if ($author_mode == 1 && $reno == 0) {
				foreach $author_name (@author) {
					if ($name eq $author_name) {
						last;
					}
					$author_no++;
				}
			}

			if ($res_mode == 1 && $in{'mode'} eq 'search' && $in{'no'} eq '') {
				$skin_log =~ s/<!--LOG_ALLSHOW_START-->//g;
				$skin_log =~ s/<!--LOG_ALLSHOW_END-->//g;

				local $show = 0;
				if ($reno == 0) {
					$show = $no;
				} else {
					$show = $reno;
				}
				$skin_log =~ s/\$LOG_ALLSHOW_LINK/$script?mode=search&amp;obje=$in{'obje'}&amp;no=$show/g;

				$skin_log =~ s/<!--LOG_ALLSHOW_START-->//g;
				$skin_log =~ s/<!--LOG_ALLSHOW_END-->//g;
			} else {
				$skin_log =~ s/<!--LOG_ALLSHOW_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_ALLSHOW_END-->/-->/g;
			}

			if ($res_mode == 1 && $logs[0] + 0 != 0 && ($in{'mode'} eq '' || $in{'mode'} eq 'view')) {
				if ($comment{$no} eq '') {
					$comment = 0;
				} else {
					$comment = $comment{$no};
				}

				$skin_log =~ s/<!--LOG_COMMENT_START-->//g;
				$skin_log =~ s/<!--LOG_COMMENT_END-->//g;

				$skin_log =~ s/\$LOG_COMMENT_LINK/$script?mode=res&amp;no=$no#comment/g;
				$skin_log =~ s/\$LOG_COMMENT/$comment/g;
			} else {
				$skin_log =~ s/<!--LOG_COMMENT_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_COMMENT_END-->/-->/g;
			}
			if ($trackback_mode == 1 && $in{'preview'} ne 'on' && $diary_flag == 1) {
				if (-e "$reserved_trackback_dir$no$reserved_trackback_ext") {
					open(TB, "$reserved_trackback_dir$no$reserved_trackback_ext") || &error("受信トラックバック一覧ファイルを読み出せません。");
					local @trackback = <TB>;
					close(TB);

					$trackback = $#trackback + 1;
				} else {
					$trackback = 0;
				}

				$skin_log =~ s/<!--LOG_TRACKBACK_START-->//g;
				$skin_log =~ s/<!--LOG_TRACKBACK_END-->//g;

				$skin_log =~ s/\$LOG_TRACKBACK_LINK/$script?mode=trackback&amp;no=$no/g;
				$skin_log =~ s/\$LOG_TRACKBACKT/$trackback/g;
			} else {
				$skin_log =~ s/<!--LOG_TRACKBACK_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_TRACKBACK_END-->/-->/g;
			}

			if ($new_flag == 1 && $diary_flag == 1) {
				$skin_log =~ s/<!--LOG_NEW_START-->//g;
				$skin_log =~ s/<!--LOG_NEW_END-->//g;
			} else {
				$skin_log =~ s/<!--LOG_NEW_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_NEW_END-->/-->/g;
			}
			if ($fields[1] ne '' && $diary_flag == 1) {
				$skin_log =~ s/<!--LOG_FIELD_START-->//g;
				$skin_log =~ s/<!--LOG_FIELD_END-->//g;

				$skin_log =~ s/\$LOG_FIELD_LINK/$script?field=$field_no/g;
				$skin_log =~ s/\$LOG_FIELD/$field/g;
			} else {
				$skin_log =~ s/<!--LOG_FIELD_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_FIELD_END-->/-->/g;
			}

			if ($author_mode == 1) {
				if ($diary_flag == 0) {
					$skin_log =~ s/<!--LOG_PROFILE_START-->/<!--/g;
					$skin_log =~ s/<!--LOG_PROFILE_END-->/-->/g;

					if ($in{'profile'} eq '') {
						$in{'profile'} = $in{'author'};
					}
					$skin_log =~ s/\$LOG_AUTHOR_LINK/$script?author=$in{'profile'}/g;
					$skin_log =~ s/\$LOG_AUTHOR/$author[$in{'profile'}]/g;

					$skin_log =~ s/<!--LOG_AUTHOR_START-->//g;
					$skin_log =~ s/<!--LOG_AUTHOR_END-->//g;
				} else {
					$skin_log =~ s/<!--LOG_PROFILE_START-->//g;
					$skin_log =~ s/<!--LOG_PROFILE_END-->//g;

					$skin_log =~ s/\$LOG_PROFILE_LINK/$script?profile=$author_no/g;
					$skin_log =~ s/\$LOG_PROFILE/$name/g;

					$skin_log =~ s/<!--LOG_AUTHOR_START-->/<!--/g;
					$skin_log =~ s/<!--LOG_AUTHOR_END-->/-->/g;
				}
			} else {
				$skin_log =~ s/<!--LOG_PROFILE_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_PROFILE_END-->/-->/g;

				$skin_log =~ s/<!--LOG_AUTHOR_START-->/<!--/g;
				$skin_log =~ s/<!--LOG_AUTHOR_END-->/-->/g;
			}

			$html{'logs'} .= &skin_basic($skin_log);

			if ($diary_flag == 1 && $html_flag == 0 && ($file_name{'file1'} ne '' || $file_name{'file2'} ne '' || $file_name{'file3'} ne '')) {
				$skin_filepreview = $skin{'filepreview'};

				if ($file_name{'file1'} ne '') {
					$file_name{'file1'} =~ /([^\/\\]*)\.([^.\/\\]*)$/;
					local $filename1 = "$1\.$2";

					$skin_filepreview =~ s/\$FILE1PREVIEW/$filename1/g;
				} else {
					$skin_filepreview =~ s/<!--FILE1PREVIEW_START-->/<!--/g;
					$skin_filepreview =~ s/<!--FILE1PREVIEW_END-->/-->/g;
				}
				if ($file_name{'file2'} ne '') {
					$file_name{'file2'} =~ /([^\/\\]*)\.([^.\/\\]*)$/;
					local $filename2 = "$1\.$2";

					$skin_filepreview =~ s/\$FILE2PREVIEW/$filename2/g;
				} else {
					$skin_filepreview =~ s/<!--FILE2PREVIEW_START-->/<!--/g;
					$skin_filepreview =~ s/<!--FILE2PREVIEW_END-->/-->/g;
				}
				if ($file_name{'file3'} ne '') {
					$file_name{'file3'} =~ /([^\/\\]*)\.([^.\/\\]*)$/;
					local $filename3 = "$1\.$2";

					$skin_filepreview =~ s/\$FILE3PREVIEW/$filename3/g;
				} else {
					$skin_filepreview =~ s/<!--FILE3PREVIEW_START-->/<!--/g;
					$skin_filepreview =~ s/<!--FILE3PREVIEW_END-->/-->/g;
				}

				$html{'logs'} .= &skin_basic($skin_filepreview);
			} elsif ($in{'mode'} eq 'res') {
				$html{'logs'} .= &skin_basic($skin{'comment_head'});
			}
		} else {
			$skin_comment = $skin{'comment'};
			$skin_comment =~ s/\$COMMENT_NAME/$name/g;
			$skin_comment =~ s/\$COMMENT_TEXT/$text/g;
			$skin_comment =~ s/\$COMMENT_ICON/$icon/g;
			$skin_comment =~ s/\$COMMENT_FILE/$file/g;
			$skin_comment =~ s/\$COMMENT_HOST/$host/g;

			if ($in{'preview'} ne 'on' && $new_date != 0 && time() - $date < $new_date * 24 * 60 * 60) {
				$new_flag = 1;
			} else {
				$new_flag = 0;
			}
			($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);

			$skin_comment =~ s/\$COMMENT_DATE/$date/g;

			if ($mail ne '') {
				$skin_comment =~ s/<!--COMMENT_MAIL_START-->/<a href="mailto:$mail">/g;
				$skin_comment =~ s/<!--COMMENT_MAIL_END-->/<\/a>/g;
			} else {
				$skin_comment =~ s/<!--COMMENT_MAIL_START-->/<!--/g;
				$skin_comment =~ s/<!--COMMENT_MAIL_END-->/-->/g;
			}
			if ($url ne '') {
				$skin_comment =~ s/<!--COMMENT_URL_START-->/<a href="$url">/g;
				$skin_comment =~ s/<!--COMMENT_URL_END-->/<\/a>/g;
			} else {
				$skin_comment =~ s/<!--COMMENT_URL_START-->/<!--/g;
				$skin_comment =~ s/<!--COMMENT_URL_END-->/-->/g;
			}
			if ($new_flag == 1) {
				$skin_comment =~ s/<!--COMMENT_NEW_START-->//g;
				$skin_comment =~ s/<!--COMMENT_NEW_END-->//g;
			} else {
				$skin_comment =~ s/<!--COMMENT_NEW_START-->/<!--/g;
				$skin_comment =~ s/<!--COMMENT_NEW_END-->/-->/g;
			}

			$html{'logs'} .= &skin_basic($skin_comment);
		}
	}

	if ($in{'mode'} eq 'res') {
		$html{'logs'} .= &skin_basic($skin{'comment_foot'});
	}
	if ($flag == 1) {
		$html{'logs'} .= &skin_basic($skin{'log_foot'});
	}

	$html{'logs'} .= &skin_basic($skin{'logs_foot'});

	if ($in{'mode'} ne 'search' && $in{'mode'} ne 'past' && $in{'date'} eq '' && $in{'year'} eq '' && $in{'month'} eq '' && $in{'no'} eq '' && $in{'profile'} eq '' && $in{'preview'} ne 'on') {
		$html{'logs'} .= &skin_basic($skin{'navi'});
	}

	return;
}

### アップロードファイル挿入
sub set_upfile {
	local ($text, $file) = @_;

	local @file_info = ();
	local @file = split(/<>/, $file);
	local $j = 0;
	if ($upload_mode == 1 && $file ne '') {
		foreach $file_name (@file) {
			if (! -e "$upfile_dir$file_name") {
				next;
			}

			$j++;

			local $file_info     = '';
			local $file_insert_c = '';
			local $file_insert_l = '';
			local $file_insert_r = '';
			local $file_insert   = '';

			$file_name =~ /[^\/\\]*\.([^.\/\\]*)$/;
			local $ext = ".$1";
			local $file_size = &comma(int((stat("$upfile_dir$file_name"))[7] / 1024));
			if ($file_size == 0) {
				$file_size = 1;
			}
			if ($ext eq '.gif' || $ext eq '.jpeg' || $ext eq '.jpg' || $ext eq '.jpe' || $ext eq '.png') {
				local $img_width  = 0;
				local $img_height = 0;
				local $org_img_width  = 0;
				local $org_img_height = 0;
				local $thumbnail_flag = 0;
				if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file_name" && ($img_single_max_width == 0 || ($file[1] ne '' || $file[2] ne ''))) {
					open(IMG, "$thumbnail_dir$file_name");
					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);
					}
					close(IMG);

					$file_info     = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="file">|;
					###$file_info     = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" style="float:left;margin : 0px 20px 10px 0px ;">|;
					$file_insert_c = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert" style="text-align:center;display:block;">|;
					$file_insert_l = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert" style="float:left;">|;
					$file_insert_r = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert" style="float:right;">|;
					$file_insert   = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert">|;

					if ($img_show_way == 2) {
						open(IMG, "$upfile_dir$file_name");
						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);
						}
						close(IMG);

						$org_img_width  = $img_width;
						$org_img_height = $img_height;
					}

					$thumbnail_flag = 1;
				} else {
					open(IMG, "$upfile_dir$file_name");
					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);
					}
					close(IMG);
					if ($img_show_way == 2) {
						$org_img_width  = $img_width;
						$org_img_height = $img_height;
					}
					if ($img_single_max_width != 0 && $file[1] eq '' && $file[2] eq '') {
						if ($img_width > $img_single_max_width) {
							$img_height = int($img_height / ($img_width / $img_single_max_width));
							$img_width  = $img_single_max_width;

							$thumbnail_flag = 1;
						} else {
							$thumbnail_flag = 0;
						}
					} else {
						if ($img_width > $img_max_width) {
							$img_height = int($img_height / ($img_width / $img_max_width));
							$img_width  = $img_max_width;

							$thumbnail_flag = 1;
						} else {
							$thumbnail_flag = 0;
						}
					}

					$file_info     = qq|<img src="$upfile_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="file">|;
					###$file_info     = qq|<img src="$thumbnail_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" style="float:left;margin : 0px 20px 10px 0px ;">|;
					$file_insert_c = qq|<img src="$upfile_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert" style="text-align:center;display:block;">|;
					$file_insert_l = qq|<img src="$upfile_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert" style="float:left;">|;
					$file_insert_r = qq|<img src="$upfile_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert" style="float:right;">|;
					$file_insert   = qq|<img src="$upfile_url$file_name" alt="アップロードファイル ${file_size}KB" width="$img_width" height="$img_height" class="insert">|;
				}

				if ($thumbnail_flag == 1) {
					if ($img_show_way == 0) {
						$link_start = qq|<a href="$upfile_url$file_name">|;
						$link_end   = '</a>';
					} elsif ($img_show_way == 1) {
						$link_start = qq|<a href="$upfile_url$file_name" target="_blank">|;
						$link_end   = '</a>';
					} elsif ($img_show_way == 2) {
						$link_start = qq|<a href="$upfile_url$file_name" onClick="window.open('$script?mode=img&amp;file=$file_name&amp;width=$org_img_width&amp;height=$org_img_height&amp;size=$file_size', 'Img', 'width=$org_img_width,height=$org_img_height');return false;" onKeypress="this.onclick();">|;
						$link_end   = '</a>';
					}

					$file_info     = "$link_start$file_info$link_end";
					$file_insert_c = "$link_start$file_insert_c$link_end";
					$file_insert_l = "$link_start$file_insert_l$link_end";
					$file_insert_r = "$link_start$file_insert_r$link_end";
					$file_insert   = "$link_start$file_insert$link_end";
				}

				$file_insert_c[$j] = $file_insert_c;
				$file_insert_l[$j] = $file_insert_l;
				$file_insert_r[$j] = $file_insert_r;
				$file_insert[$j]   = $file_insert;
			} else {
				$file_info = qq|<p><a href="$upfile_url$file_name">$file_name（${file_size}KB）</a></p>|;
				$file_insert[$j] = qq|<a href="$upfile_url$file_name">$file_name（${file_size}KB）</a>|;
			}

			if ($text !~ /\$FILE$j/) {
				push(@file_info, $file_info);
			}
		}
		$file_info = '';
		foreach $file (@file_info) {
			$file_info .= $file;
		}
	} else {
		$file_info = '';
		$j = 0;
	}

	foreach $k (1 .. 3) {
		if ($k > $j) {
			last;
		}

		$text =~ s/\$FILE${k}_c/$file_insert_c[$k]/g;
		$text =~ s/\$FILE${k}_l/$file_insert_l[$k]/g;
		$text =~ s/\$FILE${k}_r/$file_insert_r[$k]/g;
		$text =~ s/\$FILE${k}/$file_insert[$k]/g;
	}

	return ($file_info, $text);
}

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

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

		if ($display == 0) {
			next;
		}

		if ($reno != 0) {
			$comment{$reno}++;
		}

		if ($in{'date'} ne '') {
			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
				local $year  = $1;
				local $month = $2;
				local $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)(\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, $_);
			}
		} elsif ($in{'field'} ne '') {
			if ($fields[$in{'field'}] eq $field) {
				push(@show, $_);
			}
		} elsif ($in{'author'} ne '') {
			if ($author[$in{'author'}] eq $name) {
				push(@show, $_);
			}
		} else {
			push(@show, $_);
		}
	}

	if (($in{'date'} ne '' || $in{'year'} ne '' || $in{'month'} ne '' || $in{'no'} ne '') && $show[0] eq '') {
		opendir(DIR, "$past_dir") || &error("過去ログディレクトリを読み出せません。");
		local @dir = readdir(DIR);
		closedir(DIR);

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

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

				push(@past, @file);
			}
		}

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

			if ($display == 0) {
				next;
			}

			if ($reno != 0) {
				$comment{$reno}++;
			}

			if ($in{'date'} ne '') {
				if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
					local $year  = $1;
					local $month = $2;
					local $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)(\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, $_);
				}
			}
		}
	}

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

	return @logs;
}

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

		$form_subj  = 'Re:' . (split(/\t/, $logs[0]))[8];
		$form_icon  = $cookie{'icon'};
		$form_color = $cookie{'color'};

		$skin{'form'} =~ s/\$COOKIE_NAME/$cookie{'name'}/g;
		$skin{'form'} =~ s/\$COOKIE_MAIL/$cookie{'mail'}/g;
		$skin{'form'} =~ s/\$COOKIE_URL/$cookie{'url'}/g;
		$skin{'form'} =~ s/\$FORM_TEXT//g;
		$skin{'form'} =~ s/\$COOKIE_PWD/$cookie{'pwd'}/g;

		$skin{'form'} =~ s/\$FORM_MODE/regist/g;
		$skin{'form'} =~ s/\$FORM_NO//g;
		$skin{'form'} =~ s/\$FORM_RENO/$in{'no'}/g;
		$skin{'form'} =~ s/\$FORM_DATE//g;
		$skin{'form'} =~ s/\$FORM_SUBJ/$form_subj/g;
		$skin{'form'} =~ s/\$FORM_USERPWD//g;
	} elsif ($in{'back'} ne '') {
		if ($in{'url'} eq '') {
			$in{'url'} = 'http://';
		}
		$in{'text'} = &create_plain($in{'text'});

		$form_icon  = $in{'icon'};
		$form_color = $in{'color'};

		$skin{'form'} =~ s/\$COOKIE_NAME/$in{'name'}/g;
		$skin{'form'} =~ s/\$COOKIE_MAIL/$in{'mail'}/g;
		$skin{'form'} =~ s/\$COOKIE_URL/$in{'url'}/g;
		$skin{'form'} =~ s/\$FORM_TEXT/$in{'text'}/g;
		$skin{'form'} =~ s/\$COOKIE_PWD/$in{'pwd'}/g;

		$skin{'form'} =~ s/\$FORM_MODE/regist/g;
		$skin{'form'} =~ s/\$FORM_NO/$in{'no'}/g;
		$skin{'form'} =~ s/\$FORM_RENO/$in{'reno'}/g;
		$skin{'form'} =~ s/\$FORM_DATE//g;
		$skin{'form'} =~ s/\$FORM_SUBJ/$in{'subj'}/g;
		$skin{'form'} =~ s/\$FORM_USERPWD/$in{'user_pwd'}/g;
	} else {
		$text = &create_plain($text);

		$form_icon  = $icon;
		$form_color = $color;

		$skin{'form'} =~ s/\$COOKIE_NAME/$name/g;
		$skin{'form'} =~ s/\$COOKIE_MAIL/$mail/g;
		$skin{'form'} =~ s/\$COOKIE_URL/$url/g;
		$skin{'form'} =~ s/\$FORM_TEXT/$text/g;
		$skin{'form'} =~ s/\$COOKIE_PWD/$in{'user_pwd'}/g;

		$skin{'form'} =~ s/\$FORM_MODE/userdel/g;
		$skin{'form'} =~ s/\$FORM_NO/$no/g;
		$skin{'form'} =~ s/\$FORM_RENO/$reno/g;
		$skin{'form'} =~ s/\$FORM_DATE/$date/g;
		$skin{'form'} =~ s/\$FORM_SUBJ/$subj/g;
		$skin{'form'} =~ s/\$FORM_USERPWD/$in{'user_pwd'}/g;
	}

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

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

		local $icon_info = qq|<select name="icon"><option value="">なし</option>|;
		while (scalar(@icon) != 0) {
			local $icon_file = shift(@icon);
			local $icon_name = shift(@icon);
			if ($form_icon eq $icon_file) {
				$icon_info .= qq|<option value="$icon_file" selected>$icon_name</option>|;
			} else {
				$icon_info .= qq|<option value="$icon_file">$icon_name</option>|;
			}
		}
		$icon_info .= qq|</select>|;

		$skin{'form'} =~ s/\$COOKIE_ICON/$icon_info/g;
	} else {
		$skin{'form'} =~ s/<!--FORM_ICON_START-->/<!--/g;
		$skin{'form'} =~ s/<!--FORM_ICON_END-->/-->/g;
	}
	if ($color_mode == 1) {
		local $color_info = '';
		foreach (@color) {
			if ($_ eq '') {
				next;
			}

			local $color_label = $_;
			$color_label =~ s/#//g;

			if ($form_color eq $_) {
				$color_info .= qq|<input type="radio" name="color" id="radio_$color_label" value="$_" class="radio" checked><label for="radio_$color_label" style="color:$_;">■</label> |;
			} else {
				$color_info .= qq|<input type="radio" name="color" id="radio_$color_label" value="$_" class="radio"><label for="radio_$color_label" style="color:$_;">■</label> |;
			}
		}
		$skin{'form'} =~ s/\$COOKIE_COLOR/$color_info/g;
	} else {
		$skin{'form'} =~ s/<!--FORM_COLOR_START-->/<!--/g;
		$skin{'form'} =~ s/<!--FORM_COLOR_END-->/-->/g;
	}

	if ($show_preview == 0 || $in{'mode'} eq 'userdel') {
		$skin{'form'} =~ s/<!--FORM_PREVIEW_START-->/<input type="hidden" name="preview" value="off"><!--/g;
		$skin{'form'} =~ s/<!--FORM_PREVIEW_END-->/-->/g;
	} elsif ($show_preview == 1) {
		if ($in{'mode'} eq 'res' && $in{'back'} eq '') {
			if ($cookie{'preview'} eq 'on') {
				$skin{'form'} =~ s/\$COOKIE_PREVIEW/ checked/g;
			} else {
				$skin{'form'} =~ s/\$COOKIE_PREVIEW//g;
			}
		} elsif ($in{'back'} ne '') {
			if ($in{'c_preview'} eq 'on') {
				$skin{'form'} =~ s/\$COOKIE_PREVIEW/ checked/g;
			} else {
				$skin{'form'} =~ s/\$COOKIE_PREVIEW//g;
			}
		}
	} else {
		$skin{'form'} =~ s/<!--FORM_PREVIEW_START-->/<input type="hidden" name="preview" value="on"><!--/g;
		$skin{'form'} =~ s/<!--FORM_PREVIEW_END-->/-->/g;
	}

	print &skin_basic($skin{'form'});

	return;
}

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

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

	if ($res[0] eq '') {
		opendir(DIR, "$past_dir") || &error("過去ログディレクトリを読み出せません。");
		local @dir = readdir(DIR);
		closedir(DIR);

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

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

				push(@past, @file);
			}
		}

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

		$past_flag = 1;
	}

	@logs = @res;

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/コメント一覧<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print qq|</div>\n|;

	&showlog(@logs);
	print $html{'logs'};

	if ($past_flag == 1) {
		print &skin_basic($skin{'noform'});
	} else {
		&form();
	}

	print &skin_basic($skin{'link'});

	&footer();
}

### RSS配信
sub rss {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません。");
	local @logs = <FILE>;
	close(FILE);

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

		if ($reno == 0) {
			$i++;
		}
		if ($i > $rss_size) {
			last;
		}
		if ($display == 0) {
			next;
		}

		if ($reno == 0) {
			local $code = '';
			if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
				$code = "$1$2$3";
			}

			local @text = split(/<br><br>/, $text);
			$text[0] =~ s/<[^>]*>//g;
			$text[0] =~ s/&/&amp;/g;

			push(@items, "$subj\t$original_file?date=$code\t$text[0]\t$field\t$date");
		}
	}

	local $rss = &make_rss("$original_file?mode=rss", $site_title, $site_url, $site_description, $original_file, @items);

	print "Content-Type: text/xml\n\n";
	print $rss;
	exit();
}

### 受信トラックバック一覧表示
sub trackback {
	if ($in{'no'} =~ /[^\w\-\_]/) {
		&error("トラックバックIDは半角英数字で指定してください。");
	}

	local @trackback = ();
	if (-e "$reserved_trackback_dir$in{'no'}$reserved_trackback_ext") {
		open(TB, "$reserved_trackback_dir$in{'no'}$reserved_trackback_ext") || &error("受信トラックバック一覧ファイルを読み出せません。");
		@trackback = <TB>;
		close(TB);
	}

	@trackback = reverse(@trackback);

	&header();

	$skin{'trackback_head'} =~ s/\$TRACKBACK_URL/$trackback_script\/$in{'no'}/g;

	print &skin_basic($skin{'trackback_head'});

	foreach (@trackback) {
		chop($_);
		local($tb_no, $tb_time, $tb_blog_name, $tb_title, $tb_url, $tb_excerpt) = split(/\t/);

		($sec, $min, $hour, $day, $mon, $year, $week) = localtime($tb_time);
		$tb_time = sprintf("%04d年%02d月%02d日（%s）%02d:%02d", $year + 1900, $mon + 1, $day, $week[$week], $hour, $min);

		$skin_trackback = $skin{'trackback'};
		$skin_trackback =~ s/\$TRACKBACKLIST_NO/$tb_no/g;
		$skin_trackback =~ s/\$TRACKBACKLIST_DATE/$tb_time/g;
		$skin_trackback =~ s/\$TRACKBACKLIST_TITLE/$tb_blog_name/g;
		$skin_trackback =~ s/\$TRACKBACKLIST_SUBJ/$tb_title/g;
		$skin_trackback =~ s/\$TRACKBACKLIST_URL/$tb_url/g;
		$skin_trackback =~ s/\$TRACKBACKLIST_TEXT/$tb_excerpt/g;

		print &skin_basic($skin_trackback);
	}

	print &skin_basic($skin{'trackback_foot'});
	print &skin_basic($skin{'link'});

	&footer();
}

### 投稿画像表示
sub img {
	print <<"_HTML_";
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//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 style="margin:0px">
<a href="javascript:void(0)" onClick="window.close();" onKeypress="this.onclick();"><img src="$upfile_url$in{'file'}" alt="クリックするとウインドウを閉じます" width="$in{'width'}" height="$in{'height'}" style="border: 0px solid #000000;"></a>
</body>

</html>
_HTML_

	exit;
}

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

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/アイコン一覧<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print <<"_HTML_";
	<ul>
		<li>選択できるアイコンは以下のとおりです。</li>
	</ul>
	<table summary="アイコン一覧">
_HTML_

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

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

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

		local $icon_file = shift(@icon);
		local $icon_name = shift(@icon);

		print qq|\t\t\t<td><p>$icon_name</p><p><img src="$icon_dir$icon_file" alt="$icon_name"></p></td>\n|;

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

		if ($_ == $icon_no && ($_ % 5) != 0) {
			foreach (1 .. (5 - ($_ % 5))) {
				print qq|\t\t\t<td></td>\n|;
			}
			print "\t\t</tr>\n";
		}
	}

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

	print &skin_basic($skin{'link'});

	&footer();
}

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

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

	&header();
	#print qq|<p id=sub><span class=omits1>COMMUNITY　＞＞</span>\n|;
	#print qq|<a href="../index.html">BBS</a>\n|;
	#print qq|<a href="./diary.cgi">STAFFS'VOICE</a><span class=omits1>-</span>\n|;
	#print qq|<span class=omits1>Back number</span></p>\n|;
	print qq|　<br><div class="oneday"><div class="headtitle"><span class="headtext">BACK NUMBER</span></div><br><div id="log">\n|;

	#print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/タイトル一覧<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});
	print "<ul>\n";

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

	local $new = '';
	local $i = 0;
	foreach (@logs) {
		local($no, $reno, $display, $date, $field, $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 ($display == 0) {
			next;
		}

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

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

			local $year  = sprintf("%04d", $1);
			local $month = sprintf("%02d", $2);
			local $day   = sprintf("%02d", $3);
			local $week  = $week[&getweek($day, $year, $month)];

			$date = "$year/$month/$day($week)";
		}

		if ($icon_mode == 1 && $icon_display != 0 && $icon ne '') {
			$icon = qq|<img src="$icon_dir$icon" alt="$icon">|;
		} else {
			$icon = '';
		}
		print qq|<div class="oneday2">\n|;
		print qq|\t\t&nbsp;&nbsp;<font color="#9D8937">No.$no&nbsp;&nbsp;$date</font>&nbsp;&nbsp;<a href="$script?mode=view&amp;no=$i"><font color="#555555">$icon$subj$new</font></a>\n|;
		print qq|</div>\n|;
	}

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

	foreach (0 .. int(($parent - 1) / $list_size)) {
		local $page_info = $_ + 1;
		if ($_ == 0) {
			$page_info = "PAGE $page_info";
		}
		if ($in{'page'} == $_) {
		#	print qq|\t\t&nbsp;$page_info\n|;
		} else {
		#	print qq|\t\t&nbsp;<a href="$script?mode=loglist&amp;page=$_">$page_info</a>\n|;
		}
	}

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

	print &skin_basic($skin{'link'});

	&footer();
}

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

	&header();

	local %comment = ();
	local @show    = ();
	local $i   = 1;
	local $pno = 0;
	foreach (@logs) {
		local($no, $reno, $display, $date, $field, $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;
			}
			$comment{$reno}++;
		}
		if ($reno == 0) {
			$i++;
		}
	}

	local $prev_view = '前の日記≫';
	local $next_view = '≪次の日記';

	if ($in{'no'} != $i - 1) {
		local $prev_no = $in{'no'} + 1;
		$prev_view = qq|<a href="$script?mode=view&amp;no=$prev_no"><font color="#9D8937">$prev_view</font></a>|;
	} else {
		$prev_view = qq|$prev_view|;
	}
	if ($in{'no'} != 1) {
		local $next_no = $in{'no'} - 1;
		$next_view = qq|<a href="$script?mode=view&amp;no=$next_no"><font color="#9D8937">$next_view</font></a>|;
	} else {
		$next_view = qq|$next_view|;
	}

	#print qq|<p id=sub><span class=omits1>COMMUNITY　＞＞</span>\n|;
	#print qq|<a href="../index.html">BBS</a>\n|;
	#print qq|<a href="diary.cgi"><span class=omits1>STAFFS'VOICE</span></a><span class=omits1>-</span>\n|;
	#print qq|<a href="diary.cgi?mode=loglist">Back number</a>\n|;
	
	#print qq|<table class="diary"><tr>\n|;
	#print qq|<td class="lefttd">\n|;
	#print qq|<div id="left">\n|;
	print qq|<div class="backnumber">$next_view&nbsp;&nbsp;&nbsp;$prev_view&nbsp;&nbsp;&nbsp;\|&nbsp;&nbsp;&nbsp;<a href="diary.cgi?mode=loglist"><b>BACK NUMBER→</b></a></div>\n|;
	
	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/個別表\示<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});	

	&showlog(@show);
	print $html{'logs'};
	#print qq|</div>\n|;
	#print qq|</div></td><!--id=leftblock-->\n|;
	
	#print qq|<td class="righttd"><div id="right">\n|;
	#print qq|<div id='right2'>\n|;
	#	print qq|<ul>\n|;
	#	print qq|$next_view&nbsp;&nbsp;$prev_view\n|;
	#	print qq|</ul>\n|;
	#print qq|</div>\n|;
	#print qq|</div><!--id=right-->\n|;
	print <<"_HTML_";

_HTML_

	print &skin_basic($skin{'link'});

	&footer();
}

### ログ検索
sub search {
	local @search = ();
	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("過去ログディレクトリを読み出せません。");
			local @dir = readdir(DIR);
			closedir(DIR);

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

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

					push(@past, @file);
				}
			}

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

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

		@logs = ();
		foreach (@search) {
			local $string = $_;
			local $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, $_); }
		}
	}

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/ログ検索<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print <<"_HTML_";
	<ul>
		<li>検索したい<em>キーワード</em>を入力し、<em>検索</em>ボタンを押してください。</li>
		<li>キーワードは<em>半角スペース</em>で区切って複数指定することができます。</li>
	</ul>
	<form action="$script" method="get" enctype="multipart/form-data">
		<fieldset>
			<legend>検索フォーム</legend>
			<dl>
				<dt>キーワード</dt>
					<dd><input type="text" name="word" size="30" value="$in{'word'}"></dd>
				<dt>検索条件</dt>
					<dd>
						<select name="cond">
_HTML_

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

	print <<"_HTML_";
						</select>
					</dd>
_HTML_

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

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

		print <<"_HTML_";
						</select>
					</dd>
_HTML_
	}

	print <<"_HTML_";
			</dl>
			<ul>
				<li>
					<label for="checkbox_dis">大文字、小文字を区別しない</label>
_HTML_

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

	print <<"_HTML_";
				</li>
			</ul>
			<p>
				<input type="submit" value="検索する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="mode" value="search">
			</p>
		</fieldset>
	</form>
_HTML_

	if ($in{'word'} ne '' && $logs[0] eq '') {
		print <<"_HTML_";
	<ul>
		<li>該当する記事はありません。</li>
	</ul>
_HTML_
	}

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

	if ($in{'word'} ne '') {
		print <<"_HTML_";
<div id="link">
	<ul>
_HTML_

		foreach (0 .. int($#logs / $page_size)) {
			local $page_info = $_ + 1;
			if ($_ == 0) {
				$page_info = "ページ $page_info";
			}
			if ($in{'page'} == $_) {
				print qq|\t\t<li>$page_info</li>\n|;
			} else {
				local $inword = $in{'word'};
				$inword =~ s/(\W)/'%' . unpack('H2',$1)/ge;
				print qq|\t\t<li><a href="$script?mode=search&amp;word=$inword&amp;obje=$in{'obje'}&amp;page=$_">$page_info</a></li>\n|;
			}
		}

		print <<"_HTML_";
	</ul>
</div>
_HTML_
	} elsif ($in{'no'} ne '') {
		@logs = ();
		foreach (@search) {
			($no, $reno) = split(/\t/);
			if (($reno == 0 && $in{'no'} == $no) || ($reno != 0 && $in{'no'} == $reno)) {
				push(@logs, $_);
			}
		}
	}

	if ($in{'word'} ne '' || $in{'no'} ne '') {
		$default_show = $page_size;
		&showlog(@logs);
		print $html{'logs'};
	}

	print &skin_basic($skin{'link'});

	&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("ログファイルを読み出せません。");
		local @logs = <FILE>;
		close(FILE);

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

			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 ($html_mode == 1) {
			&output_htmlfile();
		}
		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("ログファイルを読み出せません。");
		local @logs = <FILE>;
		close(FILE);

		local $flag = 0;
		foreach (@logs) {
			chop($_);
			($no, $reno, $display, $date, $field, $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 qq|<div id="contents2">\n|;

		$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/記事修正<!--/g;
		$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

		print &skin_basic($skin{'subtitle'});

		print <<"_HTML_";
	<ul>
		<li>記事を修正して、投稿ボタンを押してください。</li>
	</ul>
</div>
_HTML_

		&form();

		print &skin_basic($skin{'link'});

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

		&lock($lock_file);

		open(FILE, "$log_file") || &error("ログファイルを読み出せません。");
		local @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'});
		}

		local $line = "$in{'no'}\t$in{'reno'}\t1\t$in{'date'}\t\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";

		local @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 ($html_mode == 1) {
			&output_htmlfile();
		}
		if ($js_mode != 0) {
			&output_jsfile(@new);
		}

		&unlock($lock_file);
	}

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

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

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/記事編集<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print <<"_HTML_";
	<ul>
		<li><em>削除キー</em>を入力して削除したい記事にチェックを入れ、<em>削除</em>ボタンを押してください。</li>
_HTML_

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

	print <<"_HTML_";
		<li>投稿時に削除キーを設定していなければ記事を削除することはできません。</li>
	</ul>
	
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>記事編集フォーム</legend>
			<dl>
				<dt>削除キー</dt>
					<dd><input type="password" name="user_pwd" size="15"></dd>
			</dl>
			<p>
				<input type="submit" name="delete" value="削除する" class="button">
_HTML_

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

	print <<"_HTML_";
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="mode" value="userdel">
				<input type="hidden" name="page" value="$in{'page'}">
			</p>
			<dl>
				<dt>スレッド数</dt>
					<dd><em>$parent</em>件</dd>
			</dl>
			<p>$work_mesg</p>
			<table summary="投稿記事一覧">
				<tr>
					<th>削除</th>
_HTML_

	if ($edit_mode == 1) {
		print qq|\t\t\t\t\t<th>修正</th>\n|;
	}

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

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

	local $i = 0;
	foreach (@logs) {
		local($no, $reno, $display, $date, $field, $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 ($display == 0) {
			next;
		}

		if ($reno == 0) {
			if ($name eq '') {
				$name = '管理者';
			}
			$subj = "<em>$subj</em>";
		}
		$text =~ s/<[^>]*>//g;
		if (length($text) > 60) {
			$text = substr($text, 0, 60) . ' …';
		}

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

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

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

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

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

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

	print &skin_basic($skin{'link'});

	&footer();
}

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

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

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

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/過去ログ<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print <<"_HTML_";
	<ul>
		<li>表\示したい過去ログを選択し、<em>表\示</em>ボタンを押してください。</li>
		<li><a href="$script?mode=search">ログ検索</a>によって過去ログを検索することができます。</li>
	</ul>
	<form action="$script" method="get">
		<fieldset>
			<legend>過去ログ選択フォーム</legend>
			<dl>
				<dt>表\示ログ</dt>
					<dd>
						<select name="view">
							<option value="">過去ログを選択してください</option>
_HTML_

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

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

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

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

					if ($display == 0) {
						next;
					}
					if ($in{'no'} == $no || $in{'no'} == $reno) {
						push(@show, $_);
					}
				}

				&showlog(@show);
				print $html{'logs'};
			} else {
				print <<"_HTML_";
<div id="contents2">
	<ul>
_HTML_

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

					if ($reno != 0) {
						next;
					}
					if ($display == 0) {
						next;
					}

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

						$date = "$year年$month月$day日（$week）";
					}

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

				print <<"_HTML_";
	</ul>
</div>
_HTML_
			}
		} else {
			&showlog(@logs);
			print $html{'logs'};
		}
	}

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理者用
sub admin {
	if ($in{'admin_pwd'} eq '') {
		&admin_login();
	} 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("過去ログディレクトリを読み出せません。");
			local @dir = readdir(DIR);
			closedir(DIR);

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

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

 			if ($in{'create_log'} ne '') {
 				&admin_createlog();
			}
			if ($in{'read_log'} ne '') {
 				&admin_readlog();
			}

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

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

			&unlock($lock_file);
		}

		#ログ削除
		if ($in{'delete'} ne '') {
			if ($in{'del'} eq '') {
				&error("削除したい記事を選択してください。");
			}

			&lock($lock_file);

			local @new = &admin_delete();

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

			&unlock($lock_file);
		}

		#ログ登録・修正フォーム
		if ($in{'exec'} eq 'new' || $in{'back'} ne '' || $in{'modify'} ne '') {
			&admin_form();
		}

		#ログ登録・修正
		if ($in{'regist'} ne '' || $in{'modi'} ne '') {
			&admin_inputcheck();

			&lock($lock_file);

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

			if ($in{'regist'} ne '') {
				$no = &admin_regist();
			} else {
				$no = &admin_modi();
			}

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

			if ($thumbnail_mode == 1) {
				local $make_flag = &make_thumbnail($upfile_dir, $thumbnail_dir, $img_max_width);
				if ($make_flag == 0) {
					&error("記事を新規に書き込みましたが、サムネイル画像の作成に失敗しました。");
				}
			}

			if ($trackback_mode == 1 && $in{'trackback_url'} ne '') {
				if ($in{'display'} == 1) {
					$in{'text'} =~ s/\$FILE\d_*[c|l|r]*//g;

					local($send_flag, $error_message) = &send_trackback($in{'trackback_url'}, $in{'subj'}, "$trackback_self_url?no=$no", $in{'text'}, $trackback_title);
					if ($send_flag == 0) {
						&error("記事を新規に書き込みましたが、トラックバックの送信に失敗しました。<br>Error message : $error_message");
					}

					open(TB, "$sended_trackback_file") || &error("送信トラックバック一覧ファイルを読み出せません。");
					local @trackback = <TB>;
					close(TB);

					local $date = time();

					local $trackback_no = $trackback[0] + 1;
					unshift(@trackback, "$trackback_no\t$date\t$in{'trackback_url'}\t$in{'subj'}\t$trackback_self_url?date=$in{'date'}\t$in{'text'}\t$trackback_title\n");
					splice(@trackback, $max_trackback);

					open(TB, ">$sended_trackback_file") || &error("送信トラックバック一覧ファイルに書き込めません。");
					print TB @trackback;
					close(TB);
				} else {
					$work_mesg .= "<strong>記事が下書きに設定されているので、トラックバックは送信されませんでした。</strong>";
				}
			}

			if ($ping_mode == 1) {
				@send_server = split(/\n/, $in{'ping_server'});
				local($send_flag, @error_message) = &send_ping($ping_title, $ping_self_url, @send_server);
				if ($send_flag == 0) {
					local $error_message = '';
					foreach (@error_message) {
						$error_message .= "$_<br>";
					}
					&error("記事を新規に書き込みましたが、更新PINGの送信に失敗しました。<br>Error message : $error_message");
				}
			}

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

			&unlock($lock_file);
		}

		#プロフィール設定
		if ($profile_mode == 1 && $in{'exec'} eq 'profile') {
			if ($in{'profile_regist'} ne '') {
				&admin_profile_regist();

				if ($html_mode == 1) {
					&output_htmlfile();
				}
			}
			if ($in{'profile_form'} ne '' || $author[1] eq '') {
				if ($author[1] eq '') {
					$in{'author'} = $author[0];
				}
				&admin_profile_form();
			}
			&admin_profile_list();
		}

		#受信トラックバック削除
		if ($trackback_mode == 1 && $in{'exec'} eq 'trackback') {
			if ($in{'tb_list'} ne '') {
				&admin_tb_list();
			} elsif ($in{'tb_delete'} ne '') {
				&admin_tb_delete();
			} elsif ($in{'tb_clear'} ne '') {
				&admin_tb_clear();
			}
			&admin_tb_all();
		}

		#送信トラックバック
		if ($trackback_mode == 1 && $in{'exec'} eq 'tbsended') {
			&admin_tb_sended();
		}

		#コンテンツリスト設定
		if ($contents_mode == 1 && $in{'exec'} eq 'contents') {
			if ($in{'contents_update'} ne '') {
				&admin_contents_update();

				if ($html_mode == 1) {
					&output_htmlfile();
				}
			}
			&admin_contents_list();
		}

		#ミニリンク集設定
		if ($link_mode == 1 && $in{'exec'} eq 'link') {
			if ($in{'link_update'} ne '') {
				&admin_link_update();

				if ($html_mode == 1) {
					&output_htmlfile();
				}
			}
			&admin_link_list();
		}

		#ログダウンロード
		if ($in{'download'} ne '') {
			&admin_download();
		}

		#削除修正用ログ一覧
		&admin_list();
	}
}

### 管理モード - ログイン
sub admin_login {
	&header();


	$skin_subtitle = $skin{'subtitle'};

	$skin_subtitle =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin_subtitle =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin_subtitle);

	print <<"_HTML_";

	<form action="$script" method="post" enctype="multipart/form-data">
	<table border="0" width="540">

	<tr height="30">
		<td colspan="2" align="left"><font color="#E10060">●今までの日記を<a href="./diary.cgi" target="_blank"><font color="#E10060">読む≫</font></a></font><br>
		<font color="#E10060">●新規投稿、編集をするには管理者パスワードを入力してください</font>
		
		</td>

	<tr height="30">
		<td align="left" width="100">パスワード</td>
		<td align="left"><input type="password" name="admin_pwd" size="15"></td>
	</tr>
	<tr height="30">
		<td align="left">作業内容</td>
		<td align="left"><select width="100" name="exec">
			<option value="new" selected>新規投稿</option>
			<option value="">記事編集</option>
_HTML_

	if ($profile_mode == 1) {
		print <<"_HTML_";
			<option value="profile">プロフィール設定</option>
_HTML_
	}
	if ($trackback_mode == 1) {
		print <<"_HTML_";
			<option value="trackback">受信トラックバック削除</option>
			<option value="tbsended">送信トラックバック表\示</option>
_HTML_
	}
	if ($contents_mode == 1) {
		print <<"_HTML_";
			<option value="contents">コンテンツ設定</option>
_HTML_
	}
	if ($link_mode == 1) {
		print <<"_HTML_";
			<option value="link">リンク集設定</option>
_HTML_
	}

	print <<"_HTML_";
			</select>

			<input type="submit" value="認証する" class="button">
			<input type="hidden" name="mode" value="admin">
		</td>
	</tr>

	</table>
	</form>
	<div style="height:300px;"> </div>
_HTML_

	if ($tool_mode == 1) {
		$skin_subtitle = $skin{'subtitle'};

		$skin_subtitle =~ s/<!--SUBTITLE_START-->/管理ツール<!--/g;
		$skin_subtitle =~ s/<!--SUBTITLE_END-->/-->/g;

		print &skin_basic($skin_subtitle);

		print <<"_HTML_";
	<ul>
_HTML_

		while (scalar(@tools) != 0) {
			local $tool_file = shift(@tools);
			local $tool_name = shift(@tools);

			if ($tool_file ne '' && $tool_name ne '') {
				print <<"_HTML_";
		<li><a href="$tool_file">$tool_name</a></li>
_HTML_
			}
		}

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

	print <<"_HTML_";
_HTML_

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理モード - 過去ログ作成
sub admin_createlog {
	$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(0606, "$past_dir$past_file");

	$work_mesg = "スレッド$past_size件を過去ログに移動しました。";

	return;
}

### 管理モード - 過去ログ読み戻し
sub admin_readlog {
	$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件を過去ログから読み戻しました。";

	return;
}

### 管理モード - ログ削除
sub admin_delete {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません。");
	local @logs = <FILE>;
	close(FILE);

	local @new = ();
	local $del_list = '';
	foreach (@logs) {
		local($no, $reno, $file) = (split(/\t/))[0, 1, 12];
		local $flag = 0;
		foreach $del (split(/\n/, $in{'del'})) {
			if ($no == $del || $reno == $del) { $flag = 1; last; }
		}
		if ($flag == 0) {
			push(@new, $_);
		} else {
			$del_list .= "$no ";
			foreach $file_name (split(/<>/, $file)) {
				if (-e "$upfile_dir$file_name") {
					unlink("$upfile_dir$file_name");
				}
				if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file_name") {
					unlink("$thumbnail_dir$file_name");
				}
			}
			if (-e "$reserved_trackback_dir$no$reserved_trackback_ext") {
				unlink("$reserved_trackback_dir$no$reserved_trackback_ext");
			}
		}
	}
	$work_mesg = "$del_list番の記事を削除しました。";

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

	return @new;
}

### 管理モード - ログ登録・修正フォーム
sub admin_form {
	if ($in{'exec'} eq 'new') {
		%cookie = &get_cookie($admin_cookie_id);
		if ($cookie{'color'} eq '') {
			$cookie{'color'} = $color[0];
		}

		$form_subj = '';
		($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
		$form_year  = sprintf("%04d", $year + 1900);
		$form_month = sprintf("%02d", $mon + 1);
		$form_day   = sprintf("%02d", $day);
		if ($time_mode == 1) {
			$form_hour   = sprintf("%02d", $hour);
			$form_minute = sprintf("%02d", $min);
		}
		$form_field         = '';
		$form_name          = '';
		$form_icon          = $cookie{'icon'};
		$form_color         = $cookie{'color'};
		$form_text          = '';
		$form_display       = 1;
		$form_trackback_url = '';
		$form_preview       = $cookie{'preview'};

		$form_mode  = 'regist';
		$form_info  = '書き込む';
		$form_no    = '';
		$form_file1 = '';
		$form_file2 = '';
		$form_file3 = '';
		$form_ext1  = '';
		$form_ext2  = '';
		$form_ext3  = '';
	} elsif ($in{'back'} ne '') {
		$in{'text'}          = &create_plain($in{'text'});
		$in{'trackback_url'} = &create_plain($in{'trackback_url'});

		$form_subj = $in{'subj'};
		if ($in{'date'} =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
			$form_year  = $1;
			$form_month = $2;
			$form_day   = $3;
		}
		if ($time_mode == 1) {
			$form_hour   = $in{'hour'};
			$form_minute = $in{'minute'};
		}
		$form_field         = $in{'field'};
		$form_name          = $in{'name'};
		$form_icon          = $in{'icon'};
		$form_color         = $in{'color'};
		$form_text          = $in{'text'};
		$form_display       = $in{'display'};
		$form_trackback_url = $in{'trackback_url'};
		$form_preview       = $in{'c_preview'};

		if ($in{'no'} ne '' && $in{'no'} != 0) {
			$form_mode  = 'modi';
			$form_info  = '修正する';
			$form_no    = $in{'no'};
			$form_file1 = $in{'orgfile1'};
			$form_file2 = $in{'orgfile2'};
			$form_file3 = $in{'orgfile3'};
			$form_ext1  = $in{'ext1'};
			$form_ext2  = $in{'ext2'};
			$form_ext3  = $in{'ext3'};
		} else {
			$form_mode  = 'regist';
			$form_info  = '書き込む';
			$form_no    = '';
			$form_file1 = '';
			$form_file2 = '';
			$form_file3 = '';
			$form_ext1  = '';
			$form_ext2  = '';
			$form_ext3  = '';
		}
	} else {
		if ($in{'modi'} eq '') {
			&error("修正したい記事を選択してください。");
		}

		%cookie = &get_cookie($admin_cookie_id);

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

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

		$text = &create_plain($text);

		($file1, $file2, $file3) = split(/<>/, $file);

		if ($file1 ne '') {
			$file1     =~ /([^\/\\]*)\.([^.\/\\]*)$/;
			$file1_ext = $2;
			$file1_ext =~ tr/A-Z/a-z/;
		}
		if ($file2 ne '') {
			$file2     =~ /([^\/\\]*)\.([^.\/\\]*)$/;
			$file2_ext = $2;
			$file2_ext =~ tr/A-Z/a-z/;
		}
		if ($file3 ne '') {
			$file3     =~ /([^\/\\]*)\.([^.\/\\]*)$/;
			$file3_ext = $2;
			$file3_ext =~ tr/A-Z/a-z/;
		}

		$form_subj = $subj;
		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$form_year  = sprintf("%04d", $1);
			$form_month = sprintf("%02d", $2);
			$form_day   = sprintf("%02d", $3);

			if ($time_mode == 1) {
				$form_hour   = sprintf("%02d", $4);
				$form_minute = sprintf("%02d", $5);
			}
		} else {
			($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
			$form_year  = sprintf("%04d", $year + 1900);
			$form_month = sprintf("%02d", $mon + 1);
			$form_day   = sprintf("%02d", $day);

			if ($time_mode == 1) {
				$form_hour   = sprintf("%02d", $hour);
				$form_minute = sprintf("%02d", $min);
			}
		}
		$form_field         = $field;
		$form_name          = $name;
		$form_icon          = $icon;
		$form_color         = $color;
		$form_text          = $text;
		$form_display       = $display;
		$form_trackback_url = '';
		$form_preview       = $cookie{'preview'};

		$form_mode  = 'modi';
		$form_info  = '修正する';
		$form_no    = $no;
		$form_file1 = $file1;
		$form_file2 = $file2;
		$form_file3 = $file3;
		$form_ext1  = $file1_ext;
		$form_ext2  = $file2_ext;
		$form_ext3  = $file3_ext;
	}

	&header();

	if ($insert_mode == 1) {
		print <<"_HTML_";
<script type="text/javascript">
<!--

//HTMLの挿入１
function insertHtmlA(html) {
	var ds = document.selection;

	if (ds) {
		var selected = ds.createRange();

		if (selected.text) {
			selected.text = '<' + html +  '>' + selected.text + '</' + html +  '>';
		}
	}

	return;
}

//HTMLの挿入２
function insertHtmlB(html) {
	var ds = document.selection;

	if (ds) {
		document.form.text.focus();
		ds.createRange().text = html;
	}

	return;
}

//リンクの挿入
function insertLink() {
	var ds = document.selection;

	if (ds) {
		var selected = ds.createRange();

		if (selected.text) {
			var url = window.prompt('リンク先URL', 'http://');
			if (url && url != 'http://') {
				selected.text = '<a href="' + url +  '">' + selected.text + '</a>';
			}
		}
	}

	return;
}

//メールアドレスの挿入
function insertMail() {
	var ds = document.selection;

	if (ds) {
		var selected = ds.createRange();

		if (selected.text) {
			var mail = window.prompt('メールアドレス', '');
			if (mail) {
				selected.text = '<a href="mailto:' + mail +  '">' + selected.text + '</a>';
			}
		}
	}

	return;
}

//-->
</script>
_HTML_
	}

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>各項目を入力して<em>$form_info</em>ボタンを押してください。</li>
_HTML_

	local $date_label = '';
	if ($time_mode == 1) {
		$date_label = '日時';
	} else {
		$date_label = '日付';
	}
	if ($form_file1 ne '' || $form_file2 ne '' || $form_file3 ne '') {
		print <<"_HTML_";
		<li><em>ファイル</em>の項目を入力しなければ、以前の内容が引き続いて使用されます。</li>
_HTML_
	}

	print <<"_HTML_";
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data" name="form">
		<fieldset>
			<legend>投稿フォーム</legend>
			<dl>
				<dt>題名</dt>
					<dd><input type="text" name="subj" size="40" value="$form_subj"></dd>
				<dt>$date_label</dt>
					<dd>
_HTML_

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

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

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

	if ($time_mode == 1) {
		print qq|\t\t\t\t\t\t<select name="hour">\n|;
		foreach (0 .. 23) {
			local $hour = sprintf("%02d", $_);
			if ($hour == $form_hour) {
				print qq|\t\t\t\t\t\t\t<option value="$hour" selected>$_時</option>\n|;
			} else {
				print qq|\t\t\t\t\t\t\t<option value="$hour">$_時</option>\n|;
			}
		}
		print qq|\t\t\t\t\t\t</select>\n|;

		print qq|\t\t\t\t\t\t<select name="minute">\n|;
		foreach (0 .. 59) {
			local $minute = sprintf("%02d", $_);
			if ($minute == $form_minute) {
				print qq|\t\t\t\t\t\t\t<option value="$minute" selected>$_分</option>\n|;
			} else {
				print qq|\t\t\t\t\t\t\t<option value="$minute">$_分</option>\n|;
			}
		}
		print qq|\t\t\t\t\t\t</select>\n|;
	}

	print <<"_HTML_";
					</dd>
_HTML_

	if ($fields[1] ne '') {
		print <<"_HTML_";
				<dt>分類</dt>
					<dd>
						<select name="field">
							<option value="">選択してください</option>
_HTML_

		foreach (@fields) {
			if ($_ eq '') {
				next;
			}
			if ($_ eq $form_field) {
				print qq|\t\t\t\t\t\t\t<option value="$_" selected>$_</option>\n|;
			} else {
				print qq|\t\t\t\t\t\t\t<option value="$_">$_</option>\n|;
			}
		}

		print <<"_HTML_";
						</select>
					</dd>
_HTML_
	}
	if ($author_mode == 1 && $author[1] ne '') {
		print <<"_HTML_";
				<dt>投稿者</dt>
					<dd>
						<select name="name">
							<option value="">選択してください</option>
_HTML_

		foreach (@author) {
			if ($_ eq '') {
				next;
			}
			if ($_ eq $form_name) {
				print qq|\t\t\t\t\t\t\t<option value="$_" selected>$_</option>\n|;
			} else {
				print qq|\t\t\t\t\t\t\t<option value="$_">$_</option>\n|;
			}
		}

		print <<"_HTML_";
						</select>
					</dd>
_HTML_
	}

	if ($icon_mode == 1) {
		print <<"_HTML_";
				<dt>アイコン</dt>
					<dd>
						<select name="icon">
							<option value="">なし</option>
_HTML_

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

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

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

		print <<"_HTML_";
						</select>
						<a href="$script?mode=icon" onClick="window.open('$script?mode=icon', 'Icon');return false;" onKeypress="this.onclick();">アイコン一覧</a>
					</dd>
_HTML_
	}

	if ($color_mode == 1) {
		print <<"_HTML_";
				<dt>本文の色</dt>
					<dd>
_HTML_

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

			local $color_label = $_;
			$color_label =~ s/#//g;

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

		print <<"_HTML_";
					</dd>
_HTML_
	}
	if ($insert_mode == 1) {
		print <<"_HTML_";
				<dt>装飾機能\</dt>
					<dd>
						<input type="button" value="リンク" onmousedown="insertLink();" onkeydown="this.onmousedown();" class="button">
						<input type="button" value="メール" onmousedown="insertMail();" onkeydown="this.onmousedown();" class="button">
_HTML_

		while (scalar(@insert_a) != 0) {
			local $insert_html = shift(@insert_a);
			local $insert_name = shift(@insert_a);

			if ($insert_html eq '' || $insert_name eq '') {
				next;
			}

			print qq|\t\t\t\t\t\t<input type="button" value="$insert_name" onmousedown="insertHtmlA('$insert_html');" onkeydown="this.onmousedown();" class="button">\n|;
		}

		while (scalar(@insert_b) != 0) {
			local $insert_html = shift(@insert_b);
			local $insert_name = shift(@insert_b);

			if ($insert_html eq '' || $insert_name eq '') {
				next;
			}

			print qq|\t\t\t\t\t\t<input type="button" value="$insert_name" onmousedown="insertHtmlB('$insert_html');" onkeydown="this.onmousedown();" class="button">\n|;
		}

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

	print <<"_HTML_";
				<dt>本文</dt>
					<dd><textarea name="text" cols="50" rows="20">$form_text</textarea></dd>
_HTML_

	if ($upload_mode == 1) {
		if ($form_file1 ne '') {
			$file_link1 = qq| <a href="$upfile_url$form_file1" onClick="window.open('$upfile_url$form_file1', 'File');return false;" onKeypress="this.onclick();">このファイルを表\示 （$form_file1）</a>|;
		}
		if ($form_file2 ne '') {
			$file_link2 = qq| <a href="$upfile_url$form_file2" onClick="window.open('$upfile_url$form_file2', 'File');return false;" onKeypress="this.onclick();">このファイルを表\示 （$form_file2）</a>|;
		}
		if ($form_file3 ne '') {
			$file_link3 = qq| <a href="$upfile_url$form_file3" onClick="window.open('$upfile_url$form_file3', 'File');return false;" onKeypress="this.onclick();">このファイルを表\示 （$form_file3）</a>|;
		}

		print <<"_HTML_";
				<dt>ファイル１</dt>
					<dd><input type="file" name="file1" size="40">$file_link1</dd>
				<dt>ファイル２</dt>
					<dd><input type="file" name="file2" size="40">$file_link2</dd>

_HTML_
	}

	print <<"_HTML_";
				<dt>記事の状態</dt>
					<dd>
_HTML_

	if ($form_display == 0) {
		print <<"_HTML_";
						<select name="display">
							<option value="1">公開</option>
							<option value="0" selected>下書き</option>
						</select>
_HTML_
	} else {
		print <<"_HTML_";
						<select name="display">
							<option value="1" selected>公開</option>
							<option value="0">下書き</option>
						</select>
_HTML_
	}

	print <<"_HTML_";
					</dd>
_HTML_

	if ($trackback_mode == 1) {
		print <<"_HTML_";
				<dt>Trackback URL</dt>
					<dd><textarea name="trackback_url" cols="30" rows="3">$form_trackback_url</textarea></dd>
_HTML_
	}

	print <<"_HTML_";
			</dl>
_HTML_

	if ($ping_mode == 1) {
		print qq|\t\t\t<ul>\n|;
		foreach (@ping_server) {
			if ($_ eq '') {
				next;
			}
			print qq|\t\t\t\t<li><input type="checkbox" name="ping_server" id="checkbox_$_" value="$_" class="checkbox" checked> <label for="checkbox_$_">$_ に更新PINGを送信</label></li>\n|;
		}
		print qq|\t\t\t</ul>\n|;
	}

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

		$flag = 1;
	} else {
		print qq|\t\t\t<input type="hidden" name="preview" value="on">\n|;
	}

	if ($form_file1 ne '') {
		if ($flag == 0) {
			print "\t\t\t<ul>\n";
		}

		print qq|\t\t\t\t<li><input type="checkbox" name="delfile1" id="checkbox_delfile1" value="on" class="checkbox"> <label for="checkbox_delfile1">ファイル１ <em>$form_file1</em> を削除</label></li>\n|;

		$flag = 1;
	}
	if ($form_file2 ne '') {
		if ($flag == 0) {
			print "\t\t\t<ul>\n";
		}

		print qq|\t\t\t\t<li><input type="checkbox" name="delfile2" id="checkbox_delfile2" value="on" class="checkbox"> <label for="checkbox_delfile2">ファイル２ <em>$form_file2</em> を削除</label></li>\n|;

		$flag = 1;
	}
	if ($form_file3 ne '') {
		if ($flag == 0) {
			print "\t\t\t<ul>\n";
		}

		print qq|\t\t\t\t<li><input type="checkbox" name="delfile3" id="checkbox_delfile3" value="on" class="checkbox"> <label for="checkbox_delfile3">ファイル３ <em>$form_file3</em> を削除</label></li>\n|;

		$flag = 1;
	}

	if ($form_mode eq 'modi') {
		if ($flag == 0) {
			print "\t\t\t<ul>\n";
		}

		print qq|\t\t\t\t<li><input type="checkbox" name="sort" id="checkbox_sort" value="on" class="checkbox"> <label for="checkbox_sort">日付順に再度ソ\ート</label></li>\n|;

		$flag = 1;
	}

	if ($flag == 1) {
		print "\t\t\t</ul>\n";
	}

	print <<"_HTML_";
			<p>
				<input type="submit" name="$form_mode" value="$form_info" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="mode" value="admin">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
				<input type="hidden" name="no" value="$form_no">
_HTML_

	if ($fields[1] eq '') {
		print <<"_HTML_";
				<input type="hidden" name="field" value="$fields[0]">
_HTML_
	}

	print <<"_HTML_";
				<input type="hidden" name="orgfile1" value="$form_file1">
				<input type="hidden" name="ext1" value="$form_ext1">
				<input type="hidden" name="orgfile2" value="$form_file2">
				<input type="hidden" name="ext2" value="$form_ext2">
				<input type="hidden" name="orgfile3" value="$form_file3">
				<input type="hidden" name="ext3" value="$form_ext3">
			</p>
		</fieldset>
	</form>
</div>
_HTML_

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理モード - 入力内容チェック
sub admin_inputcheck {
	$in{'subj'} = &create_line($in{'subj'});
	$in{'text'} = &create_text($in{'text'});

	if ($in{'trackback_url'} ne '') {
		$in{'trackback_url'} = &create_text($in{'trackback_url'});
	}

	$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{'subj'} eq '') {
		&error("題名が入力されていません。");
	}
	if ($in{'date'} !~ /^\d\d\d\d\d\d\d\d$/) {
		&error("日付が不正です。");
	}
	if ($time_mode == 1) {
		if ($in{'hour'} !~ /^\d\d$/) {
			&error("時間が不正です。");
		}
		if ($in{'minute'} !~ /^\d\d$/) {
			&error("分が不正です。");
		}
	} else {
		$in{'hour'}   = '00';
		$in{'minute'} = '00';
	}
	if ($fields[1] ne '' && $in{'field'} eq '') {
		&error("分類が選択されていません。");
	}
	if ($author_mode == 1 && $in{'name'} eq '') {
		if ($author[1] eq '') {
			$in{'name'} = $author[0];
		} else {
			&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);

	return;
}

### 管理モード - ログ登録
sub admin_regist {
	local $host = $ENV{'REMOTE_HOST'};
	local $addr = $ENV{'REMOTE_ADDR'};
	if ($host eq "" || $host eq $addr) {
		if ($gethostbyaddr == 1) {
			$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2) || $addr;
		}
	}

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

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

	if ($upload_mode == 1) {
		local @file_name = ();
		foreach (1 .. 3) {
			if ($file_name{'file' . $_} eq '' && ($in{'ext' . $_} eq '' || ! -e "$upfile_dir$temporary_file$_")) {
				next;
			}

			if ($file_name{'file' . $_} ne '') {
				$file_name{'file' . $_} = &upfile($file{'file' . $_}, $file_name{'file' . $_}, "$no-$_");
			} elsif ($in{'ext' . $_} ne '' && -e "$upfile_dir$temporary_file$_") {
				$file_name{'file' . $_} = "$no-$_.$in{'ext' . $_}";
				rename("$upfile_dir$temporary_file$_", "$upfile_dir$file_name{'file' . $_}");
			}
			push(@file_name, $file_name{'file' . $_});
		}
		$file_name = join('<>', @file_name);
	}

	local $line   = "$no\t0\t$in{'display'}\t$in{'date'}$in{'hour'}$in{'minute'}\t$in{'field'}\t$in{'name'}\t\t\t$in{'subj'}\t$in{'text'}\t$in{'color'}\t$in{'icon'}\t$file_name\t\t$host\n";
	local $log_no = $no;

	if ($make_past == 0) {
		foreach (0 .. ($parent - $max_log)) {
			local($no, $reno) = split(/\t/, $logs[$#logs]);
			if ($reno == 0) {
				local($file) = (split(/\t/, pop(@logs)))[12];
				foreach $file_name (split(/<>/, $file)) {
					if (-e "$upfile_dir$file_name") {
						unlink("$upfile_dir$file_name");
					}
					if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file_name") {
						unlink("$thumbnail_dir$file_name");
					}
				}
				if (-e "$reserved_trackback_dir$no$reserved_trackback_ext") {
					unlink("$reserved_trackback_dir$no$reserved_trackback_ext");
				}
			} else {
				while ((split(/\t/, $logs[$#logs]))[1] != 0) {
					local($file) = (split(/\t/, pop(@logs)))[12];
					foreach $file_name (split(/<>/, $file)) {
						if (-e "$upfile_dir$file_name") {
							unlink("$upfile_dir$file_name");
						}
						if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file_name") {
							unlink("$thumbnail_dir$file_name");
						}
					}
					if (-e "$reserved_trackback_dir$no$reserved_trackback_ext") {
						unlink("$reserved_trackback_dir$no$reserved_trackback_ext");
					}
				}
				local($file) = (split(/\t/, pop(@logs)))[12];
				foreach $file_name (split(/<>/, $file)) {
					if (-e "$upfile_dir$file_name") {
						unlink("$upfile_dir$file_name");
					}
					if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file_name") {
						unlink("$thumbnail_dir$file_name");
					}
				}
				if (-e "$reserved_trackback_dir$no$reserved_trackback_ext") {
					unlink("$reserved_trackback_dir$no$reserved_trackback_ext");
				}
			}
		}
	}

	local @new  = ();
	local $flag = 0;
	foreach (@logs) {
		local($no, $reno, $display, $date) = split(/\t/);
		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$date = "$1$2$3";
		}
		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 = "記事を新規に書き込みました。";

	return $log_no;
}

### 管理モード - ログ修正
sub admin_modi {
	local $host = $ENV{'REMOTE_HOST'};
	local $addr = $ENV{'REMOTE_ADDR'};
	if ($host eq "" || $host eq $addr) {
		if ($gethostbyaddr == 1) {
			$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2) || $addr;
		}
	}

	if ($upload_mode == 1) {
		local @file_name = ();
		foreach (1 .. 3) {
			if ($file_name{'file' . $_} eq '' && ($in{'ext' . $_} eq '' || ! -e "$upfile_dir$temporary_file$_") && $in{'delfile' . $_} eq '' && $in{'orgfile' . $_} eq '') {
				next;
			}

			if ($file_name{'file' . $_} ne '') {
				if (-e "$upfile_dir" . $in{'orgfile' . $_}) {
					unlink("$upfile_dir" . $in{'orgfile' . $_});
				}
				if ($thumbnail_mode == 1 && -e "$thumbnail_dir" . $in{'orgfile' . $_}) {
					unlink("$thumbnail_dir" . $in{'orgfile' . $_});
				}
				$file_name{'file' . $_} = &upfile($file{'file' . $_}, $file_name{'file' . $_}, "$in{'no'}-$_");
			} elsif ($in{'orgfile' . $_} ne '' && $in{'delfile' . $_} eq 'on') {
				if (-e "$upfile_dir" . $in{'orgfile' . $_}) {
					unlink("$upfile_dir" . $in{'orgfile' . $_});
				}
				if ($thumbnail_mode == 1 && -e "$thumbnail_dir" . $in{'orgfile' . $_}) {
					unlink("$thumbnail_dir" . $in{'orgfile' . $_});
				}
				$file_name{'file' . $_} = '';
			} elsif ($in{'ext' . $_} ne '' && -e "$upfile_dir$temporary_file$_") {
				$file_name{'file' . $_} = "$in{'no'}-$_.$in{'ext' . $_}";
				rename("$upfile_dir$temporary_file$_", "$upfile_dir$file_name{'file' . $_}");
				if ($thumbnail_mode == 1 && -e "$thumbnail_dir" . "$in{'no'}-$_." . $in{'ext' . $_}) {
					unlink("$thumbnail_dir" . "$in{'no'}-$_.$in{'ext' . $_}");
				}
			} elsif ($in{'orgfile' . $_} ne '') {
				$file_name{'file' . $_} = $in{'orgfile' . $_};
			}
			push(@file_name, $file_name{'file' . $_});
		}
		$file_name = join('<>', @file_name);
	}

	local $line = "$in{'no'}\t0\t$in{'display'}\t$in{'date'}$in{'hour'}$in{'minute'}\t$in{'field'}\t$in{'name'}\t\t\t$in{'subj'}\t$in{'text'}\t$in{'color'}\t$in{'icon'}\t$file_name\t\t$host\n";

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

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

	return $in{'no'};
}

### プロフィール編集
sub admin_profile_regist {
	if ($in{'author'} eq '') {
		&error("編集したいプロフィールを選択してください。");
	}

	local $flag = 0;
	foreach (@author) {
		if ($_ eq $in{'author'}) {
			$flag = 1;
			last;
		}
	}
	if ($flag == 0) {
		&error("指定された投稿者名は存在しません。");
	}

	$in{'author'} = &create_line($in{'author'});
	$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;

	local $line = "$in{'author'}\t$in{'text'}\n";

	open(PROFILE, "$profile_file") || &error("プロフィール保存ファイルを読み出せません。");
	local @profile = <PROFILE>;
	close(PROFILE);

	local @new = ();
	local $flag = 0;
	foreach (@profile) {
		local($author, $text) = split(/\t/);

		if ($in{'author'} eq $author) {
			push(@new , $line);
			$flag = 1;
		} else {
			push(@new, $_);
		}
	}
	if ($flag == 0) {
		push(@new, $line);
	}

	open(PROFILE, ">$profile_file") || &error("プロフィール保存ファイルに書き込めません。");
	print PROFILE @new;
	close(PROFILE);

	$work_mesg = "$in{'author'}のプロフィールを編集しました。";

	return;
}

### プロフィール編集フォーム
sub admin_profile_form {
	if ($in{'author'} eq '') {
		&error("編集したいプロフィールを選択してください。");
	}

	open(PROFILE, "$profile_file") || &error("プロフィール保存ファイルを読み出せません。");
	local @profile = <PROFILE>;
	close(PROFILE);

	local $flag = 0;
	foreach (@profile) {
		chop($_);
		($author, $text) = split(/\t/);

		if ($in{'author'} eq $author) {
			$flag = 1;
			last;
		}
	}
	if ($flag == 0) {
		$author = $in{'author'};
		$text   = '';
	}

	$text = &create_plain($text);

	&header();

	print qq|<div id="contents ">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>$authorのプロフィール内容です。</li>
		<li>プロフィールを入力して、<em>登録</em>ボタンを押してください。</li>
_HTML_

	if ($work_mesg ne '') {
		print <<"_HTML_";
		<li>$work_mesg</li>
_HTML_
	}

	print <<"_HTML_";
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>プロフィール設定フォーム</legend>
			<dl>
				<dt>プロフィール</dt>
					<dd><textarea name="text" cols="50" rows="20">$text</textarea></dd>
				</dt>
			</dl>
			<p>
				<input type="submit" name="profile_regist" value="登録する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="author" value="$author">
				<input type="hidden" name="exec" value="profile">
				<input type="hidden" name="mode" value="admin">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
			</p>
		</fieldset>
	</form>
</div>
_HTML_

	print &skin_basic($skin{'link'});

	&footer();
}

### プロフィール一覧
sub admin_profile_list {
	open(PROFILE, "$profile_file") || &error("プロフィール保存ファイルを読み出せません。");
	local @profile = <PROFILE>;
	close(PROFILE);

	local %profile = ();
	foreach (@profile) {
		chop($_);
		($author, $text) = split(/\t/);

		$profile{"$author"} = $text;
	}

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>投稿者のプロフィール一覧です。</li>
		<li>設定したいプロフィールにチェックを入れ、<em>編集</em>ボタンを押すと、プロフィールの設定ができます。</li>
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>プロフィール設定フォーム</legend>
			<p>
				<input type="submit" name="profile_form" value="編集する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="exec" value="profile">
				<input type="hidden" name="mode" value="admin">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
			</p>
			<p>$work_mesg</p>
			<table summary="プロフィール一覧">
				<tr>
					<th>編集</th>
					<th>名前</th>
					<th>登録内容</th>
				</tr>
_HTML_

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

		if ($profile{"$_"} ne '') {
			$text = $profile{"$_"};

			if ($text ne '') {
				$text =~ s/<[^>]*>//g;
				if (length($text) > 80) {
					$text = substr($text, 0, 80) . ' …';
				}
			}
		} else {
			$text = '';
		}

		print <<"_HTML_";
				<tr>
					<td><input type="radio" name="author" value="$_" class="radio"></td>
					<td>$_</td>
					<td>$text</td>
				</tr>
_HTML_
	}

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 受信トラックバック削除
sub admin_tb_delete {
	open(TB, "$reserved_trackback_dir$in{'no'}$reserved_trackback_ext") || &error("受信トラックバック保存ファイルを読み出せません。");
	local @trackback = <TB>;
	close(TB);

	local @new_trackback = ();
	local $del_list = '';
	foreach (@trackback) {
		local($tb_no) = (split(/\t/))[0];
		local $flag = 0;
		foreach $del (split(/\n/, $in{'del'})) {
			if ($tb_no == $del) { $flag = 1; last; }
		}
		if ($flag == 0) {
			push(@new_trackback, $_);
		} else {
			$del_list .= "$tb_no ";
		}
	}
	$work_mesg = "$del_list番のトラックバックを削除しました。";

	open(TB, ">$reserved_trackback_dir$in{'no'}$reserved_trackback_ext") || &error("受信トラックバック保存ファイルに書き込めません。");
	print TB @new_trackback;
	close(TB);

	if ($new_trackback[0] eq '') {
		unlink("$reserved_trackback_dir$in{'no'}$reserved_trackback_ext");
		if ($in{'no'} eq 'new') {
			$work_mesg = "新着トラックバックをすべて削除しました。";
		} else {
			$work_mesg = "$in{'no'}番へのトラックバックをすべて削除しました。";
		}
		&admin_tb_all();
	} else {
		$in{'show'} = $in{'no'};
		&admin_tb_list();
	}

	return;
}

### 受信トラックバックすべて削除
sub admin_tb_clear {
	local $del_list = '';
	foreach $del (split(/\n/, $in{'del'})) {
		if (-e "$reserved_trackback_dir$del$reserved_trackback_ext") {
			unlink("$reserved_trackback_dir$del$reserved_trackback_ext");
			$del_list .= "$del ";
		}
	}

	if ($del_list eq 'new ') {
		$work_mesg = "新着トラックバックをすべて削除しました。";
	} else {
		$work_mesg = "$del_list番へのトラックバックをすべて削除しました。";
	}

	return;
}

### 受信トラックバック表示
sub admin_tb_list {
	open(TB, "$reserved_trackback_dir$in{'show'}$reserved_trackback_ext") || &error("受信トラックバック保存ファイルを読み出せません。");
	local @trackback = <TB>;
	close(TB);

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	if ($in{'show'} eq 'new') {
		$trackback_info = '新着';
	} else {
		$trackback_info = "No.<em>$in{'show'}</em>への";
	}

	print <<"_HTML_";
	<ul>
		<li>$trackback_infoトラックバック一覧です。</li>
		<li>削除したいトラックバックにチェックを入れ、<em>削除</em>ボタンを押してください。</li>
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>トラックバック削除フォーム</legend>
			<p>
				<input type="submit" name="tb_delete" value="削除する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="no" value="$in{'show'}">
				<input type="hidden" name="exec" value="trackback">
				<input type="hidden" name="mode" value="admin">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
			</p>
			<p>$work_mesg</p>
			<table summary="トラックバック一覧">
				<tr>
					<th>削除</th>
					<th>No</th>
					<th>受信日時</th>
					<th>ブログ名</th>
					<th>題名</th>
					<th>本文</th>
				</tr>
_HTML_

	foreach (@trackback) {
		chop($_);
		local($tb_no, $tb_time, $tb_blog_name, $tb_title, $tb_url, $tb_excerpt) = split(/\t/);

		($sec, $min, $hour, $day, $mon, $year, $week) = localtime($tb_time);
		$tb_time = sprintf("%04d/%02d/%02d-%02d:%02d", $year + 1900, $mon + 1, $day, $week[$week], $hour, $min);

		print <<"_HTML_";
				<tr>
					<td><input type="checkbox" name="del" value="$tb_no" class="checkbox"></td>
					<td>$tb_no</td>
					<td>$tb_time</td>
					<td><a href="$tb_url">$tb_blog_name</a></td>
					<td>$tb_title</td>
					<td>$tb_excerpt</td>
				</tr>
_HTML_
	}

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 受信トラックバック一覧
sub admin_tb_all {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません。");
	local @logs = <FILE>;
	close(FILE);

	opendir(DIR, "$reserved_trackback_dir") || &error("受信トラックバック保存ディレクトリを読み出せません。");
	local @dir = readdir(DIR);
	closedir(DIR);

	local $parent = 0;
	foreach (@dir) {
		if ($_ =~ /^\d+(\.\w+)$/ && $1 eq $reserved_trackback_ext) {
			$parent++;
		}
	}

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>これまでに受信したトラックバック一覧です。</li>
		<li>削除したいトラックバックにチェックを入れ、<em>削除</em>ボタンを押すと、その記事へのトラックバックを一括削除します。</li>
		<li>表\示したいトラックバックにチェックを入れ、<em>トラックバックを表\示</em>ボタンを押すと、受信したトラックバックを個別に削除することができます。</li>
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>トラックバック削除フォーム</legend>
			<p>
				<input type="submit" name="tb_clear" value="削除する" class="button">
				<input type="submit" name="tb_list" value="トラックバックを表\示する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="exec" value="trackback">
				<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'}">
			</p>
			<p>$work_mesg</p>
			<table summary="トラックバック一覧">
				<tr>
					<th>削除</th>
					<th>表\示</th>
					<th>No</th>
					<th>記事の日付</th>
					<th>内容</th>
				</tr>
_HTML_

	if (-e "${reserved_trackback_dir}new$reserved_trackback_ext") {
		print <<"_HTML_";
				<tr>
					<td><input type="checkbox" name="del" value="new" class="checkbox"></td>
					<td><input type="radio" name="show" value="new" class="radio"></td>
					<td colspan="3">新着トラックバック</td>
				</tr>
_HTML_
	}

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

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

		if ($reno != 0) {
			next;
		}
		if (! -e "$reserved_trackback_dir$no$reserved_trackback_ext") {
			next;
		}
		$i++;
		if ($i <= $start) {
			next;
		}
		if ($i > $end) {
			last;
		}

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

			$date = "$year/$month/$day";
		}

		print <<"_HTML_";
				<tr>
					<td><input type="checkbox" name="del" value="$no" class="checkbox"></td>
					<td><input type="radio" name="show" value="$no" class="radio"></td>
					<td>$no</td>
					<td>$date</td>
					<td><em>$subj</em>へのトラックバック</td>
				</tr>
_HTML_
	}

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

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

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 送信トラックバック一覧
sub admin_tb_sended {
	open(TB, "$sended_trackback_file") || &error("送信トラックバック一覧ファイルを読み出せません。");
	local @trackback = <TB>;
	close(TB);

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>過去に送信したトラックバックの一覧です。</li>
	</ul>
	<table summary="送信トラックバック一覧">
		<tr>
			<th>送信日時</th>
			<th>送信先URL</th>
			<th>投稿記事の題名</th>
			<th>投稿記事のURL</th>
		</tr>
_HTML_

	foreach (@trackback) {
		chop($_);
		local($tb_no, $tb_date, $tb_url, $tb_subj, $tb_self) = split(/\t/);

		local($sec, $min, $hour, $day, $mon, $year, $week) = localtime($tb_date);
		$tb_date = sprintf("%02d月%02d日%02d:%02d", $mon + 1, $day, $hour, $min);

		local @tb_url  = split(/<br>/, $tb_url);
		local $tb_urls = '';
		foreach $tb_url (@tb_url) {
			$tb_urls .= qq|<a href="$tb_url">$tb_url</a><br>|;
		}

		print <<"_HTML_";
		<tr>
			<td>$tb_date</td>
			<td>$tb_urls</td>
			<td>$tb_subj</td>
			<td><a href="$tb_self">$tb_self</a></td>
		</tr>
_HTML_
	}

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理モード - コンテンツリスト更新
sub admin_contents_update {
	local @contents = ();
	foreach (0 .. ($contents_size - 1)) {
		if ($in{"name$_"} eq '') {
			next;
		}
		if ($in{"url$_"} eq '' || $in{"url$_"} eq 'http://') {
			next;
		}
		push(@contents, "$in{\"name$_\"}\t$in{\"url$_\"}\t$in{\"field$_\"}\n");
	}

	open(CONTENTS, ">$contents_file") || &error("コンテンツリスト保存ファイルに書き込めません。");
	print CONTENTS @contents;
	close(CONTENTS);

	return;
}

### 管理モード - コンテンツ一覧
sub admin_contents_list {
	open(CONTENTS, "$contents_file") || &error("コンテンツリスト保存ファイルを読み出せません。");
	local @contents = <CONTENTS>;
	close(CONTENTS);

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>トップページに表\示されるコンテンツの設定を行います。</li>
		<li>各項目を設定して<em>更新</em>ボタンを押してください。</li>
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>コンテンツ設定フォーム</legend>
			<p>
				<input type="submit" name="contents_update" value="更新する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="exec" value="contents">
				<input type="hidden" name="mode" value="admin">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
			</p>
			<p>$work_mesg</p>
			<table summary="コンテンツ一覧">
				<tr>
					<th>コンテンツ</th>
					<th>URL</th>
_HTML_

	if ($contents_fields[0] ne '') {
		print qq|\t\t\t\t\t<th>分類</th>\n|;
	}

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

	foreach (0 .. ($contents_size - 1)) {
		chop($contents[$_]);
		local($name, $url, $field) = split(/\t/, $contents[$_]);

		print <<"_HTML_";
				<tr>
					<td><input type="text" name="name$_" size="40" value="$name"></td>
					<td><input type="text" name="url$_" size="50" value="$url"></td>
_HTML_

		if ($contents_fields[0] ne '') {
			print <<"_HTML_";
					<td>
						<select name="field$_">
_HTML_

			local $flag = 0;
			foreach $contents_field (@contents_fields) {
				if ($contents_field eq '') {
					next;
				}
				if ($field eq $contents_field) {
					print qq|\t\t\t\t\t\t\t<option value="$contents_field" selected>$contents_field</option>\n|;
					$flag = 1;
				} else {
					print qq|\t\t\t\t\t\t\t<option value="$contents_field">$contents_field</option>\n|;
				}
			}
			if ($flag == 0) {
				print qq|\t\t\t\t\t\t\t<option value="" selected>未分類</option>\n|;
			} else {
				print qq|\t\t\t\t\t\t\t<option value="">未分類</option>\n|;
			}

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

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

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理モード - リンク集更新
sub admin_link_update {
	local @link = ();
	foreach (0 .. ($link_size - 1)) {
		if ($in{"name$_"} eq '') {
			next;
		}
		if ($in{"url$_"} eq '' || $in{"url$_"} eq 'http://') {
			next;
		}
		push(@link, "$in{\"name$_\"}\t$in{\"url$_\"}\t$in{\"field$_\"}\n");
	}

	open(LINK, ">$link_file") || &error("ミニリンク集保存ファイルに書き込めません。");
	print LINK @link;
	close(LINK);

	return;
}

### 管理モード - リンク集一覧
sub admin_link_list {
	open(LINK, "$link_file") || &error("ミニリンク集保存ファイルを読み出せません。");
	local @link = <LINK>;
	close(LINK);

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>トップページに表\示されるリンク集の設定を行います。</li>
		<li>各項目を設定して<em>更新</em>ボタンを押してください。</li>
	</ul>
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>リンク集設定フォーム</legend>
			<p>
				<input type="submit" name="link_update" value="更新する" class="button">
				<input type="reset" value="リセット" class="button">
				<input type="hidden" name="exec" value="link">
				<input type="hidden" name="mode" value="admin">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
			</p>
			<p>$work_mesg</p>
			<table summary="リンク一覧">
				<tr>
					<th>サイト名</th>
					<th>URL</th>
_HTML_

	if ($link_fields[0] ne '') {
		print qq|\t\t\t\t\t<th>分類</th>\n|;
	}

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

	foreach (0 .. ($link_size - 1)) {
		chop($link[$_]);
		local($name, $url, $field) = split(/\t/, $link[$_]);

		if ($url eq '') {
			$url = 'http://';
		}

		print <<"_HTML_";
				<tr>
					<td><input type="text" name="name$_" size="40" value="$name"></td>
					<td><input type="text" name="url$_" size="50" value="$url"></td>
_HTML_

		if ($link_fields[0] ne '') {
			print <<"_HTML_";
					<td>
						<select name="field$_">
_HTML_

			local $flag = 0;
			foreach $link_field (@link_fields) {
				if ($link_field eq '') {
					next;
				}
				if ($field eq $link_field) {
					print qq|\t\t\t\t\t\t\t<option value="$link_field" selected>$link_field</option>\n|;
					$flag = 1;
				} else {
					print qq|\t\t\t\t\t\t\t<option value="$link_field">$link_field</option>\n|;
				}
			}
			if ($flag == 0) {
				print qq|\t\t\t\t\t\t\t<option value="" selected>未分類</option>\n|;
			} else {
				print qq|\t\t\t\t\t\t\t<option value="">未分類</option>\n|;
			}

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

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

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理モード - ログダウンロード
sub admin_download {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません。");
	local @logs = <FILE>;
	close(FILE);

	if ($download_mode == 1) {
		$log_file =~ /([^\/\\]*)\.([^.\/\\]*)$/;
		local $filename = "$1\.$2";

		print "Content-type: application/x-csv\n";
		print "Content-Disposition: inline; filename=$filename\n\n";
		print @logs;
	} else {
		print "Content-type: text/download\n\n";
		print @logs;
	}

	exit;
}

### 管理モード - 削除修正用ログ一覧
sub admin_list {
	open(FILE, "$log_file") || &error("ログファイルを読み出せません。");
	local @logs = <FILE>;
	close(FILE);

	local $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("過去ログディレクトリを読み出せません。");
		local @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));
	}

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

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

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

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/管理者用<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	&admin_modelist($in{'exec'});

	print <<"_HTML_";
	<ul>
		<li>削除したい記事にチェックを入れ、<em>削除</em>ボタンを押してください。</li>
		<li>修正したい記事にチェックを入れ、<em>修正</em>ボタンを押してください。</li>
		<li><em>ダウンロード</em>ボタンを押すと、現在のログをダウンロードします。</li>
_HTML_

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

	print <<"_HTML_";
	</ul>
	<div style="clear:both;">
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>記事編集フォーム</legend>
			<p>
				<input type="submit" name="delete" value="削除する" class="button">
				<input type="submit" name="modify" value="修正する" class="button">
				<input type="reset" value="リセット" class="button">
_HTML_

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

	print <<"_HTML_";
				<input type="submit" name="download" value="ダウンロード" class="button">
				<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'}">
			</p>
			
			<legend>記事数
				<em>$parent</em>件
			</legend>

_HTML_



	print <<"_HTML_";
			
			<p>$work_mesg</p>
			<table summary="記事一覧">
				<tr>
					<th>削除</th>
					<th>修正</th>
					<th>No</th>
					<th>日付</th>
_HTML_

	if ($res_mode == 1 || $author_mode == 1) {
		print qq|\t\t\t\t\t<th>投稿者</th>\n|;
	}

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

	if ($upload_mode == 1) {
		print qq|\t\t\t\t\t<th>ファイル</th>\n|;
	}

	print qq|\t\t\t\t\t<th>状態</th>\n|;

	if ($res_mode == 1) {
		print qq|\t\t\t\t\t<th>ホスト</th>\n|;
	}

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

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

	local $i = 0;
	foreach (@logs) {
		chop($_);
		local($no, $reno, $display, $date, $field, $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)(\d\d)(\d\d)$/) {
			local $year  = sprintf("%04d", $1);
			local $month = sprintf("%02d", $2);
			local $day   = sprintf("%02d", $3);
			local $week = $week[&getweek($day, $year, $month)];

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

		if ($reno == 0) {
			if ($name eq '') {
				$name = '管理者';
			}
			$modi = qq|<input type="radio" name="modi" value="$no" class="radio">|;
		} else {
			$modi = '';
		}
		$text =~ s/<[^>]*>//g;
		if (length($text) > 60) {
			$text = substr($text, 0, 60) . ' …';
		}
		if ($file eq '') {
			$file_list = '';
		} else {
			local @file = split(/<>/, $file);
			$file_list = '';
			foreach $file (@file) {
				$file_list .= qq|<div class="file"><a href="$upfile_url$file">$file</a></div>|;
			}
		}
		if ($display == 1) {
			$display = '公開';
		} else {
			$display = '下書き';
		}

		print <<"_HTML_";
				<tr>
					<td><input type="checkbox" name="del" value="$no" class="checkbox"></td>
					<td>$modi</td>
					<td>$no</td>
					<td>$date</td>
_HTML_
		if ($res_mode == 1 || $author_mode == 1) {
			print qq|\t\t\t\t\t<td>$name</td>\n|;
		}

		print <<"_HTML_";
					<td>$subj</td>
					<td>$text</td>
_HTML_

		if ($upload_mode == 1) {
			print qq|\t\t\t\t\t<td>$file_list</td>\n|;
		}

		print qq|\t\t\t\t\t<td>$display</td>\n|;

		if ($res_mode == 1) {
			print qq|\t\t\t\t\t<td><small>$host</small></td>\n|;
		}

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

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

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

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

	print &skin_basic($skin{'link'});

	&footer();
}

### 管理モード - 実行モードリスト
sub admin_modelist {
	local($_) = @_;

	if ($_ eq 'new') {
		$new_selected = ' selected';
	} elsif ($_ eq 'profile') {
		$profile_selected = ' selected';
	} elsif ($_ eq 'trackback') {
		$trackback_selected = ' selected';
	} elsif ($_ eq 'tbsended') {
		$tbsended_selected = ' selected';
	} elsif ($_ eq 'contents') {
		$contents_selected = ' selected';
	} elsif ($_ eq 'link') {
		$link_selected = ' selected';
	} else {
		$edit_selected = ' selected';
	}

	print <<"_HTML_";
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>表\示設定フォーム</legend>
			<p>
				<select name="exec">
					<option value="new"$new_selected>新規投稿</option>
					<option value=""$edit_selected>記事編集</option>
_HTML_

	if ($profile_mode == 1) {
		print <<"_HTML_";
					<option value="profile"$profile_selected>プロフィール設定</option>
_HTML_
	}
	if ($trackback_mode == 1) {
		print <<"_HTML_";
					<option value="trackback"$trackback_selected>受信トラックバック削除</option>
					<option value="tbsended"$tbsended_selected>送信トラックバック表\示</option>
_HTML_
	}
	if ($contents_mode == 1) {
		print <<"_HTML_";
					<option value="contents"$contents_selected>コンテンツ設定</option>
_HTML_
	}
	if ($link_mode == 1) {
		print <<"_HTML_";
					<option value="link"$link_selected>リンク集設定</option>
_HTML_
	}

	print <<"_HTML_";
				</select>
				<input type="submit" value="実行する" class="button">
				<input type="hidden" name="admin_pwd" value="$in{'admin_pwd'}">
				<input type="hidden" name="mode" value="admin">
			</p>
		</fieldset>
	</form>
_HTML_

	return;
}

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

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

	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("ログファイルを読み出せません。");
	local @logs = <FILE>;
	close(FILE);

	local $no     = 0;
	local $re     = 0;
	local $parent = 0;
	local $name   = '';
	local $text   = '';
	foreach (@logs) {
		if ($_ + 0 > $no) {
			($no) = (split(/\t/))[0];
		}
		if ($_ + 0 > $re && (split(/\t/))[1] != 0) {
			($re, $date, $name, $text, $ip) = (split(/\t/))[0, 3, 5, 9, 14];
			chop($ip);
		}
		if ((split(/\t/))[1] == 0) {
			$parent++;
		}
	}
	$no++;

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

	local $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 ($file_name{'file'} ne '') {
		$file_name{'file'} = &upfile($file_name{'file'}, $file{'file'}, $no);
	}

	local $line = "$no\t$in{'reno'}\t1\t$date\t\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";

	local @new  = ();
	local $flag = 0;
	foreach (@logs) {
		chop($_);
		local($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, "$_\n");
	}
	if ($flag == 1) {
		push(@new, $line);
	}
	@logs = @new;

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

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

	&unlock($lock_file);

	if ($sendmail_mode == 1 || $sendmail_mode == 3) {
		&sendmail();
	}

	%in = ();

	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;
		}
	}
	local $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 ($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 ($ng_word[0] ne '') {
		foreach (@ng_word) {
			if ($_ ne '' && $in{'text'} =~ /$_/) {
				&error("『$_』は使用禁止ワードです。");
			}
		}
	}
	if ($admin_icon_mode == 1 && $in{'icon'} eq $admin_icon[0] && $in{'pwd'} ne $admin_pwd) {
		&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'}$in{'hour'}$in{'minute'}";
		if ($in{'regist'} ne '') {
			$form_mode = ' name="regist"';
		} else {
			$form_mode = ' name="modi"';
		}
		$in{'text'} =~ s/&/&amp;/g;
	} else {
		$date = time();
		$in{'no'} = $in{'reno'};
	}

	if ($in{'reno'} eq '') {
		$in{'reno'} = 0;
	}
	if ($in{'subj'} eq '') {
		$in{'subj'} = '無題';
	}
	if ($in{'url'} eq 'http://') {
		$in{'url'} = '';
	}
	local $preview_text = $in{'text'};
	$in{'text'}          = &create_plain($in{'text'});
	$in{'trackback_url'} = &create_plain($in{'trackback_url'});

	if ($upload_mode == 1 && $in{'mode'} eq 'admin') {
		@file_name = ();
		@ext       = ();
		local $i = 0;
		foreach $file ('file1', 'file2', 'file3') {
			$i++;
			if ($file_name{$file} eq '') {
				if ($in{'org' . $file} ne '') {
					push(@file_name, $in{'org' . $file});
				}
			} else {
				$file_name{$file} =~ /([^\/\\]*)\.([^.\/\\]*)$/;
				$ext{$file} = $2;
				$ext{$file} =~ tr/A-Z/a-z/;

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

				push(@file_name, $file_name{$file});
			}
		}
	}

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/プレビュー<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print <<"_HTML_";
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>投稿フォーム</legend>
			<p>
				<input type="submit" name="back" value="修正する" class="button">
				<input type="submit"$form_mode value="この記事を投稿する" class="button">
				<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="sort" value="$in{'sort'}">
				<input type="hidden" name="display" value="$in{'display'}">
				<input type="hidden" name="date" value="$in{'date'}">
				<input type="hidden" name="hour" value="$in{'hour'}">
				<input type="hidden" name="minute" value="$in{'minute'}">
				<input type="hidden" name="field" value="$in{'field'}">
				<input type="hidden" name="no" value="$in{'no'}">
				<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="orgfile1" value="$in{'orgfile1'}">
				<input type="hidden" name="orgfile2" value="$in{'orgfile2'}">
				<input type="hidden" name="orgfile3" value="$in{'orgfile3'}">
				<input type="hidden" name="ext1" value="$ext{'file1'}">
				<input type="hidden" name="ext2" value="$ext{'file2'}">
				<input type="hidden" name="ext3" value="$ext{'file3'}">
				<input type="hidden" name="delfile1" value="$in{'delfile1'}">
				<input type="hidden" name="delfile2" value="$in{'delfile2'}">
				<input type="hidden" name="delfile3" value="$in{'delfile3'}">
				<input type="hidden" name="trackback_url" value="$in{'trackback_url'}">
			</p>
		</fieldset>
	</form>
</div>
_HTML_

	local $file_name = join('<>', @file_name);

	local $logs[0] = "XXX\t$in{'reno'}\t1\t$date\t$in{'field'}\t$in{'name'}\t$in{'mail'}\t$in{'url'}\t$in{'subj'}\t$preview_text\t$in{'color'}\t$in{'icon'}\t$file_name\t\t$in{'pwd'}\t$host\n";
	&showlog(@logs);
	print $html{'logs'};

	print &skin_basic($skin{'link'});

	&footer();
}

### デコード
sub decode {
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		local $length = $ENV{'CONTENT_LENGTH'};
		binmode(STDIN);
		while ($length > 0) {
			$length  -= sysread(STDIN, $buffer, $length);
			$alldata .= $buffer;
		}

		%in        = ();
		%file      = ();
		%file_name = ();
		%file_size = ();
		%mime_type = ();

		local @in = ();
		local $flag      = 0;
		local $data_flag = 0;
		local $interval  = '';
		local $key       = '';
		foreach $data (split(/\r\n/, $alldata)) {
			if ($interval eq '') {
				$interval = $data;
			} else {
				if ($data !~ /^$interval/i) {
					if ($data =~ /^Content-Disposition:\sform-data;\sname="([^"]*)"/i && $key eq '') {
						$key = $1;
						if ($data =~ /filename="([^"]*)"/i) {
							$file_name{"$key"} = $1;
							$flag = 1;
						}
						$data_flag = 0;
					} elsif ($data =~ /^Content-type:\s(\S+)/i) {
						$mime_type{"$key"} = $1;
					} else {
						if ($data_flag == 0) {
							$data_flag = 1;
							next;
						}
						if ($flag == 1) {
							$file{"$key"} .= "$data\r\n";
						} else {
							$in{"$key"} .= "$data\n";
						}
						$file_size{"$key"} += length($data);
					}
				} else {
					local $in_flag = 0;
					foreach $in (@in) {
						if ($in eq $key) {
							$in_flag = 1;
						}
					}
					if ($in_flag == 0) {
						push(@in, $key);
					}
					$key  = '';
					$flag = 0;
				}
			}
		}
	} else {
		local $alldata = $ENV{'QUERY_STRING'};

		foreach $data (split(/&/, $alldata)) {
			local($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/\n/;
		$value =~ s/^\n//;
		$value =~ s/\n$//;

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

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

		$in{"$key"} = $value;
	}
	while (($key, $value) = each(%file_name)) {
		$value =~ s/^\n//;
		$value =~ s/\n$//;

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

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

		$file_name{"$key"} = $value;
	}

	return;
}

### HTMLヘッダ
sub header {
	if ($html_flag == 0) {
		print "Content-Type: text/html\n\n";
	}
	print &skin_basic($skin{'header'});

	return;
}

### HTMLフッタ
sub footer {
	print &skin_basic($skin{'footer'});

	if ($html_flag == 0) {
		exit();
	}
}

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

	if ($html_flag == 1) {
		*STDOUT = $stdout;
		$html_flag = 0;
	}

	if ($mobile == 1) {
		$html_mobile .= <<"_HTML_";
◆記事投稿
<hr noshade size='1'>
ｴﾗｰが発生しました。<br>
$_<br>
<hr noshade size='1'>
</body>

</html>
_HTML_

		local $length = length($html_mobile);

		print "Content-Type: text/html\n";
		print "Content-Length: $length\n\n";
		print $html_mobile;
		exit();
	}

	&header();

	print qq|<div id="contents2">\n|;

	$skin{'subtitle'} =~ s/<!--SUBTITLE_START-->/エラーが発生しました<!--/g;
	$skin{'subtitle'} =~ s/<!--SUBTITLE_END-->/-->/g;

	print &skin_basic($skin{'subtitle'});

	print <<"_HTML_";
	<ul>
		<li>$_</li>
	</ul>
_HTML_

	if ($in{'mode'} eq 'regist' || $in{'regist'} ne '' || $in{'modi'} ne '') {
		if ($in{'mode'} eq 'regist') {
			$in{'mode'} = 'res';
			$in{'no'}   = $in{'reno'};
		}

		$in{'text'}          = &create_plain($in{'text'});
		$in{'trackback_url'} = &create_plain($in{'trackback_url'});

		print <<"_HTML_";
	<form action="$script" method="post" enctype="multipart/form-data">
		<fieldset>
			<legend>投稿フォーム</legend>
			<p>
				<input type="submit" name="back" value="修正する" class="button">
				<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="display" value="$in{'display'}">
				<input type="hidden" name="no" value="$in{'no'}">
				<input type="hidden" name="date" value="$in{'date'}">
				<input type="hidden" name="hour" value="$in{'hour'}">
				<input type="hidden" name="minute" value="$in{'minute'}">
				<input type="hidden" name="field" value="$in{'field'}">
				<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="orgfile1" value="$in{'orgfile1'}">
				<input type="hidden" name="orgfile2" value="$in{'orgfile2'}">
				<input type="hidden" name="orgfile3" value="$in{'orgfile3'}">
				<input type="hidden" name="ext1" value="$in{'ext1'}">
				<input type="hidden" name="ext2" value="$in{'ext2'}">
				<input type="hidden" name="ext3" value="$in{'ext3'}">
				<input type="hidden" name="host" value="$in{'host'}">
				<input type="hidden" name="trackback_url" value="$in{'trackback_url'}">
			</p>
		</fieldset>
	</form>
_HTML_
	}

	print "</div>\n";

	print &skin_basic($skin{'link'});

	&footer();
}

### スキン読み込みエラー
sub skin_error {
	print <<"_HTML_";
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//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>Web Diary Professional ver $ver</title>
</head>

<body>
<h1>Web Diary Professional Ver $ver</h1>
<h2>エラーが発生しました</h2>
<ul>
	<li>スキンファイルを読み出せません。</li>
</ul>
<ul>
	<li><a href="javascript:history.back();">戻る</a></li>
</ul>
<!-- Copyright -->
<p><a href="http://www.web-liberty.net/">Web Diary Professional ver $ver</a></p>
</body>

</html>
_HTML_

	exit();
}

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

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

	local $method = '';
	if ($agent =~ /J-PHONE/i) {
		$method = 'get';
	} else {
		$method = 'post';
	}

	local $html_mobile = <<"_HTML_";
<html>

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

<body text="#333333" link="#333333" vlink="#333333" alink="#9D8937">
<center><br><font color="olive">STAFFS'VOICE</font></center>
<hr noshade size='1'>
_HTML_

	&mobile_decode();

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

		&mobile_check();

		&lock($lock_file);

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

		if ($in{'edit'} ne '') {
			&mobile_edit();
		} else {
			&mobile_regist();
		}

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

		if ($html_mode == 1) {
			&skin();
			&output_htmlfile();
		}
		if ($js_mode != 0) {
			&output_jsfile(@logs);
		}

		&unlock($lock_file);

		$in{'mode'} = '';
	} elsif ($in{'mode'} eq 'responce') {
		if ($html_mode == 1) {
			&skin();
		}
		#コメント投稿
		&regist();
	}

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

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

	if ($in{'mode'} eq 'form') {
		if ($in{'adminpwd'} eq '') {
			#ログインフォーム表示
			&mobile_login();
		} else {
			#記事投稿フォーム表示
			&mobile_form();
		}
	} elsif ($in{'mode'} eq 'edit') {
		#記事編集フォーム表示
		&mobile_editform();
	} elsif ($in{'mode'} eq 'res') {
		#返信フォーム表示
		&mobile_res();
	} elsif ($in{'mode'} eq 'delete') {
		#記事削除
		&mobile_delete();
	} elsif ($in{'mode'} eq 'tblist') {
		#新着トラックバック
		&mobile_tblist();
	} elsif ($in{'mode'} eq 'reslist') {
		#新着コメント
		&mobile_reslist();d
	} elsif ($in{'mode'} eq 'all') {
		#記事表示
		&mobile_all();
	} else {
		if ($counter_mode != 0) {
			if ($ENV{'REQUEST_METHOD'} ne 'POST' && $ENV{'QUERY_STRING'} eq '') {
				&count_up();
			}

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

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

		#記事一覧
		&mobile_list();
	}

	$html_mobile .= <<"_HTML_";
<hr noshade size='1'>
<font size="1"><center>COPYRIGHT 1999-2008<br>WESS INC.<br>ALL RIGHTS RESERVED.</center></font>
</body>

</html>
_HTML_

	local $length = length($html_mobile);

	print "Content-Type: text/html\n";
	print "Content-Length: $length\n\n";
	print $html_mobile;
	exit();
}

### モバイル表示 - デコード
sub mobile_decode {
	local $alldata = '';
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		if ($ENV{'CONTENT_LENGTH'} > 15000) {
			&error("投稿量が大きすぎます。現在の投稿量：$ENV{'CONTENT_LENGTH'} / 最大投稿量：15000。");
		}
		read(STDIN, $alldata, $ENV{'CONTENT_LENGTH'});
	} else {
		$alldata = $ENV{'QUERY_STRING'};
	}
	foreach $data (split(/&/, $alldata)) {
		local($key, $value) = split(/=/, $data);

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

		$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');

		$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;

	return;
}

### モバイル表示 - 投稿内容チェック
sub mobile_check {
	$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 ($time_mode == 1) {
		if ($in{'time'} !~ /^\d\d\d\d$/) {
			&error("時間の値が不正です。");
		}
	} else {
		$in{'time'} = '0000';
	}
	if ($fields[1] ne '' && $in{'field'} eq '') {
		&error("分類が選択されていません。");
	}
	if ($author_mode == 1 && $in{'name'} eq '') {
		&error("投稿者が選択されていません。");
	}
	if ($in{'text'} eq '') {
		&error("本文が入力されていません。");
	}

	return;
}

### モバイル表示 - 記事投稿
sub mobile_regist {
	local $host = $ENV{'REMOTE_HOST'};
	local $addr = $ENV{'REMOTE_ADDR'};
	if ($host eq "" || $host eq $addr) {
		if ($gethostbyaddr == 1) {
			$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2) || $addr;
		}
	}

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

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

	local $line = "$no\t0\t$in{'display'}\t$in{'date'}$in{'time'}\t$in{'field'}\t$in{'name'}\t\t\t$in{'subj'}\t$in{'text'}\t\t\t\t\t\t$host\n";

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

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

	return;
}

### モバイル表示 - 記事編集
sub mobile_edit {
	$in{'file'} =~ s/&lt;&gt;/<>/g;
	$line = "$in{'edit'}\t0\t$in{'display'}\t$in{'date'}$in{'time'}\t$in{'field'}\t$in{'name'}\t\t\t$in{'subj'}\t$in{'text'}\t\t\t$in{'file'}\t\t\t$in{'host'}\n";

	local @new = ();
	local @tmp = ();
	foreach (@logs) {
		local($no, $reno) = split(/\t/);
		if ($in{'edit'} == $no) {
			push(@tmp, $line);
		} elsif ($in{'edit'} == $reno) {
			push(@tmp, $_);
		} else {
			push(@new, $_);
		}
	}
	@logs = ();
	local $flag = 0;
	foreach (@new) {
		local($no, $reno, $display, $date) = split(/\t/);
		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$date = "$1$2$3";
		}
		if ($flag == 0 && $reno == 0 && $in{'date'} + 0 >= $date) {
			$flag = 1;
			push(@logs, @tmp);
		}
		push(@logs, $_);
	}
	if ($flag == 0) {
		push(@logs, @tmp);
	}

	return;
}

### モバイル表示 - ログインフォーム表示
sub mobile_login {
	$html_mobile .= <<"_HTML_";
◆記事投稿
<hr noshade size='1'>
ﾊﾟｽﾜｰﾄﾞを入力してください<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>
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 記事投稿フォーム表示
sub mobile_form {
	if ($in{'adminpwd'} ne $admin_pwd) {
		&error("ﾊﾟｽﾜｰﾄﾞが違います。");
	}

	if ($in{'edit'} eq '') {
		($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
		$date = sprintf("%04d%02d%02d", $year + 1900, $mon + 1, $day);
		$time = sprintf("%02d%02d", $hour, $min);

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

			if ($in{'edit'} == $no) {
				last;
			}
		}

		if ($reno != 0) {
			&error("携帯からｺﾒﾝﾄの編集はできません。");
		}

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

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

	if ($in{'edit'} eq '') {
		$html_mobile .= "◆記事投稿\n";
	} else {
		$html_mobile .= "◆記事編集\n";
	}

	$html_mobile .= <<"_HTML_";
<hr noshade size='1'>
<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>
_HTML_

	if ($time_mode == 1) {
		$html_mobile .= <<"_HTML_";
時間<br>
<input type="text" name="time" size="12" value="$time"><br>
_HTML_
	}

	if ($fields[1] ne '') {
		$html_mobile .= <<"_HTML_";
分類<br>
<select name="field">
	<option value="">選択してください</option>
_HTML_

		foreach (@fields) {
			if ($_ eq '') {
				next;
			}
			if ($_ eq $field) {
				$html_mobile .= qq|\t<option value="$_" selected>$_</option>\n|;
			} else {
				$html_mobile .= qq|\t<option value="$_">$_</option>\n|;
			}
		}

		$html_mobile .= <<"_HTML_";
</select><br>
_HTML_
	}
	if ($author_mode == 1) {
		$html_mobile .= <<"_HTML_";
投稿者<br>
<select name="name">
	<option value="">選択してください</option>
_HTML_

		foreach (@author) {
			if ($_ eq '') {
				next;
			}
			if ($_ eq $name) {
				$html_mobile .= qq|\t<option value="$_" selected>$_</option>\n|;
			} else {
				$html_mobile .= qq|\t<option value="$_">$_</option>\n|;
			}
		}

		$html_mobile .= <<"_HTML_";
</select><br>
_HTML_
	}

	$html_mobile .= <<"_HTML_";
本文<br>
<textarea name="text" cols="14" rows="5">$text</textarea><br>
記事の状態<br>
_HTML_
	if ($display == 0) {
		$html_mobile .= <<"_HTML_";
<select name="display">
	<option value="1">公開</option>
	<option value="0" selected>下書き</option>
</select>
_HTML_
	} else {
		$html_mobile .= <<"_HTML_";
<select name="display">
	<option value="1" selected>公開</option>
	<option value="0">下書き</option>
</select>
_HTML_
	}

	$html_mobile .= <<"_HTML_";
<br>
<br>
<input type="submit" value="書き込む">
<input type="hidden" name="mode" value="regist">
<input type="hidden" name="edit" value="$in{'edit'}">
_HTML_

	if ($fields[1] eq '') {
		$html_mobile .= <<"_HTML_";
<input type="hidden" name="field" value="$fields[0]">
_HTML_
	}

	$html_mobile .= <<"_HTML_";
<input type="hidden" name="file" value="$file">
<input type="hidden" name="host" value="$host">
<input type="hidden" name="adminpwd" value="$in{'adminpwd'}">
</form>
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 記事編集フォーム表示
sub mobile_editform {
	$html_mobile .= <<"_HTML_";
◆記事編集
<hr noshade size='1'>
<form action="$script" method="$method">
編集する記事の番号<br>
<input type="text" name="edit" size="6" value="$in{'no'}"><br>
ﾊﾟｽﾜｰﾄﾞ<br>
<input type="password" name="adminpwd" size="6"><br>
<br>
<input type="submit" value="認証">
<input type="hidden" name="mode" value="form">
</form>
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 返信フォーム表示
sub mobile_res {
	foreach (@logs) {
		($no, $reno, $display, $date, $field, $name, $mail, $url, $subj) = split(/\t/);

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

	$html_mobile .= <<"_HTML_";
◆コメント投稿
<hr noshade size='1'>
<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" value="http://"><br>
本文<br>
<textarea name="text" cols="14" 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>
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 記事削除
sub mobile_delete {
	if ($in{'del'} ne '') {
		if ($in{'user_pwd'} eq '') {
			&error("削除ｷｰを入力してください。");
		}

		&lock($lock_file);

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

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

			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 ";
				foreach $file_name (split(/<>/, $file)) {
					if (-e "$upfile_dir$file_name") {
						unlink("$upfile_dir$file_name");
					}
					if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file_name") {
						unlink("$thumbnail_dir$file_name");
					}
				}
				if (-e "$reserved_trackback_dir$no$reserved_trackback_ext") {
					unlink("$reserved_trackback_dir$no$reserved_trackback_ext");
				}
			}
		}
		if ($del_list eq '') {
			&error("該当記事がありません。");
		}
		$work_mesg = "$del_list番の記事を削除しました。";

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

		if ($html_mode == 1) {
			&skin();
			&output_htmlfile();
		}
		if ($js_mode != 0) {
			&output_jsfile(@logs);
		}

		&unlock($lock_file);
	}

	$html_mobile .= <<"_HTML_";
◆記事削除
<hr noshade size='1'>
$work_mesg
<form action="$script" method="$method">
削除する記事の番号<br>
<input type="text" name="del" size="6" value="$in{'no'}"><br>
削除ｷｰ<br>
<input type="password" name="user_pwd" size="6"><br>
<br>
<input type="submit" value="削除する">
<input type="hidden" name="mode" value="delete">
</form>
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 新着トラックバック
sub mobile_tblist {
	$html_mobile .= <<"_HTML_";
◆新着トラックバック
<hr noshade size='1'>
_HTML_

	local @trackback = ();
	if (-e "${reserved_trackback_dir}new$reserved_trackback_ext") {
		open(TB, "${reserved_trackback_dir}new$reserved_trackback_ext") || &error_trackback("Read error");
		@trackback = <TB>;
		close(TB);
	}

	if ($in{'no'} eq '') {
		local $i = 0;
		foreach (@trackback) {
			if ($i >= $show_trackback_list) {
				last;
			}

			local($no, $time, $blog_name, $title, $url, $excerpt) = split(/\t/);

			($sec, $min, $hour, $day, $mon, $year, $week) = localtime($time);
			$time = sprintf("%02d/%02d-%02d:%02d", $mon + 1, $day, $hour, $min);

			$html_mobile .= qq|▼<a href="$script?mode=tblist&amp;no=$no">$title</a> - $blog_name($time)<br>|;

			$i++;
		}
	} else {
		foreach (@trackback) {
			local($no, $time, $blog_name, $title, $url, $excerpt) = split(/\t/);

			if ($in{'no'} == $no) {
				($sec, $min, $hour, $day, $mon, $year, $week) = localtime($time);
				$time = sprintf("%02d/%02d-%02d:%02d", $mon + 1, $day, $hour, $min);

				$html_mobile .= <<"_HTML_";
題名：$title<br>
ブログ名：$blog_name<br>
<br>
$excerpt <a href="$url">続きを読む</a><br>
<br>
受信日時：$time<br>
_HTML_
			}
		}
	}

	$html_mobile .= <<"_HTML_";
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 新着コメント
sub mobile_reslist {
	$html_mobile .= <<"_HTML_";
◆新着コメント
<hr noshade size='1'>
_HTML_

	local %comment_list = ();
	foreach (@logs) {
		chop($_);
		local($no, $reno, $display, $date, $field, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

		if ($reno != 0) {
			$comment_list{"$date"} = "$no\t$reno\t$display\t$date\t$field\t$name\t$mail\t$url\t$subj\t$text\t$color\t$icon\t$file\t$pwd\t$host";
		}
	}

	local $i = 0;
	foreach (sort { $b <=> $a } keys %comment_list) {
		local($no, $reno, $display, $date, $field, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/, $comment_list{$_});

		$i++;
		if ($i > $show_res_list) {
			last;
		}

		($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date);
		$date = sprintf("%02d/%02d-%02d:%02d", $mon + 1, $day, $hour, $min);

		if ($reno != 0) {
			$html_mobile .= qq|▼<a href="$script?mode=all&amp;no=$reno&amp;showres=$no">$subj</a> - $name($date)<br>|;
		}
	}

	$html_mobile .= <<"_HTML_";
<hr noshade size='1'>
▲<a href="$script">記事一覧に戻る</a><br>
_HTML_

	return;
}

### モバイル表示 - 記事表示
sub mobile_all {


	local @show = ();
	local $prev_no = 0;
	local $next_no = 0;
	local $flag    = 0;
	foreach (@logs) {
		local($no, $reno) = split(/\t/);

		if ($in{'no'} == $no || $in{'no'} == $reno) {
			push(@show, $_);
		}
		if ($flag == 1 && $reno == 0) {
			$prev_no = $no;
			$flag = 2;
		}
		if ($in{'no'} == $no) {
			$flag = 1;
		}
		if ($flag == 0 && $reno == 0) {
			$next_no = $no;
		}
	}

	local $next_view = '≪次の日記';
	local $prev_view = '前の日記≫';

	if ($next_no != 0) {
		$next_view = qq|<a href="$script?mode=all&amp;no=$next_no">$next_view</a>|;
	}
	if ($prev_no != 0) {
		$prev_view = qq|<a href="$script?mode=all&amp;no=$prev_no">$prev_view</a>|;
	}

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

		if ($display == 0) {
			next;
		}

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

		if ($date =~ /^(\d\d\d\d)(\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 = '<font color="#FF0000">New!</font>';
			} else {
				$new = '';
			}

			local $year   = sprintf("%04d", $1);
			local $month  = sprintf("%02d", $2);
			local $day    = sprintf("%02d", $3);
			local $hour   = sprintf("%02d", $4);
			local $minute = sprintf("%02d", $5);
			local $week   = $week[&getweek($day, $year, $month)];

			$date = "$month月$day日($week)";

			if ($time_mode == 1) {
				$date .= "$hour:$minute";
			}
		} else {
			($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date);
			$date = sprintf("%02d月%02d日%02d:%02d", $mon + 1, $day, $hour, $min);
		}

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

		if ($file eq '') {
			$file_list = '';
		} else {
			local @file = split(/<>/, $file);
			local @file_list = ();
			local @file_info = ();
			local $i = 1;
			foreach $file (@file) {
				if ($thumbnail_mode == 1 && -e "$thumbnail_dir$file") {
					$file_info[$i] = qq|<a href="$thumbnail_url$file">写真</a>|;
				} else {
					$file_info[$i] = qq|<a href="$upfile_url$file">写真</a>|;
				}
				if ($text !~ /\$FILE$i/) {
					push(@file_list, $file_info[$i]);
				}
				$i++;
			}
			$file_list = '';
			foreach $file (@file_list) {
				$file_list .= " $file";
			}
			foreach $j (1 .. 3) {
				if ($j > $i) {
					last;
				}

				$text =~ s/\$FILE${j}_c/$file_info[$j]/g;
				$text =~ s/\$FILE${j}_l/$file_info[$j]/g;
				$text =~ s/\$FILE${j}_r/$file_info[$j]/g;
				$text =~ s/\$FILE${j}/$file_info[$j]/g;
			}
		}

		if ($res_mode == 1 && $logs[0] + 0 != 0) {
			$res = qq|▼<a href="$script?mode=res&amp;no=$in{'no'}">ｺﾒﾝﾄを書く</a><br>|;
		}

		if ($reno == 0) {
			if ($in{'showres'} eq '') {
				if ($fields[1] ne '') {
					$filed = "分類：$field<br>";
				} else {
					$filed = '';
				}
				if ($author_mode == 1 && $name ne '') {
					$name = "投稿者：$name<br>";
				} else {
					$name = '';
				}

				if ($pause_string ne '' && $text =~ /$pause_string/) {
					if ($in{'showall'} eq '') {
						$text = (split(/$pause_string/, $text, 2))[0];
						$text .= qq|... <a href="$script?mode=all&amp;no=$in{'no'}&amp;showall=on">続きを読む</a>|;
					} else {
						$text =~ s/$pause_string//;
					}
				}

				$html_mobile .= <<"_HTML_";
<br>
[$no] $subj$file_list<br>
<br>
$text<br>
<br>
$filed$name$date<br>
_HTML_
			} else {
				$html_mobile .= qq|<hr noshade size='1'>▼<a href="$script?mode=all&amp;no=$in{'no'}">$subj</a> - $date|;
			}
		} else {
			if ($flag == 0) {
				$html_mobile .= "<hr noshade size='1'>◆コメント<hr noshade size='1'>";
				$flag = 1;
			}
			$i++;

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

				if ($i != 1) {
					$html_mobile .= qq|---------------<br>|;
				}
				$html_mobile .= <<"_HTML_";
[$no] $subj<br>
<br>
投稿者：$name$mail$url$file<br>
<br>
$text<br>
<br>
$date<br>
_HTML_
				if ($i != $#show) {
					$html_mobile .= qq|---------------<br>|;
				}
			} else {
				$html_mobile .= qq|▼<a href="$script?mode=all&amp;no=$in{'no'}&amp;showres=$no">$subj</a> - $name($date)<br>|;
			}
		}
	}

	$html_mobile .= <<"_HTML_";
<br>
<!-- ◆メニュー
<hr noshade size='1'>
$res
▼<a href="$script?mode=edit&amp;no=$in{'no'}">この記事を編集</a><br>
▼<a href="$script?mode=delete&amp;no=$in{'no'}">この記事を削除</a><br>
<br>
//-->
$next_view<br>
$prev_view<br>
<hr noshade size='1'>
<div align='right'>
<a href='http://rsr.wess.co.jp/keitai/2008/'>TOP→</a>
</div>
_HTML_

	return;
}

### モバイル表示 - 記事一覧
sub mobile_list {
	$html_mobile .= <<"_HTML_";


_HTML_

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

	local $i = 0;
	foreach (@logs) {
		chop($_);
		local($no, $reno, $display, $date, $field, $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;
		}
		if ($display == 0) {
			next;
		}

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

		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			local $year   = sprintf("%04d", $1);
			local $month  = sprintf("%02d", $2);
			local $day    = sprintf("%02d", $3);
			local $hour   = sprintf("%02d", $4);
			local $minute = sprintf("%02d", $5);
			local $week   = $week[&getweek($day, $year, $month)];

			$date = "$month/$day($week)";

			if ($time_mode == 1) {
				$date .= "$hour:$minute";
			}
		}

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

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

	if ($trackback_mode == 1) {
		$tblist = qq|▼<a href="$script?mode=tblist">新着ﾄﾗｯｸﾊﾞｯｸ</a><br>|;
	}
	if ($res_mode == 1) {
		$reslist = qq|▼<a href="$script?mode=reslist">新着ｺﾒﾝﾄ</a><br>|;
	}
	if ($counter_mode == 1) {
		$counter = sprintf("◆Access %0${count_figure}d<br>", $sum);
	}

	$html_mobile .= <<"_HTML_";
<hr noshade size='1'>



<!--▲<a href="$mobile_back_url">戻る</a><br>-->
<div align='right'>
<a href='http://rsr.wess.co.jp/keitai/2008/'>TOP→</a>
</div>
_HTML_

	return;
}


### ミニアクセスカウンタカウントアップ
sub count_up {
	local $host = $ENV{'REMOTE_HOST'};
	local $addr = $ENV{'REMOTE_ADDR'};
	if ($host eq "" || $host eq $addr) {
		if ($gethostbyaddr == 1) {
			$host = gethostbyaddr(pack("C4",split(/\./,$addr)),2) || $addr;
		} else {
			$host = $addr;
		}
	}
	local $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("カウントログファイルを読み出せません。");
	local $data = <COUNT>;
	close(COUNT);

	local($sum, $today, $yesterday, $key, $ip) = split(/\t/, $data);
	if ($no_double_count == 1 && $host eq $ip) {
		&unlock($lock_file);
		return;
	}

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

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

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

	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) {
		local @js_logs = ();
		local %js_logs = ();
		local %i = ();
		local $i = 0;
		foreach (@logs) {
			local($no, $reno, $display, $date, $field, $name, $mail, $url, $subj, $text, $color, $icon, $file, $pwd, $host) = split(/\t/);

			if ($reno == 0) {
				$i++;
				$i{"$field"}++;
			} else {
				next;
			}
			if ($display == 0) {
				next;
			}

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

			($file_info, $text) = &set_upfile($text, $file);

			if ($pause_string ne '' && $text =~ /$pause_string/) {
				if ($js_text_url_target ne '') {
					$target = qq| target="$js_text_url_target"|;
				} else {
					$target = '';
				}
				$text = (split(/$pause_string/, $text, 2))[0];
				$text .= qq|... <a href="$js_text_script_url?no=$no"$target>続きを読む</a>|;
			}
			if ($auto_link == 1) {
				$text = &auto_link($text);
			}

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

			$text      =~ s/$upfile_url/$js_text_file_url/g;
			$text      =~ s/$thumbnail_url/$js_text_thumbnail_url/g;
			$file_info =~ s/$upfile_url/$js_text_file_url/g;
			$file_info =~ s/$thumbnail_url/$js_text_thumbnail_url/g;

			if ($file_info ne '') {
				$file_info .= '<br><br>';
			}

			if ($i <= $js_output_text) {
				push(@js_logs, "document.write('■ $subj ... $date<br><br>$file_info$text<hr noshade size='1'>');\n");
			}
			if ($js_text_field_mode == 1 && $i{"$field"} <= $js_output_text) {
				$js_logs{"$field"} .= "document.write('■ $subj ... $date<br><br>$file_info$text<hr noshade size='1'>');\n";
			}
		}

		local $line = qq|document.write('<div align="right"><a href="http://www.web-liberty.net/">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_text_field_mode == 1) {
			foreach (keys %js_text_field_file) {
				if ($_ eq '') {
					next;
				}

				$js_logs{$js_text_field_file{$_}} .= qq|document.write('<div align="right"><a href="http://www.web-liberty.net/">Web Diary Professional</a></div>');\n|;

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

			if ($reno == 0) {
				$i++;
				$i{"$field"}++;
			} else {
				next;
			}
			if ($display == 0) {
				next;
			}

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

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

			if ($i <= $js_output_title) {
				push(@js_logs, "document.write('$date ... $subj<br>');\n");
			}
			if ($js_title_field_mode == 1 && $i{"$field"} <= $js_output_title) {
				$js_logs{"$field"} .= "document.write('$date ... $subj<br>');\n";
			}
		}

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

		if ($js_title_field_mode == 1) {
			foreach (keys %js_title_field_file) {
				if ($_ eq '') {
					next;
				}
				open(FILE, ">$_") || &error("$js_title_field_file{$_}用JSファイルに書き込めません。");
				print FILE $js_logs{$js_title_field_file{$_}};
				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 create_plain {
	local($_) = @_;
	$_ =~ s/<br>/\n/g;
	$_ =~ s/"/&quot;/g;
	return $_;
}

### オートリンク
sub auto_link {
	local($_) = @_;
	$_ = "\n$_\n";
	$_ =~ s/([^\"(&quot;)(&gt;)])(https?:\/\/[\w\.\~\-\/\?\&\#\+\=\:\;\@\%]+)([^\"(&quot;)(&lt;)])/$1<a href="$2">$2<\/a>$3/gi;
	$_ =~ s/([^\"(&quot;)(&gt;)])([\w\-]+\@[\w\-\.]+)([^\"(&quot;)(&lt;)])/$1<a href="mailto:$2">$2<\/a>$3/gi;
	$_ =~ s/^\n//;
	$_ =~ s/\n$//;
	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 =~ /([^\/\\]*)\.([^.\/\\]*)$/;
	local $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);
}

### HTMLページ出力
sub output_htmlfile {
	$html_flag = 1;
	$stdout = *STDOUT;
	local %tmp = %in;
	%in = ();

	open(HTML, ">$html_file") || &error("HTMLファイルが作成できません。");
	*STDOUT = *HTML;
	&html();
	close(HTML);

	*STDOUT = $stdout;
	%in = %tmp;
	$html_flag = 0;

	return;
}

### ファイルロック
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("ファイルがロックされています。時間をおいてもう一度投稿してください。");
	}
	return;
}

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

### メール通知
sub sendmail {
	local $flag = 0;
	foreach (@admin) {
		if ($in{'name'} eq $_) {
			$flag = 1;
			last;
		}
	}

	if ($flag == 1) {
		return;
	}

	($sec, $min, $hour, $day, $mon, $year, $week) = localtime(time());
	local $date = sprintf("%04d/%02d/%02d(%s)-%02d:%02d", $year + 1900, $mon + 1, $day, $week[$week], $hour, $min);

	foreach ($in{'name'}, $in{'subj'}, $in{'text'}) {
		$_ =~ s/<br>/\n/g;
		$_ =~ s/&amp;/&/g;
		$_ =~ s/&lt;/</g;
		$_ =~ s/&gt;/>/g;
		$_ =~ s/&quot;/"/g;
	}

	local $mail_body = <<"_MESSAGE_";
以下のコメントが投稿されました。

投稿日時：$date

名　　前：$in{'name'}
Ｅメール：$in{'mail'}
ＵＲＬ  ：$in{'url'}
題　　名：$in{'subj'}
本　　文：

$in{'text'}

ホスト名：$host
_MESSAGE_

	if ($in{'mail'} eq '') {
		$in{'mail'} = $mailto[0];
	}

	local $mail_name    = &mail64encode($in{'name'});
	local $mail_subject = &mail64encode($in{'subj'});

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

		open(MAIL,"| $sendmail -t") || &error("メールが送信できません。");
		print MAIL "X-Mailer: Web Diary Professional ver $ver\n";
		print MAIL "To: $mailto\n";
		print MAIL "From: \"$mail_name\" <$in{'mail'}>\n";
		print MAIL "Subject: $mail_subject\n";
		print MAIL "Content-Transfer-Encoding: 7bit\n";
		print MAIL "Content-Type: text/plain; charset=iso-2022-jp\n\n";
		foreach (split(/\n/, $mail_body)) {
			&jcode'convert(*_, 'jis' ,'sjis');
			print MAIL "$_\n";
		}
		close(MAIL);
	}

	return;
}

### BASE64エンコード(参考：とほほのＷＷＷ入門 ... http://www.tohoho-web.com/)
sub mail64encode {
	local($_) = @_;

	&jcode'convert(*_, 'jis');

	$_ =~ s/\x1b\x28\x42/\x1b\x28\x4a/g;
	$_ = &base64encode($_);

	return("=?iso-2022-jp?B?$_?=");
}
sub base64encode {
	local($_) = @_;

	local $base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	local($xx, $yy, $zz, $i);

	$xx = unpack("B*", $_);
	for ($i = 0; $yy = substr($xx, $i, 6); $i += 6) {
		$zz .= substr($base, ord(pack("B*", "00" . $yy)), 1);

		if (length($yy) == 2) {
			$zz .= "==";
		} elsif (length($yy) == 4) {
			$zz .= "=";
		}
	}

	return($zz);
}

### 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 = ();
	local %cookie      = ();
	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);
}
