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

namespace Kolab;

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

	private $data = array();
	private $valid = false;

	/**
	 * Singelton getter
	 */
	public static function getInstance($dir = null)
	{
		if (!isset(self::$instance)) {
			self::$instance = new FBConfig($dir);
		}

		if ($dir && !self::$instance->valid)
			self::$instance->_load($dir);

		return self::$instance;
	}

	/**
	 * Default constructor
	 *
	 * @param string Path to load config from
	 */
	function __construct($configdir = null)
	{
		if ($configdir) {
			$this->_load($configdir);
		}
	}
	
	private function _load($configdir)
	{
		if ($raw = @parse_ini_file($configdir . '/config.ini', true)) {
			foreach ($raw as $section => $values) {
				// check for known sections
				if (in_array($section, array('httpauth','trustednetworks','log'))) {
					$this->data[$section] = $values;
				}
				else if (isset($values['fbsource'])){
					$this->data['directories'][] = $values;
				}
			}

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

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