#!/usr/bin/perl

#┌─────────────────────────────────
#│ JOYFUL NOTE : joyful.cgi - 2021/07/24
#│ copyright (c) KentWeb, 1997-2019
#│ http://www.kent-web.com/
#└─────────────────────────────────
# REBUILD(改造)
#┌─────────────────────────────────
#│  Smart Board Type-G v2.65 : smart.cgi - 2025/06/04
#│  Edited by TRANSFORM Since 2012
#│  https://www.transformsite.com/
#└─────────────────────────────────

# モジュール宣言
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Encode;
use lib "./lib";
use CGI::Minimal;
use Calendar::Japanese::Holiday;

# 設定ファイル認識
require "./init.cgi";
my %cf = set_init();

require "./changeinit.cgi";
my %cfd = change_init();

# データ受理
CGI::Minimal::max_read_size($cf{maxdata});
my $cgi = CGI::Minimal->new;
error('容量オーバー') if ($cgi->truncated);
my %in = parse_form($cgi);

# 掲示板直リンク禁止
if ($cf{refstep}) {
	if (&RefererCheck) {
		print "Content-type: text/html\n\n";
		print "Location: $cf{bbs_cgi}\n\n";
	} else {
		print "Location: $cf{redirect}\n\n";
	}
	exit;

	sub RefererCheck {
		my $referer = $ENV{'HTTP_REFERER'};

		unless ($referer) {return;}
	return 1;
	}
}

# 認証モード
my %au = authent() if ($cf{authkey});

# 処理分岐
if ($in{mode} eq 'find') { find_data(); }
if ($in{mode} eq 'form') { form_page(); }
if ($in{mode} eq 'note') { note_page(); }
if ($in{mode} eq 'pwd') { pwd_form(); }
if ($in{mode} eq 'past' && $cf{pastkey}) { past_data(); }
if ($in{mode} eq 'album') { album_img(); }
if ($in{mode} eq 'headline') { flag_page(); }
if ($in{mode} eq 'expound') { expound_page(); }
if ($in{mode} eq 'icon') { icon_page(); }
if ($in{mode} eq 'diary_edp') { diary_editpass(); }
if ($in{mode} eq 'weather') { weather_page(); }
if ($in{mode} eq 'switchover') { switchover(); }
if ($in{mode} eq 'diary_adm') { diary_admin(); }
if ($in{mode} eq 'rescom') { rescom_form(); }
if ($in{mode} eq 'compwd') { com_pwdform(); }
if ($in{mode} eq 'diary_topic') { diary_topic(); }
if ($in{mode} eq 'archv') { archv_log(); }
if ($in{mode} eq 'calen') { calen_log(); }
if ($in{mode} eq 'mon') { moncal_log(); }
if ($in{mode} eq 'com_list') { com_list(); }
if ($in{mode} eq 'source_article') { article_data(); }
if ($in{mode} eq 'lock') { lock_form(); }

if ($cfd{diary_mode} == 2) { diary_page(); }

bbs_list();

#-----------------------------------------------------------
#  記事表示部
#-----------------------------------------------------------
sub bbs_list {
	# 返信フォーム
	$in{res}  =~ s/\D//g;
	$in{read} =~ s/\D//g;
	res_form($in{res}) if ($in{res} || $in{read});

	# ページ数定義
	my $pg = $in{pg} || 0;

	# 記事展開
	my ($i,@log,%res,%nam,%eml,%sub,%dat,%com,%url,%col,%e1,%imw1,%imh1,%chk,%myid,%color2,%tube,%time,%e2,%imw2,%imh2,%e3,%imw3,%imh3,%e4,%imw4,%imh4,%e5,%imw5,%imh5,%e6,%imw6,%imh6,%e7,%imw7,%imh7,%e8,%imw8,%imh8,%e9,%imw9,%imh9,%e10,%imw10,%imh10,%e11,%imw11,%imh11,%tubeid,%ico,%wico,%imgt1,%imgt2,%imgt3,%imgt4,%imgt5,%imgt6,%imgt7,%imgt8,%imgt9,%imgt10,%imgt11,%lock);
	open(IN,"$cf{logfile}") or error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11,$lock) = split(/<>/);

		++$i if (!$reno);
		next if ($i < $pg + 1);
		next if ($i > $pg + $cf{max_thread});

		# 親記事
		if (!$reno) {
			push(@log,$no);
		# レス記事
		} else {
			$res{$reno} .= "$no,";
		}
		# リンク
		$eml &&= qq|<a href="mailto:$eml"><img src="$cf{ico_mail}" class="icon"></a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>|;
		$com = auto_link($com) if ($cf{auto_link});

		# ハッシュ化
		$nam{$no} = $name;
		$sub{$no} = $sub;
		$dat{$no} = $date;
		$time{$no} = $time;
		$com{$no} = $com;
		$col{$no} = $col;
		$url{$no} = $url;
		$eml{$no} = $eml;
		$myid{$no} = $myid;
		$color2{$no} = $color2;
		$tube{$no} = $tube;
		$ico{$no} = $ico;
		$wico{$no} = $wico;
		$tubeid{$no} = $tubeid;
		$imgt1{$no} = $imgtxt1;
		$imgt2{$no} = $imgtxt2;
		$imgt3{$no} = $imgtxt3;
		$imgt4{$no} = $imgtxt4;
		$imgt5{$no} = $imgtxt5;
		$imgt6{$no} = $imgtxt6;
		$imgt7{$no} = $imgtxt7;
		$imgt8{$no} = $imgtxt8;
		$imgt9{$no} = $imgtxt9;
		$imgt10{$no} = $imgtxt10;
		$imgt11{$no} = $imgtxt11;
		$lock{$no} = $lock;

	my %e = (1 => $e1, 2 => $e2, 3 => $e3, 4 => $e4, 5 => $e5, 6 => $e6, 7 => $e7, 8 => $e8, 9 => $e9, 10 => $e10, 11 => $e11);
	foreach my $i (1 .. 11) {
		if ($e{$i}) {
			if ($cf{img_check} && $chk eq '0') {
				($e{$i}{$no}) = "hide";
			} else {
				$e1{$no} = $e1;
				$imw1{$no} = $w1;
				$imh1{$no} = $h1;
				$e2{$no} = $e2;
				$imw2{$no} = $w2;
				$imh2{$no} = $h2;
				$e3{$no} = $e3;
				$imw3{$no} = $w3;
				$imh3{$no} = $h3;
				$e4{$no} = $e4;
				$imw4{$no} = $w4;
				$imh4{$no} = $h4;
				$e5{$no} = $e5;
				$imw5{$no} = $w5;
				$imh5{$no} = $h5;
				$e6{$no} = $e6;
				$imw6{$no} = $w6;
				$imh6{$no} = $h6;
				$e7{$no} = $e7;
				$imw7{$no} = $w7;
				$imh7{$no} = $h7;
				$e8{$no} = $e8;
				$imw8{$no} = $w8;
				$imh8{$no} = $h8;
				$e9{$no} = $e9;
				$imw9{$no} = $w9;
				$imh9{$no} = $h9;
				$e10{$no} = $e10;
				$imw10{$no} = $w10;
				$imh10{$no} = $h10;
				$e11{$no} = $e11;
				$imw11{$no} = $w11;
				$imh11{$no} = $h11;
				}
			}
		}
	}
	close(IN);

	# 繰越ボタン作成
	my $page_btn = make_pgbtn($i,$pg,'',$cf{max_thread});

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]">■</span>\n|;
	}

	# 旗色プルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[4] eq "") { $cook[4] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[4] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[5] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[5] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[5];
        	}

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# テンプレート読込
	open(IN,"$cf{tmpldir}/bbs.html") or error("open err: bbs.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!new_mark!/$cf{head_new_mark}/g;
	$tmpl =~ s/!new_time!/$cf{new_time}/g;
	$tmpl =~ s/<!-- op_icon -->/$op_icon/g;
	$tmpl =~ s/!icon_reg!/$cf{icoreg_cgi}/g;
	$tmpl =~ s|<!-- select_icon -->|<div id="Selected"><div class="select"><img id="imgchange" src="$cf{faceurl}/$selecticon" width="80px" height="80px" alt=""></div></div>|g;

	if ($cf{icon_mode} == 0) {
		$tmpl =~ s|<!-- pop_icon -->.+?<!-- /pop_icon -->||s;
	}

	if ($cfd{diary_mode} == 1) {
		$tmpl =~ s|<!-- diary -->.+?<!-- /diary -->||s;
		$tmpl =~ s|<!-- diary_memo -->|<div class="silent">サイレント・モード : 記事閲覧のみ(投稿,編集,返信不可)</div>|s;
	}

	if ($cfd{diary_mode} == 0) {
		$tmpl =~ s|<!-- joyful -->.+?<!-- /joyful -->||s;
	}

	if ($cfd{diary_mode} == 1) {
		$tmpl =~ s|<!-- silent -->.+?<!-- /silent -->||s;
		$tmpl =~ s|<!-- silent-menu -->.+?<!-- /silent-menu -->||s;
	}

	# 過去ログ
	$tmpl =~ s/<!-- past -->.+?<!-- past -->//s if ($cf{pastkey} == 0);

	open(IN,"$cf{tmpldir}/res.html") or error("open err: res.html");
	my $resloop = join('', <IN>);
	close(IN);

	$resloop =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;

	# テンプレート分割
	my ($head,$loop,$foot) = split(/<!-- loop -->/,$tmpl);

	if ($cfd{diary_mode} == 1) {
		$loop =~ s|<!-- diary -->.+?<!-- /diary -->||s;
		$resloop =~ s|<!-- diary -->.+?<!-- /diary -->||s;
	}

	# 認証クッキー
	if ($cf{authkey} && $in{mode} eq 'login') {
		&set_cookie('CGISESSID',$au{sid});
	}

	# ノンアイコン
	if ($cf{icon_mode} == 0) {
		$loop =~ s/!icon!//g;
		$resloop =~ s/!icon!//g;
	}

	# 画像認証作成
	my ($str_plain,$str_crypt);
	if ($cf{use_captcha} > 0) {
		require $cf{captcha_pl};
		($str_plain,$str_crypt) = cap::make($cf{captcha_key},$cf{cap_len});
	} else {
		$head =~ s/<!-- captcha -->.+<!-- captcha -->//s;
	}

	# 文字置換
	for ($head,$foot) {
		s/!str_crypt!/$str_crypt/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s/!page_btn!/$page_btn/g;
		s/!name!/$cook[0]/;
		s/!email!/$cook[1]/;
		s/!url!/$cook[2]/;
		s/!color!/$color/g;
		s/<!-- op_color2 -->/$color_b/g;
		s/!sub!//g;
		s/!reno!//g;
		s/!counter!/$counter/g;

		# 認証モード
		if ($cf{authkey}) {
			s/!login-name!/$au{name}/g;
		} else {
			s/<!-- auth_begin -->.+?<!-- auth_end -->//sg;
		}
	}

	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# 記事表示
	foreach (@log) {
		# レス
		my ($res,$myid);
		foreach my $r ( split(/,/, $res{$_}) ) {
			$com{$r} = qq|<span style="color:$col[$col{$r}]">$com{$r}</span>|;
			$myid{$r} = qq|( <span style="color:#A688BD"><b>$myid{$r}</b></span> )|;

			# レス添付
			if (defined($e1{$r})) {
				$com{$r} = att_file($r,$com{$r},$e1{$r},$imw1{$r},$imh1{$r},$nam{$r},$sub{$r},$imgt1{$r});
			}
			my (%clip,$clip);
				$clip{$r} = attach($r,$e2{$r},$imw2{$r},$imh2{$r},$e3{$r},$imw3{$r},$imh3{$r},$e4{$r},$imw4{$r},$imh4{$r},$e5{$r},$imw5{$r},$imh5{$r},$e6{$r},$imw6{$r},$imh6{$r},$e7{$r},$imw7{$r},$imh7{$r},$e8{$r},$imw8{$r},$imh8{$r},$e9{$r},$imw9{$r},$imh9{$r},$e10{$r},$imw10{$r},$imh10{$r},$e11{$r},$imw11{$r},$imh11{$r},$nam{$r},$sub{$r},$imgt2{$r},$imgt3{$r},$imgt4{$r},$imgt5{$r},$imgt6{$r},$imgt7{$r},$imgt8{$r},$imgt9{$r},$imgt10{$r},$imgt11{$r});
				$clip{$r} &&= "<p>$clip{$r}</p>";

			# タグ復元
			$tube{$r} =~ s/&lt;/</g;
			$tube{$r} =~ s/&gt;/>/g;
			$tube{$r} =~ s/&quot;/"/g;
			$tube{$r} =~ s/&amp;/&/g;

			# レス(YouTube)
			if ($cf{tube}) {
				$tubeid{$r} =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
			}

			$com{$r} =~ s/&amp;/&/g;

		# newマーク
		if (time - $cf{new_time}*3600 < $time{$r}) {
			$sub{$r} .= " <div id=\"newico-res\">$cf{new_mark}</div> ";
		}

			my $tmp = $resloop;
			$tmp =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=pwd&no=$r&bbs=0"><img src="$cf{ico_edit}" title="記事編集" alt="記事編集" class="icon"></a>|g;
			$tmp =~ s/!sub!/$sub{$r}/g;
			$tmp =~ s/!name!/$nam{$r}/g;
			$tmp =~ s/!url!/$url{$r}/g;
			$tmp =~ s/!email!/$eml{$r}/g;
			$tmp =~ s/!date!/$dat{$r}/g;
			$tmp =~ s/!num!/$r/g;
			$tmp =~ s/!comment!/$com{$r}/g;
			$tmp =~ s|●|<font face="MS PGothic">●</font>|g;
			$tmp =~ s/!color2!/$color2{$r}/g;
			$tmp =~ s/<!-- restube_id -->/$tubeid{$r}/ if ($tubeid{$r});
			$tmp =~ s|<!-- res_youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube{$r}</div></div>| if ($tube{$r});
			$tmp =~ s/<!-- res_clip -->/$clip{$r}/g;

		if (!$ico{$r}) {
			$tmp =~ s|!icon!||g;
		} else {
			$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico{$r}" id="faceimage" alt="" />|g;
		}

		if ($lock{$r} eq '1') {
			$tmp =~ s|<!-- lock_edit -->.+?<!-- /lock_edit -->||s;
		}

			# 認証モード
			if ($cf{authkey}) {
				$tmp =~ s/<!-- myid -->/$myid{$r}/g;
			} else {
				$tmp =~ s/<!-- id_begin -->.+?<!-- id_end -->//s;
			}

			$res .= $tmp;
		}

		# 添付
		$com{$_} = qq|<span style="color:$col[$col{$_}]">$com{$_}</span>|;
		if (defined($e1{$_})) {
			$com{$_} = att_file($_,$com{$_},$e1{$_},$imw1{$_},$imh1{$_},$nam{$_},$sub{$_},$imgt1{$_});
		}

		my (%clip,$clip);
			$clip{$_} = attach($_,$e2{$_},$imw2{$_},$imh2{$_},$e3{$_},$imw3{$_},$imh3{$_},$e4{$_},$imw4{$_},$imh4{$_},$e5{$_},$imw5{$_},$imh5{$_},$e6{$_},$imw6{$_},$imh6{$_},$e7{$_},$imw7{$_},$imh7{$_},$e8{$_},$imw8{$_},$imh8{$_},$e9{$_},$imw9{$_},$imh9{$_},$e10{$_},$imw10{$_},$imh10{$_},$e11{$_},$imw11{$_},$imh11{$_},$nam{$_},$sub{$_},$imgt2{$_},$imgt3{$_},$imgt4{$_},$imgt5{$_},$imgt6{$_},$imgt7{$_},$imgt8{$_},$imgt9{$_},$imgt10{$_},$imgt11{$_});
			$clip{$_} &&= "<p>$clip{$_}</p>";

		# タグ復元
		$tube{$_} =~ s/&lt;/</g;
		$tube{$_} =~ s/&gt;/>/g;
		$tube{$_} =~ s/&quot;/"/g;
		$tube{$_} =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid{$_} =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

		$com{$_} =~ s/&amp;/&/g;

		# newマーク
		if (time - $cf{new_time}*3600 < $time{$_}) {
			$sub{$_} .= " <div id=\"newicon\">$cf{new_mark}</div> ";
		}

		my $tmp = $loop;
		$tmp =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=pwd&no=$_"><img src="$cf{ico_edit}" title="記事編集" alt="記事編集" class="icon"></a>|g;
		$tmp =~ s|!res_form!|<a href="#" onclick="window.open('!bbs_cgi!?res=$_','_self')"><img src="$cf{ico_reply}" class="icon" title="返信" /></a>|g;
		$tmp =~ s/!sub!/$sub{$_}/g;
		$tmp =~ s/!name!/$nam{$_}/g;
		$tmp =~ s/!url!/$url{$_}/g;
		$tmp =~ s/!email!/$eml{$_}/g;
		$tmp =~ s/!date!/$dat{$_}/g;
		$tmp =~ s/!num!/$_/g;
		$tmp =~ s/!comment!/$com{$_}/g;
		$tmp =~ s/!bbs_cgi!/$cf{bbs_cgi}/g;
		$tmp =~ s/!color2!/$color2{$_}/g;
		$tmp =~ s/!reply!/$cf{ico_reply}/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid{$_}/ if ($tubeid{$_});
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube{$_}</div></div>| if ($tube{$_});
		$tmp =~ s|<!-- res -->|<div class="ta-r">$res</div>|g if ($res);
		$tmp =~ s/<!-- clip -->/$clip{$_}/g;
		$tmp =~ s|●|<font face="MS PGothic">●</font>|g;
		$tmp =~ s|!ico_lock!|<a href="$cf{bbs_cgi}?mode=lock&no=$_"><img src="$cf{cmnurl}/password.png" title="記事ロック" alt="記事ロック" class="icon" /></a>|g;

	if (!$ico{$_}) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico{$_}" id="faceimage" alt="" />|g;
	}

		if ($lock{$_} eq '1') {
			$tmp =~ s|<!-- lock_edit -->.+?<!-- /lock_edit -->||s;
			$tmp =~ s|<!-- lock_form -->.+?<!-- /lock_form -->||s;
		} elsif ($lock{$_} == 0 || $lock{$_} == '') {
			$tmp =~ s|<!-- lock -->.+?<!-- /lock -->||s;
		}

		# 認証モード
		if ($cf{authkey}) {
			$tmp =~ s/!myid!/ID:$myid{$_}/g;
		} else {
			$tmp =~ s/<!-- id_begin -->.+?<!-- id_end -->//s;
		}

		print $tmp;
	}

	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  記事ロックフォーム
#-----------------------------------------------------------
sub lock_form {

	$in{no} =~ s/\D//g;

	# 記事
	my ($name,$pwd,$lock);
	open(IN,"$cf{logfile}") or &error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11,$lock) = split(/<>/);

	if ($in{no} == $no) {
		$name = $name;
		$pwd = $pw;
		$lock = $lock;
		last;
		}
	}
	close(IN);

	# パスワード未設定
	error("この記事はパスワード未設定のため編集できません") if ($pwd eq '');

	my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11,$lock) = split(/<>/);
		$name = qq|<a href="mailto:$eml">$name</a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>|;
		$com = auto_link($com) if ($cf{autolink});

		#if (length($com) > 135) { $com = substr($com,0,140); $com .= '.....(略)'; }

	my @col = split(/\s+/, $cf{colors});

	open(IN,"$cf{tmpldir}/lock_form.html") or &error("open err: lock_form.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置換
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!ico:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!num!/$no/g;
	$tmpl =~ s/!sub!/$sub/g;
	$tmpl =~ s/!name!/$name/g;
	$tmpl =~ s/!color2!/$color2/g;

	if (!$reno) {
		$tmpl =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
	} else {
		$tmpl =~ s|!comment!|<span style="color:$col">$com</span>|g;
	}

	if ($lock == 1) {
		$tmpl =~ s|!lock_com!||g;
		$tmpl =~ s|!open_com!|<span style="color:#00a968">以下の投稿記事のロックを解除することが出来ます</span>|g;
	} 
	if ($lock == 0)  {
		$tmpl =~ s|!lock_com!|<span style="color:#dc143c">以下の投稿記事をロックして閉じることが出来ます</span>|g;
		$tmpl =~ s|!open_com!||g;
	}


	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();

	exit;
}

#-----------------------------------------------------------
#  返信フォーム
#-----------------------------------------------------------
sub res_form {
	my $resnum = $in{res} ? $in{res} : $in{read};

	my ($flg,@log,%res,$resub,@res,%nam,%eml,%sub,%dat,%com,%url,%eml,%e1,%imw1,%imh1,%chk,%col,%color2,%imw,%imh,%myid,%tube,%time,%e2,%imw2,%imh2,%e3,%imw3,%imh3,%e4,%imw4,%imh4,%e5,%imw5,%imh5,%e6,%imw6,%imh6,%e7,%imw7,%imh7,%e8,%imw8,%imh8,%e9,%imw9,%imh9,%e10,%imw10,%imh10,%e11,%imw11,%imh11,%tubeid,%ico,%wico,%imgt1,%imgt2,%imgt3,%imgt4,%imgt5,%imgt6,%imgt7,%imgt8,%imgt9,%imgt10,%imgt11);
	open(IN,"$cf{logfile}") or error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		if ($resnum == $no) {
			$flg = 1;

			# タイトル名
			if ($sub =~ /^Re:/) {
				$resub = $sub;
			} else {
				$resub = "Re: $sub";
			}

		} elsif ($resnum == $reno) {
			push(@res,$no);
		} else {
			next;
		}
		# リンク
		$eml &&= qq|<a href="mailto:$eml"><img src="$cf{ico_mail}" class="icon"></a>| if ($eml);
		#$name = qq|<a href="mailto:$eml">$name</a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>|;
		$com = auto_link($com) if ($cf{auto_link});

		$nam{$no} = $name;
		$sub{$no} = $sub;
		$dat{$no} = $date;
		$time{$no} = $time;
		$com{$no} = $com;
		$col{$no} = $col;
		$url{$no} = $url;
		$eml{$no} = $eml;
		$chk{$no} = $chk;
		$myid{$no} = $myid;
		$color2{$no} = $color2;
		$ico{$no} = $ico;
		$wico{$no} = $wico;
		$tube{$no} = $tube;
		$tubeid{$no} = $tubeid;
		$imgt1{$no} = $imgtxt1;
		$imgt2{$no} = $imgtxt2;
		$imgt3{$no} = $imgtxt3;
		$imgt4{$no} = $imgtxt4;
		$imgt5{$no} = $imgtxt5;
		$imgt6{$no} = $imgtxt6;
		$imgt7{$no} = $imgtxt7;
		$imgt8{$no} = $imgtxt8;
		$imgt9{$no} = $imgtxt9;
		$imgt10{$no} = $imgtxt10;
		$imgt11{$no} = $imgtxt11;

	my %e = (1 => $e1, 2 => $e2, 3 => $e3, 4 => $e4, 5 => $e5, 6 => $e6, 7 => $e7, 8 => $e8, 9 => $e9, 10 => $e10, 11 => $e11);
	foreach my $i (1 .. 11) {
		if ($e{$i}) {
			if ($cf{img_check} && $chk eq '0') {
				($e{$i}{$no}) = "hide";
			} else {
				$e1{$no} = $e1;
				$imw1{$no} = $w1;
				$imh1{$no} = $h1;
				$e2{$no} = $e2;
				$imw2{$no} = $w2;
				$imh2{$no} = $h2;
				$e3{$no} = $e3;
				$imw3{$no} = $w3;
				$imh3{$no} = $h3;
				$e4{$no} = $e4;
				$imw4{$no} = $w4;
				$imh4{$no} = $h4;
				$e5{$no} = $e5;
				$imw5{$no} = $w5;
				$imh5{$no} = $h5;
				$e6{$no} = $e6;
				$imw6{$no} = $w6;
				$imh6{$no} = $h6;
				$e7{$no} = $e7;
				$imw7{$no} = $w7;
				$imh7{$no} = $h7;
				$e8{$no} = $e8;
				$imw8{$no} = $w8;
				$imh8{$no} = $h8;
				$e9{$no} = $e9;
				$imw9{$no} = $w9;
				$imh9{$no} = $h9;
				$e10{$no} = $e10;
				$imw10{$no} = $w10;
				$imh10{$no} = $h10;
				$e11{$no} = $e11;
				$imw11{$no} = $w11;
				$imh11{$no} = $h11;
				}
			}
		}
	}
	close(IN);

	if (!$flg) { error("不正な返信要求です"); }

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]">■</span>\n|;
	}

	# 旗色プルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[4] eq "") { $cook[4] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[4] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[5] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[5] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[5];
        	}

	# テンプレート読込
	open(IN,"$cf{tmpldir}/read.html") or error("open err: read.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!pg!/$in{pg} eq '' ? 0 : $in{pg}/eg;
	$tmpl =~ s/!new_mark!/$cf{head_new_mark}/g;
	$tmpl =~ s/!new_time!/$cf{new_time}/g;
	$tmpl =~ s|<!-- select_icon -->|<div id="Selected"><div class="select"><img id="imgchange" src="$cf{faceurl}/$selecticon" width="80px" height="80px" alt=""></div></div>|g;
	$tmpl =~ s/<!-- op_icon -->/$op_icon/g;
	$tmpl =~ s/!icon_reg!/$cf{icoreg_cgi}/g;

	# 追加添付ファイル数
	my ($op_attach,$attach,$extxt,$att_msg,$gfc,$refno);

		# データ読み込み
		open(DAT,"$cf{addcnt}");
		my $data = join('',<DAT>);
		close(DAT);
			my ($cnt,$date) = split(/<>/, $data);

	$cnt = $cnt ne '' ? $cnt : $cf{attach_defo};
	foreach (@{$cf{addattach}}) {

			$refno = $_;
			if ($refno <= 9){ $refno = "　　" . $refno; }
			if ($refno eq 10) { $refno = "　" . $refno; }
			$refno =~ s/0/０/g;
			$refno =~ s/1/１/g;
			$refno =~ s/2/２/g;
			$refno =~ s/3/３/g;
			$refno =~ s/4/４/g;
			$refno =~ s/5/５/g;
			$refno =~ s/6/６/g;
			$refno =~ s/7/７/g;
			$refno =~ s/8/８/g;
			$refno =~ s/9/９/g;

		if ($cnt == $_) {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		} else {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		}
	}
		$tmpl =~ s/<!-- add_attach -->/$op_attach/g;

	if ($cnt > 0) {
		foreach $gfc (1 .. $cnt) {
			my $refn = $gfc + 1;
			$attach .= qq|<div class="uploadButton"><span>+ Add files $gfc</span><input type="file" name="upfile$refn" id="upload$refn" onchange="uv0$refn.style.display='inline-block'; uv0$refn.value = this.value;" /><input type="text" id="uv0$refn" class="uploadValue" disabled /></div>\n|;
			$extxt .= qq|<input type="text" name="imgtext$refn" class="reg-imgtxt_add" placeholder="追加添付$gfcタイトル" value="">\n|;
		}
	}
		if ($cnt == 0) { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームを追加出来ます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		} elsif ($cnt == 10) { 
			$att_msg .= qq|<small><span style="color:#d83473">参照フォームは10個が限度です</span> <img src="./cmn/arrow-gfno01.png" alt="追加" class="icon2"></small>\n|;
		} else { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームの数を変更できます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		}

		$cnt =~ s/0/０/g;
		$cnt =~ s/1/１/g;
		$cnt =~ s/2/２/g;
		$cnt =~ s/3/３/g;
		$cnt =~ s/4/４/g;
		$cnt =~ s/5/５/g;
		$cnt =~ s/6/６/g;
		$cnt =~ s/7/７/g;
		$cnt =~ s/8/８/g;
		$cnt =~ s/9/９/g;

		$tmpl =~ s/!ref_no!/$cnt/g;
		$tmpl =~ s/<!-- attach -->/$attach/g;
		$tmpl =~ s/<!-- extxt -->/$extxt/g;
		$tmpl =~ s/!att_msg!/$att_msg/g;
	
	if ($cf{icon_mode} == 0) {
		$tmpl =~ s|<!-- pop_icon -->.+?<!-- /pop_icon -->||s;
	}

	if ($cfd{diary_mode} == 1) {
		$tmpl =~ s|<!-- diary -->.+?<!-- /diary -->||s;
	}

	if ($cfd{diary_mode} == 0) {
		$tmpl =~ s|<!-- joyful -->.+?<!-- /joyful -->||s;
	}

	# レス用テンプレート
	open(IN,"$cf{tmpldir}/res.html") or error("open err: res.html");
	my $resloop = join('', <IN>);
	close(IN);

	$resloop =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;

	# 過去ログ
	$tmpl =~ s/<!-- past -->.+<!-- past -->//s if ($cf{pastkey} == 0);

	# テンプレート分割
	my ($head,$loop,$foot) = split(/<!-- loop -->/,$tmpl);

	if ($cfd{diary_mode} == 1) {
		$loop =~ s|<!-- diary -->.+?<!-- /diary -->||s;
	}

	# ノンアイコン
	if ($cf{icon_mode} == 0) {
		$loop =~ s/!icon!//g;
		$resloop =~ s/!icon!//g;
	}

	# 画像認証作成
	my ($str_plain,$str_crypt);
	if ($cf{use_captcha} > 0) {
		require $cf{captcha_pl};
		($str_plain,$str_crypt) = cap::make($cf{captcha_key},$cf{cap_len});
	} else {
		$head =~ s/<!-- captcha -->.+<!-- captcha -->//s;
	}

	# 文字置換
	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s/!name!/$cook[0]/;
		s/!email!/$cook[1]/;
		s/!url!/$cook[2]/;
		s/!str_crypt!/$str_crypt/g;
		s/!color!/$color/g;
		s/<!-- op_color2 -->/$color_b/g;
		s/!sub!/$resub/g;
		s/!reno!/$resnum/g;
		s/!counter!//g;
		s/!page_btn!//g;
	}

	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# レス
	my ($res,$eml,$myid);
	foreach my $r (@res) {
		# 添付
		$com{$r} = qq|<span style="color:$col[$col{$r}]">$com{$r}</span>|;
		$myid{$r} = qq|( <span style="color:#A688BD"><b>$myid{$r}</b></span> )|;

		if (defined($e1{$r})) {
			$com{$r} = att_file($r,$com{$r},$e1{$r},$imw1{$r},$imh1{$r},$nam{$r},$sub{$r},$imgt1{$r});
		}
		my (%clip,$clip);
			$clip{$r} = attach($r,$e2{$r},$imw2{$r},$imh2{$r},$e3{$r},$imw3{$r},$imh3{$r},$e4{$r},$imw4{$r},$imh4{$r},$e5{$r},$imw5{$r},$imh5{$r},$e6{$r},$imw6{$r},$imh6{$r},$e7{$r},$imw7{$r},$imh7{$r},$e8{$r},$imw8{$r},$imh8{$r},$e9{$r},$imw9{$r},$imh9{$r},$e10{$r},$imw10{$r},$imh10{$r},$e11{$r},$imw11{$r},$imh11{$r},$nam{$r},$sub{$r},$imgt2{$r},$imgt3{$r},$imgt4{$r},$imgt5{$r},$imgt6{$r},$imgt7{$r},$imgt8{$r},$imgt9{$r},$imgt10{$r},$imgt11{$r});
			$clip{$r} &&= "<p>$clip{$r}</p>";

			# タグ復元
			$tube{$r} =~ s/&lt;/</g;
			$tube{$r} =~ s/&gt;/>/g;
			$tube{$r} =~ s/&quot;/"/g;
			$tube{$r} =~ s/&amp;/&/g;

			# レス(YouTube)
			if ($cf{tube}) {
				$tubeid{$r} =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
			}

		$com{$r} =~ s/&amp;/&/g;

		# newマーク
		if (time - $cf{new_time}*3600 < $time{$r}) {
			$sub{$r} .= " <div id=\"newico-res\">$cf{new_mark}</div> ";
		}

		# 文字置換
		my $tmp = $resloop;
		$tmp =~ s/!reply!/$cf{ico_reply}/g;
		$tmp =~ s/!sub!/$sub{$r}/g;
		$tmp =~ s/!name!/$nam{$r}/g;
		$tmp =~ s/!date!/$dat{$r}/g;
		$tmp =~ s/!url!/$url{$r}/g;
		$tmp =~ s/!email!/$eml{$r}/g;
		$tmp =~ s/!num!/$r/g;
		$tmp =~ s/!color2!/$color2{$r}/g;
		$tmp =~ s|!comment!|$com{$r}|g;
		$tmp =~ s/<!-- res_clip -->/$clip{$r}/g;
		$tmp =~ s/<!-- restube_id -->/$tubeid{$r}/ if ($tubeid{$r});
		$tmp =~ s|<!-- res_youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube{$r}</div></div>| if ($tube{$r});
		$tmp =~ s|●|<font face="MS PGothic">●</font>|g;
		$tmp =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=pwd&no=$r"><img src="$cf{ico_edit}" title="記事編集" alt="記事編集" class="icon"></a>|g;

		if (!$ico{$r}) {
			$tmp =~ s|!icon!||g;
		} else {
			$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico{$r}" width="80px" height="80px" class="image" alt="" />|g;
		}

		# 認証モード
		if ($cf{authkey}) {
			$tmp =~ s/<!-- myid -->/$myid{$r}/g;
		} else {
			$tmp =~ s/<!-- id_begin -->.+?<!-- id_end -->//s;
		}

		$res .= $tmp;
	}

	# 添付
	$com{$resnum} = qq|<span style="color:$col[$col{$resnum}]">$com{$resnum}</span>|;
	if (defined($e1{$resnum})) {
		$com{$resnum} = att_file($resnum,$com{$resnum},$e1{$resnum},$imw1{$resnum},$imh1{$resnum},$nam{$resnum},$sub{$resnum},$imgt1{$resnum});
	}
	my (%clip,$clip);
	$com{$resnum} = qq|<span style="color:$col[$col{$resnum}]">$com{$resnum}</span>|;
			$clip{$resnum} = attach($resnum,$e2{$resnum},$imw2{$resnum},$imh2{$resnum},$e3{$resnum},$imw3{$resnum},$imh3{$resnum},$e4{$resnum},$imw4{$resnum},$imh4{$resnum},$e5{$resnum},$imw5{$resnum},$imh5{$resnum},$e6{$resnum},$imw6{$resnum},$imh6{$resnum},$e7{$resnum},$imw7{$resnum},$imh7{$resnum},$e8{$resnum},$imw8{$resnum},$imh8{$resnum},$e9{$resnum},$imw9{$resnum},$imh9{$resnum},$e10{$resnum},$imw10{$resnum},$imh10{$resnum},$e11{$resnum},$imw11{$resnum},$imh11{$resnum},$nam{$resnum},$sub{$resnum},$imgt2{$resnum},$imgt3{$resnum},$imgt4{$resnum},$imgt5{$resnum},$imgt6{$resnum},$imgt7{$resnum},$imgt8{$resnum},$imgt9{$resnum},$imgt10{$resnum},$imgt11{$resnum});
			$clip{$resnum} &&= "<p>$clip{$resnum}</p>";

	# タグ復元
	$tube{$resnum} =~ s/&lt;/</g;
	$tube{$resnum} =~ s/&gt;/>/g;
	$tube{$resnum} =~ s/&quot;/"/g;
	$tube{$resnum} =~ s/&amp;/&/g;

	# 親(YouTube)
	if ($cf{tube}) {
		$tubeid{$resnum} =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
	}

	$com{$resnum} =~ s/&amp;/&/g;
		# newマーク
		if (time - $cf{new_time}*3600 < $time{$resnum}) {
			$sub{$resnum} .= " <div id=\"newicon\">$cf{new_mark}</div> ";
		}

	# 文字置換
	$loop =~ s/!reply!/$cf{ico_reply}/g;
	$loop =~ s/!sub!/$sub{$resnum}/g;
	$loop =~ s/!name!/$nam{$resnum}/g;
	$loop =~ s/!date!/$dat{$resnum}/g;
	$loop =~ s/!url!/$url{$resnum}/g;
	$loop =~ s/!email!/$eml{$resnum}/g;
	$loop =~ s/!num!/$resnum/g;
	$loop =~ s/!comment!/$com{$resnum}/g;
	$loop =~ s/!color2!/$color2{$resnum}/g;
	$loop =~ s/!bbs_cgi!/$cf{bbs_cgi}/g;
	$loop =~ s/<!-- tube_id -->/$tubeid{$resnum}/ if ($tubeid{$resnum});
	$loop =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube{$resnum}</div></div>| if ($tube{$resnum});
	$loop =~ s|●|<font face="MS PGothic">●</font>|g;
	$loop =~ s|<!-- res -->|<div class="ta-r">$res</div>|g if ($res);
	$loop =~ s/<!-- clip -->/$clip{$resnum}/g;
	$loop =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=pwd&no=$resnum"><img src="$cf{ico_edit}" title="記事編集" alt="記事編集" class="icon"></a>|g;

	if (!$ico{$resnum}) {
		$loop =~ s|!icon!||g;
	} else {
		$loop =~ s|!icon!|<img src="$cf{faceurl}/$ico{$resnum}" width="80px" height="80px" class="image" alt="" />|g;
	}

	# 認証モード
	if ($cf{authkey}) {
		$loop =~ s/!myid!/ID：$myid{$resnum}/g;
	} else {
		$loop =~ s/<!-- id_begin -->.+?<!-- id_end -->//s;
	}

	print $loop;

	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  パスワードフォーム
#-----------------------------------------------------------
sub pwd_form {

	$in{no} =~ s/\D//g;

	# 記事
	my ($name,$pwd);
	open(IN,"$cf{logfile}") or &error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

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

	# パスワード未設定
	error("この記事はパスワード未設定のため編集できません") if ($pwd eq '');

		my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		$name = qq|<a href="mailto:$eml">$name</a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>|;
		$com = &autolink($com) if ($cf{autolink});
		$com =~ s/&amp;/&/g;
		$com =~ s|●|<font face="MS PGothic">●</font>|g;

		if (length($com) > 135) { $com = substr($com,0,140); $com .= '.....(略)'; }

		# 添付あり
		my ($clip01,$clip02,$clip03,$clip04,$clip05,$clip06,$clip07,$clip08,$clip09,$clip10,$clip11);
		if ($e1) {
			my $size = get_size("$cf{imgdir}/$no-1$e1");
			if ($cf{img_check} && $chk != 1) {
				$clip01 = qq|<div class="clip">[添付1]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip01 = qq|<div class="clip">[添付1]: <a href="$cf{imgurl}/$no-1$e1" target="_blank">$no-1$e1</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e2) {
			my $size = get_size("$cf{imgdir}/$no-2$e2");
			if ($cf{img_check} && $chk != 1) {
				$clip02 = qq|<div class="clip">[添付2]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip02 = qq|<div class="clip">[添付2]: <a href="$cf{imgurl}/$no-2$e2" target="_blank">$no-2$e2</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e3) {
			my $size = get_size("$cf{imgdir}/$no-3$e3");
			if ($cf{img_check} && $chk != 1) {
				$clip03 = qq|<div class="clip">[添付3]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip03 = qq|<div class="clip">[添付3]: <a href="$cf{imgurl}/$no-3$e3" target="_blank">$no-3$e3</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e4) {
			my $size = get_size("$cf{imgdir}/$no-4$e4");
			if ($cf{img_check} && $chk != 1) {
				$clip04 = qq|<div class="clip">[添付4]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip04 = qq|<div class="clip">[添付4]: <a href="$cf{imgurl}/$no-4$e4" target="_blank">$no-4$e4</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e5) {
			my $size = get_size("$cf{imgdir}/$no-5$e5");
			if ($cf{img_check} && $chk != 1) {
				$clip05 = qq|<div class="clip">[添付5]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip05 = qq|<div class="clip">[添付5]: <a href="$cf{imgurl}/$no-5$e5" target="_blank">$no-5$e5</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e6) {
			my $size = get_size("$cf{imgdir}/$no-6$e6");
			if ($cf{img_check} && $chk != 1) {
				$clip06 = qq|<div class="clip">[添付6]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip06 = qq|<div class="clip">[添付6]: <a href="$cf{imgurl}/$no-6$e6" target="_blank">$no-6$e6</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e7) {
			my $size = get_size("$cf{imgdir}/$no-7$e7");
			if ($cf{img_check} && $chk != 1) {
				$clip07 = qq|<div class="clip">[添付7]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip07 = qq|<div class="clip">[添付7]: <a href="$cf{imgurl}/$no-7$e7" target="_blank">$no-7$e7</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e8) {
			my $size = get_size("$cf{imgdir}/$no-8$e8");
			if ($cf{img_check} && $chk != 1) {
				$clip08 = qq|<div class="clip">[添付8]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip08 = qq|<div class="clip">[添付8]: <a href="$cf{imgurl}/$no-8$e8" target="_blank">$no-8$e8</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e9) {
			my $size = get_size("$cf{imgdir}/$no-9$e9");
			if ($cf{img_check} && $chk != 1) {
				$clip09 = qq|<div class="clip">[添付9]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip09 = qq|<div class="clip">[添付9]: <a href="$cf{imgurl}/$no-9$e9" target="_blank">$no-9$e9</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e10) {
			my $size = get_size("$cf{imgdir}/$no-10$e10");
			if ($cf{img_check} && $chk != 1) {
				$clip10 = qq|<div class="clip">[添付10]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip10 = qq|<div class="clip">[添付10]: <a href="$cf{imgurl}/$no-10$e10" target="_blank">$no-10$e10</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e11) {
			my $size = get_size("$cf{imgdir}/$no-11$e11");
			if ($cf{img_check} && $chk != 1) {
				$clip11 = qq|<div class="clip">[添付11]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip11 = qq|<div class="clip">[添付11]: <a href="$cf{imgurl}/$no-11$e11" target="_blank">$no-11$e11</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}

	my @col = split(/\s+/, $cf{colors});

	open(IN,"$cf{tmpldir}/pwd.html") or &error("open err: pwd.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# トピック画面用
	$in{bbs} ||= 0;
	my $read = $in{bbs} == 1 ? $in{no} : 0;

	# 文字置換
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!bbs!/$in{bbs}/g;
	$tmpl =~ s/!num!/$no/g;
	$tmpl =~ s/!sub!/$sub/g;
	$tmpl =~ s/!name!/$name/g;
	$tmpl =~ s/!date!/$date/g;
	$tmpl =~ s/!color2!/$color2/g;
	$tmpl =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
	$tmpl =~ s/<!-- clip01 -->/$clip01/ if($e1);
	$tmpl =~ s/<!-- clip02 -->/$clip02/ if($e2);
	$tmpl =~ s/<!-- clip03 -->/$clip03/ if($e3);
	$tmpl =~ s/<!-- clip04 -->/$clip04/ if($e4);
	$tmpl =~ s/<!-- clip05 -->/$clip05/ if($e5);
	$tmpl =~ s/<!-- clip06 -->/$clip06/ if($e6);
	$tmpl =~ s/<!-- clip07 -->/$clip07/ if($e7);
	$tmpl =~ s/<!-- clip08 -->/$clip08/ if($e8);
	$tmpl =~ s/<!-- clip09 -->/$clip09/ if($e9);
	$tmpl =~ s/<!-- clip10 -->/$clip10/ if($e10);
	$tmpl =~ s/<!-- clip11 -->/$clip11/ if($e11);
	$tmpl =~ s|!ico:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" class="icon2">|g;

	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();

	exit;
}

#-----------------------------------------------------------
#  投稿フォーム
#-----------------------------------------------------------
sub form_page {

	# クッキー取得
	my @cook = &get_cookie;
	$cook[2] ||= 'http://';

	# 色選択ボタン
	my @col = split(/\s+/, $cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]">■</span>\n|;
	}

	# 旗色プルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[4] eq "") { $cook[4] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[4] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[5] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[5] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[5];
        	}

	# 画像認証作成
	my ($str_plain,$str_crypt,$regikey,$JavaScript);
	if ($cf{use_captcha} > 0) {
		require $cf{captcha_pl};
		($str_plain, $str_crypt, $regikey, $JavaScript) = cap::make( $cf{captcha_key}, $cf{cap_len} );
		$JavaScript=&mkJavaScript($regikey,'myFORM') if($cf{cap_len} == 3);
	}

	open(IN,"$cf{tmpldir}/form.html") or &error("open err: form.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置換
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!str_crypt!/$str_crypt/g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!homepage!/$cf{homepage}/g;
	$tmpl =~ s/!name!/$cook[0]/g;
	$tmpl =~ s/!email!/$cook[1]/g;
	$tmpl =~ s/!url!/$cook[2]/g;
	$tmpl =~ s/!sub!//g;
	$tmpl =~ s/!color!/$color/g;
	$tmpl =~ s/<!-- op_color2 -->/$color_b/g;
	$tmpl =~ s/!JavaScript!/$JavaScript/g;
	$tmpl =~ s/<!-- op_icon -->/$op_icon/g;
	$tmpl =~ s/!icon_reg!/$cf{icoreg_cgi}/g;
	$tmpl =~ s|<!-- select_icon -->|<div id="Selected"><div class="select"><img id="imgchange" src="$cf{faceurl}/$selecticon" width="80px" height="80px" alt=""></div></div>|g;

	# 追加添付ファイル数
	my ($op_attach,$attach,$extxt,$att_msg,$gfc,$refno);

		# データ読み込み
		open(DAT,"$cf{addcnt}");
		my $data = join('',<DAT>);
		close(DAT);
			my ($cnt,$date) = split(/<>/, $data);

	$cnt = $cnt ne '' ? $cnt : $cf{attach_defo};
	foreach (@{$cf{addattach}}) {

			$refno = $_;
			if ($refno <= 9){ $refno = "　　" . $refno; }
			if ($refno eq 10) { $refno = "　" . $refno; }
			$refno =~ s/0/０/g;
			$refno =~ s/1/１/g;
			$refno =~ s/2/２/g;
			$refno =~ s/3/３/g;
			$refno =~ s/4/４/g;
			$refno =~ s/5/５/g;
			$refno =~ s/6/６/g;
			$refno =~ s/7/７/g;
			$refno =~ s/8/８/g;
			$refno =~ s/9/９/g;

		if ($cnt == $_) {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		} else {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		}
	}
		$tmpl =~ s/<!-- add_attach -->/$op_attach/g;

	if ($cnt > 0) {
		foreach $gfc (1 .. $cnt) {
			my $refn = $gfc + 1;
			$attach .= qq|<div class="uploadButton"><span>+ Add files $gfc</span><input type="file" name="upfile$refn" id="upload$refn" onchange="uv0$refn.style.display='inline-block'; uv0$refn.value = this.value;" /><input type="text" id="uv0$refn" class="uploadValue" disabled /></div>\n|;
			$extxt .= qq|<input type="text" name="imgtext$refn" class="reg-imgtxt_add" placeholder="追加添付$gfcタイトル" value="">\n|;
		}
	}
		if ($cnt == 0) { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームを追加出来ます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		} elsif ($cnt == 10) { 
			$att_msg .= qq|<small><span style="color:#d83473">参照フォームは10個が限度です</span> <img src="./cmn/arrow-gfno01.png" alt="追加" class="icon2"></small>\n|;
		} else { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームの数を変更できます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		}

		$cnt =~ s/0/０/g;
		$cnt =~ s/1/１/g;
		$cnt =~ s/2/２/g;
		$cnt =~ s/3/３/g;
		$cnt =~ s/4/４/g;
		$cnt =~ s/5/５/g;
		$cnt =~ s/6/６/g;
		$cnt =~ s/7/７/g;
		$cnt =~ s/8/８/g;
		$cnt =~ s/9/９/g;

		$tmpl =~ s/!ref_no!/$cnt/g;
		$tmpl =~ s/<!-- attach -->/$attach/g;
		$tmpl =~ s/<!-- extxt -->/$extxt/g;
		$tmpl =~ s/!att_msg!/$att_msg/g;

	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();

	exit;
}

#-----------------------------------------------------------
#  モード切り替え
#-----------------------------------------------------------
sub switchover {
	passwd3(%in) if ($cf{mode_pass} ne '');

	$in{cnt} =~ s/\D//g;

	# 実行
	if ($in{job} eq "switch") {

	# 時間取得
	$ENV{TZ} = "JST-9";
	my ($mday,$mon,$year) = (localtime(time))[3..5];
	my $date = sprintf("%04d/%02d/%02d", $year+1900,$mon+1,$mday);

	my ($flg,@data);
	open(DAT,"+< $cf{changecnt}") or error("open err: $cf{changecnt}");
	my $data = <DAT>;
	while (<DAT>) {
		my ($count,$dat) = split(/<>/, $data);

		if ($in{cnt} == $count) {
			$flg = 1;
		}
		push(@data,$_);
	}

	# データ更新
	seek(DAT,0,0);
	print DAT @data;
	print DAT "$in{cnt}<>$date<>\n";
	truncate(DAT,tell(DAT));
	close(DAT);

	# 完了メッセージ
	message("モード切り替えが完了しました！");
	}
	
	# 切り替えフォーム
	open(IN,"$cf{tmpldir}/change.html") or error("open err: $cf{tmpldir}/change.html");
	my $tmpl = join('',<IN>);
	close(IN);
	
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!pass!/$cf{mode_pass}/g;

	my ($flg,$log);
	open(DAT,"$cf{changecnt}");
	my $data = <DAT>;
	close(DAT);
		my ($cnt,$dat) = split(/<>/, $data);

	$tmpl =~ s/!cnt!/$cnt/g;
	
	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッター
	footer();
	exit;
}

#-----------------------------------------------------------
#  完了メッセージ
#-----------------------------------------------------------
sub message {
	my ($msg) = @_;
	
	open(IN,"$cf{tmpldir}/msg.html") or error("open err: msg.html");
	my $tmpl = join('',<IN>);
	close(IN);
	
	$tmpl =~ s/!bbs_cgi!/$cf{bbs_cgi}/g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!message!/$msg/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	
	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッター
	footer();
	exit;
}

#-----------------------------------------------------------
#  diary表示部
#-----------------------------------------------------------
sub diary_page {
	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# Datepicker用ログデータ読み込み
	my ($ttl,$log) = datepicker($in{y},$in{m}) if ($in{warp});

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	# ページ数定義
	my $pg = $in{pg} || 0;

	# コメント数カウント
	my ($i,@log,%res_cnt,@reply,@main_log,@rescnt);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}
	
	# 記事展開
	my ($i,@data);
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		if (!$in{warp}) {
			$i++;
			next if ($i < $pg + 1);
			next if ($i > $pg + $cf{max_thread});

			push(@data,$_);
		}

		# リンク
		$eml &&= qq|<a href="mailto:$eml"><img src="$cf{ico_mail}" class="icon"></a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{cmnurl}/home.png" alt="Home" class="icon"></a>|;
		$com = auto_link($com) if ($cf{auto_link});

	my %e = (1 => $e1, 2 => $e2, 3 => $e3, 4 => $e4, 5 => $e5, 6 => $e6, 7 => $e7, 8 => $e8, 9 => $e9, 10 => $e10, 11 => $e11);
	foreach my $i (1 .. 11) {
		if ($e{$i}) {
			if ($cf{img_check} && $chk eq '0') {
				($e{$i}{$no}) = "hide";
				}
			}
		}
	}
	close(IN);
	
	# 繰越ボタン作成
	my $page_btn = make_pgbtn($i,$pg,"&mode=diary&pass=$cf{diary_pass}",$cf{max_thread});
	
	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();
	
	# テンプレート読込
	open(IN,"$cf{tmpldir}/diary.html") or error("open err: diary.html");
	my $tmpl = join('',<IN>);
	close(IN);
	
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!ttl-name!/$ttl/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="$1" class="icon">|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s/<!-- op_icon -->/$op_icon/g;
	$tmpl =~ s/<!-- op_weather -->/$op_weather/g;
	$tmpl =~ s/!d-icon_reg!/$cf{dicoreg_cgi}/g;
	$tmpl =~ s|<!-- select_icon -->|<div id="Selected"><div class="select"><img id="imgchange" src="$cf{faceurl}/$selecticon" width="80px" height="80px" alt=""></div></div>|g;
	$tmpl =~ s|<!-- select_mark -->|<div id="weather_selected"><div class="select"><img id="imagechange" src="$cf{weatherurl}/$selectmark" width="47px" height="30px" alt=""></div></div>|g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/!comment_count!/$cf{max_com}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;

	# 追加添付ファイル数
	my ($op_attach,$attach,$extxt,$att_msg,$gfc,$refno);

		# データ読み込み
		open(DAT,"$cf{addcnt}");
		my $data = join('',<DAT>);
		close(DAT);
			my ($cnt,$date) = split(/<>/, $data);

	$cnt = $cnt ne '' ? $cnt : $cf{attach_defo};
	foreach (@{$cf{addattach}}) {

			$refno = $_;
			if ($refno <= 9){ $refno = "　　" . $refno; }
			if ($refno eq 10) { $refno = "　" . $refno; }
			$refno =~ s/0/０/g;
			$refno =~ s/1/１/g;
			$refno =~ s/2/２/g;
			$refno =~ s/3/３/g;
			$refno =~ s/4/４/g;
			$refno =~ s/5/５/g;
			$refno =~ s/6/６/g;
			$refno =~ s/7/７/g;
			$refno =~ s/8/８/g;
			$refno =~ s/9/９/g;

		if ($cnt == $_) {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		} else {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		}
	}
		$tmpl =~ s/<!-- add_attach -->/$op_attach/g;

	if ($cnt > 0) {
		foreach $gfc (1 .. $cnt) {
			my $refn = $gfc + 1;
			$attach .= qq|<div class="uploadButton"><span>+ Add files $gfc</span><input type="file" name="upfile$refn" id="upload$refn" onchange="uv0$refn.style.display='inline-block'; uv0$refn.value = this.value;" /><input type="text" id="uv0$refn" class="uploadValue" disabled /></div>\n|;
			$extxt .= qq|<input type="text" name="imgtext$refn" class="reg-imgtxt_add" placeholder="追加添付$gfcタイトル" value="">\n|;
		}
	}
		if ($cnt == 0) { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームを追加出来ます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		} elsif ($cnt == 10) { 
			$att_msg .= qq|<small><span style="color:#d83473">参照フォームは10個が限度です</span> <img src="./cmn/arrow-gfno01.png" alt="追加" class="icon2"></small>\n|;
		} else { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームの数を変更できます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		}

		$cnt =~ s/0/０/g;
		$cnt =~ s/1/１/g;
		$cnt =~ s/2/２/g;
		$cnt =~ s/3/３/g;
		$cnt =~ s/4/４/g;
		$cnt =~ s/5/５/g;
		$cnt =~ s/6/６/g;
		$cnt =~ s/7/７/g;
		$cnt =~ s/8/８/g;
		$cnt =~ s/9/９/g;

		$tmpl =~ s/!ref_no!/$cnt/g;
		$tmpl =~ s/<!-- attach -->/$attach/g;
		$tmpl =~ s/<!-- extxt -->/$extxt/g;
		$tmpl =~ s/!att_msg!/$att_msg/g;

	if (!$in{warp}) {
		$tmpl =~ s/<!-- back_begin -->.+?<!-- back_end -->//sg;
	}

	if ($cf{icon_mode} == 0) {
		$tmpl =~ s|<!-- pop_icon -->.+?<!-- /pop_icon -->||s;
	}

	if ($cfd{diary_mode} == 1) {
		$tmpl =~ s|<!-- diary -->.+?<!-- /diary -->||s;
	}
	
	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	# ノンアイコン
	if ($cf{icon_mode} == 0) {
		$loop =~ s/!icon!//g;
	}
	
	# 画像認証作成
	my ($str_plain,$str_crypt);
	if ($cf{use_captcha} > 0) {
		require $cf{captcha_pl};
		($str_plain,$str_crypt) = cap::make($cf{captcha_key},$cf{cap_len});
	} else {
		$head =~ s|<!-- captcha -->.+?<!-- /captcha -->||s;
	}

	# 認証クッキー
	if ($cf{authkey} && $in{mode} eq 'login') {
		&set_cookie('CGISESSID',$au{sid});
	}
	
	# 文字置換
	for ($head,$foot) {
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s/!pass!/$cf{diary_pass}/g;
		s/!page_btn!/$page_btn/g;
		s/!name!/$cook[0]/;
		s/!email!/$cook[1]/;
		s/!url!/$cook[2]/;
		s/!str_crypt!/$str_crypt/g;
		s/!color!/$color/g;
		s/<!-- op_color2 -->/$color_b/g;
		s/!sub!//g;
		s|!form_css!|$cf{cmnurl}/form.css|g;
		s|!input_file_btn_css!|$cf{cmnurl}/input_file_btn.css|g;
		s/!max_res!/$cf{max_res}/g;
		s/!ptop-icon!/<img src="$cf{ico_ptop}" alt="pagetop">/g;

		# 認証モード
		if ($cf{authkey}) {
			s/!login-name!/$au{name}/g;
		} else {
			s/<!-- auth_begin -->.+?<!-- auth_end -->//sg;
		}
	}
	
	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;
	
	my @file = $in{warp} ? @{$log} : @data;

	# 記事表示
	foreach (@file) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

	# 親記事
		# 添付
		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		# UPファイル
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);

		$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

		my $tmp = $loop;
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s/!pass!/$cf{diary_pass}/g;
		$tmp =~ s/!sub!/$sub/g;
		$tmp =~ s/!name!/$name/g;
		$tmp =~ s/!url!/$url/g;

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	$date = "$y年$m月$d日（$cf{week}[$week]曜日）";

		$tmp =~ s/!date!/$date/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
		$tmp =~ s/!num!/$no/g;

	if (!$ico) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
	}
		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/!bbs_cgi!/$cf{bbs_cgi}/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=diary_edp&amp;&edit=$no"><img src="$cf{ico_edit}" title="記事編集" alt="記事編集" class="clip"></a>|g;

		if (!$res_cnt{$no}) { $res_cnt{$no} = 0; }

		$tmp =~ s/!res_com!/$cf{bbs_cgi}?mode=rescom&no=$no/g;
		$tmp =~ s/!ico_com!/$cf{ico_com}/g;
		$tmp =~ s|!ico_com02!|<img src="$cf{iconurl}/discuss02.png" alt="">|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}

		print $tmp;
	}
	
	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  diary管理モード
#-----------------------------------------------------------
sub diary_admin {
	passwd2(%in) if ($cf{diary_pass} ne '');

	# ページ数定義
	my $pg = $in{pg} || 0;

	# コメント数カウント
	my ($i,@log,%res_cnt,@reply,@main_log,@rescnt);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}
	
	# 記事展開
	my ($i,@dlog);
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		
		$i++;
		next if ($i < $pg + 1);
		next if ($i > $pg + $cf{max_thread});

			push(@dlog,$_);

		# リンク
		$eml &&= qq|<a href="mailto:$eml"><img src="$cf{ico_mail}" class="icon"></a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{cmnurl}/home.png" alt="Home" class="icon"></a>|;
		$com = auto_link($com) if ($cf{auto_link});
		
	my %e = (1 => $e1, 2 => $e2, 3 => $e3, 4 => $e4, 5 => $e5, 6 => $e6, 7 => $e7, 8 => $e8, 9 => $e9, 10 => $e10, 11 => $e11);
	foreach my $i (1 .. 11) {
		if ($e{$i}) {
			if ($cf{img_check} && $chk eq '0') {
				($e{$i}{$no}) = "hide";
				}
			}
		}
	}
	close(IN);
	
	# 繰越ボタン作成
	my $page_btn = diary_adpgbtn($i,$pg,"&amp;mode=diary_adm&amp;pass=$cf{diary_pass}",$cf{max_thread});
	
	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}
	
	# テンプレート読込
	open(IN,"$cf{tmpldir}/diary_admin.html") or error("open err: diary_admin.html");
	my $tmpl = join('',<IN>);
	close(IN);
	
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="$1" class="icon">|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/<!-- op_icon -->/$op_icon/g;
	$tmpl =~ s/<!-- op_weather -->/$op_weather/g;
	$tmpl =~ s/!d-icon_reg!/$cf{dicoreg_cgi}/g;
	$tmpl =~ s|<!-- select_icon -->|<div id="Selected"><div class="select"><img id="imgchange" src="$cf{faceurl}/$selecticon" width="80px" height="80px" alt=""></div></div>|g;
	$tmpl =~ s|<!-- select_mark -->|<div id="weather_selected"><div class="select"><img id="imagechange" src="$cf{weatherurl}/$selectmark" width="47px" height="30px" alt=""></div></div>|g;

	# 追加添付ファイル数
	my ($op_attach,$attach,$extxt,$att_msg,$gfc,$refno);

		# データ読み込み
		open(DAT,"$cf{addcnt}");
		my $data = join('',<DAT>);
		close(DAT);
			my ($cnt,$date) = split(/<>/, $data);

	$cnt = $cnt ne '' ? $cnt : $cf{attach_defo};
	foreach (@{$cf{addattach}}) {

			$refno = $_;
			if ($refno <= 9){ $refno = "　　" . $refno; }
			if ($refno eq 10) { $refno = "　" . $refno; }
			$refno =~ s/0/０/g;
			$refno =~ s/1/１/g;
			$refno =~ s/2/２/g;
			$refno =~ s/3/３/g;
			$refno =~ s/4/４/g;
			$refno =~ s/5/５/g;
			$refno =~ s/6/６/g;
			$refno =~ s/7/７/g;
			$refno =~ s/8/８/g;
			$refno =~ s/9/９/g;

		if ($cnt == $_) {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		} else {
			$op_attach .= qq|<option value="$_">$refno個</option>\n|;
		}
	}
		$tmpl =~ s/<!-- add_attach -->/$op_attach/g;

	if ($cnt > 0) {
		foreach $gfc (1 .. $cnt) {
			my $refn = $gfc + 1;
			$attach .= qq|<div class="uploadButton"><span>+ Add files $gfc</span><input type="file" name="upfile$refn" id="upload$refn" onchange="uv0$refn.style.display='inline-block'; uv0$refn.value = this.value;" /><input type="text" id="uv0$refn" class="uploadValue" disabled /></div>\n|;
			$extxt .= qq|<input type="text" name="imgtext$refn" class="reg-imgtxt_add" placeholder="追加添付$gfcタイトル" value="">\n|;
		}
	}
		if ($cnt == 0) { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームを追加出来ます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		} elsif ($cnt == 10) { 
			$att_msg .= qq|<small><span style="color:#d83473">参照フォームは10個が限度です</span> <img src="./cmn/arrow-gfno01.png" alt="追加" class="icon2"></small>\n|;
		} else { 
			$att_msg .= qq|<small><span style="color:#00885a">参照フォームの数を変更できます</span> <img src="./cmn/arrow-gfno.png" alt="追加" class="icon2"></small>\n|;
		}

		$cnt =~ s/0/０/g;
		$cnt =~ s/1/１/g;
		$cnt =~ s/2/２/g;
		$cnt =~ s/3/３/g;
		$cnt =~ s/4/４/g;
		$cnt =~ s/5/５/g;
		$cnt =~ s/6/６/g;
		$cnt =~ s/7/７/g;
		$cnt =~ s/8/８/g;
		$cnt =~ s/9/９/g;

		$tmpl =~ s/!ref_no!/$cnt/g;
		$tmpl =~ s/<!-- attach -->/$attach/g;
		$tmpl =~ s/<!-- extxt -->/$extxt/g;
		$tmpl =~ s/!att_msg!/$att_msg/g;

	if ($cf{icon_mode} == 0) {
		$tmpl =~ s|<!-- pop_icon -->.+?<!-- /pop_icon -->||s;
	}

	if ($cfd{diary_mode} == 1) {
		$tmpl =~ s|<!-- diary -->.+?<!-- /diary -->||s;
	}
	
	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	# ノンアイコン
	if ($cf{icon_mode} == 0) {
		$loop =~ s/!icon!//g;
	}
	
	# 画像認証作成
	my ($str_plain,$str_crypt);
	if ($cf{use_captcha} > 0) {
		require $cf{captcha_pl};
		($str_plain,$str_crypt) = cap::make($cf{captcha_key},$cf{cap_len});
	} else {
		$head =~ s|<!-- captcha -->.+?<!-- /captcha -->||s;
	}
	
	# 文字置換
	for ($head,$foot) {
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s/!pass!/$cf{diary_pass}/g;
		s/!page_btn!/$page_btn/g;
		s/!name!/$cook[0]/;
		s/!email!/$cook[1]/;
		s/!url!/$cook[2]/;
		s/!str_crypt!/$str_crypt/g;
		s/!color!/$color/g;
		s/<!-- op_color2 -->/$color_b/g;
		s/!sub!//g;
	}
	
	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;
	
	# 記事表示
	foreach (@dlog) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		# 添付
		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		# UPファイル
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);

		$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

		my $tmp = $loop;
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s/!pass!/$cf{diary_pass}/g;
		$tmp =~ s/!sub!/$sub/g;
		$tmp =~ s/!name!/$name/g;
		$tmp =~ s/!url!/$url/g;

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	$date = "$y年$m月$d日（$cf{week}[$week]曜日）";

		$tmp =~ s/!date!/$date/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
		$tmp =~ s/!num!/$no/g;

		if (!$ico) {
			$tmp =~ s|!icon!||g;
		} else {
			$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
		}

		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/!bbs_cgi!/$cf{bbs_cgi}/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=diary_edp&edit=$no&pass=$cf{diary_pass}"><img src="$cf{ico_edit}" title="記事編集" alt="記事編集" class="clip"></a>|g;

		if (!$res_cnt{$no}) { $res_cnt{$no} = 0; }

		$tmp =~ s/!res_com!/$cf{bbs_cgi}?mode=rescom&no=$no/g;
		$tmp =~ s/!ico_com!/$cf{ico_com}/g;
		$tmp =~ s|!ico_com02!|<img src="$cf{iconurl}/discuss02.png" alt="">|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;

		# コメント返信機能ＯＦＦ
		if ($cf{comment} == 0) {
			$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
		}
		print $tmp;
	}
	
	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  ダイアリー管理モードペイジャー作成
#-----------------------------------------------------------
sub diary_adpgbtn {
	my ($i,$pg,$stat,$max) = @_;
	
	# ページ繰越定義
	$max ||= 5;
	my $next = $pg + $max;
	my $back = $pg - $max;
	
	# ページ繰越ボタン作成
	my @pg;
	if ($back >= 0 || $next < $i) {
		my $flg;
		my ($w,$x,$y,$z) = (0,1,0,$i);
		while ($z > 0) {
			if ($pg == $y) {
				$flg++;
				push(@pg,qq!<span class="page active">$x</span>!);
			} else {
				push(@pg,qq!<a href="$cf{bbs_cgi}?pg=$y$stat" class="page gradient">$x</a>!);
			}
			$x++;
			$y += $max;
			$z -= $max;
			
			if ($flg) { $w++; }
			last if ($w >= 5 && @pg >= 10);
		}
	}
	while( @pg >= 11 ) { shift(@pg); }
	my $ret = join('', @pg);
	if ($back >= 0) {
		$ret = qq!<a href="$cf{bbs_cgi}?pg=$back$stat" class="page gradient">&laquo;</a>\n! . $ret;
	}
	if ($next < $i) {
		$ret .= qq!<a href="$cf{bbs_cgi}?pg=$next$stat" class="page gradient">&raquo;</a>\n!;
	}
	
	# 結果を返す
	return $ret ? qq|<div class="pagination">$ret</div>| : '';
}

#-----------------------------------------------------------
#  diary最新記事リスト・記事閲覧
#-----------------------------------------------------------
sub diary_topic {

	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	# コメント数カウント
	my ($i,@log,%res_cnt,@reply,@main_log,@rescnt);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}

	my (@log);
	open(IN,"$cf{diarylog}") or &error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		chomp;
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		if ($in{no} == $no) {
			push(@log,$_);
		}
	}
	close(IN);

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();

	# テンプレート認識
	open(IN,"$cf{tmpldir}/diary_topic.html") or &error("open err: diary_topic.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置き換え
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!subttl!/$cf{sub_ttl}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!counter!/$counter/g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s/!comment_count!/$cf{max_com}/g;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s|!cmnurl!|$cf{cmnurl}|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!homepage!/$cf{homepage}/g;

	if ($in{warp}) {
		$tmpl =~ s/<!-- warp_begin -->.+?<!-- warp_end -->//sg;
	} else {
		$tmpl =~ s/<!-- back_begin -->.+?<!-- back_end -->//sg;
	}

	# 日付フォーマット
	my ($y,$m) = $in{mon} =~ /^(\d+)\/(\d+)/ && ($1,$2);
	my $day = "$y年$m月";

	$tmpl =~ s/!day!/$day/g;

	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	# 文字置換
	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!subttl!/$cf{sub_ttl}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s|!rss!|$cf{htmlurl}/index.xml|;
		s/<!-- past_begin -->.+<!-- past_end -->//s if ($cf{pastkey} == 0);
	}

	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;
	
	# 記事展開
	foreach (@log) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		# UPファイル
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);

		$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	$date = "$y年$m月$d日（$cf{week}[$week]曜日）";

		# 文字置換
		my $tmp = $loop;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!subject!/$sub/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;
		$tmp =~ s/!ico_com!/$cf{ico_com}/g;
		$tmp =~ s/!res_com!/$cf{bbs_cgi}?mode=rescom&no=$no/g;

	if (!$ico) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
	}

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}

		print $tmp;
	}
	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  ダイアリーお知らせ・アーカイブ画面
