<?php
/********************************************************************
 *                           DirCMS内容后台操作类
 *                            -------------------
 *   begin                : 2010-5-15
 *	 update               : 2010-11-13 
 *   copyright            : (C) 2009 DirCMS
 *   website              : http://www.dircms.net
 *
 *   $Id: content.class.php,2010-11-13  22:08:24 M Exp $
********************************************************************/
class content
{
	private $table;
	private $m_table;
	private $db;
	private $member;
	private $cache;
	private $html;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=DB_PRE.'content';
		require_once DIRCMS_ROOT.'include/member.class.php';
		$this->member=new member();
		require_once(DIRCMS_ROOT.'include/cache.class.php');
		$this->cache=new cache();
		require_once(DIRCMS_ROOT.'include/html.class.php');
		$this->html=new html();
	}

	function add($info)
	{
		global $DIRCMS,$_userid,$_groupid,$_username,$_gradeid;

		$_userid=$_userid?$_userid:0;
		$info['userid']=$_userid;
		$info['inputtime']=TIME;
		$info['username']=$_username?$_username:'游客';
		$info['groupid']=implode(',',$info['groupid'])?implode(',',$info['groupid']):0;
		$posid=$info['posid'];
		$info['posid']=$info['posid']?1:0;
		$dir_catid=$catid=$info['catid'];
		$finalcatid=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
		if(!in_array($catid,$finalcatid))showmsg('只允许在终极栏目添加内容!');
		$dir_postion=$position=get_pos($catid);
		$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');

		if($_userid || $_groupid!=1)
		{
			$info['template']=$r['contenttlp'];
		}
		$dir_search=$searchoption=searchoption($catid);
		$dir_catname=$catname=$r['categoryname'];
		$catmodelid=$r['modelid'];
		$postpriv=explode(',',$r['postpriv']);
		$passpriv=explode(',',$r['passpriv']);
		if(!in_array($_groupid,$postpriv) && $_groupid!=1)showmsg('此栏目禁止您当前的会员组发布内容!');

		if($_userid)
		{
			$this->db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`point`=`".DB_PRE."member`.`point`+".intval($r['presentpoint'])." WHERE `".DB_PRE."member`.`id`=".$_userid,true);
			$this->db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`point`=`".DB_PRE."member_cache`.`point`+".intval($r['presentpoint'])." WHERE `".DB_PRE."member_cache`.`id`=".$_userid,true);
		}
		unset($r['description'],$r['url']);
		@extract($r,EXTR_SKIP);
		$info['islink']=$info['islink']?$info['islink']:0;		
		$info['tags']=substr(strtoZoneCode($info['keywords']),0,300);
		$tags=$info['tags'];
		$info['updatetime']=$info['inputtime']?$info['inputtime']:TIME;
	
		$pages=preg_split('/<div style=\\\"page-break-after: always\\\"><span style=\\\"display: none\\\">&nbsp;<\/span><\/div>/i',$info['content']);
		$info['ispage']=sizeof($pages)>1?sizeof($pages):0;
		$this->db->insert(DB_PRE.'author',array('username'=>$info['username'],'nickname'=>$info['username'],'contactway'=>' ','url'=>$info['userspace']),true);
		
		$contentid=$this->db->insert($this->table,$info,false);
		if($posid)
		{
			foreach($posid as $_posid)
			{
				$this->db->insert(DB_PRE.'content_posid',array('contentid'=>$contentid,'posid'=>intval($_posid)),true);
			}
		}
		$dir_contentid=$info['contentid']=$info['id']=$contentid;
		if(AUTOWYC)
		{
			$dict=file_get_contents(DIRCMS_ROOT.'include/Dictionary.dircms');
			$_yc=explode("\r\n",$dict);
			foreach($_yc as $w)
			{
				$_w=explode(",",$w);
				$info['content']=str_replace($_w[0],$_w[1],$info['content']);
			}
		}
		$info=dircms_stripslashes($info);

		if($pages && sizeof($pages)>1)  //  分页代码
		{
			if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
			{
				$dir=$r['categorydir'].'/'.date('YW',$info['inputtime']).'/';
			}
			else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
			{
				$dir='html/'.date('YW',$info['inputtime']).'/';
			}
			else
			{
				if($r['content_html']=='content_html_3') //日期/内容/ID.html
				{
					$dir=date('Y',$info['inputtime']).'/'.date('m',$info['inputtime']).'/';
				}
			}
			$pagenum=sizeof($pages);
			for($i=1;$i<=$pagenum;$i++)
			{
				$tp=$i==1?$dir.$contentid.'.html':$dir.$contentid.'_'.$i.'.html';
				$dir_page=$pagestring.=$r['ishtml'] && !$info['groupid'] && !$info['point']?' <a href="'.$tp.'">'.$i.'</a> ':' <a href="'.'show.php?id='.$contentid.$page.'">'.$i.'</a> ';
			}
		}
		
		@extract($info,EXTR_SKIP);
		if($info['islink'])
		{
			$info['url']=$info['url']?$info['url']:$DIRCMS['site_url'];
		}else{
			if($r['ishtml'] && !$info['groupid'] && !$info['point']) //2010-10-28 特殊权限的话不能静态化
			{
				if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
				{
					$dir=$r['categorydir'].'/'.date('YW',$info['inputtime']).'/';
					@mkdir(DIRCMS_ROOT.$dir);
					$info['url']=$dir.$contentid.'.html';
				}
				else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
				{
					$dir='html/'.date('YW',$info['inputtime']).'/';
					@mkdir(DIRCMS_ROOT.$dir);
					$info['url']=$dir.$contentid.'.html';
				}
				else
				{
					if($r['content_html']=='content_html_3') //日期/内容/ID.html
					{
						$dir=date('Y',$info['inputtime']).'/'.date('m',$info['inputtime']).'/';
						@mkdir(DIRCMS_ROOT.date('Y',$info['inputtime']));
						@mkdir(DIRCMS_ROOT.$dir);
						$info['url']=$dir.$contentid.'.html';
					}
				}
				clear_tag_cache();

				$dir_url=$DIRCMS['site_url'].$info['url'];
				ob_start();
				$modelid=intval($r['modelid']);
				$id=intval($contentid);
				if($pages && sizeof($pages)>1)     ////////////////// 分页
				{
					
					$pagenum=sizeof($pages);
					for($pagenumi=1;$pagenumi<=$pagenum;$pagenumi++)
					{
						$p=$pagenumi-1;
						$url=$t_url=$pagenumi==1?$dir.$contentid.'.html':$dir.$contentid.'_'.$pagenumi.'.html';
						$content = stripslashes($pages[$p]);
						include template($info['template']);
						$content=ob_get_contents();
						ob_clean();	
						file_put_contents(DIRCMS_ROOT.$t_url,$content);
					}
					
				}else{
					$url=$info['url'];
					include template($info['template']);
					$content=ob_get_contents();
					ob_clean();
					file_put_contents(DIRCMS_ROOT.$info['url'],$content);
				}
				unset($modelid,$id);
			}else{
				$page=$info['pages']?'&page=1':'';
				$info['url']='show.php?id='.$contentid.$page;
			}
		}

		$info['status']=in_array($_groupid,$passpriv) || $_groupid==1?1:0;
		
		$info=dircms_addslashes($info);
		$this->db->insert($this->table,$info,true);
		$model_r=cache_read('cache_model'.$catmodelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
		$this->m_table=DB_PRE.$model_r['name'];
		$this->db->insert($this->m_table,$info,false);
		$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
		$totalnumber=$r['totalnumber']+1;
		$this->db->update(DB_PRE.'category',array('totalnumber'=>$totalnumber),' id='.intval($catid));
		$this->cache->cache_category();

		// 耗时操作
		if(AUTOCREATECATEGORY)
		{
			$tmp_page=is_final_catid($info['catid'])?1:0;
			$this->html->category($info['catid'],$tmp_page);

			
			if($parentid)
			{
				$this->html->category($parentid,0);
			}
		}
		if(AUTOCREATEINDEX)
		{
			$this->html->index();
		}
		// 耗时操作
		return $contentid;
	}

	function edit($info,$id)
	{
		global $DIRCMS,$inputtime,$_groupid,$_gradeid,$_userid,$_username;

		$dir_contentid=$id=intval($id);
		$info['groupid']=$info['groupid']?$info['groupid']:array();
		$info['groupid']=implode(',',$info['groupid'])?implode(',',$info['groupid']):0;
		$posid=$info['posid'];
		$info['posid']=$info['posid']?1:0;
		if($posid)
		{
			$this->db->mysql_delete(DB_PRE.'content_posid',$id,'contentid');
			foreach($posid as $_posid)
			{
				$this->db->insert(DB_PRE.'content_posid',array('contentid'=>$id,'posid'=>intval($_posid)),true);
			}
		}
		else
		{
			$this->db->mysql_delete(DB_PRE.'content_posid',$id,'contentid');
		}
		$dir_catid=$catid=$info['catid'];
		$finalcatid=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
		if(!in_array($catid,$finalcatid))showmsg('只允许在终极栏目添加内容!');

		$dir_postion=$position=get_pos($catid);
		$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');

		$dir_search=$searchoption=searchoption($catid);
		$dir_catname=$catname=$r['categoryname'];

		$catmodelid=$r['modelid'];
		$passpriv=explode(',',$r['passpriv']);
		unset($r['description'],$r['url']);
		@extract($r,EXTR_SKIP);
		$info['islink']=$info['islink']?$info['islink']:0;		
		$info['tags']=substr(strtoZoneCode($info['keywords']),0,300);
		$tags=$info['tags'];
		$info['updatetime']=TIME;
		$info['inputtime']=strtotime($info['inputtime']);
		$pages=preg_split('/<div style=\\\"page-break-after: always\\\"><span style=\\\"display: none\\\">&nbsp;<\/span><\/div>/i',$info['content']);
		
		$info['pages']=sizeof($pages)>1?sizeof($pages):0;
		$info=dircms_stripslashes($info);

		if($pages && sizeof($pages)>1)  //  分页
		{
			if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
			{
				$dir=$r['categorydir'].'/'.date('YW',$info['inputtime']).'/';
			}
			else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
			{
				$dir='html/'.date('YW',$info['inputtime']).'/';
			}
			else
			{
				if($r['content_html']=='content_html_3') //日期/内容/ID.html
				{
					$dir=date('Y',$info['inputtime']).'/'.date('m',$info['inputtime']).'/';
				}
			}
			$pagenum=sizeof($pages);

			for($i=1;$i<=$pagenum;$i++)
			{
				$tp=$i==1?$dir.$id.'.html':$dir.$id.'_'.$i.'.html';
				$pagestring.=$r['ishtml'] && !$info['groupid'] && !$info['point']?' <a href="'.$tp.'">'.$i.'</a> ':' <a href="'.'show.php?id='.$id.$page.'">'.$i.'</a> ';
			}
		}
		$dir_page=$pagestring;

		@extract($info,EXTR_SKIP,$i);
		if($info['islink'])
		{
			$info['url']=$info['url']?$info['url']:$DIRCMS['site_url'];
		}else{
			if($r['ishtml'] && !$info['groupid'] && !$info['point']) //2010-10-28 特殊权限的话不能静态化
			{
				if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
				{
					$dir=$r['categorydir'].'/'.date('YW',$info['inputtime']).'/';
					@mkdir(DIRCMS_ROOT.$dir);
					$info['url']=$dir.$id.'.html';
				}
				else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
				{
					$dir='html/'.date('YW',$info['inputtime']).'/';
					@mkdir(DIRCMS_ROOT.$dir);
					$info['url']=$dir.$id.'.html';
				}
				else
				{
					if($r['content_html']=='content_html_3') //日期/内容/ID.html
					{
						$dir=date('Y',$info['inputtime']).'/'.date('m',$info['inputtime']).'/';
						@mkdir(DIRCMS_ROOT.date('Y',$info['inputtime']));
						@mkdir(DIRCMS_ROOT.$dir);
						$info['url']=$dir.$id.'.html';
					}
				}
				
				$dir_url=$DIRCMS['site_url'].$info['url'];
				clear_tag_cache();

				ob_start();			
				$modelid=intval($r['modelid']);
				if($pages && sizeof($pages)>1)
				{
					$pagenum=sizeof($pages);
					for($pagenumi=1;$pagenumi<=$pagenum;$pagenumi++)
					{
						$p=$pagenumi-1;
						$content = stripslashes($pages[$p]);
						include template($info['template']);
						$content=ob_get_contents();
						ob_clean();
						$t_url=$pagenumi==1?$dir.$id.'.html':$dir.$id.'_'.$pagenumi.'.html';
						@unlink(DIRCMS_ROOT.$t_url);
						file_put_contents(DIRCMS_ROOT.$t_url,$content);
					}
				}else{
					include template($info['template']);
					$content=ob_get_contents();
					ob_clean();
					@unlink(DIRCMS_ROOT.$info['url']);
					file_put_contents(DIRCMS_ROOT.$info['url'],$content);
				}
				unset($modelid);
			}else{
				if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
				{
					$dir=$r['categorydir'].'/'.date('YW',$info['inputtime']).'/';
					@unlink($dir.$id.'.html');
				}
				else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
				{
					$dir='html/'.date('YW',$info['inputtime']).'/';
					@unlink($dir.$id.'.html');
				}
				else
				{
					if($r['content_html']=='content_html_3') //日期/内容/ID.html
					{
						$dir=date('Y',$info['inputtime']).'/'.date('m',$info['inputtime']).'/';
						@unlink($dir.$id.'.html');
					}
				}
				$page=$info['pages']?'&page=1':'';
				$info['url']='show.php?id='.$id.$page;
			}
		}

		$info['status']=in_array($_groupid,$passpriv) || $_groupid==1?1:0;
		$info=dircms_addslashes($info);
		$this->db->update($this->table,$info,' id='.intval($id));
		$model_r=cache_read('cache_model'.$catmodelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
		
		$this->m_table=DB_PRE.$model_r['name'];
		$this->db->update($this->m_table,$info,' contentid='.intval($id));

		// 耗时操作
		if(AUTOCREATECATEGORY)
		{
			$tmp_page=is_final_catid($info['catid'])?1:0;		
			$this->html->category($info['catid'],$tmp_page);
			
			if($parentid)
			{
				$this->html->category($parentid,0);
			}
		}
		if(AUTOCREATEINDEX)
		{
			$this->html->index();
		}
		
		// 耗时操作
		
		return $id;
	}

	function order($orderby,$id,$catid=array())
	{
		$i=0;
		foreach($id as $val)
		{
			$this->db->query("UPDATE `{$this->table}` SET $this->table.orderby=".intval($orderby[$i])." WHERE $this->table.id=".intval($val),true);		
			$i++;
		}
		$catid=array_unique($catid);
		if($catid)foreach($catid as $v)
		{
			$tmp_page=is_final_catid($v)?1:0;
			$this->html->category($v,$tmp_page);
		}
		clear_tag_cache();
		return true;
	}

	function pass($idarr,$catid=array(),$status=1)
	{
		if(!$idarr)return false;
		$i=0;
		foreach($idarr as $id)
		{
			$this->db->update($this->table,array('status' => $status),' id='.intval($id));
			$i++;
		}
		$catid=array_unique($catid);
		if($catid)foreach($catid as $v)
		{
			$tmp_page=is_final_catid($v)?1:0;
			$this->html->category($v,$tmp_page);
		}
		clear_tag_cache();
		return true;
	}

	function delete($idarr)
	{
		if(!$idarr)return false;
		foreach($idarr as $id)
		{
			$id=intval($id);
			if($id)
			{
				$r=$this->db->fetch_one("SELECT `{$this->table}`.`url`,`{$this->table}`.`catid`,`{$this->table}`.`pages`,`{$this->table}`.`inputtime`,`{$this->table}`.`thumb` FROM `{$this->table}` WHERE `{$this->table}`.`id`={$id}");
				if($r['thumb'])@unlink(DIRCMS_ROOT.$r['thumb']); /////////////// 删除缩略图
				$cat_r=cache_read('category'.$r['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				
				if($cat_r['ishtml'])
				{
					@unlink(DIRCMS_ROOT.$r['url']);
					for($i=2;$i<=$r['pages'];$i++)
					{
						@unlink(DIRCMS_ROOT.dirname($r['url']).'/'.$id.'_'.$i.'.html');
					}
				}

				$model_r=cache_read('cache_model'.$cat_r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				$this->m_table=DB_PRE.$model_r['name'];
				
				if($model_r['name'])
				{
					$this->db->query("DELETE $this->table,$this->m_table FROM $this->table,$this->m_table WHERE {$this->table}.id={$this->m_table}.contentid AND {$this->table}.id={$id}",true);	
					$totalnumber=max($cat_r['totalnumber']-1,0);
					$this->db->update(DB_PRE.'category',array('totalnumber'=>$totalnumber),' id='.intval($r['catid']));
				}
				
				$this->db->query("DELETE FROM `".DB_PRE."sign` WHERE `".DB_PRE."sign`.`contentid`=$id");
			}
		}
		//exit('here');
		$this->cache->cache_category();
		$tmp_page=is_final_catid($r['catid'])?1:0;
		$this->html->category($r['catid'],$tmp_page);
		clear_tag_cache();
		return true;
	}

	function getfield($id,$field='id')
	{	
		if(!in_array($field,$this->db->get_fields($this->table)))return '';
		$r=$this->db->fetch_one("SELECT `{$this->table}`.`{$field}` FROM `{$this->table}` WHERE `{$this->table}`.`id`={$id}");
		return $r?$r[$field]:'';
	}

	function get($id,$catid=0,$mtable='',$isbase=false)  ////////////////////根据ID读取文章内容 
	{
		$t=$this->db->fetch_one("SELECT `{$this->table}`.* FROM `{$this->table}` WHERE `{$this->table}`.`id`={$id}");
		if(!$t)return array();
		if($isbase===true) return $t;
		else
		{
			if($mtable) return $this->db->fetch_one("SELECT a.*,b.* FROM `{$this->table}` a LEFT JOIN `{$mtable}` b ON a.id=b.contentid AND a.id={$id}");
			if($catid)
			{
				$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				$r=cache_read('cache_model'.$r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				$this->m_table=DB_PRE.$r['name'];
				//exit("SELECT a.*,b.* FROM `{$this->table}` a LEFT JOIN `{$this->m_table}` b ON a.id=b.contentid WHERE a.id={$id}");
				return $this->db->fetch_one("SELECT a.*,b.* FROM `{$this->table}` a LEFT JOIN `{$this->m_table}` b ON a.id=b.contentid WHERE a.id={$id}");
			}
			$r=$this->db->fetch_one("SELECT `{$this->table}`.* FROM `{$this->table}` WHERE `{$this->table}`.id={$id}");
			$_r=cache_read('category'.$r['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			$_r=cache_read('cache_model'.$_r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$this->m_table=DB_PRE.$_r['name'];
			$r2=$this->db->fetch_one("SELECT `{$this->m_table}`.* FROM `{$this->m_table}` WHERE `{$this->m_table}`.contentid={$id}");
			return array_merge($r,$r2); /////////////将数组合并
		}
	}
}
?>