<?php
	define('DIRCMS_ROOT', str_replace("\\", '/',substr(dirname(__FILE__),0,-9)));
	include DIRCMS_ROOT.'include/common.inc.php';
	session_start();
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$action=empty($action)?'index':trim($action);
	require_once(DIRCMS_ROOT.'include/form.class.php');	
	$form=new form();
	$table=DB_PRE.'guestbook';
	switch($action)
	{
		case 'index':
			$data=array();
			$list=new dircmslist();			
			$where = " `$table`.`passed`=1";
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$data=$list->getlist($table,$where,$orderby,$page,10,0,'center');
			if(!$data)$data=array();
			$pages=$list->pages;
			$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'">网站首页</a> > 留言列表';
			include template('guestbook_index');
			break;
		case 'post':
			if($do_submit)
			{
				if($_SESSION['checkcode']!=$chkcode || !$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(strlen($info['title'])>200 || strlen($info['title'])<1)showmsg('标题不要大于200个或小于1个字符!');
				if(strlen(strip_tags($info['content']))>500 || strlen(strip_tags($info['content']))<1)showmsg('留言内容不要超过500个字符或小于1个字符!');
				$info['title']=sub_string(htmlspecialchars($info['title']),200);
				$info['userid']=$_userid?$_userid:0;
				$info['username']=$_username?$_username:'游客';
				$info['hidden']=1;
				$info['passed']=$_userid?1:0;
				$info['ip']=IP;
				$info['addtime']=TIME;
				if($db->insert($table,$info))
				{
					showmsg('留言添加成功'.($info['passed']?'':'，请等待管理员审核').'!');
				}
				else
				{
					showmsg('留言添加失败!');
				}				
			}
			$contentform=$form->editor('content','','','','',0,50,550,200,'Basic');
			$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'">网站首页</a> > 写留言';
			include template('guestbook_post');
			break;
		default:
			showmsg('Bad Request!!!',$DIRCMS['site_url']);
			break;
	}
?>