<?php
if(!defined('R_P')) exit('Access Denied');
@include_once(R_P . "/config/guestbook.php");
if(!intval($_SET['plugin_guestbook_open'])) base::showmsg("Guestbook is closed");
$_SET['plugin_guestbook_verify'] = intval($_SET['plugin_guestbook_verify']);
$time_limit = 1; // hour
$limit = 15;
$pageno = max(intval($_GET['pageno']), 1);
$offset = ($pageno - 1) * $limit;
$url = REWRITE ? "index-ac-guestbook.html" : "index.php?ac=guestbook";
$op = $_GET['op'] ? trim($_GET['op']) : 'manage';
switch($op){
	case 'manage':
		$total = qdb::queryAlone("select count(*) as num from ".TABLEPRE."guestbook ");
		if($total > 0){
			$data = qdb::fetchAll("select * from ".TABLEPRE."guestbook order by gid desc limit {$offset}, {$limit}");
		}else{
			$data = array();
		}
		$pager = qbase::page($total , $pageno, $limit, 1);
		require_once( R_P."/include/inc.ubb.php");
	break;

	case 'add':
		if($_POST['myFormAc'] == 'update'){
			$param = $_POST['guest'];
			$field = array('pic','qq','email','message');
			foreach($param as $key=>$val){
				if(!in_array($key, $field)) unset($param[$key]);
			}
			if($_SET['plugin_guestbook_verify']){
				ini_get('session.auto_start') || @session_start();
				$code = strtolower(trim($_SESSION['code']));
				$verify_code = strtolower(trim($_POST['verify_code']));
				unset($_SESSION['code']);
				if(!$code || $code != $verify_code) qbase::showmsg("出错了，验证码错误", $_SET['skin'], $url . "#reply");
			}
			if(strlen($param['message']) < 4){
				qbase::showmsg("出错了，留言内容不能少于4个字符");
			}
			$param['ip'] = qbase::get_ip();
			$last_post = qdb::queryAlone("select dateline from " . TABLEPRE . "guestbook where ip = '{$param['ip']}' order by gid desc limit 1");
			$last_post = intval($last_post);
			if($_SERVER['REQUEST_TIME'] - $last_post < $time_limit * 3600) qbase::showmsg("出错了，您每{$time_limit}个小时才能提交一次留言", $_SET['skin'], $url);
			$param['dateline'] = $_SERVER['REQUEST_TIME'];
			$param['has_reply'] = $param['reply_time'] = 0;
			$param['reply'] = '';
			qdb::query(qbase::createSql(TABLEPRE . 'guestbook', $param, 'insert'));
			qbase::showmsg("恭喜你，留言提交成功", $_SET['skin'], $url);
		}else{
			qbase::showmsg("bad request");
		}
	break;

	default:
		qbase::showmsg("bad request");
	break;
}
include qbase::template($_SET['skin'] . '/guestbook');
?>