<?php
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	$action=empty($action)?'manage':trim($action);
	require_once(DIRCMS_ROOT.'include/form.class.php');	
	$form=new form();
	require_once(DIRCMS_ROOT.'include/admin/content.class.php');
	$content=new content();
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	require_once(DIRCMS_ROOT.'include/content.input.class.php');
	switch($action)
	{
		case 'manage':
			$catid=$catid?$catid:0;
			$catid=$info['catid']?$info['catid']:$catid;
			$catidselect=$form->catid(0,$catid);
			$where='userid='.intval($_userid);
			$where=$catid?$where." AND ".getCatid(intval($catid)):$where;
			$k=strtoZoneCode($k);
			$where=!empty($k)?$where." AND MATCH(tags) AGAINST ('$k' IN BOOLEAN MODE)":$where; // 使用全文检索
			$orderby="orderby DESC,id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'content',$where,$orderby,$page,15);
			if(is_array($result))
			{
				foreach($result as $t_key => $t_r)
				{
					$cat_r=cache_read('category'.$t_r['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
					$result[$t_key]['catname']=$cat_r['categoryname'];
				}
			}
			$pages=$l->pages;
			$k=zoneCodetoStr($k);
			if($field=='catid')
			{
				//echo $catid;
				$_category=cache_read('category'.$fieldvalue.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				//print_r($_category);
			}		
			include member_tlp('content_manage');
			break;
		case 'delete':
			if(!$id)showmsg('请选择需要删除的信息!');
			$id=intval($id);
			$r=$db->fetch_one("SELECT userid,status FROM ".DB_PRE."content WHERE id=".$id);
			if(!$r || $r['userid']!=$_userid)showmsg_nourl('错误的请求!');	
			if($r['status'])showmsg('已通过审核的信息不能删除!');
			if($content->delete($id))showmsg('信息成功删除!');
			else showmsg('信息删除失败!','index.php?file=content&action=manage');
			break;
		case 'top':
			if(!$id)showmsg('请选择需要置顶的信息!');
			if($_point<intval(TOPPOINT))showmsg('您的积分不够不能进行置顶操作!','',2000);
			$id=intval($id);

			$db->update(DB_PRE.'member', array('point'=>intval($_point-intval(TOPPOINT))), 'id='.$_userid);
			$db->update(DB_PRE.'member_cache', array('point'=>intval($_point-intval(TOPPOINT))), 'id='.$_userid);
			$colorrand=array('#ff0000','#00ff00','#0000ff');
			$colorrand=$colorrand[mt_rand(0,2)];
			$db->update(DB_PRE.'content', array('posid'=>1,'style'=>'color:'.$colorrand.';font-weight:bold'), 'id='.$id.' AND userid='.$_userid);
			$db->query("DELETE FROM ".DB_PRE."content_posid WHERE contentid={$id} AND posid=12");
			$db->insert(DB_PRE.'content_posid',array('contentid'=>$id,'posid'=>12));

			$r=$db->fetch_one("SELECT catid FROM ".DB_PRE."content WHERE id=".$id);
			clear_tag_cache();
			require_once(DIRCMS_ROOT.'include/html.class.php');
			$html=new html();
			$html->category($r['catid'],1);
			$r=cache_read('category'.$r['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
			$html->category($r['parentid'],0);

			showmsg('信息成功置顶!');
			break;
		case 'edit':
			if(isset($do_submit))
			{
				$id=intval($id);
				$r=$db->fetch_one("SELECT userid FROM ".DB_PRE."content WHERE id=".$id);
				if(!$r || $r['userid']!=$_userid)showmsg_nourl('错误的请求!');
				$r=cache_read('category'.$info['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if(!$r || !intval($info['catid']))showmsg('指定栏目不允许添加内容!');
				$info['template']=$r['contenttlp'];
				$info['updatetime']=date('Y-m-d H:i:s',TIME);

				$privcheck=cache_read('category'.$info['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				$privcheck['postpriv']=explode(',',$privcheck['postpriv']);
				if($privcheck['ispage'] || !$privcheck['isshow'] || (!in_array($_groupid,$privcheck['postpriv']) && $_groupid!=1)) // 权限检查
				{
					showmsg_nourl('错误的请求!');
				}

				$content_input=new content_input($r['modelid']);
				$info=$content_input->check($info);
				$info['description']=isset($info['description'])?$info['description']:sub_string(strip_tags($info['content']),200,'');
				$info['keywords']=isset($info['keywords'])?$info['keywords']:$info['title'];
				$info['description']=($auto_description && !$info['description'])?sub_string(strip_tags($info['content']),200,''):sub_string($info['description'],200,'');
				$info['content']=add_alt($info['content'],$info['title']);
				$info['title']=sub_string($info['title'],160,'');
				$info['keywords']=sub_string($info['keywords'],100,'');
				if($auto_image)
				{
					$info['content']=down_url_images($info['content']);
				}

				if($auto_thumb && !$info['thumb'])
				{
					$imgindex=intval($imgindex);
					$thumb_r=get_images($info['content']);
					$imgindex=$imgindex>count($thumb_r)?count($thumb_r):$imgindex;
					$imgindex--;
					if($thumb_r && $r['isthumb'])
					{
						require_once(DIRCMS_ROOT.'include/image.class.php');
						$image=new image();
						if(substr($thumb_r[$imgindex],0,7)!='http://')
						{
							$info['thumb']=substr($image->thumb($_SERVER['DOCUMENT_ROOT'].$thumb_r[$imgindex],$r['thumb_width'],$r['thumb_height']),strlen($_SERVER['DOCUMENT_ROOT']));
						}
						else
						{
							$info['thumb']=$thumb_r[$imgindex];
						}
					}
					else
					{
						$info['thumb']=$thumb_r[$imgindex];
					}
				}
				
				$info['thumb']=substr($info['thumb'],0,strlen(DIRCMS_PATH))==DIRCMS_PATH?substr($info['thumb'],strlen(DIRCMS_PATH)):$info['thumb'];
				
				if($content->edit($info,$id))
				{
					
					showmsg('编辑成功!','index.php?file=content&action=manage');
				}
				else 
				{
					showmsg('编辑失败!');		
				}
			}
			$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/'); 
			if(!$r)showmsg_nourl('错误的请求!');

			$modelid=$r['modelid'];
			$r=cache_read('cache_model'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$m_table=$r['name'];
			$c=$db->fetch_one("SELECT a.*,b.* FROM ".DB_PRE."content a LEFT JOIN ".DB_PRE.$m_table." b ON a.id=b.contentid WHERE a.id=".intval($id));
			require_once(DIRCMS_ROOT.'include/form.class.php');
			$f=new form($modelid);
			$base_r=$f->get($c);
			$catid_select=$f->catid($catid,$catid,true);			
			@extract($c);
			if($userid!=$_userid)showmsg_nourl('错误的请求!');	
			include member_tlp('content_edit');
			break;
		case 'add':
			if(isset($do_submit))
			{
				$r=cache_read('category'.$info['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if(!$r || !intval($info['catid']))showmsg('指定栏目不允许添加内容!');
				$info['template']=$r['contenttlp'];
				$info['inputtime']=$info['updatetime']=TIME;

				$privcheck=cache_read('category'.$info['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				$privcheck['postpriv']=explode(',',$privcheck['postpriv']);
				if($privcheck['ispage'] || !$privcheck['isshow'] || (!in_array($_groupid,$privcheck['postpriv']) && $_groupid!=1)) // 权限检查
				{
					showmsg_nourl('错误的请求!');
				}

				$content_input=new content_input($r['modelid']);
				$info=$content_input->check($info);
				$info['description']=isset($info['description'])?$info['description']:sub_string(strip_tags($info['content']),200,'');
				$info['keywords']=isset($info['keywords'])?$info['keywords']:$info['title'];
				$info['description']=($auto_description && !$info['description'])?sub_string(strip_tags($info['content']),200,''):sub_string($info['description'],200,'');
				$info['content']=add_alt($info['content'],$info['title']); //////////////////////自动加 ALT标签
				$info['title']=sub_string($info['title'],160,'');
				$info['keywords']=sub_string($info['keywords'],100,'');

				if($auto_image)
				{
					$info['content']=down_url_images($info['content']);
				}

				if($auto_thumb && !$info['thumb'])
				{
					$imgindex=intval($imgindex);
					$thumb_r=get_images($info['content']);
					$imgindex=$imgindex>count($thumb_r)?count($thumb_r):$imgindex;
					$imgindex--;
					if($thumb_r && $r['isthumb'])
					{
						require_once(DIRCMS_ROOT.'include/image.class.php');
						$image=new image();
						if(substr($thumb_r[$imgindex],0,7)!='http://')
						{
							$info['thumb']=substr($image->thumb($_SERVER['DOCUMENT_ROOT'].$thumb_r[$imgindex],$r['thumb_width'],$r['thumb_height']),strlen($_SERVER['DOCUMENT_ROOT']));
						}
						else
						{
							$info['thumb']=$thumb_r[$imgindex];
						}
					}
					else
					{
						$info['thumb']=$thumb_r[$imgindex];
					}
				}
				$info['thumb']=substr($info['thumb'],0,strlen(DIRCMS_PATH))==DIRCMS_PATH?substr($info['thumb'],strlen(DIRCMS_PATH)):$info['thumb'];
				if($content->add($info))showmsg('发布成功!','index.php?file=content&action=add&catid='.$info['catid']);
				else showmsg('发布失败!','index.php?file=content&action=add&catid='.$info['catid']);
			}
			$catid=$info['catid']?$info['catid']:($catid?$catid:0);
			if(!$catid)
			{
				showmsg_nourl('错误的请求!');
			}
			else
			{
				$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/'); 
				if(!$r && $catid)showmsg_nourl('错误的请求!');
				$template=$r['contenttlp'];
				$modelid=$r['modelid'];
			}
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$f=new form($modelid);
			$base_r=$f->get();	
			$catid_select=$f->catid($catid,$catid,true);
			$finalcatid_r=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');		
			include member_tlp('content_add');
			break;
		case 'sign':
			$where='userid='.$_userid;
			$where=$contentid?$where." AND contentid=".$contentid:$where;
			$k=trim($k);
			$where=!empty($k)?$where." AND	title like '%{$k}%'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'sign',$where,$orderby,$page,15);
			$pages=$l->pages;
			include member_tlp('sign_manage');
			break;
		default:
			showmsg('参数有误,返回网站首页!',$DIRCMS['site_url']);
			break;
	}
?>