#!/usr/bin/perl --
;#-----------------------------------------------------------------------------
$root_path = "";
#$server_path = "/home/lcvirtualdomain/sumireya.com/users/admin/public_html_sumireya.com";
$server_path = $ENV{'DOCUMENT_ROOT'};
;#------------------------------------------------------------------------------
use Fcntl qw(:flock);
use lib '../../common/lib/pl';
require 'jcode.pl';
require 'mimew.pl';
require 'unicode.pl';
require 'fold.pl-1.6.pl';
require 'text.pl';
require 'env.pl';
require 'str.pl';
require 'cookie.pl';
require 'jtime.pl';
require 'iodata.pl';
require 'backup.pl';

use lib './';
require 'update.pl';
;#------------------------------------------------------------------------------
$data_path = '../../dat/main/bbs/';
if(!-e $data_path){umask(0);mkdir($data_path,0777)};
if(!-e $data_path."backup/"){umask(0);mkdir($data_path."backup/",0777)};

$index_name = 'index.dat';
$index_path = $data_path.$index_name;
$backup_path = $data_path."backup/".$index_name;

;#------------------------------------------------------------------------------
;# 変数格納
if($ENV{'REQUEST_METHOD'} eq 'POST'){
	read(STDIN,$key,$ENV{'CONTENT_LENGTH'});
}else{
	$key = $ENV{'QUERY_STRING'};
}

@pairs = split(/&/, $key);
foreach $pair (@pairs){
	($field, $value) = split(/=/, $pair);
	$field =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/ge;
	&jcode'convert(*field,"euc");

	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/ge;
	$value =~ s/\</&lt;/g;
	$value =~ s/\>/&gt;/g;
	$value =~ s/\n+$//g;
	$value =~ s/\n/\<br\>/g;
	$value =~ s/\t/　/g;
	$value =~ s/%//g;
	&jcode'convert(*value,"euc");

	$FORM{$field} = $value;
	$mode = $FORM{'mode'};
	$res = $FORM{'res'};
	$number = $FORM{'number'};
	$exto = $FORM{'exto'};

	$cflg = $FORM{'cflg'};
	$kflg = $FORM{'kflg'};
	$sflg = $FORM{'sflg'};
	$nflg = $FORM{'nflg'};
	$word = $FORM{'word'};

	$name = $FORM{'name'};
	$mail = $FORM{'mail'};
	$title = $FORM{'title'};
	$comment = $FORM{'comment'};
}

