File: Config.php

package info (click to toggle)
postfixadmin 4.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,888 kB
  • sloc: php: 12,256; perl: 1,156; sh: 717; python: 142; xml: 63; sql: 3; makefile: 2
file content (246 lines) | stat: -rw-r--r-- 6,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php

# $Id$

# 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 ;-)
final class Config
{
    private static $instance = null;

    /**
     * @var array
     */
    private $config = [];

    # do not error_log() 'undefined config option' for deprecated options
    private static $deprecated_options = array(
        'min_password_length',
        'create_mailbox_subdirs',
    );

    /**
     * Return a singleton instance of Config
     * @return Config
     */
    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Used to write a dynamic var in the Configure instance.
     *
     * @param string $key
     * @param mixed $value to set for key.
     * @return void
     */
    public static function write(string $key, $value = null)
    {
        $_this = self::getInstance();
        $newConfig = $_this->getAll();
        $newConfig[$key] = $value;
        $_this->setAll($newConfig);
    }

    /**
     * @param string $var
     * @return array
     */
    public static function read_array(string $var): array
    {
        $stuff = self::read($var);

        if (!is_array($stuff)) {
            trigger_error('In ' . __FUNCTION__ . ": expected config $var to be an array, but received a " . gettype($stuff), E_USER_ERROR);
        }

        return $stuff;
    }

    public static function has(string $var): bool
    {
        $x = self::getInstance()->getAll();
        return array_key_exists($var, $x);
    }
    /**
     * @param string $var
     * @return string
     */
    public static function read_string(string $var): string
    {
        $stuff = self::read($var);

        if ($stuff === null) {
            return '';
        }

        if (!is_string($stuff)) {
            trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string, but received a " . gettype($stuff), E_USER_ERROR);
            return '';
        }

        return $stuff;
    }

    /**
     * @param string $var Variable to obtain
     * @return callable|array|string|null|bool some value
     */
    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)) {
            error_log('Config::read(): attempt to read undefined config option "' . $var . '", returning null');
        }

        return null;
    }

    /**
     * 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
     */
    public static function read_f(string $var, string $value): string
    {
        $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
     * (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)
     */
    public static function bool(string $var): bool
    {
        $value = self::read($var);

        if (is_bool($value)) {
            return $value;
        }

        if (!is_string($value)) {
            trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string, but received a " . gettype($value), E_USER_ERROR);
            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()
     */
    public static function intbool($var): int
    {
        return Config::bool($var) ? 1 : 0;
    }


    /**
     * 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
     */
    public static function lang(string $var): string
    {
        $languages = self::read_array('__LANG');

        $value = $languages[$var] ?? '';

        if (!is_string($value)) {
            trigger_error('In ' . __FUNCTION__ . ": expected config $var to be a string , but received a " . gettype($value), E_USER_ERROR);
        }

        return $value;
    }

    /**
     * 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
     */
    public static function lang_f(string $var, $value): string
    {
        $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
     */
    public function getAll(): array
    {
        return $this->config;
    }

    /**
     * @param array $config
     */
    public function setAll(array $config)
    {
        $this->config = $config;
    }
}

/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */