Passed
Push — 2.x ( c5a5a3...39da0b )
by Terry
02:01
created

DriverProvider::assertInvalidDataTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
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\Driver;
24
25
use Shieldon\Firewall\Driver\AbstractDriver;
26
27
/**
28
 * Driver Provider.
29
 */
30
class DriverProvider extends AbstractDriver
31
{
32
    /**
33
     * Data table for regular filter logs.
34
     *
35
     * @var string
36
     */
37
    protected $tableFilterLogs = 'shieldon_filter_logs';
38
39
    /**
40
     * Data table name for whitelist.
41
     *
42
     * @var string
43
     */
44
    protected $tableRuleList = 'shieldon_rule_list';
45
46
    /**
47
     * Data table for recording online session count.
48
     *
49
     * @var string
50
     */
51
    protected $tableSessions = 'shieldon_sessions';
52
53
    /**
54
     * The prefix of the database tables, or the name of file directory.
55
     *
56
     * @var string
57
     */
58
    protected $channel = '';
59
60
    /**
61
     * Check if is initialized or not.
62
     *
63
     * @var bool
64
     */
65
    protected $isInitialized = false;
66
67
    /**
68
     * The table types.
69
     *
70
     * @var array
71
     */
72
    protected $tableTypes = [
73
        'rule',
74
        'filter',
75
        'session',
76
    ];
77
78
    /**
79
     * Set data channel.
80
     *
81
     * @param string $channel The prefix of the data tables.
82
     *
83
     * @return void
84
     */
85
    public function setChannel(string $channel): void
86
    {
87
        $this->channel = $channel;
88
89
        if (!empty($this->channel)) {
90
            $this->tableFilterLogs = $this->channel . '_shieldon_filter_logs';
91
            $this->tableRuleList = $this->channel . '_shieldon_rule_list';
92
            $this->tableSessions = $this->channel . '_shieldon_sessions';
93
        }
94
    }
95
96
    /**
97
     * Get channel name.
98
     *
99
     * @return string
100
     */
101
    public function getChannel(): string
102
    {
103
        return $this->channel;
104
    }
105
106
    /**
107
     * Return parsed full data structure.
108
     *
109
     * @param array  $data The data needed to be parsed.
110
     * @param string $type The type of data table. accepts: filter | session | rule
111
     *
112
     * @return array
113
     */
114
    public function parseData(array $data, string $type = 'filter'): array
115
    {
116
        $parsedData = [];
117
118
        switch ($type) {
119
            // Rule table data structure.
120
            case 'rule':
121
                break;
122
123
            // Session table data structure.
124
            case 'session':
125
                break;
126
127
            // Log table data structure.
128
            case 'filter':
129
                // no break
130
            default:
131
132
                $fields = [
133
134
                    // Basic IP data.
135
                    'ip'       => 'string', 
136
                    'session'  => 'string', 
137
                    'hostname' => 'string', 
138
139
                    // timesamp while visting first time.
140
                    'first_time_s'    => 'int',
141
                    'first_time_m'    => 'int',
142
                    'first_time_h'    => 'int',
143
                    'first_time_d'    => 'int',
144
                    'first_time_flag' => 'int',
145
                    'last_time'       => 'int',
146
147
                    // Signals for flagged bad behavior.
148
                    'flag_js_cookie'     => 'int',
149
                    'flag_multi_session' => 'int',
150
                    'flag_empty_referer' => 'int',
151
152
                    // Pageview count.
153
                    'pageviews_cookie' => 'int',
154
                    'pageviews_s'      => 'int',
155
                    'pageviews_m'      => 'int',
156
                    'pageviews_h'      => 'int',
157
                    'pageviews_d'      => 'int',
158
                ];
159
160
                foreach ($fields as $k => $v) {
161
                    $tmp = $data[$k] ?? '';
162
163
                    if ('string' === $v) {
164
                        $parsedData[$k] = (string) $tmp;
165
                    }
166
167
                    if ('int' === $v) {
168
                        $parsedData[$k] = (int) $tmp;
169
                    }
170
                }
171
                break;
172
            // end switch
173
        }
174
175
        return $parsedData;
176
    }
177
178
    // @codeCoverageIgnoreStart
179
180
    /**
181
     * Implement fetch.
182
     *
183
     * @param string $ip   The data id of the entry to fetch.
184
     * @param string $type The type of data table. accepts: filter | session | rule
185
     *
186
     * @return array The data or an empty array.
187
     */
188
    protected function doFetch(string $ip, string $type = 'filter'): array
189
    {
190
        return [];
191
    }
192
193
    /**
194
     * Implement fetch all.
195
     *
196
     * @param string $type The type of data table. accepts: filter | session | rule
197
     *
198
     * @return array The data or an empty array.
199
     */
200
    protected function doFetchAll(string $type = 'filter'): array
201
    {
202
        return [];
203
    }
204
205
    /**
206
     * Implement has.
207
     *
208
     * @param string $ip   The data id of the entry to check for.
209
     * @param string $type The type of data table. accepts: filter | session | rule
210
     *
211
     * @return bool
212
     */
213
    protected function checkExist(string $ip, string $type = 'filter'): bool
214
    {
215
        return false;
216
    }
217
218
    /**
219
     * Implement save.
220
     *
221
     * @param string $ip     The IP address as the data id.
222
     * @param array  $data   The data.
223
     * @param string $type   The type of the data table.
224
     * @param int    $expire The data will be deleted after expiring.
225
     *
226
     * @return bool
227
     */
228
    protected function doSave(string $ip, array $data, string $type = 'filter', $expire = 0): bool 
229
    {
230
        return false;
231
    }
232
233
    /**
234
     * Implement delete.
235
     *
236
     * @param string $ip   The IP address.
237
     * @param string $type The type of data table. accepts: filter | session | rule
238
     *
239
     * @return bool
240
     */
241
    protected function doDelete(string $ip, string $type = 'filter'): bool
242
    {
243
        return false;
244
    }
245
246
    /**
247
     * Rebuild data tables.
248
     *
249
     * @return bool
250
     */
251
    protected function doRebuild(): bool
252
    {
253
        return false;
254
    }
255
256
    /**
257
     * Initialize data tables.
258
     *
259
     * @param bool $dbCheck This is for creating data tables automatically
260
     *                      Turn it off, if you don't want to check data tables every pageview.
261
     *
262
     * @return void
263
     */
264
    protected function doInitialize(bool $dbCheck = true): void
265
    {
266
267
    }
268
269
    /**
270
     * Check data type.
271
     *
272
     * @param string $type The type of the data tables.
273
     *
274
     * @return void
275
     */
276
    protected function assertInvalidDataTable(string $type): void
277
    {
278
        if (!in_array($type, $this->tableTypes)) {
279
            throw new RuntimeException(
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Driver\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
280
                'Invalid data type of the data tables.'
281
            );
282
        }
283
    }
284
285
    // @codeCoverageIgnoreEnd
286
}
287