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 RuntimeException;
26

27
use function is_array;
28
use function is_bool;
29
use function json_decode;
30

31
/**
32
 * SQL Driver Trait
33
 */
34
trait SqlDriverTrait
35
{
36
    /**
37
     * Fetch data from filter table.
38
     *
39
     * @param string $ip An IP address.
40
     *
41
     * @return array
42
     */
43 3
    protected function doFetchFromFilterTable(string $ip): array
44
    {
45 3
        $results = [];
46

47 3
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs . '
48
            WHERE log_ip = :log_ip
49
            LIMIT 1';
50

51 3
        $query = $this->db->prepare($sql);
52

53 3
        $this->assertPrepare($query);
54

55 3
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
56 3
        $query->execute();
57 3
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
58

59
        // No data found.
60 3
        if (is_bool($resultData) && !$resultData) {
61 3
            $resultData = [];
62
        }
63

64 3
        if (!empty($resultData['log_data'])) {
65 3
            $results = json_decode($resultData['log_data'], true); 
66
        }
67

68 3
        return $results;
69
    }
70

71
    /**
72
     * Fetch data from rule table.
73
     *
74
     * @param string $ip An IP address.
75
     *
76
     * @return array
77
     */
78 3
    protected function doFetchFromRuleTable(string $ip): array
79
    {
80 3
        $results = [];
81

82 3
        $sql = 'SELECT * FROM ' . $this->tableRuleList . '
83
            WHERE log_ip = :log_ip
84
            LIMIT 1';
85

86 3
        $query = $this->db->prepare($sql);
87

88 3
        $this->assertPrepare($query);
89
        
90 3
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
91 3
        $query->execute();
92 3
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
93

94
        // No data found.
95 3
        if (is_bool($resultData) && !$resultData) {
96 3
            $resultData = [];
97
        }
98

99 3
        if (is_array($resultData)) {
100 3
            $results = $resultData;
101
        }
102

103 3
        return $results;
104
    }
105

106
    /**
107
     * Fetch data from session table.
108
     *
109
     * @param string $id A session ID.
110
     *
111
     * @return array
112
     */
113 3
    protected function doFetchFromSessionTable(string $id): array
114
    {
115 3
        $results = [];
116

117 3
        $sql = 'SELECT * FROM ' . $this->tableSessions . '
118
            WHERE id = :id
119
            LIMIT 1';
120

121
      
122 3
        $query = $this->db->prepare($sql);
123

124 3
        $this->assertPrepare($query);
125

126 3
        $query->bindValue(':id', $id, $this->db::PARAM_STR);
127 3
        $query->execute();
128 3
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
129

130
        // No data found.
131 3
        if (is_bool($resultData) && !$resultData) {
132 3
            $resultData = [];
133
        }
134

135 3
        if (is_array($resultData)) {
136 3
            $results = $resultData;
137
        }
138

139 3
        return $results;
140
    }
141

142
    /**
143
     * Fetch all data from filter table.
144
     *
145
     * @return array
146
     */
147 3
    protected function doFetchAllFromFilterTable(): array
148
    {
149 3
        $results = [];
150

151 3
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs;
152

153 3
        $query = $this->db->prepare($sql);
154

155 3
        $this->assertPrepare($query);
156

157 3
        $query->execute();
158 3
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
159

160 3
        if (is_array($resultData)) {
161 3
            $results = $resultData;
162
        }
163

164 3
        return $results;
165
    }
166

167
    /**
168
     * Fetch all data from filter table.
169
     *
170
     * @return array
171
     */
172 3
    protected function doFetchAllFromRuleTable(): array
173
    {
174 3
        $results = [];
175

176 3
        $sql = 'SELECT * FROM ' . $this->tableRuleList;
177

178 3
        $query = $this->db->prepare($sql);
179

180 3
        $this->assertPrepare($query);
181

182 3
        $query->execute();
183 3
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
184

185 3
        if (is_array($resultData)) {
186 3
            $results = $resultData;
187
        }
188

189 3
        return $results;
190
    }
191

192
    /**
193
     * Fetch all data from session table.
194
     *
195
     * @return array
196
     */
197 3
    protected function doFetchAllFromSessionTable(): array
198
    {
199 3
        $results = [];
200

201 3
        $sql = 'SELECT * FROM ' . $this->tableSessions . ' ORDER BY microtimestamp ASC';
202

203 3
        $query = $this->db->prepare($sql);
204

205 3
        $this->assertPrepare($query);
206

207 3
        $query->execute();
208 3
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
209

210 3
        if (is_array($resultData)) {
211 3
            $results = $resultData;
212
        }
213

214 3
        return $results;
215
    }
216

217
    /**
218
     * Check the prepare statement status.
219
     *
220
     * @param object|bool $status Return false if failed.
221
     * 
222
     * @return void
223
     */
224 3
    protected function assertPrepare($status): void
225
    {
226 3
        if (!$status) {
227 3
            throw new RuntimeException(
228 3
                json_encode($this->db->errorInfo())
229
            );
230
        }
231
    }
232
}

Read our documentation on viewing source code .

Loading