Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
5.56% |
10 / 180 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
mysqli | |
5.56% |
10 / 180 |
|
0.00% |
0 / 14 |
7222.25 | |
0.00% |
0 / 1 |
sql_connect | |
8.11% |
3 / 37 |
|
0.00% |
0 / 1 |
189.59 | |||
sql_server_info | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
72 | |||
_sql_transaction | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
sql_query | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
240 | |||
sql_affectedrows | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
sql_fetchrow | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
42 | |||
sql_rowseek | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
sql_last_inserted_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
sql_freeresult | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
sql_escape | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
_sql_error | |
46.67% |
7 / 15 |
|
0.00% |
0 / 1 |
4.37 | |||
_sql_close | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
_sql_report | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
552 | |||
sql_quote | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\db\driver; |
15 | |
16 | /** |
17 | * MySQLi Database Abstraction Layer |
18 | * mysqli-extension has to be compiled with: |
19 | * MySQL 4.1+ or MySQL 5.0+ |
20 | */ |
21 | class mysqli extends \phpbb\db\driver\mysql_base |
22 | { |
23 | var $multi_insert = true; |
24 | var $connect_error = ''; |
25 | |
26 | /** |
27 | * {@inheritDoc} |
28 | */ |
29 | function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) |
30 | { |
31 | if (!function_exists('mysqli_connect')) |
32 | { |
33 | $this->connect_error = 'mysqli_connect function does not exist, is mysqli extension installed?'; |
34 | return $this->sql_error(''); |
35 | } |
36 | |
37 | $this->persistency = $persistency; |
38 | $this->user = $sqluser; |
39 | |
40 | // If persistent connection, set dbhost to localhost when empty and prepend it with 'p:' prefix |
41 | $this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver; |
42 | |
43 | $this->dbname = $database; |
44 | $port = (!$port) ? null : $port; |
45 | |
46 | // If port is set and it is not numeric, most likely mysqli socket is set. |
47 | // Try to map it to the $socket parameter. |
48 | $socket = null; |
49 | if ($port) |
50 | { |
51 | if (is_numeric($port)) |
52 | { |
53 | $port = (int) $port; |
54 | } |
55 | else |
56 | { |
57 | $socket = $port; |
58 | $port = null; |
59 | } |
60 | } |
61 | |
62 | $this->db_connect_id = mysqli_init(); |
63 | |
64 | if (!@mysqli_real_connect($this->db_connect_id, $this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS)) |
65 | { |
66 | $this->db_connect_id = ''; |
67 | } |
68 | |
69 | if ($this->db_connect_id && $this->dbname != '') |
70 | { |
71 | // Disable loading local files on client side |
72 | @mysqli_options($this->db_connect_id, MYSQLI_OPT_LOCAL_INFILE, 0); |
73 | |
74 | /* |
75 | * As of PHP 8.1 MySQLi default error mode is set to MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT |
76 | * See https://wiki.php.net/rfc/mysqli_default_errmode |
77 | * Since phpBB implements own SQL errors handling, explicitly set it back to MYSQLI_REPORT_OFF |
78 | */ |
79 | mysqli_report(MYSQLI_REPORT_OFF); |
80 | |
81 | @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'"); |
82 | |
83 | // enforce strict mode on databases that support it |
84 | if (version_compare($this->sql_server_info(true), '5.0.2', '>=')) |
85 | { |
86 | $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode'); |
87 | if ($result) |
88 | { |
89 | $row = mysqli_fetch_assoc($result); |
90 | mysqli_free_result($result); |
91 | |
92 | $modes = array_map('trim', explode(',', $row['sql_mode'])); |
93 | } |
94 | else |
95 | { |
96 | $modes = array(); |
97 | } |
98 | |
99 | // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES |
100 | if (!in_array('TRADITIONAL', $modes)) |
101 | { |
102 | if (!in_array('STRICT_ALL_TABLES', $modes)) |
103 | { |
104 | $modes[] = 'STRICT_ALL_TABLES'; |
105 | } |
106 | |
107 | if (!in_array('STRICT_TRANS_TABLES', $modes)) |
108 | { |
109 | $modes[] = 'STRICT_TRANS_TABLES'; |
110 | } |
111 | } |
112 | |
113 | $mode = implode(',', $modes); |
114 | @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'"); |
115 | } |
116 | return $this->db_connect_id; |
117 | } |
118 | |
119 | return $this->sql_error(''); |
120 | } |
121 | |
122 | /** |
123 | * {@inheritDoc} |
124 | */ |
125 | function sql_server_info($raw = false, $use_cache = true) |
126 | { |
127 | global $cache; |
128 | |
129 | if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false) |
130 | { |
131 | $result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version'); |
132 | if ($result) |
133 | { |
134 | $row = mysqli_fetch_assoc($result); |
135 | mysqli_free_result($result); |
136 | |
137 | $this->sql_server_version = $row['version']; |
138 | |
139 | if (!empty($cache) && $use_cache) |
140 | { |
141 | $cache->put('mysqli_version', $this->sql_server_version); |
142 | } |
143 | } |
144 | } |
145 | |
146 | return ($raw) ? (string) $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version; |
147 | } |
148 | |
149 | /** |
150 | * {@inheritDoc} |
151 | */ |
152 | protected function _sql_transaction(string $status = 'begin'): bool |
153 | { |
154 | switch ($status) |
155 | { |
156 | case 'begin': |
157 | @mysqli_autocommit($this->db_connect_id, false); |
158 | $result = @mysqli_begin_transaction($this->db_connect_id); |
159 | return $result; |
160 | |
161 | case 'commit': |
162 | $result = @mysqli_commit($this->db_connect_id); |
163 | @mysqli_autocommit($this->db_connect_id, true); |
164 | return $result; |
165 | |
166 | case 'rollback': |
167 | $result = @mysqli_rollback($this->db_connect_id); |
168 | @mysqli_autocommit($this->db_connect_id, true); |
169 | return $result; |
170 | } |
171 | |
172 | return true; |
173 | } |
174 | |
175 | /** |
176 | * {@inheritDoc} |
177 | */ |
178 | function sql_query($query = '', $cache_ttl = 0) |
179 | { |
180 | if ($query != '') |
181 | { |
182 | global $cache; |
183 | |
184 | if ($this->debug_sql_explain) |
185 | { |
186 | $this->sql_report('start', $query); |
187 | } |
188 | else if ($this->debug_load_time) |
189 | { |
190 | $this->curtime = microtime(true); |
191 | } |
192 | |
193 | $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false; |
194 | $this->sql_add_num_queries($this->query_result); |
195 | |
196 | if ($this->query_result === false) |
197 | { |
198 | try |
199 | { |
200 | $this->query_result = @mysqli_query($this->db_connect_id, $query); |
201 | } |
202 | catch (\Error $e) |
203 | { |
204 | // Do nothing as SQL driver will report the error |
205 | } |
206 | |
207 | if ($this->query_result === false) |
208 | { |
209 | $this->sql_error($query); |
210 | } |
211 | |
212 | if ($this->debug_sql_explain) |
213 | { |
214 | $this->sql_report('stop', $query); |
215 | } |
216 | else if ($this->debug_load_time) |
217 | { |
218 | $this->sql_time += microtime(true) - $this->curtime; |
219 | } |
220 | |
221 | if (!$this->query_result) |
222 | { |
223 | return false; |
224 | } |
225 | |
226 | if ($cache && $cache_ttl) |
227 | { |
228 | $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl); |
229 | } |
230 | } |
231 | else if ($this->debug_sql_explain) |
232 | { |
233 | $this->sql_report('fromcache', $query); |
234 | } |
235 | } |
236 | else |
237 | { |
238 | return false; |
239 | } |
240 | |
241 | return $this->query_result; |
242 | } |
243 | |
244 | /** |
245 | * {@inheritDoc} |
246 | */ |
247 | function sql_affectedrows() |
248 | { |
249 | return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false; |
250 | } |
251 | |
252 | /** |
253 | * {@inheritDoc} |
254 | */ |
255 | function sql_fetchrow($query_id = false) |
256 | { |
257 | global $cache; |
258 | |
259 | if ($query_id === false) |
260 | { |
261 | $query_id = $this->query_result; |
262 | } |
263 | |
264 | $safe_query_id = $this->clean_query_id($query_id); |
265 | if ($cache && $cache->sql_exists($safe_query_id)) |
266 | { |
267 | return $cache->sql_fetchrow($safe_query_id); |
268 | } |
269 | |
270 | if ($query_id) |
271 | { |
272 | $result = mysqli_fetch_assoc($query_id); |
273 | return $result !== null ? $result : false; |
274 | } |
275 | |
276 | return false; |
277 | } |
278 | |
279 | /** |
280 | * {@inheritDoc} |
281 | */ |
282 | function sql_rowseek($rownum, &$query_id) |
283 | { |
284 | global $cache; |
285 | |
286 | if ($query_id === false) |
287 | { |
288 | $query_id = $this->query_result; |
289 | } |
290 | |
291 | $safe_query_id = $this->clean_query_id($query_id); |
292 | if ($cache && $cache->sql_exists($safe_query_id)) |
293 | { |
294 | return $cache->sql_rowseek($rownum, $safe_query_id); |
295 | } |
296 | |
297 | return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false; |
298 | } |
299 | |
300 | /** |
301 | * {@inheritdoc} |
302 | */ |
303 | public function sql_last_inserted_id() |
304 | { |
305 | return ($this->db_connect_id) ? (int) @mysqli_insert_id($this->db_connect_id) : false; |
306 | } |
307 | |
308 | /** |
309 | * {@inheritDoc} |
310 | */ |
311 | function sql_freeresult($query_id = false) |
312 | { |
313 | global $cache; |
314 | |
315 | if ($query_id === false) |
316 | { |
317 | $query_id = $this->query_result; |
318 | } |
319 | |
320 | $safe_query_id = $this->clean_query_id($query_id); |
321 | if ($cache && $cache->sql_exists($safe_query_id)) |
322 | { |
323 | $cache->sql_freeresult($safe_query_id); |
324 | } |
325 | else if ($query_id && $query_id !== true) |
326 | { |
327 | mysqli_free_result($query_id); |
328 | } |
329 | } |
330 | |
331 | /** |
332 | * {@inheritDoc} |
333 | */ |
334 | function sql_escape($msg) |
335 | { |
336 | return @mysqli_real_escape_string($this->db_connect_id, $msg); |
337 | } |
338 | |
339 | /** |
340 | * {@inheritDoc} |
341 | */ |
342 | protected function _sql_error(): array |
343 | { |
344 | if ($this->db_connect_id) |
345 | { |
346 | $error = [ |
347 | 'message' => $this->db_connect_id->error, |
348 | 'code' => $this->db_connect_id->errno, |
349 | ]; |
350 | } |
351 | else if (function_exists('mysqli_connect_error')) |
352 | { |
353 | $error = [ |
354 | 'message' => $this->db_connect_id->connect_error, |
355 | 'code' => $this->db_connect_id->connect_errno, |
356 | ]; |
357 | } |
358 | else |
359 | { |
360 | $error = [ |
361 | 'message' => $this->connect_error, |
362 | 'code' => '', |
363 | ]; |
364 | } |
365 | |
366 | return $error; |
367 | } |
368 | |
369 | /** |
370 | * {@inheritDoc} |
371 | */ |
372 | protected function _sql_close(): bool |
373 | { |
374 | return @mysqli_close($this->db_connect_id); |
375 | } |
376 | |
377 | /** |
378 | * {@inheritDoc} |
379 | */ |
380 | protected function _sql_report(string $mode, string $query = ''): void |
381 | { |
382 | static $test_prof; |
383 | |
384 | // current detection method, might just switch to see the existence of INFORMATION_SCHEMA.PROFILING |
385 | if ($test_prof === null) |
386 | { |
387 | $test_prof = false; |
388 | if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false) |
389 | { |
390 | $ver = mysqli_get_server_version($this->db_connect_id); |
391 | if ($ver >= 50037 && $ver < 50100) |
392 | { |
393 | $test_prof = true; |
394 | } |
395 | } |
396 | } |
397 | |
398 | switch ($mode) |
399 | { |
400 | case 'start': |
401 | |
402 | $explain_query = $query; |
403 | if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) |
404 | { |
405 | $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; |
406 | } |
407 | else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) |
408 | { |
409 | $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; |
410 | } |
411 | |
412 | if (preg_match('/^SELECT/', $explain_query)) |
413 | { |
414 | $html_table = false; |
415 | |
416 | // begin profiling |
417 | if ($test_prof) |
418 | { |
419 | @mysqli_query($this->db_connect_id, 'SET profiling = 1;'); |
420 | } |
421 | |
422 | if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query")) |
423 | { |
424 | while ($row = mysqli_fetch_assoc($result)) |
425 | { |
426 | $html_table = $this->sql_report('add_select_row', $query, $html_table, $row); |
427 | } |
428 | mysqli_free_result($result); |
429 | } |
430 | |
431 | if ($html_table) |
432 | { |
433 | $this->html_hold .= '</table>'; |
434 | } |
435 | |
436 | if ($test_prof) |
437 | { |
438 | $html_table = false; |
439 | |
440 | // get the last profile |
441 | if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;')) |
442 | { |
443 | $this->html_hold .= '<br />'; |
444 | while ($row = mysqli_fetch_assoc($result)) |
445 | { |
446 | // make <unknown> HTML safe |
447 | if (!empty($row['Source_function'])) |
448 | { |
449 | $row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']); |
450 | } |
451 | |
452 | // remove unsupported features |
453 | foreach ($row as $key => $val) |
454 | { |
455 | if ($val === null) |
456 | { |
457 | unset($row[$key]); |
458 | } |
459 | } |
460 | $html_table = $this->sql_report('add_select_row', $query, $html_table, $row); |
461 | } |
462 | mysqli_free_result($result); |
463 | } |
464 | |
465 | if ($html_table) |
466 | { |
467 | $this->html_hold .= '</table>'; |
468 | } |
469 | |
470 | @mysqli_query($this->db_connect_id, 'SET profiling = 0;'); |
471 | } |
472 | } |
473 | |
474 | break; |
475 | |
476 | case 'fromcache': |
477 | $endtime = explode(' ', microtime()); |
478 | $endtime = $endtime[0] + $endtime[1]; |
479 | |
480 | $result = @mysqli_query($this->db_connect_id, $query); |
481 | if ($result) |
482 | { |
483 | while ($void = mysqli_fetch_assoc($result)) |
484 | { |
485 | // Take the time spent on parsing rows into account |
486 | } |
487 | mysqli_free_result($result); |
488 | } |
489 | |
490 | $splittime = explode(' ', microtime()); |
491 | $splittime = $splittime[0] + $splittime[1]; |
492 | |
493 | $this->sql_report('record_fromcache', $query, $endtime, $splittime); |
494 | |
495 | break; |
496 | } |
497 | } |
498 | |
499 | /** |
500 | * {@inheritDoc} |
501 | */ |
502 | function sql_quote($msg) |
503 | { |
504 | return '`' . $msg . '`'; |
505 | } |
506 | } |