<?php
!function_exists('html') && exit('ERR');

if($hack&&ereg("^([a-z_0-9]+)$",$hack))
{
	if(is_file("inc/hack/$hack.php")){
		include(dirname(__FILE__)."/"."inc/hack/$hack.php");
	}elseif(is_file(PHP168_PATH."inc/hack/$hack/admin.php")){
		include(PHP168_PATH."inc/hack/$hack/admin.php");
	}else{
		showmsg("文件不存在");
	}
	
}
elseif($job=='list'&&$Apower[hack_list])
{
	$query = $db->query("SELECT * FROM {$pre}hack ");
	while($rs = $db->fetch_array($query)){
		$listdb[]=$rs;
	}
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/hack/list.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($job=='add'&&$Apower[hack_list])
{
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/hack/edit.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=='add'&&$Apower[hack_list])
{
	$rs=$db->get_one("SELECT * FROM {$pre}hack WHERE keywords='$postdb[keywords]' ");
	if($rs){
		showmsg("$postdb[keywords],关键字已经存在了.不能重复");
	}elseif(!ereg("([0-9_a-z]+)",$postdb[keywords])){
		showmsg("关键字必须是0-9a-z_这些字符");
	}
	$db->query("INSERT INTO `{$pre}hack` ( `keywords` , `name` , `isclose` , `author` , `config` , `htmlcode` , `hackfile` , `hacksqltable` , `about`,`adminurl` ) VALUES ('$postdb[keywords]','$postdb[name]','$postdb[isclose]','$postdb[author]','$postdb[config]','$postdb[htmlcode]','$postdb[hackfile]','$postdb[hacksqltable]','$postdb[about]','$postdb[adminurl]')");
	write_hackmenu_cache();
	jump("安装成功,请去添加管理权限,否则无权管理","index.php?lfj=group&job=admin_gr&gid=3",10);
}
elseif($job=='edit'&&$Apower[hack_list])
{
	$rsdb=$db->get_one("SELECT * FROM {$pre}hack WHERE keywords='$keywords' ");
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/hack/edit.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=='edit'&&$Apower[hack_list])
{
	$db->query("UPDATE `{$pre}hack` SET name='$postdb[name]',hackfile='$postdb[hackfile]',hacksqltable='$postdb[hacksqltable]',about='$postdb[about]',adminurl='$postdb[adminurl]' WHERE keywords='$keywords'");
	write_hackmenu_cache();
	jump("修改成功",$FROMURL,1);
}
elseif($action=='delete'&&$Apower[hack_list])
{
	$rsdb=$db->get_one("SELECT * FROM {$pre}hack WHERE keywords='$keywords' ");
	$db->query("DELETE FROM {$pre}hack WHERE keywords='$keywords'");
	$rsdb[hackfile]=str_replace("\r\n","<br>",$rsdb[hackfile]);
	write_hackmenu_cache();
	jump("为确保安全,系统不自动删除此插件的文件.请手工删除以下文件:<br>$rsdb[hackfile]",$FROMURL,600);
}

function write_hackmenu_cache(){
	global $db,$pre;
	$show="<?php\r\n";
	$query = $db->query("SELECT * FROM {$pre}hack");
	while($rs = $db->fetch_array($query)){
		$show.="\r\n\$menudb['插件管理中心']['$rs[name]']['link']='$rs[adminurl]';";
		$show.="\r\n\$menudb['插件管理中心']['$rs[name]']['power']='hack_$rs[keywords]';\r\n";
	}
	//write_file(PHP168_PATH."php168/hack.php",$show);
}

?>