Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigCacheLoader.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Friendica\Core\Config\Cache;
  3. use Friendica\Core\Addon;
  4. /**
  5. * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
  6. *
  7. * It is capable of loading the following config files:
  8. * - *.config.php (current)
  9. * - *.ini.php (deprecated)
  10. * - *.htconfig.php (deprecated)
  11. */
  12. class ConfigCacheLoader
  13. {
  14. /**
  15. * The Sub directory of the config-files
  16. * @var string
  17. */
  18. const SUBDIRECTORY = 'config';
  19. private $baseDir;
  20. private $configDir;
  21. public function __construct($baseDir)
  22. {
  23. $this->baseDir = $baseDir;
  24. $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
  25. }
  26. /**
  27. * Load the configuration files
  28. *
  29. * First loads the default value for all the configuration keys, then the legacy configuration files, then the
  30. * expected local.config.php
  31. */
  32. public function loadConfigFiles(ConfigCache $config)
  33. {
  34. // Setting at least the basepath we know
  35. $config->set('system', 'basepath', $this->baseDir);
  36. $config->load($this->loadCoreConfig('defaults'));
  37. $config->load($this->loadCoreConfig('settings'));
  38. $config->load($this->loadLegacyConfig('htpreconfig'), true);
  39. $config->load($this->loadLegacyConfig('htconfig'), true);
  40. $config->load($this->loadCoreConfig('local'), true);
  41. }
  42. /**
  43. * Tries to load the specified core-configuration and returns the config array.
  44. *
  45. * @param string $name The name of the configuration
  46. *
  47. * @return array The config array (empty if no config found)
  48. *
  49. * @throws \Exception if the configuration file isn't readable
  50. */
  51. public function loadCoreConfig($name)
  52. {
  53. if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
  54. return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
  55. } elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
  56. return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
  57. } else {
  58. return [];
  59. }
  60. }
  61. /**
  62. * Tries to load the specified addon-configuration and returns the config array.
  63. *
  64. * @param string $name The name of the configuration
  65. *
  66. * @return array The config array (empty if no config found)
  67. *
  68. * @throws \Exception if the configuration file isn't readable
  69. */
  70. public function loadAddonConfig($name)
  71. {
  72. $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
  73. Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
  74. $name . DIRECTORY_SEPARATOR . // openstreetmap/
  75. self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/
  76. $name . ".config.php"; // openstreetmap.config.php
  77. if (file_exists($filepath)) {
  78. return $this->loadConfigFile($filepath);
  79. } else {
  80. return [];
  81. }
  82. }
  83. /**
  84. * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
  85. *
  86. * @param string $name The name of the config file
  87. *
  88. * @return array The configuration array (empty if no config found)
  89. *
  90. * @deprecated since version 2018.09
  91. */
  92. private function loadLegacyConfig($name)
  93. {
  94. $filePath = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
  95. if (file_exists($filePath)) {
  96. $a = new \stdClass();
  97. $a->config = [];
  98. include $filePath;
  99. if (isset($db_host)) {
  100. $a->config['database']['hostname'] = $db_host;
  101. unset($db_host);
  102. }
  103. if (isset($db_user)) {
  104. $a->config['database']['username'] = $db_user;
  105. unset($db_user);
  106. }
  107. if (isset($db_pass)) {
  108. $a->config['database']['password'] = $db_pass;
  109. unset($db_pass);
  110. }
  111. if (isset($db_data)) {
  112. $a->config['database']['database'] = $db_data;
  113. unset($db_data);
  114. }
  115. if (isset($a->config['system']['db_charset'])) {
  116. $a->config['database']['charset'] = $a->config['system']['charset'];
  117. }
  118. if (isset($pidfile)) {
  119. $a->config['system']['pidfile'] = $pidfile;
  120. unset($pidfile);
  121. }
  122. if (isset($default_timezone)) {
  123. $a->config['system']['default_timezone'] = $default_timezone;
  124. unset($default_timezone);
  125. }
  126. if (isset($lang)) {
  127. $a->config['system']['language'] = $lang;
  128. unset($lang);
  129. }
  130. return $a->config;
  131. } else {
  132. return [];
  133. }
  134. }
  135. /**
  136. * Tries to load the specified legacy configuration file and returns the config array.
  137. *
  138. * @deprecated since version 2018.12
  139. * @param string $filepath
  140. *
  141. * @return array The configuration array
  142. * @throws \Exception
  143. */
  144. private function loadINIConfigFile($filepath)
  145. {
  146. $contents = include($filepath);
  147. $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
  148. if ($config === false) {
  149. throw new \Exception('Error parsing INI config file ' . $filepath);
  150. }
  151. return $config;
  152. }
  153. /**
  154. * Tries to load the specified configuration file and returns the config array.
  155. *
  156. * The config format is PHP array and the template for configuration files is the following:
  157. *
  158. * <?php return [
  159. * 'section' => [
  160. * 'key' => 'value',
  161. * ],
  162. * ];
  163. *
  164. * @param string $filepath The filepath of the
  165. * @return array The config array0
  166. *
  167. * @throws \Exception if the config cannot get loaded.
  168. */
  169. private function loadConfigFile($filepath)
  170. {
  171. $config = include($filepath);
  172. if (!is_array($config)) {
  173. throw new \Exception('Error loading config file ' . $filepath);
  174. }
  175. return $config;
  176. }
  177. }