<?php
require_once "HTML/Template/IT.php";
header("Content-Type: text/xml");

$logfile="/opt/gryphon/html/off/flashbbs.log";

$fn=fopen($logfile,"r");

$doc_count=0;

$conn=@pg_connect("user=sano dbname=gryphon");
if(!$conn)exit;

while($in=fgets($fn)){
	$in=str_replace("<><>","<>_<>",$in);
	$st1=str_replace("<>","<#>",mb_convert_encoding($in,"utf8","sjis"));
	$st2=split("<#>",$st1);

	//echo $st1,"<br>";
	//print_r($st2);
	//continue;
	//echo count($st2),"\n";

	if(count($st2)<13){
		continue;
	}

	for($i=0;$i<(count($st2)-1);($i=$i+13)){
		$n=$st2[$i];
		$dat["root_id"]=$st2[$i+3];
		if($dat["root_id"]=="root")$dat["root_id"]=$st2[$i+0];
		$dat["sub_id"]=$st2[$i+0];

		$dat["subject"]=$st2[$i+4];
		$dat["handle"]=$st2[$i+5];
		$dat["date"]=$st2[$i+7];
		$dat["comment"]=$st2[$i+12];

		//output_data($dat);
		$data[$doc_count]=$dat;
		$doc_count++;
	}
	if($doc_count>20)break;
}

//for($i=0;$i<$doc_count;$i++){
	//print_r($data[$i]);
//}

$tpl = new HTML_Template_IT("./");
$tpl->loadTemplatefile("offbbs.xml", true, true);

$tpl->setCurrentBlock("MAIN");
$tpl->setVariable("RSS_TITLE","the Gryphon Offline Meeting BBS");
$tpl->setVariable("RSS_URL","http://www.gryphon.to/off/flashbbs.cgi");
$tpl->setVariable("RSS_DESCRIPTION",mb_convert_encoding("the Gryphon Off ·Ç¼¨ÈÄ","UTF-8","euc-jp"));
$tpl->parseCurrentBlock("MAIN");

for($i=0;$i<$doc_count;$i++){
	$row=$data[$i];
	//print_r($row);
	$comment1=str_replace("\n","<br />",$row["comment"]);
	$comment1=str_replace("\r","",$comment1);
	$comment1=str_replace("[","&#91;",$comment1);
	$comment1=str_replace("]","&#93;",$comment1);

	/*
	$comment2=str_replace("\n","<br />",$row["s_comment"]);
	$comment2=str_replace("\r","",$comment2);
	$comment2=str_replace("[","&#91;",$comment2);
	$comment2=str_replace("]","&#93;",$comment2);
	*/

	$SQL="SELECT to_char('20".$row["date"]."'::timestamp(0),'Dy, DD Mon YYYY HH24:MI:SS +0900') as mdate;";
	$res=pg_query($SQL);
	$mdate=$row["date"];
	if($res){
		$rows=pg_fetch_array($res);
		$mdate=$rows["mdate"];
	}
	//echo $mdate,"\n";

	$tpl->setCurrentBlock("SUB");
	$tpl->setVariable("TITLE",mb_convert_encoding($row["subject"],"UTF-8","utf-8"));
	$tpl->setVariable("HANDLE_NAME",mb_convert_encoding($row["handle"],"UTF-8","utf-8"));
	$tpl->setVariable("COMMENT1",mb_convert_encoding($comment1,"UTF-8","utf-8"));
	$tpl->setVariable("MODIFY_DATE",$mdate);
	$tpl->setVariable("ROOT_ID",$row["root_id"]);
	$tpl->setVariable("SUB_ID",$row["sub_id"]);
	$tpl->parseCurrentBlock("SUB");
}

$tpl->show();

pg_close($conn);

exit;
?>
