| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?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
- *
- * If conf file not found '.cnf*.ini.php' then try to find file '.cnf*.inc.php'
- *
- * 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
- )
- )
- *
- * Struktura pliku .cnf*.inc.php
- <?php
- return function ($secret = '') {
- if ('secret-p5-password' !== $secret) return null;
- $conf = [
- 'host' => "",
- // ...
- ];
- 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}");
- }
- public static function generateDefaultDbConfigFile($params = [], $outputFormat = 'ini') {
- if (empty($params['database'])) throw new Exception("Missing database name");
- if (empty($params['password'])) throw new Exception("Missing password");
- if (!in_array($outputFormat, ['ini', 'inc'])) throw new Exception("Not implemented outputFormat = '{$outputFormat}'");
- if ('inc' == $outputFormat) {
- return implode("\n", [
- '<?php',
- 'return function ($secret = "") {',
- ' if ("secret-p5-password" !== $secret) return null;',
- ' $conf = [];',
- ' $conf["type"] = "mysql";',
- ' $conf["host"] = "127.0.0.1";',
- ' $conf["port"] = "3306";',
- ' $conf["user"] = "root";',
- ' $conf["pass"] = "' . $params['password'] . '";',
- ' $conf["database"] = "' . $params['database'] . '";',
- ' $conf["zasob_id"] = "2";',
- ' return $conf;',
- '};',
- ]);
- }
- return implode("\n", [
- ';<?php',
- ';die(); // For further security',
- ';// default DB',
- '',
- 'type="mysql"',
- 'host="127.0.0.1"',
- 'port="3306"',
- 'user="root"',
- 'pass="' . $params['password'] . '"',
- 'database="' . $params['database'] . '"',
- 'zasob_id="2"',
- '',
- ]);
- }
- public static function generateDefaultLdapConfigFile($params = [], $outputFormat = 'ini') {
- if (empty($params['user'])) throw new Exception("Missing user");
- if (empty($params['pass'])) throw new Exception("Missing pass");
- if (empty($params['base_dn'])) throw new Exception("Missing base_dn");
- if (!in_array($outputFormat, ['ini', 'inc'])) throw new Exception("Not implemented outputFormat = '{$outputFormat}'");
- if ('inc' == $outputFormat) {
- return implode("\n", [
- '<?php',
- 'return function ($secret = "") {',
- ' if ("secret-p5-password" !== $secret) return null;',
- ' $conf = [];',
- ' $conf["version"] = "3";',
- ' $conf["host"] = "127.0.0.1";',
- ' // $conf["port"] = "3306";',
- ' $conf["user"] = "' . $params['user'] . '";',
- ' $conf["pass"] = "' . $params['pass'] . '";',
- ' $conf["base_dn"] = "' . $params['base_dn'] . '";',
- ' return $conf;',
- '};',
- ]);
- }
- return implode("\n", [
- ';<?php',
- ';die(); // For further security',
- ';// default LDAP',
- '',
- 'version="3"',
- 'host="127.0.0.1"',
- ';//port="3306"',
- 'user="' . $params['user'] . '"',
- 'pass="' . $params['pass'] . '"',
- 'base_dn="' . $params['base_dn'] . '"',
- '',
- ]);
- }
- /**
- * 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');
- if (null === $_cnf[$conf_file]) throw new Exception("Config error");
- 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}";
- }
- }
|