Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * Struktura pliku .cnf*.ini.php
  26. ;<?php
  27. ;die(); // For further security
  28. ;// @COMMENT
  29. foo="bar"
  30. [section1]
  31. foo1="bar1"
  32. * parse_ini_file( file, true ) # true - process_sections
  33. Array
  34. (
  35. [foo] => bar
  36. [section1] => Array
  37. (
  38. [foo1] => bar1
  39. )
  40. )
  41. */
  42. class Config {
  43. public static function get($key, $section = null) {
  44. static $_main_config;
  45. if ($_main_config === null) {
  46. $_main_config = self::getConfFile();
  47. }
  48. if ($key == 'get-all-data') {
  49. return $_main_config;
  50. }
  51. $cur_conf = $_main_config;
  52. if ($section !== null) {
  53. $cur_conf = V::get($section, null, $_main_config);
  54. }
  55. return V::get($key, null, $cur_conf);
  56. }
  57. public static function getData() {
  58. return self::get('get-all-data');
  59. }
  60. /**
  61. * Search for zasob config ini file.
  62. * Config file name must contain zasob_{$ID} for example:
  63. * .cnf--zasob_{$ID}.ini.php - config file for zasob $ZASOB_ID
  64. * .cnf--zasob_{$ID}-{$host}.ini.php - config file for zasob $ZASOB_ID on host
  65. */
  66. public static function getZasobConf($zasob_id) {
  67. return self::getConfFile("zasob_{$zasob_id}");
  68. }
  69. /**
  70. * Search for zasob config ini file.
  71. */
  72. public static function getColumnConf($col) {
  73. return self::getConfFile("column_init_{$col}");
  74. }
  75. /**
  76. * Search for config ini file.
  77. * TODO: $conf_file == '' - main config file
  78. */
  79. public static function getConfFile($conf_file = '') {
  80. static $_cnf;
  81. if (!is_array($_cnf)) $_cnf = array();
  82. if (array_key_exists($conf_file, $_cnf)) {
  83. return $_cnf[$conf_file];
  84. }
  85. $_cnf[$conf_file] = null;
  86. $cnf = null;
  87. $file_prefix = '.cnf';
  88. if ($conf_file != '') $file_prefix .= '--'.$conf_file;
  89. $file_suffix = '.ini.php';
  90. $search_for_files = array();// kolejka includowania plikow
  91. $host = $_SERVER['SERVER_NAME'];
  92. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host . $file_suffix;
  93. // dziedziczenie - subdomain np. www.biall.com.pl i www2.biall.com.pl -> biall.com.pl
  94. $host_subdomain_exp = explode('.', $host);
  95. $host_subdomain = reset($host_subdomain_exp);
  96. $host_parent = substr($host, strlen($host_subdomain . '.'));
  97. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . '-' . $host_parent . $file_suffix;
  98. $search_for_files[] = APP_PATH_CONFIG . DS . $file_prefix . $file_suffix;
  99. foreach ($search_for_files as $f) {
  100. if (1 == V::get('DBG_CNF', '', $_GET)) { echo "f(" . end(explode('/',$f)) . ")=(" . file_exists($f) . ")"; }
  101. if (file_exists($f)) {
  102. Lib::loadClass('Core_Config_INI');
  103. $cnf = new Core_Config_INI($f);
  104. $_cnf[$conf_file] = $cnf->getData();
  105. break;
  106. }
  107. }
  108. return $_cnf[$conf_file];
  109. }
  110. }