<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	$action=empty($action)?'manage':trim($action);
	
	require_once(DIRCMS_ROOT.'include/html.class.php');
	$html=new html();

	$c=new cache();
	switch($action)
	{
		case 'category_manage':	
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');
			
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			require_once(DIRCMS_ROOT.'include/form.class.php');	

			if(isset($do_submit))
			{
				if($categoryname)foreach($categoryname as $key => $value)
				{
					$orderby=intval($categoryorderby[$key]);
					$db->query("UPDATE `".DB_PRE."category` SET `".DB_PRE."category`.`categoryname`='{$value}',`".DB_PRE."category`.`orderby`={$orderby} WHERE `".DB_PRE."category`.`id`=".intval($key),true);
				}
				$c->cache_category();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=content&action=category_manage');
			}
			function tree ($parentid=0)
			{
				global $db,$_roleid,$_categorypriv;
				$r=cache_read('parent'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if($r)foreach($r as $_r)
				{
					if(in_array($_r['id'],$_categorypriv) || $_roleid==1)
					{
						$lockstr=$_r['isshow']?'启用 <a href="'.ADMIN_FILE.'?file=content&action=category_lock&isshow=0&id='.$_r['id'].'"><u>禁用</u></a>':'<font color="#666666">禁用</font> <a href="'.ADMIN_FILE.'?file=content&action=category_lock&isshow=1&id='.$_r['id'].'"><u>启用</u></a>';
						echo '<tr>
				<td width="67" align="center"><input type="text" size="2" name="categoryorderby['.$_r['id'].']" value="'.$_r['orderby'].'" /></td>
				<td width="48" align="center">'.$_r['id'].'</td>
				<td width="250" align="left">'.str_repeat('&nbsp;',($_r['grade']-1)*3).'└─ <input type="text" size="15" name="categoryname['.$_r['id'].']" value="'.$_r['categoryname'].'" /></td>
				 <td width="180" align="center"><input type="text" readonly="1" size="25" ondblclick="javascript:copy(\'{{getCategoryname('.$_r['id'].')}}\')";  value="{{getCategoryname('.$_r['id'].')}}" /></td>
				<td width="59" align="center">'.($_r['ispage']?'<font color="#666666">单页</font>':'普通').'</td>
				<td width="80" align="center">'.$lockstr.'</td>
				<td width="300" align="center"><a href="'.ADMIN_FILE.'?file=content&action=category_add&modelid='.$_r['modelid'].'&parentid='.$_r['id'].'"><u>添加栏目</u></a> &nbsp;<a href="'.ADMIN_FILE.'?file=content&action=page_add&parentid='.$_r['id'].'"><u>添加单页</u></a> &nbsp;<a href="'.ADMIN_FILE.'?file=content&action='.($_r['ispage']?'page_edit':'category_edit').'&id='.$_r['id'].'"><u>配置</u></a> &nbsp;<a href="javascript:confirmUrl(\'确实要删除栏目 '.$_r['categoryname'].' 吗?该操作不可逆!\',\''.ADMIN_FILE.'?file=content&action=category_delete&id='.$_r['id'].'\')"><u>删除</u></a></td>
			</tr>';
						tree($_r['id']);
					}
				}
			}
			$category=cache_read('parent.cache.php',DIRCMS_ROOT.'data/cache_category/');
			include admin_tlp('category_manage');			
			break;
		case 'category_lock':
			if(!in_array($id,$_categorypriv) && $_roleid!=1)
			{
				showmsg_nourl('您没有该操作的权限!');
			}

			$db->update(DB_PRE.'category',array('isshow'=>$isshow),'id='.$id);
			$db->update(DB_PRE.'category',array('isshow'=>$isshow),'parentid='.$id);
			$c->cache_category();
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1');
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=content&action=category_manage');
			break;
		case 'category_delete':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');
			@set_time_limit(0);

			if(!in_array($id,$_categorypriv) && $_roleid!=1)
			{
				showmsg_nourl('您没有该操作的权限!');
			}

			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			require_once(DIRCMS_ROOT.'include/form.class.php');	

			function delete_category($id)
			{
				global $db,$c;
				$r=cache_read('category'.$id.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				delete_dir(DIRCMS_ROOT.$r['categorydir']);
				delete_file(DIRCMS_ROOT.$r['categorypic']);
				$db->mysql_delete(DB_PRE.'category',intval($id));
				$model_r=cache_read('cache_model'.$r['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				$db->mysql_delete(DB_PRE.'category',intval($id),'id'); 
				
				$r=$db->fetch_all("SELECT id,url FROM ".DB_PRE."content WHERE catid=".$id);
				foreach($r as $_r)
				{	
					$db->query("DELETE ".DB_PRE."content,".DB_PRE.$model_r['name']." FROM ".DB_PRE."content,".DB_PRE.$model_r['name']." WHERE ".DB_PRE."content.id=".DB_PRE.$model_r['name'].".contentid AND ".DB_PRE."content.id=".$_r['id'],true);
					@unlink(DIRCMS_ROOT.$_r['url']);
				}
				$r=cache_read('parent'.$id.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if(is_array($r) && $r)
				{
					foreach($r as $_r)
					{
						delete_category($_r['id']);
					}
				}
				else 
				{
					return false;
				}
			}
			delete_category($id);
			$c->cache_category();
			$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1');
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=content&action=category_manage');
			break;
		case 'category_edit':
			@set_time_limit(0);
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			if(!in_array($id,$_categorypriv) && $_roleid!=1)
			{
				showmsg_nourl('您没有该操作的权限!');
			}

			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			require_once(DIRCMS_ROOT.'include/form.class.php');	

			$val_r=$db->fetch_one("SELECT * FROM `".DB_PRE."category`  WHERE `".DB_PRE."category`.`id`=".intval($id));
			$oldmodelid=$val_r['modelid'];
			if(isset($do_submit))
			{
				if(strlen($info['categoryname'])<2||strlen($info['categoryname'])>100)showmsg('栏目名称由2-100个字符组成!');
				if(!preg_match('/^[a-z0-9_]{2,50}$/i',$info['categorydir']))showmsg('栏目目录由2-50个字母数字下划线组成!');
				if(strlen($info['meta_title'])>200)showmsg('meta_title不能超过200个字符!');
				if(strlen($info['meta_keywords'])>200)showmsg('meta_keywords不能超过200个字符!');

				if(is_uploaded_file($_FILES['categorylogo']['tmp_name']))
				{
					@move_uploaded_file($_FILES['categorylogo']['tmp_name'],DIRCMS_ROOT.'images/logo/'.str_replace('/','_',$val_r['categorydir']).'.'.get_fileext($_FILES['categorylogo']['name']));
					$info['categorypic']='images/logo/'.str_replace('/','_',$val_r['categorydir']).'.'.get_fileext($_FILES['categorylogo']['name']);
				}
				else
				{
					$info['categorypic']=$val_r['categorypic'];
				}

				$r=cache_read('category'.$id.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				$newmodelid=$info['modelid'];

				if($oldmodelid!=$newmodelid) /* 开始 更换内容表模型 */
				{
					$tmp_content=$db->fetch_all("SELECT `".DB_PRE."content`.`id` FROM `".DB_PRE."content` WHERE `".DB_PRE."content`.`catid`=$id");
					$db->update(DB_PRE."content",array('template'=>$info['contenttlp']),'catid='.$id);
					$new_md=cache_read('cache_model'.$newmodelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
					$new_m_table=DB_PRE.$new_md['name'];
					$old_md=cache_read('cache_model'.$oldmodelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
					$old_m_table=DB_PRE.$old_md['name'];
					foreach($tmp_content as $_tmpcid)
					{
						$tmp_data=$db->fetch_one("SELECT `".$old_m_table."`.`content` FROM `".$old_m_table."` WHERE `".$old_m_table."`.`contentid`=".$_tmpcid['id']);
						$db->mysql_delete($old_m_table,$_tmpcid['id'],'contentid');
						$db->insert($new_m_table,array('contentid'=>$_tmpcid['id'],'content'=>$tmp_data['content']));
					}
				}

				$oldname=$r['categorydir'];
				
				$parentid=$r['parentid'];
				if($parentid) // 子栏目
				{
					$r=cache_read('category'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if($info['domain']) //绑定二级域名
					{
						$info['url']=$info['domain'];
						$info['categorydir']=$r['categorydir'].'/'.$info['categorydir'];
					}
					else
					{
						if($r['domain'])
						{
							$info['url']=$r['domain'].$info['categorydir'];
							$info['categorydir']=$r['categorydir'].'/'.$info['categorydir'];
						}
						else
						{
							$info['categorydir']=$r['categorydir'].'/'.$info['categorydir'];
							$info['url']=$info['categorydir'];
						}
					}
				}
				else
				{
					if($info['domain'])
					{
						$info['url']=$info['domain'];
					}
					else
					{
						$info['url']=$info['categorydir'];
					}
				}
				
				if($info['ishtml'])
				{
					$htmlnotice=$r['content_html']==$info['content_html']?false:true;

					if($info['categorydir']!=$r['categorydir'] || !file_exists(DIRCMS_ROOT.$info['categorydir']))
					{
						if(file_exists(DIRCMS_ROOT.$info['categorydir']) && $info['categorydir']!=$oldname)
						{
							showmsg('目录'.$info['categorydir'].'已经存在，请更换!');
						}
						make_dir($oldname);

						if($info['categorydir']!=$oldname)
						{
							rename(DIRCMS_ROOT.$oldname,DIRCMS_ROOT.$info['categorydir']);
						}						
					}
					
				}
				else 
				{
					$info['url']='category.php?id='.$id;
					delete_dir($r['categorydir']);
				}
				$info['postpriv']=implode(',',$info['postpriv']);
				$info['passpriv']=implode(',',$info['passpriv']);
				$info['pagesize']=intval($info['pagesize'])?intval($info['pagesize']):PAGESIZE;
				$db->update(DB_PRE.'category',$info,DB_PRE.'category.id='.intval($id));
				$c->cache_category();
				$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1');
				
				if($htmlnotice)
				{
					showmsg('操作成功，请生成栏目和内容!','?file=content&action=category_manage',3000);
				}
				else
				{
					showmsg('操作成功!','?file=content&action=category_manage');
				}
			}

			$val_r['categorydir']=strpos($val_r['categorydir'],'/')?substr(strrchr($val_r['categorydir'],'/'),1):$val_r['categorydir'];
			$modelselect='<select name="info[modelid]" onchange="self.location.href=\''.ADMIN_FILE.'?file=content&action=category_edit&modelid=\'+this.value+\'&id='.$id.'\'">'."\n\t";
			$tmp=cache_read('cache_model.cache.php',DIRCMS_ROOT.'data/cache_model/');
			if($tmp)foreach($tmp as $tr)
			{
				if($modelid)
				{
					$optionselect=$modelid==$tr['id']?' selected="selected"':'';
				}
				else
				{
					$optionselect=$val_r['modelid']==$tr['id']?' selected="selected"':'';
				}
				$modelselect.='<option value='.$tr['id'].$optionselect.'>'.$tr['modelname'].'</option>'."\n\t";
			}
			$modelselect.='</select>';
			$indextlpselect='<select name="info[categorytlp]" id="categorytlp">';
			$listtlpselect='<select name="info[listtlp]" id="listtlp">';
			$showtlpselect='<select name="info[contenttlp]" id="contenttlp">';
			$cache=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			if(!$modelid)
			{
				if($cache)foreach($cache as $r)
				{
					if(substr($r['template'],0,9)=='category_')
					{
						$isselected=$val_r['categorytlp']==$r['template']?' selected="selected"':'';
						$indextlpselect.='<option value="'.$r['template'].'"'.$isselected.'>'.$r['name'].'('.$r['template'].'.html)</option>';
					}
					if(substr($r['template'],0,15)=='final_category_')
					{
						$isselected=$val_r['listtlp']==$r['template']?' selected="selected"':'';
						$listtlpselect.='<option value="'.$r['template'].'"'.$isselected.'>'.$r['name'].'('.$r['template'].'.html)</option>';
					}
					if(substr($r['template'],0,4)=='show')
					{
						$isselected=$val_r['contenttlp']==$r['template']?' selected="selected"':'';
						$showtlpselect.='<option value="'.$r['template'].'"'.$isselected.'>'.$r['name'].'('.$r['template'].'.html)</option>';
					}
				}
			}
			else
			{
				$modeltlp=cache_read('cache_model'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				if($cache)foreach($cache as $r)
				{
					if(substr($r['template'],0,9)=='category_')
					{
						$isselect=$modeltlp['categorytlp']==$r['template']?' selected="selected"':'';
						$indextlpselect.='<option value="'.$r['template'].'"'.$isselect.'>'.$r['name'].'('.$r['template'].'.html)</option>';
					}

					if(substr($r['template'],0,15)=='final_category_')
					{
						$isselect=$modeltlp['listtlp']==$r['template']?' selected="selected"':'';
						$listtlpselect.='<option value="'.$r['template'].'"'.$isselect.'>'.$r['name'].'('.$r['template'].'.html)</option>';
					}

					if(substr($r['template'],0,4)=='show')
					{
						$isselect=$modeltlp['contenttlp']==$r['template']?' selected="selected"':'';
						$showtlpselect.='<option value="'.$r['template'].'"'.$isselect.'>'.$r['name'].'('.$r['template'].'.html)</option>';
					}
				}
			}
			$indextlpselect.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'categorytlp\').value);" />';
			$listtlpselect.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'listtlp\').value);" />';
			$showtlpselect.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'contenttlp\').value);" />';	

			$membergroup=cache_read('membergroup.cache.php',DIRCMS_ROOT.'data/cache/');
			$val_r['postpriv']=explode(',',$val_r['postpriv']);
			$val_r['passpriv']=explode(',',$val_r['passpriv']);
			include admin_tlp('category_edit');
			break;
		case 'category_add':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			require_once(DIRCMS_ROOT.'include/form.class.php');	

			if(isset($do_submit))
			{	
				if(!$info['modelid'])showmsg('请选择内容模型!');
				if(strlen($info['categoryname'])<2||strlen($info['categoryname'])>100)showmsg('栏目名称由2-100个字符组成!');
				if(!preg_match('/^[a-z0-9_]{2,50}$/i',$info['categorydir']))showmsg('栏目目录由2-50个字母数字下划线组成!');
				if(strlen($info['meta_title'])>200)showmsg('meta_title不能超过200个字符!');
				if(strlen($info['meta_keywords'])>200)showmsg('meta_keywords不能超过200个字符!');
				if(!preg_match('/^[0-9a-z_]{1,50}$/i',$info['categorydir']))showmsg('目录名称由1-50位数字，字母，下划线组成，请更换!');
				$parentid=$info['parentid'];
				if($parentid) // 子栏目
				{
					$r=cache_read('category'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if($info['domain']) //绑定二级域名
					{
						$info['url']=$info['domain'];
						$info['categorydir']=$r['categorydir'].'/'.$info['categorydir'];
					}
					else
					{
						if($r['domain'])
						{
							$info['url']=$r['domain'].$info['categorydir'];
							$info['categorydir']=$r['categorydir'].'/'.$info['categorydir'];
						}
						else
						{
							$info['categorydir']=$r['categorydir'].'/'.$info['categorydir'];
							$info['url']=$info['categorydir'];
						}
					}
					$info['grade']=$r['grade']+1;
				}
				else
				{
					if($info['domain'])
					{
						$info['url']=$info['domain'];
					}
					else
					{
						$info['url']=$info['categorydir'];
					}
				}
				if(file_exists(DIRCMS_ROOT.$info['categorydir']))
				{
					showmsg('目录'.$info['categorydir'].'已经存在，请更换!');
				}
				make_dir($info['categorydir']);
				if(is_uploaded_file($_FILES['categorylogo']['tmp_name']))
				{
					@move_uploaded_file($_FILES['categorylogo']['tmp_name'],DIRCMS_ROOT.'images/logo/'.str_replace('/','_',$info['categorydir']).'.'.get_fileext($_FILES['categorylogo']['name']));
					$info['categorypic']='images/logo/'.str_replace('/','_',$info['categorydir']).'.'.get_fileext($_FILES['categorylogo']['name']);
				}
				else
				{
					$info['categorypic']='';
				}
				
				$info['pagesize']=intval($info['pagesize'])?intval($info['pagesize']):PAGESIZE;
				$info['postpriv']=implode(',',$info['postpriv']);
				$info['passpriv']=implode(',',$info['passpriv']);
				$id=$db->insert(DB_PRE.'category',$info);

				if(!$info['ishtml'])
				{
					$info['url']='category.php?id='.$id;
					$db->update(DB_PRE.'category',array('url'=>$info['url']),'id='.intval($id));
				}				
				$c->cache_category();
				$db->update(DB_PRE.'category',array('isend'=>0),'id='.$parentid);
				$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1');
				showmsg($LANG['MANAGE_SUCEESS']);
			}

			$parentid=$parentid?$parentid:0;
			$modelselect='<select name="info[modelid]" onchange="self.location.href=\''.ADMIN_FILE.'?file=content&action=category_add&modelid=\'+this.value+\'&parentid='.$parentid.'\'"><option value=0>选择内容模型</option>'."\n\t";
			$tmp=cache_read('cache_model.cache.php',DIRCMS_ROOT.'data/cache_model/');
			if($tmp)foreach($tmp as $tr)
			{
				$optionselect=$modelid==$tr['id']?' selected="selected"':'';
				$modelselect.='<option value='.$tr['id'].$optionselect.'>'.$tr['modelname'].'</option>'."\n\t";
			}
			$modelselect.='</select>';
			$categoryselect=$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1 && ispage=0');
			$indextlpselect='<select name="info[categorytlp]" id="categorytlp">';
			$listtlpselect='<select name="info[listtlp]" id="listtlp">';
			$showtlpselect='<select name="info[contenttlp]" id="contenttlp">';
			$cache=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			$modeltlp=cache_read('cache_model'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			if($cache)foreach($cache as $r)
			{
				if(substr($r['template'],0,9)=='category_')
				{
					$isselect=$modeltlp['categorytlp']==$r['template']?' selected="selected"':'';
					$indextlpselect.='<option value="'.$r['template'].'"'.$isselect.'>'.$r['name'].'('.$r['template'].'.html)</option>';
				}

				if(substr($r['template'],0,15)=='final_category_')
				{
					$isselect=$modeltlp['listtlp']==$r['template']?' selected="selected"':'';
					$listtlpselect.='<option value="'.$r['template'].'"'.$isselect.'>'.$r['name'].'('.$r['template'].'.html)</option>';
				}

				if(substr($r['template'],0,4)=='show')
				{
					$isselect=$modeltlp['contenttlp']==$r['template']?' selected="selected"':'';
					$showtlpselect.='<option value="'.$r['template'].'"'.$isselect.'>'.$r['name'].'('.$r['template'].'.html)</option>';
				}
			}
			$indextlpselect.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'categorytlp\').value);" />';
			$listtlpselect.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'listtlp\').value);" />';
			$showtlpselect.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'contenttlp\').value);" />';
			$membergroup=cache_read('membergroup.cache.php',DIRCMS_ROOT.'data/cache/');
			$val_r['postpriv']=explode(',',$val_r['postpriv']);
			$val_r['passpriv']=explode(',',$val_r['passpriv']);
			include admin_tlp('category_add');
			break;
		case 'page_add':
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$form=new form();
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			if(isset($do_submit))
			{
				$parentid=$info['parentid'];
				$t=array();
				$info['filename']=basename($info['filename'],'.html'); // 2010-12-30
				$pageid=$id=$db->insert(DB_PRE.'page',$info);
				if($parentid)
				{
					$r=cache_read('category'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if($r['domain'])
					{
						$t['url']=$r['domain'].$info['dir'].'/'.$info['filename'].'.html';
					}
					else
					{
						$t['url']=$r['categorydir'].'/'.$info['dir'].$info['filename'].'.html';
					}
					$info['dir']=$r['categorydir'].'/'.$info['dir'];
					$t['grade']=$r['grade']+1;
				}
				else
				{
					$t['url']=$info['dir'].'/'.$info['filename'].'.html';
				}
				make_dir($info['dir']);
				ob_start();
				@extract($info);
				include template($info['template']);
				$content=ob_get_contents();
				ob_clean();
				@file_put_contents(DIRCMS_ROOT.$info['dir'].'/'.$info['filename'].'.html',stripslashes($content));
				$t['ispage']=1;
				$t['pageid']=$pageid;
				$t['modelid']=0;
				$t['parentid']=$info['parentid'];
				$t['categorydir']=$info['dir'];
				$t['istop']=$info['istop'];
				$t['isshow']=$info['isshow'];
				$t['categoryname']=$info['pagename'];
				$t['meta_title']=$info['meta_title'];
				$t['meta_keywords']=$info['meta_keywords'];
				$t['meta_description']=$info['meta_description'];
				$id=$db->insert(DB_PRE.'category',$t);
				$db->update(DB_PRE.'page',array('catid'=>$id),'id='.$pageid);
				$c->cache_category();
				$db->update(DB_PRE.'category',array('isend'=>0),'id='.$parentid);
				$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1');
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=content&action=category_manage');
			}
			$parentid=$parentid?$parentid:0;
			$categoryselect=$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',false,'categoryname','isshow=1');
			$select='<select name="info[template]" id="tlpname">';
			$cache=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			if($cache)foreach($cache as $r)
			{
				if(substr($r['template'],0,5)=='page_')
				{
					$select.='<option value="'.$r['template'].'">'.$r['name'].'('.$r['template'].'.html)</option>';
				}
			}
			$select.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'tlpname\').value);" />';
			include admin_tlp('page_category_add');
			break;
		case 'page_edit':
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$form=new form();
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();

			if(!in_array($id,$_categorypriv) && $_roleid!=1)
			{
				showmsg_nourl('您没有该操作的权限!');
			}

			if(isset($do_submit))
			{
				$parentid=$info['parentid'];
				$t=array();
				$info['catid']=$id;
				$info['filename']=basename($info['filename'],'.html'); // 2010-12-30 
				$db->update(DB_PRE.'page',$info,'id='.$pageid);
				if($parentid)
				{
					$r=cache_read('category'.$parentid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if($r['domain'])
					{
						$t['url']=$r['domain'].$info['dir'].'/'.$info['filename'].'.html';
					}
					else
					{
						$t['url']=$r['categorydir'].'/'.$info['dir'].$info['filename'].'.html';
					}
					$info['dir']=$r['categorydir'].'/'.$info['dir'];
					$t['grade']=$r['grade']+1;
				}
				else
				{
					$t['url']=$info['dir'].'/'.$info['filename'].'.html';
				}
				make_dir($info['dir']);
				ob_start();
				@extract($info);
				include template($info['template']);
				$content=ob_get_contents();
				ob_clean();
				@file_put_contents(DIRCMS_ROOT.$info['dir'].'/'.$info['filename'].'.html',stripslashes($content));
				$t['ispage']=1;
				$t['modelid']=0;
				$t['parentid']=$info['parentid'];
				$t['categorydir']=$info['dir'];
				$t['istop']=$info['istop'];
				$t['isshow']=$info['isshow'];
				$t['categoryname']=$info['pagename'];
				$t['meta_title']=$info['meta_title'];
				$t['meta_keywords']=$info['meta_keywords'];
				$t['meta_description']=$info['meta_description'];
				$db->update(DB_PRE.'category',$t,'id='.$id);
				$c->cache_category();
				$db->update(DB_PRE.'category',array('isend'=>0),'id='.$parentid);
				$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',true,'categoryname','isshow=1');
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=content&action=category_manage');
			}
			
			$categoryselect=$s->getSelect('info[parentid]','作为一级栏目',DB_PRE.'category',false,'categoryname','isshow=1');
			$r=$db->fetch_one("SELECT * FROM `".DB_PRE."category` WHERE `".DB_PRE."category`.`id`=".intval($id));
			$parentid=$r['parentid'];
			$r2=$db->fetch_one("SELECT * FROM `".DB_PRE."page` WHERE `".DB_PRE."page`.`catid`=".intval($id));
			$r=array_merge($r,$r2);
			$select='<select name="info[template]" id="tlpname">';
			$cache=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			if($cache)foreach($cache as $_r)
			{
				if(substr($_r['template'],0,5)=='page_')
				{
					$isselected=$_r['template']==$r['template']?' selected="selected"':'';
					$select.='<option value="'.$_r['template'].'"'.$isselected.'>'.$_r['name'].'('.$_r['template'].'.html)</option>';
				}
			}
			$select.='</select>&nbsp;<input type="button" class="button" value="修改选中模板" title="修改选中模板" onClick="javascript:window.open(\''.ADMIN_FILE.'?file=template&action=edit&id=\'+document.getElementById(\'tlpname\').value);" />';
			include admin_tlp('page_category_edit');
			break;
		case 'create_index':
			@set_time_limit(0);
			$filesize=$html->index();
			$filesize=round((float)($filesize/1024),2);	
			if($filesize)showmsg_nourl('网站首页更新成功!<br />文件大小：'.$filesize.'K');
			else showmsg_nourl('网站首页更新失败!<br />请确定您的网站根目录读写权限为0777!');
			break;
		case 'create_rss':
			$rss='<?xml version="1.0" encoding="UTF-8" ?>';
			$rss.='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			$rss.='<url>
  <loc>'.$DIRCMS['site_url'].'</loc> 
  <lastmod>'.date('Y-m-d').'</lastmod> 
  <changefreq>always</changefreq> 
  <priority>1.0</priority> 
  </url>';
			$r=cache_read('finalcategory.cache.php',DIRCMS_ROOT.'data/cache_category/');
			if($r)foreach($r as $_r)
			{
				$rss.='<url>
  <loc>'.$DIRCMS['site_url'].$_r['url'].'</loc> 
  <lastmod>'.date('Y-m-d').'</lastmod> 
  <changefreq>daily</changefreq> 
  <priority>'.($_r['istop']?'0.9':'0.8').'</priority> 
  </url>';
				$content=$db->fetch_all("SELECT url,updatetime,posid FROM ".DB_PRE."content WHERE catid=".intval($_r['id'])." ORDER BY id DESC LIMIT 0,50");
				
				if($content)foreach($content as $_content)
				{
					$rss.='<url>
  <loc>'.$DIRCMS['site_url'].$_content['url'].'</loc> 
  <lastmod>'.date('Y-m-d',$_content['updatetime']).'</lastmod> 
  <changefreq>monthly</changefreq> 
  <priority>'.($_content['posid']?'0.7':'0.6').'</priority> 
  </url>';
				}
			}
			$rss.='</urlset>';

			file_put_contents(DIRCMS_ROOT.'sitemaps.xml',$rss);
			$filesize=round(filesize(DIRCMS_ROOT.'sitemaps.xml')/1024,2);
			showmsg_nourl('Sitemaps 生成成功! <a href="sitemaps.xml" target="_blank">查看</a><br /><br />文件大小：'.$filesize.'K');
			break;
		case 'create_category':
			@set_time_limit(0);
			if(isset($do_submit))
			{
				$catid=$hidecatid?explode(',',$hidecatid):$catid;
				$final_catid=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if($catid)
				{
					if(!$m)
					{
						$DIRCMS['t']='Dircms';
						cache_delete('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/'); // $_userid 防止冲突
						cache_write('catid.cache_tmp'.$_userid.'.php',$catid,DIRCMS_ROOT.'data/cache_tmp/');
						unset($r,$_r,$indexfile,$head,$position,$DIRCMS['t']);
					}
					$tmp_catid_cache=cache_read('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
					$t_catid=$pagecount?$catid:array_shift($tmp_catid_cache);

					cache_delete('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/'); 
					cache_write('catid.cache_tmp'.$_userid.'.php',$tmp_catid_cache,DIRCMS_ROOT.'data/cache_tmp/');
					if($t_catid)
					{
						$r=cache_read('category'.$t_catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
						$html->category($t_catid);

						if(in_array($t_catid,$final_catid) || $r['listpage']) //更新列表页
						{
							if(!isset($pagecount))
							{
								$count=get_cache_counts("SELECT COUNT(*) AS `count` FROM `".DB_PRE."content` WHERE ".getCatid($t_catid)." AND status=1"); 
								$pagecount=max(ceil($count/PAGESIZE),1);
								$len=min($pagecount,50);
								for($i=1;$i<=$len;$i++)
								{
									$html->category($t_catid,$i);
									
								}
								
								if($pagecount>50)
								{
									showmsg('栏目【'.$r['categoryname'].'】列表1-'.$len.'更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&do_submit=1&m=1&catid='.$t_catid.'&start='.$len.'&pagecount='.($pagecount-50),'0');
								}
								else
								{
									showmsg('栏目【'.$r['categoryname'].'】更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&do_submit=1&m=1&catid=1','0');
								}
							}
							else
							{
								$len=min($pagecount,50);
								if($pagecount>50)
								{
									for($i=$start;$i<=($start+$len);$i++)
									{
										$html->category($t_catid,$i);
									}
									showmsg('栏目【'.$r['categoryname'].'】列表'.$start.'-'.($start+$len).'更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&do_submit=1&m=1&catid='.$t_catid.'&start='.($start+$len).'&pagecount='.($pagecount-50),'0');
								}
								else
								{
									showmsg('栏目【'.$r['categoryname'].'】更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&do_submit=1&m=1&catid=1','0');
								}
							}
						}
						else
						{
							showmsg('栏目【'.$r['categoryname'].'】更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&do_submit=1&m=1&catid=1','0');
						}
						

					}else{
						showmsg('所有栏目更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action);
					}
				}else{
					showmsg('请选择需要更新的栏目!',ADMIN_FILE.'?file='.$file.'&action='.$action);
				}
			}
			else
			{
				function tree ($parentid=0)
				{
					global $db;
					static $flag;
					$r=$db->fetch_all("SELECT * FROM ".DB_PRE."category WHERE parentid=".intval($parentid));
					if($r)foreach($r as $_r)
					{
						if(($_r['ishtml']=='1' && $_r['isshow']) || $_r['ispage'])
						{
							echo '<tr>
				<td width="9%" align="center"><input type="checkbox"  class="checkbox" checked="checked" name="catid[]" value="'.$_r['id'].'" /></td>
				<td width="91%" align="left" style="padding-left:0px">'.str_repeat('&nbsp;',$_r['grade']*5).'└─ '.$_r['categoryname'].'</td>
				 </tr>';
						}
						tree($_r['id']);
					}
				}
				$category=cache_read('parent.cache.php',DIRCMS_ROOT.'data/cache_category/');
				include admin_tlp('create_category');
			}
			break;
		case 'create_content':
			@set_time_limit(0);
			if(isset($do_submit))
			{
				$DIRCMS['t']='Dircms';
				if(!$catid && !$m)showmsg('请选择需要更新的栏目!',ADMIN_FILE.'?file='.$file.'&action='.$action);
				$pagesize=intval($pagesize)?intval($pagesize):1;
				if(!$m)
				{
					$catid=array_map('intval',$catid);
					cache_write('catid.cache_tmp'.$_userid.'.php',$catid,DIRCMS_ROOT.'data/cache_tmp/');

					$sql='SELECT `'.DB_PRE.'content`.`id`,`'.DB_PRE.'content`.`catid` FROM `'.DB_PRE.'content`';
					$where='WHERE `'.DB_PRE.'content`.`catid` IN('.implode(',',$catid).') AND `'.DB_PRE.'content`.`status`=1';

					if($type=='realate')
					{
						$where.=' ORDER BY `'.DB_PRE.'content`.`id` DESC LIMIT 0,'.intval($number);
					}

					if($type=='limit')
					{
						$where.=' AND `'.DB_PRE.'content`.`id` BETWEEN '.intval($fromid).' AND '.intval($toid);
					}
					
					$r=$db->fetch_all($sql.$where);
					$count=count($r);
					$pagecount=ceil($count/$pagesize);
					
					cache_write('contentid.cache_tmp'.$_userid.'.php',$r,DIRCMS_ROOT.'data/cache_tmp/');
					cache_write('count.cache_tmp'.$_userid.'.php',array($count,$pagecount),DIRCMS_ROOT.'data/cache_tmp/');
				}
				else
				{
					$catid=cache_read('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
					$r=cache_read('count.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
					$count=$r[0];
					$pagecount=$r[1];
				}

				$page=intval($page)?intval($page):1;
				$curpage=$page+1;

				if($page<=$pagecount)
				{
					$r=cache_read('contentid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
					$len=count($r)>$pagesize?$pagesize:count($r);
					for($i=0;$i<$len;$i++)
					{
						$_r=array_shift($r);
						$html->content($_r['id'],$_r['catid']);
						cache_write('contentid.cache_tmp'.$_userid.'.php',$r,DIRCMS_ROOT.'data/cache_tmp/');
					}
					$from=$pagesize*($page-1);
					$to=min($count,$from+$pagesize);
					$per=100-round(count($r)/$count,1)*100;
					showmsg("共需更新 <font color='red'>$count</font> 个网页<br />已更新 <font color='red'>{$to}</font> 个网页（<font color='red'>{$per}%</font>）",ADMIN_FILE.'?file=content&action=create_content&pagesize='.$pagesize.'&do_submit=1&m=1&page='.$curpage,'0');
				}
				else
				{
					showmsg('所有内容页更新完毕!',ADMIN_FILE.'?file=content&action=create_content');
				}
			}
			else
			{
				function tree ($parentid=0)
				{
					global $db;
					static $flag;
					$r=$db->fetch_all("SELECT * FROM ".DB_PRE."category WHERE parentid=".intval($parentid));
					if($r)foreach($r as $_r)
					{
						if($_r['ishtml']=='1' && !$_r['ispage']&& $_r['isshow'])
						{
							echo '<tr>
				<td width="30%" align="center"><input type="checkbox" class="checkbox"  checked="checked" name="catid[]" value="'.$_r['id'].'" /></td>
				<td width="70%" align="left" style="padding-left:0px">'.str_repeat('&nbsp;',$_r['grade']*5).'└─ '.$_r['categoryname'].'</td>
				 </tr>';
						}
						tree($_r['id']);
					}
				}
				$category=cache_read('parent.cache.php',DIRCMS_ROOT.'data/cache_category/');
				include admin_tlp('create_content');
			}
			break;
		case 'cache':
			@set_time_limit(0);
			foreach (glob(DIRCMS_ROOT."data/cache_page/*.php") as $filename)
			{
				@unlink($filename);
			}
			$html->cache();
			showmsg_nourl('所有缓存更新成功!');
			break;
		case 'all_cache':
			@set_time_limit(0);
			if(isset($do_submit))
			{
				$job=!isset($job)?'cache':$job;
				if($job=='cache')
				{
					foreach (glob(DIRCMS_ROOT."data/cache_page/*.php") as $filename)
					{
						@unlink($filename);
					}
					$html->cache();
					showmsg('缓存更新完毕，开始更新首页...','?file=content&do_submit=1&action=all_cache&job=index');
				}
				if($job=='index')
				{
					$html->index();
					showmsg('网站首页更新完毕，开始更新栏目...','?file=content&do_submit=1&action=all_cache&job=category');
				}
				if($job=='category')
				{
					$catid=cache_read('catid.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if(!$catid)showmsg('暂无栏目需要更新，开始更新RSS...','?file=content&do_submit=1&action=all_cache&job=rss');
					$final_catid=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if($catid)
					{
						if(!$m)
						{
							$DIRCMS['t']='Dircms';
							cache_delete('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/'); // $_userid 防止冲突
							cache_write('catid.cache_tmp'.$_userid.'.php',$catid,DIRCMS_ROOT.'data/cache_tmp/');
							unset($r,$_r,$indexfile,$head,$position,$DIRCMS['t']);
						}
						$tmp_catid_cache=cache_read('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
						$t_catid=$pagecount?$catid:array_shift($tmp_catid_cache);

						cache_delete('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/'); 
						cache_write('catid.cache_tmp'.$_userid.'.php',$tmp_catid_cache,DIRCMS_ROOT.'data/cache_tmp/');
						if($t_catid)
						{
							$r=cache_read('category'.$t_catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
							$html->category($t_catid);

							if(in_array($t_catid,$final_catid) || $r['listpage']) //更新列表页
							{
								if(!isset($pagecount))
								{
									$count=get_cache_counts("SELECT COUNT(*) AS `count` FROM `".DB_PRE."content` WHERE ".getCatid($t_catid)." AND status=1"); 
									//$pagecount=max(ceil($count/$r['pagesize']),1);
									$pagecount=max(ceil($count/PAGESIZE),1);

									$len=min($pagecount,50);
									for($i=1;$i<=$len;$i++)
									{
										$html->category($t_catid,$i);
									}
									if($pagecount>50)
									{
										showmsg('栏目【'.$r['categoryname'].'】列表1-'.$len.'更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&job=category&do_submit=1&m=1&catid='.$t_catid.'&start='.$len.'&pagecount='.($pagecount-50),'0');
									}
									else
									{
										showmsg('栏目【'.$r['categoryname'].'】更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&job=category&do_submit=1&m=1&catid=1','0');
									}
								}
								else
								{
									$len=min($pagecount,50);
									if($pagecount>50)
									{
										for($i=$start;$i<=($start+$len);$i++)
										{
											$html->category($t_catid,$i);
										}
										showmsg('栏目【'.$r['categoryname'].'】列表'.$start.'-'.($start+$len).'更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&job=category&do_submit=1&m=1&catid='.$t_catid.'&start='.($start+$len).'&pagecount='.($pagecount-50),'0');
									}
									else
									{
										showmsg('栏目【'.$r['categoryname'].'】更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&job=category&do_submit=1&m=1&catid=1','0');
									}
								}
							}
							else
							{
								showmsg('栏目【'.$r['categoryname'].'】更新成功!',ADMIN_FILE.'?file='.$file.'&action='.$action.'&job=category&do_submit=1&m=1&catid=1','0');
							}
							

						}else{
							showmsg('栏目更新完毕，开始更新rss...','?file=content&do_submit=1&action=all_cache&job=rss');
						}
					}else{
						showmsg('暂无栏目需要更新，开始更新RSS...','?file=content&do_submit=1&action=all_cache&job=rss');
					}
				}
				if($job=='create_content')
				{
					$DIRCMS['t']='Dircms';
					$type='all';
					$catid=cache_read('catid.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if(!$catid)showmsg('暂无内容需要更新，一键更新完毕!','?file=content&action=all_cache',2000);
					$pagesize=intval($pagesize)?intval($pagesize):50;
					if(!$m)
					{
						$catid=array_map('intval',$catid);
						cache_write('catid.cache_tmp'.$_userid.'.php',$catid,DIRCMS_ROOT.'data/cache_tmp/');

						$sql='SELECT `'.DB_PRE.'content`.`id`,`'.DB_PRE.'content`.`catid` FROM `'.DB_PRE.'content`';
						$where='WHERE `'.DB_PRE.'content`.`catid` IN('.implode(',',$catid).') AND `'.DB_PRE.'content`.`status`=1';

						if($type=='realate')
						{
							$where.=' ORDER BY `'.DB_PRE.'content`.`id` DESC LIMIT 0,'.intval($number);
						}

						if($type=='limit')
						{
							$where.=' AND `'.DB_PRE.'content`.`id` BETWEEN '.intval($fromid).' AND '.intval($toid);
						}
						
						$r=$db->fetch_all($sql.$where);
						$count=count($r);
						$pagecount=ceil($count/$pagesize);
						
						cache_write('contentid.cache_tmp'.$_userid.'.php',$r,DIRCMS_ROOT.'data/cache_tmp/');
						cache_write('count.cache_tmp'.$_userid.'.php',array($count,$pagecount),DIRCMS_ROOT.'data/cache_tmp/');
					}
					else
					{
						$catid=cache_read('catid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
						$r=cache_read('count.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
						$count=$r[0];
						$pagecount=$r[1];
					}

					$page=intval($page)?intval($page):1;
					$curpage=$page+1;

					if($page<=$pagecount)
					{
						$r=cache_read('contentid.cache_tmp'.$_userid.'.php',DIRCMS_ROOT.'data/cache_tmp/');
						$len=count($r)>$pagesize?$pagesize:count($r);
						for($i=0;$i<$len;$i++)
						{
							$_r=array_shift($r);
							$html->content($_r['id'],$_r['catid']);
							cache_write('contentid.cache_tmp'.$_userid.'.php',$r,DIRCMS_ROOT.'data/cache_tmp/');
						}
						$from=$pagesize*($page-1);
						$to=min($count,$from+$pagesize);
						$per=100-round(count($r)/$count,1)*100;
						showmsg("共需更新 <font color='red'>$count</font> 个网页<br />已更新 <font color='red'>{$to}</font> 个网页（<font color='red'>{$per}%</font>）",ADMIN_FILE.'?file='.$file.'&job=create_content&action='.$action.'&pagesize='.$pagesize.'&do_submit=1&m=1&page='.$curpage,'0');
					}
					else
					{
						showmsg('内容更新完毕!一键更新完毕!','?file=content&action=all_cache',2000);
					}
				}
				if($job=='rss')
				{
					$rss='<?xml version="1.0" encoding="UTF-8" ?>';
					$rss.='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
					$rss.='<url>
		  <loc>'.$DIRCMS['site_url'].'</loc> 
		  <lastmod>'.date('Y-m-d').'</lastmod> 
		  <changefreq>always</changefreq> 
		  <priority>1.0</priority> 
		  </url>';
					$r=cache_read('finalcategory.cache.php',DIRCMS_ROOT.'data/cache_category/');
					if($r)foreach($r as $_r)
					{
						$rss.='<url>
		  <loc>'.$DIRCMS['site_url'].$_r['url'].'</loc> 
		  <lastmod>'.date('Y-m-d').'</lastmod> 
		  <changefreq>daily</changefreq> 
		  <priority>'.($_r['istop']?'0.9':'0.8').'</priority> 
		  </url>';
						$content=$db->fetch_all("SELECT url,updatetime,posid FROM ".DB_PRE."content WHERE catid=".intval($_r['id'])." ORDER BY id DESC LIMIT 0,50");
						
						if($content)foreach($content as $_content)
						{
							$rss.='<url>
		  <loc>'.$DIRCMS['site_url'].$_content['url'].'</loc> 
		  <lastmod>'.date('Y-m-d',$_content['updatetime']).'</lastmod> 
		  <changefreq>monthly</changefreq> 
		  <priority>'.($_content['posid']?'0.7':'0.6').'</priority> 
		  </url>';
						}
					}
					$rss.='</urlset>';

					file_put_contents(DIRCMS_ROOT.'sitemaps.xml',$rss);
					$filesize=round(filesize(DIRCMS_ROOT.'sitemaps.xml')/1024,2);
					showmsg('RSS更新完毕，开始更新内容...','?file=content&do_submit=1&action=all_cache&job=create_content');
				}
			}
			include admin_tlp('create_all');
			break;
		case 'add':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/form.class.php');	
			require_once(DIRCMS_ROOT.'include/admin/content.class.php');
			$content=new content();
			require_once(DIRCMS_ROOT.'include/content.input.class.php');
			if(isset($do_submit))
			{
				$info['catid']=$info['catid']?$info['catid']:intval($catid);
				if(!$info['catid'])showmsg('请选择栏目!');
				$r=cache_read('category'.$info['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				$content_input=new content_input($r['modelid']);
				$info=$content_input->check($info);
				$info['description']=isset($info['description']) && $info['description']?$info['description']:sub_string(strip_tags($info['content']),200,'');
				$info['keywords']=isset($info['keywords']) && $info['keywords']?$info['keywords']:$info['title'];
				$info['description']=($auto_description && !$info['description'])?sub_string(strip_tags($info['content']),255,''):sub_string($info['description'],255,'');
				$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(!$info['thumb'])unset($info['thumb']);
				if($content->add($info))showmsg('发布成功!',ADMIN_FILE.'?file=content&action=add&catid='.$info['catid']);
				else showmsg('发布失败!',ADMIN_FILE.'?file=content&action=add&catid='.$info['catid']);
			}
			$catid=intval($catid);
			$r=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
			$defaultvalue=in_array($catid,$r)?$catid:0;
			$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/'); 
			if(!$r || !$catid)showmsg_nourl('所选栏目不存在!');
			$template=$r['contenttlp'];
			$modelid=$r['modelid'];
			$point=$r['viewpoint'];

			$f=new form($modelid);
			$base_r=$f->get();	
			$more_r=$f->getmore();	
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			$catid_select=$s->getSelect('info[catid]','选择栏目',DB_PRE.'category',true,'categoryname','isshow=1 && ispage=0 && (modelid='.$modelid.' || parentid=0)',0,$modelid);
			include admin_tlp('content_add');
			break;
		case 'manage':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/list.class.php');
			$l=new dircmslist();
			require_once(DIRCMS_ROOT.'include/admin/content.class.php');
			$content=new content();
			if(isset($do_submit))
			{
				if($content->order($orderby,$id,$catid))
				{
					showmsg('操作成功!');
				}
				else 
				{
					showmsg('操作失败!',ADMIN_FILE.'?file=content&action=&action=manage');
				}
			}
			$catid=intval($catid);
			$where=1;
			$where=$catid?$where." AND catid=".$catid:$where;
			$where=isset($field) && $field?$where." AND {$field}='".intval($fieldvalue)."'":$where;

			$k=trim($k);
			if($stype=='title')
			{
				$where=!empty($k)?$where." AND	title like '%{$k}%'":$where;
			}
			else if($stype=='id')
			{
				$where=!empty($k)?$where." AND	id =$k":$where;
			}
			else
			{
				$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);
			include admin_tlp('content_manage');
			break;
		case 'pass':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/admin/content.class.php');
			$content=new content();
			if(!$checkedid)showmsg('请选择要审核的内容!');
			if($content->pass($checkedid,$catid))showmsg('操作成功!');
			else showmsg('操作失败!',ADMIN_FILE.'?file=content&action=manage&field='.$field.'&fieldvalue='.$fieldvalue);
			break;
		case 'unpass':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/admin/content.class.php');
			$content=new content();
			if(!$checkedid)showmsg('请选择要待审的内容!');
			if($content->pass($checkedid,$catid,0))showmsg('操作成功!');
			else showmsg('操作失败!',ADMIN_FILE.'?file=content&action=manage&field='.$field.'&fieldvalue='.$fieldvalue);
			break;
		case 'delete':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/admin/content.class.php');
			$content=new content();
			if(!$checkedid)showmsg('请选择要删除的内容!');
			if($content->delete($checkedid))showmsg('操作成功!');
			else showmsg('操作失败!',ADMIN_FILE.'?file=content&action=manage&field='.$field.'&fieldvalue='.$fieldvalue);
			break;
		case 'move':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			if(isset($do_submit) && $do_submit=='2')
			{
				$DIRCMS['t']='Dircms';
				if(!$id)showmsg('请选择要移动的内容!',ADMIN_FILE.'?file=content&action=manage&catid='.intval($catid).'&field='.$field.'&fieldvalue='.$fieldvalue);
				require_once(DIRCMS_ROOT.'include/html.class.php');
				$html=new html();
				$catid=intval($catid);
				$r=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if(!in_array($catid,$r))showmsg('要移动到的栏目必须为最终栏目!');
				$id=explode(',',$id);
				$id=array_map('intval',$id);
				$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				foreach($id as $_id)
				{
					$inputtime=TIME;
					if($r['ishtml'])
					{
						if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
						{
							$dir=$r['categorydir'].'/'.date('YW',$inputtime).'/';
							@mkdir(DIRCMS_ROOT.$dir);
							$contenturl=$dir.$_id.'.html';
						}
						else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
						{
							$dir='html/'.date('YW',$inputtime).'/';
							@mkdir(DIRCMS_ROOT.$dir);
							$contenturl=$dir.$_id.'.html';
						}
						else
						{
							if($r['content_html']=='content_html_3') //日期/内容/ID.html
							{
								$dir=date('Y',$inputtime).'/'.date('m',$inputtime).'/';
								@mkdir(DIRCMS_ROOT.date('Y',$inputtime));
								@mkdir(DIRCMS_ROOT.$dir);
								$contenturl=$dir.$_id.'.html';
							}
						}
					}
					else
					{
						$contenturl='show.php?id='.$_id;
					}
					$db->update(DB_PRE.'content',array('catid'=>$catid,'template'=>$r['contenttlp'],'url'=>$contenturl,'inputtime'=>$inputtime,'updatetime'=>$inputtime),'id='.$_id);
					$html->content($_id,$catid);
				}
				$html->category($catid,1);
				$html->index();
				showmsg('所选内容移动成功!',ADMIN_FILE.'?file=content&action=manage&catid='.$catid.'&field='.$field.'&fieldvalue='.$fieldvalue);
			}
			else
			{
				require_once(DIRCMS_ROOT.'include/select.class.php');
				$s=new select();

				if(!$checkedid)showmsg('请选择要移动的内容!');
				$r=array();
				$t=$db->fetch_all("SELECT `".DB_PRE."content`.`catid` FROM `".DB_PRE."content` WHERE `".DB_PRE."content`.`id` IN (".implode(',',$checkedid).")");
				if($t)foreach($t as $_t)
				{
					$r[]=$_t['catid'];
				}
				$r=array_unique($r);
				if(sizeof($r)>1)showmsg('只能同时移动相同栏目的内容!','',2000);
				$catid=$r[0];
				$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/'); 
				$s->getOption(0,DB_PRE.'category','categoryname','modelid='.intval($r['modelid']));
				$catid_select='<select name="catid" id="catid" style="width:120px">';
				$catid_select.=$s->select;
				$catid_select.='</select>';
				include admin_tlp('content_move');
			}
			break;
		case 'support':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			if(isset($do_submit) && $do_submit=='2')
			{
				if(!$id)showmsg('请选择要移动的内容!');
				$id=explode(',',$id);
				$id=array_map('intval',$id);
				foreach($id as $val)
				{
					if($posid)
					{
						$db->update(DB_PRE.'content',array('posid'=>1),'id='.intval($val));
						$db->mysql_delete(DB_PRE.'content_posid',$val,'contentid');
						foreach($posid as $_posid)
						{
							$db->insert(DB_PRE.'content_posid',array('contentid'=>$val,'posid'=>intval($_posid)),true);
						}
					}
				}
				require_once(DIRCMS_ROOT.'include/html.class.php');
				$html=new html();
				$html->index();
				showmsg('所选内容推荐成功!',ADMIN_FILE.'?file=content&action=manage&field='.$field.'&fieldvalue='.$fieldvalue);
			}
			else
			{
				if(!$checkedid)showmsg('请选择要推荐的内容!');
				$posid_select='';
				$r=cache_read('posid.cache.php',DIRCMS_ROOT.'data/cache/');
				if($r)foreach($r as $_r)
				{
					$posid_select.='<input type="checkbox" style="border:0px" name="posid[]" value="'.$_r['id'].'" checked="checked">'.$_r['posname'].'&nbsp;';
				}
				include admin_tlp('content_support');
			}
			break;
		case 'preview':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			@extract($info);
			include template($template);
			break;
		case 'edit':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/admin/content.class.php');
			$content=new content();
			require_once(DIRCMS_ROOT.'include/content.input.class.php');
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			if(isset($do_submit))
			{	
				$r=cache_read('category'.$info['catid'].'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				$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']),255,''):sub_string($info['description'],255,'');
				$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(!$info['thumb'])unset($info['thumb']);
				if($content->edit($info,$id))showmsg('内容编辑成功!',ADMIN_FILE.'?file=content&action=manage&field=catid&fieldvalue='.$info['catid']);
				else showmsg('内容编辑失败!',$forwardurl);
			}
			$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/'); 
			$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);
			$more_r=$f->getmore($c);
			@extract($c);
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			$catid_select=$s->getSelect('info[catid]','选择栏目',DB_PRE.'category',true,'categoryname','isshow=1 && ispage=0 && (modelid='.$modelid.' || parentid=0)',0,$modelid);
			include admin_tlp('content_edit');
			break;
		case 'comment':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			require_once(DIRCMS_ROOT.'include/list.class.php');
			$l=new dircmslist();
			$where=1;
			if($timeid)
			{	
				$time = TIME; $timeid = TIME - $timeid*24*60*60; 
				$where.= " AND `addtime` >= '$timeid' AND `addtime` <= '$time'";
			}
			
			$where=isset($contentid)?$where." AND `contentid`=".intval($contentid):$where;
			$where=isset($status)?$where." AND `status`=".intval($status):$where;
			$where=!empty($k)?$where." AND `username` LIKE '%{$k}%' OR `content` LIKE '%{$k}%'":$where;
			$orderby="support DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'comment',$where,$orderby,$page,15);
			$pages=$l->pages;
			include admin_tlp('comment_manage');
			break;
		case 'comment_pass':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			if(isset($do_submit))
			{	
				if($id)
				{
					$db->update(DB_PRE.'comment',array('status'=>1),'`id` IN('.implode(',',array_map('intval',$id)).')');
					showmsg('操作成功!',$cururl);
				}
				else showmsg('请选择要审核的评论!',$cururl);
			}
			break;
		case 'comment_unpass':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			if(isset($do_submit))
			{	
				if($id)
				{
					$db->update(DB_PRE.'comment',array('status'=>0),'`id` IN('.implode(',',array_map('intval',$id)).')');
					showmsg('操作成功!',$cururl);
				}
				else showmsg('请选择要待审的评论!',$cururl);
			}
			break;
		case 'comment_delete':
			$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
			$r=$r['content']['roleid']; 
			if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

			if(isset($do_submit))
			{	
				if($id)
				{
					$db->query("DELETE FROM `".DB_PRE."comment` WHERE `".DB_PRE."comment`.`id` IN (".implode(',',array_map('intval',$id)).")",true);
					showmsg('操作成功!',$cururl);
				}
				else showmsg('请选择要删除的评论!',$cururl);
			}
			break;
	}
?>
