3 * FusionForge configuration functions
5 * Copyright 2009, Roland Mas
6 * Copyright 2013, Franck Villaume - TrivialDev
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 // See docs in http://fusionforge.org/plugins/mediawiki/wiki/fusionforge/index.php/Configuration
28 * Singleton FusionForge configuration database manager TODO : Enter better description here ...
31 class FusionForgeConfig {
32 static protected $instance = NULL ;
34 private $bools = array () ;
37 * Singleton accessor to the configuration database
38 * @return FusionForgeConfig instance
40 static public function get_instance () {
41 if (self::$instance == NULL) {
42 self::$instance = new FusionForgeConfig () ;
44 return self::$instance ;
47 // TODO: add a constructor that initializes self::$instance to self ?
49 public function get_sections () {
50 return array_keys ($this->settings) ;
53 public function get_variables ($section='core') {
54 if (isset ($this->settings[$section])) {
55 return array_keys ($this->settings[$section]) ;
60 public function get_value ($section, $var) {
61 if (!isset ($this->settings[$section])
62 || !isset ($this->settings[$section][$var])) {
66 $tmp = $this->settings[$section][$var] ;
67 preg_match_all ('/\$[a-z_]+\/[a-z_]+/', $tmp, $matches) ;
69 foreach ($matches[0] as $m) {
70 $c = explode ('/', substr($m,1)) ;
72 if (isset ($this->settings[$c[0]][$c[1]])) {
73 $tmp = str_replace ($m, $this->get_value($c[0],$c[1]), $tmp) ;
76 if ($this->is_bool ($section, $var)) {
77 $tmp = $this->_interpret_as_bool ($tmp) ;
82 public function get_raw_value ($section, $var) {
83 if (!isset ($this->settings[$section])
84 || !isset ($this->settings[$section][$var])) {
87 return $this->settings[$section][$var] ;
90 public function set_value ($section, $var, $value) {
91 if (!isset ($this->settings[$section])) {
92 $this->settings[$section] = array () ;
95 if (!isset ($this->settings[$section][$var])) {
96 $this->settings[$section][$var] = $value ;
100 public function reset_value ($section, $var, $value) {
101 if (!isset ($this->settings[$section])) {
102 $this->settings[$section] = array () ;
105 $this->settings[$section][$var] = $value ;
108 function read_config_file($filename) {
109 if (getenv('FUSIONFORGE_USE_PRE_51_CONFIG') == 'true') {
110 $fallback_only = true;
112 $fallback_only = false;
115 if (file_exists($filename) && is_readable($filename)) {
116 $sections = parse_ini_file($filename, true);
117 if (is_array($sections)) {
118 foreach ($sections as $section => $options) {
119 if (is_array($options)) {
120 foreach ($options as $var => $value) {
121 if ($fallback_only) {
122 $this->set_value($section, $var, $value);
124 $this->reset_value($section, $var, $value);
134 function mark_as_bool ($section, $var) {
135 if (!array_key_exists ($section, $this->bools)) {
136 $this->bools[$section] = array () ;
138 $this->bools[$section][$var] = true ;
141 function is_bool ($section, $var) {
142 if (isset ($this->bools[$section])
143 && isset ($this->bools[$section][$var])) {
144 return $this->bools[$section][$var] ;
149 private function _interpret_as_bool ($val) {
150 $val = strtolower ($val) ;
164 if (!isset ($fusionforge_config)) {
165 $fusionforge_config = new FusionForgeConfig();
169 * Get value of variable "name" in section "section"
171 * @param string $section defaults to "core" if missing
172 * @return mixed <NULL, boolean>
174 function forge_get_config ($var, $section = 'core') {
175 $c = FusionForgeConfig::get_instance () ;
176 return $c->get_value ($section, $var) ;
179 function forge_get_config_array () {
180 $c = FusionForgeConfig::get_instance () ;
184 foreach (func_get_args() as $item) {
185 if (! is_array ($item)) {
186 $item = array ($item) ;
189 if (isset ($item[1])) {
190 $section = $item[1] ;
194 $ret[] = $c->get_value ($section, $var) ;
200 function forge_set_vars_from_config () {
201 $c = FusionForgeConfig::get_instance () ;
203 foreach (func_get_args() as $item) {
204 if (is_array ($item)) {
207 if (isset ($item[1])) {
208 $section = $item[1] ;
209 $x = $section.'__'.$var ;
210 $value = forge_get_config ($var, $section) ;
215 $value = forge_get_config ($var) ;
225 * Define a new configuration item with given name/section and default value
226 * @param string $var name
227 * @param string $section
228 * @param any $default
230 function forge_define_config_item ($var, $section, $default) {
231 $c = FusionForgeConfig::get_instance () ;
233 return $c->set_value ($section, $var, $default) ;
237 * 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)
238 * @param string $var name
239 * @param string $section
241 function forge_set_config_item_bool ($var, $section) {
242 $c = FusionForgeConfig::get_instance () ;
244 return $c->mark_as_bool ($section, $var) ;
248 * Read a *.ini file and inject its contents into the configuration database
249 * @param string $file
251 function forge_read_config_file ($filename) {
252 $c = FusionForgeConfig::get_instance () ;
254 return $c->read_config_file ($filename) ;
258 * Read all configuration files in a directory
259 * @param string $path
261 function forge_read_config_dir ($path) {
262 $c = FusionForgeConfig::get_instance () ;
267 if ($handle = opendir($path)) {
268 while (false !== ($file = readdir($handle))) {
271 // Avoid .bak, .old, .dpkg-old and so on, but keep .ini
272 && preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
273 $files[] = "$path/$file" ;
281 foreach ($files as $file) {
282 $c->read_config_file ($file) ;
286 function forge_reset_config_item ($var, $section, $default) {
287 $c = FusionForgeConfig::get_instance () ;
289 return $c->reset_value ($section, $var, $default) ;
294 // c-file-style: "bsd"