0
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2024-09-19 19:22:14 +02:00
postfixadmin/model/Config.php

246 lines
6.7 KiB
PHP
Raw Permalink Normal View History

<?php
2022-06-28 14:46:11 +02:00
# $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
# Therefore the class is marked as final to prevent someone accidently does this ;-)
2021-03-22 10:28:28 +01:00
final class Config
{
private static $instance = null;
/**
* @var array
*/
2020-09-28 21:33:54 +02:00
private $config = [];
# do not error_log() 'undefined config option' for deprecated options
private static $deprecated_options = array(
'min_password_length',
);
/**
* Return a singleton instance of Config
* @return Config
*/
2021-04-13 22:19:16 +02:00
public static function getInstance()
{
2018-01-26 15:45:57 +01:00
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Used to write a dynamic var in the Configure instance.
*
2020-09-28 21:33:54 +02:00
* @param string $key
* @param mixed $value to set for key.
* @return void
*/
2021-04-13 22:19:16 +02:00
public static function write(string $key, $value = null)
{
$_this = self::getInstance();
$newConfig = $_this->getAll();
2020-09-28 21:33:54 +02:00
$newConfig[$key] = $value;
$_this->setAll($newConfig);
}
2018-12-27 22:43:11 +01:00
/**
* @param string $var
* @return array
2018-12-27 22:43:11 +01:00
*/
2022-06-28 14:46:11 +02:00
public static function read_array(string $var): array
2021-04-13 22:19:16 +02:00
{
2018-12-27 22:43:11 +01:00
$stuff = self::read($var);
2018-12-28 20:31:43 +01:00
if (!is_array($stuff)) {
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;
}
2022-06-28 14:46:11 +02:00
public static function has(string $var): bool
2021-04-13 22:19:16 +02:00
{
$x = self::getInstance()->getAll();
return array_key_exists($var, $x);
}
2018-12-27 22:43:11 +01:00
/**
* @param string $var
* @return string
*/
2022-06-28 14:46:11 +02:00
public static function read_string(string $var): string
2021-04-13 22:19:16 +02:00
{
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)) {
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;
}
/**
* @param string $var Variable to obtain
2020-09-28 21:33:54 +02:00
* @return callable|array|string|null|bool some value
*/
2021-04-13 22:19:16 +02:00
public static function read(string $var)
{
$_this = self::getInstance();
$config = $_this->getAll();
if ($var === 'all') {
return $config;
}
if (isset($config[$var])) {
return $config[$var];
}
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');
}
return null;
}
2018-01-26 15:45:57 +01: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
*/
2022-06-28 14:46:11 +02:00
public static function read_f(string $var, string $value): string
2021-04-13 22:19:16 +02:00
{
2018-12-27 22:43:11 +01:00
$text = self::read_string($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;
}
/**
* 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)
*
* 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)
*/
2022-06-28 14:46:11 +02:00
public static function bool(string $var): bool
2021-04-13 22:19:16 +02:00
{
$value = self::read($var);
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)) {
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;
}
$value = strtoupper($value);
if ($value == 'YES' || $value == 'TRUE') { # YES
return true;
} elseif ($value == 'NO' || $value == 'FALSE') { # NO
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)");
return false;
}
}
/**
* Used to read Config::$var, converted to bool, returned as integer (0 or 1)
* @see bool()
*/
2022-06-28 14:46:11 +02:00
public static function intbool($var): int
2021-04-13 22:19:16 +02:00
{
return Config::bool($var) ? 1 : 0;
}
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
*/
2022-06-28 14:46:11 +02:00
public static function lang(string $var): string
2021-04-13 22:19:16 +02:00
{
$languages = self::read_array('__LANG');
$value = $languages[$var] ?? '';
2018-12-28 20:31:43 +01:00
if (!is_string($value)) {
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
}
2018-12-27 22:43:11 +01:00
return $value;
}
2018-01-26 15:45:57 +01: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
*/
2022-06-28 14:46:11 +02:00
public static function lang_f(string $var, $value): string
2021-04-13 22:19:16 +02:00
{
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;
}
/**
* @return array
*/
2022-06-28 14:46:11 +02:00
public function getAll(): array
2021-04-13 22:19:16 +02:00
{
2020-09-28 21:33:54 +02:00
return $this->config;
}
/**
* @param array $config
*/
2021-04-13 22:19:16 +02:00
public function setAll(array $config)
{
$this->config = $config;
}
}
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */