bar [section1] => Array ( [foo1] => bar1 ) ) * * Struktura pliku .cnf*.inc.php "", // ... ]; return $conf; }; */ 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; } } try { if (empty($_cnf[$conf_file])) { $file_suffix = '.inc.php'; $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix; $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)) { $fun = include $f; if (!is_callable($fun)) throw new Exception("Config func is not callable"); $_cnf[$conf_file] = $fun('secret-p5-password'); break; } } } } catch (Exception $e) { DBG::log($e); } return $_cnf[$conf_file]; } /** * activeProject - SE/projects/{activeProject}/ * may be set by ENV or in CRM_CONFIG */ public static function getProjectName() { static $_activeProject = null; if (null !== $_activeProject) return $_activeProject; $activeProject = V::get('P5_ACTIVE_PROJECT', '', $_SERVER); DBG::log("\$activeProject = '{$activeProject}' (ENV)"); if ($activeProject) { $path = APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}"; if (!file_exists($path)) throw new Exception("Missing project folder '{$activeProject}' (env)"); } if (!$activeProject) { $activeProject = DB::getPDO()->fetchValue(" select c.CONF_VAL from CRM_CONFIG c where c.CONF_KEY = 'activeProject' "); if ($activeProject) { $path = APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}"; if (!file_exists($path)) throw new Exception("Missing project folder '{$activeProject}' (db)"); } } DBG::log("\$activeProject = '{$activeProject}' (ENV, DB)"); $_activeProject = (string)$activeProject; return $_activeProject; } public static function getProjectPath() { $activeProject = self::getProjectName(); if (!$activeProject) return null; return APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}"; } }