PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
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.

226 lines
6.5 KiB

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