<?php
require_once "./includes/common.inc.php";


	if(checksubmit('postmsg',1, $useseccode)){
		$username=trim($username);
		$email = trim($email);
		$company = trim($company);
		if(empty($username)){
			msg('nachen_empty');
		}

		if(strlen($username) > 15) {
			msg('nachen_too_long');
		}
		$guestexp = '\xA1\xA1|\xAC\xA3|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';



		if(preg_match("/^\s*$|^c:\\con\\con$|[%,\*\"\s\t\<\>\&]|$guestexp/is", $username)) {
			msg('nachen_unaccepted');
		}
		$censorexp = '/('.str_replace("\r\n", '|', preg_quote($censoremail, '/')).')$/i';
		if(isset($email) && (!isemail($email) || ($censoremail && preg_match($censorexp, $email)))) {
			msg('email_unaccepted');
		}
		$phone=trim($phone);
		if(empty($phone) || !ereg("(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)",$phone)){
			msg('phone_unaccepted');
		}
		$subject = htmlconvert(trim($subject));

		if($subject == '' || $msg == '') {
			msg('contents_unaccepted');
		}
		if(strlen($subject) >79){
			msg('subject_too_long');
		}
		if(strlen($msg) >1000){
			msg('contents_too_long');
		}

		$company = htmlconvert($company);
		$postcode = htmlconvert($postcode);
		$address = htmlconvert($address);
		$phone = htmlconvert($phone);
		$mobile = htmlconvert($mobile);
		$oicq = htmlconvert($oicq);
		$contact = htmlconvert($contact);
		$old = $db -> queryarray("SELECT postime FROM {$tablepre}guestbook WHERE postip='$onlineip' ORDER BY postime DESC LIMIT 1");
		if(($time - $old['postime'])<30){
			msg('post_water');
		}
		$db -> query("INSERT INTO {$tablepre}guestbook  (username, company, subject, address, postcode, postime, postip, email, oicq, phone, mobile, connact, msg) VALUES ('$username', '$company', '$subject', '$address', '$postcode', '$time', '$onlineip', '$email', '$oicq', '$phone','$mobile', '$connact', '$msg')");
		msg('thanks', mreferer());
	}else{		
		if($useseccode) {
			$seccode = random(4, 1);
		}

		$referer = mreferer();


	
	$page = ispage($page) ? $page : 1;
	$pagenum = 5;
	$start_limit = ($page-1) * $pagenum;
	$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}guestbook");
	$lines = $db -> result($query, 0);
	$paging = paging($lines, $pagenum, $page, 'guestbook.php');
	$msgs=array();
	$query = $db -> query("SELECT * FROM {$tablepre}guestbook ORDER BY postime DESC LIMIT $start_limit, $pagenum");
	while($msg = $db -> fetch_array($query)){
		$msg['postime']= gmdate("$dateformat $timeformat", $msg['postime'] + $timeoffset * 3600);
		$msg['replaytime']= gmdate("$dateformat $timeformat", $msg['replaytime'] + $timeoffset * 3600);
		$msgs[$msg['gid']] = $msg;

	}
include theme('book');	
}
?>