<?php
/**
 * require define(APP_PATH_CONFIG)
 *
 * Config file name format:
 ** po "-" zawsze $host ($_SERVER['SERVER_NAME']), inne "--"
 ** main config file
 * .cnf.ini.php
 * .cnf-{$host}.ini.php
 ** special $config_file
 * .cnf--{$config_file}.ini.php
 * .cnf--{$config_file}-{$host}.ini.php
 **** eg. "external_ids":
 *   .cnf--external_ids.ini.php
 *   .cnf--external_ids-{$host}.ini.php
 **** eg. $config_file = zasob_{$ID}
 *   .cnf--zasob_{$ID}.ini.php - config file for zasob $ZASOB_ID
 *   .cnf--zasob_{$ID}-{$host}.ini.php - config file for zasob $ZASOB_ID on host
 **** eg. $config_file = column_init_{$ID}
 *   .cnf--column_init_{$table}.ini.php - config file for column
 *   .cnf--column_init_{$table}-{$host}.ini.php - config file for column on host
 *
 * TODO: dziedziczenie plikow na podstawie hosta - kropki? dla hosta www2.biall.com.pl i www.biall.com.pl dziedziczy po: biall.com.pl
 *
 * Struktura pliku .cnf*.ini.php
;<?php
;die(); // For further security
;// @COMMENT

foo="bar"
[section1]
foo1="bar1"
 * parse_ini_file( file, true ) # true - process_sections
Array
(
    [foo] => bar
    [section1] => Array
        (
            [foo1] => bar1
        )
)
 */

class Config {

	public static function get($key, $section = null) {
		static $_main_config;

		if ($_main_config === null) {
			$_main_config = self::getConfFile();
		}

		if ($key == 'get-all-data') {
			return $_main_config;
		}

		$cur_conf = $_main_config;
		if ($section !== null) {
			$cur_conf = V::get($section, null, $_main_config);
		}
		return V::get($key, null, $cur_conf);
	}

	public static function getData() {
		return self::get('get-all-data');
	}

	/**
	 * Search for zasob config ini file.
	 * Config file name must contain zasob_{$ID} for example:
	 *   .cnf--zasob_{$ID}.ini.php - config file for zasob $ZASOB_ID
	 *   .cnf--zasob_{$ID}-{$host}.ini.php - config file for zasob $ZASOB_ID on host
	 */
	public static function getZasobConf($zasob_id) {
		return self::getConfFile("zasob_{$zasob_id}");
	}

	/**
	 * Search for zasob config ini file.
	 */
	public static function getColumnConf($col) {
		return self::getConfFile("column_init_{$col}");
	}

	/**
	 * Search for config ini file.
	 * TODO: $conf_file == '' - main config file
	 */
	public static function getConfFile($conf_file = '') {
		static $_cnf;
		if (!is_array($_cnf)) $_cnf = array();

		if (array_key_exists($conf_file, $_cnf)) {
			return $_cnf[$conf_file];
		}
		$_cnf[$conf_file] = null;

		$cnf = null;
		$file_prefix = '.cnf';
		if ($conf_file != '') $file_prefix .= '--'.$conf_file;
		$file_suffix = '.ini.php';
		$search_for_files = array();// kolejka includowania plikow
		$host = $_SERVER['SERVER_NAME'];
		$search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix;

		// dziedziczenie - subdomain np. www.biall.com.pl i www2.biall.com.pl -> biall.com.pl
		$host_subdomain_exp = explode('.', $host);
		$host_subdomain = reset($host_subdomain_exp);
		$host_parent = substr($host, strlen($host_subdomain . '.'));
		$search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host_parent . $file_suffix;

		$search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . $file_suffix;
		foreach ($search_for_files as $f) {
			if (1 == V::get('DBG_CNF', '', $_GET)) { echo "f(" . end(explode('/',$f)) . ")=(" . file_exists($f) . ")"; }
			if (file_exists($f)) {
				Lib::loadClass('Core_Config_INI');
				$cnf = new Core_Config_INI($f);
				$_cnf[$conf_file] = $cnf->getData();
				break;
			}
		}

		return $_cnf[$conf_file];
	}

}
