<?php

class content
{
	private $db;
	private $table;
	private $comment_table;
	private $type_table;
	private $postcount_table;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=LA_PRE.'content';
		$this->tab=LA_PRE.'liuyan';
		$this->comment_table=LA_PRE.'comment';
		$this->type_table=LA_PRE.'type';
		$this->postcount_table=LA_PRE.'postcount';
	}
	
	function click($contentid)
	{
		$contentid=intval($contentid);
		return $this->db->query("UPDATE `$this->table` SET `$this->table`.`clicks`=`$this->table`.`clicks`+1 WHERE `$this->table`.`contentid`=$contentid",true);
	}

	function datalist($moduleid=0,$status=-1,$ishidden=-1,$userid=0,$starttime='',$endtime='',$k='',$typeid=0,$contacttel='')
	{
		global $page,$_siteid,$siteidobj;
		$moduleid=intval($moduleid);

		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).') AND isdeleted=0':'isdeleted=0';
		$where.=$moduleid?' AND moduleid='.$moduleid:'';
		$where.=$status>=0?' AND status='.$status:'';
		$where.=$ishidden>=0?' AND ishidden='.$ishidden:'';
		$where.=$userid?' AND userid='.$userid:'';
		$where.=$typeid?' AND typeid='.$typeid:'';
		$where.=$starttime?' AND updatetime>=\''.trim($starttime).'\'':'';
		$where.=$endtime?' AND updatetime<=\''.(trim($endtime)+24*3600).'\'':'';
		$where.=$k?' AND title LIKE \'%'.$k.'%\'':'';
		$where.=$contacttel?' AND contacttel = \''.htmlspecialchars($contacttel).'\'':'';

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='contentid DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=15;
		$result=$datalist->getlist($this->table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function commentlist($moduleid=0,$contentid=0,$status=-1,$userid=0,$starttime='',$endtime='',$k='')
	{
		global $page,$_siteid,$siteidobj;
		$moduleid=intval($moduleid);

		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':'1';
		$where.=$moduleid?' AND moduleid='.$moduleid:'';
		$where.=$contentid?' AND contentid='.$contentid:'';
		$where.=$status>=0?' AND status='.$status:'';
		$where.=$userid?' AND userid='.$userid:'';
		$where.=$starttime?' AND inputtime>=\''.trim($starttime).'\'':'';
		$where.=$endtime?' AND inputtime<=\''.(trim($endtime)+24*3600).'\'':'';
		$where.=$k?' AND content LIKE \'%'.$k.'%\'':'';

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='id DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=15;
		$result=$datalist->getlist($this->comment_table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function mem_commentlist($moduleid=0,$contentid=0,$status=-1)
	{
		global $page,$siteidobj,$_userid;
		$moduleid=intval($moduleid);

		$where=$_SESSION['siteid']?'b.siteid IN('.implode(',',$siteidobj->getchildid($_SESSION['siteid'])).')':'1';
		$where.=$moduleid?' AND b.moduleid='.$moduleid:'';
		$where.=$contentid?' AND a.contentid='.$contentid:'';
		$where.=$status>=0?' AND a.status='.$status:'';
		$where.=' AND b.userid='.$_userid;

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='id DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=15;
		$result=$datalist->getlist($this->comment_table,$where,$orderby,$page,$pagesize,0,'right',$this->table,'contentid','contentid','userid');
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function commentinfo($commentid)
	{	
		$commentid=intval($commentid);
		return $this->db->fetch_one("SELECT * FROM `$this->comment_table` WHERE `$this->comment_table`.`id`=$commentid");
	}

	function comment_set($info,$ids=array())
	{
		global $settings,$_siteid,$siteidobj;

		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));
		foreach($ids as $id)
		{
			if($id)
			{
				$cinfo=$this->commentinfo($id);
				if(!$cinfo || ($_siteid && !in_array($cinfo['siteid'],$siteidobj->getchildid($_siteid))))
				{
					return false;
				}
				else
				{
					$this->db->update($this->comment_table,$info,'id='.$id);
				}
			}
		}
		return true;
	}

	function comment_submit($info)
	{
		global $cacheobj;
		$cacheobj->clear();
		return $this->db->insert($this->comment_table,$info);
	}

	function comment_delete($ids=array(),$userid=0)
	{
		global $settings,$_siteid,$siteidobj;

		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));
		foreach($ids as $id)
		{
			if($id)
			{
				$cinfo=$this->commentinfo($id);
				if(!$cinfo || ($_siteid && !in_array($cinfo['siteid'],$siteidobj->getchildid($_siteid))))
				{
					return false;
				}
				else
				{
					$coninfo=$this->get($cinfo['contentid']);
					if(!$userid || $userid==$coninfo['userid'])
					{
						$this->db->mysql_delete($this->comment_table,$id,'id');
					}
				}
			}
		}

		return true;
	}
	
	function mem_comment_set($info,$ids=array())
	{
		global $_userid, $cacheobj;

		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));
		foreach($ids as $id)
		{
			
			if($id)
			{
				$coninfo=$this->commentinfo($id);
				if(!$_userid || $_userid==$coninfo['userid'])
				{
					$this->db->update($this->comment_table,$info,'id='.$id);
				}
			}
		}
		$cacheobj->clear();
		return true;
	}

	function  mem_comment_delete($ids=array())
	{
		global $_userid, $cacheobj;
		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));
		foreach($ids as $id)
		{
			
			if($id)
			{
				$coninfo=$this->commentinfo($id);
				if(!$_userid || $_userid==$coninfo['userid'])
				{
					$this->db->mysql_delete($this->comment_table,$id,'id');
				}
			}
		}
		$cacheobj->clear();
		return true;
	}

	function commentstatis($moduleid,$contentid=0,$status=-1,$userid=0)
	{
		global $siteidobj,$_siteid;
		$moduleid=intval($moduleid);
		$contentid=intval($contentid);
		$status=intval($status);
		$userid=intval($userid);

		$where='1';
		$where.=$_siteid?' AND siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':'';
		$where.=$status>=0?' AND status='.$status:'';
		$where.=$userid?' AND userid='.$userid:'';
		$where.=$contentid?' AND contentid='.$contentid:'';
		$where.=$moduleid?' AND moduleid='.$moduleid:'';
		return get_cache_counts("SELECT COUNT(*) AS count FROM `$this->comment_table` WHERE $where");
	}

	function recyclelist($moduleid,$userid=0,$starttime='',$endtime='',$k='')
	{
		global $page,$_siteid,$siteidobj;
		$moduleid=intval($moduleid);

		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).') AND isdeleted=1':'isdeleted=1';
		$where.=$moduleid?' AND moduleid='.$moduleid:'';
		$where.=$userid?' AND userid='.$userid:'';
		$where.=$starttime?' AND updatetime>=\''.trim($starttime).'\'':'';
		$where.=$endtime?' AND updatetime<=\''.(trim($endtime)+24*3600).'\'':'';
		$where.=$k?' AND title LIKE \'%'.$k.'%\'':'';

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='contentid DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=15;
		$result=$datalist->getlist($this->table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}
   function liuyan(){
   	  $res=$this->db->fetch_all("SELECT * FROM $this->tab");
   	  return $res;
   }

	function constatis($moduleid=7,$typeid=0,$status=-1,$isdeleted=0)
	{
		global $siteidobj,$_siteid;
		$moduleid=intval($moduleid);
		$typeid=intval($typeid);
		$status=intval($status);
		$isdeleted=intval($isdeleted);

		$where='isdeleted='.$isdeleted;
		$where.=$_siteid?' AND siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':'';
		$where.=$typeid?' AND typeid IN('.implode(',',getchildtypeid($typeid)).')':'';
		$where.=$status>=0?' AND status='.$status:'';
		$where.=$moduleid?' AND moduleid='.$moduleid:'';
		return get_cache_counts("SELECT COUNT(*) AS count FROM `$this->table` WHERE $where");
	}

	function member_datalist($moduleid=0,$ishidden=-1)
	{
		global $page,$_siteid,$siteidobj,$_userid;
		$moduleid=intval($moduleid);
		$ishidden=intval($ishidden);

		$where=$_siteid?'userid= '.$_userid.' AND siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':'userid='.$_userid;
		$where.=$moduleid?' AND moduleid='.$moduleid:'';
		$where.=$ishidden>=0?' AND ishidden='.$ishidden:'';
		$where.=$ishidden==0?' AND status=1':'';
		$where.=' AND isdeleted=0';

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='updatetime DESC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=6;
		$result=$datalist->getlist($this->table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function typelist($moduleid=0,$typeid=0)
	{
		global $page,$_siteid,$siteidobj;
		$moduleid=intval($moduleid);
		$typeid=intval($typeid);

		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':1;
		$where.=$typeid?' AND parentid='.$typeid:' AND parentid=0';
		$where.=$moduleid?' AND moduleid='.$moduleid:'';

		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='orderby ASC ,typeid ASC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=25;
		$result=$datalist->getlist($this->type_table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function type_add($info)
	{
		$info['grandparentid']=$info['parentid']?$this->typeparentid($info['parentid']):0;
		$info['siteid']=intval($_SESSION['siteid']);
		return $this->db->insert($this->type_table,$info);
	}

	function type_delete($id)
	{
		global $_siteid,$siteidobj;
		$id=intval($id);
		$minfo=$this->typeinfo($id);
		if(!$minfo || ($_siteid && !in_array($minfo['siteid'],$siteidobj->getchildid($_siteid))))
		{
			return false;
		}

		$this->db->mysql_delete($this->type_table,intval($id),'typeid');
		$this->db->mysql_delete($this->type_table,intval($id),'parentid');
		$this->db->mysql_delete($this->type_table,intval($id),'grandparentid');
		return true;
	}

	function gettypeid($moduleid=7)
	{
		$r=$this->db->fetch_one("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`moduleid`=".intval($moduleid)." ORDER BY `$this->type_table`.`typeid` ASC");
		return $r?$r['typeid']:0;
	}

	function type_select($moduleid=7,$parentid=0,$g=3)
	{
		$str='';
		$r=$this->db->fetch_all("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`moduleid`=".intval($moduleid)." AND `$this->type_table`.`parentid`=".intval($parentid)." ORDER BY `$this->type_table`.`orderby` ASC,`$this->type_table`.`typeid` ASC");
		foreach($r as $_r)
		{
			if($g>=2)
			{
				$r2=$this->db->fetch_all("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`moduleid`=".intval($moduleid)." AND `$this->type_table`.`parentid`=".intval($_r['typeid'])." ORDER BY `$this->type_table`.`orderby` ASC,`$this->type_table`.`typeid` ASC");
				if($r2)
				{
					$str.='<optgroup label="'.$_r['name'].'">';
					foreach($r2 as $_r2)
					{
						if($g>=3)
						{
							$r3=$this->db->fetch_all("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`moduleid`=".intval($moduleid)." AND `$this->type_table`.`parentid`=".intval($_r2['typeid'])." ORDER BY `$this->type_table`.`orderby` ASC,`$this->type_table`.`typeid` ASC");
							if($r3)
							{
								$str.='<option value="'.$_r2['typeid'].'" disabled="disabled">'.$_r2['name'].'</option>';
								foreach($r3 as $_r3)
								{
									$str.='<option value="'.$_r3['typeid'].'">|--'.$_r3['name'].'</option>';
								}
							}
							else
							{
								$str.='<option value="'.$_r2['typeid'].'">'.$_r2['name'].'</option>';
							}
						}
						else
						{
							$str.='<option value="'.$_r2['typeid'].'">'.$_r2['name'].'</option>';
						}
					}
					$str.='</optgroup>';
				}
				else
				{
					$str.='<option value="'.$_r['typeid'].'">'.$_r['name'].'</option>';
				}
			}
			else
			{
				$str.='<option value="'.$_r['typeid'].'">'.$_r['name'].'</option>';
			}
		}

		return $str;
	}

	function type_select_cat($moduleid=7,$parentid=0)
	{
		$str='';
		$r=$this->db->fetch_all("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`moduleid`=".intval($moduleid)." AND `$this->type_table`.`parentid`=".intval($parentid)." ORDER BY `$this->type_table`.`orderby` ASC,`$this->type_table`.`typeid` ASC");
		foreach($r as $_r)
		{
			$r2=$this->db->fetch_all("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`moduleid`=".intval($moduleid)." AND `$this->type_table`.`parentid`=".intval($_r['typeid'])." ORDER BY `$this->type_table`.`orderby` ASC,`$this->type_table`.`typeid` ASC");
			if($r2)
			{
				$str.='<option value="'.$_r['typeid'].'">'.$_r['name'].'</option>';
				foreach($r2 as $_r2)
				{
					$str.='<option value="'.$_r2['typeid'].'">|--'.$_r2['name'].'</option>';
				}
				$str.='</option>';
			}
			else
			{
				$str.='<option value="'.$_r['typeid'].'">'.$_r['name'].'</option>';
			}
		}

		return $str;
	}

	function type_edit($info,$id)
	{
		global $_siteid,$siteidobj;
		$id=intval($id);
		$minfo=$this->typeinfo($id);
		if(!$minfo || ($_siteid && !in_array($minfo['siteid'],$siteidobj->getchildid($_siteid))))
		{
			return false;
		}
		$info['grandparentid']=$info['parentid']?$this->typeparentid($info['parentid']):0;
		return $this->db->update($this->type_table,$info,'typeid='.intval($id));
	}

	function typeinfo($id)
	{
		return $this->db->fetch_one("SELECT * FROM `$this->type_table` WHERE `$this->type_table`.`typeid`=".intval($id));
	}

	function typename($id)
	{
		$r=$this->typeinfo($id);
		return $r['name'];
	}

	function parenttypename($id)
	{
		$pid=$this->typeparentid($id);
		$r=$pid?$this->typeinfo($pid):array('name'=>'');
		return $r['name'];
	}

	function typeparentid($id)
	{
		$r=$this->typeinfo($id);
		return $r['parentid'];
	}

	function admin_add($info,$modelid=0,$extend=0)
	{
		global $modelobj,$settings,$dataobj,$cacheobj,$LA;
		$info['modelid']=intval($modelid);
		$modelinfo=$modelobj->info($modelid);

		$dataobj->setmodel(array(intval($modelid),intval($extend)));
		$info=$dataobj->filter($info);
		
		$info['inputtime']=$info['updatetime']=TIME;
		$info['ip']=IP;
		$info['clicks']=$info['comments']=0;
		$info['istopic']=$info['istopic']?(TIME+24*3600):0;
		$info['istop']=$info['istop']?(TIME+24*3600):0;
		$info['isfocus']=($info['isfocus'] && is_image($info['thumb']))?(TIME+max(intval($LA['top_expire']),24)*3600):0;
		$info['siteid']=intval($_SESSION['siteid']);
		$info['adminpost']=1;
		$info['description']=$info['description']?$info['description']:sub_string(trim(strip_tags($info['content'])),255);
		$contentid=$this->db->insert($this->table,$info);
		$info['contentid']=$info['id']=$info['mid']=$contentid;
		$this->db->insert(LA_PRE.$modelinfo['tbname'],$info,true);
		$hash_table=get_hash_table(LA_PRE.$modelinfo['tbname'].'_data',$info['mid'],$modelobj->subformcount(intval($extend)));
		$cacheobj->clear();
		return $this->db->insert($hash_table,$info,true);
	}

	function admin_edit($info,$contentid,$modelid=0,$extend=0)
	{
		global $modelobj,$settings,$dataobj,$cacheobj,$LA;
		$modelid=intval($modelid);
		$extend=intval($extend);
		$contentid=intval($contentid);
		
		$modelinfo=$modelobj->info($modelid);

		$dataobj->setmodel(array(intval($modelid),intval($extend)));
		$info=$dataobj->filter($info);
		$info['updatetime']=TIME;

		if($info['istopic'])
		{
			$info['istopic']=TIME+max(intval($LA['top_expire']),24)*3600;
		}
		else
		{
			unset($info['istopic']);
		}

		if($info['istop'])
		{
			$info['istop']=TIME+max(intval($LA['top_expire']),24)*3600;
		}
		else
		{
			unset($info['istop']);
		}

		if($info['isfocus'])
		{
			$info['isfocus']=TIME+max(intval($LA['top_expire']),24)*3600;
		}
		else
		{
			unset($info['isfocus']);
		}
		$info['siteid']=intval($_SESSION['siteid']);
		$info['description']=$info['description']?$info['description']:sub_string(trim(strip_tags($info['content'])),255);
		$this->db->update($this->table,$info,'contentid='.$contentid);
		$this->db->update(LA_PRE.$modelinfo['tbname'],$info,'id='.$contentid);
		$hash_table=get_hash_table(LA_PRE.$modelinfo['tbname'].'_data',$contentid,$modelobj->subformcount(intval($extend)));
		$cacheobj->clear();
		return $this->db->update($hash_table,$info,'mid='.$contentid);
	}

	function get($contentid)
	{	
		$contentid=intval($contentid);
		return $this->db->fetch_one("SELECT * FROM `$this->table` WHERE `$this->table`.`contentid`=$contentid");
	}

	function sdelete($ids,$userid=0)
	{
		global $settings,$_siteid,$siteidobj,$modelobj,$cacheobj;
		$userid=intval($userid);

		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));

		foreach($ids as $id)
		{
			if($id)
			{
				$cinfo=$this->get($id);
				if(!$cinfo || ($_siteid && !in_array($cinfo['siteid'],$siteidobj->getchildid($_siteid))))
				{
					return false;
				}
				else
				{
					$modelinfo=$modelobj->info($cinfo['modelid']);
					$hash_table=get_hash_table(LA_PRE.$modelinfo['tbname'].'_data',$id,$modelobj->subformcount2(intval($cinfo['modelid'])));
					if(!$userid || $userid==$cinfo['userid'])
					{
						$this->db->mysql_delete($this->table,$id,'contentid');
						$this->db->mysql_delete(LA_PRE.$modelinfo['tbname'],$id,'id');
						$this->db->mysql_delete($hash_table,$id,'mid');
						$this->db->mysql_delete($this->comment_table,$id,'contentid');
					}
				}
			}
		}
		$cacheobj->clear();
		return true;
	}

	function baseset($info,$ids=array())
	{
		global $settings,$_siteid,$siteidobj;

		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));
		foreach($ids as $id)
		{
			if($id)
			{
				$cinfo=$this->get($id);
				if(!$cinfo || ($_siteid && !in_array($cinfo['siteid'],$siteidobj->getchildid($_siteid))))
				{
					return false;
				}
				else
				{
					$this->db->update($this->table,$info,'contentid='.$id);
				}
			}
		}
		return true;
	}

	function move($movetotypeid,$ids=array())
	{
		global $modelobj;
		$movetotypeid=intval($movetotypeid);
		$ids=is_array($ids)?array_map('intval',$ids):array(intval($ids));
		foreach($ids as $id)
		{
			if($id)
			{
				$cinfo=$this->get($id);
				if(!$cinfo || ($_siteid && !in_array($cinfo['siteid'],$siteidobj->getchildid($_siteid))))
				{
					return false;
				}
				else
				{
					/*
						旧拓展表
					*/
					
					$newtypeinfo=$this->typeinfo($movetotypeid);
					if($newtypeinfo['modelid']!=$cinfo['modelid'])
					{
						$oldmodel=$modelobj->info($cinfo['modelid']);
						$oldtable=LA_PRE.$oldmodel['tbname'];
						$oldhash_table=get_hash_table(LA_PRE.$oldmodel['tbname'].'_data',$id,$modelobj->subformcount(intval($cinfo['modelid']+1)));

						$newmodel=$modelobj->info($newtypeinfo['modelid']);
						$newtable=LA_PRE.$newmodel['tbname'];
						$newhash_table=get_hash_table(LA_PRE.$newmodel['tbname'].'_data',$id,$modelobj->subformcount(intval($newtypeinfo['modelid']+1)));

						$base=$this->db->fetch_one("SELECT * FROM `$oldtable` WHERE `$oldtable`.`id`=$id");
						$more=$this->db->fetch_one("SELECT * FROM `$oldhash_table` WHERE `$oldhash_table`.`mid`=$id");

						$this->db->mysql_delete($oldtable,$id,'id');
						$this->db->mysql_delete($oldhash_table,$id,'mid');

						$this->db->insert($newtable,$base,true);
						$this->db->insert($newhash_table,$more,true);
					}

					$this->db->update($this->table,array('typeid'=>$movetotypeid,'modelid'=>intval($newtypeinfo['modelid'])),'contentid='.$id);
				}
			}
		}
		return true;
	}

	function formatconurl($contentid,$is_rewrite=1,$preview=0)
	{	
		global $moduleobj,$configobj,$urlcacheobj;

		$key='key-content-'.$contentid;
		$url=$urlcacheobj->get($key);
		
		if(!$url)
		{
			$coninfo=$this->get($contentid);

			if($coninfo['islink'] && preg_match('/http([s]?):\/\/(.+)/i',$coninfo['url']))
			{
				return $coninfo['url'];
			}
			else
			{
				$modinfo=$moduleobj->get(intval($coninfo['moduleid']));
				$modsettings=$configobj->getconfig($_SESSION['siteid'],$coninfo['moduleid']);
				
				if(preg_match('/http:\/\/(.+)/i',$modsettings['domain']))
				{
					if($modsettings['is_rewrite'] && $is_rewrite)
					{
						$url= $modsettings['domain'].'show-'.intval($contentid).'.html';
					}
					else
					{
						$url= $modsettings['domain'].'index.php?action=show&contentid='.intval($contentid).($preview?'&preview=1':'');
					}
				}
				else
				{
					if($modsettings['is_rewrite'] && $is_rewrite)
					{
						$url= LA_PATH.$modinfo['folder'].'/show-'.intval($contentid).'.html';
					}
					else
					{
						$url= LA_PATH.$modinfo['folder'].'/index.php?action=show&contentid='.intval($contentid).($preview?'&preview=1':'');
					}
				}
			}

			$urlcacheobj->set($key,$url);
		}

		return $url;
		
	}

	/* 会员操作 */
	function con_setdelete($contentid,$userid)
	{
		$contentid=intval($contentid);
		$userid=intval($userid);
		return $this->db->query("UPDATE `$this->table` SET `$this->table`.`isdeleted`=1 WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
	}

	function con_sethide($contentid,$userid,$ishidden=1)
	{
		$contentid=intval($contentid);
		$userid=intval($userid);
		$ishidden=intval($ishidden);
		return $this->db->query("UPDATE `$this->table` SET `$this->table`.`ishidden`=$ishidden WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
	}

	function con_settop($contentid,$userid,$moduleid)
	{
		global $settings,$_gradeid,$_point,$_amount,$memberobj,$LA;
		$contentid=intval($contentid);
		$userid=intval($userid);
		$moduleid=intval($moduleid);

		$tiao=$settings['top_type']==0?$_point:$_amount;
		if($settings['top_fee']<=$tiao)
		{
			if($settings['top_type']==0)
			{
				$memberobj->set($userid,array('point'=>max(0,$_point-$settings['top_fee'])));
			}
			else
			{
				$memberobj->set($userid,array('amount'=>max(0,$_amount-$settings['top_fee'])));
			}
			return $this->db->query("UPDATE `$this->table` SET `$this->table`.`istop`='".(TIME+max(24,$LA['top_expire'])*3600)."' WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
		}
		
		return false;
	}

	function con_settopic($contentid,$userid,$moduleid)
	{
		global $settings,$_gradeid,$_point,$_amount,$memberobj,$LA;
		$contentid=intval($contentid);
		$userid=intval($userid);
		$moduleid=intval($moduleid);

		$tiao=$settings['top_type']==0?$_point:$_amount;
		if($settings['top_fee']<=$tiao)
		{
			if($settings['top_type']==0)
			{
				$memberobj->set($userid,array('point'=>max(0,$_point-$settings['top_fee'])));
			}
			else
			{
				$memberobj->set($userid,array('amount'=>max(0,$_amount-$settings['top_fee'])));
			}
			
			return $this->db->query("UPDATE `$this->table` SET `$this->table`.`istopic`='".(TIME+max(24,$LA['top_expire'])*3600)."' WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
		}
		
		return false;
	}

	function con_setfocus($contentid,$userid,$moduleid)
	{
		global $settings,$_gradeid,$_point,$_amount,$memberobj,$LA;
		$contentid=intval($contentid);
		$userid=intval($userid);
		$moduleid=intval($moduleid);

		$tiao=$settings['top_type']==0?$_point:$_amount;
		if($settings['top_fee']<=$tiao)
		{
			if($settings['top_type']==0)
			{
				$memberobj->set($userid,array('point'=>max(0,$_point-$settings['top_fee'])));
			}
			else
			{
				$memberobj->set($userid,array('amount'=>max(0,$_amount-$settings['top_fee'])));
			}
			return $this->db->query("UPDATE `$this->table` SET `$this->table`.`isfocus`='".(TIME+max(24,$LA['top_expire'])*3600)."' WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
		}
		
		return false;
	}

	function con_refresh($contentid,$userid,$moduleid)
	{
		global $settings,$_gradeid,$_point,$_amount,$memberobj;

		$contentid=intval($contentid);
		$userid=intval($userid);
		$moduleid=intval($moduleid);
		
		$counter=$this->con_postcount($userid,$moduleid);

		if($settings['membergrade_count'][$_gradeid]>$counter)
		{
			if($counter==-1)
			{
				$this->db->insert($this->postcount_table,array('userid'=>$userid,'moduleid'=>$moduleid,'counter'=>1,'datetime'=>strtotime(date('Y-m-d',TIME))));
			}
			else
			{
				$this->db->query("UPDATE `$this->postcount_table` SET `$this->postcount_table`.`counter`=`$this->postcount_table`.`counter`+1 WHERE `$this->postcount_table`.`userid`=$userid AND  `$this->postcount_table`.`moduleid`=$moduleid AND  `$this->postcount_table`.`datetime`='".strtotime(date('Y-m-d',TIME))."'");
			}
			$this->db->query("UPDATE `$this->table` SET `$this->table`.`updatetime`=".TIME." WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
			return true;
		}
		else
		{
			$tiao=$settings['pay_type']==0?$_point:$_amount;
			if($settings['pay_fee']<=$tiao)
			{
				if($settings['pay_type']==0)
				{
					$memberobj->set($userid,array('point'=>max(0,$_point-$settings['pay_fee'])));
				}
				else
				{
					$memberobj->set($userid,array('amount'=>max(0,$_amount-$settings['pay_fee'])));
				}
				$this->db->query("UPDATE `$this->table` SET `$this->table`.`updatetime`=".TIME." WHERE `$this->table`.`contentid`=$contentid AND  `$this->table`.`userid`=$userid");
				return true;
			}
		}
		return false;
	}

	function con_postcount($userid,$moduleid)
	{
		$moduleid=intval($moduleid);
		$userid=intval($userid);
		if($userid)
		{
			$this->db->query("DELETE FROM `$this->postcount_table` WHERE `$this->postcount_table`.`moduleid`=$moduleid AND `$this->postcount_table`.`datetime`<'".strtotime(date('Y-m-d',(TIME-24*3600)))."' AND  `$this->postcount_table`.`userid`=$userid");
			$r=$this->db->fetch_one("SELECT * FROM `$this->postcount_table` WHERE `$this->postcount_table`.`userid`=$userid AND `$this->postcount_table`.`datetime`='".strtotime(date('Y-m-d',TIME))."' AND `$this->postcount_table`.`moduleid`=$moduleid");
		}
		else
		{
			$this->db->query("DELETE FROM `$this->postcount_table` WHERE `$this->postcount_table`.`moduleid`=$moduleid AND `$this->postcount_table`.`datetime`<'".strtotime(date('Y-m-d',(TIME-24*3600)))."' AND  `$this->postcount_table`.`ip`='".IP."'");
			$r=$this->db->fetch_one("SELECT * FROM `$this->postcount_table` WHERE `$this->postcount_table`.`ip`='".IP."' AND `$this->postcount_table`.`datetime`='".strtotime(date('Y-m-d',TIME))."' AND `$this->postcount_table`.`moduleid`=$moduleid");
		}
		return $r?intval($r['counter']-1):-1;
	}

	function edit($info,$contentid,$modelid=0,$extend=0)
	{
		global $modelobj,$settings,$_gradeid,$_point,$_amount,$_userid,$dataobj,$cacheobj,$memberobj;
		$modelid=intval($modelid);
		$extend=intval($extend);
		$contentid=intval($contentid);

		$coninfo=$this->get($contentid);
		if($_userid!=$coninfo['userid'])
		{
			return false;
		}

		$moduleid=$coninfo['moduleid'];
		$userid=$coninfo['userid'];

		$modelinfo=$modelobj->info($modelid);
		unset($info['istopic'],$info['istop'],$info['isfocus']);
		$info['updatetime']=TIME;
		$info['description']=$info['description']?$info['description']:sub_string(trim(strip_tags($info['content'])),255);
		$info['siteid']=intval($_SESSION['siteid']);

		$dataobj->setmodel(array(intval($modelid),intval($extend)));
		$info=$dataobj->filter($info);
		$hash_table=get_hash_table(LA_PRE.$modelinfo['tbname'].'_data',$contentid,$modelobj->subformcount(intval($extend)));
		
		$counter=$this->con_postcount($_userid,$moduleid);

		if($settings['membergrade_count'][$_gradeid]>$counter)
		{
			if($counter==-1)
			{
				$this->db->insert($this->postcount_table,array('userid'=>$_userid,'moduleid'=>$moduleid,'counter'=>1,'ip'=>IP,'datetime'=>strtotime(date('Y-m-d',TIME))));
			}
			else
			{
				$this->db->query("UPDATE `$this->postcount_table` SET `$this->postcount_table`.`counter`=`$this->postcount_table`.`counter`+1 WHERE `$this->postcount_table`.`userid`=$_userid AND  `$this->postcount_table`.`moduleid`=$moduleid AND  `$this->postcount_table`.`datetime`='".strtotime(date('Y-m-d',TIME))."'");
			}
			$this->db->update($this->table,$info,'contentid='.$contentid);
			$this->db->update(LA_PRE.$modelinfo['tbname'],$info,'id='.$contentid);
			$this->db->update($hash_table,$info,'mid='.$contentid);
		}
		else
		{
			$tiao=$settings['pay_type']==0?$_point:$_amount;
			if($settings['pay_fee']<=$tiao)
			{
				if($settings['pay_type']==0)
				{
					$memberobj->set($_userid,array('point'=>max(0,$_point-$settings['pay_fee'])));
				}
				else
				{
					$memberobj->set($_userid,array('amount'=>max(0,$_amount-$settings['pay_fee'])));
				}
				$this->db->update($this->table,$info,'contentid='.$contentid);
				$this->db->update(LA_PRE.$modelinfo['tbname'],$info,'id='.$contentid);
				$this->db->update($hash_table,$info,'mid='.$contentid);
			}
			else
			{
				return false;
			}
		}
		

		return $cacheobj->clear();
	}

	function add($info,$modelid=0,$extend=0)
	{
		global $modelobj,$settings,$_gradeid,$_point,$_amount,$_userid,$dataobj,$cacheobj,$memberobj,$LA;
		$modelid=intval($modelid);
		$extend=intval($extend);

		$typeinfo=$this->typeinfo($info['typeid']);
		
		if(!$typeinfo)
		{
			return 0;
		}
		$moduleid=$info['moduleid'];
		$info['userid']=$_userid;
		$info['modelid']=$modelid;
		$info['ip']=IP;
		$modelinfo=$modelobj->info($modelid);

		$info['updatetime']=$info['inputtime']=TIME;
		$info['description']=$info['description']?$info['description']:sub_string(trim(strip_tags($info['content'])),255);
		$info['siteid']=intval($_SESSION['siteid']);

		$dataobj->setmodel(array(intval($modelid),intval($extend)));
		$info=$dataobj->filter($info);

		if($info['isunstatus']==1)
		{
			$info['status']=0;
		}
		else
		{
			$info['status']=in_array($_gradeid,explode(',',$LA['passgradeid']))?1:0;
		}
		//print_r($info);exit();
		if($LA['bulling_english']==1 && (mb_strlen($info['content'],'gb2312')==strlen($info['content'])))
		{
			return -5;
		}

		if($LA['bulling_english']==2 && (mb_strlen($info['content'],'gb2312')==strlen($info['content'])))
		{
			$info['status']=0;
		}

		if($LA['bulling_area']==1)
		{
			$re=-1;
			$access_city=explode('|',trim($LA['access_city']));
			$forbidden_city=explode('|',$LA['forbidden_city']);
			
			foreach($forbidden_city as $_city)
			{
				if(strstr(ip2area(IP),$_city))
				{
					return -1;
				}
			}

			foreach($access_city as $_city)
			{
				if(strstr(ip2area(IP),$_city))
				{
					$re=0;
				}
			}

			if($re==-1 && trim($LA['access_city']))
			{
				return -1;
			}
		}

		if($LA['bulling_area']==2)
		{
			$re=-1;
			$access_city=explode('|',$LA['access_city']);

			$forbidden_city=explode('|',$LA['forbidden_city']);
			
			foreach($forbidden_city as $_city)
			{
				if(strstr(ip2area(IP),$_city))
				{
					$info['status']=0;
				}
			}

			foreach($access_city as $_city)
			{
				if(strstr(ip2area(IP),$_city))
				{
					$re=0;
				}
			}

			if($re==-1 && trim($LA['access_city']))
			{
				$info['status']=0;
			}
		}

		$counter=$this->con_postcount($_userid,$moduleid);
		$membergrade_count=explode(',',$settings['membergrade_count']);
		unset($settings['membergrade_count']);
		foreach($membergrade_count as $v)
		{
			$v=explode(':',$v);
			$settings['membergrade_count'][$v[0]]=$v[1];
		}
//print_r($settings);echo $_gradeid.':'.$counter;exit();
		if($settings['membergrade_count'][$_gradeid]>$counter)
		{
			if($counter==-1)
			{
				$this->db->insert($this->postcount_table,array('userid'=>$_userid,'moduleid'=>$moduleid,'ip'=>IP,'counter'=>1,'datetime'=>strtotime(date('Y-m-d',TIME))));
			}
			else
			{
				$this->db->query("UPDATE `$this->postcount_table` SET `$this->postcount_table`.`counter`=`$this->postcount_table`.`counter`+1 WHERE `$this->postcount_table`.`userid`=$_userid AND  `$this->postcount_table`.`moduleid`=$moduleid AND  `$this->postcount_table`.`datetime`='".strtotime(date('Y-m-d',TIME))."'");
			}
			$info['mid']=$info['id']=$contentid=$this->db->insert($this->table,$info,true);
			$hash_table=get_hash_table(LA_PRE.$modelinfo['tbname'].'_data',$contentid,$modelobj->subformcount(intval($extend)));
			$this->db->insert(LA_PRE.$modelinfo['tbname'],$info,true);
			$this->db->insert($hash_table,$info,true);
		}
		else
		{
			$tiao=$settings['pay_type']==0?$_point:$_amount;
			if($settings['pay_fee']<=$tiao)
			{
				if($settings['pay_type']==0)
				{
					$memberobj->set($_userid,array('point'=>max(0,$_point-$settings['pay_fee'])));
				}
				else
				{
					$memberobj->set($_userid,array('amount'=>max(0,$_amount-$settings['pay_fee'])));
				}
				$info['mid']=$info['id']=$contentid=$this->db->insert($this->table,$info,true);
				$hash_table=get_hash_table(LA_PRE.$modelinfo['tbname'].'_data',$contentid,$modelobj->subformcount(intval($extend)));
				$this->db->insert(LA_PRE.$modelinfo['tbname'],$info,true);
				$this->db->insert($hash_table,$info,true);
			}
			else
			{
				return false;
			}
		}
		
		
		$cacheobj->clear();
		if($LA['bulling_english']==2 && (mb_strlen($info['content'],'gb2312')==strlen($info['content'])))
		{
			return -6;
		}

		if($LA['bulling_area']==2)
		{
			$re=-1;
			$access_city=explode('|',$LA['access_city']);

			$forbidden_city=explode('|',$LA['forbidden_city']);
			
			foreach($forbidden_city as $_city)
			{
				if(strstr(ip2area(IP),$_city))
				{
					return -2;
				}
			}

			foreach($access_city as $_city)
			{
				if(strstr(ip2area(IP),$_city))
				{
					$re=0;
				}
			}

			if($re==-1 && trim($LA['access_city']))
			{
				return -2;
			}
		}

		if($info['isunstatus']==1)
		{
			return -2;
		}
		else
		{
			return $contentid;
		}
	}
}
?>