1
|
|
|
<?php |
2
|
|
|
require_once(dirname(__FILE__).'/../require/settings.php'); |
3
|
|
|
require_once(dirname(__FILE__).'/../require/class.Common.php'); |
4
|
|
|
|
5
|
|
|
class settings { |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
* This function is used to modify a setting in settings.php file |
9
|
|
|
* @param Array list of settings and their values |
10
|
|
|
*/ |
11
|
|
|
public static function modify_settings($settings) { |
12
|
|
|
$Common = new Common(); |
13
|
|
|
$settings_filename = '../require/settings.php'; |
14
|
|
|
$content = file_get_contents($settings_filename); |
15
|
|
|
$fh = fopen($settings_filename,'w'); |
16
|
|
|
foreach ($settings as $settingname => $value) { |
17
|
|
|
if ($value == 'TRUE' || $value == 'FALSE') { |
18
|
|
|
$pattern = '/\R\$'.$settingname." = ".'(TRUE|FALSE)'."/"; |
19
|
|
|
$replace = "\n".'\$'.$settingname." = ".$value.""; |
20
|
|
|
} elseif (is_array($value)) { |
21
|
|
|
$pattern = '/\R\$'.$settingname." = array\(".'(.*)'."\)/"; |
22
|
|
|
if ($Common->isAssoc($value)) { |
23
|
|
|
foreach ($value as $key => $data) { |
24
|
|
|
if (!isset($array_value)) { |
25
|
|
|
if (is_array($data)) { |
26
|
|
|
foreach ($data as $keya => $dataa) { |
27
|
|
|
if (is_array($dataa) && !empty($dataa)) { |
28
|
|
|
foreach ($dataa as $dataaa) { |
29
|
|
|
if (!isset($dataarraya)) $dataarraya = $dataaa; |
30
|
|
|
else $dataarraya .= "','".$dataaa; |
31
|
|
|
} |
32
|
|
|
$dataarray = "array('".$keya."' => array('".$dataarraya."'))"; |
|
|
|
|
33
|
|
|
unset($dataarraya); |
34
|
|
|
} else { |
35
|
|
|
if (!isset($dataarray)) $dataarray = "'".$dataa."'"; |
36
|
|
|
else $dataarray .= ",'".$dataa."'"; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
$array_value = "'".$key."' => ".$dataarray; |
|
|
|
|
40
|
|
|
unset($dataarray); |
41
|
|
|
} else { |
42
|
|
|
if ($data == 'TRUE' || $data == 'FALSE') { |
43
|
|
|
$array_value = "'".$key."' => ".$data.""; |
44
|
|
|
} else { |
45
|
|
|
$array_value = "'".$key."' => '".$data."'"; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
if (is_array($data)) { |
50
|
|
|
foreach ($data as $keya => $dataa) { |
51
|
|
|
if (is_array($dataa) && !empty($dataa)) { |
52
|
|
|
foreach ($dataa as $dataaa) { |
53
|
|
|
if (!isset($dataarraya)) $dataarraya = $dataaa; |
54
|
|
|
else $dataarraya .= "','".$dataaa; |
55
|
|
|
} |
56
|
|
|
$dataarray = "array('".$keya."' => array('".$dataarraya."'))"; |
57
|
|
|
unset($dataarraya); |
58
|
|
|
} else { |
59
|
|
|
if (!isset($dataarray)) $dataarray = "'".$dataa."'"; |
60
|
|
|
else $dataarray .= "','".$dataa."'"; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
$array_value .= ",'".$key."' => ".$dataarray; |
64
|
|
|
unset($dataarray); |
65
|
|
|
} else { |
66
|
|
|
if ($data == 'TRUE' || $data == 'FALSE') { |
67
|
|
|
$array_value .= ",'".$key."' => ".$data.""; |
68
|
|
|
} else { |
69
|
|
|
$array_value .= ",'".$key."' => '".$data."'"; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
foreach ($value as $key => $data) { |
76
|
|
|
if (is_array($data) && $Common->isAssoc($data)) { |
77
|
|
|
foreach ($data as $keyd => $datad) { |
78
|
|
|
if (!isset($arrayd_value)) { |
79
|
|
|
if ($datad == 'TRUE' || $datad == 'FALSE') { |
80
|
|
|
$arrayd_value = "'".$keyd."' => ".$datad.""; |
81
|
|
|
} else { |
82
|
|
|
$arrayd_value = "'".$keyd."' => '".$datad."'"; |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
if ($datad == 'TRUE' || $datad == 'FALSE') { |
86
|
|
|
$arrayd_value .= ",'".$keyd."' => ".$datad.""; |
87
|
|
|
} else { |
88
|
|
|
$arrayd_value .= ",'".$keyd."' => '".$datad."'"; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
if (!isset($array_value)) { |
93
|
|
|
if (!isset($arrayd_value)) $arrayd_value = ''; |
94
|
|
|
//$array_value = "'".$key."' => array(".$arrayd_value.")"; |
95
|
|
|
$array_value = "array(".$arrayd_value.")"; |
96
|
|
|
} elseif (isset($arrayd_value)) { |
97
|
|
|
//$array_value .= ",'".$key."' => array(".$arrayd_value.")"; |
98
|
|
|
$array_value .= ",array(".$arrayd_value.")"; |
99
|
|
|
} |
100
|
|
|
unset($arrayd_value); |
101
|
|
|
} else { |
102
|
|
|
if (!isset($array_value)) { |
103
|
|
|
$array_value = "'".$data."'"; |
104
|
|
|
} else { |
105
|
|
|
$array_value .= ",'".$data."'"; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
if (!isset($array_value)) $array_value = ''; |
111
|
|
|
$replace = "\n".'\$'.$settingname." = array(".$array_value.")"; |
112
|
|
|
unset($array_value); |
113
|
|
|
} else { |
114
|
|
|
$pattern = '/\R\$'.$settingname." = '".'(.*)'."'/"; |
115
|
|
|
$replace = "\n".'\$'.$settingname." = '".$value."'"; |
116
|
|
|
} |
117
|
|
|
$rep_cnt = 0; |
118
|
|
|
$content = preg_replace($pattern,$replace,$content,1,$rep_cnt); |
119
|
|
|
|
120
|
|
|
/// If setting was a string and is now an array |
121
|
|
|
if ($rep_cnt === 0 && is_array($value)) { |
122
|
|
|
$pattern = '/\R\$'.$settingname." = '".'(.*)'."'/"; |
123
|
|
|
$content = preg_replace($pattern,$replace,$content,1,$rep_cnt); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// If setting is not in settings.php (for update) |
127
|
|
|
if ($rep_cnt === 0) { |
128
|
|
|
$content = preg_replace('/\?>/',$replace.";\n?>",$content,1,$rep_cnt); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
fwrite($fh,$content); |
133
|
|
|
fclose($fh); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/* |
137
|
|
|
* This function is used to comment a setting in settings.php file |
138
|
|
|
* @param Array list of settings to comment |
139
|
|
|
*/ |
140
|
|
|
public static function comment_settings($settings) { |
141
|
|
|
$Common = new Common(); |
|
|
|
|
142
|
|
|
$settings_filename = '../require/settings.php'; |
143
|
|
|
$content = file_get_contents($settings_filename); |
144
|
|
|
$fh = fopen($settings_filename,'w'); |
145
|
|
|
foreach ($settings as $settingname) { |
146
|
|
|
$pattern = '/\R\$'.$settingname." = /"; |
147
|
|
|
$replace = '//$'.$settingname." = "; |
148
|
|
|
$content = preg_replace($pattern,$replace,$content); |
149
|
|
|
} |
150
|
|
|
fwrite($fh,$content); |
151
|
|
|
fclose($fh); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
//settings::comment_settings(array('globalSBS1Hosts')); |
156
|
|
|
?> |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: