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 6.8KB

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