#!/usr/local/bin/perl

#$CPU_start = (times)[0];

$|=1;

require './cgi-lib.pl';
require './boardini.cgi';

$time = time;
($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime($time);
$year = $year+1900;
$mon = $mon+1;
$datenum = sprintf("%04d%02d%02d%02d%02d%02d",$year,$mon,$mday,$hour,$min,$sec);
$upurl = $updir if $upurl eq "";

require './skinini.cgi';
require './conv.cgi';
require './ta-lib.cgi';

#---------------------------------------------------------------main

&host_get;
&decode;
&count;
if    ($in{'command'} eq "write")    {&write;}
elsif ($in{'command'} eq "reswrite") {&reswrite;}
elsif ($in{'command'} eq "remove")   {&remove;}
elsif ($in{'command'} eq "download") {&download;}
else  {&html;}
exit;

#---------------------------------------------------------------html

sub html {
	if ($pict) {$rport = $ENV{'REMOTE_PORT'}; $himicode .= "?$rport$facheck1";} #ヒミコード
	if ($html_mode eq 'go') {
		$view_res = $view_resh;
		$view_thr = $view_thrh;
		$changedef = $view_thr;
		$skin_html = $skin_htmlh;
		$in{'command'} = '';
		$in{'thrpass'} = '';
		$c_name = '';
		$c_mail = '';
		$c_url = '';
		$c_pass = '';
		$c_icon = '';
		$c_color = '';
		$thread_new = '';
		$thread_midoku = '';
	} else {
		if ($in{'command'} eq "read") {$skin_html = $thread_html;}
		if ($in{'page'}) {($in{'page'},$in{'def'},$in{'log'}) = split(/-/, $in{'page'});}
		if ($in{'def'}) {$changedef = $in{'def'};} else {$changedef = $view_thr;}
		if ($in{'log'} > 0) {$log_d = $in{'log'}.$kak_d;}
	}
	open(IN, $skin_html) || &error('スキンエラー');
	$skin = join('',<IN>);
	close(IN);
	open(LOG, "$logdir$filecount") || &error('ファイルエラー');
	$filecc = join('',<LOG>);
	close(LOG);
	open(IN,"$logdir$log_d") || &error('ファイルエラー');
	@lines = <IN>;
	close (IN);
	if ($in{'s_word'}) {&search;}
	if ($in{'sort'}) { #投票機能のアイデアbyぴぃロッキー氏
		if ($in{'sort'} eq 'color') {@lines = @{&csvsort(6,@lines);};}
		elsif ($in{'sort'} eq 'icon') {@lines = @{&csvsort(7,@lines);};}
		elsif ($in{'sort'} eq 'res') {@lines = @{&csvsort(8,@lines);};}
		elsif ($in{'sort'} eq 'date') {@lines = @{&csvsort(1,@lines);};}
		elsif ($in{'sort'} eq 'new') {@lines = @{&csvsort(0,@lines);};}
	}
	$last = @lines;
	if ($in{'page'} eq '') {
		$pagestart = 0;
	} else {
		$pagestart = $in{'page'};
		if ($pagestart < 0) {$pagestart = 0;}
		if ($pagestart > $last) {$pagestart = $last - $changedef;}
	}
	$pageend  = $pagestart + $changedef;
	if ($pageend > $last) {$pageend = $last;}
	$pagefirst = $pagestart + 1;
	$line_select = "<SELECT name=\"def\">\n";
	$line_select .= "<OPTION value=1 ";
	$line_select .= "SELECTED" if($changedef == 1);
	$line_select .= ">1\n";
	$line_select .= "<OPTION value=$view_thr ";
	$line_select .= "SELECTED" if($changedef == $view_thr);
	$line_select .= ">$view_thr\n";
	$line_select .= "<OPTION value=50 ";
	$line_select .= "SELECTED" if($changedef == 50);
	$line_select .= ">50\n";
	$line_select .= "<OPTION value=100 ";
	$line_select .= "SELECTED" if($changedef == 100);
	$line_select .= ">100\n";
	$line_select .= "</SELECT>\n";
	$skin =~ s/lines<!--s-->/$line_select/g;
	$log_select = "<SELECT name=\"log\">\n";
	$log_select .= "<OPTION value=\"0\">new\n";
	open(IN,"$logdir$kakono") || &error('ファイルエラー');
	my $in=<IN>;
	close(IN);
	($pastno,$since,$allmessages,$last_up_name,$last_up_date) = split(/<>/,$in);
	my ($allmessages,$threadnum) = split(/-/,$allmessages);
	while($pastno > 0){
		$pastselect="$pastno$kak_d";
		if ($log_d eq $pastselect){
			$log_select .= "<OPTION value=\"$pastno\" SELECTED>log $pastno\n";
		}else{
			$log_select .= "<OPTION value=\"$pastno\">log $pastno\n";
		}
		$pastno--;
	}
	$log_select .= "</SELECT>\n";
	$skin =~ s/logs<!--s-->/$log_select/g;
	$log_select = '';
	for (my $i = 1; $i < ($last / $changedef) + .9; $i++) {
		my $j = ($i - 1) * $changedef;
		if ($j == $pagestart) {$log_select .= qq| $i|;}
		else {$log_select .= qq| <a href="$reload?page=$j-$changedef-$in{'log'}">$i</A>|;}
	}
	$skin =~ s/page<!--s-->/$log_select/g;
	$skin =~ s/<FORM(.*?)write(.*?)<\/FORM>/<FORM$1write$2
<INPUT type="hidden" name="facheck1" value="facheck1<!--s-->">
<INPUT type="hidden" name="threno" value="threno<!--s-->">
<INPUT type="hidden" name="thrpass" value="thrpass<!--s-->">
<\/FORM>/sgi;
	$skin =~ s/reload<!--s-->/$reload/g; #このcgiのurl
	$skin =~ s/<a href="top">/<a href="$reload">/gi;
	$skin =~ s/home<!--s-->/$home/g; #戻り先url
	$skin =~ s/<a href="back_url">/<a href="$home">/gi;
	$skin =~ s/<a href="back">/<!--nextstart--><a href="$reload?page=$pageend-$changedef-$in{'log'}"><!--nextend-->/gi;
	$skin =~ s/next<!--s-->/$pageend-$changedef-$in{'log'}/g; #次のページ処理用パラメータ
	$skin =~ s/mtitle<!--s-->/$title/g; #タイトル
	$skin =~ s/count<!--s-->/$countt/g; #カウンタートータル
	$skin =~ s/count24<!--s-->/$count24/g; #カウンター24時間以内
	$skin =~ s/count5<!--s-->/$count5/g; #カウンター5分以内
	$skin =~ s/last<!--s-->/$last/g; #全スレッド数
	$skin =~ s/maxres<!--s-->/$max_res/g; #最大レス数
	$skin =~ s/maxthr<!--s-->/$max_thr/g; #最大スレッド数
	$skin =~ s/pagestart<!--s-->/$pagefirst/g; #表示されている最初のスレ番号
	$skin =~ s/pageend<!--s-->/$pageend/g; #表示されている最後のスレ番号
	$skin =~ s/(name="s_word")/$1 value="$in{s_word}"/gi; #検索語句
	$skin =~ s/(name="name")/$1 value="$c_name"/gi; #クッキー名前
	$skin =~ s/(name="mail")/$1 value="$c_mail"/gi; #クッキーメール
	$skin =~ s/(name="url")/$1 value="$c_url"/gi; #クッキーurl
	$skin =~ s/(name="pwd")/$1 value="$c_pass"/gi; #クッキーパスワード
	$skin =~ s/(name="icon".*?value="$c_icon")/$1 selected checked/gi; #クッキーアイコン 強引に select radio 両対応
	$skin =~ s/(name="color".*?value="$c_color")/$1 selected checked/gi; #クッキーカラー 問題があれば selected checked のどちらか消す
	$skin =~ s/himicode<!--s-->/$himicode/g; #himicode
	$skin =~ s/facheck3<!--s-->/$rport/g; #rport
	$skin =~ s/facheck1<!--s-->/$facheck1/g; #facheck1
	$skin =~ s/logno<!--s-->/$in{'log'}/g; #表示するログの選択用
	$skin =~ s/thrpass<!--s-->/$in{'thrpass'}/g; #スレッドパス
	$skin =~ s/since<!--s-->/$since/g; #最初のアクセス時間
	$skin =~ s/allmessages<!--s-->/$allmessages/g; #全メッセージ数
	$skin =~ s/lastupname<!--s-->/$last_up_name/g; #最終投稿者
	$skin =~ s/lastupdate<!--s-->/$last_up_date/g; #最終投稿時間

	if ($skin =~ m/<!--recentrystart-->(.*?)<!--recentryend-->/s) {$recentryhtml = $1;}
	$skin =~ s/<!--recentrystart-->.*?<!--recentryend-->/<!--recentry-->/sg;
	$i = 0;
	my $recentry_list;
	open(IN,"$logdir$recentry") || &error('ファイルエラー');
	my @recentry = <IN>;
	close(IN);
	foreach (@recentry) { #最近の投稿
		$recentrytmp = $recentryhtml;
		my ($newtime,$firsttime,$d,$d,$d,$d,$d,$d,$d,$subject,$resno,$date,$name,$message,$d,$d,$filename) = split(/<>/);
		$recentrytmp =~ s/threno<!--s-->/$firsttime/;
		$recentrytmp =~ s/date<!--s-->/$date/g;
		$recentrytmp =~ s/name<!--s-->/$name/;
		$recentrytmp =~ s/subject<!--s-->/$subject/;
		$recentrytmp =~ s/resno<!--s-->/$resno/;
		$message =~ s/<BR>//gi;
		$message = substr($message,0,70)." ･･･" if (length($message) > 70); # ← 70が文字数です。
		$recentrytmp =~ s/message<!--s-->/$message/;
		$recentry_list .= $recentrytmp;
	}
	$skin =~ s/<!--recentry-->/$recentry_list/g;
	if ($skin =~ m/<!--menustart-->(.*?)<!--menuend-->/s) {$menuhtml = $1;}
	$skin =~ s/<!--menustart-->.*?<!--menuend-->/<!--menu-->/sg;
	$i = 0;
	my $menu_list;
	while ($i < $last) { #スレッドリスト処理
		$menutmp = $menuhtml;
		($newtime,$firsttime,$threadtitle,$resno,$d,$d,$d,$d,$total_res) = split(/<>/, $lines[$i]);
		$i++;
		$menutmp =~ s/thrban<!--s-->/$i/g; #スレッド番号
		$menutmp =~ s/subject<!--s-->/<A href="$reload?command=read&log=$in{'log'}&no=$firsttime&ap=last">$threadtitle<\/A>/;
		if ($del_res_num) {$menutmp =~ s/resnum<!--s-->/$resno/;} #スレッドの全レス数
		else {my $resno2=$total_res+1; $menutmp =~ s/resnum<!--s-->/$resno2/;}
		if ($c_lastv && $c_lastv < $newtime) {$menutmp =~ s/midoku<!--s-->/$thread_midoku/;} #未読スレッド
		else {$menutmp =~ s/midoku<!--s-->//;}
		if ($time < $newtime + $new_hyouji) {$menutmp =~ s/new<!--s-->/$thread_new/;} #新着スレッド
		else {$menutmp =~ s/new<!--s-->//;}
		$menu_list .= $menutmp;
	}
	$skin =~ s/<!--menu-->/$menu_list/g;
	if ($pageend == $last) {$skin =~ s/<!--nextstart-->.*?<!--nextend-->//sg;} #最後のページならネクストボタンを消す
	if (!$pict) {$skin =~ s/<!--himicostart-->.*?<!--himicoend-->//sg;} #himicode
	if ($skin =~ m/<!--colorstart-->(.*?)<!--colorend-->/) {@color = split(/,/,$1);} #color
	$skin =~ s/<!--colorstart-->.*?<!--colorend-->//;
	if ($skin =~ m/<!--iconstart-->(.*?)<!--iconend-->/) {@icon = split(/,/,$1);} #icon
	$skin =~ s/<!--iconstart-->.*?<!--iconend-->//;
	($head_html,$mainhtml,$fot_html) = split(/<!--cut-->/, $skin);
	$head_html =~ s/threno<!--s-->/$in{'no'}/; #スレッド
	$fot_html =~ s/threno<!--s-->/$in{'no'}/; #スレッド
	my $secrethtml,$diaryhtml;
	if ($mainhtml =~ m/<!--secretstart-->(.*?)<!--secretend-->/s) {$secrethtml = $1;} #パスワード入室制限スレッド
	$mainhtml =~ s/<!--secretstart-->.*?<!--secretend-->//s;
	if ($mainhtml =~ m/<!--diarystart-->(.*?)<!--diaryend-->/s) {$diaryhtml = $1;} #パスワード投稿制限スレッド
	$mainhtml =~ s/<!--diarystart-->.*?<!--diaryend-->//s;
	if ($mainhtml =~ m/<!--resstart-->(.*?)<!--resend-->/s) {$reshtml = $1;}
	$mainhtml =~ s/<!--resstart-->.*?<!--resend-->/<!--res-->/s;
	if ($secrethtml =~ m/<!--resstart-->(.*?)<!--resend-->/si) {$secretreshtml = $1;}
	$secrethtml =~ s/<!--resstart-->.*?<!--resend-->/<!--res-->/s;
	if ($diaryhtml =~ m/<!--resstart-->(.*?)<!--resend-->/si) {$diaryreshtml = $1;}
	$diaryhtml =~ s/<!--resstart-->.*?<!--resend-->/<!--res-->/s;
	if ($html_mode eq 'go') {
		if (!open(OUT, ">$html_file")) {&error('ファイルエラー');}
		print OUT $head_html;
	} else {
		print "Content-type: text/html\n\n";
		print $head_html;
	}
	if ($in{'command'} eq "read") {$pagestart = 0; $pageend = $last;}
	while ($pagestart < $pageend) {
		($newtime,$firsttime,$thrtitle,$resno,$thrmode,$thrpass,$total_color,$total_icon,$total_res,
		$subject,$logno,$date_l,$name_l,$message_l,$spwd_l,$delete_l,$filename_l,$mail_l,$url_l,
		$how_l,$link_l,$addr_l,$timek_l,$user_l,$color_l,$icon_l,$w,$h,$mesnum) = split(/<>/, $lines[$pagestart]);
		($mesnum,$thread_num) = split(/-/,$mesnum);
		$pagestart++;
		if ($in{'command'} eq "read" && $firsttime ne $in{'no'}) {next;}
		if ($in{'pwd'} eq $pass || $in{'thrpass'} eq $pass) {$maintmp = $mainhtml;} else {
			if ($in{'command'} eq "read" && $in{'thrpass'} ne '' && $in{'thrpass'} ne $thrpass) {&error('パスワードが違います');}
			if ($thrmode == 2 && $in{'thrpass'} ne $thrpass) {$maintmp = $secrethtml;} #secret
			elsif ($thrmode == 3 && $in{'thrpass'} ne $thrpass) {$maintmp = $diaryhtml;} #diary
			else {$maintmp = $mainhtml;}
		}
		$pagestartdawn = $pagestart+1;
		if ($pagestartdawn > $pageend) {$pagestartdawn = $pagefirst;}
		$pagestartup = $pagestart-1;
		if ($pagestartup < $pagefirst) {$pagestartup = $pageend;}
		if ($delete_l) {
			$name_l = $del_name if ($del_name ne '1');
			$message_l = $del_mess if ($del_mes ne '1');
			$date_l = $del_date if ($del_date ne '1');
			$filename_l = $del_filename if ($del_filename ne '1');
			$spwd_l = $del_spwd if ($del_spwd ne '1');
			$mail_l = $del_mail if ($del_mail ne '1');
			$url_l = $del_url if ($del_url ne '1');
			$addr_l = $del_addr if ($del_addr ne '1');
			$timek_l = $del_time if ($del_time ne '1');
			$user_l = $del_user if ($del_user ne '1');
			$maintmp =~ s/color<!--s-->/$del_color/g if ($del_color ne '1');
			$maintmp =~ s/icon<!--s-->/$del_icon/g if ($del_icon ne '1');
		}
		if ($icon_path) {$maintmp =~ s/color<!--s-->/$color_l/g;}
		else {
			if ($color[$color_l] eq '') {$color_l = 0;}
			$maintmp =~ s/color<!--s-->/$color[$color_l]/g; #color
		}
		if ($icon_path) {$maintmp =~ s/icon<!--s-->/<img src=$icon_path$icon_l\.$icon_format>/g;}
		else {
			if ($icon[$icon_l] eq '') {$icon_l = 0;}
			$maintmp =~ s/icon<!--s-->/$icon[$icon_l]/g; #icon
		}
		if ($mail_l) {
			if ($mail_image01 eq '') {$mail_image01 = $mail_l;}
			$mail_l = "<A href=\"mailto:$mail_l\">$mail_image01</A>";
		} else {
			$mail_l = $mail_image02;
		}
		$maintmp =~ s/mail<!--s-->/$mail_l/g; # mail
		if ($url_l) {
			if ($url_image01 eq '') {$url_image01 = $url_l;}
			$url_l = "<A href=\"$url_l\" target=\"_blank\">$url_image01</A>";
		} else {
			$url_l = $url_image02;
		}
		$maintmp =~ s/url<!--s-->/$url_l/g; # url
		if ($filename_l) { # file
			my $size = int((-s "$upurl$filename_l")/1024).'kb';
			my $extension = $filename_l;
			$extension =~ s/.+\.(\w)/$1/;
			if ($filecc =~ /$filename_l/) {
				if ($down_rate eq '0') {
					$file_image01 = $file{"$extension"};
					$maintmp =~ s/file<!--s-->/<A href=\"$upurl$filename_l\" target=\"_blank\">$file_image01<\/A>/g;
				} else {
					$file_image01 = $file{"$extension"};
					$maintmp =~ s/file<!--s-->/$file_image01/g;
				}
			} else {
				$maintmp =~ s/file<!--s-->/$file_image03/g;
			}
			if ($down_rate eq '0' && $filename_l =~ /gif$|jpg$|png$/ && $filecc =~ /$filename_l/) {
				$maintmp =~ s/image<!--s-->/<a href=\"$upurl$filename_l\" target=\"_blank\"><img src=\"$upurl$filename_l\" width=$w height=$h border=0 $align><\/a>/g;
			} else {
				$maintmp =~ s/image<!--s-->//g;
			}
			$maintmp =~ s/size<!--s-->/$size/g;
			$maintmp =~ s/extension<!--s-->/$extension/g;
		} else {
			$maintmp =~ s/size<!--s-->//g;
			$maintmp =~ s/extension<!--s-->//g;
			$maintmp =~ s/image<!--s-->//g;
			$maintmp =~ s/file<!--s-->/$file_image02/g;
		}
		$maintmp =~ s/colortotal<!--s-->/$total_color/g; #カラー合計
		$maintmp =~ s/icontotal<!--s-->/$total_icon/g; #アイコン合計
		$maintmp =~ s/restotal<!--s-->/$total_res/g; #res合計
		$maintmp =~ s/subject<!--s-->/$thrtitle/g; #スレッドタイトル
		$maintmp =~ s/resinput/$reload?command=read&log=$in{'log'}&no=$firsttime&ap=last/g; #レス画面呼び出し用リンク
		$maintmp =~ s/thrban<!--s-->/$pagestart/g; #スレッド番号
		$maintmp =~ s/idnum<!--s-->/$thread_num/g; #スレッド番号(逆順)
		if ($del_res_num) {$maintmp =~ s/resnum<!--s-->/$resno/g;} #スレッドの全レス数
		else {my $resno2=$total_res+1; $maintmp =~ s/resnum<!--s-->/$resno2/g;}
		$maintmp =~ s/pageup<!--s-->/$pagestartup/g; #前のスレッド番号(▲リンク用)
		$maintmp =~ s/pagedown<!--s-->/$pagestartdawn/g; #次のスレッド番号(▼リンク用)
		$maintmp =~ s/resno<!--s-->/$logno/g; #スレッド内のレス番号
		$maintmp =~ s/threno<!--s-->/$firsttime/g; #スレッド呼出用(スレッドナンバー)
		$maintmp =~ s/name<!--s-->/$name_l/g; #名前
		$maintmp =~ s/date<!--s-->/$date_l/g; #日付
		$addr_l =~ s/\d/\*/g; #完全に表示するなら削除
		$maintmp =~ s/addr<!--s-->/$addr_l/g; #リモートホスト(IP)
		$maintmp =~ s/mesnum<!--s-->/$mesnum/g; #メッセージナンバー(全て通しの投稿番号)
		$maintmp =~ s/time<!--s-->/$timek_l/g; #書き込み所要時間
		$maintmp =~ s/user<!--s-->/$user_l/g; #ユーザーエージェント
		$maintmp =~ s/(name="target")/$1 value="$firsttime-$logno"/g; #削除用
		$spwd_l = substr $spwd_l,-7;
		$spwd_l = '???' if ($spwd_l eq '') ;
		$maintmp =~ s/id<!--s-->/ID:$spwd_l/g; # 認識ＩＤの表示
		if ($how_l) {
			$message_l =~ s/<BR>/\n/g;
			$maintmp =~ s/message<!--s-->/<PRE>$message_l<\/PRE>/g;
		} else {
			&conv;
			$maintmp =~ s/message<!--s-->/$message_l/g;
		}
		$all_res = "";
		if ($max_res && $resno >1) {
			open(IN,"$logdir$firsttime$res_d") || &error('ファイルエラー');
			@lines_l = <IN>;
			close (IN);
			if ($in{'command'} eq 'read') {
				if ($in{'ap'}) {
					($where,$howhow) = split(/-/, $in{'ap'});
					if ($where eq 'first') {if ($howhow eq '') {$howhow = $view_res2;} $start = 0; $end = $howhow;}
					elsif ($where eq 'last') {if ($howhow eq '') {$howhow = $view_res2;} $end  = @lines_l; $start = $end - $howhow;}
					elsif ($where eq 'prev') {$start = $howhow - $view_res2; $end = $howhow;}
					elsif ($where eq 'next') {$start = $howhow; $end = $howhow + $view_res2;}
					elsif ($where =~ /\d+/) {$start = $where - 2; if ($howhow =~ /\d+/) {$end = $howhow - 1;} else {$end = $where - 1;}}
				} else {
					if ($imod) {$end = @lines_l; $start = $end - $view_res2;} else {$start = 0; $end  = @lines_l;}
				}
			} else {
				$end  = @lines_l;
				$start = $end - $view_res;
			}
			if ($end > @lines_l) {$end = @lines_l;}
			if ($start < 0) {$start = 0;}
			if ($start == 0) {$maintmp =~ s/<!--prevresstart-->.*?<!--prevresend-->//sg;} #最後のページならネクストボタンを消す
			else {$maintmp =~ s/prev-nextres<!--s-->/prev-$start/g;} #前のページ
			if ($end == @lines_l) {$maintmp =~ s/<!--nextresstart-->.*?<!--nextresend-->//sg;} #最後のページならネクストボタンを消す
			else {$maintmp =~ s/next-nextres<!--s-->/next-$end/g;} #次のページ
			while ($start < $end) {
				if ($thrmode == 2 && $in{'thrpass'} ne $thrpass) {$restmp = $secretreshtml;} #secret
				elsif ($thrmode == 3 && $in{'thrpass'} ne $thrpass) {$restmp = $diaryreshtml;} #diary
				else {$restmp = $reshtml;}
				($subject_l,$logno,$date_l,$name_l,$message_l,$spwd_l,$delete_l,$filename_l,$mail_l,$url_l,
				$how_l,$link_l,$addr_l,$timek_l,$user_l,$color_l,$icon_l,$w,$h,$mesnum) = split(/<>/, $lines_l[$start]);
				$start++;
				if ($delete_l) {
					$name_l = $del_name;
					$subject_l = $del_titl;
					$message_l = $del_mess;
					$date_l = $del_date;
					$filename_l = $del_filename if ($del_filename ne '1');
					$spwd_l = $del_spwd if ($del_spwd ne '1');
					$mail_l = $del_mail if ($del_mail ne '1');
					$url_l = $del_url if ($del_url ne '1');
					$addr_l = $del_addr if ($del_addr ne '1');
					$timek_l = $del_time if ($del_time ne '1');
					$user_l = $del_user if ($del_user ne '1');
					$maintmp =~ s/color<!--s-->/$del_color/g if ($del_color ne '1');
					$maintmp =~ s/icon<!--s-->/$del_icon/g if ($del_icon ne '1');
				}
				if ($icon_path) {$restmp =~ s/(res)?color<!--s-->/$color_l/g;}
				else {
					if ($color[$color_l] eq '') {$color_l = 0;}
					$restmp =~ s/(res)?color<!--s-->/$color[$color_l]/g; #color
				}
				if ($icon_path) {$restmp =~ s/(res)?icon<!--s-->/<img src=$icon_path$icon_l\.$icon_format>/g;}
				else {
					if ($icon[$icon_l] eq '') {$icon_l = 0;}
					$restmp =~ s/(res)?icon<!--s-->/$icon[$icon_l]/g; #icon
				}
				if ($mail_l) {
					if ($res_mail_image01 eq '') {$res_mail_image01 = $mail_l;}
					$mail_l = "<A href=\"mailto:$mail_l\">$res_mail_image01</A>";
				} else {
					$mail_l = $res_mail_image02;
				}
				$restmp =~ s/(res)?mail<!--s-->/$mail_l/g; # mail
				if ($url_l) {
					if ($res_url_image01 eq '') {$res_url_image01 = $url_l;}
					$url_l = "<A href=\"$url_l\" target=\"_blank\">$res_url_image01</A>";
				} else {
					$url_l = $res_url_image02;
				}
				$restmp =~ s/(res)?url<!--s-->/$url_l/g; # url
				if ($filename_l) { # file
					my $size = int((-s "$upurl$filename_l")/1024).'kb';
					my $extension = $filename_l;
					$extension =~ s/.+\.(\w)/$1/;
					if ($filecc =~ /$filename_l/) {
						if ($down_rate eq '0') {
							$file_image01 = $file{"$extension"};
							$restmp =~ s/(res)?file<!--s-->/<A href=\"$upurl$filename_l\" target=\"_blank\">$file_image01<\/A>/g;
						} else {
							$file_image01 = $file{"$extension"};
							$restmp =~ s/(res)?file<!--s-->/$file_image01/g;
						}
					} else {
						$restmp =~ s/(res)?file<!--s-->/$file_image03/g;
					}
					if ($down_rate eq '0' && $filename_l =~ /gif$|jpg$|png$/ && $filecc =~ /$filename_l/) {
						$restmp =~ s/(res)?image<!--s-->/<a href=\"$upurl$filename_l\" target=\"_blank\"><img src=\"$upurl$filename_l\" width=$w height=$h border=0 $align><\/a>/g;
					} else {
						$restmp =~ s/(res)?image<!--s-->//g;
					}
					$restmp =~ s/(res)?size<!--s-->/$size/g;
					$restmp =~ s/(res)?extension<!--s-->/$extension/g;
				} else {
					$restmp =~ s/(res)?size<!--s-->//g;
					$restmp =~ s/(res)?extension<!--s-->//g;
					$restmp =~ s/(res)?image<!--s-->//g;
					$restmp =~ s/(res)?file<!--s-->/$file_image02/g;
				}
				$restmp =~ s/threno<!--s-->/$firsttime/g; #スレッド呼出用
				if ($del_res_num) {$restmp =~ s/resno<!--s-->/$logno/g;} #レス番号
				else {my $logno2=$start+1; $restmp =~ s/resno<!--s-->/$logno2/g;}
				$restmp =~ s/(res)?subject<!--s-->/$subject_l/g; #レスタイトル
				$restmp =~ s/(res)?name<!--s-->/$name_l/g; #名前
				$restmp =~ s/(res)?date<!--s-->/$date_l/g; #日付
				$addr_l =~ s/\d/\*/g; #完全に表示するなら削除
				$restmp =~ s/(res)?addr<!--s-->/$addr_l/g; #リモートホスト(IP)
				$restmp =~ s/(res)?mesnum<!--s-->/$mesnum/g; #メッセージナンバー(全て通しの投稿番号)
				$restmp =~ s/(res)?time<!--s-->/$timek_l/g; #書き込み所要時間
				$restmp =~ s/(res)?user<!--s-->/$user_l/g; #ユーザーエージェント
				$restmp =~ s/(name="target")/$1 value="$firsttime-$logno"/g; #削除用
				$spwd_l = substr $spwd_l,-7;
				$spwd_l = '???' if ($spwd_l eq '') ;
				$restmp =~ s/(res)?id<!--s-->/ID:$spwd_l/g; # 認識ＩＤの表示
				if ($how_l) {
					$message_l =~ s/<BR>/\n/g;
					$restmp =~ s/(res)?message<!--s-->/<PRE>$message_l<\/PRE>/g;
				} else {
					&conv;
					$restmp =~ s/(res)?message<!--s-->/$message_l/g;
				}
				$all_res .= $restmp;
			}
		} else {
			$maintmp =~ s/<!--prevresstart-->.*?<!--prevresend-->//sg;
			$maintmp =~ s/<!--nextresstart-->.*?<!--nextresend-->//sg;
		}
		if ($del_res_num) {$resno2 = $resno;} else {$resno2 = $total_res + 1;}
		if ($max_res && $resno2 >= $max_res) { # レス拒否表示
			$maintmp =~ s/over<!--s-->/$over/g;
		} else {
			$maintmp =~ s/over<!--s-->//g;
		}
		$maintmp =~ s/<!--res-->/$all_res/;
		if ($html_mode eq 'go') {print OUT $maintmp;} else {print $maintmp;}
	}
	if ($html_mode eq 'go') {
		print OUT $fot_html;
		close (OUT);
	} else {
		print $fot_html;
		print "<!-- sample A -->";
#		$CPU_end = (times)[0]; printf("<DIV align=right>消費時間： %.3f CPU秒</DIV>\n",$CPU_end-$CPU_start);
	}
}

#---------------------------------------------------------------search

sub search {
	if (!$search) {&error('検索機能オフに設定されています。');}
	$in{s_word} =~ s/　/ /gi;
	if (length($in{'s_word'}) > 120) {&error('検索キーワードが制限文字数を超えています。');}
	my @keywords = split(/ /, $in{s_word});
	if(@keywords > 5) {&error("検索キーワードは5個までです。");}
	my @news = ();
	foreach (@lines) {
		my ($flag) = 0;
		my ($d,$firsttime,$d,$resno,$d,$d,$d,$d,$d,$subject_l,$d,$d,$name_l,$message_l) = split(/<>/);
		my $word = $subject_l.$name_l.$message_l;
		foreach $keyword (@keywords) {
			if (index($word, $keyword) >= 0) {
				$flag = 1;
				if ($in{s_type} == 0) { last; }
			} else {
				if ($in{s_type} != 0) { $flag = 0; last; }
			}
		}
		if ($flag == 0 && $max_res && $resno >1) {
			open(IN,"$logdir$firsttime$res_d") || &error('検索ファイルエラー');
			my @lines_l = <IN>;
			close (IN);
			foreach $res (@lines_l) {
				my ($subject_l,$d,$d,$name_l,$message_l) = split(/<>/, $res);
				my $word = $subject_l.$name_l.$message_l;
				foreach $keyword (@keywords) {
					if (index($word, $keyword) >= 0) {
						$flag = 1;
						if ($in{s_type} == 0) { last; }
					} else {
						if ($in{s_type} != 0) { $flag = 0; last; }
					}
				}
				if ($flag) {last;}
			}
		}
		if ($flag) { push (@news, $_); }
	}
	@lines = @news;
}

#---------------------------------------------------------------remove

sub remove {
	if ($proxycheck >= 3 && $proxy) {&error('現在プロクシ制限中です。');}
	if ($in{'target'}) {
		($in{'threno'},$in{'resno'}) = split(/-/, $in{'target'});
	} else {
		&error('削除する記事が指定されていません');
	}
	if (!$in{'dpwd'}) {$in{'dpwd'} = $in{'pwd'};}
	if (!$in{'dpwd'}) {&error('削除パスワードが入力されていません');}
	my $delete_flag = 1;
	if ($delete_type == 3) {$delete_flag = &delete1($in{'dpwd'},0,$in{'threno'},$in{'resno'});}
	elsif ($delete_type == 1) {$delete_flag = &delete2($in{'dpwd'},0,$in{'threno'},$in{'resno'});}
	elsif ($in{'resno'} ==1 ) {$delete_flag = &delete2($in{'dpwd'},0,$in{'threno'},$in{'resno'});}
	else {$delete_flag = &delete1($in{'dpwd'},0,$in{'threno'},$in{'resno'});}
	if ($delete_flag) {&error('削除パスが違います');}
	if ($ipget && $proxy) {require './telget.cgi'; &telget;}
	&html;
	if ($skin_htmlh) {
		$html_mode = 'go';
		&html;
	}
	exit;
}

#---------------------------------------------------------------download

sub download {
	if ($in{'target'}) {
		($in{'threno'},$in{'resno'}) = split(/-/, $in{'target'});
	} else {
		&error('ダウンロードするファイルが指定されていません');
	}
	if (!$in{'dpwd'}) {$in{'dpwd'} = $in{'pwd'};}
	if (!$in{'dpwd'}) {&error('パスワードが入力されていません');}
	if (!$in{'name'}) {&error('名前が入力されていません');}
	if ($in{'name'} =~ /(.*?)#(.+)/) {$in{'name'} = $1;}
	open(LOG,"+<$logdir$up_count") || &error('ファイルエラー');
	eval {flock(LOG,2);};
	my @lines=<LOG>;
	my $flag = 1;
	foreach (@lines) {
		my ($name,$pswd,$dtime,$rtime,$cap,$last_post,$private_icon) = split(/<>/);
		if ($name eq $in{'name'} && crypt($in{'dpwd'}, $pswd) eq $pswd) {
			$flag = 0;
			$dtime--;
			if ($dtime < 0) {$flag = 1; $dtime = 0;}
			$_ = "$name<>$pswd<>$dtime<>$rtime<>$cap<>$last_post<>$private_icon<><><>\n";
			last;
		}
	}
	seek(LOG, 0, 0);
	print LOG @lines;
	truncate(LOG, tell(LOG));
	close(LOG);
	if ($flag) {&error('ダウンロードするには、なにかアップロードして下さい');}
	if ($in{'resno'} == 1) {
		open(IN,"$logdir$log_d") || &error('ファイルエラー');
		my @lines = <IN>;
		close (IN);
		foreach (@lines) {
			my ($d,$firsttime,$d,$d,$d,$d,$d,$d,$d,$d,$d,$d,$d,$d,$d,$d,$d6) = split(/<>/);
			if ($in{'threno'} eq $firsttime) {
				$filename = $d6;
			}
		}
	} else {
		open(IN,"$logdir$in{'threno'}$res_d") || &error('ファイルエラー');
		my @lines = <IN>;
		close (IN);
		foreach (@lines) {
			my ($d,$resno_l,$d,$d,$d,$d,$d,$d6) = split(/<>/);
			if ($in{'resno'} eq $resno_l) {
				$filename = $d6;
			}
		}
	}
	if (!$filename) {&error('ファイルがありません');}
	print "Content-type: text/html\n\n";
	print qq|<HTML><HEAD><TITLE>$title</TITLE>|;
	print qq|<META http-equiv="Refresh" CONTENT="0;url=$upurl$filename">|;
	print qq|</HEAD><BODY><A href="$upurl$filename">$filename</A></BODY></HTML>|;
}

#---------------------------------------------------------------error

sub error {
	$c_error++;
	&set_cookie;
	open(ER, "+<$logdir$errorlog");
	eval {flock(ER, 2);};
	my @clines = <ER>;
	my $kiroku_data = "$date<>$time<>$addr<>$_[0]<>$in{'pasad'}<>$in{'thrpass'}<>$in{'dpwd'}<>$host<>$user<>$in{'name'}<>$in{'message'}<>";
	$kiroku_data = substr($kiroku_data,0,1000)."\n";
	unshift(@clines, "$kiroku_data");
	pop @clines while @clines > $alcue;
	seek(ER, 0, 0);
	print ER @clines;
	truncate(ER, tell(ER));
	close(ER);
	print "Content-type: text/html\n\n";
	print qq|<HTML><HEAD><TITLE>error !!</TITLE>|;
	print qq|<META http-equiv="Content-Type" content="text/html; charset=Shift_JIS">|;
	print qq|</HEAD><BODY><BR><FONT size="+1" color="cc3333"><B>$_[0]</B></FONT><BR>|;
	if($in{'message'} ne ''){
		$in{'message'} =~ s/<BR>/\n/gi;
		print qq|<P>[下記は投稿内容のバックアップ]|;
		print qq|[<A href="$reload?error" target="_blank">別ウィンドウで表\示</A>]|;
		print qq|<FORM><TEXTAREA name="repost" rows="6" cols="64">$in{'message'}</TEXTAREA></FORM>|;
	}
	print qq|</BODY></HTML>|;
	exit;
}

#---------------------------------------------------------------count

sub count {
	&cookie if ($cookiename);
	$proxy = 0;
	&proxy_check if ($proxycheck >= 1 && $imod != 1);
	&host_check	if ($proxycheck >= 1);
	if ($proxy) {$host .= "(proxy)";}
	if ($proxycheck >= 4 && $proxy) {&error('現在プロクシ制限中です。');}
	open(DB,"$logdir$mainte") || &error('ファイルエラー');
	$mainte_mode = <DB>;
	close(DB);
	if ($mainte_mode > 2) {&error('メンテナンス中。アクセスできません。');}
	&access_log_r;
	&make_code;
}
