<?php
if(!defined('MOD'))  die("access die");
purviewCheck('guestbook', 'plugin');
$action = $_GET['action'] ? $_GET['action'] : 'manage';
switch($action){
	case 'manage':
		$limit = 20;
		$page = max(intval($_GET['page']), 1);
		$offset = ($page - 1) * $limit ;
		$data = qdb::fetchAll("select * from ".TABLEPRE."guestbook order by gid desc limit {$offset}, {$limit}");
		$total = qdb::queryAlone("select count(*) as num from ".TABLEPRE."guestbook ");
	break;

	case 'reply':
		$gid = intval($_GET['gid']);
		if($_POST['myFormAc']){
			$param = array();
			$param['reply'] = trim($_POST['reply']);
			$param['has_reply'] = 1;
			$param['reply_time'] = $_SERVER['REQUEST_TIME'];
			$sql = qbase::createSql(TABLEPRE."guestbook", $param, 'update') . " where gid = '{$gid}' ";
			qdb::query($sql);
			$return = qbase::xmlPrint("Submit success");
			die($return);
		}else{
			$data = qdb::queryFirst("select * from ".TABLEPRE."guestbook  where gid = '{$gid}' ");
		}
	break;

	case 'delete':
		checkAdminReferer();
		$gid = intval($_GET['gid']);
		qdb::query("delete from ".TABLEPRE."guestbook  where gid = '{$gid}' ");
		qbase::showmsg("删除成功", "default", $_SERVER['HTTP_REFERER']);
	break;

	case 'set':
		$file = R_P . "/config/guestbook.php";
		if($_POST['myFormAc'] == 'update'){
			$param = array();
			$param['plugin_guestbook_open'] = intval($_POST['plugin_guestbook_open']);
			$param['plugin_guestbook_verify'] = intval($_POST['plugin_guestbook_verify']);
			$html = "<?php\n";
			foreach($param as $key=>$val){
				$html .= "\$_SET['{$key}'] = '{$val}';\n";
			}
			$html .= "?>";
			qbase::writetofile($file, $html);
			echo qbase::xmlprint("操作成功");
			exit();
		}else{
			if(is_file($file)) @include_once($file);
			else{
				$_SET['plugin_guestbook_open'] = 0;
				$_SET['plugin_guestbook_verify'] = 0;
			}
		}
	break;
}
include qbase::template($path . 'plugin/guestbook');
?>