#-----------------------------------------------------------
sub read_side {
	
	# サイドデータ
	open(IN,"$cf{datadir}/side.dat") or error('open err: $cf{datadir}/side.dat');
	my $side = join('', <IN>);
	close(IN);
	
	# アーカイブ生成
	my (%mons,@top_ttl,$archive);
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while(<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		# 年月
		my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
		$mons{"$y$m"}++;

		$archive = put_archive(%mons);

	}
	close(IN);

	$archive = qq|$archive\n|;
	
	# タグ/改行
	$side =~ s|\n|<br />|g;

	# 結果を返す
	return ($side,$archive);
}

#-----------------------------------------------------------
#  ダイアリー最新記事INDEX作成
#-----------------------------------------------------------
sub read_topic {

	# 最新記事リスト表示
	my ($i,$topic,@top_ttl,%mons);
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while(<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

			if (defined($no)) { push(@top_ttl,$_); }

		# 文字カット
		#if (length($sub) > 27) {
		#	$sub = substr($sub,0,34) . '...略';
		#}
		
		$i++;
		last if ($i >= $cf{max_topics} + 1);

		# 年月
		my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
		$mons{"$y$m"}++;

		$topic .= qq|<div id="topi-ttl">○ <a href="$cf{bbs_cgi}?mode=diary_topic&no=$no&mon=$y/$m">$sub</a></div>\n|;
	}
	close(IN);

	foreach my $ttl (@top_ttl) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico) = split(/<>/, $ttl);

			$topic = qq|$topic\n|;
	}
	# 結果を返す
	return $topic;
}

#-----------------------------------------------------------
#  月間アーカイブ作成
#-----------------------------------------------------------
sub put_archive {
	my %mon = @_;

	my ($ret,$i);
	for ( sort{ $b <=> $a }keys %mon ) {
		/^(\d{4})(\d{2})/;

		$i++;
		last if ($i >= $cf{max_archive} + 1);

		$ret .= qq|<div id="topi-ttl">○ <a href="$cf{bbs_cgi}?mode=archv&mon=$1/$2">$1年$2月 ($mon{$_})</a></div>\n|;
	}
	# 結果を返す
	return $ret;
}

#-----------------------------------------------------------
#  ダイアリー月間アーカイブ記事
#-----------------------------------------------------------
sub archv_log {

	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	# コメント数カウント
	my ($i,@log,%res_cnt,@reply,@main_log,@rescnt);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}

	# 記事抽出条件
	$in{cond} =~ s/\D//g;
	$in{mon} =~ s/\t//g;

	# 記事抽出
	my @log = archv_pickup($in{mon},$in{cond}) if ($in{mon} ne '');

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();

	# テンプレート
	open(IN,"$cf{tmpldir}/archv.html") or error("open err: archv.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!counter!/$counter/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s|!cmnurl!|$cf{cmnurl}|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;

	# 日付フォーマット
	my ($y,$m) = $in{mon} =~ /^(\d+)\/(\d+)/ && ($1,$2);
	my $day = "$y年$m月";

	$tmpl =~ s/!day!/$day/g;

	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!subttl!/$cf{sub_ttl}/g;
		s/!bbs_cgi!/$cf{bbs_cgi}/g;
		s/!word!/$in{mon}/;
	}

	# ヘッダ部
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# ループ部
	foreach (@log) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
		$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	$date = "$y年$m月$d日（$cf{week}[$week]曜日）";

		my $tmp = $loop;
		$tmp =~ s/!subject!/$sub/g;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;
		$tmp =~ s/!ico_com!/$cf{ico_com}/g;
		$tmp =~ s/!res_com!/$cf{bbs_cgi}?mode=rescom&no=$no/g;

	if (!$ico) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
	}

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}

		print $tmp;
	}

	# フッタ部
	footer($foot);
}

#-----------------------------------------------------------
#  ダイアリー月間アーカイブ記事抽出
#-----------------------------------------------------------
sub archv_pickup {
	my ($word,$cond) = @_;

	# キーワードを配列化
	$word =~ s/　/ /g;
	my @wd = split(/\s+/,$word);
	
	# UTF-8定義
	my $byte1 = '[\x00-\x7f]';
	my $byte2 = '[\xC0-\xDF][\x80-\xBF]';
	my $byte3 = '[\xE0-\xEF][\x80-\xBF]{2}';
	my $byte4 = '[\xF0-\xF7][\x80-\xBF]{3}';

	# 記事抽出処理
	my @log;
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		my $flg;
		foreach my $wd (@wd) {
			if ("$date" =~ /^(?:$byte1|$byte2|$byte3|$byte4)*?\Q$wd\E/i) {
				$flg++;
				if ($cond == 0) { last; }
			} else {
				if ($cond == 1) { $flg = 0; last; }
			}
		}
		next if (!$flg);

		push(@log,$_);
	}
	close(IN);

	# 検索結果
	return @log;
}

#-----------------------------------------------------------
#  カレンダデータ読み込み（ダイアリー）
#-----------------------------------------------------------
sub calen_data {
	
	# 現在の年月
	my ($mday,$mon,$year) = (localtime())[3 .. 5];
	$mon = sprintf("%02d",++$mon);
	$year += 1900;
	
	# datepicker
	if ($in{warp} =~ m|^(\d{4})/(\d{2})|) {
		$in{y} = $1;
		$in{m} = $2;
	
	# カレンダ指定
	} elsif ($in{ymd} =~ /^(\d{4})\/(\d{2})\/(\d{2})$/) {
		$in{y} = $1;
		$in{m} = $2;
		$in{d} = $3;
		
	} elsif ($in{mon} =~ /^(\d{4})\/(\d{2})$/) {
		$in{y} = $1;
		$in{m} = $2;
		
	} else {
		# 年月引数
		$in{y} =~ s/\D//g;
		$in{m} =~ s/\D//g;
		
		if ($in{y} < 1970 or $in{m} < 1 or $in{m} > 12) {
			$in{y} = $year;
			$in{m} = $mon;
		} else {
			$in{m} = sprintf("%02d",$in{m});
		}
	}

	# データ認識
	my (%com,%ym,%ymd);
	open(IN,"$cf{diarylog}") or &error("open err: $cf{diarylog}");
	my $top = <IN>;
	while(<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$ico,$color2,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
		$ym{"$y$m"}++;
		$ymd{"$y$m$d"}++;
			if ($com) { $com{"$y$m$d"} = $com; }

	}
	close(IN);

	# カレンダ生成
	my $calen = calen_tbl($year,$mon,$mday,$in{y},$in{m},\%com);

	return $calen;

}

#-----------------------------------------------------------
#  Datepicker記事読み込み（ダイアリー）
#-----------------------------------------------------------
sub datepicker {
	my ($y,$m) = @_;

	open(LOG,"$cf{diarylog}");
	my @log = join('',<LOG>);
	close(LOG);

	$in{cond} =~ s/\D//g;
	my @ym =  ($in{y},$in{m});
	$in{ym} = join('/', @ym); 

	# Datepicker記事抽出
	@log = moncal_pickup($in{ym},$in{cond}) if ($in{ym} ne '');

	my $ttl = "<div class=\"art-title\">$y年$m月 一覧</div>" if ($in{warp});
	return ($ttl,\@log);
}

#-----------------------------------------------------------
#  ダイアリーカレンダ作成
#-----------------------------------------------------------
sub calen_tbl {
	my ($year,$mon,$mday,$y,$m,$com) = @_;
	$in{y} = $y;
	$in{m} = $m;

	# テンプレート読込
	open(IN,"$cf{tmpldir}/calendar.html") or error("open err: calendar.html");
	my $tmpl = join('',<IN>);
	close(IN);
	
	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");
	
	# 1日の曜日を求める
	my $week = get_week($in{y},$in{m},1);
	
	# 末日を求める
	my $last = last_day($in{y},$in{m});
	
	# 祝日を認識
	my $hdays = getHolidays($in{y},$in{m},1);
	my %hol;
	while ( my ($key,$val) = each %{$hdays} ) {
		$hol{$key} = encode('utf8',$val);
	}
	
	my $cal = $head;
	my ($day,$end);
	foreach my $k (1 .. 6) {
		# 週のテンプレート情報
		my $tmp = $loop;
		
		# 末日のフラグが立った場合はループを終了
		last if ($end);
		
		# 日から土まで7日分をループ
		foreach my $i (0 .. 6) {
			# 第１週で初日の週より小のとき、又は終了フラグが立ったときは「空き枠」
			if (($k == 1 && $i < $week) || $end) {
				$tmp =~ s/!$i!/&nbsp;/;
			
			# 実枠あり
			} else {
				# 日を数える
				$day++;
				
				# 本日のときは背景色を変える
				if ($in{y} == $year && $in{m} == $mon && $day == $mday) {
					$tmp =~ s/<td>!$i!/<td style="background:$cf{week_col}[8]">!$i!/;
				}
				
				# 枠処理
				my $d = sprintf("%02d",$day);
				my $cday;
				if (defined($hol{$d})) {
					$cday = qq|<span style="color:$cf{week_col}[7]" title="$hol{$d}">$day</span>|;
				} else {
					$cday = qq|<span style="color:$cf{week_col}[$i]">$day</span>|;
				}
				if (defined($$com{"$y$m$d"})) {
					$cday .= qq|<br /><a href="$cf{bbs_cgi}?mode=calen&ymd=$y/$m/$d"><img src="$cf{cmnurl}/$cf{guidance_icon}" alt="" /></a>|;
				}

				$tmp =~ s/!$i!/$cday/;
			}
			
			# 末日に達した場合フラグを立てる
			if ($day >= $last) { $end = 1; }
		}
		$cal .= $tmp;
	}
	
	# フッタ
	$cal .= $foot;

	return $cal;
}

#-----------------------------------------------------------
#  前後の年月を求める
#-----------------------------------------------------------
sub get_ym {
	my ($y,$m) = @_;
	
	# 前月
	my ($b_y,$b_m);
	if ($m == 1) {
		$b_y = $y - 1;
		$b_m = 12;
	} else {
		$b_y = $y;
		$b_m = sprintf("%02d",$m-1);
	}
	
	# 翌月
	my ($n_y,$n_m);
	if ($m == 12) {
		$n_y = $y + 1;
		$n_m = '01';
	} else {
		$n_y = $y;
		$n_m = sprintf("%02d",$m+1);
	}
	return ($b_y,$b_m,$n_y,$n_m);
}

