-
Notifications
You must be signed in to change notification settings - Fork 0
/
fz_config.php
104 lines (89 loc) · 2.55 KB
/
fz_config.php
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
<?php
/**
* @file
* Short description.
*
* Long description.
*
* @package FileZ
*/
/**
* Load config/filez.ini in "option ('fz_config')".
* @return boolean Whether if filez.ini was found or not
*/
function fz_config_load ($config_dir) {
$result = true;
$config = parse_ini_file ($config_dir.'filez.ini', true);
if (empty ($config)) {
$config = array();
$result = false;
trigger_error ('Missing or malformed config file.', E_USER_WARNING);
}
$default = parse_ini_file ($config_dir.'filez.default.ini', true);
if (empty ($default)) {
trigger_error ('Missing file "filez.default.ini".', E_USER_ERROR);
}
option ('fz_config', merge_config ($config, $default));
return $result;
}
/**
* Merge 2 configuration array. $user configuration will overide $default
* configuration.
*
* @param array $user
* @param array $default
* @return array
*/
function merge_config ($user, $default) {
$result = array ();
foreach ($default as $section => $values) {
$result [$section] = (array_key_exists ($section, $user) ?
array_merge ($default[$section], $user[$section]) : $default[$section]);
}
return $result + array_diff_key ($user, $default);
}
/**
* Return a config var
*/
function fz_config_get ($section = null, $var = null, $default = null) {
$conf = option ('fz_config');
if (! is_array ($conf))
return $default;
if ($section === null) {
return $conf;
}
if (array_key_exists ($section, $conf)) {
if ($var === null)
return $conf [$section];
if (array_key_exists ($var, $conf[$section]))
return $conf [$section][$var];
}
return $default;
}
/**
* Write config to the config/filez.ini file.
* (Used in installer).
*/
function fz_config_save ($config, $file) {
return file_put_contents ($file, fz_serialize_ini_array($config, true));
}
function fz_serialize_ini_array ($assoc_arr, $has_sections = false) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $section => $values) {
$content .= "\n[$section]\n".fz_serialize_ini_array($values);
}
}
else {
foreach ($assoc_arr as $key => $value) {
if (is_array ($value))
foreach ($value as $v)
$content .= "{$key}[] = \"$v\"\n";
elseif (is_numeric ($value))
$content .= "$key = $value\n";
else if($value != "")
$content .= "$key = \"$value\"\n";
}
}
return $content;
}