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.

Config.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. # $Id$
  3. # This class is too static - if you inherit a class from it, it will share the static $instance and all its contents
  4. # Therefore the class is marked as final to prevent someone accidently does this ;-)
  5. final class Config {
  6. private static $instance = null;
  7. /**
  8. * @var array
  9. */
  10. private $config;
  11. # do not error_log() 'undefined config option' for deprecated options
  12. private static $deprecated_options = array(
  13. 'min_password_length',
  14. );
  15. /**
  16. * Return a singleton instance of Config
  17. * @return Config
  18. */
  19. public static function getInstance() {
  20. if (self::$instance == null) {
  21. self::$instance = new self();
  22. }
  23. return self::$instance;
  24. }
  25. /**
  26. * Used to write a dynamic var in the Configure instance.
  27. *
  28. * Usage
  29. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  30. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  31. * Configure::write('One', array('key1'=>'value of the Configure::One[key1]', 'key2'=>'value of the Configure::One[key2]');
  32. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]', 'One.key2' => 'value of the Configure::One[key2]'));
  33. *
  34. * @param mixed $config string or array of var to write
  35. * @param mixed $value to set for key.
  36. * @return void
  37. */
  38. public static function write($config, $value = null) {
  39. $_this = self::getInstance();
  40. if (!is_array($config)) {
  41. $config = array($config => $value);
  42. }
  43. $newConfig = $_this->getAll();
  44. foreach ($config as $names => $value) {
  45. $name = $_this->__configVarNames($names);
  46. switch (count($name)) {
  47. case 3:
  48. $newConfig[$name[0]][$name[1]][$name[2]] = $value;
  49. break;
  50. case 2:
  51. $newConfig[$name[0]][$name[1]] = $value;
  52. break;
  53. case 1:
  54. $newConfig[$name[0]] = $value;
  55. break;
  56. }
  57. }
  58. $_this->setAll($newConfig);
  59. }
  60. /**
  61. * Used to read Configure::$var
  62. *
  63. * Usage
  64. * Configure::read('Name'); will return all values for Name
  65. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  66. *
  67. * @param string $var Variable to obtain
  68. * @return string value of Configure::$var
  69. * @access public
  70. */
  71. public static function read($var) {
  72. $_this = self::getInstance();
  73. $config = $_this->getAll();
  74. if ($var === 'all') {
  75. return $config;
  76. }
  77. $name = $_this->__configVarNames($var);
  78. switch (count($name)) {
  79. case 3:
  80. $zero = $name[0];
  81. $one = $name[1];
  82. $two = $name[2];
  83. if (isset($config[$zero], $config[$zero][$one], $config[$zero][$one][$two])) {
  84. return $config[$zero][$one][$two];
  85. }
  86. break;
  87. case 2:
  88. $zero = $name[0];
  89. $one = $name[1];
  90. if (isset($config[$zero], $config[$zero][$one])) {
  91. return $config[$zero][$one];
  92. }
  93. break;
  94. case 1:
  95. $zero = $name[0];
  96. if (isset($config[$zero])) {
  97. return $config[$zero];
  98. }
  99. break;
  100. }
  101. if (!in_array(join('.', $name), self::$deprecated_options)) {
  102. error_log('Config::read(): attempt to read undefined config option "' . join('.', $name) . '", returning null');
  103. }
  104. return null;
  105. }
  106. /**
  107. * read Config::$var and apply sprintf on it
  108. * also checks if $var is changed by sprintf - if not, it writes a warning to error_log
  109. *
  110. * @param string $var Variable to obtain
  111. * @param string $value Value to use as sprintf parameter
  112. * @return string value of Config::$var, parsed by sprintf
  113. * @access public
  114. */
  115. public static function read_f($var, $value) {
  116. $text = self::read($var);
  117. $newtext = sprintf($text, $value);
  118. # check if sprintf changed something - if not, there are chances that $text didn't contain a %s
  119. if ($text == $newtext) {
  120. if (is_array($var)) {
  121. $var = join('.', $var);
  122. }
  123. error_log("$var used via read_f, but nothing replaced (value $value)");
  124. }
  125. return $newtext;
  126. }
  127. /**
  128. * Used to read Config::$var, converted to boolean
  129. * (obviously only useful for settings that can be YES or NO)
  130. *
  131. * Usage
  132. * Configure::read('Name'); will return the value for Name, converted to boolean
  133. *
  134. * @param string $var Variable to obtain
  135. * @return bool value of Configure::$var (TRUE (on YES/yes) or FALSE (on NO/no/not set/unknown value)
  136. * @access public
  137. */
  138. public static function bool($var) {
  139. $value = self::read($var);
  140. if (strtoupper($value) == 'YES') { # YES
  141. return true;
  142. } elseif (strtoupper($value) == 'NO') { # NO
  143. return false;
  144. } else { # unknown value
  145. # show and log error message on unknown value
  146. $msg = "\$CONF['$var'] has an invalid value, should be 'YES' or 'NO'";
  147. flash_error($msg);
  148. error_log("$msg (value: $value)");
  149. return false;
  150. }
  151. }
  152. /**
  153. * Used to read Config::$var, converted to bool, returned as integer (0 or 1)
  154. * @see bool()
  155. */
  156. public static function intbool($var) {
  157. return Config::bool($var) ? 1 : 0;
  158. }
  159. /**
  160. * Get translated text from $PALANG
  161. * (wrapper for self::read(), see also the comments there)
  162. *
  163. * @param string $var Variable to obtain
  164. * @return string value of $PALANG[$var]
  165. * @access public
  166. */
  167. public static function lang($var) {
  168. return self::read(array('__LANG', $var));
  169. }
  170. /**
  171. * Get translated text from $PALANG and apply sprintf on it
  172. * (wrapper for self::read_f(), see also the comments there)
  173. *
  174. * @param string $var Text (from $PALANG) to obtain
  175. * @param string $value Value to use as sprintf parameter
  176. * @return string value of $PALANG[$var], parsed by sprintf
  177. * @access public
  178. */
  179. public static function lang_f($var, $value) {
  180. return self::read_f(array('__LANG', $var), $value);
  181. }
  182. /**
  183. * @return array
  184. */
  185. public function getAll() {
  186. $output = $this->config;
  187. return $output;
  188. }
  189. /**
  190. * @param array $config
  191. */
  192. public function setAll(array $config) {
  193. $this->config = $config;
  194. }
  195. /**
  196. * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
  197. *
  198. * @param mixed $name Name to split
  199. * @return array Name separated in items through dot notation
  200. * @access private
  201. */
  202. private function __configVarNames($name) {
  203. if (is_string($name)) {
  204. if (strpos($name, ".")) {
  205. return explode(".", $name);
  206. }
  207. return array($name);
  208. }
  209. return $name;
  210. }
  211. }
  212. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */