config-functions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // Create config file in the return syntax
  3. function createConfig($array, $path = null, $nest = 0)
  4. {
  5. $path = ($path) ? $path : $GLOBALS['userConfigPath'];
  6. // Define Initial Value
  7. $output = array();
  8. // Sort Items
  9. ksort($array);
  10. // Update the current config version
  11. if (!$nest) {
  12. // Inject Current Version
  13. $output[] = "\t'configVersion' => '" . (isset($array['apply_CONFIG_VERSION']) ? $array['apply_CONFIG_VERSION'] : $GLOBALS['installedVersion']) . "'";
  14. }
  15. unset($array['configVersion']);
  16. unset($array['apply_CONFIG_VERSION']);
  17. // Process Settings
  18. foreach ($array as $k => $v) {
  19. $allowCommit = true;
  20. $item = '';
  21. switch (gettype($v)) {
  22. case 'boolean':
  23. $item = ($v ? 'true' : 'false');
  24. break;
  25. case 'integer':
  26. case 'double':
  27. case 'NULL':
  28. $item = $v;
  29. break;
  30. case 'string':
  31. $item = "'" . str_replace(array('\\', "'"), array('\\\\', "\'"), $v) . "'";
  32. break;
  33. case 'array':
  34. $item = createConfig($v, false, $nest + 1);
  35. break;
  36. default:
  37. $allowCommit = false;
  38. }
  39. if ($allowCommit) {
  40. $output[] = str_repeat("\t", $nest + 1) . "'$k' => $item";
  41. }
  42. }
  43. // Build output
  44. $output = (!$nest ? "<?php\nreturn " : '') . "array(\n" . implode(",\n", $output) . "\n" . str_repeat("\t", $nest) . ')' . (!$nest ? ';' : '');
  45. if (!$nest && $path) {
  46. $pathDigest = pathinfo($path);
  47. @mkdir($pathDigest['dirname'], 0770, true);
  48. if (file_exists($path)) {
  49. rename($path, $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '.bak.php');
  50. }
  51. $file = fopen($path, 'w');
  52. fwrite($file, $output);
  53. fclose($file);
  54. if (file_exists($path)) {
  55. return true;
  56. }
  57. // writeLog("error", "config was unable to write");
  58. return false;
  59. } else {
  60. // writeLog("success", "config was updated with new values");
  61. return $output;
  62. }
  63. }
  64. // Commit new values to the configuration
  65. function updateConfig($new, $current = false)
  66. {
  67. // Get config if not supplied
  68. if ($current === false) {
  69. $current = loadConfig();
  70. } elseif (is_string($current) && is_file($current)) {
  71. $current = loadConfig($current);
  72. }
  73. // Inject Parts
  74. foreach ($new as $k => $v) {
  75. $current[$k] = $v;
  76. }
  77. // Return Create
  78. return createConfig($current);
  79. }
  80. function configLazy()
  81. {
  82. // Load config or default
  83. if (file_exists($GLOBALS['userConfigPath'])) {
  84. $config = fillDefaultConfig(loadConfig($GLOBALS['userConfigPath']));
  85. } else {
  86. $config = fillDefaultConfig(loadConfig($GLOBALS['defaultConfigPath']));
  87. }
  88. if (is_array($config)) {
  89. defineConfig($config);
  90. }
  91. return $config;
  92. }
  93. function loadConfig($path = null)
  94. {
  95. $path = ($path) ? $path : $GLOBALS['userConfigPath'];
  96. if (!is_file($path)) {
  97. return null;
  98. } else {
  99. return (array)call_user_func(function () use ($path) {
  100. return include($path);
  101. });
  102. }
  103. }
  104. function fillDefaultConfig($array)
  105. {
  106. $path = $GLOBALS['defaultConfigPath'];
  107. if (is_string($path)) {
  108. $loadedDefaults = loadConfig($path);
  109. } else {
  110. $loadedDefaults = $path;
  111. }
  112. // Include all plugin config files
  113. foreach (glob(dirname(__DIR__, 1) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . "*.php") as $filename) {
  114. $loadedDefaults = array_merge($loadedDefaults, loadConfig($filename));
  115. }
  116. return (is_array($loadedDefaults) ? fillDefaultConfig_recurse($array, $loadedDefaults) : false);
  117. }
  118. function fillDefaultConfig_recurse($current, $defaults)
  119. {
  120. foreach ($defaults as $k => $v) {
  121. if (!isset($current[$k])) {
  122. $current[$k] = $v;
  123. } elseif (is_array($current[$k]) && is_array($v)) {
  124. $current[$k] = fillDefaultConfig_recurse($current[$k], $v);
  125. }
  126. }
  127. return $current;
  128. }
  129. function defineConfig($array, $anyCase = true, $nest_prefix = false)
  130. {
  131. foreach ($array as $k => $v) {
  132. if (is_scalar($v) && !defined($nest_prefix . $k)) {
  133. $GLOBALS[$nest_prefix . $k] = $v;
  134. } elseif (is_array($v)) {
  135. defineConfig($v, $anyCase, $nest_prefix . $k . '_');
  136. }
  137. }
  138. }