<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class LabConfig
{
	static $cfg=array();
	static $mcfg=array();
	static $cached=false;
	
	static function init()
	{
		if ($data=@file_get_contents(LAB_PATH_DATA.'/objects/configs.obj'))
		{
			self::$cfg=unserialize($data);
		}
		if (empty(self::$cfg['cache'])) self::_set();
		if (self::$cfg['debug_mode']) self::$cfg['cache']=false;
		if (self::$cfg['cache'] && !self::$cached && !$data)
		{
			self::cache();
		}
		define('SECURECODE', self::$cfg["secure"]["code"]);
	}

	
	protected static function _set()
	{
		$config = array();
		$config = parse_ini_file(LAB_PATH_DATA."/cms.ini",true);
		self::$cfg=array_merge(self::$cfg, $config);
		
		$config = array();
		//подключаем глобальные настройки
		require_once(LAB_PATH_DATA.'/configs/core.conf');
		self::$cfg=array_merge(self::$cfg, $config);
		include LAB_PATH_KERNEL.'/install/revision.php';
		self::$cfg['version']['revision']=$revision;
		self::$cfg['version']['branch']=$branch;
	}
	
	public static function cache()
	{
		self::$cached=true;
		self::$cfg['lastCacheReset']=time();
		file_put_contents(LAB_PATH_DATA.'/objects/configs.obj', serialize(self::$cfg));
		@chmod(LAB_PATH_DATA.'/objects/configs.obj', 0666);
	}
	
	public static function save($data)
	{
		file_put_contents(LAB_PATH_DATA.'/configs/core.conf', $data);
		@chmod(LAB_PATH_DATA.'/configs/core.conf', 0666);
	}
	
	public static function get($var)
	{
		//небольшой костыль для примерения с несовершенством вселенной
		if (in_array($var, array('title', 'description', 'keywords')))
		{
			if (LabVars::${$var}!==null) return LabVars::${$var};
			if (LabCMS::i()->$var!==null) return LabCMS::i()->$var;
			if ($var=='title') return LabConfig::$cfg['site_name'];
			if ($var=='description') return LabConfig::$cfg['site_desc'];
			if ($var=='keywords') return LabConfig::$cfg['site_keywords'];
		}
		return (isset(LabVars::$cfg[$var]))?LabVars::$cfg[$var]:self::$cfg[$var];
	}
}
