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

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)) {
			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'])){
					$config['directories'][] = $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 = '')
	{
		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;
			}
		}
	}

	/**
	 * 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);
	}
}