;#------------------------------------------------------------------------------
$getstr = "";
if($cflg ne ""){$getstr .= "&cflg=".$cflg};
if($kflg ne ""){$getstr .= "&kflg=".$kflg};
if($sflg ne ""){$getstr .= "&sflg=".$sflg};
if($nflg ne ""){$getstr .= "&nflg=".$nflg};
if($word ne ""){$getstr .= "&word=".&text'urlEncode($word)};
;#------------------------------------------------------------------------------
@status_array = ("削除","非表示","表示中");
;#------------------------------------------------------------------------------
if($sflg ne ""){push(@tmp_h1title2,$status_array[$sflg]);};
if($nflg ne ""){push(@tmp_h1title2,"ＮＥＷ");};
$tmp = join(" ",@tmp_h1title2);
if($tmp ne ""){$h1title2="(".$tmp.")"}
;#------------------------------------------------------------------------------
&iodata'existFile($index_path,0666);
;#------------------------------------------------------------------------------
if($word ne ""){
	$word =~ s/　/ /gi;
	@word_array = split(/ /,$word);
	$word_keys = "";
	for($i=0;$i<$#word_array+1;$i++){
		$word_keys .= '(?=.*'.$word_array[$i].')';
	}
}
;#------------------------------------------------------------------------------
if($mode eq '1'){
	*tmpcookie = &cookie'getCookie();
	$stamp = $tmpcookie{"STAMP"};
	if($stamp eq 'undefined'){$stamp = 0;}
	if(&text'fakeDecode($stamp,"1") eq 'true'){&str'errorOut("有効な書き込みではありません。",$root_path);exit;}


	&backup'fileCopy($index_path,$backup_path);

	$row = 0;
	open(IN, "< $index_path");
	flock IN,LOCK_SH;
	while(<IN>){
		chomp $_;
		($tmpdel,$tmpsort,$tmpregdate,$tmpupdate,$tmphost,$tmpaddr,$tmpname,$tmpmail,$tmptitle,$tmpcomment) = split("	",$_);

		$index[$row][0]=$tmpdel;
		$index[$row][1]=$tmpsort;
		$index[$row][2]=$tmpregdate;
		$index[$row][3]=$tmpupdate;
		$index[$row][4]=$tmphost;
		$index[$row][5]=$tmpaddr;
		$index[$row][6]=$tmpname;
		$index[$row][7]=$tmpmail;
		$index[$row][8]=$tmptitle;
		$index[$row][9]=$tmpcomment;
		$row++;
	}
	close(IN);

	$host = $ENV{'REMOTE_HOST'};
	$addr = $ENV{'REMOTE_ADDR'};
	if($host eq ''){$host = $addr;}
	if($host eq $addr){$host = gethostbyaddr(pack('C4',split(/\./,$host)),2) || $addr;}


	$timestamp = time;
	$index[$row][0] = "2";
	$index[$row][1] = $row;
	$index[$row][2] = $timestamp;
	$index[$row][3] = $timestamp;
	$index[$row][4] = $host;
	$index[$row][5] = $addr;
	$index[$row][6] = $name;
	$index[$row][7] = $mail;
	$index[$row][8] = $title;
	$index[$row][9] = $comment;
	$row = $row + 1;


	open(REG, "> $index_path");
	flock REG,LOCK_EX;
	for($i=0;$i<$row;$i++){
		print REG $index[$i][0]."\t".$index[$i][1]."\t".$index[$i][2]."\t".$index[$i][3]."\t".$index[$i][4]."\t".$index[$i][5]."\t".$index[$i][6]."\t".$index[$i][7]."\t".$index[$i][8]."\t".$index[$i][9]."\n";
	}
	close(REG);
	chmod (0666, "$index_path");

	&update'main;
	&str'jumpOut("index.cgi?res=".$res.$getstr);
	exit;
}
;#------------------------------------------------------------------------------

$newlimit = time - (24*60*60);

$tmp_row=0;
open(IN, "< $index_path");
flock IN,LOCK_SH;
while(<IN>){
	chomp $_;
	($tmpdel,$tmpsort,$tmpregdate,$tmpupdate,$tmphost,$tmpaddr,$tmpname,$tmpmail,$tmptitle,$tmpcomment) = split("	",$_);

	$tmp_index[$tmpsort][0]=$tmpdel;
	$tmp_index[$tmpsort][1]=$tmp_row;
	$tmp_index[$tmpsort][2]=$tmpregdate;
	$tmp_index[$tmpsort][3]=$tmpupdate;
	$tmp_index[$tmpsort][4]=$tmphost;
	$tmp_index[$tmpsort][5]=$tmpaddr;
	$tmp_index[$tmpsort][6]=$tmpname;
	$tmp_index[$tmpsort][7]=$tmpmail;
	$tmp_index[$tmpsort][8]=$tmptitle;
	$tmp_index[$tmpsort][9]=$tmpcomment;

	if($mode eq '2' && $number eq $tmp_row){
		$title = 'Re: '.$tmptitle;
		$comment = &text'inputRES($tmpcomment);
	}


	$tmp_row=$tmp_row + 1;
}
close(IN);



$row=0;
for($i=0;$i<$tmp_row;$i++){
	if($tmp_index[$i][0]>1){

		$tmp = "";
		if($word ne ""){
			$tmp .= $status_array[$tmp_index[$i][0]]."	";
			if($tmp_index[$i][2] > $newlimit){$tmp .= "ＮＥＷ"."	"."NEW"."	"."ｎｅｗ"."	"."new"."	";}

			$tmp .= $tmp_index[$i][6]."	";
			$tmp .= $tmp_index[$i][7]."	";
			$tmp .= $tmp_index[$i][8]."	";
			$tmp .= $tmp_index[$i][9]."	";
		}

		if(
			(($sflg ne "" && $tmp_index[$i][0] eq $sflg) || $sflg eq "") && 
			(($nflg ne "" && $tmp_index[$i][2] > $newlimit) || $nflg eq "") && 
			(($word ne "" && ($tmp =~ /$word_keys/)) || $word eq "")
		){
			$index[$row][0] = $tmp_index[$i][0];
			$index[$row][1] = $tmp_index[$i][1];
			$index[$row][2] = $tmp_index[$i][2];
			$index[$row][3] = $tmp_index[$i][3];
			$index[$row][4] = $tmp_index[$i][4];
			$index[$row][5] = $tmp_index[$i][5];
			$index[$row][6] = $tmp_index[$i][6];
			$index[$row][7] = $tmp_index[$i][7];
			$index[$row][8] = $tmp_index[$i][8];
			$index[$row][9] = $tmp_index[$i][9];
			$row++;
		};
	};
}

@index_rev=reverse @index;
;#------------------------------------------------------------------------------
;# ページ数

$limit_value=10;

if(!$res){$res=0;};

$row=$#index+1;
$page_total=sprintf("%d",$row/$limit_value);
if($row%$limit_value){$page_total=$page_total+1;}

if(($row-$res)<$limit_value){$limit=$row;}else{$limit=$res+$limit_value;};

$page_now=sprintf("%d",$limit/$limit_value);
if($limit%$limit_value){$page_now=$page_now+1;}
;#------------------------------------------------------------------------------
print <<'END_OF_HTML';
Content-type: text/html; charset=EUC-JP


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/format.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<!-- InstanceBeginEditable name="doctitle" -->
<title>札幌の味噌ラーメン　すみれ</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<script type="text/javascript">
<!--
var errorStr = new Array();
errorStr['msgname'] = "名前を入力してください。";
errorStr['msgmail1'] = "メールアドレスを入力してください。";
errorStr['msgmail2'] = "メールアドレスを正しく入力してください。例)xxx@xxxx.jp";
errorStr['msgtitle'] = "タイトルを入力してください。";
errorStr['msgcomment'] = "本文を入力してください。";
END_OF_HTML
print 'errorStr[\'msghoge\'] = "'.&text'fakeEncode("1").'";'."\n";
print <<END_OF_HTML;
//-->
</script>
<script type="text/javascript" src="check.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<!-- InstanceEndEditable -->
<link href="/common/css/basic.css" rel="stylesheet" type="text/css">
</head>

<body>

END_OF_HTML
open(IN,"$server_path/common/ssi/static/ssl_header.html" );
flock IN,LOCK_SH;
#while(<IN>){if($_ eq "\n"){last;}}
while(<IN>){print $_;}
close(IN);
print <<"END_OF_HTML";


<div id="mainbox">
	<!-- InstanceBeginEditable name="top" -->
	<!-- InstanceEndEditable -->
	<div id="L-menu">


END_OF_HTML
open(IN,"$server_path/common/ssi/static/ssl_menu.html" );
flock IN,LOCK_SH;
#while(<IN>){if($_ eq "\n"){last;}}
while(<IN>){print $_;}
close(IN);
print <<"END_OF_HTML";


END_OF_HTML
open(IN,"$server_path/common/ssi/dynamic/info.html" );
flock IN,LOCK_SH;
#while(<IN>){if($_ eq "\n"){last;}}
while(<IN>){print $_;}
close(IN);
print <<"END_OF_HTML";


	<!-- InstanceBeginEditable name="other" -->
	<!-- InstanceEndEditable -->
	</div>
	
	<div id="R-main">
	<!-- InstanceBeginEditable name="main" -->


<table width="510" border="0" align="center" cellpadding="5" cellspacing="0" class="boxbar01px"> 
<tr> 
<td align="center" valign="top"><img src="img/title.jpg" width="501" height="65"> <br> 
<br> 
<span class="px12"><font color="#030D22">掲示板について<br><br>
平素より当すみれホームページをご利用いただき誠にありがとうございます。<br> 
<br>
このたび、すみれ掲示板を停止させて頂くことになりました。心無いメッセージがあり、すみれ掲示板を定期的にご覧のお客様には大変申し訳ございませんが、以後ご意見等ございましたら連絡先を明記の上問い合わせよりメール頂きたく存じます。 <br>
ご了承ください。 <br>
宜しくお願い致します。</font></span> <br>

<br>
</td>
</tr>
</table>
</div>
END_OF_HTML




open(IN,"$server_path/common/ssi/static/ssl_footer.html" );
flock IN,LOCK_SH;
#while(<IN>){if($_ eq "\n"){last;}}
while(<IN>){print $_;}
close(IN);
print <<"END_OF_HTML";


</div>
</body>
<!-- InstanceEnd --></html>



END_OF_HTML
