File: config.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (291 lines) | stat: -rw-r--r-- 7,298 bytes parent folder | download | duplicates (3)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
 * FusionForge configuration functions
 *
 * Copyright 2009, Roland Mas
 * Copyright 2013, Franck Villaume - TrivialDev
 *
 * This file is part of FusionForge. FusionForge is free software;
 * you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the Licence, or (at your option)
 * any later version.
 *
 * FusionForge 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

// See docs in http://fusionforge.org/plugins/mediawiki/wiki/fusionforge/index.php/Configuration

/**
 *
 * Singleton FusionForge configuration database manager TODO : Enter better description here ...
 *
 */
class FusionForgeConfig {
	static protected $instance = NULL ;
	private $settings ;
	private $bools = array () ;

	/**
	 * Singleton accessor to the configuration database
	 * @return FusionForgeConfig instance
	 */
	static public function get_instance () {
		if (self::$instance == NULL) {
			self::$instance = new FusionForgeConfig () ;
		}
		return self::$instance ;
	}

	// TODO: add a constructor that initializes self::$instance to self ?

	public function get_sections () {
		return array_keys ($this->settings) ;
	}

	public function get_variables ($section='core') {
		if (isset ($this->settings[$section])) {
			return array_keys ($this->settings[$section]) ;
		}
		return array () ;
	}

	public function get_value ($section, $var) {
		if (!isset ($this->settings[$section])
		    || !isset ($this->settings[$section][$var])) {
			return NULL ;
		}

		$tmp = $this->settings[$section][$var] ;
		preg_match_all ('/\$[a-z_]+\/[a-z_]+/', $tmp, $matches) ;

		foreach ($matches[0] as $m) {
			$c = explode ('/', substr($m,1)) ;

			if (isset ($this->settings[$c[0]][$c[1]])) {
				$tmp = str_replace ($m, $this->get_value($c[0],$c[1]), $tmp) ;
			}
		}
		if ($this->is_bool ($section, $var)) {
			$tmp = $this->_interpret_as_bool ($tmp) ;
		}
		return $tmp ;
	}

	public function get_raw_value ($section, $var) {
		if (!isset ($this->settings[$section])
		    || !isset ($this->settings[$section][$var])) {
			return NULL ;
		}
		return $this->settings[$section][$var] ;
	}

	public function set_value ($section, $var, $value) {
		if (!isset ($this->settings[$section])) {
			$this->settings[$section] = array () ;
		}

		if (!isset ($this->settings[$section][$var])) {
			$this->settings[$section][$var] = $value ;
		}
	}

	public function reset_value ($section, $var, $value) {
		if (!isset ($this->settings[$section])) {
			$this->settings[$section] = array () ;
		}

		$this->settings[$section][$var] = $value ;
	}

	function read_config_file($filename) {
		if (file_exists($filename) && is_readable($filename)) {
			$sections = parse_ini_file($filename, true);
			if (is_array($sections)) {
				foreach ($sections as $section => $options) {
					if (is_array($options)) {
						foreach ($options as $var => $value) {
							$this->reset_value($section, $var, $value);
						}
					}
				}
			}
		}
		return ;
	}

	function mark_as_bool ($section, $var) {
		if (!array_key_exists ($section, $this->bools)) {
			$this->bools[$section] = array () ;
		}
		$this->bools[$section][$var] = true ;
	}

	function is_bool ($section, $var) {
		if (isset ($this->bools[$section])
		    && isset ($this->bools[$section][$var])) {
			return $this->bools[$section][$var] ;
		}
		return false ;
	}

	private function _interpret_as_bool ($val) {
		$val = strtolower ($val) ;
		switch ($val) {
		case 'true':
		case 'on':
		case 'yes':
		case '1':
			return true ;
		}

		return false ;
	}

}

if (!isset ($fusionforge_config)) {
	$fusionforge_config = new FusionForgeConfig();
}

/**
 * Get value of variable "name" in section "section"
 * @param string $var
 * @param string $section defaults to "core" if missing
 * @return mixed <NULL, boolean>
 */
function forge_get_config ($var, $section = 'core') {
	$c = FusionForgeConfig::get_instance () ;
	return $c->get_value ($section, $var) ;
}

function forge_get_config_array () {
	$c = FusionForgeConfig::get_instance () ;

	$ret = array () ;

	foreach (func_get_args() as $item) {
		if (! is_array ($item)) {
			$item = array ($item) ;
		}
		$var = $item[0] ;
		if (isset ($item[1])) {
			$section = $item[1] ;
		} else {
			$section = 'core' ;
		}
		$ret[] = $c->get_value ($section, $var) ;
	}

	return $ret ;
}

function forge_set_vars_from_config () {
	$c = FusionForgeConfig::get_instance () ;

	foreach (func_get_args() as $item) {
		if (is_array ($item)) {
			$var = $item[0] ;
			$x = $var ;
			if (isset ($item[1])) {
				$section = $item[1] ;
				$x = $section.'__'.$var ;
				$value = forge_get_config ($var, $section) ;
			}
		} else {
			$var = $item ;
			$x = $item ;
			$value = forge_get_config ($var) ;
		}

		global $$x ;
		$$x = $value ;
	}
}

/**
 * Define a new configuration item with given name/section and default value
 * @param string $var name
 * @param string $section
 * @param any $default
 */
function forge_define_config_item ($var, $section, $default) {
	$c = FusionForgeConfig::get_instance () ;
	$c->set_value ($section, $var, $default) ;
}

/**
 * Tag the variable as boolean, which allows human-readable values in the configuration files (such as yes, true, on and 1; anything else is mapped to false)
 * @param string $var name
 * @param string $section
 */
function forge_set_config_item_bool ($var, $section) {
	$c = FusionForgeConfig::get_instance () ;
	$c->mark_as_bool ($section, $var) ;
}

/**
 * Define a new boolean configuration item with given name/section and default value
 * @param string $var name
 * @param string $section
 * @param any $default
 */
function forge_define_config_item_bool ($var, $section, $default) {
	forge_define_config_item ($var, $section, $default);
	forge_set_config_item_bool ($var, $section);
}

/**
 * Read a *.ini file and inject its contents into the configuration database
 * @param string $filename
 */
function forge_read_config_file ($filename) {
	$c = FusionForgeConfig::get_instance () ;
	$c->read_config_file ($filename) ;
}

/**
 * Read all configuration files in a directory
 * @param string $path
 */
function forge_read_config_dir ($path) {
	$c = FusionForgeConfig::get_instance () ;

	$files = array () ;

	if (is_dir($path)){
		if ($handle = opendir($path)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "."
			    	&& $file != ".."
			    	// Avoid .bak, .old, .dpkg-old and so on, but keep .ini
			    	&& preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
					$files[] = "$path/$file" ;
				}
			}
			closedir($handle);
		}
	}

	natsort ($files) ;
	foreach ($files as $file) {
		$c->read_config_file ($file) ;
	}
}

function forge_reset_config_item ($var, $section, $default) {
	$c = FusionForgeConfig::get_instance () ;
	$c->reset_value ($section, $var, $default) ;
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: