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\Driver;
24

25
use Shieldon\Firewall\Driver\AbstractDriver;
26
use RuntimeException;
27

28
/**
29
 * Driver Provider.
30
 */
31
class DriverProvider extends AbstractDriver
32
{
33
    /**
34
     * Data table for regular filter logs.
35
     *
36
     * @var string
37
     */
38
    protected $tableFilterLogs = 'shieldon_filter_logs';
39

40
    /**
41
     * Data table name for whitelist.
42
     *
43
     * @var string
44
     */
45
    protected $tableRuleList = 'shieldon_rule_list';
46

47
    /**
48
     * Data table for recording online session count.
49
     *
50
     * @var string
51
     */
52
    protected $tableSessions = 'shieldon_sessions';
53

54
    /**
55
     * The prefix of the database tables, or the name of file directory.
56
     *
57
     * @var string
58
     */
59
    protected $channel = '';
60

61
    /**
62
     * Check if is initialized or not.
63
     *
64
     * @var bool
65
     */
66
    protected $isInitialized = false;
67

68
    /**
69
     * The table types.
70
     *
71
     * @var array
72
     */
73
    protected $tableTypes = [
74
        'rule',
75
        'filter',
76
        'session',
77
    ];
78

79
    /**
80
     * Set data channel.
81
     *
82
     * @param string $channel The prefix of the data tables.
83
     *
84
     * @return void
85
     */
86 3
    public function setChannel(string $channel): void
87
    {
88 3
        $this->channel = $channel;
89

90 3
        if (!empty($this->channel)) {
91 3
            $this->tableFilterLogs = $this->channel . '_shieldon_filter_logs';
92 3
            $this->tableRuleList = $this->channel . '_shieldon_rule_list';
93 3
            $this->tableSessions = $this->channel . '_shieldon_sessions';
94
        }
95
    }
96

97
    /**
98
     * Get channel name.
99
     *
100
     * @return string
101
     */
102 3
    public function getChannel(): string
103
    {
104 3
        return $this->channel;
105
    }
106

107
    /**
108
     * Return parsed full data structure.
109
     *
110
     * @param array  $data The data needed to be parsed.
111
     * @param string $type The type of data table. accepts: filter | session | rule
112
     *
113
     * @return array
114
     */
115 3
    public function parseData(array $data, string $type = 'filter'): array
116
    {
117 3
        $parsedData = [];
118

119 2
        switch ($type) {
120
            // Rule table data structure.
121 3
            case 'rule':
122 3
                break;
123

124
            // Session table data structure.
125 3
            case 'session':
126 3
                break;
127

128
            // Log table data structure.
129 3
            case 'filter':
130
                // no break
131
            default:
132

133
                $fields = [
134

135
                    // Basic IP data.
136 3
                    'ip'       => 'string', 
137
                    'session'  => 'string', 
138
                    'hostname' => 'string', 
139

140
                    // timestamp while visting first time.
141
                    'first_time_s'    => 'int',
142
                    'first_time_m'    => 'int',
143
                    'first_time_h'    => 'int',
144
                    'first_time_d'    => 'int',
145
                    'first_time_flag' => 'int',
146
                    'last_time'       => 'int',
147

148
                    // Signals for flagged bad behavior.
149
                    'flag_js_cookie'     => 'int',
150
                    'flag_multi_session' => 'int',
151
                    'flag_empty_referer' => 'int',
152

153
                    // Pageview count.
154
                    'pageviews_cookie' => 'int',
155
                    'pageviews_s'      => 'int',
156
                    'pageviews_m'      => 'int',
157
                    'pageviews_h'      => 'int',
158
                    'pageviews_d'      => 'int',
159
                ];
160

161 3
                foreach ($fields as $k => $v) {
162 3
                    $tmp = $data[$k] ?? '';
163

164 3
                    if ('string' === $v) {
165 3
                        $parsedData[$k] = (string) $tmp;
166
                    }
167

168 3
                    if ('int' === $v) {
169 3
                        $parsedData[$k] = (int) $tmp;
170
                    }
171
                }
172 3
                break;
173
            // end switch
174
        }
175

176 3
        return $parsedData;
177
    }
178

179
    // @codeCoverageIgnoreStart
180

181
    /**
182
     * Implement fetch.
183
     *
184
     * @param string $ip   The data id of the entry to fetch.
185
     * @param string $type The type of data table. accepts: filter | session | rule
186
     *
187
     * @return array The data or an empty array.
188
     */
189
    protected function doFetch(string $ip, string $type = 'filter'): array
190
    {
191
        return [];
192
    }
193

194
    /**
195
     * Implement fetch all.
196
     *
197
     * @param string $type The type of data table. accepts: filter | session | rule
198
     *
199
     * @return array The data or an empty array.
200
     */
201
    protected function doFetchAll(string $type = 'filter'): array
202
    {
203
        return [];
204
    }
205

206
    /**
207
     * Implement has.
208
     *
209
     * @param string $ip   The data id of the entry to check for.
210
     * @param string $type The type of data table. accepts: filter | session | rule
211
     *
212
     * @return bool
213
     */
214
    protected function checkExist(string $ip, string $type = 'filter'): bool
215
    {
216
        return false;
217
    }
218

219
    /**
220
     * Implement save.
221
     *
222
     * @param string $ip     The IP address as the data id.
223
     * @param array  $data   The data.
224
     * @param string $type   The type of the data table.
225
     * @param int    $expire The data will be deleted after expiring.
226
     *
227
     * @return bool
228
     */
229
    protected function doSave(string $ip, array $data, string $type = 'filter', $expire = 0): bool 
230
    {
231
        return false;
232
    }
233

234
    /**
235
     * Implement delete.
236
     *
237
     * @param string $ip   The IP address.
238
     * @param string $type The type of data table. accepts: filter | session | rule
239
     *
240
     * @return bool
241
     */
242
    protected function doDelete(string $ip, string $type = 'filter'): bool
243
    {
244
        return false;
245
    }
246

247
    /**
248
     * Rebuild data tables.
249
     *
250
     * @return bool
251
     */
252
    protected function doRebuild(): bool
253
    {
254
        return false;
255
    }
256

257
    /**
258
     * Initialize data tables.
259
     *
260
     * @param bool $dbCheck This is for creating data tables automatically
261
     *                      Turn it off, if you don't want to check data tables every pageview.
262
     *
263
     * @return void
264
     */
265
    protected function doInitialize(bool $dbCheck = true): void
266
    {
267

268
    }
269

270
    /**
271
     * Check data type.
272
     *
273
     * @param string $type The type of the data tables.
274
     *
275
     * @return void
276
     */
277
    protected function assertInvalidDataTable(string $type): void
278
    {
279
        if (!in_array($type, $this->tableTypes)) {
280
            throw new RuntimeException(
281
                'Invalid data type of the data tables.'
282
            );
283
        }
284
    }
285

286
    // @codeCoverageIgnoreEnd
287
}

Read our documentation on viewing source code .

Loading