#-----------------------------------------------------------
#  ダイアリーカレンダー日次記事
#-----------------------------------------------------------
sub calen_log {

	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	# コメント数カウント
	my ($i,@log,%res_cnt,@reply,@main_log,@rescnt);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}

	# 記事抽出条件
	$in{cond} =~ s/\D//g;
	$in{ymd} =~ s/\t//g;

	# 記事抽出
	my @log = calen_pickup($in{ymd},$in{cond}) if ($in{ymd} ne '');

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();

	# テンプレート
	open(IN,"$cf{tmpldir}/calen.html") or error("open err: calen.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!counter!/$counter/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s/!comment_count!/$cf{max_com}/g;
	$tmpl =~ s|!cmnurl!|$cf{cmnurl}|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;

	# 日付フォーマット
	my ($y,$m,$d) = $in{ymd} =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	my $day = "$y年$m月$d日【$cf{week}[$week]曜日】";

	$tmpl =~ s/!day!/$day/g;

	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!subttl!/$cf{sub_ttl}/g;
		s/!bbs_cgi!/$cf{bbs_cgi}/g;
		s/!word!/$in{ymd}/;
		s/<!-- past_begin -->.+<!-- past_end -->//s if ($cf{pastkey} == 0);
	}

	# ヘッダ部
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# ループ部
	foreach (@log) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
		$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	$date = "$y年$m月$d日（$cf{week}[$week]曜日）";

		my $tmp = $loop;
		$tmp =~ s/!subject!/$sub/g;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
	if (!$ico) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
	}

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;
		print $tmp;
	}

	# フッタ部
	footer($foot);
}

#-----------------------------------------------------------
#  ダイアリーカレンダー日次記事抽出
#-----------------------------------------------------------
sub calen_pickup {
	my ($word,$cond) = @_;

	# キーワードを配列化
	$word =~ s/　/ /g;
	my @wd = split(/\s+/,$word);
	
	# UTF-8定義
	my $byte1 = '[\x00-\x7f]';
	my $byte2 = '[\xC0-\xDF][\x80-\xBF]';
	my $byte3 = '[\xE0-\xEF][\x80-\xBF]{2}';
	my $byte4 = '[\xF0-\xF7][\x80-\xBF]{3}';

	# 記事抽出処理
	my @log;
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$ico,$color2,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		my $flg;
		foreach my $wd (@wd) {
			if ("$date" =~ /^(?:$byte1|$byte2|$byte3|$byte4)*?\Q$wd\E/i) {
				$flg++;
				if ($cond == 0) { last; }
			} else {
				if ($cond == 1) { $flg = 0; last; }
			}
		}
		next if (!$flg);

		push(@log,$_);
	}
	close(IN);

	# 検索結果
	return @log;
}

#-----------------------------------------------------------
#  ダイアリーカレンダー月次記事
#-----------------------------------------------------------
sub moncal_log {
	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	# コメント数カウント
	my ($i,@log,%res_cnt,@reply,@main_log,@rescnt);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}

	# 記事抽出条件
	$in{cond} =~ s/\D//g;
	$in{ym} =~ s/\t//g;
	my @ym =  ($in{y},$in{m});
	$in{ym} = join('/', @ym); 

	# 記事抽出
	my @log = moncal_pickup($in{ym},$in{cond}) if ($in{ym} ne '');

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();

	# テンプレート
	open(IN,"$cf{tmpldir}/mon_cal.html") or error("open err: mon_cal.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!counter!/$counter/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s/!comment_count!/$cf{max_com}/g;
	$tmpl =~ s|!cmnurl!|$cf{cmnurl}|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;

	# 日付フォーマット
	my $day = "$in{y}年$in{m}月";

	$tmpl =~ s/!day!/$day/g;

	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!subttl!/$cf{sub_ttl}/g;
		s/!bbs_cgi!/$cf{bbs_cgi}/g;
	}

	# ヘッダ部
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# ループ部
	foreach (@log) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		# UPファイル
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);

		$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# 親(YouTube)
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	my $week = get_week($y,$m,$d);
	$date = "$y年$m月$d日（$cf{week}[$week]曜日）";

		my $tmp = $loop;
		$tmp =~ s/!subject!/$sub/g;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: $cf{tubesize};"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
	if (!$ico) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
	}

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}

		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;

		print $tmp;
	}

	# フッタ部
	footer($foot);
}

#-----------------------------------------------------------
#  カレンダー月次記事抽出（ダイアリー）
#-----------------------------------------------------------
sub moncal_pickup {
	my ($word,$cond) = @_;

	# キーワードを配列化
	$word =~ s/　/ /g;
	my @wd = split(/\s+/,$word);
	
	# UTF-8定義
	my $byte1 = '[\x00-\x7f]';
	my $byte2 = '[\xC0-\xDF][\x80-\xBF]';
	my $byte3 = '[\xE0-\xEF][\x80-\xBF]{2}';
	my $byte4 = '[\xF0-\xF7][\x80-\xBF]{3}';

	# 記事抽出処理
	my @log;
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		my $flg;
		foreach my $wd (@wd) {
			if ("$date" =~ /^(?:$byte1|$byte2|$byte3|$byte4)*?\Q$wd\E/i) {
				$flg++;
				if ($cond == 0) { last; }
			} else {
				if ($cond == 1) { $flg = 0; last; }
			}
		}
		next if (!$flg);

		push(@log,$_);
	}
	close(IN);

	# 検索結果
	return @log;
}

#-----------------------------------------------------------
#  最新コメントリスト表示
#-----------------------------------------------------------
sub com_list {

	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	# 条件
	$in{cond} =~ s/\D//g;
	$in{no} =~ s/\t//g;

	# 検索条件プルダウン
	my %op = (1 => 'AND', 0 => 'OR');
	my $op_cond;
	foreach (1,0) {
		if ($in{cond} eq $_) {
			$op_cond .= qq|<option value="$_" selected>$op{$_}\n|;
		} else {
			$op_cond .= qq|<option value="$_">$op{$_}\n|;
		}
	}

	# 検索実行
	my @reply = res_select($in{no},$in{cond}) if ($in{no} ne '');

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();

	# テンプレート認識
	open(IN,"$cf{tmpldir}/com_read.html") or &error("open err: com_read.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置き換え
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!counter!/$counter/g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/!comment_count!/$cf{max_com}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s|!cmnurl!|$cf{cmnurl}|g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!homepage!/$cf{homepage}/g;
	#$tmpl =~ s/!no!/$in{no}/g;

	if ($in{warp}) {
		$tmpl =~ s/<!-- warp_begin -->.+?<!-- warp_end -->//sg;
	} else {
		$tmpl =~ s/<!-- back_begin -->.+?<!-- back_end -->//sg;
	}

	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	# 文字置換
	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!subttl!/$cf{sub_ttl}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s|!form_css!|$cf{cmnurl}/form.css|g;
	}

	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;
	
	# 記事展開
	foreach my $rescom (@reply) {
		my ($no,$ent,$comsub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/, $rescom);
		if ($url) { $url = qq |&lt;<a href="$url" target="_blank">URL</a>&gt;|; }

			$com = auto_link($com);

		my $clip;
		# 添付あり
		if ($ex) {
			# 確認待ち
			if ($cf{imgcheck} && $chk != 1) {
				my $size = get_size("$cf{r_upldir}/$no$ex");
				$com .= qq|<p>[添付]: 認証待ち (<b style="color:#FF9966">$size</b>)</p>|;

			# 添付画像
			} elsif ($ex =~ /jpg$|gif$|png$/) {
				my $op;
				if (-f "$cf{r_upldir}/$no-s$ex") {
					$op = qq|src="$cf{r_uplurl}/$no-s$ex"|;
				} else {
					($w,$h) = resize_res($w,$h);
					$op = qq|src="$cf{r_uplurl}/$no$ex" width="$w" height="$h"|;
				}
				$clip .= qq|<div id="clip"><a href="$cf{r_uplurl}/$no$ex" class="fancybox-effects-c" title="No.$no&nbsp; ( [$name] $comsub )"><img $op border="0" class="snapshot"></a></div>|;

			# 指定アイコン (画像以外)
			} elsif (defined($cf{icons}{$ex})) {
				my $size = get_size("$cf{r_upldir}/$no$ex");
				$com .= qq|<div class="clip">添付: <a href="$cf{r_uplurl}/$no$ex" target="_blank"><img src="$cf{atturl}/$cf{icons}{$ex}" alt="" class="icon" /></a> (<b style="color:#00a1e9">$size</b>)</div>|;
			}
		}

			my $tmp = $loop;
			$tmp =~ s/!r_num!/$no/g;
			$tmp =~ s/!r_name!/$name/g;
			$tmp =~ s/!r_comsub!/$comsub/g;
			$tmp =~ s/!r_date!/$date/g;
			$tmp =~ s/!r_url!/$url/g;
			$tmp =~ s/!r_comment!/$com/g;
			$tmp =~ s/<!-- ext -->/$clip/g;
			$tmp =~ s/!entry!/$ent/g;

		# 日付フォーマット
		my $mon;
		my ($y,$m,$d) = $mont =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
		$mont = "$y/$m/$d";
		$mon = "$y/$m";
			$tmp =~ s/!mon!/$mon/g;
			$tmp =~ s/!sub!/$ttl/g;

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}

		print $tmp;
	}
	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  コメント返信抽出実行
#-----------------------------------------------------------
sub res_select {
	my ($word,$cond) = @_;

	# キーワードを配列化
	$word =~ s/\x81\x40/ /g;
	my @wd = split(/\s+/,$word);

	# UTF-8定義
	my $byte1 = '[\x00-\x7f]';
	my $byte2 = '[\xC0-\xDF][\x80-\xBF]';
	my $byte3 = '[\xE0-\xEF][\x80-\xBF]{2}';
	my $byte4 = '[\xF0-\xF7][\x80-\xBF]{3}';

	# 検索処理
	my ($i,@reply);
	open(IN,"$cf{reslog}") or error("open err: $cf{logfile}");
	while (<IN>) {
		my ($no,$ent,$comsub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/);

		my $flg;
		foreach my $wd (@wd) {
			if ("$no" =~ /^(?:$byte1|$byte2|$byte3|$byte4)*?\Q$wd\E/i) {
				$flg++;
				if ($cond == 0) { last; }
			} else {
				if ($cond == 1) { $flg = 0; last; }
			}
		}
		next if (!$flg);

		push(@reply,$_);
	}
	close(IN);

	# 検索結果
	return @reply;
}

#-----------------------------------------------------------
#  コメントINDEX作成
#-----------------------------------------------------------
sub read_com {

	# 最新コメントリスト表示
	my ($i,$comlist,@top_com,@data);
	open(IN,"$cf{reslog}") or error("open err: $cf{reslog}");
	my @data = <IN>;
	close(IN);

	my $mon;
	foreach(@data) {
		my ($no,$ent,$comsub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/);

		if (defined($no)) { push(@top_com,$_); }

		# 文字カット
		#if (length($comsub) > 27) {
		#	$comsub = substr($comsub,0,36) . '...略';
		#}

		$i++;
		last if ($i >= $cf{max_com} + 1);

	# 日付フォーマット
	my ($y,$m,$d) = $date =~ /^(\d+)\/(\d+)\/(\d+)/ && ($1,$2,$3);
	$date = "$y/$m/$d";
	$mon = "$y/$m";
		
		$comlist .= qq|<div id="topi-ttl">○ <a href="$cf{bbs_cgi}?mode=com_list&no=$no&mon=$mon">$comsub - <span style="color:#d83473;">$name</span></a></div>\n|;
	}

	foreach my $ttl (@top_com) {
		my ($no,$ent,$comsub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$sub,$mont) = split(/<>/, $ttl);

			$comlist = qq|$comlist\n|;
	}
	# 結果を返す
	return $comlist;
}

#-----------------------------------------------------------
#  コメント元記事閲覧 (コメントリスト個別表示対応)
#-----------------------------------------------------------
sub article_data {

	my ($year,$mon) = @_;

	# カレンダデータ読み込み
	my $calen = calen_data();

	# 前月・翌月を求める
	my ($bk_y,$bk_m,$nx_y,$nx_m) = get_ym($in{y},$in{m});

	my (@log);
	open(IN,"$cf{diarylog}") or &error("open err: $cf{diarylog}");
	my $top = <IN>;
	while(<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		if ($in{no} == $no) {

			push(@log,$_);
		}
	}
	close(IN);

	# レス{コメント}カウント
	my ($i,@data,%res_cnt,%cat);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}

	# カウンタ
	my $counter = bbs_count() if ($cf{counter});

	# サイドデータ
	my ($side,$archive) = read_side();
	my $topic = read_topic();
	my $comment = read_com();

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# お天気マーク
	my @wicon;
	foreach (@{$cf{w_icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@wicon,$ico);
	}
	# お天気マークプルダウン
	my $op_weather;
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($icon,$nam) = split(/,/, $cf{w_icon}->[$_]);
		if ($cook[6] eq $wicon[$_]) {
			$op_weather .= qq|<option value="$wicon[$_]" selected>$nam\n|;
		} else {
			$op_weather .= qq|<option value="$wicon[$_]">$nam\n|;
		}
	}
        #セレクトお天気マーク
	my ($selectmark,$wicon);
        	if ($cook[6] eq '') {
			$selectmark = $wicon[0];
        	} else {
			$selectmark = $cook[6];
        	}
	
	# 色選択ボタン
	my @col = split(/\s+/,$cf{colors});
	my $color;
	foreach (0 .. $#col) {
		if ($_ == $cook[3]) {
			$color .= qq|<input type="radio" name="color" value="$_" checked>|;
		} else {
			$color .= qq|<input type="radio" name="color" value="$_">|;
		}
		$color .= qq|<span style="color:$col[$_]; font-size:14pt;">■</span>\n|;
	}

	# 見出しカラープルダウン
        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	my $color_b;
	if ($cook[5] eq "") { $cook[5] = $col3[0]; }
	foreach (0 .. $#col3) {
		if ($cook[5] eq $col3[$_]) {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]" selected>$col4[$_]</option>\n|;
		} else {
			$color_b .= qq|<option value="$col3[$_]" style="color:$col3[$_]">$col4[$_]</option>\n|;
		}
	}

	# ユーザー登録アイコン・ファイルの読み込み/追加
	if ($cf{user_icon} == 1) {
		put_iconlist();
	}

	# アイコン
	my @icon;
	foreach (@{$cf{icon}}) {
		my ($ico,$nam) = split(/,/);
		push(@icon,$ico);
	}

	# アイコンプルダウン
	my $op_icon;
	foreach (0 .. $#{$cf{icon}}) {
		my ($icon,$nam) = split(/,/, $cf{icon}->[$_]);
		if ($cook[4] eq $icon[$_]) {
			$op_icon .= qq|<option value="$icon[$_]" selected>$nam\n|;
		} else {
			$op_icon .= qq|<option value="$icon[$_]">$nam\n|;
		}
	}
        #セレクトアイコン
	my ($selecticon,$icon);
        	if ($cook[4] eq '') {
			$selecticon = $icon[0];
        	} else {
			$selecticon = $cook[4];
        	}

	# テンプレート認識
	open(IN,"$cf{tmpldir}/article.html") or &error("open err: article.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置き換え
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!subttl!/$cf{sub_ttl}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!counter!/$counter/g;
	$tmpl =~ s/!topic_count!/$cf{max_topics}/g;
	$tmpl =~ s/!comment_count!/$cf{max_com}/g;
	$tmpl =~ s/<!-- topic -->/$topic/;
	$tmpl =~ s/<!-- comment -->/$comment/;
	$tmpl =~ s/<!-- archive -->/$archive/;
	$tmpl =~ s/<!-- sidebody -->/$side/;
	$tmpl =~ s/!calendar!/$calen/;
	$tmpl =~ s/!year!/$in{y}/g;
	$tmpl =~ s/!month!/$in{m}/g;
	$tmpl =~ s|!back_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$bk_y&amp;m=$bk_m"><img src="$cf{cmnurl}/back.png" alt="前月" class="icn" /></a>|;
	$tmpl =~ s|!this_btn!|<a href="$cf{bbs_cgi}?y=$year&amp;m=$mon"><img src="$cf{cmnurl}/this.png" alt="今月" class="icn" /></a>|;
	$tmpl =~ s|!next_btn!|<a href="$cf{bbs_cgi}?mode=mon&amp;y=$nx_y&amp;m=$nx_m"><img src="$cf{cmnurl}/next.png" alt="翌月" class="icn" /></a>|;
	$tmpl =~ s|!topic_icon!|<img src="$cf{cmnurl}/aqua.gif" class="icon" alt="" /><span style="font-size:9pt; color:gray;"> Topic</span>|;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s|!jquery_js!|$cf{jsurl}/jquery.js|g;
	$tmpl =~ s|!sliding_js!|$cf{jsurl}/sliding_effect.js|g;
	$tmpl =~ s|!cmnurl!|$cf{cmnurl}|g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!homepage!/$cf{homepage}/g;

	if ($in{warp}) {
		$tmpl =~ s/<!-- warp_begin -->.+?<!-- warp_end -->//sg;
	} else {
		$tmpl =~ s/<!-- back_begin -->.+?<!-- back_end -->//sg;
	}

	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ m|(.+)<!-- loop -->(.+?)<!-- /loop -->(.+)|s
			? ($1,$2,$3)
			: error("テンプレート不正");

	# 文字置換
	for ($head, $foot) {
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!subttl!/$cf{sub_ttl}/g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!homepage!/$cf{homepage}/g;
		s|!rss!|$cf{htmlurl}/index.xml|;
	}

	# ヘッダ表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;
	
	# 記事展開
	foreach (@log) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		if (!$res_cnt{$no}) { $res_cnt{$no} = 0; }

			$com = auto_link($com);

		$com = qq|<span style="color:$col[$col]">$com</span>|;
		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		# UPファイル
		my $clip;
		$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);

		$clip &&= "<p>$clip</p>";

		# 文字置換
		my $tmp = $loop;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!subject!/$sub/g;
		$tmp =~ s/!comment!/$com/g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s/!res_com!/$cf{bbs_cgi}?mode=rescom&no=$no/g;
		$tmp =~ s/!ico_com!/$cf{ico_com}/g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;

	if (!$ico) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;
	}
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;

	# コメント返信機能ＯＦＦ
	if ($cf{comment} == 0) {
		$tmp =~ s/<!-- rescom_job_begin -->.+<!-- rescom_job_end -->//s;
	}
		print $tmp;
	}
	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  コメント返信フォーム
#-----------------------------------------------------------
sub rescom_form {

	# レスカウント
	my (@log,%res_cnt,@reply,$max_res_flg);
	open(IN,"$cf{rescnt}");
	while(<IN>) {
		my ($no,$date,$name,$res) = split(/<>/);

		$res_cnt{$no} = $res;

	}

	# 表示データ認識
	my (@log,@reply,%res_cnt,%res,$max_res_flg);
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		
		if ($in{no} == $no) {

			$res{sub} = $sub;

		push(@log,$_);
		}
	}

	# コメント返信ログ
	open(IN,"$cf{reslog}");
	while(<IN>) {
		my ($no,$ent,$comsub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/);

		# 該当レス
		if ($in{no} == $ent) {

			#$res{sub} = $comsub;

		push(@reply,$_);

			$res_cnt{$ent}++;
		}
	}
	close(IN);

	# クッキー取得
	my ($c_nam,$c_url) = get_cookie();
	$c_url ||= 'http://';

	# テンプレート読み込み
	open(IN,"$cf{tmpldir}/com_form.html") or error("open err: com_form.html");
	my $tmpl = join('', <IN>);
	close(IN);

	open(IN,"$cf{tmpldir}/comment.html") or error("open err: comment.html");
	my $resloop = join('', <IN>);
	close(IN);

	# 画像認証作成
	my ($str_plain,$str_crypt);
	if ($cf{use_captcha} > 0) {
		require $cf{captcha_pl};
		($str_plain,$str_crypt) = cap::make($cf{captcha_key},$cf{cap_len});
		$tmpl =~ s/!str_crypt!/$str_crypt/g;
	} else {
		$tmpl =~ s/<!-- captcha_begin -->.+<!-- captcha_end -->//s;
	}

	# テンプレート分割
	my ($head,$loop,$foot) = split(/<!-- loop -->/,$tmpl,$resloop);

	# 文字置換
	for ($head,$foot) {
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s|!bbs_css!|$cf{cmnurl}/smart.css|g;
		s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
		s|!form_css!|$cf{cmnurl}/form.css|g;
		s|!input_file_btn_css!|$cf{cmnurl}/input_file_btn.css|g;
		s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
		s/!c_name!/$c_nam/g;
		s/!c_url!/$c_url/g;
		s/!bbs_title!/$cf{bbs_title}/g;
		s/!max_res!/$cf{max_res}/g;
		s/!no!/$in{no}/g;
		s/!r-sub!/$res{sub}/g;
		s/!ptop-icon!/<img src="$cf{ico_ptop}" alt="pagetop">/g;

	# 返信コメント
	if ($res_cnt{$in{no}} == 0) {
		s|<!-- rescomment -->|<span style="color:#ed6d46">《この記事にはまだコメントがありません》</span>|g;
	} else {
		s/<!-- rescomment -->//g;
	}

	# 返信フォームのサブミットボタン(ディスエイブル)｛エラーメッセージを表示したい場合は、コメントアウト｝
	#if ($res_cnt{$in{no}} >= $cf{max_res}) {
	#	s|<input class="submit-button" type="submit" value="Submit" />|<input class="submit-button" type="submit" value="Submit" disabled />|g;
	#	}

	}

	# 画面展開
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	foreach my $log (@log) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/,$log);
		$name = qq|<a href="mailto:$eml">$name</a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon" alt="" /></a>|;
		$com = auto_link($com) if ($cf{autolink});
		$eml = qq|<a href="mailto:$eml"><img src="$cf{ico_mail}" class="icon" alt="" /></a>| if ($eml);

		if (!$res_cnt{$no}) { $res_cnt{$no} = 0; }

		if (defined($e1)) {
			$com = diary_att_file($no,$com,$e1,$w1,$h1,$sub,$imgtxt1);
		}
		my (%clip,$clip);
			$clip = diary_attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$name,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
			$clip &&= "<p>$clip</p>";

		my @col = split(/\s+/, $cf{colors});

		# 記事書き出し
		my $tmp = $loop;
		$tmp =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
		$tmp =~ s/!sub!/$sub/g;
		$tmp =~ s/!name!/$name/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
		$tmp =~ s/!num!/$in{no}/g;
		$tmp =~ s|!diary_com!|<b style="color:#d83473">DIARY MODE</b>|g;
		$tmp =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s|!ico_edit!|<a href="$cf{bbs_cgi}?mode=p_form&no=$no"><img src="$cf{com_iconurl}/Edit02.png" class="clip" alt="" /></a>|g;
		$tmp =~ s/!res_com!/$cf{bbs_cgi}?mode=rescom&no=$no/g;
		$tmp =~ s/!ico_com!/$cf{ico_com}/g;
		$tmp =~ s|!ico_com02!|<img src="$cf{com_iconurl}/discuss02.png" class="clip" alt="">|g;
		$tmp =~ s/!res_cnt!/$res_cnt{$no}/g;

	if (!$res{ico}) {
		$tmp =~ s|!icon!||g;
	} else {
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$res{ico}" width="80px" height="80px" class="image" alt="" />|g;

	}
		print $tmp;

	foreach (@reply) {
		my ($no,$ent,$comsub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/);
		if ($url) { $url = qq |&lt;<a href="$url" target="_blank">URL</a>&gt;|; }

		my $clip;
		# 添付あり
		if ($ex) {
			# 確認待ち
			if ($cf{imgcheck} && $chk != 1) {
				my $size = get_size("$cf{r_upldir}/$no$ex");
				$com .= qq|<p>[添付]: 認証待ち (<b style="color:#FF9966">$size</b>)</p>|;

			# 添付画像
			} elsif ($ex =~ /jpg$|gif$|png$/) {
				my $op;
				if (-f "$cf{r_upldir}/$no-s$ex") {
					$op = qq|src="$cf{r_uplurl}/$no-s$ex"|;
				} else {
					($w,$h) = resize_res($w,$h);
					$op = qq|src="$cf{r_uplurl}/$no$ex" width="$w" height="$h"|;
				}

				$clip .= qq|<a href="$cf{r_uplurl}/$no$ex" class="fancybox-effects-c" title="No.$no&nbsp; ( $name $comsub )"><img $op border="0" class="snapshot" id="clip"></a>|;

			# 指定アイコン (画像以外)
			} elsif (defined($cf{icons}{$ex})) {
				my $size = get_size("$cf{r_upldir}/$no$ex");
				$com .= qq|<div class="clip">添付: <a href="$cf{r_uplurl}/$no$ex" target="_blank"><img src="$cf{atturl}/$cf{icons}{$ex}" alt="" class="icon" /></a> (<b style="color:#00a1e9">$size</b>)</div>|;
			}
		}

			my $tmp = $resloop;
			$tmp =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
			$tmp =~ s|!com_edit!|<a href="$cf{bbs_cgi}?mode=compwd&no=$no"><img src="$cf{com_iconurl}/edit03.png" alt="" class="clip" /></a>|g;
			$tmp =~ s/!r_name!/$name/g;
			$tmp =~ s/!r_date!/$date/g;
			$tmp =~ s/!r_comsub!/$comsub/g;
			$tmp =~ s/!r_url!/$url/g;
			$tmp =~ s/!r_comment!/$com/g;
			$tmp =~ s/<!-- ext -->/$clip/g;
			print $tmp;
		}
	}
	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  コメント返信パスワードフォーム
