<?php
/*-------------------------------------
	版权所有：南京牛昆网络科技有限公司（TEL-15896098989）
	程序版本：1.0
	更新日期：2015-03-24 
-------------------------------------*/
class config
{
	private $db;
	private $table;
	private $membermodel_table;
	private $membergrade_table;
	private $adminrole_table;
	private $code_table;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=LA_PRE.'config';
		$this->model_table=LA_PRE.'model';
		$this->membergrade_table=LA_PRE.'member_grade';
		$this->adminrole_table=LA_PRE.'adminrole';
		$this->code_table=LA_PRE.'code';
	}

	function setconfigcode($code)
	{
		return $this->db->insert($this->code_table,array('code'=>$code));
	}

	function getconfigcode($id)
	{
		$r=$this->db->fetch_one("SELECT * FROM `$this->code_table` WHERE `$this->code_table`.`id`=".intval($id));
		return $r['code'];
	}

	function setconfig($setting,$moduleid=0)
	{
		$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`moduleid`=".intval($moduleid)." AND `$this->table`.`siteid`=".intval($_SESSION['siteid']));
		if($r)
		{		
			if(!$moduleid)
			{
				$base=array_map('addslashes',string2array($r['settings']));
				$setting=array_merge($base,$setting);
			}
			$this->db->query("UPDATE `$this->table` SET `$this->table`.`settings`='".addslashes(var_export($setting,true))."' WHERE `$this->table`.`moduleid`=".intval($moduleid)." AND`$this->table`.`siteid`=".intval($_SESSION['siteid']));
		}
		else
		{
			$this->db->insert($this->table,array('siteid'=>$_SESSION['siteid'],'settings'=>var_export($setting,true),'moduleid'=>intval($moduleid)),true);
		}
		
		if(!$moduleid)
		{
			$configfile = SYSTEM_ROOT.'data/config.inc.php';
			$pattern = $replacement = array();
			foreach($setting as $k=>$v)
			{
				$pattern[$k] = "/define\(\s*['\"]".strtoupper($k)."['\"]\s*,\s*([']?)[^']*([']?)\s*\)/is";
				$replacement[$k] = "define('".strtoupper($k)."', \${1}".$v."\${2})";
			}
			$str = file_get_contents($configfile);
			$str = preg_replace($pattern, $replacement, $str);
			return file_put_contents($configfile, $str);
		}

		return true;
	}

	function getconfig($siteid,$m=0)
	{
		$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`moduleid`=".intval($m)." AND `$this->table`.`siteid`=1");
		$base=array_map('trim',string2array($r['settings']));
		$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`moduleid`=".intval($m)." AND `$this->table`.`siteid`=".intval($siteid));
		
		$result=array_map('trim',string2array($r['settings']));	

		return array_merge($base,$result);
	}

	function getsiteconfig($siteid=0)
	{
		$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`moduleid`=0 AND `$this->table`.`siteid`=".intval($siteid));
		return array_map('stripslashes',string2array($r['settings']));
	}
	
	function getmembermodel()
	{
		return $this->db->fetch_all("SELECT * FROM `$this->model_table` WHERE `$this->model_table`.`typeid`='member' AND `$this->model_table`.`mid`=0");
	}

	function getmembergrade()
	{
		return $this->db->fetch_all("SELECT * FROM `$this->membergrade_table` WHERE 1");
	}

	function getadminrole()
	{
		return $this->db->fetch_all("SELECT * FROM `$this->adminrole_table` WHERE 1");
	}
}
?>
