Config.php 3.5 KB

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