Config.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /**
  89. * Search for config ini file.
  90. * TODO: $conf_file == '' - main config file
  91. */
  92. public static function getConfFile($conf_file = '') {
  93. static $_cnf;
  94. if (!is_array($_cnf)) $_cnf = array();
  95. if (array_key_exists($conf_file, $_cnf)) {
  96. return $_cnf[$conf_file];
  97. }
  98. $_cnf[$conf_file] = null;
  99. $cnf = null;
  100. $file_prefix = '.cnf';
  101. if ($conf_file != '') $file_prefix .= '--'.$conf_file;
  102. $file_suffix = '.ini.php';
  103. $search_for_files = array();// kolejka includowania plikow
  104. $host = $_SERVER['SERVER_NAME'];
  105. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix;
  106. // dziedziczenie - subdomain np. www.biall.com.pl i www2.biall.com.pl -> biall.com.pl
  107. $host_subdomain_exp = explode('.', $host);
  108. $host_subdomain = reset($host_subdomain_exp);
  109. $host_parent = substr($host, strlen($host_subdomain . '.'));
  110. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host_parent . $file_suffix;
  111. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . $file_suffix;
  112. foreach ($search_for_files as $f) {
  113. if (1 == V::get('DBG_CNF', '', $_GET)) { echo "f(" . end(explode('/',$f)) . ")=(" . file_exists($f) . ")"; }
  114. if (file_exists($f)) {
  115. Lib::loadClass('Core_Config_INI');
  116. $cnf = new Core_Config_INI($f);
  117. $_cnf[$conf_file] = $cnf->getData();
  118. break;
  119. }
  120. }
  121. try {
  122. if (empty($_cnf[$conf_file])) {
  123. $file_suffix = '.inc.php';
  124. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix;
  125. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host_parent . $file_suffix;
  126. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . $file_suffix;
  127. foreach ($search_for_files as $f) {
  128. if (1 == V::get('DBG_CNF', '', $_GET)) { echo "f(" . end(explode('/',$f)) . ")=(" . file_exists($f) . ")"; }
  129. if (file_exists($f)) {
  130. $fun = include $f;
  131. if (!is_callable($fun)) throw new Exception("Config func is not callable");
  132. $_cnf[$conf_file] = $fun('secret-p5-password');
  133. break;
  134. }
  135. }
  136. }
  137. } catch (Exception $e) {
  138. DBG::log($e);
  139. }
  140. return $_cnf[$conf_file];
  141. }
  142. /**
  143. * activeProject - SE/projects/{activeProject}/
  144. * may be set by ENV or in CRM_CONFIG
  145. */
  146. public static function getProjectName() {
  147. static $_activeProject = null;
  148. if (null !== $_activeProject) return $_activeProject;
  149. $activeProject = V::get('P5_ACTIVE_PROJECT', '', $_SERVER);
  150. DBG::log("\$activeProject = '{$activeProject}' (ENV)");
  151. if ($activeProject) {
  152. $path = APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}";
  153. if (!file_exists($path)) throw new Exception("Missing project folder '{$activeProject}' (env)");
  154. }
  155. if (!$activeProject) {
  156. $activeProject = DB::getPDO()->fetchValue(" select c.CONF_VAL from CRM_CONFIG c where c.CONF_KEY = 'activeProject' ");
  157. if ($activeProject) {
  158. $path = APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}";
  159. if (!file_exists($path)) throw new Exception("Missing project folder '{$activeProject}' (db)");
  160. }
  161. }
  162. DBG::log("\$activeProject = '{$activeProject}' (ENV, DB)");
  163. $_activeProject = (string)$activeProject;
  164. return $_activeProject;
  165. }
  166. public static function getProjectPath() {
  167. $activeProject = self::getProjectName();
  168. if (!$activeProject) return null;
  169. return APP_PATH_ROOT . DS . "projects" . DS . "{$activeProject}";
  170. }
  171. }