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\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 3
    public function __construct(PDO $pdo, bool $debug = false)
43
    {
44 3
        parent::__construct($pdo, $debug);
45
    }
46

47
    /**
48
     * Create SQL tables that Shieldon needs.
49
     *
50
     * @return bool
51
     */
52 3
    protected function installSql(): bool
53
    {
54
        try {
55
            $sql = "
56 3
                CREATE TABLE IF NOT EXISTS {$this->tableFilterLogs} (
57
                    log_ip VARCHAR(46) PRIMARY KEY,
58
                    log_data BLOB
59
                );
60
            ";
61

62 3
            $this->db->query($sql);
63

64
            $sql = "
65 3
                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
            ";
74

75 3
            $this->db->query($sql);
76

77
            $sql = "
78 3
                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
            ";
86

87 3
            $this->db->query($sql);
88

89 3
            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 3
    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 3
            $result = $this->db->query("SELECT 1 FROM $this->tableFilterLogs LIMIT 1");
111

112
            // @codeCoverageIgnoreStart
113
        } catch (Exception $e) {
114

115
            // We got an exception == table not found
116
            return false;
117
        }
118
        // @codeCoverageIgnoreEnd
119

120 3
        return ($result !== false);
121
    }
122
}

Read our documentation on viewing source code .

Loading