Config.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * @param string $key
  29. * @param mixed $value to set for key.
  30. * @return void
  31. */
  32. public static function write(string $key, $value = null) {
  33. $_this = self::getInstance();
  34. $newConfig = $_this->getAll();
  35. $newConfig[$key] = $value;
  36. $_this->setAll($newConfig);
  37. }
  38. /**
  39. * @param string $var
  40. * @return array
  41. */
  42. public static function read_array(string $var) : array {
  43. $stuff = self::read($var);
  44. if (!is_array($stuff)) {
  45. trigger_error('In ' . __FUNCTION__ . ": expected config $var to be an array, but received a " . gettype($stuff), E_USER_ERROR);
  46. }
  47. return $stuff;
  48. }
  49. public static function has(string $var) : bool {
  50. $x = self::getInstance()->getAll();
  51. return array_key_exists($var, $x);
  52. }
  53. /**
  54. * @param string $var
  55. * @return string
  56. */
  57. public static function read_string(string $var) : string {
  58. $stuff = self::read($var);
  59. if ($stuff === null) {
  60. return '';
  61. }
  62. if (!is_string($stuff)) {
  63. trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string, but received a " . gettype($stuff), E_USER_ERROR);
  64. return '';
  65. }
  66. return $stuff;
  67. }
  68. /**
  69. * @param string $var Variable to obtain
  70. * @return callable|array|string|null|bool some value
  71. */
  72. public static function read(string $var) {
  73. $_this = self::getInstance();
  74. $config = $_this->getAll();
  75. if ($var === 'all') {
  76. return $config;
  77. }
  78. if (isset($config[$var])) {
  79. return $config[$var];
  80. }
  81. if (!in_array($var, self::$deprecated_options)) {
  82. error_log('Config::read(): attempt to read undefined config option "' . $var . '", returning null');
  83. }
  84. return null;
  85. }
  86. /**
  87. * read Config::$var and apply sprintf on it
  88. * also checks if $var is changed by sprintf - if not, it writes a warning to error_log
  89. *
  90. * @param string $var Variable to obtain
  91. * @param string $value Value to use as sprintf parameter
  92. * @return string value of Config::$var, parsed by sprintf
  93. */
  94. public static function read_f(string $var, string $value) : string {
  95. $text = self::read_string($var);
  96. $newtext = sprintf($text, $value);
  97. # check if sprintf changed something - if not, there are chances that $text didn't contain a %s
  98. if ($text == $newtext) {
  99. error_log("$var used via read_f, but nothing replaced (value $value)");
  100. }
  101. return $newtext;
  102. }
  103. /**
  104. * Used to read Config::$var, converted to boolean
  105. * (obviously only useful for settings that can be YES or NO, or boolean like values)
  106. *
  107. * Usage
  108. * Configure::read('Name'); will return the value for Name, converted to boolean
  109. *
  110. * @param string $var Variable to obtain
  111. * @return bool value of Configure::$var (TRUE (on YES/yes) or FALSE (on NO/no/not set/unknown value)
  112. */
  113. public static function bool(string $var) : bool {
  114. $value = self::read($var);
  115. if (is_bool($value)) {
  116. return $value;
  117. }
  118. if (!is_string($value)) {
  119. trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string, but received a " . gettype($value), E_USER_ERROR);
  120. error_log("config $var should be a string, found: " . json_encode($value));
  121. return false;
  122. }
  123. $value = strtoupper($value);
  124. if ($value == 'YES' || $value == 'TRUE') { # YES
  125. return true;
  126. } elseif ($value == 'NO' || $value == 'FALSE') { # NO
  127. return false;
  128. } else { # unknown value
  129. # show and log error message on unknown value
  130. $msg = "\$CONF['$var'] has an invalid value, should be 'YES' or 'NO'";
  131. flash_error($msg);
  132. error_log("$msg (value: $value)");
  133. return false;
  134. }
  135. }
  136. /**
  137. * Used to read Config::$var, converted to bool, returned as integer (0 or 1)
  138. * @see bool()
  139. */
  140. public static function intbool($var) : int {
  141. return Config::bool($var) ? 1 : 0;
  142. }
  143. /**
  144. * Get translated text from $PALANG
  145. * (wrapper for self::read(), see also the comments there)
  146. *
  147. * @param string $var Variable to obtain
  148. * @return string value of $PALANG[$var]
  149. * @access public
  150. */
  151. public static function lang(string $var) : string {
  152. $languages = self::read_array('__LANG');
  153. $value = $languages[$var] ?? '';
  154. if (!is_string($value)) {
  155. trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string , but received a " . gettype($value), E_USER_ERROR);
  156. }
  157. return $value;
  158. }
  159. /**
  160. * Get translated text from $PALANG and apply sprintf on it
  161. * (wrapper for self::read_f(), see also the comments there)
  162. *
  163. * @param string $var Text (from $PALANG) to obtain
  164. * @param string $value Value to use as sprintf parameter
  165. * @return string value of $PALANG[$var], parsed by sprintf
  166. */
  167. public static function lang_f(string $var, $value) : string {
  168. $all = self::read_array('__LANG');
  169. $text = $all[$var] ?? '';
  170. $newtext = sprintf($text, $value);
  171. # check if sprintf changed something - if not, there are chances that $text didn't contain a %s
  172. if ($text == $newtext) {
  173. error_log("$var used via read_f, but nothing replaced (value $value)");
  174. }
  175. return $newtext;
  176. }
  177. /**
  178. * @return array
  179. */
  180. public function getAll() : array {
  181. return $this->config;
  182. }
  183. /**
  184. * @param array $config
  185. */
  186. public function setAll(array $config) {
  187. $this->config = $config;
  188. }
  189. }
  190. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */