2011-10-17 23:47:58 +02:00
|
|
|
<?php
|
|
|
|
# $Id$
|
|
|
|
|
2018-01-26 15:45:57 +01:00
|
|
|
# This class is too static - if you inherit a class from it, it will share the static $instance and all its contents
|
2013-10-13 20:11:18 +02:00
|
|
|
# Therefore the class is marked as final to prevent someone accidently does this ;-)
|
|
|
|
final class Config {
|
2011-10-25 00:24:21 +02:00
|
|
|
private static $instance = null;
|
2013-10-13 20:11:18 +02:00
|
|
|
|
2018-04-29 22:52:57 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
private $config = [];
|
2018-04-29 22:52:57 +02:00
|
|
|
|
2014-03-18 22:57:47 +01:00
|
|
|
# do not error_log() 'undefined config option' for deprecated options
|
|
|
|
private static $deprecated_options = array(
|
|
|
|
'min_password_length',
|
|
|
|
);
|
|
|
|
|
2011-10-25 00:24:21 +02:00
|
|
|
/**
|
2018-04-29 22:52:57 +02:00
|
|
|
* Return a singleton instance of Config
|
|
|
|
* @return Config
|
2011-10-25 00:24:21 +02:00
|
|
|
*/
|
|
|
|
public static function getInstance() {
|
2018-01-26 15:45:57 +01:00
|
|
|
if (self::$instance == null) {
|
2011-10-25 00:24:21 +02:00
|
|
|
self::$instance = new self();
|
2011-10-17 23:47:58 +02:00
|
|
|
}
|
2011-10-25 00:24:21 +02:00
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to write a dynamic var in the Configure instance.
|
|
|
|
*
|
2020-09-28 21:33:54 +02:00
|
|
|
* @param string $key
|
2018-04-29 22:52:57 +02:00
|
|
|
* @param mixed $value to set for key.
|
2011-10-25 00:24:21 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function write(string $key, $value = null) {
|
2011-10-25 00:24:21 +02:00
|
|
|
$_this = self::getInstance();
|
2018-04-29 22:52:57 +02:00
|
|
|
$newConfig = $_this->getAll();
|
2020-09-28 21:33:54 +02:00
|
|
|
$newConfig[$key] = $value;
|
2018-04-29 22:52:57 +02:00
|
|
|
$_this->setAll($newConfig);
|
2011-10-25 00:24:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 22:43:11 +01:00
|
|
|
/**
|
|
|
|
* @param string $var
|
2020-04-14 22:26:01 +02:00
|
|
|
* @return array
|
2018-12-27 22:43:11 +01:00
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function read_array(string $var) : array {
|
2018-12-27 22:43:11 +01:00
|
|
|
$stuff = self::read($var);
|
|
|
|
|
2018-12-28 20:31:43 +01:00
|
|
|
if (!is_array($stuff)) {
|
2020-04-14 22:26:01 +02:00
|
|
|
trigger_error('In ' . __FUNCTION__ . ": expected config $var to be an array, but received a " . gettype($stuff), E_USER_ERROR);
|
2018-12-27 22:43:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $stuff;
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:02:41 +01:00
|
|
|
public static function has(string $var) : bool {
|
|
|
|
$x = self::getInstance()->getAll();
|
|
|
|
return array_key_exists($var, $x);
|
|
|
|
}
|
2018-12-27 22:43:11 +01:00
|
|
|
/**
|
|
|
|
* @param string $var
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function read_string(string $var) : string {
|
2018-12-27 22:43:11 +01:00
|
|
|
$stuff = self::read($var);
|
|
|
|
|
2018-12-28 20:31:43 +01:00
|
|
|
if ($stuff === null) {
|
2018-12-27 22:43:11 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-12-28 20:31:43 +01:00
|
|
|
if (!is_string($stuff)) {
|
2020-04-14 22:26:01 +02:00
|
|
|
trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string, but received a " . gettype($stuff), E_USER_ERROR);
|
2018-12-27 22:43:11 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stuff;
|
|
|
|
}
|
|
|
|
|
2011-10-25 00:24:21 +02:00
|
|
|
/**
|
|
|
|
* @param string $var Variable to obtain
|
2020-09-28 21:33:54 +02:00
|
|
|
* @return callable|array|string|null|bool some value
|
2011-10-25 00:24:21 +02:00
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function read(string $var) {
|
2011-10-25 00:24:21 +02:00
|
|
|
$_this = self::getInstance();
|
|
|
|
|
2018-04-29 22:52:57 +02:00
|
|
|
$config = $_this->getAll();
|
|
|
|
|
2011-10-25 00:24:21 +02:00
|
|
|
if ($var === 'all') {
|
2018-04-29 22:52:57 +02:00
|
|
|
return $config;
|
2011-10-17 23:47:58 +02:00
|
|
|
}
|
2011-10-25 00:24:21 +02:00
|
|
|
|
2020-04-14 22:26:01 +02:00
|
|
|
if (isset($config[$var])) {
|
|
|
|
return $config[$var];
|
2011-10-25 00:24:21 +02:00
|
|
|
}
|
2013-10-31 22:02:04 +01:00
|
|
|
|
2020-04-14 22:26:01 +02:00
|
|
|
if (!in_array($var, self::$deprecated_options)) {
|
2020-04-22 10:37:31 +02:00
|
|
|
error_log('Config::read(): attempt to read undefined config option "' . $var . '", returning null');
|
2014-03-18 22:57:47 +01:00
|
|
|
}
|
|
|
|
|
2011-10-25 00:24:21 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:45:57 +01:00
|
|
|
/**
|
2013-06-06 01:09:10 +02:00
|
|
|
* read Config::$var and apply sprintf on it
|
|
|
|
* also checks if $var is changed by sprintf - if not, it writes a warning to error_log
|
|
|
|
*
|
|
|
|
* @param string $var Variable to obtain
|
|
|
|
* @param string $value Value to use as sprintf parameter
|
|
|
|
* @return string value of Config::$var, parsed by sprintf
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function read_f(string $var, string $value) : string {
|
2018-12-27 22:43:11 +01:00
|
|
|
$text = self::read_string($var);
|
2013-06-06 01:09:10 +02:00
|
|
|
|
|
|
|
$newtext = sprintf($text, $value);
|
|
|
|
|
|
|
|
# check if sprintf changed something - if not, there are chances that $text didn't contain a %s
|
2013-11-10 17:00:20 +01:00
|
|
|
if ($text == $newtext) {
|
|
|
|
error_log("$var used via read_f, but nothing replaced (value $value)");
|
|
|
|
}
|
2013-06-06 01:09:10 +02:00
|
|
|
|
|
|
|
return $newtext;
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:35:30 +02:00
|
|
|
/**
|
2013-06-06 01:09:10 +02:00
|
|
|
* Used to read Config::$var, converted to boolean
|
2018-12-27 22:43:11 +01:00
|
|
|
* (obviously only useful for settings that can be YES or NO, or boolean like values)
|
2013-06-05 22:35:30 +02:00
|
|
|
*
|
|
|
|
* Usage
|
|
|
|
* Configure::read('Name'); will return the value for Name, converted to boolean
|
|
|
|
*
|
|
|
|
* @param string $var Variable to obtain
|
|
|
|
* @return bool value of Configure::$var (TRUE (on YES/yes) or FALSE (on NO/no/not set/unknown value)
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function bool(string $var) : bool {
|
2013-06-06 01:09:10 +02:00
|
|
|
$value = self::read($var);
|
2013-06-05 22:35:30 +02:00
|
|
|
|
2018-12-28 20:31:43 +01:00
|
|
|
if (is_bool($value)) {
|
2018-12-27 22:43:11 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2018-12-28 20:31:43 +01:00
|
|
|
if (!is_string($value)) {
|
2020-04-14 22:26:01 +02:00
|
|
|
trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string, but received a " . gettype($value), E_USER_ERROR);
|
2018-12-27 22:43:11 +01:00
|
|
|
error_log("config $var should be a string, found: " . json_encode($value));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:53:20 +02:00
|
|
|
$value = strtoupper($value);
|
|
|
|
if ($value == 'YES' || $value == 'TRUE') { # YES
|
2013-06-05 22:35:30 +02:00
|
|
|
return true;
|
2019-07-12 11:53:20 +02:00
|
|
|
} elseif ($value == 'NO' || $value == 'FALSE') { # NO
|
2013-10-13 20:11:18 +02:00
|
|
|
return false;
|
|
|
|
} else { # unknown value
|
|
|
|
# show and log error message on unknown value
|
|
|
|
$msg = "\$CONF['$var'] has an invalid value, should be 'YES' or 'NO'";
|
|
|
|
flash_error($msg);
|
|
|
|
error_log("$msg (value: $value)");
|
2013-06-05 22:35:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 22:47:28 +02:00
|
|
|
/**
|
|
|
|
* Used to read Config::$var, converted to bool, returned as integer (0 or 1)
|
|
|
|
* @see bool()
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function intbool($var) : int {
|
2013-06-06 22:47:28 +02:00
|
|
|
return Config::bool($var) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:35:30 +02:00
|
|
|
|
2018-01-26 15:45:57 +01:00
|
|
|
/**
|
2018-12-27 22:43:11 +01:00
|
|
|
* Get translated text from $PALANG
|
|
|
|
* (wrapper for self::read(), see also the comments there)
|
|
|
|
*
|
|
|
|
* @param string $var Variable to obtain
|
|
|
|
* @return string value of $PALANG[$var]
|
|
|
|
* @access public
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function lang(string $var) : string {
|
2020-04-22 10:34:25 +02:00
|
|
|
$languages = self::read_array('__LANG');
|
|
|
|
|
|
|
|
$value = $languages[$var] ?? '';
|
2018-12-28 20:27:33 +01:00
|
|
|
|
2018-12-28 20:31:43 +01:00
|
|
|
if (!is_string($value)) {
|
2020-04-14 22:26:01 +02:00
|
|
|
trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string , but received a " . gettype($value), E_USER_ERROR);
|
2018-12-27 22:43:11 +01:00
|
|
|
}
|
2020-04-22 10:34:25 +02:00
|
|
|
|
2018-12-27 22:43:11 +01:00
|
|
|
return $value;
|
2013-10-13 20:11:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-26 15:45:57 +01:00
|
|
|
/**
|
2013-10-13 20:11:18 +02:00
|
|
|
* Get translated text from $PALANG and apply sprintf on it
|
|
|
|
* (wrapper for self::read_f(), see also the comments there)
|
|
|
|
*
|
|
|
|
* @param string $var Text (from $PALANG) to obtain
|
|
|
|
* @param string $value Value to use as sprintf parameter
|
|
|
|
* @return string value of $PALANG[$var], parsed by sprintf
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public static function lang_f(string $var, $value) : string {
|
2020-05-02 20:08:47 +02:00
|
|
|
$all = self::read_array('__LANG');
|
|
|
|
|
|
|
|
$text = $all[$var] ?? '';
|
|
|
|
|
|
|
|
$newtext = sprintf($text, $value);
|
|
|
|
|
|
|
|
# check if sprintf changed something - if not, there are chances that $text didn't contain a %s
|
|
|
|
if ($text == $newtext) {
|
|
|
|
error_log("$var used via read_f, but nothing replaced (value $value)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newtext;
|
2013-10-13 20:11:18 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 22:52:57 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-09-28 21:33:54 +02:00
|
|
|
public function getAll() : array {
|
|
|
|
return $this->config;
|
2011-10-25 00:24:21 +02:00
|
|
|
}
|
2018-04-29 22:52:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
public function setAll(array $config) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2011-10-17 23:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|