1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall;
24

25
use function count;
26
use function date;
27
use function explode;
28
use function file_exists;
29
use function file_put_contents;
30
use function is_dir;
31
use function json_encode;
32
use function mkdir;
33
use function umask;
34
use const JSON_PRETTY_PRINT;
35

36
/*
37
 * FirewallTrait
38
 */
39
trait FirewallTrait
40
{
41
    /**
42
     *   Public methods       | Desctiotion
43
     *  ----------------------|---------------------------------------------
44
     *   getKernel            | Get the Shieldon Kernel instance.
45
     *   getConfiguration     | Get the configuration data.
46
     *   getDirectory         | Get the dictionary where the data is stored.
47
     *   getFileName          | Get the path of the configuration file.
48
     *   getConfig            | Get the value by identification string.
49
     *   setConfig            | Set the value by identification string.
50
     *  ----------------------|---------------------------------------------
51
     */
52

53
    /**
54
     * Shieldon instance.
55
     *
56
     * @var object
57
     */
58
    protected $kernel;
59

60
    /**
61
     * Configuration data of Shieldon.
62
     *
63
     * @var array
64
     */
65
    protected $configuration = [];
66

67
    /**
68
     * The configuation file's path.
69
     *
70
     * @var string
71
     */
72
    protected $directory = '/tmp';
73

74
    /**
75
     * The filename of the configuration file.
76
     *
77
     * @var string
78
     */
79
    protected $filename = 'config.firewall.json';
80

81
    /**
82
     * A file that confirms the required dictories or database tables 
83
     * have been created.
84
     *
85
     * @var string
86
     */
87
    protected $checkpoint = 'driver_initialized.txt';
88
    
89
    /**
90
     * The prefix of the database tables, or the name of file directory.
91
     *
92
     * @var string
93
     */
94
    protected $channel = '';
95

96
    /**
97
     * Version number.
98
     *
99
     * @var string
100
     */
101
    protected $version = '2.0.1';
102

103
    /**
104
     * Get the Shieldon instance.
105
     *
106
     * @return object
107
     */
108 3
    public function getKernel()
109
    {
110 3
        return $this->kernel;
111
    }
112

113
    /**
114
     * Get the configuation settings.
115
     *
116
     * @return array
117
     */
118 3
    public function getConfiguration(): array
119
    {
120 3
        return $this->configuration;
121
    }
122

123
    /**
124
     * Get the directory where the data stores.
125
     *
126
     * @return string
127
     */
128 3
    public function getDirectory(): string
129
    {
130 3
        return $this->directory;
131
    }
132

133
    /**
134
     * Get the filename where the configuration saves.
135
     *
136
     * @return string
137
     */
138 3
    public function getFileName(): string
139
    {
140 3
        return $this->filename;
141
    }
142

143
    /**
144
     * Get a variable from configuration.
145
     *
146
     * @param string $field The field of the configuration.
147
     *
148
     * @return mixed
149
     */
150 3
    public function getConfig(string $field)
151
    {
152 3
        $v = explode('.', $field);
153 3
        $c = count($v);
154

155 2
        switch ($c) {
156 3
            case 1:
157 3
                return $this->configuration[$v[0]] ?? '';
158

159 3
            case 2:
160 3
                return $this->configuration[$v[0]][$v[1]] ?? '';
161

162 3
            case 3:
163 3
                return $this->configuration[$v[0]][$v[1]][$v[2]] ?? '';
164

165 3
            case 4:
166 3
                return $this->configuration[$v[0]][$v[1]][$v[2]][$v[3]] ?? '';
167

168 3
            case 5:
169 3
                return $this->configuration[$v[0]][$v[1]][$v[2]][$v[3]][$v[4]] ?? '';
170
        }
171 3
        return '';
172
    }
173

174
    /**
175
     * Set a variable to the configuration.
176
     *
177
     * @param string $field The field of the configuration.
178
     * @param mixed  $value The vale of a field in the configuration.
179
     *
180
     * @return void
181
     */
182 3
    public function setConfig(string $field, $value): void
183
    {
184 3
        $v = explode('.', $field);
185 3
        $c = count($v);
186

187 2
        switch ($c) {
188 3
            case 1:
189 3
                $this->configuration[$v[0]] = $value;
190 3
                break;
191

192 3
            case 2:
193 3
                $this->configuration[$v[0]][$v[1]] = $value;
194 3
                break;
195

196 3
            case 3:
197 3
                $this->configuration[$v[0]][$v[1]][$v[2]] = $value;
198 3
                break;
199

200 3
            case 4:
201 3
                $this->configuration[$v[0]][$v[1]][$v[2]][$v[3]] = $value;
202 3
                break;
203

204 3
            case 5:
205 3
                $this->configuration[$v[0]][$v[1]][$v[2]][$v[3]][$v[4]] = $value;
206 3
                break;
207
        }
208
    }
209

210
    /**
211
     * Get options from the configuration file.
212
     * This method is same as `$this->getConfig()` but returning value from array directly.
213
     *
214
     * @param string $option  The option of the section in the the configuration.
215
     * @param string $section The section in the configuration.
216
     *
217
     * @return mixed
218
     */
219 3
    protected function getOption(string $option, string $section = '')
220
    {
221 3
        if (!empty($this->configuration[$section][$option])) {
222 3
            return $this->configuration[$section][$option];
223
        }
224

225 3
        if (!empty($this->configuration[$option]) && $section === '') {
226 3
            return $this->configuration[$option];
227
        }
228

229 3
        return false;
230
    }
231

232
    /**
233
     * Update configuration file.
234
     *
235
     * @return void
236
     */
237 3
    protected function updateConfig(): void
238
    {
239 3
        $configFilePath = $this->directory . '/' . $this->filename;
240

241 3
        if (!file_exists($configFilePath)) {
242 3
            if (!is_dir($this->directory)) {
243

244
                // @codeCoverageIgnoreStart
245
        
246
                $originalUmask = umask(0);
247
                mkdir($this->directory, 0777, true);
248
                umask($originalUmask);
249

250
                // @codeCoverageIgnoreEnd
251
            }
252
        }
253

254 3
        file_put_contents(
255 3
            $configFilePath,
256 3
            json_encode($this->configuration, JSON_PRETTY_PRINT)
257
        );
258
    }
259

260
    /**
261
     * Get the filename of the checkpoint file.
262
     *
263
     * @return string
264
     */
265 3
    protected function getCheckpoint(): string
266
    {
267 3
        $driverType = (string) $this->getOption('driver_type');
268

269 3
        $channel = '';
270

271 3
        if (!empty($this->channel)) {
272 3
            $channel = '_' . $this->channel;
273
        }
274

275 3
        return $this->directory . '/' . $this->version . $channel .  '_' . $driverType . '_' . $this->checkpoint;
276
    }
277

278
    /**
279
     * Are database tables created? 
280
     *
281
     * @return bool
282
     */
283 3
    protected function hasCheckpoint(): bool
284
    {
285 3
        if (file_exists($this->getCheckpoint())) {
286 3
            return true;
287
        }
288

289 3
        return false;
290
    }
291

292
    /**
293
     * Are database tables created?
294
     * 
295
     * @param bool $create Is create the checkpoint file, or not.
296
     *
297
     * @return void
298
     */
299 3
    protected function setCheckpoint(bool $create = true): void
300
    {
301 3
        if ($create) {
302 3
            file_put_contents($this->getCheckpoint(), date('Y-m-d H:i:s'));
303
        }
304
    }
305
}

Read our documentation on viewing source code .

Loading