#!/usr/bin/perl

;# -------------------------------------------------------------------
;# Copyright
;#
;#		Script   :  Mini Thread
;#		Homepage :  Flash CGI
;#		URL      :  http://www.flashcgi.net/
;#		Mail     :  webmaster@flashcgi.net
;#		Type     :  Free Ware(May Convert)
;# -------------------------------------------------------------------
;#
;# 設定ファイル名
;; require 'config.cgi';
;#
;; &main();
;#
;# 初期ページ
;#
sub main {
	#
	# データ多すぎ
	#
	&error(__LINE__,'???') if (length($ENV{'QUERY_STRING'}) > $sobig);
	#
	# 変数代入
	#
	my ($bbs,$page) = split(/&/,$ENV{'QUERY_STRING'});
	my ($cnt,%BBS) = &allBbsList();
	my $plus = $pluspath.$bbs.'/';
	my $read = $readcgi.'/'.$bbs;
	#
	# パス設定
	#
	if (&checkBbs($bbs)) {
		&readsubset($pluspath.$bbs.'/'.$subsetcgi);
	} else {
		&error(__LINE__,'板が見つかりません');
	}
	#
	# フォームチェック
	#
	if ($page and $page =~ /\D/) {
		&error(__LINE__,'不正なデータです。<br>数値以外が含まれています');
	}
	#
	# データファイルから記事を読みこみます
	#
	open(DATA,$plus.$savefile) or &error(__LINE__,$plus.$savefile.'近辺をチェックしてください');
	#
	# スレッド数
	#
	my $threadcount;
	++$threadcount while(<DATA>);
	my $last = $threadcount > $pagemax ? $pagemax : $threadcount;
	seek(DATA,0,0);
	#
	# 変なデータを受信した場合
	#
	if ($page >= $threadcount) {
		$page = $threadcount - $pagemax;
	}
	#
	# 先頭
	#
	if ($page eq '' or $page < 0) {
		$page = 0;
	}
	#
	# 圧縮
	#
	&encode();
print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=x-sjis">
$main_style
<title>$board_name</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
cka = new Array();
function stck(r,cks,i){
with(r){cka[0]=FROM.value;cka[1]=mail.value;cka[2]=home.value;cka[3]=pass.value;}
exp=new Date();exp.setTime(exp.getTime()+1000*60*60*24*1);
cks=escape(cka[0]);for(i=1;i<4;i++){cks+="%00"+escape(cka[i]);}
document.cookie="NMHP="+cks+";$path expires="+exp.toGMTString();
}
function gtck(){
var i=0,cks;window.onerror = null;
cka=document.cookie.split("; ");
while(cka[i]){
if(cka[i].substr(0,5) == "NMHP="){cks=cka[i].substr(5,cka[i].length);break;}
i++;}
if(!cks){return;}cka=cks.split("%00");
with(document)for(i=1;i<forms.length;i++)if(forms[i].FROM&&forms[i].mail&&forms[i].home&&forms[i].pass)
with(forms[i]){FROM.value=unescape(cka[0]);mail.value=unescape(cka[1]);home.value=unescape(cka[2]);pass.value=unescape(cka[3]);}
}
onload=gtck;
//-->
</SCRIPT>
</head>
<body background="$main_back" bgcolor="$main_bgc" text="$main_text" link="$main_link" vlink="$main_vlink" alink="$main_alink">
EOF
	print $title;
	print '<table border=0 width="'.$main_width.'" cellspacing=0 cellpadding=0 bgcolor="'.$formborder.'" align=center><tr><td>';
	print '<table border=0 width=100% cellspacing=1 cellpadding=5><tr><td bgcolor="'.$formtable.'" colspan=3>';
	print $formcom;
	print 'ご利用ありがとうございました。引き続き<a href="http://rsr.wess.co.jp/2006/community/">こちら</a>でお楽しみ下さい。<br>';
	print '<img src="../cube.gif">スレッドを作成する ';
	print '<small>';
	print '&nbsp;&nbsp;<img src="../cube.gif"><a href="'.$read.'/past/" target="'.$pasttg.'">過去ログ</a> ';
	print '&nbsp;&nbsp;<img src="../cube.gif"><a href="'.$plus.$celldir.$cellindex.'">i-mode</a> ' if ($usecell);
	print '&nbsp;&nbsp;<img src="../cube.gif"><a href="'.$returnpath.$searchcgi.'?b='.$bbs.'" target="'.$searchtg.'">全検索</a> ';
	print '&nbsp;&nbsp;<img src="../cube.gif"><a href="'.$plus.$indexfile.'">戻る</a> ';
	print '</small></td></tr>';
	#
	# 板一覧
	#
	if ($allboard) {
		my $i = 0;
		print '<tr bgcolor="'.$formbottom.'"><td colspan=3><small>';
		while($i < $cnt) {
			my @TEMP = split(/<>/,$BBS{$i});
			print '<a href="../'.$TEMP[0].'/'.$indexfile.'">'.$TEMP[1].'</a> ' if (!$TEMP[2]);
			print '【'.$TEMP[1].'】 ' if ($TEMP[2] == 2);
			++$i;
		}
		close(BBS);
		print '</small></td></tr>';
	}
	#
	# スレッドが無ければ飛ばす
	#
	if (!$threadcount) {
		close(DATA);
		print '</table></td></tr></table>';
		&copyright;
	}
	#
	# タイトル一覧数
	#
	my $view = $threadcount > $titlemax ? $titlemax : $threadcount;
	#
	# 整形表示対応
	#
	my ($fst,$snd,$cols,$pause);
	if ($arrange) {
		if ($view <= 2) {
			$cols = ' valign=top colspan=3';
		} else {
			my $no = $view/3;
			$fst = int($no) == $no ? $no : int($no+1);
			$no *= 2;
			$snd = int($no) == $no ? $no : int($no+1);
			$cols = ' valign=top';
		}
		$pause = '<br>';
	} else {
		$cols = ' colspan=3';
		$pause = ' / ';
	}
	#
	# タイトル一覧先頭
	#
	#print '<tr bgcolor="'.$formbottom.'"><td'.$cols.'><a name=menu><small>' if ($view);
	print '<tr bgcolor="#3D382C"><td'.$cols.'><a name=menu><small>' if ($view);
	my ($i,$an);
	while(<DATA>) {
		my @SUB = split(/<>/);
		my $key = $SUB[0]+0;
		++$i;
		chop $SUB[1];
		if ($i > $page and $i <= ($page+$pagemax)) {
			++$anc;
			print '<a href="'.$read.'/'.$key.'/l'.$newest.'" target="'.$titletg.'">'.$i.'</a>: ';
			print '<a href=#'.$anc.'>';
		} else {
			print '<a href="'.$read.'/'.$key.'/l'.$newest.'" target="'.$titletg.'">'.$i.': ';
		}
		print $SUB[1].'</a>'.$pause;
		if ($i == $fst or $i == $snd) {
			print '</small></td><td valign=top><small>' if ($arrange);
		}
		last if ($i >= $view);
	}
	print '</small><tr><td bgcolor="'.$formbottom.'" align=right colspan=3><small>';
	#
	# ページ処理
	#
	if ($page > 0) {
		my $back = $page-$pagemax > 0 ? $page-$pagemax : 0 ;
		if ($back == 0) {
			print '<a href="'.$plus.$indexfile.'">前のページ</a> ';
		} else {
			print '<a href="'.$pagecgi.'?'.$bbs.'&'.$back.'">前のページ</a> ';
		}
	}
	if ($page+$pagemax < $threadcount) {
		my $next = $page+$pagemax;
		print '<a href="'.$pagecgi.'?'.$bbs.'&'.$next.'">次のページ</a> ';
	}
	my $lst = ($threadcount-1)/$pagemax;
	my $i;
	while ($i <= $lst) {
		my $temp = $i*$pagemax;
		++$i;
		if ($temp == $page) {
			print $i.' ';
		} elsif ($i == 1) {
			print '<a href="'.$plus.$indexfile.'">'.$i.'</a> ';
		} else {
			print '<a href="'.$paging.'?'.$bbs.'&'.$temp.'">'.$i.'</a> ';
		}
	}
	#
	# タイトル一覧末端
	#
	print '<a href="'.$plus.$subback.'" target="'.$viewtg.'">';
	print 'スレッド一覧['.$threadcount.']</a></small>';
	print '</td></tr></table></td></tr></table><br>';
	#
	# 広告位置
	#
	print $banner;
	#
	# とばす
	#
	my $i;
	seek(DATA,0,0);
	<DATA> while($i++ < $page);
	#
	# 表示開始
	#
	my $i;
	while (<DATA>) {
		#
		# ログファイル名指定
		#
		last if (++$i > $pagemax);
		my @SUB = split(/<>/);
		my $key = $SUB[0]+0;
		my $file = $plus.$savedir.$SUB[0];
		my $resno = 1;
		#
		# リンク用
		#
		my $d = $i - 1;
		my $c = $i + 1;
		if ($c > $pagemax) {
			$c = 0;
		}
		#
		# 読み込み
		#
		my $count;
		open (LOG,$file) or &printingerror(__LINE__,$file.'を開けませんでした',);
		$count++ while(<LOG>);
		seek(LOG,0,0);
		#
		# タイトル用
		#
		my @TEMP = split(/<>/,scalar(<LOG>));
		#
		# ストップ判断用
		#
		my $stop = $TEMP[9];
		my $j = $page + $i;
		#
		# 記事表示部
		#
		print '<form action="'.$mainscript.'" method=post>';
		print '<input type=hidden name=key value='.$key.'>';
		print '<input type=hidden name=bbs value="'.$bbs.'">';
		print '<table border=0 width="'.$main_width.'" cellspacing=0 cellpadding=1 bgcolor="'.$threadborder.'" align=center><tr><td>';
		print '<table width=100% border=0 bgcolor="'.$threadtable.'" cellspacing=0 cellpadding=5><tr><td>';
		print '<dl><a name='.$i.'></a><div align=right>';
		print '<a href="'.$readcgi.'/'.$bbs.'/'.$key.'/" target="'.$readtotg.'">全部読む</a> ';
		print '<a href="'.$pagecgi.'?'.$ENV{'QUERY_STRING'}.'">リロード</a> ';
		print '</div><b>【'.$j.':'.$count.'】<font size="'.$main_subj_font.'" color="'.$main_subj_clr.'">'.$TEMP[4].'</font></b>';
		#
		# データを指定した形式に変換
		#
		&convertData(\@TEMP,'return'=>$pluspath,'link'=>$read.'/'.$key.'/','resno'=>$resno,'color'=>$main_nameclr,'count'=>$count,'omit' => 1);
		#
		# ログ数チェック
		#
		my $v = $count > $viewres ? $count - $viewres : 0;
		$resno += $v;
		#
		# 早送り
		#
		while ($v--) {
			<LOG>;
		}
		#
		# ログ表示
		#
		while(<LOG>) {
			++$resno;
			my @TEMP = split(/<>/);
			print '<br><br>';
			#
			# Mail/URL/DATE処理
			#
			&convertData(\@TEMP,'return'=>$pluspath,'link'=>$read.'/'.$key.'/','resno'=>$resno,'color'=>$main_nameclr,'count'=>$count,'omit' => 1);
		}
		close(LOG);
		#
		# レスフォーム
		#
		print '</dl>';
		#
		# スレッドストップのチェック
		# ファイルサイズとレス数のチェック
		#
		my $volume = -s $file;
		$volume = int($volume/1024)+1;
		if ($stop) {
			print '<b><font size=2 color="'.$alertclr.'">このスレッドは停止されています。</font></b>';
		} else {
			&cv_check($count,$volume);
		}
		#print '<select name=action>';
		#print '<option value=delshow selected>削除';
		#print '<option value=change>修正';
		#print '</select> ';
		#print '<input type=submit value="送信"></form>';
		#print '<form action="'.$returnpath.$mainscript.'" method=post>';
		#print '<input type=hidden name=bbs value='.$bbs.'>';
		#print '<input type=hidden name=key value='.$key.'>';
		#print '<table border=0><tr><td align=right>';
		#print 'name：</td><td colspan=2 nowrap><input name=FROM size=20> ';
		#print 'mail：<input name=mail size=20> ';
		#print '</tr>';
		#print '<tr><td align=right valign=top nowrap>message：</td>';
		#print '<td colspan=2><textarea rows=5 cols=60 wrap=soft name=MESSAGE></textarea></td></tr>';
		#print '<tr><td align=right>ＵＲＬ：</td><td colspan=2><input name=home size=60></td></tr>';
		#print '<td align=right>password：</td><td><input type=password name=pass size=10>&nbsp;&nbsp;<input type=submit name=submit value="書き込む" onClick="stck(this.form)"></td>';
		#print '</tr>';
		#print '<tr><td>';
		#print '<a href=#menu>スレッドのトップ</a> ';
		print '</td></tr></table></td></tr></table></td></tr></table></form>';
	}
	#
	# 著作権と終端
	#
	&copyright;
}
;#
;# ページ生成中エラー
;#
sub printingerror {
	my $line = shift;
	my $com  = shift;
	print '<table border=0 width="'.$main_width.'" cellspacing=0 cellpadding=1 bgcolor="'.$formborder.'" align=center><tr><td>';
	print '<table width=100% border=0 bgcolor="'.$formtable.'" cellspacing=0 cellpadding=5><tr><td>';
	print 'ページの生成中にエラーが発生しました。<br>';
	print '<a href="'.$pagecgi.'?'.$ENV{'QUERY_STRING'}.'">こちら</a>を押して再描画してください。<br>';
	print '症状が改善しない場合は管理者に連絡してください。<br>';
	print $com;
	print '<div align=right>CALL POINT：'.$line.'</div></td></tr></table></td></tr></table>';
	&copyright;
}

