<?
defined('ROOT_PHPLEFT') or exit('phpleft: Not page');
/*
	phpleftDms Copyright (c) 2011 phpleft.COM
	商业授权：QQ45150208
	
*/
$cache_path = PL_ROOT.$PL_CFG['cache']."/";
$docache = new allcache;
switch($post) {
	//基本设置
	case 'indexset':
	$rs= new admin;
    $path = PL_ROOT.'config.inc.php';
    $html = file_get($path);
    $dos = new allcache;
    foreach($config as $k=>$v)	{
			$html = preg_replace("/[$]PL_CFG\['$k'\]\s*\=\s*[\"'].*?[\"']/is", "\$PL_CFG['$k'] = '$v'", $html);
     }
    if ($PL_CFG['htmlis']==1 and $config['htmlis']==0){
	$ThisFileindex = 'index';
    $pathindex = PL_ROOT.$ThisFileindex.$PL_CFG['html'];
	deletehtml($pathindex);
    }
    if ($PL_CFG['htmlis']==0 and $config['htmlis']==1 or $PL_CFG['htmlis']==1 and $config['htmlis']==0){
	$lefts=1;
    }
    MakeHtmlFile($path, $html, 1);
    $rs->update_setting($moduleid, $set);
    $dos->menu_cache(5);
    $files = "set-".$moduleid.".php";
    cache_write($files, $set);
    msg('系统更新成功', 'phpleft.php?file=indexset',$lefts);
	break;
	//模板管理
	case 'template':
	$path = PL_ROOT.'config.inc.php';
    $html = file_get($path);
    $config['skin'] = $skin;
    $config['template'] = $template ? $template: 'default';
    foreach($config as $k=>$v)	{
			$html = preg_replace("/[$]PL_CFG\['$k'\]\s*\=\s*[\"'].*?[\"']/is", "\$PL_CFG['$k'] = '$v'", $html);
    }
    MakeHtmlFile($path, $html, 1);
    echo gourl('phpleft.php?file=template','0');
	break;
	case 'templateset':
    cache_write('phpleft.php', $add, $templatefileis);
    msg('设置成功', 'phpleft.php?file=template&show=set');
	break;
	
	//管理员管理
    case 'adminadd':
    $sql = sql_list("table=admin&where=username='".$add['username']."'&order=id asc");
    if($sql){
    msg('管理员已经存在', 'phpleft.php?file=admin&show='.$post,0,1,1,'n');
    }else{
	if($pwdm=='' or $add['username']=='') msg('密码和用户名不能为空！', 'phpleft.php?file=admin&show='.$post,0,1,1,'n');
	$do = new post;
    $add['userpwd']=md5($pwdm.md5(DMS));
    $add['logn'] = 0;
    $add['addtime'] = time();
    $do->into($add,'admin');
    msg('添加成功', 'phpleft.php?file=admin');
    }
    break;
    case 'admindel':
    $db->query("DELETE FROM ".$PL_CFG['tb_pre']."admin WHERE id=".$id);
    msg('删除成功', 'phpleft.php?file=admin');
    break;
    case 'adminbj':
	$sql = sql_list("table=admin&where=username='".$add['username']."' and id<>'$id'&order=id asc");
	if($sql){
    msg('管理员已经存在!','?file=admin&show=bj&id='.$id,0,1,1,'n');
    }else{
	$do = new post;
    if ($pwdm) $add['userpwd'] = md5($pwdm.md5(DMS));
    $do->update($add,'admin','id',$id);
    msg('编辑成功', '?file=admin&show=bj&id='.$id);
	}
    break;
	//频道添加
	case 'menu':
	switch($input) {
		case 'del':
		switch($style) {
			case '3':
			$db->query("DELETE FROM ".$PL_CFG['tb_pre']."menu WHERE id='$id'");	
			break;
			case '2':
            $dirFrom = PL_ROOT.modpath($id);
            if (PL_ROOT.modpath($id)==PL_ROOT) msg('系统错误', 'phpleft.php?file=menu',0,1,1,'n');
            if (PL_ROOT.modpath($id)<>PL_ROOT) deldir($dirFrom);
            $db->query("DELETE FROM ".$PL_CFG['tb_pre']."menu WHERE id='$id'");
            $moduleiddir = $cache_path.'moduleid-'.$id.'.php';
            delfile($moduleiddir);
			break;
			case '1':
			$dirFrom = PL_ROOT.modpath($id);
            if (PL_ROOT.modpath($id)==PL_ROOT) msg('系统错误', 'phpleft.php?file=menu',0,1,1,'n');
            if (PL_ROOT.modpath($id)<>PL_ROOT) deldir($dirFrom);
            $db->query("DELETE FROM ".$PL_CFG['tb_pre']."menu WHERE id='$id'");
            $db->query("DELETE FROM ".$PL_CFG['tb_pre']."sort WHERE module='$id'");
            $db->query("DELETE FROM ".$PL_CFG['tb_pre']."setting WHERE item='$id'");
			delfile($cache_path.'catid_'.$id.'.php');
			delfile($cache_path.'set-'.$id.'.php');
			delfile($cache_path.'moduleid-'.$id.'.php');
			delfile($cache_path.'table_'.$id.'.php');
			delfile($cache_path.'list/tb_'.$id.'.php');
			$sqltable = $PL_CFG['tb_pre'].$modname."_".$id;
            $db->query("drop table ".$sqltable);
			break;
		}
		$docache->menu_cache(1);
		msg('删除成功', 'phpleft.php?file=menu');
		break;
		case 'bj':
		$do = new post;
		if ($style=='1'){
$do->update($alladd,'menu','id',$id);	
}else{
$dirTo = PL_ROOT.$alladd[path];

if ($paths<>$alladd[path]){
if (!file_exists($dirTo)){
$dirFrom = PL_ROOT.$paths;    
copyDir($dirFrom,$dirTo);
deldir($dirFrom);
}else{
msg('文件夹'.$alladd['path'].'已经存在', 'phpleft.php?file=menu',0,1,1,'n');		
}
}
$alladd[linkurl] = PL_URL.$alladd['path'].'/';
$do->update($alladd,'menu','id',$id);

}
$docache->menu_cache(1);
$docache->menu_cache(4);
msg('更新成功', 'phpleft.php?file=menu');
break;
		case 'add':
		switch($is) {
			case '1':
			$dirTo = PL_ROOT.$alladd['path'];
            if (!file_exists($dirTo)){
            $alladd[plugins] = '';
            $alladd[fou] = $is;
            $alladd[linkurl] = PL_URL.$alladd['path'].'/';
			$do->into($alladd,'menu');
            $tableadd = cache_read('table.inc.php',PL_ROOT."phpleft/".$alladd[module]."/");
            if ($tableadd<>''){
            foreach($tableadd as $id=>$m){
	        $length = $m['length'] ? "(".$m['length'].")" : '';
	        $sqlname.=",
			".$m['thiskeys']." ".$m['keystype'].$length;
            }
            }else{
	        $sqlname=",";
            }
            $getID=mysql_insert_id();//
            $sqltable = $PL_CFG['tb_pre'].$alladd['module']."_".$getID;
            $sql = "CREATE TABLE ".$sqltable." (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id)".$sqlname.") DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci";
            $db->query($sql);
            $tablem=sql_cache("table=table&where=module='".$alladd[module]."' and  moduleid='".$alladd[module]."'&order=keylist asc");
            $files = "table_".$getID.".inc.php";
            $pathcache = PL_ROOT.$PL_CFG['cache']."/";
            $dirFrom = PL_ROOT."phpleft/".$alladd[module]."/"."show" ;
            copyDir($dirFrom,$dirTo);
            $path = $dirTo.'/config.inc.php';
            $html = file_get($path);
            $html = preg_replace("/[$]moduleid\s*\=\s*[\"'].*?[\"']/is", "\$moduleid = '$getID'", $html);
            MakeHtmlFile($path, $html, 1);
            foreach($tablem as $id=>$m){
	          if($m['thiskeys']=="catid"){
		      $db->update($PL_CFG['tb_pre']."menu","catid=catid+1","id='".$getID."'");
	         }
            }
            foreach($tableadd as $id=>$m){
              $tabledata['keylist'] = $m['keylist'];
              $tabledata['module'] = $m['module'];
              $tabledata['name'] = $m['name'];
              $tabledata['thiskeys'] = $m['thiskeys'];
              $tabledata['note'] = $m['note'];
              $tabledata['keystype'] = $m['keystype'];
              $tabledata['length'] = $m['length'];
              $tabledata['type'] = $m['type'];
              $tabledata['typevalue'] = $m['typevalue'];
              $tabledata['add_js'] = $m['add_js'];
              $tabledata['default_value'] = $m['default_value'];
              $tabledata['geton'] = $m['geton'];
              $tabledata['is_or'] = $m['is_or'];
              $tabledata['qis_or'] = $m['qis_or'];
              $tabledata['fou'] = $m['fou'];
              $tabledata['moduleid'] = $getID;
              $do->into($tabledata,'table');
              }
            }else{
              msg('文件夹'.$alladd['path'].'已经存在', 'phpleft.php?file=menu',0,1,1,'n');		
            }
			break;
			case '2':
			$dirTo = PL_ROOT.$alladd['path'];
            if (!file_exists($dirTo)){
            $alladd[module] = '';
            $alladd[fou] = $is;
            $alladd[linkurl] = PL_URL.$alladd['path'].'/';
			$do->into($alladd,'menu');
            $getID=mysql_insert_id();
            $dirFrom = PL_ROOT."plugins/".$alladd[plugins]."/show";
            copyDir($dirFrom,$dirTo);
            $path = $dirTo.'/config.inc.php';
            $html = file_get($path);
            $html = preg_replace("/[$]moduleid\s*\=\s*[\"'].*?[\"']/is", "\$moduleid = '$getID'", $html);
            MakeHtmlFile($path, $html, 1);
            }else{
            msg('文件夹'.$alladd['path'].'已经存在', 'phpleft.php?file=menu',0,1,1,'n');	
            }
            break;
			case '3':
			$alladd[path] = '';
            $alladd[plugins] = '';
            $alladd[fou] = $is;
            $alladd[linkurl] = $linkurl;
			$do->into($alladd,'menu');
			break;
		}
		$docache->menu_cache(1);
		$docache->menu_cache(4);
		msg('添加成功', 'phpleft.php?file=menu');
		break;
	}
	break;
	//模型
	case 'module':
	$files = "table.inc.php";
	switch($input) {
		case 'install':
		$set = cache_read('config.inc.php',PL_ROOT."phpleft/".$install."/");
        $rs->update_post($set,'module','moduleinstall');
        $set1 = cache_read('table.inc.php',PL_ROOT."phpleft/".$install."/");
        foreach($set1 as $id=>$m){
        $tabledata['keylist'] = $m['keylist'];
        $tabledata['module'] = $install;
        $tabledata['name'] = $m['name'];
        $tabledata['thiskeys'] = $m['thiskeys'];
        $tabledata['note'] = $m['note'];
        $tabledata['keystype'] = $m['keystype'];
        $tabledata['length'] = $m['length'];
        $tabledata['type'] = $m['type'];
        $tabledata['typevalue'] = $m['typevalue'];
        $tabledata['add_js'] = $m['add_js'];
        $tabledata['default_value'] = $m['default_value'];
        $tabledata['geton'] = $m['geton'];
        $tabledata['is_or'] = $m['is_or'];
        $tabledata['fou'] = $m['fou'];
        $tabledata['moduleid'] = $install;
        $rs->update_post($tabledata,'table','moduleid');
        }
        msg('安装成功', 'phpleft.php?file=module');
		break;
		case 'delete':
		if ($delete){
		    $dirpath=PL_ROOT.SOFTNAME.'/'.$delete.'/';
		    deldir($dirpath);
		    $db->query("DELETE FROM ".$PL_CFG['tb_pre']."module WHERE module='$delete'");
		    $db->query("DELETE FROM ".$PL_CFG['tb_pre']."table WHERE moduleid='$delete'");
		    msg('删除成功', 'phpleft.php?file=module');
		}else{
			msg('删除失败！', 'phpleft.php?file=module',0,1,1,'n');
		}
		break;
		case 'paixu':
		if ($inid<>''){
            foreach($inid as $id=>$t) {
			    $query=$db->query("update 	".$PL_CFG['tb_pre']."table set keylist=".$keylist[$id]." where id='".$t."'");
		    }
            $pathcache = PL_ROOT."Phpleft/".$table."/";
            $tablem=sql_cache("table=table&where=module='".$table."' and moduleid='".$table."'&order=keylist asc");
            cache_write($files, $tablem, $pathcache);
            msg('更新排序成功', 'phpleft.php?file=module&table='.$table);
        }else{
            msg('没有数据，无任何操作！', 'phpleft.php?file=module&table='.$table);	
        }
		break;
		case 'keyb':
		$do->update($alladd,'table','id',$id);
        $pathcache = PL_ROOT."Phpleft/".$alladd[module]."/";
        $tablem=sql_cache("table=table&where=module='".$alladd[module]."' and moduleid='".$alladd[module]."'&order=keylist asc");
        cache_write($files, $tablem, $pathcache);
        msg('修改字段成功', 'phpleft.php?file=module&table='.$alladd[module]);
		break;
		case 'del':
		$db->query("DELETE FROM ".$PL_CFG['tb_pre']."table WHERE id='".$deltab."'");
		$pathcache = PL_ROOT."Phpleft/".$modulename."/";
		$tablem=sql_cache("table=table&where=module='".$modulename."' and moduleid='".$modulename."'&order=keylist asc");
		cache_write($files, $tablem, $pathcache);
		msg('删除成功', 'phpleft.php?file=module&table='.$modulename);
		break;
		case 'key':
		$do->into($alladd,'table');
		$pathcache = PL_ROOT."Phpleft/".$alladd[module]."/";
        $tablem=sql_cache("table=table&where=module='".$alladd[module]."' and moduleid='".$alladd[module]."'&order=keylist asc");
        cache_write($files, $tablem, $pathcache);
        msg('添加字段成功', 'phpleft.php?file=module&table='.$alladd[module]);
		break;
	}
	break;
	//插件
	case 'plugins':
	switch($input) {
	case 'install':
	$set = cache_read('config.inc.php',PL_ROOT."plugins/".$install."/");
    $do->into($set,'plugins');
	$tableadd = cache_read('myisam.inc.php',PL_ROOT."plugins/".$install."/");
	$sql = "CREATE TABLE ".$PL_CFG['tb_pre'].$install." 
	(
	 id int NOT NULL AUTO_INCREMENT, 
	 PRIMARY KEY(id)".$tableadd['text']."
	 ) DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci";
	$db->query($sql);
	msg('安装成功', 'phpleft.php?file=plugins');
	break;
	case 'delete':
	$db->query("DELETE FROM ".$PL_CFG['tb_pre']."plugins WHERE plugins='$delete'");	
	$sqltable = $PL_CFG['tb_pre'].$delete;
    $db->query("drop table ".$sqltable);
	msg('删除成功', 'phpleft.php?file=plugins');
	break;
	case 'del':
	if(!$delete) exit(msg('删除的参数不存在！', 'phpleft.php?file=plugins'));
    $dirpath=PL_ROOT.'plugins/'.$delete.'/';
    deldir($dirpath);
	msg('彻底删除成功', 'phpleft.php?file=plugins');
	break;
	
}
	break;
	//模块字段
	case 'menukey':
	switch($input) {
	case 'del':
    $db->query("DELETE FROM ".$PL_CFG['tb_pre']."table WHERE id='".$deltab."'");
	$db->query("ALTER TABLE ".$PL_CFG['tb_pre'].modname($menuid)."_".$menuid." DROP COLUMN ".$keys);
	$docache->menu_cache(4);
    msg('删除成功', 'phpleft.php?file=menukey&menuid='.$menuid.'&show=key');
	break;
	case 'paixu':
	if ($inid<>''){
    foreach($inid as $id=>$t) { 
     $query=$db->query("update 	".$PL_CFG['tb_pre']."table set keylist=".$keylist[$id]." where id='".$t."'");
    }
    $docache->menu_cache(4);
    msg('更新排序成功', 'phpleft.php?file=menukey&menuid='.$menuid.'&show=key');
   }else{
    msg('没有数据，无任何操作！', 'phpleft.php?file=menukey&menuid='.$menuid.'&show=key');	
    }
   break;
   case 'add':
   $file = 'table_'.$alladd[moduleid].'.inc.php';
   $do->into($alladd,'table');
   $docache->menu_cache(4);
   $length = $alladd[length] ? "(".$alladd[length].")" : '';
   $db->query("ALTER TABLE ".$PL_CFG['tb_pre'].$alladd[module]."_".$alladd[moduleid]." ADD ".$alladd[thiskeys]." ".$alladd[keystype].$length);
   msg('添加字段成功', 'phpleft.php?file=menukey&menuid='.$alladd[moduleid].'&show=key');
   break;
   case 'bj':
   $do->update($alladd,'table','id',$id);
   $docache->menu_cache(4);
   msg('修改字段成功', 'phpleft.php?file=menukey&menuid='.$alladd[moduleid].'&show=key');
   break;
}
	
	break;
	//数据中心
	case 'database':
	switch($input) {
	case 'del':
	$db->query("TRUNCATE TABLE $tabthis");
	msg('清空成功！', 'phpleft.php?file=database');
	break;
	}
	break;
}


?>