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\SqlDriverProvider; |
26
|
|
|
use Exception; |
27
|
|
|
use PDO; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Sqlite Driver. |
31
|
|
|
*/ |
32
|
|
|
class SqliteDriver extends SqlDriverProvider |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param PDO $pdo The PDO instance. |
38
|
|
|
* @param bool $debug The option to enable debugging or not. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
27 |
|
public function __construct(PDO $pdo, bool $debug = false) |
43
|
|
|
{ |
44
|
27 |
|
parent::__construct($pdo, $debug); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create SQL tables that Shieldon needs. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
16 |
|
protected function installSql(): bool |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
16 |
|
$sql = " |
56
|
16 |
|
CREATE TABLE IF NOT EXISTS {$this->tableFilterLogs} ( |
57
|
|
|
log_ip VARCHAR(46) PRIMARY KEY, |
58
|
|
|
log_data BLOB |
59
|
|
|
); |
60
|
16 |
|
"; |
61
|
|
|
|
62
|
16 |
|
$this->db->query($sql); |
63
|
|
|
|
64
|
16 |
|
$sql = " |
65
|
16 |
|
CREATE TABLE IF NOT EXISTS {$this->tableRuleList} ( |
66
|
|
|
log_ip VARCHAR(46) PRIMARY KEY, |
67
|
|
|
ip_resolve VARCHAR(255), |
68
|
|
|
type TINYINT(3), |
69
|
|
|
reason TINYINT(3), |
70
|
|
|
time INT(10), |
71
|
|
|
attempts INT(10) |
72
|
|
|
); |
73
|
16 |
|
"; |
74
|
|
|
|
75
|
16 |
|
$this->db->query($sql); |
76
|
|
|
|
77
|
16 |
|
$sql = " |
78
|
16 |
|
CREATE TABLE IF NOT EXISTS {$this->tableSessions} ( |
79
|
|
|
id VARCHAR(40) PRIMARY KEY, |
80
|
|
|
ip VARCHAR(46), |
81
|
|
|
time INT(10), |
82
|
|
|
microtimestamp BIGINT(20), |
83
|
|
|
data |
84
|
|
|
); |
85
|
16 |
|
"; |
86
|
|
|
|
87
|
16 |
|
$this->db->query($sql); |
88
|
|
|
|
89
|
16 |
|
return true; |
90
|
|
|
|
91
|
|
|
// @codeCoverageIgnoreStart |
92
|
|
|
} catch (Exception $e) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
// @codeCoverageIgnoreEnd |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check required tables exist or not. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
23 |
|
protected function checkTableExists(): bool |
104
|
|
|
{ |
105
|
|
|
// Try a select statement against the table |
106
|
|
|
// Run it in try/catch in case PDO is in ERRMODE_EXCEPTION. |
107
|
|
|
// $debug should be false, otherwise an error occurs. |
108
|
|
|
|
109
|
|
|
try { |
110
|
23 |
|
$result = $this->db->query("SELECT 1 FROM $this->tableFilterLogs LIMIT 1"); |
111
|
|
|
|
112
|
|
|
// @codeCoverageIgnoreStart |
113
|
|
|
} catch (Exception $e) { |
114
|
|
|
// We got an exception == table not found |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
// @codeCoverageIgnoreEnd |
118
|
|
|
|
119
|
|
|
return ($result !== false); |
120
|
23 |
|
} |
121
|
|
|
} |
122
|
|
|
|