1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Firewall; |
14
|
|
|
|
15
|
|
|
use function count; |
16
|
|
|
use function explode; |
17
|
|
|
|
18
|
|
|
/* |
19
|
|
|
* FirewallTrait |
20
|
|
|
*/ |
21
|
|
|
trait FirewallTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Shieldon instance. |
25
|
|
|
* |
26
|
|
|
* @var object |
27
|
|
|
*/ |
28
|
|
|
protected $kernel; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Configuration data of Shieldon. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $configuration; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* If status is false and then Sheldon will stop working. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $status = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The configuation file's path. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $directory = '/tmp'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The filename of the configuration file. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $filename = 'config.firewall.json'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the Shieldon instance. |
60
|
|
|
* |
61
|
|
|
* @return object |
62
|
|
|
*/ |
63
|
|
|
public function getKernel() |
64
|
|
|
{ |
65
|
|
|
return $this->kernel; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the configuation settings. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getConfiguration(): array |
74
|
|
|
{ |
75
|
|
|
return $this->configuration; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the directory where the data stores. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getDirectory(): string |
84
|
|
|
{ |
85
|
|
|
return $this->directory; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the filename where the configuration saves. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getFileName(): string |
94
|
|
|
{ |
95
|
|
|
return $this->filename; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get a variable from configuration. |
100
|
|
|
* |
101
|
|
|
* @param string $field |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function getConfig(string $field) |
106
|
|
|
{ |
107
|
|
|
$v = explode('.', $field); |
108
|
|
|
$c = count($v); |
109
|
|
|
|
110
|
|
|
switch ($c) { |
111
|
|
|
case 1: |
112
|
|
|
return $this->configuration[$v[0]] ?? ''; |
113
|
|
|
break; |
|
|
|
|
114
|
|
|
case 2: |
115
|
|
|
return $this->configuration[$v[0]][$v[1]] ?? ''; |
116
|
|
|
break; |
117
|
|
|
|
118
|
|
|
case 3: |
119
|
|
|
return $this->configuration[$v[0]][$v[1]][$v[2]] ?? ''; |
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
case 4: |
123
|
|
|
return $this->configuration[$v[0]][$v[1]][$v[2]][$v[3]] ?? ''; |
124
|
|
|
break; |
125
|
|
|
|
126
|
|
|
case 5: |
127
|
|
|
return $this->configuration[$v[0]][$v[1]][$v[2]][$v[3]][$v[4]] ?? ''; |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
return ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set a variable to the configuration. |
135
|
|
|
* |
136
|
|
|
* @param string $field |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function setConfig(string $field, $value) |
141
|
|
|
{ |
142
|
|
|
$v = explode('.', $field); |
143
|
|
|
$c = count($v); |
144
|
|
|
|
145
|
|
|
switch ($c) { |
146
|
|
|
case 1: |
147
|
|
|
$this->configuration[$v[0]] = $value; |
148
|
|
|
break; |
149
|
|
|
|
150
|
|
|
case 2: |
151
|
|
|
$this->configuration[$v[0]][$v[1]] = $value; |
152
|
|
|
break; |
153
|
|
|
|
154
|
|
|
case 3: |
155
|
|
|
$this->configuration[$v[0]][$v[1]][$v[2]] = $value; |
156
|
|
|
break; |
157
|
|
|
|
158
|
|
|
case 4: |
159
|
|
|
$this->configuration[$v[0]][$v[1]][$v[2]][$v[3]] = $value; |
160
|
|
|
break; |
161
|
|
|
|
162
|
|
|
case 5: |
163
|
|
|
$this->configuration[$v[0]][$v[1]][$v[2]][$v[3]][$v[4]] = $value; |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get options from the configuration file. |
170
|
|
|
* |
171
|
|
|
* This method is same as `$this->getConfig()` but returning value from array directly, |
172
|
|
|
* saving a `explode()` process. |
173
|
|
|
* |
174
|
|
|
* @param string $option |
175
|
|
|
* @param string $section |
176
|
|
|
* |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
protected function getOption(string $option, string $section = '') |
180
|
|
|
{ |
181
|
|
|
if (!empty($this->configuration[$section][$option])) { |
182
|
|
|
return $this->configuration[$section][$option]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (!empty($this->configuration[$option]) && $section === '') { |
186
|
|
|
return $this->configuration[$option]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Update configuration file. |
194
|
|
|
* |
195
|
|
|
* @return void |
196
|
|
|
*/ |
197
|
|
|
protected function updateConfig() |
198
|
|
|
{ |
199
|
|
|
$configFilePath = $this->directory . '/' . $this->filename; |
200
|
|
|
|
201
|
|
|
if (!file_exists($configFilePath)) { |
202
|
|
|
if (!is_dir($this->directory)) { |
203
|
|
|
// @codeCoverageIgnoreStart |
204
|
|
|
$originalUmask = umask(0); |
205
|
|
|
@mkdir($this->directory, 0777, true); |
|
|
|
|
206
|
|
|
umask($originalUmask); |
207
|
|
|
// @codeCoverageIgnoreEnd |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
file_put_contents($configFilePath, json_encode($this->configuration)); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.