summaryrefslogtreecommitdiff
path: root/lib/Kolab/FreeBusy/Config.php
blob: ab82a1ad76d5c6d9af2f5220345d514bcc707b5a (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
<?php

/**
 * This file is part of the Kolab Server Free/Busy Service
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
 *
 * 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/>.
 */

namespace Kolab\FreeBusy;

/**
 * Wrapper class for service configuration
 */
class Config
{
	private static $instance;

	private $basedir = '.';
	private $data = array();
	private $valid = false;

	/**
	 * Singelton getter
	 *
	 * @param string Path to load config from
	 */
	public static function getInstance($dir = null)
	{
		if (!isset(self::$instance)) {
			self::$instance = new Config($dir);
		}

		if ($dir && !self::$instance->valid) {
			self::$instance->load($configdir . '/config.ini');
		}

		return self::$instance;
	}

	/**
	 * Default constructor
	 */
	function __construct($configdir = null)
	{
		if ($configdir) {
			$this->basedir = $configdir;
			$this->load($configdir . '/config.ini');
		}
	}

	/**
	 * Load config from the given .ini file
	 */
	private function load($inifile)
	{
		if ($raw = parse_ini_file($inifile, true)) {
			$config['directories'] = array();
			foreach ($raw as $section => $values) {
				// check for known sections
				if (in_array($section, array('httpauth','trustednetworks','log'))) {
					$config[$section] = $values;
				}
				else if (strpos($section, 'directory') === 0 || isset($values['fbsource'])) {
					$sect = preg_replace('/^directory\s*/', '', $section);
					$key = strlen($sect) ? $sect : count($config['directories']);
					$config['directories'][$key] = $values;
				}
				else if (!empty($values) && is_array($values)) {
					$config[$section] = $values;
				}
			}

			$this->register($config);
			$this->valid = !empty($this->data['directories']);
		}
		else {
			trigger_error("Failed to parse configuration from $inifile", E_USER_ERROR);
		}
	}

	/**
	 * Dump the hierarchical structure of config options into a flat list with keys delimited by dots
	 */
	private function register($config, $prefix = '')
	{
		// merge the new config values over existing data
		if (empty($prefix)) {
			$this->data = array_replace_recursive($this->data, $config);
		}
		else if (is_array($config)) {
			$pkey = rtrim($prefix, '.');
			$this->data[$pkey] = is_array($this->data[$pkey]) ? array_replace_recursive((array)$this->data[$pkey], $config) : $config;
		}

		foreach ((array)$config as $key => $val) {
			if (is_array($val)) {
				$this->register($val, "$prefix$key.");
			}
			else {
				$this->data[$prefix.$key] = $val;
			}
		}

		// resolve references in config options (e.g. %(foo.bar))
		if (empty($prefix)) {
			array_walk_recursive($this->data, array($this, 'resolve_reference'));
		}
	}

	/**
	 * Callback to resolve references in the given config option value
	 */
	private function resolve_reference(&$value, $key)
	{
		if (is_string($value)) {
			$value = preg_replace_callback('/%[({]([\w.]+)[})]/i', array($this, 'replace_reference'), $value);
		}
	}

	/**
	 * Callback function to replace the given reference with the read config value
	 */
	private function replace_reference($m)
	{
		return $this->data[$m[1]];
	}

	/**
	 * Magic getter for direct read-only access to config options
	 */
	public function __get($name)
	{
		return $this->data[$name];
	}

	/** 
	 * Common getter for config options with fallback in default values
	 *
	 * @param string Config option name
	 * @param mixed  Default value if option isn't set in config
	 * @return mixed Config option value
	 */
	public function get($name, $default = null)
	{
		return array_key_exists($name, $this->data) ? $this->data[$name] : $default;
	}

	/**
	 * Determines whether we have a valid configuration loaded
	 *
	 * @return boolean True if valid, False otherwise
	 */
	public function isValid()
	{
		return !empty($this->data);
	}
}