#-----------------------------------------------------------
sub com_pwdform {

	$in{no} =~ s/\D//g;

	# クッキー取得
	my @cook = get_cookie();
	$cook[2] ||= 'http://';

	# 記事
	my ($name,$pwd);
	open(DAT,"+< $cf{reslog}") || &error("Open Error: $cf{reslog}");
	eval "flock(DAT, 2);";
	while (<DAT>) {
		my ($no,$ent,$sub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/);

	if ($in{no} == $no) {
		$name = $name;
		$sub = $sub;
		$pwd = $pw;
		last;
		}
	}
	close(DAT);

	# パスワード未設定
	&error("このコメントはパスワード未設定のため編集できません") if ($pwd eq '');

	my ($no,$ent,$sub,$date,$name,$url,$host,$com,$ex,$w,$h,$chk,$pw,$ttl,$mont) = split(/<>/);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>|;
		$com = auto_link($com) if ($cf{autolink});
		if (length($com) > 110) { $com = substr($com,0,120); $com .= '.....(略)'; }

		my $clip;
		# 添付あり
		if ($ex) {
			# 確認待ち
			if ($cf{imgcheck} && $chk != 1) {
				my $size = get_size("$cf{r_upldir}/$no$ex");
				$com .= qq|<p>[添付]: 認証待ち (<b style="color:#FF9966">$size</b>)</p>|;

			# 添付画像
			} elsif ($ex =~ /jpg$|gif$|png$/) {
				my $op;
				if (-f "$cf{r_upldir}/$no-s$ex") {
					$op = qq|src="$cf{r_uplurl}/$no-s$ex"|;
				} else {
					($w,$h) = resize_res($w,$h);
					$op = qq|src="$cf{r_uplurl}/$no$ex" width="$w" height="$h"|;
				}
				$clip .= qq|<div id="clip"><a href="$cf{r_uplurl}/$no$ex" class="lightview" data-lightview-title="投稿者：$name　 記事タイトル：$sub" data-lightview-group-options="skin: 'mac'"><img $op border="0" class="snapshot"></a></div>|;

			# 指定アイコン (画像以外)
			} elsif (defined($cf{icons}{$ex})) {
				my $size = get_size("$cf{r_upldir}/$no$ex");
				$com .= qq|<div class="clip">添付: <a href="$cf{r_uplurl}/$no$ex" target="_blank"><img src="$cf{icourl}/$cf{icons}{$ex}" alt="" class="icon" /></a> (<b style="color:#00a1e9">$size</b>)</div>|;
			}
		}

	my @col = split(/\s+/, $cf{colors});

	open(IN,"$cf{tmpldir}/com_pwdform.html") or &error("open err: com_pwdform.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置換
	$tmpl =~ s/!sub!/$sub/g;
	$tmpl =~ s/!name!/$name/g;
	$tmpl =~ s/!no!/$in{no}/g;
	$tmpl =~ s|!comment!|$com|g;
	$tmpl =~ s/!date!/$date/g;
	$tmpl =~ s/!url!/$url/g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!num!/$no/g;
	$tmpl =~ s/!pwd!/$pwd/g;

	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();

	exit;
}

#-----------------------------------------------------------
#  ダイアリーモード編集パスワードフォーム
#-----------------------------------------------------------
sub diary_editpass {

	$in{edit} =~ s/\D//g;

	# 記事
	my ($name,$pwd);
	open(IN,"$cf{diarylog}") or error("open err: $cf{diarylog}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		if ($in{edit} == $no) {
			$name = $name;
			$pwd = $pw;
		last;
		}
	}
	close(IN);

	# パスワード未設定
	error("この記事はパスワード未設定のため編集できません") if ($pwd eq '');

		my ($no,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		$eml &&= qq|<a href="mailto:$eml"><img src="$cf{ico_mail}" class="icon"></a>| if ($eml);
		$url &&= qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>|;
		$com = &autolink($com) if ($cf{autolink});

		if (length($com) > 135) { $com = substr($com,0,140); $com .= '.....(略)'; }

		my ($clip01,$clip02,$clip03,$clip04,$clip05,$clip06,$clip07,$clip08,$clip09,$clip10,$clip11);
		if ($e1) {
			my $size = get_size("$cf{diaryimgdir}/$no-1$e1");
			if ($cf{img_check} && $chk != 1) {
				$clip01 = qq|<div class="clip">[添付1]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip01 = qq|<div class="clip">[添付1]: <a href="$cf{diaryimgurl}/$no-1$e1" target="_blank">$no-1$e1</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e2) {
			my $size = get_size("$cf{diaryimgdir}/$no-2$e2");
			if ($cf{img_check} && $chk != 1) {
				$clip02 = qq|<div class="clip">[添付2]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip02 = qq|<div class="clip">[添付2]: <a href="$cf{diaryimgurl}/$no-2$e2" target="_blank">$no-2$e2</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e3) {
			my $size = get_size("$cf{diaryimgdir}/$no-3$e3");
			if ($cf{img_check} && $chk != 1) {
				$clip03 = qq|<div class="clip">[添付3]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip03 = qq|<div class="clip">[添付3]: <a href="$cf{diaryimgurl}/$no-3$e3" target="_blank">$no-3$e3</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e4) {
			my $size = get_size("$cf{diaryimgdir}/$no-4$e4");
			if ($cf{img_check} && $chk != 1) {
				$clip04 = qq|<div class="clip">[添付4]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip04 = qq|<div class="clip">[添付4]: <a href="$cf{diaryimgurl}/$no-4$e4" target="_blank">$no-4$e4</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e5) {
			my $size = get_size("$cf{diaryimgdir}/$no-5$e5");
			if ($cf{img_check} && $chk != 1) {
				$clip05 = qq|<div class="clip">[添付5]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip05 = qq|<div class="clip">[添付5]: <a href="$cf{diaryimgurl}/$no-5$e5" target="_blank">$no-5$e5</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e6) {
			my $size = get_size("$cf{diaryimgdir}/$no-6$e6");
			if ($cf{img_check} && $chk != 1) {
				$clip06 = qq|<div class="clip">[添付6]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip06 = qq|<div class="clip">[添付6]: <a href="$cf{diaryimgurl}/$no-6$e6" target="_blank">$no-6$e6</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e7) {
			my $size = get_size("$cf{diaryimgdir}/$no-7$e7");
			if ($cf{img_check} && $chk != 1) {
				$clip07 = qq|<div class="clip">[添付7]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip07 = qq|<div class="clip">[添付7]: <a href="$cf{diaryimgurl}/$no-7$e7" target="_blank">$no-7$e7</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e8) {
			my $size = get_size("$cf{diaryimgdir}/$no-8$e8");
			if ($cf{img_check} && $chk != 1) {
				$clip08 = qq|<div class="clip">[添付8]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip08 = qq|<div class="clip">[添付8]: <a href="$cf{diaryimgurl}/$no-8$e8" target="_blank">$no-8$e8</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e9) {
			my $size = get_size("$cf{diaryimgdir}/$no-9$e9");
			if ($cf{img_check} && $chk != 1) {
				$clip09 = qq|<div class="clip">[添付9]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip09 = qq|<div class="clip">[添付9]: <a href="$cf{diaryimgurl}/$no-9$e9" target="_blank">$no-9$e9</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e10) {
			my $size = get_size("$cf{diaryimgdir}/$no-10$e10");
			if ($cf{img_check} && $chk != 1) {
				$clip10 = qq|<div class="clip">[添付10]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip10 = qq|<div class="clip">[添付10]: <a href="$cf{diaryimgurl}/$no-10$e10" target="_blank">$no-10$e10</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}
		if ($e11) {
			my $size = get_size("$cf{diaryimgdir}/$no-11$e11");
			if ($cf{img_check} && $chk != 1) {
				$clip11 = qq|<div class="clip">[添付11]: 認証待ち (<span style="color:#00a1e9">$size</span>)</div>|;
			} else {
				$clip11 = qq|<div class="clip">[添付11]: <a href="$cf{diaryimgurl}/$no-11$e11" target="_blank">$no-11$e11</a> (<span style="color:#00a1e9">$size</span>)</div>|;
			}
		}

	my @col = split(/\s+/, $cf{colors});

	# テンプレート認識
	open(IN,"$cf{tmpldir}/diary_editpass.html") or &error("open err: $cf{tmpldir}/diary_editpass.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置換
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!pass!/$cf{diary_pass}/g;
	$tmpl =~ s/!no!/$in{edit}/g;
	$tmpl =~ s/!num!/$no/g;
	$tmpl =~ s/!sub!/$sub/g;
	$tmpl =~ s/!name!/$name/g;
	$tmpl =~ s/!date!/$date/g;
	$tmpl =~ s/!color2!/$color2/g;
	$tmpl =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
	$tmpl =~ s|!w-ico!|<img src="$cf{weatherurl}/$wico" class="clip" alt="" />|g;
	$tmpl =~ s/<!-- clip01 -->/$clip01/ if($e1);
	$tmpl =~ s/<!-- clip02 -->/$clip02/ if($e2);
	$tmpl =~ s/<!-- clip03 -->/$clip03/ if($e3);
	$tmpl =~ s/<!-- clip04 -->/$clip04/ if($e4);
	$tmpl =~ s/<!-- clip05 -->/$clip05/ if($e5);
	$tmpl =~ s/<!-- clip06 -->/$clip06/ if($e6);
	$tmpl =~ s/<!-- clip07 -->/$clip07/ if($e7);
	$tmpl =~ s/<!-- clip08 -->/$clip08/ if($e8);
	$tmpl =~ s/<!-- clip09 -->/$clip09/ if($e9);
	$tmpl =~ s/<!-- clip10 -->/$clip10/ if($e10);
	$tmpl =~ s/<!-- clip11 -->/$clip11/ if($e11);
	$tmpl =~ s|!ico:(\w+\.\w+)!|<img src="$cf{iconurl}/$1" class="icon2">|g;

	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();

	exit;
}

#-----------------------------------------------------------
#  お天気マーク一覧
#-----------------------------------------------------------
sub weather_page {

	# お天気マークサイズ再定義
	$cf{wico_max_img_w} = 94;
	$cf{wico_max_img_h} = 60;
	
	# テンプレート認識
	open(IN,"$cf{tmpldir}/weather_mark.html") or error("open err: weather_mark.html");
	my $tmpl = join('', <IN>);
	close(IN);
	
	# 文字置換
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!version!/$cf{version}/g;
	
	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ /(.+)<!-- photo_begin -->(.+)<!-- photo_end -->(.+)/s
			? ($1,$2,$3)
			: error("テンプレートが不正です");
	
	# 画面展開
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;
	
	foreach (0 .. $#{$cf{w_icon}}) {
		my ($ico,$cap) = split(/,/,$cf{w_icon}[$_]);
		
		my $tmp = $loop;
		$tmp =~ s|!image!|<img src="$cf{weatherurl}/$ico" width="94px" height="60px" alt="" />|g;
		$tmp =~ s/!caption!/$cap/g;
		print $tmp;
	}
	
	# フッタ
	print $foot;
	exit;
}

#-----------------------------------------------------------
#  ワード検索
#-----------------------------------------------------------
sub find_data {
	# 条件/表示形式
	$in{cond} =~ s/\D//g;

	# 検索条件プルダウン
	my %op = (1 => 'AND', 0 => 'OR');
	my $op_cond;
	foreach (1,0) {
		if ($in{cond} eq $_) {
			$op_cond .= qq|<option value="$_" selected>$op{$_}\n|;
		} else {
			$op_cond .= qq|<option value="$_">$op{$_}\n|;
		}
	}

	# 検索実行
	my ($hit,@log) = search($cf{logfile}) if ($in{word} ne '');

	# テンプレート
	open(IN,"$cf{tmpldir}/find.html") or error("open err: find.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;

	# 分割
	my ($head,$loop,$foot) = split(/<!-- loop -->/,$tmpl);

	# 文字置換え
	for ($head, $foot) {
		s/!bbs_cgi!/$cf{bbs_cgi}/g;
		s/<!-- op_cond -->/$op_cond/;
		s/!word!/$in{word}/;

		# ワード検索
		if ($in{word}) {
			s/!result!/$hit/g;
		} else {
			s|<!-- find -->.+?<!-- /find -->||sg;
		}
	}

	my @col = split(/\s+/, $cf{colors});

	# ヘッダ部
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# ループ部
	foreach my $log (@log) {
		my ($no,$reno,$date,$name,$eml,$sub,$com,$url,$host,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/,$log);
		$name = qq|<a href="mailto:$eml">$name</a>| if ($eml);
		$com  = auto_link($com) if ($cf{auto_link});
		$url  = qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>| if ($url);

		# 添付
		if (defined($e1)) {
			$com = att_file($no,$com,$e1,$w1,$h1,$name,$sub,$imgtxt1);
		}
		my (%clip,$clip);
			$clip = attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$name,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
			$clip &&= "<p>$clip</p>";

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# YouTube
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

		$com =~ s/&amp;/&/g;

		my $tmp = $loop;
		$tmp =~ s/!reply!/$cf{ico_reply}/g;
		$tmp =~ s|!res!|res|g;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!sub!/$sub/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!name!/$name/g;
		$tmp =~ s/!home!/$url/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s/<!-- youtube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: 800px;"><div class="movie-wrap">$tube</div></div>| if ($tube);
		$tmp =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
		$tmp =~ s/<!-- clip -->/$clip/g;
		$tmp =~ s|!icon!|<img src="$cf{faceurl}/$ico" width="80px" height="80px" class="image" alt="" />|g;

		print $tmp;
	}

	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  検索実行
#-----------------------------------------------------------
sub search {
	my ($file,$list,$stat) = @_;

	# キーワードを配列化
	$in{word} =~ s/　/ /g;
	my @wd = split(/\s+/,$in{word});

	# UTF-8定義
	my $byte1 = '[\x00-\x7f]';
	my $byte2 = '[\xC0-\xDF][\x80-\xBF]';
	my $byte3 = '[\xE0-\xEF][\x80-\xBF]{2}';
	my $byte4 = '[\xF0-\xF7][\x80-\xBF]{3}';

	# 検索処理
	my ($i,@log);
	open(IN,"$file") or error("open err: $file");
	my $top = <IN> if (!$stat);
	while (<IN>) {
		my ($no,$reno,$date,$nam,$eml,$sub,$com,$url,$hos,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);

		my $flg;
		foreach my $wd (@wd) {
			if ("$nam $eml $sub $com $url" =~ /^(?:$byte1|$byte2|$byte3|$byte4)*?\Q$wd\E/i) {
				$flg++;
				if ($in{cond} == 0) { last; }
			} else {
				if ($in{cond} == 1) { $flg = 0; last; }
			}
		}
		next if (!$flg);

		$i++;
		if ($list > 0) {
			next if ($i < $in{pg} + 1);
			next if ($i > $in{pg} + $list);
		}

		push(@log,$_);
	}
	close(IN);

	# 検索結果
	return ($i,@log);
}

#-----------------------------------------------------------
#  アイコン一覧
#-----------------------------------------------------------
sub icon_page {
	# 画像サイズ再定義
	$cf{ico_max_img_w} = 80;
	$cf{ico_max_img_h} = 80;
	
	# テンプレート認識
	open(IN,"$cf{tmpldir}/icon.html") or error("open err: icon.html");
	my $tmpl = join('', <IN>);
	close(IN);
	
	# 文字置換
	$tmpl =~ s/!([a-z]+_cgi)!/$cf{$1}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!version!/$cf{version}/g;
	
	# テンプレート分割
	my ($head,$loop,$foot) = $tmpl =~ /(.+)<!-- photo_begin -->(.+)<!-- photo_end -->(.+)/s
			? ($1,$2,$3)
			: error("テンプレートが不正です");
	
	# 画面展開
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	# ユーザー登録アイコン・ファイルの読み込み/追加
	put_iconlist();
	
	foreach (1 .. $#{$cf{icon}}) {
		my ($ico,$cap) = split(/,/,$cf{icon}[$_]);
		
		my $tmp = $loop;
		$tmp =~ s|!image!|<img src="$cf{faceurl}/$ico" width="100px" height="100px" alt="" />|g;
		$tmp =~ s/!caption!/$cap/g;
		print $tmp;
	}
	
	# フッタ
	print $foot;
	exit;
}

#-----------------------------------------------------------
#  ユーザー登録アイコン・リスト挿入
#-----------------------------------------------------------
sub put_iconlist{

	my @log;
	open(IN,"$cf{iconfile}") or error("open err: $cf{iconfile}");
	while (<IN>) {
		push(@log,$_);
	}
	close(IN);
	foreach (@log) {
		my ($no,$date,$nam,$sub,$com,$hos,$pw,$ex,$w,$h,$only) = split(/<>/);

		if ($only) { 
			$only = "（$nam 様専用）"; 
		} else { 
			$only = "（共用アイコン）";
		}

		push(@{$cf{icon}},"$no$ex,<span style=\"color:#008db7\">＊ $sub$only</span>") if ($cf{user_icon});
	}
}

#-----------------------------------------------------------
#  YouTube貼付方法
#-----------------------------------------------------------
sub expound_page {

	open(IN,"$cf{tmpldir}/expound.html  ") or error("open err: expound.html  ");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;

	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;
	exit;
}

#-----------------------------------------------------------
#  留意事項表示
#-----------------------------------------------------------
sub note_page {
	# 許可拡張子（表示用）
	my $ext = ext_file();

	open(IN,"$cf{tmpldir}/note.html") or error("open err: note.html");
	my $tmpl = join('', <IN>);
	close(IN);

	my $size;
	$size = ($cf{maxdata} / 1024) . 'KB';
	$tmpl =~ s/!file!/$ext/g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s/!maxdata!/$size/g;
	$tmpl =~ s/!max_w!/$cf{max_img_w}/g;
	$tmpl =~ s/!max_h!/$cf{max_img_h}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;

	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();
	exit;
}

#-----------------------------------------------------------
#  アルバム機能
#-----------------------------------------------------------
sub album_img {
	# ページ数/表示形式
	my $pg = $in{pg} || 0;

	# 画像サイズ再定義
	$cf{max_img_w} = $cf{alb_img_w};
	$cf{max_img_h} = $cf{alb_img_h};

	# テンプレート認識
	open(IN,"$cf{tmpldir}/album.html") or error("open err: album.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;

	# テンプレート分割
	my ($head,$loop,$foot) = split(/<!-- loop -->/,$tmpl);

	# データ読み込み
	my ($i,@img);
	open(IN,"$cf{logfile}") or error("open err: $cf{logfile}");
	my $top = <IN>;
	while (<IN>) {
		my ($no,$reno,$date,$nam,$eml,$sub,$com,$url,$hos,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		next if ($cf{img_check} && $chk eq '0');
		next if ($e1 !~ /(\.jpe?g|\.png|\.gif)$/i);

		$i++;
		next if ($i < $pg + 1);
		next if ($i > $pg + $cf{max_albums});

		# 画像データ収集
		push(@img,"$no\t$sub\t$nam\t$e1\t$w1\t$h1\t$e2\t$w2\t$h2\t$e3\t$w3\t$h3\t$e4\t$w4\t$h4\t$e5\t$w5\t$h5\t$e6\t$w6\t$h6\t$e7\t$w7\t$h7\t$e8\t$w8\t$h8\t$e9\t$w9\t$h9\t$e10\t$w10\t$h10\t$e11\t$w11\t$h11\t$imgtxt1\t$imgtxt2\t$imgtxt3\t$imgtxt4\t$imgtxt5\t$imgtxt6\t$imgtxt7\t$imgtxt8\t$imgtxt9\t$imgtxt10\t$imgtxt11");
	}
	close(IN);

	# 繰越ボタン
	my $page_btn = make_pgbtn($i,$pg,'&amp;mode=album',$cf{max_albums});

	# 文字置換
	for ($head, $foot) {
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!page_btn!/$page_btn/g;
	}

	# 画面展開
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	foreach (@img) {
		my ($no,$sub,$name,$e1,$w1,$h1,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/\t/);
		
		my $clip;
			$clip = image($no,$e1,$w1,$h1,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$sub,$name,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
			$clip &&= "<p>$clip</p>";

		my $tmp = $loop;
		$tmp =~ s/!image!/$clip/g;
		$tmp =~ s/!caption!/$name/g;
		$tmp =~ s/!no!/$no/g;
		print $tmp;
	}

	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  フラグカラー・サンプル
#-----------------------------------------------------------
sub flag_page {

	# 時間取得
	my ($date,$time) = get_time2();

	# テンプレート認識
	open(IN,"$cf{tmpldir}/flag_color.html") or &error("open err: flag_color.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;

	# テンプレート分割
	my ($head,$loop,$foot);
	if ($tmpl =~ /(.+)<!-- loop_begin -->(.+)<!-- loop_end -->(.+)/s) {
		($head,$loop,$foot) = ($1, $2, $3);
	} else {
		&error("テンプレートが不正です");
	}

	# 文字置換
	foreach ($head, $foot) {
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/!version!/$cf{version}/g;
	}

	# 画面展開
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

        my @col3 = split(/\s+/, $cf{colors_b});
        my @col4 = split(/\s+/, $cf{colors_b2});
	foreach (0 .. $#col3) {

		my $tmp = $loop;
		$tmp =~ s|!color_b!|<div class="art_border" style="border-color:$col3[$_];"><h3 id="flag-sample" style="background:$col3[$_];">$date</h3>旗色SAMPLE</div>|g;
		$tmp =~ s/!name!/$col4[$_]/g;
		print $tmp;
	}

	# フッタ
	footer($foot);
	exit;
}

#-----------------------------------------------------------
#  過去ログ画面
#-----------------------------------------------------------
sub past_data {
	# 過去ログ番号
	open(IN,"$cf{nofile}") or error("open err: $cf{nofile}");
	my $pastnum = <IN>;
	close(IN);

	my $pastnum = sprintf("%04d", $pastnum);
	$in{pno} =~ s/\D//g;
	$in{pno} ||= $pastnum;

	# プルダウンタグ作成
	my $op_pno;
	for ( my $i = $pastnum; $i > 0; $i-- ) {
		$i = sprintf("%04d", $i);

		if ($in{pno} == $i) {
			$op_pno .= qq|<option value="$i" selected>$i\n|;
		} else {
			$op_pno .= qq|<option value="$i">$i\n|;
		}
	}

	# ページ数
	my $pg = $in{pg} || 0;

	# 初期化
	my ($hit,$page_btn,$hit,@log,%res);

	# 対象ログ定義
	my $file = "$cf{pastdir}/" . sprintf("%04d", $in{pno}) . ".cgi";

	# ワード検索
	if ($in{find} && $in{word} ne '') {
		# 検索
		($hit,@log) = search($file,$in{list},'past');

		# 結果
		$page_btn = "検索結果：<b>$hit</b>件 &nbsp;&nbsp;" . pgbtn_old($hit,$in{pno},$pg,$in{list},'find');

	# ログ一覧
	} else {
		my $pg_max = $cf{max_thread} * 2;

		# 過去ログオープン
		my $i = 0;
		open(IN,"$file") or error("open err: $file");
		while(<IN>) {
			my ($no,$reno,$date,$nam,$eml,$sub,$com,$url,$hos,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
			++$i if ($reno eq '');
			next if ($i < $pg + 1);
			next if ($i > $pg + $pg_max);

			if ($reno) {
				$res{$reno} .= "$no<>$reno<>$date<>$nam<>$eml<>$sub<>$com<>$url<>$hos<>$pw<>$col<>$e1<>$w1<>$h1<>$chk<>$myid<>$color2<>$tube<>$time<>$e2<>$w2<>$h2<>$e3<>$w3<>$h3<>$e4<>$w4<>$h4<>$e5<>$w5<>$h5<>$e6<>$w6<>$h6<>$e7<>$w7<>$h7<>$e8<>$w8<>$h8<>$e9<>$w9<>$h9<>$e10<>$w10<>$h10<>$e11<>$w11<>$h11<>$tubeid<>$ico<>$wico<>$imgtxt1<>$imgtxt2<>$imgtxt3<>$imgtxt4<>$imgtxt5<>$imgtxt6<>$imgtxt7<>$imgtxt8<>$imgtxt9<>$imgtxt10<>$imgtxt11\0";
				next;
			}
			push(@log,$_);
		}
		close(IN);

		# 繰越ボタン作成
		$page_btn = pgbtn_old($i,$in{pno},$pg,$pg_max);
	}

	# プルダウン作成（検索条件）
	my %op = make_op();

	# テンプレート読み込み
	open(IN,"$cf{tmpldir}/past.html") or error("open err: past.html");
	my $tmpl = join('', <IN>);
	close(IN);

	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;

	open(IN,"$cf{tmpldir}/res_past.html") or error("open err: res_past.html");
	my $restmpl = join('', <IN>);
	close(IN);

	$restmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;

	# テンプレート分割
	my ($head,$loop,$foot) = split(/<!-- loop -->/,$tmpl);

	if ($in{change}) { $in{word} = ''; }

	my @col = split(/\s+/,$cf{colors});

	# 文字置換
	for ($head, $foot) {
		s/!past_num!/$in{pno}/g;
		s/!bbs_url!//g;
		s/!([a-z]+_cgi)!/$cf{$1}/g;
		s/<!-- op_pno -->/$op_pno/g;
		s/<!-- op_(\w+) -->/$op{$1}/g;
		s/!word!/$in{word}/g;
		s/!page_btn!/$page_btn/g;
	}

	# 画面表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $head;

	foreach (@log) {
		my ($no,$reno,$date,$nam,$eml,$sub,$com,$url,$hos,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/);
		$nam = qq|<a href="mailto:$eml">$nam</a>| if ($eml);
		$com = auto_link($com) if ($cf{auto_link});
		$url = qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>| if ($url);

		# レス
		my $res;
		foreach my $log ( split(/\0/,$res{$no}) ) {
			my ($no,$reno,$date,$nam,$eml,$sub,$com,$url,$hos,$pw,$col,$e1,$w1,$h1,$chk,$myid,$color2,$tube,$time,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$tubeid,$ico,$wico,$imgtxt1,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11) = split(/<>/, $log);
			$nam = qq|<a href="mailto:$eml">$nam</a>| if ($eml);
			$com = auto_link($com) if ($cf{auto_link});
			$url = qq|<a href="$url" target="_blank"><img src="$cf{ico_home}" class="icon"></a>| if ($url);

			# タグ復元
			$tube =~ s/&lt;/</g;
			$tube =~ s/&gt;/>/g;
			$tube =~ s/&quot;/"/g;
			$tube =~ s/&amp;/&/g;

		# YouTube
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

			$com =~ s/&amp;/&/g;

		# 添付
		if (defined($e1)) {
			$com = att_file($no,$com,$e1,$w1,$h1,$nam,$sub,$imgtxt1);
		}
		my (%clip,$clip);
			$clip = attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$nam,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
			$clip &&= "<p>$clip</p>";

			my $tmp = $restmpl;
			$tmp =~ s/!sub!/$sub/g;
			$tmp =~ s/!name!/$nam/g;
			$tmp =~ s/!date!/$date/g;
			$tmp =~ s/!url!/$url/g;
			$tmp =~ s/!email!/$eml/g;
			$tmp =~ s/!num!/$no/g;
			$tmp =~ s/!color2!/$color2/g;
			$tmp =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
			$tmp =~ s|!icon!|<img src="$cf{icourl}/$ico" class="image" alt="" />|g;
			$tmp =~ s/<!-- res_clip -->/$clip/;
			$tmp =~ s/<!-- restube_id -->/$tubeid/ if ($tubeid);
			$tmp =~ s|<!-- res_youtube -->|<div style="max-width: 800px;"><div class="movie-wrap">$tube</div></div>| if ($tube);

			# 認証モード
			if ($cf{authkey}) {
				$tmp =~ s/!myid!/ID:$myid/g;
			} else {
				$tmp =~ s/<!-- id_begin -->.+?<!-- id_end -->//s;
			}

			$res .= $tmp;
		}

		# タグ復元
		$tube =~ s/&lt;/</g;
		$tube =~ s/&gt;/>/g;
		$tube =~ s/&quot;/"/g;
		$tube =~ s/&amp;/&/g;

		# YouTube
		if ($cf{tube}) {
			$tubeid =~ s|!([\w\-]{1,})!|<div class="imageCover"><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="https://i.ytimg.com/vi/$1/mqdefault.jpg" width="240" height="154" class="baseImage"></a><a class="fancybox-media" href="http://www.youtube.com/watch?v=$1" title="Youtube"><img src="$cf{tube_ico}" width="64" height="64" class="coverImage"></a></div>|g;
		}

		$com =~ s/&amp;/&/g;

		# 添付
		if (defined($e1)) {
			$com = att_file($no,$com,$e1,$w1,$h1,$nam,$sub,$imgtxt1);
		}
		my (%clip,$clip);
			$clip = attach($no,$e2,$w2,$h2,$e3,$w3,$h3,$e4,$w4,$h4,$e5,$w5,$h5,$e6,$w6,$h6,$e7,$w7,$h7,$e8,$w8,$h8,$e9,$w9,$h9,$e10,$w10,$h10,$e11,$w11,$h11,$nam,$sub,$imgtxt2,$imgtxt3,$imgtxt4,$imgtxt5,$imgtxt6,$imgtxt7,$imgtxt8,$imgtxt9,$imgtxt10,$imgtxt11);
			$clip &&= "<p>$clip</p>";

		my $tmp = $loop;
		$tmp =~ s/!num!/$no/g;
		$tmp =~ s/!sub!/$sub/g;
		$tmp =~ s/!date!/$date/g;
		$tmp =~ s/!name!/$nam/g;
		$tmp =~ s/!url!/$url/g;
		$tmp =~ s/!email!/$eml/g;
		$tmp =~ s/!color2!/$color2/g;
		$tmp =~ s|!comment!|<span style="color:$col[$col]">$com</span>|g;
		$tmp =~ s/<!-- res -->/<blockquote>$res<\/blockquote>/g if ($res);
		$tmp =~ s|!icon!|<img src="$cf{icourl}/$ico" class="image" alt="" />|g;
		$tmp =~ s/<!-- clip -->/$clip/;
		$tmp =~ s/<!-- tube_id -->/$tubeid/ if ($tubeid);
		$tmp =~ s|<!-- youtube -->|<div style="max-width: 800px;"><div class="movie-wrap">$tube</div></div>| if ($tube);

		# 認証モード
		if ($cf{authkey}) {
			$tmp =~ s/!myid!/ID:$myid/g;
		} else {
			$tmp =~ s/<!-- id_begin -->.+?<!-- id_end -->//s;
		}

		print $tmp;
	}

	# フッタ
	footer($foot);
}

#-----------------------------------------------------------
#  認証モード
#-----------------------------------------------------------
sub authent {
	# セッションモジュール取り込み
	require $cf{session_pl};

	# ログイン
	if ($in{mode} eq 'login') {
		# 入力チェック
		if ($in{id} eq '' || $in{pw} eq '') {
			&error("IDまたはパスワードが未入力です");
		}
		# セッション作成
		make_ses($in{id},$in{pw},$cf{memfile},$cf{sesdir},$cf{authtime});

	# ログオフ
	} elsif ($in{mode} eq 'logoff') {
		# セッション削除
		del_ses($cf{sesdir});

		# 入室画面
		enter_form('cook_del');

	# 入室画面
	} elsif ($in{mode} eq 'enter') {
		enter_form();

	# セッション管理
	} else {
		session($cf{sesdir},$cf{bbs_cgi});
	}
}

#-----------------------------------------------------------
#  入室画面
#-----------------------------------------------------------
sub enter_form {
	my $ck = shift;

	# テンプレート読み込み
	open(IN,"$cf{tmpldir}/enter.html") or &error("open err: enter.html");
	my $tmpl = join('', <IN>);
	close(IN);

	# 文字置換
	$tmpl =~ s|!icon:(\w+\.\w+)!|<img src="$cf{cmnurl}/$1" alt="" class="icon" />|g;
	$tmpl =~ s|!bbs_css!|$cf{cmnurl}/smart.css|g;
	$tmpl =~ s|!mob_css!|$cf{cmnurl}/smart-mobile.css|g;
	$tmpl =~ s|!bbs_js!|$cf{cmnurl}/bbs.js|g;
	$tmpl =~ s/!bbs_cgi!/$cf{bbs_cgi}/g;
	$tmpl =~ s/!bbs_title!/$cf{bbs_title}/g;

	# クッキー排除
	if ($ck eq 'cook_del') {
		set_cookie('CGISESSID','','del');
	}

	# 画面表示
	print "Content-type: text/html; charset=utf-8\n\n";
	print $tmpl;

	# フッタ
	footer();

	exit;
}

#-----------------------------------------------------------
#  URLエンコード
#-----------------------------------------------------------
sub url_enc {
	local($_) = @_;

	s/(\W)/'%' . unpack('H2', $1)/eg;
	s/\s/+/g;
	$_;
}

#-----------------------------------------------------------
#  繰越ボタン作成 [ 過去ログ ]
#-----------------------------------------------------------
sub pgbtn_old {
	my ($i,$pno,$pg,$list,$stat) = @_;
	
	# ページ繰越定義
	my $next = $pg + $list;
	my $back = $pg - $list;
	
	my $link;
	if ($stat eq 'find') {
		my $wd = url_enc($in{word});
		$link = "$cf{bbs_cgi}?mode=$in{mode}&amp;pno=$pno&amp;find=1&amp;word=$wd&amp;list=$list";
	} else {
		$link = "$cf{bbs_cgi}?mode=$in{mode}&amp;pno=$pno";
	}
	
	# ページ繰越ボタン作成
	my $pg_btn;
	if ($back >= 0 || $next < $i) {
		$pg_btn .= "Page: ";
		
		my ($x, $y) = (1, 0);
		while ($i > 0) {
			if ($pg == $y) {
				$pg_btn .= qq(| <b>$x</b> );
			} else {
				$pg_btn .= qq(| <a href="$link&pg=$y">$x</a> );
			}
			$x++;
			$y += $list;
			$i -= $list;
		}
		$pg_btn .= "|";
	}
	return $pg_btn;
}

#-----------------------------------------------------------
#  プルダウン作成 [ 検索条件 ]
#-----------------------------------------------------------
sub make_op {
	my %op;
	my %cond = (1 => 'AND', 0 => 'OR');
	foreach (1,0) {
		if ($in{cond} eq $_) {
			$op{cond} .= qq|<option value="$_" selected>$cond{$_}\n|;
		} else {
			$op{cond} .= qq|<option value="$_">$cond{$_}\n|;
		}
	}
	for ( my $i = 10; $i <= 30; $i += 5 ) {
		if ($in{list} == $i) {
			$op{list} .= qq|<option value="$i" selected>$i件\n|;
		} else {
			$op{list} .= qq|<option value="$i">$i件\n|;
		}
	}
	return %op;
}

#-----------------------------------------------------------
#  カウンタ処理
#-----------------------------------------------------------
sub bbs_count {
	# IP取得
	my $addr = $ENV{REMOTE_ADDR};

	# 閲覧時のみカウントアップ
	my $cntup = $in{mode} eq '' ? 1 : 0;

	# カウントファイルを読みこみ
	open(LOG,"+< $cf{cntfile}") or error("open err: $cf{cntfile}");
	eval "flock(LOG, 2);";
	my $count = <LOG>;

	# IPチェックとログ破損チェック
	my ($cnt,$ip) = split(/:/, $count);
	if ($addr eq $ip || $cnt eq "") { $cntup = 0; }

	# カウントアップ
	if ($cntup) {
		$cnt++;
		seek(LOG, 0, 0);
		print LOG "$cnt:$addr";
		truncate(LOG, tell(LOG));
	}
	close(LOG);

	# 桁数調整
	while(length($cnt) < $cf{mini_fig}) { $cnt = '0' . $cnt; }
	my @cnts = split(//, $cnt);

	# GIFカウンタ表示
	my $counter;
	if ($cf{counter} == 2) {
		foreach (0 .. $#cnts) {
			$counter .= qq|<img src="$cf{gif_path}/$cnts[$_].gif" alt="$cnts[$_]">|;
		}

	# テキストカウンタ表示
	} else {
		$counter = qq|<span style="color:$cf{cntcol};font-family:Verdana,Helvetica,Arial">$cnt</span>\n|;
	}
	return $counter;
}

#-----------------------------------------------------------
#  自動リンク
#-----------------------------------------------------------
sub auto_link {
	my $text = shift;

	$text =~ s/(s?https?:\/\/([\w-.!~*'();\/?:\@=+\$,%#]|&amp;)+)/<a href="$1" target="_blank">$1<\/a>/g;
	return $text;
}

#-----------------------------------------------------------
#  フッター
#-----------------------------------------------------------
sub footer {
	my $foot = shift;

	# 著作権表記（削除・改変禁止）
	my $copy = <<EOM;
<div id="footer">
	- <a href="https://www.kent-web.com/" target="_top">Joyful Note</a> -<br />
	- <a href="https://www.transformsite.com/" target="_top">$cf{version}</a> -<br>
	++ <a href="https://www.transformsite.com/" target="_top">Edited by TRANSFORM</a> ++
</div>
EOM

	if ($foot =~ /(.+)(<\/body[^>]*>.*)/si) {
		print "$1$copy$2\n";
	} else {
		print "$foot$copy\n";
		print "</body></html>\n";
	}
	exit;
}

#-----------------------------------------------------------
#  繰越ボタン作成
#-----------------------------------------------------------
sub make_pgbtn_p {
	my ($i,$pg,$stat,$max) = @_;
	$max ||= 5;

	# ページ繰越定義
	my $next = $pg + $max;
	my $back = $pg - $max;

	# ページ繰越ボタン作成
	my @pg;
	if ($back >= 0 || $next < $i) {
		my $flg;
		my ($w,$x,$y,$z) = (0,1,0,$i);
		while ($z > 0) {
			if ($pg == $y) {
				$flg++;
				push(@pg,qq!<span class="pg-on"><b>$x</b></span>!);
			} else {
				push(@pg,qq!<span class="pg-off"><a href="$cf{bbs_cgi}?pg=$y$stat">$x</a></span>!);
			}
			$x++;
			$y += $max;
			$z -= $max;

			if ($flg) { $w++; }
			last if ($w >= 5 && @pg >= 10);
		}
	}
	while( @pg >= 11 ) { shift(@pg); }
	my $ret = join('', @pg);
	if ($back >= 0) {
		$ret = qq!<span class="pg-off"><a href="$cf{bbs_cgi}?pg=$back$stat">&lt;</a></span>\n! . $ret;
	}
	if ($next < $i) {
		$ret .= qq!<span class="pg-off"><a href="$cf{bbs_cgi}?pg=$next$stat">&gt;</a></span>\n!;
	}
	$ret;
}

#-----------------------------------------------------------
#  繰越ボタン作成
#-----------------------------------------------------------
sub make_pgbtn {
	my ($i,$pg,$stat,$max) = @_;
	$max ||= 5;

	# ページ繰越定義
	my $next = $pg + $max;
	my $back = $pg - $max;

	# ページ繰越ボタン作成
	my @pg;
	if ($back >= 0 || $next < $i) {
		my $flg;
		my ($w,$x,$y,$z) = (0,1,0,$i);
		while ($z > 0) {
			if ($pg == $y) {
				$flg++;
				push(@pg,qq!<li><span>$x</span></li>!);
			} else {
				push(@pg,qq!<li><a href="$cf{bbs_cgi}?pg=$y$stat">$x</a></li>!);
			}
			$x++;
			$y += $max;
			$z -= $max;

			if ($flg) { $w++; }
			last if ($w >= 5 && @pg >= 10);
		}
	}
	while( @pg >= 11 ) { shift(@pg); }
	my $ret = join('', @pg);
	if ($back >= 0) {
		$ret = qq!<li><a href="$cf{bbs_cgi}?pg=$back$stat">&laquo;</a></li>\n! . $ret;
	}
	if ($next < $i) {
		$ret .= qq!<li><a href="$cf{bbs_cgi}?pg=$next$stat">&raquo;</a></li>\n!;
	}
	
	# 結果を返す
	return $ret ? qq|<ul class="pager">$ret</ul>| : '';
}

#-----------------------------------------------------------
#  クッキー取得
#-----------------------------------------------------------
sub get_cookie {
	# クッキー取得
	my $cook = $ENV{HTTP_COOKIE};

	# 該当IDを取り出す
	my %cook;
	foreach ( split(/;/, $cook) ) {
		my ($key,$val) = split(/=/);
		$key =~ s/\s//g;
		$cook{$key} = $val;
	}

	# URLデコード
	my @cook;
	foreach ( split(/<>/,$cook{$cf{cookie_id}}) ) {
		s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("H2", $1)/eg;
		s/[&"'<>]//g;

		push(@cook,$_);
	}
	return @cook;
}

#-----------------------------------------------------------
#  クッキー発行
#-----------------------------------------------------------
sub set_cookie {
	my ($key,$val,$del) = @_;

	# 時間定義
	my $gtime = $del eq 'del' ? time - 365*24*60*60 : time + 60*24*60*60;

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,undef,undef) = gmtime($gtime);
	my @mon  = qw|Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec|;
	my @week = qw|Sun Mon Tue Wed Thu Fri Sat|;

	# 時刻フォーマット
	my $gmt = sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
				$week[$wday],$mday,$mon[$mon],$year+1900,$hour,$min,$sec);

	print "Set-Cookie: $key=$val; expires=$gmt\n";
}

#-----------------------------------------------------------
#  crypt照合
#-----------------------------------------------------------
sub decrypt {
	my ($in, $dec) = @_;

	my $salt = $dec =~ /^\$1\$(.*)\$/ ? $1 : substr($dec, 0, 2);
	if (crypt($in, $salt) eq $dec || crypt($in, '$1$' . $salt) eq $dec) {
		return 1;
	} else {
		return 0;
	}
}

#-----------------------------------------------------------
#  時間取得[旗色サンプル用]
#-----------------------------------------------------------
sub get_time2 {
	$ENV{TZ} = "JST-9";
	my $time = time;
	my ($min,$hour,$mday,$mon,$year,$wday) = (localtime($time))[1..6];
	my @week = qw|Sun Mon Tue Wed Thu Fri Sat|;

	# 日時のフォーマット
	my $date = sprintf("%04d/%02d/%02d(%s)",
			$year+1900,$mon+1,$mday,$week[$wday],$hour,$min);

	return ($date, $time);
}

#-----------------------------------------------------------
#  添付リンク
#-----------------------------------------------------------
sub att_file {
	my ($no,$com,$e1,$w1,$h1,$nam,$sub,$imgt1) = @_;
	
	# 未公開
	if ($e1 eq 'hide') {
		$com .= qq|<p>[添付]: 認証待ち</p>|;
	
	# 画像のとき
	} elsif ($e1 =~ /(jpg|png|gif)$/) {
		
		my $op;
		if (-f "$cf{imgdir}/$no-s-1$e1") {
			$op = qq|src="$cf{imgdir}/$no-s-1$e1"|;
		} else {
			($w1,$h1) = resize($w1,$h1);
			$op = qq|src="$cf{imgdir}/$no-1$e1" width="$w1" height="$h1"|;
		}
		
		# 画像はコメントの下
		if ($cf{image_position} == 1) {
			$com .= qq|<p><a href="$cf{imgdir}/$no-1$e1" class="fancybox-effects-c" title="No.$no&nbsp; ( $nam $sub $imgt1)"><div class="snap"><img $op alt="$no"></a><br />$imgt1</div></p>|;
		
		# 画像はコメントの左（廻り込み）
		} else {
			$com = qq|<a href="$cf{imgdir}/$no-1$e1" class="fancybox-effects-c" title="No.$no&nbsp; ( $nam $sub $imgt1)"><div class="snap"><img $op align="left" hspace="28" class="snapshot" alt="$no"></a><br />$imgt1</div>$com<br clear="all">|;
		}
	
	# 指定アイコン (画像以外)
	} elsif (defined($cf{icons}{$e1})) {
		my $size = get_size("$cf{imgdir}/$no-1$e1");
		$com = qq|<a href="$cf{imgurl}/$no-1$e1" target="_blank"><div class="attachment"><img src="$cf{atturl}/$cf{icons}{$e1}" alt="" class="clip" /></a> (<b style="color:#00a1e9">$size</b>) <br />$imgt1</div>$com<br clear="all">|;
	}
	
	return $com;
}

#-----------------------------------------------------------
#  追加添付表示
#-----------------------------------------------------------
sub attach {
	my ($com) = @_;
	my $no = shift;
	my (%e,%w,%h,$nam,$sub,%imgtxt);
	($e{2},$w{2},$h{2},$e{3},$w{3},$h{3},$e{4},$w{4},$h{4},$e{5},$w{5},$h{5},$e{6},$w{6},$h{6},$e{7},$w{7},$h{7},$e{8},$w{8},$h{8},$e{9},$w{9},$h{9},$e{10},$w{10},$h{10},$e{11},$w{11},$h{11},$nam,$sub,$imgtxt{2},$imgtxt{3},$imgtxt{4},$imgtxt{5},$imgtxt{6},$imgtxt{7},$imgtxt{8},$imgtxt{9},$imgtxt{10},$imgtxt{11}) = @_;

	my $ret;
	foreach my $i (2 .. 11) {
		next if (!$e{$i});

		# 未公開
		if ($e{$i} eq 'hide') {
		$com .= qq|<p>[添付]: 認証待ち</p>|;

		# 指定アイコン (画像以外)の場合
		} elsif ($e{$i} !~ /^\.(jpg|gif|png)$/) {
			my $size = -s "$cf{imgdir}/$no-$i$e{$i}";
			my $size = get_size("$cf{imgdir}/$no-$i$e{$i}");
			$ret .= qq|<div class="addclip"><a href="$cf{imgurl}/$no-$i$e{$i}" target="_blank"><img src="$cf{atturl}/$cf{icons}{$e{$i}}" alt="" /></a><br />$imgtxt{$i}<br /> (<b style="color:#00a1e9">$size</b>)</div>|;

		# 画像の場合
		} elsif (-f "$cf{imgdir}/$no-s-$i$e{$i}") {
			$ret .= qq|<a href="$cf{imgurl}/$no-$i$e{$i}" class="fancybox-effects-c" title="No.$no&nbsp; ( $nam $sub $imgtxt{$i})"><div class="snap"><img src="$cf{imgurl}/$no-s-$i$e{$i}" class="snapshot"></a><br />$imgtxt{$i}</div>|;

		} else {
			my ($w,$h) = resize($w{$i},$h{$i});
			$ret .= qq|<a href="$cf{imgurl}/$no-$i$e{$i}" class="fancybox-effects-c" title="No.$no&nbsp; ( $nam $sub $imgtxt{$i})"><div class="snap"><img src="$cf{imgurl}/$no-$i$e{$i}" width="$w" height="$h" class="snapshot"></a><br />$imgtxt{$i}</div>|;
		}
	}
	return $ret;
}

#-----------------------------------------------------------
#  ダイアリー添付リンク
#-----------------------------------------------------------
sub diary_att_file {
	my ($no,$com,$e1,$w1,$h1,$sub,$imgtxt1) = @_;
	
	# 未公開
	if ($e1 eq 'hide') {
		$com .= qq|<p>[添付]: 認証待ち</p>|;
	
	# 画像のとき
	} elsif ($e1 =~ /(jpg|png|gif)$/) {
		
		my $op;
		if (-f "$cf{diaryimgdir}/$no-s-1$e1") {
			$op = qq|src="$cf{diaryimgdir}/$no-s-1$e1"|;
		} else {
			($w1,$h1) = resize($w1,$h1);
			$op = qq|src="$cf{diaryimgdir}/$no-1$e1" width="$w1" height="$h1"|;
		}
		
		# 画像はコメントの下
		if ($cf{image_position} == 1) {
			$com .= qq|<p><a href="$cf{diaryimgdir}/$no-1$e1" class="fancybox-effects-c" title="No.$no&nbsp; ( $sub $imgtxt1)"><div class="snap"><img $op alt="$no"></a><br />$imgtxt1</div></p>|;
		
		# 画像はコメントの左（廻り込み）
		} else {
			$com = qq|<a href="$cf{diaryimgdir}/$no-1$e1" class="fancybox-effects-c" title="No.$no&nbsp; ( $sub $imgtxt1)"><div class="snap"><img $op align="left" hspace="18" class="snapshot" alt="$no"></a><br />$imgtxt1</div>$com<br clear="all">|;
		}
	
	# 指定アイコン (画像以外)
	} elsif (defined($cf{icons}{$e1})) {
		my $size = get_size("$cf{diaryimgdir}/$no-1$e1");
		$com = qq|<a href="$cf{diaryimgdir}/$no-1$e1" target="_blank"><img src="$cf{atturl}/$cf{icons}{$e1}" alt="" class="clip" /></a> (<b style="color:#00a1e9">$size</b>)<br />$com<br clear="all">|;
	}
	
	return $com;
}

#-----------------------------------------------------------
#  ダイアリー追加添付表示
#-----------------------------------------------------------
sub diary_attach {
	my ($com) = @_;
	my $no = shift;
	my (%e,%w,%h,$sub,%imgtxt);
	($e{2},$w{2},$h{2},$e{3},$w{3},$h{3},$e{4},$w{4},$h{4},$e{5},$w{5},$h{5},$e{6},$w{6},$h{6},$e{7},$w{7},$h{7},$e{8},$w{8},$h{8},$e{9},$w{9},$h{9},$e{10},$w{10},$h{10},$e{11},$w{11},$h{11},$sub,$imgtxt{2},$imgtxt{3},$imgtxt{4},$imgtxt{5},$imgtxt{6},$imgtxt{7},$imgtxt{8},$imgtxt{9},$imgtxt{10},$imgtxt{11}) = @_;

	my $ret;
	foreach my $i (2 .. 11) {
		next if (!$e{$i});

		# 未公開
		if ($e{$i} eq 'hide') {
		$com .= qq|<p>[添付]: 認証待ち</p>|;

		# 指定アイコン (画像以外)の場合
		} elsif ($e{$i} !~ /^\.(jpg|gif|png)$/) {
			my $size = -s "$cf{diaryimgdir}/$no-$i$e{$i}";
			my $size = get_size("$cf{diaryimgdir}/$no-$i$e{$i}");
			$ret .= qq|<div class="addclip"><a href="$cf{diaryimgdir}/$no-$i$e{$i}" target="_blank"><img src="$cf{atturl}/$cf{icons}{$e{$i}}" alt="" /></a> (<b style="color:#00a1e9">$size</b>)</div>|;

		# 画像の場合
		} elsif (-f "$cf{diaryimgdir}/$no-s-$i$e{$i}") {
			$ret .= qq|<a href="$cf{diaryimgdir}/$no-$i$e{$i}" class="fancybox-effects-c" title="No.$no&nbsp; ( タイトル：$sub ファイル名：$imgtxt{$i})"><div class="snap"><img src="$cf{diaryimgurl}/$no-s-$i$e{$i}" class="snapshot"></a><br />$imgtxt{$i}</div>|;

		} else {
			my ($w,$h) = resize($w{$i},$h{$i});
			$ret .= qq|<a href="$cf{diaryimgdir}/$no-$i$e{$i}" class="fancybox-effects-c" title="No.$no&nbsp; ( タイトル：$sub ファイル名：$imgtxt{$i})"><div class="snap"><img src="$cf{diaryimgurl}/$no-$i$e{$i}" width="$w" height="$h" class="snapshot"></a><br />$imgtxt{$i}</div>|;
		}
	}
	return $ret;
}

#-----------------------------------------------------------
#  取り扱いファイル
#-----------------------------------------------------------
sub ext_file {
	my $ext;
	foreach ( keys %cf ) {
		next if ($_ !~ /^ok_(\w+)/);
		my $file = $1;
		next if (!$cf{$_});

		$file =~ tr/a-z/A-Z/;
		$ext .= "$file, ";
	}
	$ext =~ s/, $//;

	return $ext;
}

#-----------------------------------------------------------
#  画像表示
#-----------------------------------------------------------
sub image {
	my $no = shift;
	my (%e,%w,%h,$sub,$nam,%imgtxt);
	($e{1},$w{1},$h{1},$e{2},$w{2},$h{2},$e{3},$w{3},$h{3},$e{4},$w{4},$h{4},$e{5},$w{5},$h{5},$e{6},$w{6},$h{6},$e{7},$w{7},$h{7},$e{8},$w{8},$h{8},$e{9},$w{9},$h{9},$e{10},$w{10},$h{10},$e{11},$w{11},$h{11},$sub,$nam,$imgtxt{1},$imgtxt{2},$imgtxt{3},$imgtxt{4},$imgtxt{5},$imgtxt{6},$imgtxt{7},$imgtxt{8},$imgtxt{9},$imgtxt{10},$imgtxt{11}) = @_;

	my $ret;
	foreach my $i (1 .. 11) {
		next if (!$e{$i});

		if ($e{$i} =~ /^\.(jpg|gif|png)$/) {
			if (-f "$cf{imgdir}/$no-s-$i$e{$i}") {
				$ret .= qq|<a href="$cf{imgurl}/$no-$i$e{$i}" class="fancybox-buttons" title="No.$no&nbsp;&nbsp; ( $nam $sub $imgtxt{$i})"><img src="$cf{imgurl}/$no-s-$i$e{$i}" class="alb_snapshot"></a>|;

			} else {
				my ($w,$h) = alb_resize($w{$i},$h{$i});
				$ret .= qq|<a href="$cf{imgurl}/$no-$i$e{$i}" class="fancybox-buttons" title="No.$no&nbsp;&nbsp; ( $nam $sub $imgtxt{$i})"><img src="$cf{imgurl}/$no-$i$e{$i}" width="$w" height="$h" class="alb_snapshot"></a>|;
			}
		}
	}
	return $ret;
}

#-----------------------------------------------------------
#  ファイル容量
#-----------------------------------------------------------
sub get_size {
	my $file = shift;
	
	my $size = -s $file || 0;
	return $size > 1024000 ? comma(int($size/1024000)) . 'MB' : $size > 1024 ? comma(int($size/1024)) . 'KB' : $size . 'Bytes';
}

#-----------------------------------------------------------
#  桁区切り
#-----------------------------------------------------------
sub comma {
	local($_) = @_;

	1 while s/(.*\d)(\d\d\d)/$1,$2/;
	$_;
}
