1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
<?php
namespace Securimage\StorageAdapter;
use Securimage\StorageAdapter\AdapterInterface;
class Mysqli implements AdapterInterface { protected $database_host; protected $database_user; protected $database_pass; protected $database_name; protected $database_table; protected $database_file; protected $skip_table_check; protected $mysqli_conn; protected $expiry_time = 900;
public function __construct($options = null) { if (!empty($options) && is_array($options)) { foreach($options as $name => $val) { if (property_exists($this, $name)) { $this->$name = $val; } } }
$this->bootstrap(); }
public function store($captchaId, $captchaInfo) { return $this->saveCodeToDatabase($captchaId, $captchaInfo); }
public function storeAudioData($captchaId, $audioData) { return $this->saveAudioToDatabase($captchaId, $audioData); }
public function get($captchaId, $what = null) { $code = $this->getCodeFromDatabase($captchaId);
if ($code) { $info = new \Securimage\CaptchaObject; $info->captchaId = $captchaId; $info->code = $code['code']; $info->code_display = $code['code_display']; $info->creationTime = (int)$code['created']; $info->captchaImageAudio = $code['audio_data'];
return $info; }
return null; }
public function delete($captchaId) { return $this->clearCodeFromDatabase($captchaId); }
protected function bootstrap() { if ($this->openDatabase()) { if (mt_rand(0, 1000) % 100 === 0) { // approximately once per 100 connections $this->purgeOldCodesFromDatabase(); } } }
/** * Opens a connection to the configured database. * * @see Securimage::$use_database Use database * @see Securimage::$database_driver Database driver * * @return bool true if the database connection was successful, false if not */ protected function openDatabase() { if ($this->mysqli_conn && is_object($this->mysqli_conn) && $this->mysqli_conn instanceof \mysqli) { return true; }
if (!extension_loaded('mysqli')) { trigger_error("Mysqli adapter is enabled in Securimage, but the mysqli extension is not loaded in PHP.", E_USER_WARNING); return false; }
$this->mysqli_conn = new \mysqli($this->database_host, $this->database_user, $this->database_pass, $this->database_name);
if (mysqli_connect_error()) { trigger_error("Mysqli database connection failed. Error " . mysqli_connect_errno() . ': ' . mysqli_connect_error(), E_USER_WARNING); $this->mysqli_conn = null; return false; }
try { if (!$this->skip_table_check && !$this->checkTablesExist()) { // create tables... $this->createDatabaseTables(); } } catch (\Exception $ex) { trigger_error($ex->getMessage(), E_USER_WARNING); $this->mysqli_conn = null; return false; }
return true; }
/** * Checks if the necessary database tables for storing captcha codes exist * * @throws Exception If the table check failed for some reason * @return boolean true if the database do exist, false if not */ protected function checkTablesExist() { $table = $this->database_table; $query = "SHOW TABLES LIKE '$table'"; $result = $this->mysqli_conn->query($query);
if (!$result) { $errno = $this->mysqli_conn->errno; $error = $this->mysqli_conn->error;
throw new \Exception("Failed to check Securimage tables. Error {$errno}: {$error}"); } else if ($result->num_rows < 1) { $result->free(); return false; } else { $result->free(); return true; } }
/** * Create the necessary databaes table for storing captcha codes. * * Based on the database adapter used, the tables will created in the existing connection. * * @see Securimage::$database_driver Database driver * @return boolean true if the tables were created, false if not */ protected function createDatabaseTables() { $query = "CREATE TABLE `{$this->database_table}` ( `id` VARCHAR(40) NOT NULL, `namespace` VARCHAR(32) NOT NULL, `code` VARCHAR(32) NOT NULL, `code_display` VARCHAR(32) NOT NULL, `created` INT NOT NULL, `audio_data` MEDIUMBLOB NULL, PRIMARY KEY(id), INDEX(created) )";
$result = $this->mysqli_conn->query($query);
if (!$result) { $errno = $this->mysqli_conn->errno; $error = $this->mysqli_conn->error;
trigger_error("Failed to create table. Error {$errno}: {$error}", E_USER_WARNING); $this->mysqli_conn = null; return false; }
return true; }
/** * Saves the CAPTCHA data to the configured database. */ protected function saveCodeToDatabase($captchaId, $captchaInfo) { $success = false;
if ($this->mysqli_conn) { $time = $captchaInfo->creationTime; $code = $captchaInfo->code; $code_disp = $captchaInfo->code_display;
$query = "REPLACE INTO {$this->database_table} (id, code, code_display, namespace, created) VALUES(?, ?, ?, '', ?)"; $stmt = $this->mysqli_conn->prepare($query); $stmt->bind_param('sssi', $captchaId, $code, $code_disp, $time);
$success = $stmt->execute();
if (!$success) { $errno = $stmt->errno; $error = $stmt->error;
trigger_error("Failed to insert code into database. Error {$errno}: {$error}", E_USER_WARNING); } }
return $success !== false; }
/** * Saves CAPTCHA audio to the configured database * * @param string $data Audio data * @return boolean true on success, false on failure */ protected function saveAudioToDatabase($captchaId, $data) { $success = false;
if ($this->mysqli_conn) { $query = "UPDATE `{$this->database_table}` SET audio_data = ? WHERE id = ?"; $stmt = $this->mysqli_conn->prepare($query); $stmt->bind_param('ss', $data, $captchaId); $success = $stmt->execute(); }
return $success !== false; }
/** * Retrieves a stored code from the database for based on the captchaId or * IP address if captcha ID not used. * * @return string|array Empty string if no code was found or has expired, * otherwise returns array of code information. */ protected function getCodeFromDatabase($captchaId) { $code = '';
if ($this->mysqli_conn) { $query = "SELECT * FROM `{$this->database_table}` WHERE id = ?"; $stmt = $this->mysqli_conn->prepare($query); $stmt->bind_param('s', $captchaId);
$result = $stmt->execute();
if (!$result) { $errno = $stmt->errno; $errmsg = $stmt->error; trigger_error("Failed to select code from database. {$errno}: {$errmsg}", E_USER_WARNING); } else { $stmt->bind_result($id, $namespace, $code_val, $code_display, $created, $audio_data); if ($stmt->fetch() === true) { $code = array( 'code' => $code_val, 'code_display' => $code_display, 'created' => $created, 'audio_data' => $audio_data, ); } } }
return $code; }
/** * Remove a stored code from the database based on captchaId or IP address. */ protected function clearCodeFromDatabase($captchaId) { if ($this->mysqli_conn) { $query = "DELETE FROM `{$this->database_table}` WHERE id = ?"; $stmt = $this->mysqli_conn->prepare($query); $stmt->bind_param('s', $captchaId); $result = $stmt->execute();
if (!$result) { $errno = $stmt->errno; $error = $stmt->error; trigger_error("Failed to delete code from database. Error {$errno}: {$error}", E_USER_WARNING); } else { return true; } }
return false; }
/** * Deletes old (expired) codes from the database */ protected function purgeOldCodesFromDatabase() { $result = 0;
if ($this->mysqli_conn) { $now = time(); $limit = (!is_numeric($this->expiry_time) || $this->expiry_time < 1) ? 86400 : $this->expiry_time;
$query = sprintf("DELETE FROM `%s` WHERE %s - created > %s", $this->database_table, $now, $limit );
$result = $this->mysqli_conn->query($query); }
return $result; } }
|