#!/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 {
	#
	# 局所変数
	#
	my ($bbs,$QUERY_DATA);
	#
	# 引数を受け取ります
	#
	if ($ENV{'PATH_INFO'} ne '') {
		$QUERY_DATA = $ENV{'PATH_INFO'};
		$QUERY_DATA = substr($QUERY_DATA,0,(length($QUERY_DATA)/2)) if ($xrea);	# XREA
	} else {
		$QUERY_DATA = $ENV{'QUERY_STRING'};
	}
	
	if($QUERY_DATA =~ /$readcgi/) {
		$QUERY_DATA = $'; #'
	}
	#
	# サイズチェック
	#
	&error(__LINE__,'送信データが大きすぎます') if (length($QUERY_DATA) > $sobig);
	#
	# ごみ箱閲覧
	#
	if ($QUERY_DATA eq '/trash') {
		require 'file.pl';
		&trashTitle($QUERY_DATA);
	}
	elsif ($QUERY_DATA =~ m|^/delc/|) {
		require 'file.pl';
		&deleteCookie($QUERY_DATA);
	}
	elsif ($QUERY_DATA eq '/path') {
		require 'file.pl';
		&pathTest;
	}
	#
	# 板
	#
	$QUERY_DATA =~ s|^/([-a-zA-Z0-9]+)/|/|;
	$bbs = $1;
	if (&checkBbs($bbs)) {
		&readsubset($pluspath.$bbs.'/'.$subsetcgi);
	} else {
		&error(__LINE__,'そんな板ありません');
	}
	#
	# 処理分岐
	#
	if ($QUERY_DATA eq '/submit') {
		require 'file.pl';
		&submit($bbs);
	}
	elsif ($QUERY_DATA eq '/cap') {
		require 'file.pl';
		&cap($bbs);
	}
	elsif ($QUERY_DATA =~ m|^/past/?|) {
		require 'file.pl';
		&pasttitle($QUERY_DATA,$bbs);
	}
	else { &look($QUERY_DATA,$bbs); }
}
;#
;# レス表示
;#
sub look {
	#
	# 変数取得
	#
	my $data= shift;
	my $bbs = shift;
	my ($key,$order,$from,$to,$want,@resNo);
	my ($goget,$goback);
	my $plus = $pluspath.$bbs.'/';
	my $diswant = 5;
	#
	# 汚染チェック
	#
	if ($data =~ m|^/([0-9]+)/(l?[-+0-9]*n?)$|) {
		$key = $1;
		$order = $2;
	} else {
		&error(__LINE__,'不正なデータを受信しました');
	}
	#
	# 変数取得
	#
	if ($order =~ /^([+0-9]+)n?$/) {
		@resNo = split(/\+/,$1);
	} elsif ($order =~ /^([0-9]*)-?([0-9]*)(n?)$/) {
		$from = $1 ? $1 : 1;
		$to   = $2;
		$resNo[0] = 1 if (!$3);
	} elsif ($order =~ /^l([0-9]+)(n?)$/) {
		$want = $1;
		$resNo[0] = 1 if (!$2);
	} else {
		&error(__LINE__,'データを取得できません');
	}
	#
	# get用
	#
	if ($getmethod) {
		$goget = $readcgi.'/'.$bbs.'/'.$key.'/';
	} else {
		$goback = '../../../';
	}
	#
	# ファイルチェック
	#
	my $file = $plus.$savedir.$key.'.dat';
	unless (-e $file) {
		require 'file.pl';
		&search($data,$bbs,$key,$goback);
	}
	#
	# データファイルから記事を読みこみます
	#
	open(LOG,$file) or &error(__LINE__,'データを開けません');
	my $stop = (split(/<>/,scalar(<LOG>)))[9];
	seek(LOG,0,0);
	#
	# レス数
	#
	my $count;
	++$count while(<LOG>);
	#
	# 続いたレスを表示する場合
	#
	if ($from) {
		if ($from > $count) {
			if ($count - $someres > 0) {
				$from = $count - $someres + 1;
			} else {
				$from = 1;
			}
		}
		if (!$to or $to > $count) {
			$to = $count;
		}
		$want = $to - $from + 1;
		pop @resNo if ($from == $resNo[0]);
	}
	#
	# 最新xx件という指定の場合
	#
	elsif ($want) {
		$from = $count - $want + 1;
		$from = 1 if ($from<0);
		$to = $count;
		$want = $count if ($want > $count);
		pop @resNo if ($from == $resNo[0]);
	}
	#
	# タイトル取得
	#
	seek(LOG,0,0);
	my @TEMP = split(/<>/,scalar(<LOG>));
	#
	# 圧縮
	#
	&encode();
	#
	# Header
	#
	print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
	print '<html><head>';
	print '<meta http-equiv="Content-Type" content="text/html; charset=x-sjis">';
	print $read_style;
	print '<title>'.$TEMP[4].'</title>';
	if (!$stop) {
print <<EOF;
<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*$cooklife);
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;
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.resfm){FROM.value=unescape(cka[0]);mail.value=unescape(cka[1]);home.value=unescape(cka[2]);pass.value=unescape(cka[3]);}
}
onload=gtck;
//-->
</SCRIPT>
EOF
	}
	print '</head>';
	print '<body background="'.$read_back.'" bgcolor="'.$read_bgc.'" text="'.$read_text.'" link="'.$read_link.'" vlink="'.$read_vlink.'" alink="'.$read_alink.'">';
	print '<a href="'.$goback.$plus.$indexfile.'">■掲示板に戻る■</a> ';
	print '<a href="'.$goget.'">全部</a> ';
	print '<form action="'.$goback.$mainscript.'" method=post>';
	print '<input type=hidden name=stay value="'.$readcgi.'/'.$bbs.$data.'">';
	print '<input type=hidden name=key value='.$key.'><input type=hidden name=bbs value='.$bbs.'>';
	print '<b><font size="'.$read_subj_font.'" color="'.$read_subjclr.'">'.$TEMP[4].'</font></b><br>';
	#
	# スレッドストップのチェック
	# ファイルサイズとレス数のチェック
	#
	my $volume = -s $file;
	$volume = int($volume/1024)+1;
	my $on;
	if ($stop) {
		print '<b><font size=2 color="'.$alertclr.'">このスレッドは停止されています。</font></b>';
	} else {
		$on = &cv_check($count,$volume);
	}
	print '<dl>';
	#
	# ひとつずつ表示させていく
	#
	my $com;
	foreach $resno (@resNo) {
		#
		# 指定の位置からログを取得
		#
		next if ($resno == 0 or $resno > $count);
		seek(LOG,0,0);
		my $j = $resno;
		while(--$j > 0) {
			<LOG>;
		}
		my @TEMP = split(/<>/,scalar(<LOG>));
		#
		# リプライ用
		#
		$com = $TEMP[3] if (@resNo == 1);
		#
		# データを指定した形式に変換
		#
		&convertData(\@TEMP,'return'=>$goback.$pluspath,'link'=>$goget,'resno'=>$resno,'color'=>$read_nameclr,'count'=>$count);
		print '<br><br>';
	}
	#
	# ここからは連続したレス
	#
	if ($from) {
		seek(LOG,0,0);
		my $j = $from;
		my $no = --$j;
		my $sbt = $want;
		#
		# 早送り
		#
		while($j--) {
			<LOG>;
		}
		#
		# 出力開始
		#
		while (<LOG>) {
			last if ($sbt-- <= 0);
			++$no;
			#
			# 分割
			#
			my @TEMP = split(/<>/);
			#
			# データを指定した形式に変換
			#
			&convertData(\@TEMP,'return'=>$goback.$pluspath,'link'=>$goget,'resno'=>$no,'color'=>$read_nameclr,'count'=>$count);
			print '<br><br>';
		}
	}
	close(LOG);
	#
	# FORM
	#
	print '</dl>';
	#print '<select name=action>';
	#print '<option value=delshow selected>削除';
	#print '<option value=change>修正';
	#print '</select> ';
	#print '<input type=submit name=submit value="送信"></div><hr>';
	#
	# 記事が少ない場合は省略
	#
	if ($from and $want > $diswant) {
		print '<a href="'.$goback.$plus.$indexfile.'">■掲示板に戻る■</a> ';
		print '<a href="'.$goget.'">全部</a> ';
		#
		# 前のページ
		#
		if ($from > 1) {
			my $t   = $from - 1;
			my $f = $from - $someres;
			$f = 1 if ($f < 0);
			print '<a href="'.$goget.$f.'-'.$t.'">前'.$someres.'</a> ';
		}
		#
		# 次のページ
		#
		if ($to < $count) {
			my $f = $to + 1;
			my $t = $to + $someres;
			print '<a href="'.$goget.$f.'-'.$t.'">次'.$someres.'</a> ';
		}
		#
		# リンク表示
		#
		my $part = int(($count - 1)/$someres)+1;
		my $i;
		while ($part--) {
			my $st = $i * $someres + 1;
			my $lt = $i * $someres + $someres;
			print '<a href="'.$goget.$st.'-'.$lt.'">'.$st.'-</a> ';
			++$i;
		}
		print '<a href="'.$goget.'l'.$newest.'">最新'.$newest.'</a> ';
		print '<a href="'.$goget.$count.'-">新着レス</a><hr>';
	}
	print '</form>';
	if ($stop eq '' and !$on) {
		#print '<form name=resfm id=resfm action="'.$goback.$mainscript.'" method=post>';
		#print '<input type=hidden name=bbs value='.$bbs.'>';
		#print '<input type=hidden name=key value='.$key.'>';
		#print '<input type=hidden name=r value=1>';
		#print '<table width=550 border=0 cellspacing=0 cellpadding=5>';
		#print '<tr><td align=right>name：</td><td colspan=2 nowrap><input name=FROM size=20> mail：<input name=mail size=20> ';
		#print '</td></tr>';
		#print '<tr><td align=right>Profile：</td><td colspan=2 nowrap>性別:<select name=sex><option selected>その他<option>男性<option>女性<option>ハーフ</select>&nbsp;年齢:<select name=age><option selected>ヒミツ<option>10歳未満<option>10代<option>20代<option>30代<option>40代<option>50代<option>60代<option>70代<option>70歳以上</select>&nbsp;';
		#print '住所:<select name=address><option selected>住所不定<option>北海道<option>青森<option>岩手<option>宮城<option>秋田<option>山形<option>福島<option>群馬<option>栃木<option>茨城<option>埼玉<option>千葉<option>東京<option>神奈川<option>新潟<option>富山<option>石川<option>福井<option>山梨<option>長野<option>岐阜<option>静岡<option>愛知<option>三重<option>滋賀<option>京都<option>大阪<option>兵庫<option>奈良<option>和歌山<option>鳥取<option>島根<option>岡山<option>広島<option>山口<option>徳島<option>香川<option>愛媛<option>高知<option>福岡<option>佐賀<option>長崎<option>熊本<option>大分<option>宮崎<option>鹿児島<option>沖縄<option>海外<option>地球外</select>&nbsp;';
		#print '交通手段:<select name=traffic><OPTION selected>未定<option>飛行機<option>汽車<option>バス<option>車<option>バイク<option>自転車<option>徒歩<option>その他</select></td></tr>';
		#print '<tr><td align=right valign=top nowrap>message：</td>';
		#print '<td colspan=2><textarea rows=5 cols=60 wrap=soft name=MESSAGE>';
		#
		# リプライ
		#
		if (@resNo == 1 and !$from) {
			$com = '&gt;&gt;'.$resNo[0]."\n&gt; ".$com;
			$com =~ s/<br>/\n&gt; /g;
			print $com;
		}
		#print '</textarea></td></tr>';
		#print '<tr><td align=right>ＵＲＬ：</td><td colspan=3><input name=home size=60></td></tr>';
		#print '<tr><td align="right">';
		#print 'password：<td nowrap><input type=password name=pass size=10>&nbsp;&nbsp;<input type=submit name=submit value="書き込む" onClick="stck(this.form)">';
		
		# チェックボックス
		#
		#print '</td></tr></table></form>';
	}
	#
	# 著作と終端
	#
	&copyright;
}
