config-functions.php 3.5 KB

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