Config.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * require define(APP_PATH_CONFIG)
  4. *
  5. * Config file name format:
  6. ** po "-" zawsze $host ($_SERVER['SERVER_NAME']), inne "--"
  7. ** main config file
  8. * .cnf.ini.php
  9. * .cnf-{$host}.ini.php
  10. ** special $config_file
  11. * .cnf--{$config_file}.ini.php
  12. * .cnf--{$config_file}-{$host}.ini.php
  13. **** eg. "external_ids":
  14. * .cnf--external_ids.ini.php
  15. * .cnf--external_ids-{$host}.ini.php
  16. **** eg. $config_file = zasob_{$ID}
  17. * .cnf--zasob_{$ID}.ini.php - config file for zasob $ZASOB_ID
  18. * .cnf--zasob_{$ID}-{$host}.ini.php - config file for zasob $ZASOB_ID on host
  19. **** eg. $config_file = column_init_{$ID}
  20. * .cnf--column_init_{$table}.ini.php - config file for column
  21. * .cnf--column_init_{$table}-{$host}.ini.php - config file for column on host
  22. *
  23. * TODO: dziedziczenie plikow na podstawie hosta - kropki? dla hosta www2.biall.com.pl i www.biall.com.pl dziedziczy po: biall.com.pl
  24. *
  25. * If conf file not found '.cnf*.ini.php' then try to find file '.cnf*.inc.php'
  26. *
  27. * Struktura pliku .cnf*.ini.php
  28. ;<?php
  29. ;die(); // For further security
  30. ;// @COMMENT
  31. foo="bar"
  32. [section1]
  33. foo1="bar1"
  34. * parse_ini_file( file, true ) # true - process_sections
  35. Array
  36. (
  37. [foo] => bar
  38. [section1] => Array
  39. (
  40. [foo1] => bar1
  41. )
  42. )
  43. *
  44. * Struktura pliku .cnf*.inc.php
  45. <?php
  46. return function ($secret = '') {
  47. if ('secret-p5-password' !== $secret) return null;
  48. $conf = [
  49. 'host' => "",
  50. // ...
  51. ];
  52. return $conf;
  53. };
  54. */
  55. class Config {
  56. public static function get($key, $section = null) {
  57. static $_main_config;
  58. if ($_main_config === null) {
  59. $_main_config = self::getConfFile();
  60. }
  61. if ($key == 'get-all-data') {
  62. return $_main_config;
  63. }
  64. $cur_conf = $_main_config;
  65. if ($section !== null) {
  66. $cur_conf = V::get($section, null, $_main_config);
  67. }
  68. return V::get($key, null, $cur_conf);
  69. }
  70. public static function getData() {
  71. return self::get('get-all-data');
  72. }
  73. /**
  74. * Search for zasob config ini file.
  75. * Config file name must contain zasob_{$ID} for example:
  76. * .cnf--zasob_{$ID}.ini.php - config file for zasob $ZASOB_ID
  77. * .cnf--zasob_{$ID}-{$host}.ini.php - config file for zasob $ZASOB_ID on host
  78. */
  79. public static function getZasobConf($zasob_id) {
  80. return self::getConfFile("zasob_{$zasob_id}");
  81. }
  82. /**
  83. * Search for zasob config ini file.
  84. */
  85. public static function getColumnConf($col) {
  86. return self::getConfFile("column_init_{$col}");
  87. }
  88. public static function generateDefaultDbConfigFile($params = [], $outputFormat = 'ini') {
  89. if (empty($params['database'])) throw new Exception("Missing database name");
  90. if (empty($params['password'])) throw new Exception("Missing password");
  91. if (!in_array($outputFormat, ['ini', 'inc'])) throw new Exception("Not implemented outputFormat = '{$outputFormat}'");
  92. if ('inc' == $outputFormat) {
  93. return implode("\n", [
  94. '<?php',
  95. 'return function ($secret = "") {',
  96. ' if ("secret-p5-password" !== $secret) return null;',
  97. ' $conf = [];',
  98. ' $conf["type"] = "mysql";',
  99. ' $conf["host"] = "127.0.0.1";',
  100. ' $conf["port"] = "3306";',
  101. ' $conf["user"] = "root";',
  102. ' $conf["pass"] = "' . $params['password'] . '";',
  103. ' $conf["database"] = "' . $params['database'] . '";',
  104. ' $conf["zasob_id"] = "2";',
  105. ' return $conf;',
  106. '};',
  107. ]);
  108. }
  109. return implode("\n", [
  110. ';<?php',
  111. ';die(); // For further security',
  112. ';// default DB',
  113. '',
  114. 'type="mysql"',
  115. 'host="127.0.0.1"',
  116. 'port="3306"',
  117. 'user="root"',
  118. 'pass="' . $params['password'] . '"',
  119. 'database="' . $params['database'] . '"',
  120. 'zasob_id="2"',
  121. '',
  122. ]);
  123. }
  124. public static function generateDefaultLdapConfigFile($params = [], $outputFormat = 'ini') {
  125. if (empty($params['user'])) throw new Exception("Missing user");
  126. if (empty($params['pass'])) throw new Exception("Missing pass");
  127. if (empty($params['base_dn'])) throw new Exception("Missing base_dn");
  128. if (!in_array($outputFormat, ['ini', 'inc'])) throw new Exception("Not implemented outputFormat = '{$outputFormat}'");
  129. if ('inc' == $outputFormat) {
  130. return implode("\n", [
  131. '<?php',
  132. 'return function ($secret = "") {',
  133. ' if ("secret-p5-password" !== $secret) return null;',
  134. ' $conf = [];',
  135. ' $conf["version"] = "3";',
  136. ' $conf["host"] = "127.0.0.1";',
  137. ' // $conf["port"] = "3306";',
  138. ' $conf["user"] = "' . $params['user'] . '";',
  139. ' $conf["pass"] = "' . $params['pass'] . '";',
  140. ' $conf["base_dn"] = "' . $params['base_dn'] . '";',
  141. ' return $conf;',
  142. '};',
  143. ]);
  144. }
  145. return implode("\n", [
  146. ';<?php',
  147. ';die(); // For further security',
  148. ';// default LDAP',
  149. '',
  150. 'version="3"',
  151. 'host="127.0.0.1"',
  152. ';//port="3306"',
  153. 'user="' . $params['user'] . '"',
  154. 'pass="' . $params['pass'] . '"',
  155. 'base_dn="' . $params['base_dn'] . '"',
  156. '',
  157. ]);
  158. }
  159. /**
  160. * Search for config ini file.
  161. * TODO: $conf_file == '' - main config file
  162. */
  163. public static function getConfFile($conf_file = '') {
  164. static $_cnf;
  165. if (!is_array($_cnf)) $_cnf = array();
  166. if (array_key_exists($conf_file, $_cnf)) {
  167. return $_cnf[$conf_file];
  168. }
  169. $_cnf[$conf_file] = null;
  170. $cnf = null;
  171. $file_prefix = '.cnf';
  172. if ($conf_file != '') $file_prefix .= '--'.$conf_file;
  173. $file_suffix = '.ini.php';
  174. $search_for_files = array();// kolejka includowania plikow
  175. $host = $_SERVER['SERVER_NAME'];
  176. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix;
  177. // dziedziczenie - subdomain np. www.biall.com.pl i www2.biall.com.pl -> biall.com.pl
  178. $host_subdomain_exp = explode('.', $host);
  179. $host_subdomain = reset($host_subdomain_exp);
  180. $host_parent = substr($host, strlen($host_subdomain . '.'));
  181. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host_parent . $file_suffix;
  182. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . $file_suffix;
  183. foreach ($search_for_files as $f) {
  184. if (1 == V::get('DBG_CNF', '', $_GET)) { echo "f(" . end(explode('/',$f)) . ")=(" . file_exists($f) . ")"; }
  185. if (file_exists($f)) {
  186. Lib::loadClass('Core_Config_INI');
  187. $cnf = new Core_Config_INI($f);
  188. $_cnf[$conf_file] = $cnf->getData();
  189. break;
  190. }
  191. }
  192. try {
  193. if (empty($_cnf[$conf_file])) {
  194. $file_suffix = '.inc.php';
  195. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix;
  196. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host_parent . $file_suffix;
  197. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . $file_suffix;
  198. foreach ($search_for_files as $f) {
  199. if (1 == V::get('DBG_CNF', '', $_GET)) { echo "f(" . end(explode('/',$f)) . ")=(" . file_exists($f) . ")"; }
  200. if (file_exists($f)) {
  201. $fun = include $f;
  202. if (!is_callable($fun)) throw new Exception("Config func is not callable");
  203. $_cnf[$conf_file] = $fun('secret-p5-password');
  204. if (null === $_cnf[$conf_file]) throw new Exception("Config error");
  205. break;
  206. }
  207. }
  208. }
  209. } catch (Exception $e) {
  210. DBG::log($e);
  211. }
  212. return $_cnf[$conf_file];
  213. }
  214. /**
  215. * activeProject - SE/projects/{activeProject}/
  216. * may be set by ENV or in CRM_CONFIG
  217. */
  218. public static function getProjectName() {
  219. static $_activeProject = null;
  220. if (null !== $_activeProject) return $_activeProject;
  221. $activeProject = V::get('P5_ACTIVE_PROJECT', '', $_SERVER);
  222. DBG::log("\$activeProject = '{$activeProject}' (ENV)");
  223. if ($activeProject) {
  224. $path = APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}";
  225. if (!file_exists($path)) throw new Exception("Missing project folder '{$activeProject}' (env)");
  226. }
  227. if (!$activeProject) {
  228. $activeProject = DB::getPDO()->fetchValue(" select c.CONF_VAL from CRM_CONFIG c where c.CONF_KEY = 'activeProject' ");
  229. if ($activeProject) {
  230. $path = APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}";
  231. if (!file_exists($path)) throw new Exception("Missing project folder '{$activeProject}' (db)");
  232. }
  233. }
  234. DBG::log("\$activeProject = '{$activeProject}' (ENV, DB)");
  235. $_activeProject = (string)$activeProject;
  236. return $_activeProject;
  237. }
  238. public static function getProjectPath() {
  239. $activeProject = self::getProjectName();
  240. if (!$activeProject) return null;
  241. return APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}";
  242. }
  243. }