Passed
Push — 2.x ( 3c49c1...b272d3 )
by Terry
02:18
created

SqlDriverTrait::assertPrepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 1
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 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
    protected function doFetchFromFilterTable(string $ip): array
44
    {
45
        $results = [];
46
47
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs . '
48
            WHERE log_ip = :log_ip
49
            LIMIT 1';
50
51
        $query = $this->db->prepare($sql);
52
53
        $this->assertPrepare($query);
54
55
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
56
        $query->execute();
57
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
58
59
        // No data found.
60
        if (is_bool($resultData) && !$resultData) {
61
            $resultData = [];
62
        }
63
64
        if (!empty($resultData['log_data'])) {
65
            $results = json_decode($resultData['log_data'], true); 
66
        }
67
68
        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
    protected function doFetchFromRuleTable(string $ip): array
79
    {
80
        $results = [];
81
82
        $sql = 'SELECT * FROM ' . $this->tableRuleList . '
83
            WHERE log_ip = :log_ip
84
            LIMIT 1';
85
86
        $query = $this->db->prepare($sql);
87
88
        $this->assertPrepare($query);
89
        
90
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
91
        $query->execute();
92
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
93
94
        // No data found.
95
        if (is_bool($resultData) && !$resultData) {
96
            $resultData = [];
97
        }
98
99
        if (is_array($resultData)) {
100
            $results = $resultData;
101
        }
102
103
        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
    protected function doFetchFromSessionTable(string $id): array
114
    {
115
        $results = [];
116
117
        $sql = 'SELECT * FROM ' . $this->tableSessions . '
118
            WHERE id = :id
119
            LIMIT 1';
120
121
      
122
        $query = $this->db->prepare($sql);
123
124
        $this->assertPrepare($query);
125
126
        $query->bindValue(':id', $id, $this->db::PARAM_STR);
127
        $query->execute();
128
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
129
130
        // No data found.
131
        if (is_bool($resultData) && !$resultData) {
132
            $resultData = [];
133
        }
134
135
        if (is_array($resultData)) {
136
            $results = $resultData;
137
        }
138
139
        return $results;
140
    }
141
142
    /**
143
     * Fetch all data from filter table.
144
     *
145
     * @return array
146
     */
147
    protected function doFetchAllFromFilterTable(): array
148
    {
149
        $results = [];
150
151
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs;
152
153
        $query = $this->db->prepare($sql);
154
155
        $this->assertPrepare($query);
156
157
        $query->execute();
158
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
159
160
        if (is_array($resultData)) {
161
            $results = $resultData;
162
        }
163
164
        return $results;
165
    }
166
167
    /**
168
     * Fetch all data from filter table.
169
     *
170
     * @return array
171
     */
172
    protected function doFetchAllFromRuleTable(): array
173
    {
174
        $results = [];
175
176
        $sql = 'SELECT * FROM ' . $this->tableRuleList;
177
178
        $query = $this->db->prepare($sql);
179
180
        $this->assertPrepare($query);
181
182
        $query->execute();
183
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
184
185
        if (is_array($resultData)) {
186
            $results = $resultData;
187
        }
188
189
        return $results;
190
    }
191
192
    /**
193
     * Fetch all data from session table.
194
     *
195
     * @return array
196
     */
197
    protected function doFetchAllFromSessionTable(): array
198
    {
199
        $results = [];
200
201
        $sql = 'SELECT * FROM ' . $this->tableSessions . ' ORDER BY microtimesamp ASC';
202
203
        $query = $this->db->prepare($sql);
204
205
        $this->assertPrepare($query);
206
207
        $query->execute();
208
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
209
210
        if (is_array($resultData)) {
211
            $results = $resultData;
212
        }
213
214
        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
    protected function assertPrepare($status): void
225
    {
226
        if (!$status) {
227
            throw new RuntimeException(
228
                json_encode($this->db->errorInfo())
229
            );
230
        }
231
    }
232
}
233