<?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
*/
$success=true;

Db::i()->query(
	"CREATE TABLE IF NOT EXISTS ?_config (
	`type` enum('config','access') NOT NULL default 'config',
	`module_id` int(11) NOT NULL default '0' COMMENT '0 - core',
	`group_id` int(11) NOT NULL default '-1' COMMENT '-1 - гости',
	`key` varchar(100) NOT NULL,
	`value` varchar(255) NOT NULL default '',
	PRIMARY KEY  (`group_id` , `module_id` , `key` , `type`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8"
);

$configs=glob(LAB_PATH_DATA.'/configs/*.conf');
foreach ($configs as $cfg) {
	$data=$config=$access=array();
	include $cfg;
	$name=basename($cfg, '.conf');
	//[1] - group_id, [2]-module_id
	if ($name=='core' || preg_match('/(-?\d+)_(\d+)/', $name, $data)) {
		foreach ($config as $k=>$v) {
			config_save($k, $v, $data[1]?$data[1]:-1, $data[2]?$data[2]:0, 'config');
		}
		foreach ($access as $k=>$v) {
			config_save($k, $v, $data[1]?$data[1]:-1, $data[2]?$data[2]:0, 'access');
		}
	}
}

rename(LAB_PATH_DATA.'/configs', LAB_PATH_DATA.'/configs_del');

function config_save($key, $value, $group_id, $module_id, $type)
{
	$value=is_object($value)?'':((is_array($value))?serialize($value):$value);
	Db::i()->query(
			"INSERT INTO ?_config (`type`, module_id, group_id, `key`, `value`)
			VALUES(?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `value`=?",
			$type, $module_id, $group_id, $key, $value, $value
		);
}