summaryrefslogtreecommitdiffstats
path: root/lib/Conf.php
blob: f4da8548e6dfa5cb07e468722b38560519c13d54 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2012, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 | Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                     |
 +--------------------------------------------------------------------------+
*/

class Conf {
    static private $instance;

    private $_conf = array();

    const CONFIG_FILE = '/etc/kolab/kolab.conf';

    const STRING = 0;
    const BOOL   = 1;
    const INT    = 2;
    const FLOAT  = 3;
    const AUTO   = 4;

    /**
     * This implements the 'singleton' design pattern
     *
     * @return Conf The one and only instance
     */
    static function get_instance()
    {
        if (!self::$instance) {
            self::$instance = new Conf();
        }

        return self::$instance;
    }

    public function __construct()
    {
        if (!@is_readable(self::CONFIG_FILE)) {
            trigger_error("Unable to read " . self::CONFIG_FILE, E_USER_ERROR);
        }

        // Do some magic configuration loading here.
        $this->read_config();
    }

    public function get($key1, $key2 = null, $type = null)
    {
        $value = $this->expand($this->get_raw($key1, $key2));

        if ($value === null) {
            return $value;
        }

        switch ($type) {
            case self::INT:
                return intval($value);

            case self::FLOAT:
                return floatval($value);

            case self::BOOL:
                return (bool) preg_match('/^(true|1|on|enabled|yes)$/i', $value);

            case self::AUTO:
                return $this->auto($value);
        }

        return (string) $value;
    }

    public function get_list($key1, $key2 = NULL)
    {
        $list  = array();
        $value = $this->get($key1, $key2);
        $value_components = explode(',', $value);

        foreach ($value_components as $component) {
            $component = trim($component);
            if (!empty($component)) {
                $list[] = $component;
            }
        }

        return $list;
    }

    public function get_raw($key1, $key2 = NULL)
    {
        if (isset($this->_conf[$key1])) {
            if ($key2) {
                if (isset($this->_conf[$key1][$key2])) {
                    return $this->_conf[$key1][$key2];
                }
                else if (isset($this->_conf['kolab'][$key2])) {
                    return $this->_conf['kolab'][$key2];
                }
            }
            else {
                return $this->_conf[$key1];
            }
        }

        // If section is specified, but requested setting doesn't exist there
        // we fall back to (global) 'kolab' section
        if ($key2) {
            if (isset($this->_conf['kolab'][$key2])) {
                return $this->_conf['kolab'][$key2];
            }
/*
            // fall back to primary domain section
            $primary_domain = $this->_conf['kolab']['primary_domain'];
            if ($primary_domain && isset($this->_conf[$primary_domain])) {
                if (isset($this->_conf[$primary_domain][$key2])) {
                    return $this->_conf[$primary_domain][$key2];
                }
            }
*/
            return null;
        }

        // Simple (global) settings may be obtained by calling the key and
        // omitting the section. This goes for sections 'kolab', and whatever
        // is the equivalent of 'kolab', 'auth_mechanism', such as getting
        // 'ldap_uri', which is in the [$domain] section, or in section 'ldap',
        // and we can try and iterate over it.

        // First, try user domain.
        if (isset($_SESSION['user']) && method_exists($_SESSION['user'], 'get_domain')) {
            try {
                $domain = $_SESSION['user']->get_domain();

                if ($domain && isset($this->_conf[$domain])) {
                    if (isset($this->_conf[$domain][$key1])) {
                        return $this->_conf[$domain][$key1];
                    }
                }
            } catch (Exception $e) { }
        }

        // Fall back to whatever is the equivalent of auth_mechanism as the
        // section (i.e. 'ldap', or 'sql')
        $auth_mech = $this->_conf['kolab']['auth_mechanism'];
        if (isset($this->_conf[$auth_mech])) {
            if (isset($this->_conf[$auth_mech][$key1])) {
                return $this->_conf[$auth_mech][$key1];
            }
        }

        // Fall back to global settings in the 'kolab' section.
        if (isset($this->_conf['kolab'][$key1])) {
            return $this->_conf['kolab'][$key1];
        }

//        error_log("Could not find setting for \$key1: " . $key1 .
//                " with \$key2: " . $key2);

        return null;
    }

    public function expand($str, $custom = FALSE)
    {
        if (preg_match_all('/%\((?P<variable>\w+)\)s/', $str, $matches)) {
            if (isset($matches['variable']) && !empty($matches['variable'])) {
                if (is_array($matches['variable'])) {
                    foreach ($matches['variable'] as $key => $value) {
                        if (is_array($custom) && array_key_exists($value, $custom)) {
                            $str = str_replace("%(" . $value . ")s", $custom[$value], $str);
                        }

                        $str = str_replace("%(" . $value . ")s", $this->get($value), $str);
                    }

                    return $str;
                }
                else {
                    return str_replace("%(" . $matches['variable'] . ")s", $this->get($matches['variable']), $str);
                }
            }

            return $str;
        }
        else {
            return $str;
        }
    }

    private function read_config()
    {
        $_ini_raw = file(self::CONFIG_FILE);

        $this->_conf = array();

        foreach ($_ini_raw as $_line) {
            if (preg_match('/^\[([a-z0-9-_\.]+)\]/', $_line, $matches)) {
                $_cur_section = $matches[1];
                $this->_conf[$_cur_section] = array();
                unset($_cur_key);
            }

            if (preg_match('/^;/', $_line, $matches)) {
            }

            if (preg_match('/^([a-z0-9\.-_]+)\s*=\s*(.*)/', $_line, $matches)) {
                if (isset($_cur_section) && !empty($_cur_section)) {
                    $_cur_key = $matches[1];
                    $this->_conf[$_cur_section][$matches[1]] = isset($matches[2]) ? $matches[2] : '';
                }
            }

            if (preg_match('/^\s+(.*)$/', $_line, $matches)) {
                if (isset($_cur_key) && !empty($_cur_key)) {
                    $this->_conf[$_cur_section][$_cur_key] .= $matches[1];
                }
            }
        }
    }

    /**
     * Auto-detection and conversion of config value
     */
    private function auto($value)
    {
        if (preg_match('/^(true|on|enabled|yes|false|off|disabled|no)$/i', $value)) {
            return (bool) preg_match('/^(true|on|enabled|yes)$/i', $value);
        }

        if (is_numeric($value)) {
            if (strpos($value, '.')) {
                return floatval($value);
            }
            else {
                return intval($value);
            }
        }

        // array in JSON format
        if ($value[0] == '{' || $value[0] == '[') {
             return json_decode($value, true);
        }

        return (string) $value;
    }
}