1 | <?php |
---|
2 | /** |
---|
3 | * WordPress DB Class |
---|
4 | * |
---|
5 | * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)} |
---|
6 | * |
---|
7 | * @package WordPress |
---|
8 | * @subpackage Database |
---|
9 | * @since 0.71 |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * @since 0.71 |
---|
14 | */ |
---|
15 | define( 'EZSQL_VERSION', 'WP1.25' ); |
---|
16 | |
---|
17 | /** |
---|
18 | * @since 0.71 |
---|
19 | */ |
---|
20 | define( 'OBJECT', 'OBJECT' ); |
---|
21 | // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase |
---|
22 | define( 'object', 'OBJECT' ); // Back compat. |
---|
23 | |
---|
24 | /** |
---|
25 | * @since 2.5.0 |
---|
26 | */ |
---|
27 | define( 'OBJECT_K', 'OBJECT_K' ); |
---|
28 | |
---|
29 | /** |
---|
30 | * @since 0.71 |
---|
31 | */ |
---|
32 | define( 'ARRAY_A', 'ARRAY_A' ); |
---|
33 | |
---|
34 | /** |
---|
35 | * @since 0.71 |
---|
36 | */ |
---|
37 | define( 'ARRAY_N', 'ARRAY_N' ); |
---|
38 | |
---|
39 | /** |
---|
40 | * WordPress Database Access Abstraction Object |
---|
41 | * |
---|
42 | * It is possible to replace this class with your own |
---|
43 | * by setting the $wpdb global variable in wp-content/db.php |
---|
44 | * file to your class. The wpdb class will still be included, |
---|
45 | * so you can extend it or simply use your own. |
---|
46 | * |
---|
47 | * @link https://codex.wordpress.org/Function_Reference/wpdb_Class |
---|
48 | * |
---|
49 | * @since 0.71 |
---|
50 | */ |
---|
51 | class wpdb { |
---|
52 | |
---|
53 | /** |
---|
54 | * Whether to show SQL/DB errors. |
---|
55 | * |
---|
56 | * Default behavior is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY |
---|
57 | * evaluated to true. |
---|
58 | * |
---|
59 | * @since 0.71 |
---|
60 | * @var bool |
---|
61 | */ |
---|
62 | var $show_errors = false; |
---|
63 | |
---|
64 | /** |
---|
65 | * Whether to suppress errors during the DB bootstrapping. |
---|
66 | * |
---|
67 | * @since 2.5.0 |
---|
68 | * @var bool |
---|
69 | */ |
---|
70 | var $suppress_errors = false; |
---|
71 | |
---|
72 | /** |
---|
73 | * Whether to suppress filters 'query' filter prior to execution.. |
---|
74 | * |
---|
75 | * @since 5.4.0 |
---|
76 | * @var bool |
---|
77 | */ |
---|
78 | var $suppress_filters = false; |
---|
79 | |
---|
80 | /** |
---|
81 | * The last error during query. |
---|
82 | * |
---|
83 | * @since 2.5.0 |
---|
84 | * @var string |
---|
85 | */ |
---|
86 | public $last_error = ''; |
---|
87 | |
---|
88 | /** |
---|
89 | * Amount of queries made |
---|
90 | * |
---|
91 | * @since 1.2.0 |
---|
92 | * @var int |
---|
93 | */ |
---|
94 | public $num_queries = 0; |
---|
95 | |
---|
96 | /** |
---|
97 | * Count of rows returned by previous query |
---|
98 | * |
---|
99 | * @since 0.71 |
---|
100 | * @var int |
---|
101 | */ |
---|
102 | public $num_rows = 0; |
---|
103 | |
---|
104 | /** |
---|
105 | * Count of affected rows by previous query |
---|
106 | * |
---|
107 | * @since 0.71 |
---|
108 | * @var int |
---|
109 | */ |
---|
110 | var $rows_affected = 0; |
---|
111 | |
---|
112 | /** |
---|
113 | * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). |
---|
114 | * |
---|
115 | * @since 0.71 |
---|
116 | * @var int |
---|
117 | */ |
---|
118 | public $insert_id = 0; |
---|
119 | |
---|
120 | /** |
---|
121 | * Last query made |
---|
122 | * |
---|
123 | * @since 0.71 |
---|
124 | * @var string |
---|
125 | */ |
---|
126 | var $last_query; |
---|
127 | |
---|
128 | /** |
---|
129 | * Results of the last query made |
---|
130 | * |
---|
131 | * @since 0.71 |
---|
132 | * @var array|null |
---|
133 | */ |
---|
134 | var $last_result; |
---|
135 | |
---|
136 | /** |
---|
137 | * MySQL result, which is either a resource or boolean. |
---|
138 | * |
---|
139 | * @since 0.71 |
---|
140 | * @var mixed |
---|
141 | */ |
---|
142 | protected $result; |
---|
143 | |
---|
144 | /** |
---|
145 | * Cached column info, for sanity checking data before inserting |
---|
146 | * |
---|
147 | * @since 4.2.0 |
---|
148 | * @var array |
---|
149 | */ |
---|
150 | protected $col_meta = array(); |
---|
151 | |
---|
152 | /** |
---|
153 | * Calculated character sets on tables |
---|
154 | * |
---|
155 | * @since 4.2.0 |
---|
156 | * @var array |
---|
157 | */ |
---|
158 | protected $table_charset = array(); |
---|
159 | |
---|
160 | /** |
---|
161 | * Whether text fields in the current query need to be sanity checked. |
---|
162 | * |
---|
163 | * @since 4.2.0 |
---|
164 | * @var bool |
---|
165 | */ |
---|
166 | protected $check_current_query = true; |
---|
167 | |
---|
168 | /** |
---|
169 | * Flag to ensure we don't run into recursion problems when checking the collation. |
---|
170 | * |
---|
171 | * @since 4.2.0 |
---|
172 | * @see wpdb::check_safe_collation() |
---|
173 | * @var bool |
---|
174 | */ |
---|
175 | private $checking_collation = false; |
---|
176 | |
---|
177 | /** |
---|
178 | * Saved info on the table column |
---|
179 | * |
---|
180 | * @since 0.71 |
---|
181 | * @var array |
---|
182 | */ |
---|
183 | protected $col_info; |
---|
184 | |
---|
185 | /** |
---|
186 | * Log of queries that were executed, for debugging purposes. |
---|
187 | * |
---|
188 | * @since 1.5.0 |
---|
189 | * @since 2.5.0 The third element in each query log was added to record the calling functions. |
---|
190 | * @since 5.1.0 The fourth element in each query log was added to record the start time. |
---|
191 | * @since 5.3.0 The fifth element in each query log was added to record custom data. |
---|
192 | * |
---|
193 | * @var array[] { |
---|
194 | * Array of queries that were executed. |
---|
195 | * |
---|
196 | * @type array ...$0 { |
---|
197 | * Data for each query. |
---|
198 | * |
---|
199 | * @type string $0 The query's SQL. |
---|
200 | * @type float $1 Total time spent on the query, in seconds. |
---|
201 | * @type string $2 Comma separated list of the calling functions. |
---|
202 | * @type float $3 Unix timestamp of the time at the start of the query. |
---|
203 | * @type array $4 Custom query data. |
---|
204 | * } |
---|
205 | * } |
---|
206 | */ |
---|
207 | var $queries; |
---|
208 | |
---|
209 | /** |
---|
210 | * The number of times to retry reconnecting before dying. |
---|
211 | * |
---|
212 | * @since 3.9.0 |
---|
213 | * @see wpdb::check_connection() |
---|
214 | * @var int |
---|
215 | */ |
---|
216 | protected $reconnect_retries = 5; |
---|
217 | |
---|
218 | /** |
---|
219 | * WordPress table prefix |
---|
220 | * |
---|
221 | * You can set this to have multiple WordPress installations |
---|
222 | * in a single database. The second reason is for possible |
---|
223 | * security precautions. |
---|
224 | * |
---|
225 | * @since 2.5.0 |
---|
226 | * @var string |
---|
227 | */ |
---|
228 | public $prefix = ''; |
---|
229 | |
---|
230 | /** |
---|
231 | * WordPress base table prefix. |
---|
232 | * |
---|
233 | * @since 3.0.0 |
---|
234 | * @var string |
---|
235 | */ |
---|
236 | public $base_prefix; |
---|
237 | |
---|
238 | /** |
---|
239 | * Whether the database queries are ready to start executing. |
---|
240 | * |
---|
241 | * @since 2.3.2 |
---|
242 | * @var bool |
---|
243 | */ |
---|
244 | var $ready = false; |
---|
245 | |
---|
246 | /** |
---|
247 | * Blog ID. |
---|
248 | * |
---|
249 | * @since 3.0.0 |
---|
250 | * @var int |
---|
251 | */ |
---|
252 | public $blogid = 0; |
---|
253 | |
---|
254 | /** |
---|
255 | * Site ID. |
---|
256 | * |
---|
257 | * @since 3.0.0 |
---|
258 | * @var int |
---|
259 | */ |
---|
260 | public $siteid = 0; |
---|
261 | |
---|
262 | /** |
---|
263 | * List of WordPress per-blog tables |
---|
264 | * |
---|
265 | * @since 2.5.0 |
---|
266 | * @see wpdb::tables() |
---|
267 | * @var array |
---|
268 | */ |
---|
269 | var $tables = array( |
---|
270 | 'posts', |
---|
271 | 'comments', |
---|
272 | 'links', |
---|
273 | 'options', |
---|
274 | 'postmeta', |
---|
275 | 'terms', |
---|
276 | 'term_taxonomy', |
---|
277 | 'term_relationships', |
---|
278 | 'termmeta', |
---|
279 | 'commentmeta', |
---|
280 | ); |
---|
281 | |
---|
282 | /** |
---|
283 | * List of deprecated WordPress tables |
---|
284 | * |
---|
285 | * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539 |
---|
286 | * |
---|
287 | * @since 2.9.0 |
---|
288 | * @see wpdb::tables() |
---|
289 | * @var array |
---|
290 | */ |
---|
291 | var $old_tables = array( 'categories', 'post2cat', 'link2cat' ); |
---|
292 | |
---|
293 | /** |
---|
294 | * List of WordPress global tables |
---|
295 | * |
---|
296 | * @since 3.0.0 |
---|
297 | * @see wpdb::tables() |
---|
298 | * @var array |
---|
299 | */ |
---|
300 | var $global_tables = array( 'users', 'usermeta' ); |
---|
301 | |
---|
302 | /** |
---|
303 | * List of Multisite global tables |
---|
304 | * |
---|
305 | * @since 3.0.0 |
---|
306 | * @see wpdb::tables() |
---|
307 | * @var array |
---|
308 | */ |
---|
309 | var $ms_global_tables = array( |
---|
310 | 'blogs', |
---|
311 | 'blogmeta', |
---|
312 | 'signups', |
---|
313 | 'site', |
---|
314 | 'sitemeta', |
---|
315 | 'sitecategories', |
---|
316 | 'registration_log', |
---|
317 | ); |
---|
318 | |
---|
319 | /** |
---|
320 | * WordPress Comments table |
---|
321 | * |
---|
322 | * @since 1.5.0 |
---|
323 | * @var string |
---|
324 | */ |
---|
325 | public $comments; |
---|
326 | |
---|
327 | /** |
---|
328 | * WordPress Comment Metadata table |
---|
329 | * |
---|
330 | * @since 2.9.0 |
---|
331 | * @var string |
---|
332 | */ |
---|
333 | public $commentmeta; |
---|
334 | |
---|
335 | /** |
---|
336 | * WordPress Links table |
---|
337 | * |
---|
338 | * @since 1.5.0 |
---|
339 | * @var string |
---|
340 | */ |
---|
341 | public $links; |
---|
342 | |
---|
343 | /** |
---|
344 | * WordPress Options table |
---|
345 | * |
---|
346 | * @since 1.5.0 |
---|
347 | * @var string |
---|
348 | */ |
---|
349 | public $options; |
---|
350 | |
---|
351 | /** |
---|
352 | * WordPress Post Metadata table |
---|
353 | * |
---|
354 | * @since 1.5.0 |
---|
355 | * @var string |
---|
356 | */ |
---|
357 | public $postmeta; |
---|
358 | |
---|
359 | /** |
---|
360 | * WordPress Posts table |
---|
361 | * |
---|
362 | * @since 1.5.0 |
---|
363 | * @var string |
---|
364 | */ |
---|
365 | public $posts; |
---|
366 | |
---|
367 | /** |
---|
368 | * WordPress Terms table |
---|
369 | * |
---|
370 | * @since 2.3.0 |
---|
371 | * @var string |
---|
372 | */ |
---|
373 | public $terms; |
---|
374 | |
---|
375 | /** |
---|
376 | * WordPress Term Relationships table |
---|
377 | * |
---|
378 | * @since 2.3.0 |
---|
379 | * @var string |
---|
380 | */ |
---|
381 | public $term_relationships; |
---|
382 | |
---|
383 | /** |
---|
384 | * WordPress Term Taxonomy table |
---|
385 | * |
---|
386 | * @since 2.3.0 |
---|
387 | * @var string |
---|
388 | */ |
---|
389 | public $term_taxonomy; |
---|
390 | |
---|
391 | /** |
---|
392 | * WordPress Term Meta table. |
---|
393 | * |
---|
394 | * @since 4.4.0 |
---|
395 | * @var string |
---|
396 | */ |
---|
397 | public $termmeta; |
---|
398 | |
---|
399 | // |
---|
400 | // Global and Multisite tables |
---|
401 | // |
---|
402 | |
---|
403 | /** |
---|
404 | * WordPress User Metadata table |
---|
405 | * |
---|
406 | * @since 2.3.0 |
---|
407 | * @var string |
---|
408 | */ |
---|
409 | public $usermeta; |
---|
410 | |
---|
411 | /** |
---|
412 | * WordPress Users table |
---|
413 | * |
---|
414 | * @since 1.5.0 |
---|
415 | * @var string |
---|
416 | */ |
---|
417 | public $users; |
---|
418 | |
---|
419 | /** |
---|
420 | * Multisite Blogs table |
---|
421 | * |
---|
422 | * @since 3.0.0 |
---|
423 | * @var string |
---|
424 | */ |
---|
425 | public $blogs; |
---|
426 | |
---|
427 | /** |
---|
428 | * Multisite Blog Metadata table |
---|
429 | * |
---|
430 | * @since 5.1.0 |
---|
431 | * @var string |
---|
432 | */ |
---|
433 | public $blogmeta; |
---|
434 | |
---|
435 | /** |
---|
436 | * Multisite Registration Log table |
---|
437 | * |
---|
438 | * @since 3.0.0 |
---|
439 | * @var string |
---|
440 | */ |
---|
441 | public $registration_log; |
---|
442 | |
---|
443 | /** |
---|
444 | * Multisite Signups table |
---|
445 | * |
---|
446 | * @since 3.0.0 |
---|
447 | * @var string |
---|
448 | */ |
---|
449 | public $signups; |
---|
450 | |
---|
451 | /** |
---|
452 | * Multisite Sites table |
---|
453 | * |
---|
454 | * @since 3.0.0 |
---|
455 | * @var string |
---|
456 | */ |
---|
457 | public $site; |
---|
458 | |
---|
459 | /** |
---|
460 | * Multisite Sitewide Terms table |
---|
461 | * |
---|
462 | * @since 3.0.0 |
---|
463 | * @var string |
---|
464 | */ |
---|
465 | public $sitecategories; |
---|
466 | |
---|
467 | /** |
---|
468 | * Multisite Site Metadata table |
---|
469 | * |
---|
470 | * @since 3.0.0 |
---|
471 | * @var string |
---|
472 | */ |
---|
473 | public $sitemeta; |
---|
474 | |
---|
475 | /** |
---|
476 | * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load. |
---|
477 | * |
---|
478 | * Keys are column names, values are format types: 'ID' => '%d' |
---|
479 | * |
---|
480 | * @since 2.8.0 |
---|
481 | * @see wpdb::prepare() |
---|
482 | * @see wpdb::insert() |
---|
483 | * @see wpdb::update() |
---|
484 | * @see wpdb::delete() |
---|
485 | * @see wp_set_wpdb_vars() |
---|
486 | * @var array |
---|
487 | */ |
---|
488 | public $field_types = array(); |
---|
489 | |
---|
490 | /** |
---|
491 | * Database table columns charset |
---|
492 | * |
---|
493 | * @since 2.2.0 |
---|
494 | * @var string |
---|
495 | */ |
---|
496 | public $charset; |
---|
497 | |
---|
498 | /** |
---|
499 | * Database table columns collate |
---|
500 | * |
---|
501 | * @since 2.2.0 |
---|
502 | * @var string |
---|
503 | */ |
---|
504 | public $collate; |
---|
505 | |
---|
506 | /** |
---|
507 | * Database Username |
---|
508 | * |
---|
509 | * @since 2.9.0 |
---|
510 | * @var string |
---|
511 | */ |
---|
512 | protected $dbuser; |
---|
513 | |
---|
514 | /** |
---|
515 | * Database Password |
---|
516 | * |
---|
517 | * @since 3.1.0 |
---|
518 | * @var string |
---|
519 | */ |
---|
520 | protected $dbpassword; |
---|
521 | |
---|
522 | /** |
---|
523 | * Database Name |
---|
524 | * |
---|
525 | * @since 3.1.0 |
---|
526 | * @var string |
---|
527 | */ |
---|
528 | protected $dbname; |
---|
529 | |
---|
530 | /** |
---|
531 | * Database Host |
---|
532 | * |
---|
533 | * @since 3.1.0 |
---|
534 | * @var string |
---|
535 | */ |
---|
536 | protected $dbhost; |
---|
537 | |
---|
538 | /** |
---|
539 | * Database Handle |
---|
540 | * |
---|
541 | * @since 0.71 |
---|
542 | * @var string |
---|
543 | */ |
---|
544 | protected $dbh; |
---|
545 | |
---|
546 | /** |
---|
547 | * A textual description of the last query/get_row/get_var call |
---|
548 | * |
---|
549 | * @since 3.0.0 |
---|
550 | * @var string |
---|
551 | */ |
---|
552 | public $func_call; |
---|
553 | |
---|
554 | /** |
---|
555 | * Whether MySQL is used as the database engine. |
---|
556 | * |
---|
557 | * Set in WPDB::db_connect() to true, by default. This is used when checking |
---|
558 | * against the required MySQL version for WordPress. Normally, a replacement |
---|
559 | * database drop-in (db.php) will skip these checks, but setting this to true |
---|
560 | * will force the checks to occur. |
---|
561 | * |
---|
562 | * @since 3.3.0 |
---|
563 | * @var bool |
---|
564 | */ |
---|
565 | public $is_mysql = null; |
---|
566 | |
---|
567 | /** |
---|
568 | * A list of incompatible SQL modes. |
---|
569 | * |
---|
570 | * @since 3.9.0 |
---|
571 | * @var array |
---|
572 | */ |
---|
573 | protected $incompatible_modes = array( |
---|
574 | 'NO_ZERO_DATE', |
---|
575 | 'ONLY_FULL_GROUP_BY', |
---|
576 | 'STRICT_TRANS_TABLES', |
---|
577 | 'STRICT_ALL_TABLES', |
---|
578 | 'TRADITIONAL', |
---|
579 | ); |
---|
580 | |
---|
581 | /** |
---|
582 | * Whether to use mysqli over mysql. |
---|
583 | * |
---|
584 | * @since 3.9.0 |
---|
585 | * @var bool |
---|
586 | */ |
---|
587 | private $use_mysqli = false; |
---|
588 | |
---|
589 | /** |
---|
590 | * Whether we've managed to successfully connect at some point |
---|
591 | * |
---|
592 | * @since 3.9.0 |
---|
593 | * @var bool |
---|
594 | */ |
---|
595 | private $has_connected = false; |
---|
596 | |
---|
597 | /** |
---|
598 | * Connects to the database server and selects a database |
---|
599 | * |
---|
600 | * PHP5 style constructor for compatibility with PHP5. Does |
---|
601 | * the actual setting up of the class properties and connection |
---|
602 | * to the database. |
---|
603 | * |
---|
604 | * @link https://core.trac.wordpress.org/ticket/3354 |
---|
605 | * @since 2.0.8 |
---|
606 | * |
---|
607 | * @global string $wp_version |
---|
608 | * |
---|
609 | * @param string $dbuser MySQL database user |
---|
610 | * @param string $dbpassword MySQL database password |
---|
611 | * @param string $dbname MySQL database name |
---|
612 | * @param string $dbhost MySQL database host |
---|
613 | */ |
---|
614 | public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
---|
615 | register_shutdown_function( array( $this, '__destruct' ) ); |
---|
616 | |
---|
617 | if ( WP_DEBUG && WP_DEBUG_DISPLAY ) { |
---|
618 | $this->show_errors(); |
---|
619 | } |
---|
620 | |
---|
621 | // Use ext/mysqli if it exists unless WP_USE_EXT_MYSQL is defined as true |
---|
622 | if ( function_exists( 'mysqli_connect' ) ) { |
---|
623 | $this->use_mysqli = true; |
---|
624 | |
---|
625 | if ( defined( 'WP_USE_EXT_MYSQL' ) ) { |
---|
626 | $this->use_mysqli = ! WP_USE_EXT_MYSQL; |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | $this->dbuser = $dbuser; |
---|
631 | $this->dbpassword = $dbpassword; |
---|
632 | $this->dbname = $dbname; |
---|
633 | $this->dbhost = $dbhost; |
---|
634 | |
---|
635 | // wp-config.php creation will manually connect when ready. |
---|
636 | if ( defined( 'WP_SETUP_CONFIG' ) ) { |
---|
637 | return; |
---|
638 | } |
---|
639 | |
---|
640 | $this->db_connect(); |
---|
641 | } |
---|
642 | |
---|
643 | /** |
---|
644 | * PHP5 style destructor and will run when database object is destroyed. |
---|
645 | * |
---|
646 | * @see wpdb::__construct() |
---|
647 | * @since 2.0.8 |
---|
648 | * @return true |
---|
649 | */ |
---|
650 | public function __destruct() { |
---|
651 | return true; |
---|
652 | } |
---|
653 | |
---|
654 | /** |
---|
655 | * Makes private properties readable for backward compatibility. |
---|
656 | * |
---|
657 | * @since 3.5.0 |
---|
658 | * |
---|
659 | * @param string $name The private member to get, and optionally process |
---|
660 | * @return mixed The private member |
---|
661 | */ |
---|
662 | public function __get( $name ) { |
---|
663 | if ( 'col_info' === $name ) { |
---|
664 | $this->load_col_info(); |
---|
665 | } |
---|
666 | |
---|
667 | return $this->$name; |
---|
668 | } |
---|
669 | |
---|
670 | /** |
---|
671 | * Makes private properties settable for backward compatibility. |
---|
672 | * |
---|
673 | * @since 3.5.0 |
---|
674 | * |
---|
675 | * @param string $name The private member to set |
---|
676 | * @param mixed $value The value to set |
---|
677 | */ |
---|
678 | public function __set( $name, $value ) { |
---|
679 | $protected_members = array( |
---|
680 | 'col_meta', |
---|
681 | 'table_charset', |
---|
682 | 'check_current_query', |
---|
683 | ); |
---|
684 | if ( in_array( $name, $protected_members, true ) ) { |
---|
685 | return; |
---|
686 | } |
---|
687 | $this->$name = $value; |
---|
688 | } |
---|
689 | |
---|
690 | /** |
---|
691 | * Makes private properties check-able for backward compatibility. |
---|
692 | * |
---|
693 | * @since 3.5.0 |
---|
694 | * |
---|
695 | * @param string $name The private member to check |
---|
696 | * |
---|
697 | * @return bool If the member is set or not |
---|
698 | */ |
---|
699 | public function __isset( $name ) { |
---|
700 | return isset( $this->$name ); |
---|
701 | } |
---|
702 | |
---|
703 | /** |
---|
704 | * Makes private properties un-settable for backward compatibility. |
---|
705 | * |
---|
706 | * @since 3.5.0 |
---|
707 | * |
---|
708 | * @param string $name The private member to unset |
---|
709 | */ |
---|
710 | public function __unset( $name ) { |
---|
711 | unset( $this->$name ); |
---|
712 | } |
---|
713 | |
---|
714 | /** |
---|
715 | * Set $this->charset and $this->collate |
---|
716 | * |
---|
717 | * @since 3.1.0 |
---|
718 | */ |
---|
719 | public function init_charset() { |
---|
720 | $charset = ''; |
---|
721 | $collate = ''; |
---|
722 | |
---|
723 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
---|
724 | $charset = 'utf8'; |
---|
725 | if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) { |
---|
726 | $collate = DB_COLLATE; |
---|
727 | } else { |
---|
728 | $collate = 'utf8_general_ci'; |
---|
729 | } |
---|
730 | } elseif ( defined( 'DB_COLLATE' ) ) { |
---|
731 | $collate = DB_COLLATE; |
---|
732 | } |
---|
733 | |
---|
734 | if ( defined( 'DB_CHARSET' ) ) { |
---|
735 | $charset = DB_CHARSET; |
---|
736 | } |
---|
737 | |
---|
738 | $charset_collate = $this->determine_charset( $charset, $collate ); |
---|
739 | |
---|
740 | $this->charset = $charset_collate['charset']; |
---|
741 | $this->collate = $charset_collate['collate']; |
---|
742 | } |
---|
743 | |
---|
744 | /** |
---|
745 | * Determines the best charset and collation to use given a charset and collation. |
---|
746 | * |
---|
747 | * For example, when able, utf8mb4 should be used instead of utf8. |
---|
748 | * |
---|
749 | * @since 4.6.0 |
---|
750 | * |
---|
751 | * @param string $charset The character set to check. |
---|
752 | * @param string $collate The collation to check. |
---|
753 | * @return array The most appropriate character set and collation to use. |
---|
754 | */ |
---|
755 | public function determine_charset( $charset, $collate ) { |
---|
756 | if ( ( $this->use_mysqli && ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) { |
---|
757 | return compact( 'charset', 'collate' ); |
---|
758 | } |
---|
759 | |
---|
760 | if ( 'utf8' === $charset && $this->has_cap( 'utf8mb4' ) ) { |
---|
761 | $charset = 'utf8mb4'; |
---|
762 | } |
---|
763 | |
---|
764 | if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) { |
---|
765 | $charset = 'utf8'; |
---|
766 | $collate = str_replace( 'utf8mb4_', 'utf8_', $collate ); |
---|
767 | } |
---|
768 | |
---|
769 | if ( 'utf8mb4' === $charset ) { |
---|
770 | // _general_ is outdated, so we can upgrade it to _unicode_, instead. |
---|
771 | if ( ! $collate || 'utf8_general_ci' === $collate ) { |
---|
772 | $collate = 'utf8mb4_unicode_ci'; |
---|
773 | } else { |
---|
774 | $collate = str_replace( 'utf8_', 'utf8mb4_', $collate ); |
---|
775 | } |
---|
776 | } |
---|
777 | |
---|
778 | // _unicode_520_ is a better collation, we should use that when it's available. |
---|
779 | if ( $this->has_cap( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $collate ) { |
---|
780 | $collate = 'utf8mb4_unicode_520_ci'; |
---|
781 | } |
---|
782 | |
---|
783 | return compact( 'charset', 'collate' ); |
---|
784 | } |
---|
785 | |
---|
786 | /** |
---|
787 | * Sets the connection's character set. |
---|
788 | * |
---|
789 | * @since 3.1.0 |
---|
790 | * |
---|
791 | * @param resource $dbh The resource given by mysql_connect |
---|
792 | * @param string $charset Optional. The character set. Default null. |
---|
793 | * @param string $collate Optional. The collation. Default null. |
---|
794 | */ |
---|
795 | public function set_charset( $dbh, $charset = null, $collate = null ) { |
---|
796 | if ( ! isset( $charset ) ) { |
---|
797 | $charset = $this->charset; |
---|
798 | } |
---|
799 | if ( ! isset( $collate ) ) { |
---|
800 | $collate = $this->collate; |
---|
801 | } |
---|
802 | if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { |
---|
803 | $set_charset_succeeded = true; |
---|
804 | |
---|
805 | if ( $this->use_mysqli ) { |
---|
806 | if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { |
---|
807 | $set_charset_succeeded = mysqli_set_charset( $dbh, $charset ); |
---|
808 | } |
---|
809 | |
---|
810 | if ( $set_charset_succeeded ) { |
---|
811 | $query = $this->prepare( 'SET NAMES %s', $charset ); |
---|
812 | if ( ! empty( $collate ) ) { |
---|
813 | $query .= $this->prepare( ' COLLATE %s', $collate ); |
---|
814 | } |
---|
815 | mysqli_query( $dbh, $query ); |
---|
816 | } |
---|
817 | } else { |
---|
818 | if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { |
---|
819 | $set_charset_succeeded = mysql_set_charset( $charset, $dbh ); |
---|
820 | } |
---|
821 | if ( $set_charset_succeeded ) { |
---|
822 | $query = $this->prepare( 'SET NAMES %s', $charset ); |
---|
823 | if ( ! empty( $collate ) ) { |
---|
824 | $query .= $this->prepare( ' COLLATE %s', $collate ); |
---|
825 | } |
---|
826 | mysql_query( $query, $dbh ); |
---|
827 | } |
---|
828 | } |
---|
829 | } |
---|
830 | } |
---|
831 | |
---|
832 | /** |
---|
833 | * Change the current SQL mode, and ensure its WordPress compatibility. |
---|
834 | * |
---|
835 | * If no modes are passed, it will ensure the current MySQL server |
---|
836 | * modes are compatible. |
---|
837 | * |
---|
838 | * @since 3.9.0 |
---|
839 | * |
---|
840 | * @param array $modes Optional. A list of SQL modes to set. |
---|
841 | */ |
---|
842 | public function set_sql_mode( $modes = array() ) { |
---|
843 | if ( empty( $modes ) ) { |
---|
844 | if ( $this->use_mysqli ) { |
---|
845 | $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' ); |
---|
846 | } else { |
---|
847 | $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh ); |
---|
848 | } |
---|
849 | |
---|
850 | if ( empty( $res ) ) { |
---|
851 | return; |
---|
852 | } |
---|
853 | |
---|
854 | if ( $this->use_mysqli ) { |
---|
855 | $modes_array = mysqli_fetch_array( $res ); |
---|
856 | if ( empty( $modes_array[0] ) ) { |
---|
857 | return; |
---|
858 | } |
---|
859 | $modes_str = $modes_array[0]; |
---|
860 | } else { |
---|
861 | $modes_str = mysql_result( $res, 0 ); |
---|
862 | } |
---|
863 | |
---|
864 | if ( empty( $modes_str ) ) { |
---|
865 | return; |
---|
866 | } |
---|
867 | |
---|
868 | $modes = explode( ',', $modes_str ); |
---|
869 | } |
---|
870 | |
---|
871 | $modes = array_change_key_case( $modes, CASE_UPPER ); |
---|
872 | |
---|
873 | /** |
---|
874 | * Filters the list of incompatible SQL modes to exclude. |
---|
875 | * |
---|
876 | * @since 3.9.0 |
---|
877 | * |
---|
878 | * @param array $incompatible_modes An array of incompatible modes. |
---|
879 | */ |
---|
880 | $incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes ); |
---|
881 | |
---|
882 | foreach ( $modes as $i => $mode ) { |
---|
883 | if ( in_array( $mode, $incompatible_modes ) ) { |
---|
884 | unset( $modes[ $i ] ); |
---|
885 | } |
---|
886 | } |
---|
887 | |
---|
888 | $modes_str = implode( ',', $modes ); |
---|
889 | |
---|
890 | if ( $this->use_mysqli ) { |
---|
891 | mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" ); |
---|
892 | } else { |
---|
893 | mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh ); |
---|
894 | } |
---|
895 | } |
---|
896 | |
---|
897 | /** |
---|
898 | * Sets the table prefix for the WordPress tables. |
---|
899 | * |
---|
900 | * @since 2.5.0 |
---|
901 | * |
---|
902 | * @param string $prefix Alphanumeric name for the new prefix. |
---|
903 | * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not. |
---|
904 | * @return string|WP_Error Old prefix or WP_Error on error |
---|
905 | */ |
---|
906 | public function set_prefix( $prefix, $set_table_names = true ) { |
---|
907 | |
---|
908 | if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) { |
---|
909 | return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' ); |
---|
910 | } |
---|
911 | |
---|
912 | $old_prefix = is_multisite() ? '' : $prefix; |
---|
913 | |
---|
914 | if ( isset( $this->base_prefix ) ) { |
---|
915 | $old_prefix = $this->base_prefix; |
---|
916 | } |
---|
917 | |
---|
918 | $this->base_prefix = $prefix; |
---|
919 | |
---|
920 | if ( $set_table_names ) { |
---|
921 | foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) { |
---|
922 | $this->$table = $prefixed_table; |
---|
923 | } |
---|
924 | |
---|
925 | if ( is_multisite() && empty( $this->blogid ) ) { |
---|
926 | return $old_prefix; |
---|
927 | } |
---|
928 | |
---|
929 | $this->prefix = $this->get_blog_prefix(); |
---|
930 | |
---|
931 | foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) { |
---|
932 | $this->$table = $prefixed_table; |
---|
933 | } |
---|
934 | |
---|
935 | foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) { |
---|
936 | $this->$table = $prefixed_table; |
---|
937 | } |
---|
938 | } |
---|
939 | return $old_prefix; |
---|
940 | } |
---|
941 | |
---|
942 | /** |
---|
943 | * Sets blog id. |
---|
944 | * |
---|
945 | * @since 3.0.0 |
---|
946 | * |
---|
947 | * @param int $blog_id |
---|
948 | * @param int $network_id Optional. |
---|
949 | * @return int previous blog id |
---|
950 | */ |
---|
951 | public function set_blog_id( $blog_id, $network_id = 0 ) { |
---|
952 | if ( ! empty( $network_id ) ) { |
---|
953 | $this->siteid = $network_id; |
---|
954 | } |
---|
955 | |
---|
956 | $old_blog_id = $this->blogid; |
---|
957 | $this->blogid = $blog_id; |
---|
958 | |
---|
959 | $this->prefix = $this->get_blog_prefix(); |
---|
960 | |
---|
961 | foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) { |
---|
962 | $this->$table = $prefixed_table; |
---|
963 | } |
---|
964 | |
---|
965 | foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) { |
---|
966 | $this->$table = $prefixed_table; |
---|
967 | } |
---|
968 | |
---|
969 | return $old_blog_id; |
---|
970 | } |
---|
971 | |
---|
972 | /** |
---|
973 | * Gets blog prefix. |
---|
974 | * |
---|
975 | * @since 3.0.0 |
---|
976 | * @param int $blog_id Optional. |
---|
977 | * @return string Blog prefix. |
---|
978 | */ |
---|
979 | public function get_blog_prefix( $blog_id = null ) { |
---|
980 | if ( is_multisite() ) { |
---|
981 | if ( null === $blog_id ) { |
---|
982 | $blog_id = $this->blogid; |
---|
983 | } |
---|
984 | $blog_id = (int) $blog_id; |
---|
985 | if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) { |
---|
986 | return $this->base_prefix; |
---|
987 | } else { |
---|
988 | return $this->base_prefix . $blog_id . '_'; |
---|
989 | } |
---|
990 | } else { |
---|
991 | return $this->base_prefix; |
---|
992 | } |
---|
993 | } |
---|
994 | |
---|
995 | /** |
---|
996 | * Returns an array of WordPress tables. |
---|
997 | * |
---|
998 | * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to |
---|
999 | * override the WordPress users and usermeta tables that would otherwise |
---|
1000 | * be determined by the prefix. |
---|
1001 | * |
---|
1002 | * The scope argument can take one of the following: |
---|
1003 | * |
---|
1004 | * 'all' - returns 'all' and 'global' tables. No old tables are returned. |
---|
1005 | * 'blog' - returns the blog-level tables for the queried blog. |
---|
1006 | * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite. |
---|
1007 | * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. |
---|
1008 | * 'old' - returns tables which are deprecated. |
---|
1009 | * |
---|
1010 | * @since 3.0.0 |
---|
1011 | * @uses wpdb::$tables |
---|
1012 | * @uses wpdb::$old_tables |
---|
1013 | * @uses wpdb::$global_tables |
---|
1014 | * @uses wpdb::$ms_global_tables |
---|
1015 | * |
---|
1016 | * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all. |
---|
1017 | * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog |
---|
1018 | * prefix is requested, then the custom users and usermeta tables will be mapped. |
---|
1019 | * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested. |
---|
1020 | * @return array Table names. When a prefix is requested, the key is the unprefixed table name. |
---|
1021 | */ |
---|
1022 | public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { |
---|
1023 | switch ( $scope ) { |
---|
1024 | case 'all': |
---|
1025 | $tables = array_merge( $this->global_tables, $this->tables ); |
---|
1026 | if ( is_multisite() ) { |
---|
1027 | $tables = array_merge( $tables, $this->ms_global_tables ); |
---|
1028 | } |
---|
1029 | break; |
---|
1030 | case 'blog': |
---|
1031 | $tables = $this->tables; |
---|
1032 | break; |
---|
1033 | case 'global': |
---|
1034 | $tables = $this->global_tables; |
---|
1035 | if ( is_multisite() ) { |
---|
1036 | $tables = array_merge( $tables, $this->ms_global_tables ); |
---|
1037 | } |
---|
1038 | break; |
---|
1039 | case 'ms_global': |
---|
1040 | $tables = $this->ms_global_tables; |
---|
1041 | break; |
---|
1042 | case 'old': |
---|
1043 | $tables = $this->old_tables; |
---|
1044 | break; |
---|
1045 | default: |
---|
1046 | return array(); |
---|
1047 | } |
---|
1048 | |
---|
1049 | if ( $prefix ) { |
---|
1050 | if ( ! $blog_id ) { |
---|
1051 | $blog_id = $this->blogid; |
---|
1052 | } |
---|
1053 | $blog_prefix = $this->get_blog_prefix( $blog_id ); |
---|
1054 | $base_prefix = $this->base_prefix; |
---|
1055 | $global_tables = array_merge( $this->global_tables, $this->ms_global_tables ); |
---|
1056 | foreach ( $tables as $k => $table ) { |
---|
1057 | if ( in_array( $table, $global_tables ) ) { |
---|
1058 | $tables[ $table ] = $base_prefix . $table; |
---|
1059 | } else { |
---|
1060 | $tables[ $table ] = $blog_prefix . $table; |
---|
1061 | } |
---|
1062 | unset( $tables[ $k ] ); |
---|
1063 | } |
---|
1064 | |
---|
1065 | if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) { |
---|
1066 | $tables['users'] = CUSTOM_USER_TABLE; |
---|
1067 | } |
---|
1068 | |
---|
1069 | if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) { |
---|
1070 | $tables['usermeta'] = CUSTOM_USER_META_TABLE; |
---|
1071 | } |
---|
1072 | } |
---|
1073 | |
---|
1074 | return $tables; |
---|
1075 | } |
---|
1076 | |
---|
1077 | /** |
---|
1078 | * Selects a database using the current database connection. |
---|
1079 | * |
---|
1080 | * The database name will be changed based on the current database |
---|
1081 | * connection. On failure, the execution will bail and display an DB error. |
---|
1082 | * |
---|
1083 | * @since 0.71 |
---|
1084 | * |
---|
1085 | * @param string $db MySQL database name |
---|
1086 | * @param resource|null $dbh Optional link identifier. |
---|
1087 | */ |
---|
1088 | public function select( $db, $dbh = null ) { |
---|
1089 | if ( is_null( $dbh ) ) { |
---|
1090 | $dbh = $this->dbh; |
---|
1091 | } |
---|
1092 | |
---|
1093 | if ( $this->use_mysqli ) { |
---|
1094 | $success = mysqli_select_db( $dbh, $db ); |
---|
1095 | } else { |
---|
1096 | $success = mysql_select_db( $db, $dbh ); |
---|
1097 | } |
---|
1098 | if ( ! $success ) { |
---|
1099 | $this->ready = false; |
---|
1100 | if ( ! did_action( 'template_redirect' ) ) { |
---|
1101 | wp_load_translations_early(); |
---|
1102 | |
---|
1103 | $message = '<h1>' . __( 'Can’t select database' ) . "</h1>\n"; |
---|
1104 | |
---|
1105 | $message .= '<p>' . sprintf( |
---|
1106 | /* translators: %s: Database name. */ |
---|
1107 | __( 'We were able to connect to the database server (which means your username and password is okay) but not able to select the %s database.' ), |
---|
1108 | '<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>' |
---|
1109 | ) . "</p>\n"; |
---|
1110 | |
---|
1111 | $message .= "<ul>\n"; |
---|
1112 | $message .= '<li>' . __( 'Are you sure it exists?' ) . "</li>\n"; |
---|
1113 | |
---|
1114 | $message .= '<li>' . sprintf( |
---|
1115 | /* translators: 1: Database user, 2: Database name. */ |
---|
1116 | __( 'Does the user %1$s have permission to use the %2$s database?' ), |
---|
1117 | '<code>' . htmlspecialchars( $this->dbuser, ENT_QUOTES ) . '</code>', |
---|
1118 | '<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>' |
---|
1119 | ) . "</li>\n"; |
---|
1120 | |
---|
1121 | $message .= '<li>' . sprintf( |
---|
1122 | /* translators: %s: Database name. */ |
---|
1123 | __( 'On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?' ), |
---|
1124 | htmlspecialchars( $db, ENT_QUOTES ) |
---|
1125 | ) . "</li>\n"; |
---|
1126 | |
---|
1127 | $message .= "</ul>\n"; |
---|
1128 | |
---|
1129 | $message .= '<p>' . sprintf( |
---|
1130 | /* translators: %s: Support forums URL. */ |
---|
1131 | __( 'If you don’t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="%s">WordPress Support Forums</a>.' ), |
---|
1132 | __( 'https://wordpress.org/support/forums/' ) |
---|
1133 | ) . "</p>\n"; |
---|
1134 | |
---|
1135 | $this->bail( $message, 'db_select_fail' ); |
---|
1136 | } |
---|
1137 | } |
---|
1138 | } |
---|
1139 | |
---|
1140 | /** |
---|
1141 | * Do not use, deprecated. |
---|
1142 | * |
---|
1143 | * Use esc_sql() or wpdb::prepare() instead. |
---|
1144 | * |
---|
1145 | * @since 2.8.0 |
---|
1146 | * @deprecated 3.6.0 Use wpdb::prepare() |
---|
1147 | * @see wpdb::prepare |
---|
1148 | * @see esc_sql() |
---|
1149 | * |
---|
1150 | * @param string $string |
---|
1151 | * @return string |
---|
1152 | */ |
---|
1153 | function _weak_escape( $string ) { |
---|
1154 | if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) { |
---|
1155 | _deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' ); |
---|
1156 | } |
---|
1157 | return addslashes( $string ); |
---|
1158 | } |
---|
1159 | |
---|
1160 | /** |
---|
1161 | * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() |
---|
1162 | * |
---|
1163 | * @see mysqli_real_escape_string() |
---|
1164 | * @see mysql_real_escape_string() |
---|
1165 | * @since 2.8.0 |
---|
1166 | * |
---|
1167 | * @param string $string to escape |
---|
1168 | * @return string escaped |
---|
1169 | */ |
---|
1170 | function _real_escape( $string ) { |
---|
1171 | if ( $this->dbh ) { |
---|
1172 | if ( $this->use_mysqli ) { |
---|
1173 | $escaped = mysqli_real_escape_string( $this->dbh, $string ); |
---|
1174 | } else { |
---|
1175 | $escaped = mysql_real_escape_string( $string, $this->dbh ); |
---|
1176 | } |
---|
1177 | } else { |
---|
1178 | $class = get_class( $this ); |
---|
1179 | if ( function_exists( '__' ) ) { |
---|
1180 | /* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */ |
---|
1181 | _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); |
---|
1182 | } else { |
---|
1183 | _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' ); |
---|
1184 | } |
---|
1185 | $escaped = addslashes( $string ); |
---|
1186 | } |
---|
1187 | |
---|
1188 | return $this->add_placeholder_escape( $escaped ); |
---|
1189 | } |
---|
1190 | |
---|
1191 | /** |
---|
1192 | * Escape data. Works on arrays. |
---|
1193 | * |
---|
1194 | * @uses wpdb::_real_escape() |
---|
1195 | * @since 2.8.0 |
---|
1196 | * |
---|
1197 | * @param string|array $data |
---|
1198 | * @return string|array escaped |
---|
1199 | */ |
---|
1200 | public function _escape( $data ) { |
---|
1201 | if ( is_array( $data ) ) { |
---|
1202 | foreach ( $data as $k => $v ) { |
---|
1203 | if ( is_array( $v ) ) { |
---|
1204 | $data[ $k ] = $this->_escape( $v ); |
---|
1205 | } else { |
---|
1206 | $data[ $k ] = $this->_real_escape( $v ); |
---|
1207 | } |
---|
1208 | } |
---|
1209 | } else { |
---|
1210 | $data = $this->_real_escape( $data ); |
---|
1211 | } |
---|
1212 | |
---|
1213 | return $data; |
---|
1214 | } |
---|
1215 | |
---|
1216 | /** |
---|
1217 | * Do not use, deprecated. |
---|
1218 | * |
---|
1219 | * Use esc_sql() or wpdb::prepare() instead. |
---|
1220 | * |
---|
1221 | * @since 0.71 |
---|
1222 | * @deprecated 3.6.0 Use wpdb::prepare() |
---|
1223 | * @see wpdb::prepare() |
---|
1224 | * @see esc_sql() |
---|
1225 | * |
---|
1226 | * @param mixed $data |
---|
1227 | * @return mixed |
---|
1228 | */ |
---|
1229 | public function escape( $data ) { |
---|
1230 | if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) { |
---|
1231 | _deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' ); |
---|
1232 | } |
---|
1233 | if ( is_array( $data ) ) { |
---|
1234 | foreach ( $data as $k => $v ) { |
---|
1235 | if ( is_array( $v ) ) { |
---|
1236 | $data[ $k ] = $this->escape( $v, 'recursive' ); |
---|
1237 | } else { |
---|
1238 | $data[ $k ] = $this->_weak_escape( $v, 'internal' ); |
---|
1239 | } |
---|
1240 | } |
---|
1241 | } else { |
---|
1242 | $data = $this->_weak_escape( $data, 'internal' ); |
---|
1243 | } |
---|
1244 | |
---|
1245 | return $data; |
---|
1246 | } |
---|
1247 | |
---|
1248 | /** |
---|
1249 | * Escapes content by reference for insertion into the database, for security |
---|
1250 | * |
---|
1251 | * @uses wpdb::_real_escape() |
---|
1252 | * |
---|
1253 | * @since 2.3.0 |
---|
1254 | * |
---|
1255 | * @param string $string to escape |
---|
1256 | */ |
---|
1257 | public function escape_by_ref( &$string ) { |
---|
1258 | if ( ! is_float( $string ) ) { |
---|
1259 | $string = $this->_real_escape( $string ); |
---|
1260 | } |
---|
1261 | } |
---|
1262 | |
---|
1263 | /** |
---|
1264 | * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. |
---|
1265 | * |
---|
1266 | * The following placeholders can be used in the query string: |
---|
1267 | * %d (integer) |
---|
1268 | * %f (float) |
---|
1269 | * %s (string) |
---|
1270 | * |
---|
1271 | * All placeholders MUST be left unquoted in the query string. A corresponding argument |
---|
1272 | * MUST be passed for each placeholder. |
---|
1273 | * |
---|
1274 | * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s) |
---|
1275 | * will not have quotes added by this function, so should be passed with appropriate quotes around |
---|
1276 | * them for your usage. |
---|
1277 | * |
---|
1278 | * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, |
---|
1279 | * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these |
---|
1280 | * cannot be inserted directly in the query string. Also see wpdb::esc_like(). |
---|
1281 | * |
---|
1282 | * Arguments may be passed as individual arguments to the method, or as a single array containing |
---|
1283 | * all arguments. A combination of the two is not supported. |
---|
1284 | * |
---|
1285 | * Examples: |
---|
1286 | * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); |
---|
1287 | * $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); |
---|
1288 | * |
---|
1289 | * @link https://secure.php.net/sprintf Description of syntax. |
---|
1290 | * @since 2.3.0 |
---|
1291 | * @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
---|
1292 | * by updating the function signature. The second parameter was changed |
---|
1293 | * from `$args` to `...$args`. |
---|
1294 | * |
---|
1295 | * @param string $query Query statement with sprintf()-like placeholders |
---|
1296 | * @param array|mixed $args The array of variables to substitute into the query's placeholders |
---|
1297 | * if being called with an array of arguments, or the first variable |
---|
1298 | * to substitute into the query's placeholders if being called with |
---|
1299 | * individual arguments. |
---|
1300 | * @param mixed ...$args Further variables to substitute into the query's placeholders |
---|
1301 | * if being called with individual arguments. |
---|
1302 | * @return string|void Sanitized query string, if there is a query to prepare. |
---|
1303 | */ |
---|
1304 | public function prepare( $query, ...$args ) { |
---|
1305 | if ( is_null( $query ) ) { |
---|
1306 | return; |
---|
1307 | } |
---|
1308 | |
---|
1309 | // This is not meant to be foolproof -- but it will catch obviously incorrect usage. |
---|
1310 | if ( strpos( $query, '%' ) === false ) { |
---|
1311 | wp_load_translations_early(); |
---|
1312 | _doing_it_wrong( |
---|
1313 | 'wpdb::prepare', |
---|
1314 | sprintf( |
---|
1315 | /* translators: %s: wpdb::prepare() */ |
---|
1316 | __( 'The query argument of %s must have a placeholder.' ), |
---|
1317 | 'wpdb::prepare()' |
---|
1318 | ), |
---|
1319 | '3.9.0' |
---|
1320 | ); |
---|
1321 | } |
---|
1322 | |
---|
1323 | // If args were passed as an array (as in vsprintf), move them up. |
---|
1324 | $passed_as_array = false; |
---|
1325 | if ( is_array( $args[0] ) && count( $args ) == 1 ) { |
---|
1326 | $passed_as_array = true; |
---|
1327 | $args = $args[0]; |
---|
1328 | } |
---|
1329 | |
---|
1330 | foreach ( $args as $arg ) { |
---|
1331 | if ( ! is_scalar( $arg ) && ! is_null( $arg ) ) { |
---|
1332 | wp_load_translations_early(); |
---|
1333 | _doing_it_wrong( |
---|
1334 | 'wpdb::prepare', |
---|
1335 | sprintf( |
---|
1336 | /* translators: %s: Value type. */ |
---|
1337 | __( 'Unsupported value type (%s).' ), |
---|
1338 | gettype( $arg ) |
---|
1339 | ), |
---|
1340 | '4.8.2' |
---|
1341 | ); |
---|
1342 | } |
---|
1343 | } |
---|
1344 | |
---|
1345 | /* |
---|
1346 | * Specify the formatting allowed in a placeholder. The following are allowed: |
---|
1347 | * |
---|
1348 | * - Sign specifier. eg, $+d |
---|
1349 | * - Numbered placeholders. eg, %1$s |
---|
1350 | * - Padding specifier, including custom padding characters. eg, %05s, %'#5s |
---|
1351 | * - Alignment specifier. eg, %05-s |
---|
1352 | * - Precision specifier. eg, %.2f |
---|
1353 | */ |
---|
1354 | $allowed_format = '(?:[1-9][0-9]*[$])?[-+0-9]*(?: |0|\'.)?[-+0-9]*(?:\.[0-9]+)?'; |
---|
1355 | |
---|
1356 | /* |
---|
1357 | * If a %s placeholder already has quotes around it, removing the existing quotes and re-inserting them |
---|
1358 | * ensures the quotes are consistent. |
---|
1359 | * |
---|
1360 | * For backward compatibility, this is only applied to %s, and not to placeholders like %1$s, which are frequently |
---|
1361 | * used in the middle of longer strings, or as table name placeholders. |
---|
1362 | */ |
---|
1363 | $query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes. |
---|
1364 | $query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes. |
---|
1365 | $query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s. |
---|
1366 | |
---|
1367 | $query = preg_replace( "/(?<!%)(%($allowed_format)?f)/", '%\\2F', $query ); // Force floats to be locale unaware. |
---|
1368 | |
---|
1369 | $query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents. |
---|
1370 | |
---|
1371 | // Count the number of valid placeholders in the query. |
---|
1372 | $placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches ); |
---|
1373 | |
---|
1374 | if ( count( $args ) !== $placeholders ) { |
---|
1375 | if ( 1 === $placeholders && $passed_as_array ) { |
---|
1376 | // If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail. |
---|
1377 | wp_load_translations_early(); |
---|
1378 | _doing_it_wrong( |
---|
1379 | 'wpdb::prepare', |
---|
1380 | __( 'The query only expected one placeholder, but an array of multiple placeholders was sent.' ), |
---|
1381 | '4.9.0' |
---|
1382 | ); |
---|
1383 | |
---|
1384 | return; |
---|
1385 | } else { |
---|
1386 | /* |
---|
1387 | * If we don't have the right number of placeholders, but they were passed as individual arguments, |
---|
1388 | * or we were expecting multiple arguments in an array, throw a warning. |
---|
1389 | */ |
---|
1390 | wp_load_translations_early(); |
---|
1391 | _doing_it_wrong( |
---|
1392 | 'wpdb::prepare', |
---|
1393 | sprintf( |
---|
1394 | /* translators: 1: Number of placeholders, 2: Number of arguments passed. */ |
---|
1395 | __( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ), |
---|
1396 | $placeholders, |
---|
1397 | count( $args ) |
---|
1398 | ), |
---|
1399 | '4.8.3' |
---|
1400 | ); |
---|
1401 | } |
---|
1402 | } |
---|
1403 | |
---|
1404 | array_walk( $args, array( $this, 'escape_by_ref' ) ); |
---|
1405 | $query = vsprintf( $query, $args ); |
---|
1406 | |
---|
1407 | return $this->add_placeholder_escape( $query ); |
---|
1408 | } |
---|
1409 | |
---|
1410 | /** |
---|
1411 | * First half of escaping for LIKE special characters % and _ before preparing for MySQL. |
---|
1412 | * |
---|
1413 | * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security. |
---|
1414 | * |
---|
1415 | * Example Prepared Statement: |
---|
1416 | * |
---|
1417 | * $wild = '%'; |
---|
1418 | * $find = 'only 43% of planets'; |
---|
1419 | * $like = $wild . $wpdb->esc_like( $find ) . $wild; |
---|
1420 | * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like ); |
---|
1421 | * |
---|
1422 | * Example Escape Chain: |
---|
1423 | * |
---|
1424 | * $sql = esc_sql( $wpdb->esc_like( $input ) ); |
---|
1425 | * |
---|
1426 | * @since 4.0.0 |
---|
1427 | * |
---|
1428 | * @param string $text The raw text to be escaped. The input typed by the user should have no |
---|
1429 | * extra or deleted slashes. |
---|
1430 | * @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare() |
---|
1431 | * or real_escape next. |
---|
1432 | */ |
---|
1433 | public function esc_like( $text ) { |
---|
1434 | return addcslashes( $text, '_%\\' ); |
---|
1435 | } |
---|
1436 | |
---|
1437 | /** |
---|
1438 | * Print SQL/DB error. |
---|
1439 | * |
---|
1440 | * @since 0.71 |
---|
1441 | * @global array $EZSQL_ERROR Stores error information of query and error string |
---|
1442 | * |
---|
1443 | * @param string $str The error to display |
---|
1444 | * @return false|void False if the showing of errors is disabled. |
---|
1445 | */ |
---|
1446 | public function print_error( $str = '' ) { |
---|
1447 | global $EZSQL_ERROR; |
---|
1448 | |
---|
1449 | if ( ! $str ) { |
---|
1450 | if ( $this->use_mysqli ) { |
---|
1451 | $str = mysqli_error( $this->dbh ); |
---|
1452 | } else { |
---|
1453 | $str = mysql_error( $this->dbh ); |
---|
1454 | } |
---|
1455 | } |
---|
1456 | $EZSQL_ERROR[] = array( |
---|
1457 | 'query' => $this->last_query, |
---|
1458 | 'error_str' => $str, |
---|
1459 | ); |
---|
1460 | |
---|
1461 | if ( $this->suppress_errors ) { |
---|
1462 | return false; |
---|
1463 | } |
---|
1464 | |
---|
1465 | wp_load_translations_early(); |
---|
1466 | |
---|
1467 | $caller = $this->get_caller(); |
---|
1468 | if ( $caller ) { |
---|
1469 | /* translators: 1: Database error message, 2: SQL query, 3: Name of the calling function. */ |
---|
1470 | $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); |
---|
1471 | } else { |
---|
1472 | /* translators: 1: Database error message, 2: SQL query. */ |
---|
1473 | $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); |
---|
1474 | } |
---|
1475 | |
---|
1476 | error_log( $error_str ); |
---|
1477 | |
---|
1478 | // Are we showing errors? |
---|
1479 | if ( ! $this->show_errors ) { |
---|
1480 | return false; |
---|
1481 | } |
---|
1482 | |
---|
1483 | // If there is an error then take note of it |
---|
1484 | if ( is_multisite() ) { |
---|
1485 | $msg = sprintf( |
---|
1486 | "%s [%s]\n%s\n", |
---|
1487 | __( 'WordPress database error:' ), |
---|
1488 | $str, |
---|
1489 | $this->last_query |
---|
1490 | ); |
---|
1491 | |
---|
1492 | if ( defined( 'ERRORLOGFILE' ) ) { |
---|
1493 | error_log( $msg, 3, ERRORLOGFILE ); |
---|
1494 | } |
---|
1495 | if ( defined( 'DIEONDBERROR' ) ) { |
---|
1496 | wp_die( $msg ); |
---|
1497 | } |
---|
1498 | } else { |
---|
1499 | $str = htmlspecialchars( $str, ENT_QUOTES ); |
---|
1500 | $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); |
---|
1501 | |
---|
1502 | printf( |
---|
1503 | '<div id="error"><p class="wpdberror"><strong>%s</strong> [%s]<br /><code>%s</code></p></div>', |
---|
1504 | __( 'WordPress database error:' ), |
---|
1505 | $str, |
---|
1506 | $query |
---|
1507 | ); |
---|
1508 | } |
---|
1509 | } |
---|
1510 | |
---|
1511 | /** |
---|
1512 | * Enables showing of database errors. |
---|
1513 | * |
---|
1514 | * This function should be used only to enable showing of errors. |
---|
1515 | * wpdb::hide_errors() should be used instead for hiding of errors. However, |
---|
1516 | * this function can be used to enable and disable showing of database |
---|
1517 | * errors. |
---|
1518 | * |
---|
1519 | * @since 0.71 |
---|
1520 | * @see wpdb::hide_errors() |
---|
1521 | * |
---|
1522 | * @param bool $show Whether to show or hide errors |
---|
1523 | * @return bool Old value for showing errors. |
---|
1524 | */ |
---|
1525 | public function show_errors( $show = true ) { |
---|
1526 | $errors = $this->show_errors; |
---|
1527 | $this->show_errors = $show; |
---|
1528 | return $errors; |
---|
1529 | } |
---|
1530 | |
---|
1531 | /** |
---|
1532 | * Disables showing of database errors. |
---|
1533 | * |
---|
1534 | * By default database errors are not shown. |
---|
1535 | * |
---|
1536 | * @since 0.71 |
---|
1537 | * @see wpdb::show_errors() |
---|
1538 | * |
---|
1539 | * @return bool Whether showing of errors was active |
---|
1540 | */ |
---|
1541 | public function hide_errors() { |
---|
1542 | $show = $this->show_errors; |
---|
1543 | $this->show_errors = false; |
---|
1544 | return $show; |
---|
1545 | } |
---|
1546 | |
---|
1547 | /** |
---|
1548 | * Whether to suppress database errors. |
---|
1549 | * |
---|
1550 | * By default database errors are suppressed, with a simple |
---|
1551 | * call to this function they can be enabled. |
---|
1552 | * |
---|
1553 | * @since 2.5.0 |
---|
1554 | * @see wpdb::hide_errors() |
---|
1555 | * @param bool $suppress Optional. New value. Defaults to true. |
---|
1556 | * @return bool Old value |
---|
1557 | */ |
---|
1558 | public function suppress_errors( $suppress = true ) { |
---|
1559 | $errors = $this->suppress_errors; |
---|
1560 | $this->suppress_errors = (bool) $suppress; |
---|
1561 | return $errors; |
---|
1562 | } |
---|
1563 | |
---|
1564 | /** |
---|
1565 | * Kill cached query results. |
---|
1566 | * |
---|
1567 | * @since 0.71 |
---|
1568 | */ |
---|
1569 | public function flush() { |
---|
1570 | $this->last_result = array(); |
---|
1571 | $this->col_info = null; |
---|
1572 | $this->last_query = null; |
---|
1573 | $this->rows_affected = 0; |
---|
1574 | $this->num_rows = 0; |
---|
1575 | $this->last_error = ''; |
---|
1576 | |
---|
1577 | if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { |
---|
1578 | mysqli_free_result( $this->result ); |
---|
1579 | $this->result = null; |
---|
1580 | |
---|
1581 | // Sanity check before using the handle |
---|
1582 | if ( empty( $this->dbh ) || ! ( $this->dbh instanceof mysqli ) ) { |
---|
1583 | return; |
---|
1584 | } |
---|
1585 | |
---|
1586 | // Clear out any results from a multi-query |
---|
1587 | while ( mysqli_more_results( $this->dbh ) ) { |
---|
1588 | mysqli_next_result( $this->dbh ); |
---|
1589 | } |
---|
1590 | } elseif ( is_resource( $this->result ) ) { |
---|
1591 | mysql_free_result( $this->result ); |
---|
1592 | } |
---|
1593 | } |
---|
1594 | |
---|
1595 | /** |
---|
1596 | * Connect to and select database. |
---|
1597 | * |
---|
1598 | * If $allow_bail is false, the lack of database connection will need |
---|
1599 | * to be handled manually. |
---|
1600 | * |
---|
1601 | * @since 3.0.0 |
---|
1602 | * @since 3.9.0 $allow_bail parameter added. |
---|
1603 | * |
---|
1604 | * @param bool $allow_bail Optional. Allows the function to bail. Default true. |
---|
1605 | * @return bool True with a successful connection, false on failure. |
---|
1606 | */ |
---|
1607 | public function db_connect( $allow_bail = true ) { |
---|
1608 | $this->is_mysql = true; |
---|
1609 | |
---|
1610 | /* |
---|
1611 | * Deprecated in 3.9+ when using MySQLi. No equivalent |
---|
1612 | * $new_link parameter exists for mysqli_* functions. |
---|
1613 | */ |
---|
1614 | $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
---|
1615 | $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
---|
1616 | |
---|
1617 | if ( $this->use_mysqli ) { |
---|
1618 | $this->dbh = mysqli_init(); |
---|
1619 | |
---|
1620 | $host = $this->dbhost; |
---|
1621 | $port = null; |
---|
1622 | $socket = null; |
---|
1623 | $is_ipv6 = false; |
---|
1624 | |
---|
1625 | $host_data = $this->parse_db_host( $this->dbhost ); |
---|
1626 | if ( $host_data ) { |
---|
1627 | list( $host, $port, $socket, $is_ipv6 ) = $host_data; |
---|
1628 | } |
---|
1629 | |
---|
1630 | /* |
---|
1631 | * If using the `mysqlnd` library, the IPv6 address needs to be |
---|
1632 | * enclosed in square brackets, whereas it doesn't while using the |
---|
1633 | * `libmysqlclient` library. |
---|
1634 | * @see https://bugs.php.net/bug.php?id=67563 |
---|
1635 | */ |
---|
1636 | if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) { |
---|
1637 | $host = "[$host]"; |
---|
1638 | } |
---|
1639 | |
---|
1640 | if ( WP_DEBUG ) { |
---|
1641 | mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
---|
1642 | } else { |
---|
1643 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
---|
1644 | @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
---|
1645 | } |
---|
1646 | |
---|
1647 | if ( $this->dbh->connect_errno ) { |
---|
1648 | $this->dbh = null; |
---|
1649 | |
---|
1650 | /* |
---|
1651 | * It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: |
---|
1652 | * - We haven't previously connected, and |
---|
1653 | * - WP_USE_EXT_MYSQL isn't set to false, and |
---|
1654 | * - ext/mysql is loaded. |
---|
1655 | */ |
---|
1656 | $attempt_fallback = true; |
---|
1657 | |
---|
1658 | if ( $this->has_connected ) { |
---|
1659 | $attempt_fallback = false; |
---|
1660 | } elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) { |
---|
1661 | $attempt_fallback = false; |
---|
1662 | } elseif ( ! function_exists( 'mysql_connect' ) ) { |
---|
1663 | $attempt_fallback = false; |
---|
1664 | } |
---|
1665 | |
---|
1666 | if ( $attempt_fallback ) { |
---|
1667 | $this->use_mysqli = false; |
---|
1668 | return $this->db_connect( $allow_bail ); |
---|
1669 | } |
---|
1670 | } |
---|
1671 | } else { |
---|
1672 | if ( WP_DEBUG ) { |
---|
1673 | $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
---|
1674 | } else { |
---|
1675 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
---|
1676 | $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
---|
1677 | } |
---|
1678 | } |
---|
1679 | |
---|
1680 | if ( ! $this->dbh && $allow_bail ) { |
---|
1681 | wp_load_translations_early(); |
---|
1682 | |
---|
1683 | // Load custom DB error template, if present. |
---|
1684 | if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
---|
1685 | require_once( WP_CONTENT_DIR . '/db-error.php' ); |
---|
1686 | die(); |
---|
1687 | } |
---|
1688 | |
---|
1689 | $message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n"; |
---|
1690 | |
---|
1691 | $message .= '<p>' . sprintf( |
---|
1692 | /* translators: 1: wp-config.php, 2: Database host. */ |
---|
1693 | __( 'This either means that the username and password information in your %1$s file is incorrect or we can’t contact the database server at %2$s. This could mean your host’s database server is down.' ), |
---|
1694 | '<code>wp-config.php</code>', |
---|
1695 | '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
---|
1696 | ) . "</p>\n"; |
---|
1697 | |
---|
1698 | $message .= "<ul>\n"; |
---|
1699 | $message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n"; |
---|
1700 | $message .= '<li>' . __( 'Are you sure you have typed the correct hostname?' ) . "</li>\n"; |
---|
1701 | $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
---|
1702 | $message .= "</ul>\n"; |
---|
1703 | |
---|
1704 | $message .= '<p>' . sprintf( |
---|
1705 | /* translators: %s: Support forums URL. */ |
---|
1706 | __( 'If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ), |
---|
1707 | __( 'https://wordpress.org/support/forums/' ) |
---|
1708 | ) . "</p>\n"; |
---|
1709 | |
---|
1710 | $this->bail( $message, 'db_connect_fail' ); |
---|
1711 | |
---|
1712 | return false; |
---|
1713 | } elseif ( $this->dbh ) { |
---|
1714 | if ( ! $this->has_connected ) { |
---|
1715 | $this->init_charset(); |
---|
1716 | } |
---|
1717 | |
---|
1718 | $this->has_connected = true; |
---|
1719 | |
---|
1720 | $this->set_charset( $this->dbh ); |
---|
1721 | |
---|
1722 | $this->ready = true; |
---|
1723 | $this->set_sql_mode(); |
---|
1724 | $this->select( $this->dbname, $this->dbh ); |
---|
1725 | |
---|
1726 | return true; |
---|
1727 | } |
---|
1728 | |
---|
1729 | return false; |
---|
1730 | } |
---|
1731 | |
---|
1732 | /** |
---|
1733 | * Parse the DB_HOST setting to interpret it for mysqli_real_connect. |
---|
1734 | * |
---|
1735 | * mysqli_real_connect doesn't support the host param including a port or |
---|
1736 | * socket like mysql_connect does. This duplicates how mysql_connect detects |
---|
1737 | * a port and/or socket file. |
---|
1738 | * |
---|
1739 | * @since 4.9.0 |
---|
1740 | * |
---|
1741 | * @param string $host The DB_HOST setting to parse. |
---|
1742 | * @return array|bool Array containing the host, the port, the socket and whether |
---|
1743 | * it is an IPv6 address, in that order. If $host couldn't be parsed, |
---|
1744 | * returns false. |
---|
1745 | */ |
---|
1746 | public function parse_db_host( $host ) { |
---|
1747 | $port = null; |
---|
1748 | $socket = null; |
---|
1749 | $is_ipv6 = false; |
---|
1750 | |
---|
1751 | // First peel off the socket parameter from the right, if it exists. |
---|
1752 | $socket_pos = strpos( $host, ':/' ); |
---|
1753 | if ( $socket_pos !== false ) { |
---|
1754 | $socket = substr( $host, $socket_pos + 1 ); |
---|
1755 | $host = substr( $host, 0, $socket_pos ); |
---|
1756 | } |
---|
1757 | |
---|
1758 | // We need to check for an IPv6 address first. |
---|
1759 | // An IPv6 address will always contain at least two colons. |
---|
1760 | if ( substr_count( $host, ':' ) > 1 ) { |
---|
1761 | $pattern = '#^(?:\[)?(?P<host>[0-9a-fA-F:]+)(?:\]:(?P<port>[\d]+))?#'; |
---|
1762 | $is_ipv6 = true; |
---|
1763 | } else { |
---|
1764 | // We seem to be dealing with an IPv4 address. |
---|
1765 | $pattern = '#^(?P<host>[^:/]*)(?::(?P<port>[\d]+))?#'; |
---|
1766 | } |
---|
1767 | |
---|
1768 | $matches = array(); |
---|
1769 | $result = preg_match( $pattern, $host, $matches ); |
---|
1770 | |
---|
1771 | if ( 1 !== $result ) { |
---|
1772 | // Couldn't parse the address, bail. |
---|
1773 | return false; |
---|
1774 | } |
---|
1775 | |
---|
1776 | $host = ''; |
---|
1777 | foreach ( array( 'host', 'port' ) as $component ) { |
---|
1778 | if ( ! empty( $matches[ $component ] ) ) { |
---|
1779 | $$component = $matches[ $component ]; |
---|
1780 | } |
---|
1781 | } |
---|
1782 | |
---|
1783 | return array( $host, $port, $socket, $is_ipv6 ); |
---|
1784 | } |
---|
1785 | |
---|
1786 | /** |
---|
1787 | * Checks that the connection to the database is still up. If not, try to reconnect. |
---|
1788 | * |
---|
1789 | * If this function is unable to reconnect, it will forcibly die, or if after the |
---|
1790 | * the {@see 'template_redirect'} hook has been fired, return false instead. |
---|
1791 | * |
---|
1792 | * If $allow_bail is false, the lack of database connection will need |
---|
1793 | * to be handled manually. |
---|
1794 | * |
---|
1795 | * @since 3.9.0 |
---|
1796 | * |
---|
1797 | * @param bool $allow_bail Optional. Allows the function to bail. Default true. |
---|
1798 | * @return bool|void True if the connection is up. |
---|
1799 | */ |
---|
1800 | public function check_connection( $allow_bail = true ) { |
---|
1801 | if ( $this->use_mysqli ) { |
---|
1802 | if ( ! empty( $this->dbh ) && mysqli_ping( $this->dbh ) ) { |
---|
1803 | return true; |
---|
1804 | } |
---|
1805 | } else { |
---|
1806 | if ( ! empty( $this->dbh ) && mysql_ping( $this->dbh ) ) { |
---|
1807 | return true; |
---|
1808 | } |
---|
1809 | } |
---|
1810 | |
---|
1811 | $error_reporting = false; |
---|
1812 | |
---|
1813 | // Disable warnings, as we don't want to see a multitude of "unable to connect" messages |
---|
1814 | if ( WP_DEBUG ) { |
---|
1815 | $error_reporting = error_reporting(); |
---|
1816 | error_reporting( $error_reporting & ~E_WARNING ); |
---|
1817 | } |
---|
1818 | |
---|
1819 | for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) { |
---|
1820 | // On the last try, re-enable warnings. We want to see a single instance of the |
---|
1821 | // "unable to connect" message on the bail() screen, if it appears. |
---|
1822 | if ( $this->reconnect_retries === $tries && WP_DEBUG ) { |
---|
1823 | error_reporting( $error_reporting ); |
---|
1824 | } |
---|
1825 | |
---|
1826 | if ( $this->db_connect( false ) ) { |
---|
1827 | if ( $error_reporting ) { |
---|
1828 | error_reporting( $error_reporting ); |
---|
1829 | } |
---|
1830 | |
---|
1831 | return true; |
---|
1832 | } |
---|
1833 | |
---|
1834 | sleep( 1 ); |
---|
1835 | } |
---|
1836 | |
---|
1837 | // If template_redirect has already happened, it's too late for wp_die()/dead_db(). |
---|
1838 | // Let's just return and hope for the best. |
---|
1839 | if ( did_action( 'template_redirect' ) ) { |
---|
1840 | return false; |
---|
1841 | } |
---|
1842 | |
---|
1843 | if ( ! $allow_bail ) { |
---|
1844 | return false; |
---|
1845 | } |
---|
1846 | |
---|
1847 | wp_load_translations_early(); |
---|
1848 | |
---|
1849 | $message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n"; |
---|
1850 | |
---|
1851 | $message .= '<p>' . sprintf( |
---|
1852 | /* translators: %s: Database host. */ |
---|
1853 | __( 'This means that we lost contact with the database server at %s. This could mean your host’s database server is down.' ), |
---|
1854 | '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
---|
1855 | ) . "</p>\n"; |
---|
1856 | |
---|
1857 | $message .= "<ul>\n"; |
---|
1858 | $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
---|
1859 | $message .= '<li>' . __( 'Are you sure the database server is not under particularly heavy load?' ) . "</li>\n"; |
---|
1860 | $message .= "</ul>\n"; |
---|
1861 | |
---|
1862 | $message .= '<p>' . sprintf( |
---|
1863 | /* translators: %s: Support forums URL. */ |
---|
1864 | __( 'If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ), |
---|
1865 | __( 'https://wordpress.org/support/forums/' ) |
---|
1866 | ) . "</p>\n"; |
---|
1867 | |
---|
1868 | // We weren't able to reconnect, so we better bail. |
---|
1869 | $this->bail( $message, 'db_connect_fail' ); |
---|
1870 | |
---|
1871 | // Call dead_db() if bail didn't die, because this database is no more. It has ceased to be (at least temporarily). |
---|
1872 | dead_db(); |
---|
1873 | } |
---|
1874 | |
---|
1875 | /** |
---|
1876 | * Perform a MySQL database query, using current database connection. |
---|
1877 | * |
---|
1878 | * More information can be found on the codex page. |
---|
1879 | * |
---|
1880 | * @since 0.71 |
---|
1881 | * |
---|
1882 | * @param string $query Database query |
---|
1883 | * @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows |
---|
1884 | * affected/selected for all other queries. Boolean false on error. |
---|
1885 | */ |
---|
1886 | public function query( $query ) { |
---|
1887 | if ( ! $this->ready ) { |
---|
1888 | $this->check_current_query = true; |
---|
1889 | return false; |
---|
1890 | } |
---|
1891 | |
---|
1892 | /** |
---|
1893 | * Filters the database query. |
---|
1894 | * |
---|
1895 | * Some queries are made before the plugins have been loaded, |
---|
1896 | * and thus cannot be filtered with this method. |
---|
1897 | * |
---|
1898 | * @since 2.1.0 |
---|
1899 | * |
---|
1900 | * @param string $query Database query. |
---|
1901 | */ |
---|
1902 | if(!$this->suppress_filters) $query = apply_filters( 'query', $query ); |
---|
1903 | |
---|
1904 | $this->flush(); |
---|
1905 | |
---|
1906 | // Log how the function was called |
---|
1907 | $this->func_call = "\$db->query(\"$query\")"; |
---|
1908 | |
---|
1909 | // If we're writing to the database, make sure the query will write safely. |
---|
1910 | if ( $this->check_current_query && ! $this->check_ascii( $query ) ) { |
---|
1911 | $stripped_query = $this->strip_invalid_text_from_query( $query ); |
---|
1912 | // strip_invalid_text_from_query() can perform queries, so we need |
---|
1913 | // to flush again, just to make sure everything is clear. |
---|
1914 | $this->flush(); |
---|
1915 | if ( $stripped_query !== $query ) { |
---|
1916 | $this->insert_id = 0; |
---|
1917 | return false; |
---|
1918 | } |
---|
1919 | } |
---|
1920 | |
---|
1921 | $this->check_current_query = true; |
---|
1922 | |
---|
1923 | // Keep track of the last query for debug. |
---|
1924 | $this->last_query = $query; |
---|
1925 | |
---|
1926 | $this->_do_query( $query ); |
---|
1927 | |
---|
1928 | // MySQL server has gone away, try to reconnect. |
---|
1929 | $mysql_errno = 0; |
---|
1930 | if ( ! empty( $this->dbh ) ) { |
---|
1931 | if ( $this->use_mysqli ) { |
---|
1932 | if ( $this->dbh instanceof mysqli ) { |
---|
1933 | $mysql_errno = mysqli_errno( $this->dbh ); |
---|
1934 | } else { |
---|
1935 | // $dbh is defined, but isn't a real connection. |
---|
1936 | // Something has gone horribly wrong, let's try a reconnect. |
---|
1937 | $mysql_errno = 2006; |
---|
1938 | } |
---|
1939 | } else { |
---|
1940 | if ( is_resource( $this->dbh ) ) { |
---|
1941 | $mysql_errno = mysql_errno( $this->dbh ); |
---|
1942 | } else { |
---|
1943 | $mysql_errno = 2006; |
---|
1944 | } |
---|
1945 | } |
---|
1946 | } |
---|
1947 | |
---|
1948 | if ( empty( $this->dbh ) || 2006 == $mysql_errno ) { |
---|
1949 | if ( $this->check_connection() ) { |
---|
1950 | $this->_do_query( $query ); |
---|
1951 | } else { |
---|
1952 | $this->insert_id = 0; |
---|
1953 | return false; |
---|
1954 | } |
---|
1955 | } |
---|
1956 | |
---|
1957 | // If there is an error then take note of it. |
---|
1958 | if ( $this->use_mysqli ) { |
---|
1959 | if ( $this->dbh instanceof mysqli ) { |
---|
1960 | $this->last_error = mysqli_error( $this->dbh ); |
---|
1961 | } else { |
---|
1962 | $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); |
---|
1963 | } |
---|
1964 | } else { |
---|
1965 | if ( is_resource( $this->dbh ) ) { |
---|
1966 | $this->last_error = mysql_error( $this->dbh ); |
---|
1967 | } else { |
---|
1968 | $this->last_error = __( 'Unable to retrieve the error message from MySQL' ); |
---|
1969 | } |
---|
1970 | } |
---|
1971 | |
---|
1972 | if ( $this->last_error ) { |
---|
1973 | // Clear insert_id on a subsequent failed insert. |
---|
1974 | if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
---|
1975 | $this->insert_id = 0; |
---|
1976 | } |
---|
1977 | |
---|
1978 | $this->print_error(); |
---|
1979 | return false; |
---|
1980 | } |
---|
1981 | |
---|
1982 | if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { |
---|
1983 | $return_val = $this->result; |
---|
1984 | } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { |
---|
1985 | if ( $this->use_mysqli ) { |
---|
1986 | $this->rows_affected = mysqli_affected_rows( $this->dbh ); |
---|
1987 | } else { |
---|
1988 | $this->rows_affected = mysql_affected_rows( $this->dbh ); |
---|
1989 | } |
---|
1990 | // Take note of the insert_id |
---|
1991 | if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
---|
1992 | if ( $this->use_mysqli ) { |
---|
1993 | $this->insert_id = mysqli_insert_id( $this->dbh ); |
---|
1994 | } else { |
---|
1995 | $this->insert_id = mysql_insert_id( $this->dbh ); |
---|
1996 | } |
---|
1997 | } |
---|
1998 | // Return number of rows affected |
---|
1999 | $return_val = $this->rows_affected; |
---|
2000 | } else { |
---|
2001 | $num_rows = 0; |
---|
2002 | if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { |
---|
2003 | while ( $row = mysqli_fetch_object( $this->result ) ) { |
---|
2004 | $this->last_result[ $num_rows ] = $row; |
---|
2005 | $num_rows++; |
---|
2006 | } |
---|
2007 | } elseif ( is_resource( $this->result ) ) { |
---|
2008 | while ( $row = mysql_fetch_object( $this->result ) ) { |
---|
2009 | $this->last_result[ $num_rows ] = $row; |
---|
2010 | $num_rows++; |
---|
2011 | } |
---|
2012 | } |
---|
2013 | |
---|
2014 | // Log number of rows the query returned |
---|
2015 | // and return number of rows selected |
---|
2016 | $this->num_rows = $num_rows; |
---|
2017 | $return_val = $num_rows; |
---|
2018 | } |
---|
2019 | |
---|
2020 | return $return_val; |
---|
2021 | } |
---|
2022 | |
---|
2023 | /** |
---|
2024 | * Internal function to perform the mysql_query() call. |
---|
2025 | * |
---|
2026 | * @since 3.9.0 |
---|
2027 | * |
---|
2028 | * @see wpdb::query() |
---|
2029 | * |
---|
2030 | * @param string $query The query to run. |
---|
2031 | */ |
---|
2032 | private function _do_query( $query ) { |
---|
2033 | if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
---|
2034 | $this->timer_start(); |
---|
2035 | } |
---|
2036 | |
---|
2037 | if ( ! empty( $this->dbh ) && $this->use_mysqli ) { |
---|
2038 | $this->result = mysqli_query( $this->dbh, $query ); |
---|
2039 | } elseif ( ! empty( $this->dbh ) ) { |
---|
2040 | $this->result = mysql_query( $query, $this->dbh ); |
---|
2041 | } |
---|
2042 | $this->num_queries++; |
---|
2043 | |
---|
2044 | if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
---|
2045 | $this->log_query( |
---|
2046 | $query, |
---|
2047 | $this->timer_stop(), |
---|
2048 | $this->get_caller(), |
---|
2049 | $this->time_start, |
---|
2050 | array() |
---|
2051 | ); |
---|
2052 | } |
---|
2053 | } |
---|
2054 | |
---|
2055 | /** |
---|
2056 | * Logs query data. |
---|
2057 | * |
---|
2058 | * @since 5.3.0 |
---|
2059 | * |
---|
2060 | * @param string $query The query's SQL. |
---|
2061 | * @param float $query_time Total time spent on the query, in seconds. |
---|
2062 | * @param string $query_callstack Comma separated list of the calling functions. |
---|
2063 | * @param float $query_start Unix timestamp of the time at the start of the query. |
---|
2064 | * @param array $query_data Custom query data. |
---|
2065 | * } |
---|
2066 | */ |
---|
2067 | public function log_query( $query, $query_time, $query_callstack, $query_start, $query_data ) { |
---|
2068 | /** |
---|
2069 | * Filters the custom query data being logged. |
---|
2070 | * |
---|
2071 | * Caution should be used when modifying any of this data, it is recommended that any additional |
---|
2072 | * information you need to store about a query be added as a new associative entry to the fourth |
---|
2073 | * element $query_data. |
---|
2074 | * |
---|
2075 | * @since 5.3.0 |
---|
2076 | * |
---|
2077 | * @param array $query_data Custom query data. |
---|
2078 | * @param string $query The query's SQL. |
---|
2079 | * @param float $query_time Total time spent on the query, in seconds. |
---|
2080 | * @param string $query_callstack Comma separated list of the calling functions. |
---|
2081 | * @param float $query_start Unix timestamp of the time at the start of the query. |
---|
2082 | */ |
---|
2083 | $query_data = apply_filters( 'log_query_custom_data', $query_data, $query, $query_time, $query_callstack, $query_start ); |
---|
2084 | |
---|
2085 | $this->queries[] = array( |
---|
2086 | $query, |
---|
2087 | $query_time, |
---|
2088 | $query_callstack, |
---|
2089 | $query_start, |
---|
2090 | $query_data, |
---|
2091 | ); |
---|
2092 | } |
---|
2093 | |
---|
2094 | /** |
---|
2095 | * Generates and returns a placeholder escape string for use in queries returned by ::prepare(). |
---|
2096 | * |
---|
2097 | * @since 4.8.3 |
---|
2098 | * |
---|
2099 | * @return string String to escape placeholders. |
---|
2100 | */ |
---|
2101 | public function placeholder_escape() { |
---|
2102 | static $placeholder; |
---|
2103 | |
---|
2104 | if ( ! $placeholder ) { |
---|
2105 | // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. |
---|
2106 | $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; |
---|
2107 | // Old WP installs may not have AUTH_SALT defined. |
---|
2108 | $salt = defined( 'AUTH_SALT' ) && AUTH_SALT ? AUTH_SALT : (string) rand(); |
---|
2109 | |
---|
2110 | $placeholder = '{' . hash_hmac( $algo, uniqid( $salt, true ), $salt ) . '}'; |
---|
2111 | } |
---|
2112 | |
---|
2113 | /* |
---|
2114 | * Add the filter to remove the placeholder escaper. Uses priority 0, so that anything |
---|
2115 | * else attached to this filter will receive the query with the placeholder string removed. |
---|
2116 | */ |
---|
2117 | if ( false === has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) { |
---|
2118 | add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 ); |
---|
2119 | } |
---|
2120 | |
---|
2121 | return $placeholder; |
---|
2122 | } |
---|
2123 | |
---|
2124 | /** |
---|
2125 | * Adds a placeholder escape string, to escape anything that resembles a printf() placeholder. |
---|
2126 | * |
---|
2127 | * @since 4.8.3 |
---|
2128 | * |
---|
2129 | * @param string $query The query to escape. |
---|
2130 | * @return string The query with the placeholder escape string inserted where necessary. |
---|
2131 | */ |
---|
2132 | public function add_placeholder_escape( $query ) { |
---|
2133 | /* |
---|
2134 | * To prevent returning anything that even vaguely resembles a placeholder, |
---|
2135 | * we clobber every % we can find. |
---|
2136 | */ |
---|
2137 | return str_replace( '%', $this->placeholder_escape(), $query ); |
---|
2138 | } |
---|
2139 | |
---|
2140 | /** |
---|
2141 | * Removes the placeholder escape strings from a query. |
---|
2142 | * |
---|
2143 | * @since 4.8.3 |
---|
2144 | * |
---|
2145 | * @param string $query The query from which the placeholder will be removed. |
---|
2146 | * @return string The query with the placeholder removed. |
---|
2147 | */ |
---|
2148 | public function remove_placeholder_escape( $query ) { |
---|
2149 | return str_replace( $this->placeholder_escape(), '%', $query ); |
---|
2150 | } |
---|
2151 | |
---|
2152 | /** |
---|
2153 | * Insert a row into a table. |
---|
2154 | * |
---|
2155 | * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
---|
2156 | * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
---|
2157 | * |
---|
2158 | * @since 2.5.0 |
---|
2159 | * @see wpdb::prepare() |
---|
2160 | * @see wpdb::$field_types |
---|
2161 | * @see wp_set_wpdb_vars() |
---|
2162 | * |
---|
2163 | * @param string $table Table name |
---|
2164 | * @param array $data Data to insert (in column => value pairs). |
---|
2165 | * Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
---|
2166 | * Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case. |
---|
2167 | * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. |
---|
2168 | * If string, that format will be used for all of the values in $data. |
---|
2169 | * A format is one of '%d', '%f', '%s' (integer, float, string). |
---|
2170 | * If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
---|
2171 | * @return int|false The number of rows inserted, or false on error. |
---|
2172 | */ |
---|
2173 | public function insert( $table, $data, $format = null ) { |
---|
2174 | return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' ); |
---|
2175 | } |
---|
2176 | |
---|
2177 | /** |
---|
2178 | * Replace a row into a table. |
---|
2179 | * |
---|
2180 | * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
---|
2181 | * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
---|
2182 | * |
---|
2183 | * @since 3.0.0 |
---|
2184 | * @see wpdb::prepare() |
---|
2185 | * @see wpdb::$field_types |
---|
2186 | * @see wp_set_wpdb_vars() |
---|
2187 | * |
---|
2188 | * @param string $table Table name |
---|
2189 | * @param array $data Data to insert (in column => value pairs). |
---|
2190 | * Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
---|
2191 | * Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case. |
---|
2192 | * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. |
---|
2193 | * If string, that format will be used for all of the values in $data. |
---|
2194 | * A format is one of '%d', '%f', '%s' (integer, float, string). |
---|
2195 | * If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
---|
2196 | * @return int|false The number of rows affected, or false on error. |
---|
2197 | */ |
---|
2198 | public function replace( $table, $data, $format = null ) { |
---|
2199 | return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' ); |
---|
2200 | } |
---|
2201 | |
---|
2202 | /** |
---|
2203 | * Helper function for insert and replace. |
---|
2204 | * |
---|
2205 | * Runs an insert or replace query based on $type argument. |
---|
2206 | * |
---|
2207 | * @since 3.0.0 |
---|
2208 | * @see wpdb::prepare() |
---|
2209 | * @see wpdb::$field_types |
---|
2210 | * @see wp_set_wpdb_vars() |
---|
2211 | * |
---|
2212 | * @param string $table Table name |
---|
2213 | * @param array $data Data to insert (in column => value pairs). |
---|
2214 | * Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
---|
2215 | * Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case. |
---|
2216 | * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. |
---|
2217 | * If string, that format will be used for all of the values in $data. |
---|
2218 | * A format is one of '%d', '%f', '%s' (integer, float, string). |
---|
2219 | * If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
---|
2220 | * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. |
---|
2221 | * @return int|false The number of rows affected, or false on error. |
---|
2222 | */ |
---|
2223 | function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { |
---|
2224 | $this->insert_id = 0; |
---|
2225 | |
---|
2226 | if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) { |
---|
2227 | return false; |
---|
2228 | } |
---|
2229 | |
---|
2230 | $data = $this->process_fields( $table, $data, $format ); |
---|
2231 | if ( false === $data ) { |
---|
2232 | return false; |
---|
2233 | } |
---|
2234 | |
---|
2235 | $formats = array(); |
---|
2236 | $values = array(); |
---|
2237 | foreach ( $data as $value ) { |
---|
2238 | if ( is_null( $value['value'] ) ) { |
---|
2239 | $formats[] = 'NULL'; |
---|
2240 | continue; |
---|
2241 | } |
---|
2242 | |
---|
2243 | $formats[] = $value['format']; |
---|
2244 | $values[] = $value['value']; |
---|
2245 | } |
---|
2246 | |
---|
2247 | $fields = '`' . implode( '`, `', array_keys( $data ) ) . '`'; |
---|
2248 | $formats = implode( ', ', $formats ); |
---|
2249 | |
---|
2250 | $sql = "$type INTO `$table` ($fields) VALUES ($formats)"; |
---|
2251 | |
---|
2252 | $this->check_current_query = false; |
---|
2253 | return $this->query( $this->prepare( $sql, $values ) ); |
---|
2254 | } |
---|
2255 | |
---|
2256 | /** |
---|
2257 | * Update a row in the table |
---|
2258 | * |
---|
2259 | * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) |
---|
2260 | * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
---|
2261 | * |
---|
2262 | * @since 2.5.0 |
---|
2263 | * @see wpdb::prepare() |
---|
2264 | * @see wpdb::$field_types |
---|
2265 | * @see wp_set_wpdb_vars() |
---|
2266 | * |
---|
2267 | * @param string $table Table name |
---|
2268 | * @param array $data Data to update (in column => value pairs). |
---|
2269 | * Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
---|
2270 | * Sending a null value will cause the column to be set to NULL - the corresponding |
---|
2271 | * format is ignored in this case. |
---|
2272 | * @param array $where A named array of WHERE clauses (in column => value pairs). |
---|
2273 | * Multiple clauses will be joined with ANDs. |
---|
2274 | * Both $where columns and $where values should be "raw". |
---|
2275 | * Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case. |
---|
2276 | * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. |
---|
2277 | * If string, that format will be used for all of the values in $data. |
---|
2278 | * A format is one of '%d', '%f', '%s' (integer, float, string). |
---|
2279 | * If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
---|
2280 | * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. |
---|
2281 | * If string, that format will be used for all of the items in $where. |
---|
2282 | * A format is one of '%d', '%f', '%s' (integer, float, string). |
---|
2283 | * If omitted, all values in $where will be treated as strings. |
---|
2284 | * @return int|false The number of rows updated, or false on error. |
---|
2285 | */ |
---|
2286 | public function update( $table, $data, $where, $format = null, $where_format = null ) { |
---|
2287 | if ( ! is_array( $data ) || ! is_array( $where ) ) { |
---|
2288 | return false; |
---|
2289 | } |
---|
2290 | |
---|
2291 | $data = $this->process_fields( $table, $data, $format ); |
---|
2292 | if ( false === $data ) { |
---|
2293 | return false; |
---|
2294 | } |
---|
2295 | $where = $this->process_fields( $table, $where, $where_format ); |
---|
2296 | if ( false === $where ) { |
---|
2297 | return false; |
---|
2298 | } |
---|
2299 | |
---|
2300 | $fields = array(); |
---|
2301 | $conditions = array(); |
---|
2302 | $values = array(); |
---|
2303 | foreach ( $data as $field => $value ) { |
---|
2304 | if ( is_null( $value['value'] ) ) { |
---|
2305 | $fields[] = "`$field` = NULL"; |
---|
2306 | continue; |
---|
2307 | } |
---|
2308 | |
---|
2309 | $fields[] = "`$field` = " . $value['format']; |
---|
2310 | $values[] = $value['value']; |
---|
2311 | } |
---|
2312 | foreach ( $where as $field => $value ) { |
---|
2313 | if ( is_null( $value['value'] ) ) { |
---|
2314 | $conditions[] = "`$field` IS NULL"; |
---|
2315 | continue; |
---|
2316 | } |
---|
2317 | |
---|
2318 | $conditions[] = "`$field` = " . $value['format']; |
---|
2319 | $values[] = $value['value']; |
---|
2320 | } |
---|
2321 | |
---|
2322 | $fields = implode( ', ', $fields ); |
---|
2323 | $conditions = implode( ' AND ', $conditions ); |
---|
2324 | |
---|
2325 | $sql = "UPDATE `$table` SET $fields WHERE $conditions"; |
---|
2326 | |
---|
2327 | $this->check_current_query = false; |
---|
2328 | return $this->query( $this->prepare( $sql, $values ) ); |
---|
2329 | } |
---|
2330 | |
---|
2331 | /** |
---|
2332 | * Delete a row in the table |
---|
2333 | * |
---|
2334 | * wpdb::delete( 'table', array( 'ID' => 1 ) ) |
---|
2335 | * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) |
---|
2336 | * |
---|
2337 | * @since 3.4.0 |
---|
2338 | * @see wpdb::prepare() |
---|
2339 | * @see wpdb::$field_types |
---|
2340 | * @see wp_set_wpdb_vars() |
---|
2341 | * |
---|
2342 | * @param string $table Table name |
---|
2343 | * @param array $where A named array of WHERE clauses (in column => value pairs). |
---|
2344 | * Multiple clauses will be joined with ANDs. |
---|
2345 | * Both $where columns and $where values should be "raw". |
---|
2346 | * Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case. |
---|
2347 | * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. |
---|
2348 | * If string, that format will be used for all of the items in $where. |
---|
2349 | * A format is one of '%d', '%f', '%s' (integer, float, string). |
---|
2350 | * If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types. |
---|
2351 | * @return int|false The number of rows updated, or false on error. |
---|
2352 | */ |
---|
2353 | public function delete( $table, $where, $where_format = null ) { |
---|
2354 | if ( ! is_array( $where ) ) { |
---|
2355 | return false; |
---|
2356 | } |
---|
2357 | |
---|
2358 | $where = $this->process_fields( $table, $where, $where_format ); |
---|
2359 | if ( false === $where ) { |
---|
2360 | return false; |
---|
2361 | } |
---|
2362 | |
---|
2363 | $conditions = array(); |
---|
2364 | $values = array(); |
---|
2365 | foreach ( $where as $field => $value ) { |
---|
2366 | if ( is_null( $value['value'] ) ) { |
---|
2367 | $conditions[] = "`$field` IS NULL"; |
---|
2368 | continue; |
---|
2369 | } |
---|
2370 | |
---|
2371 | $conditions[] = "`$field` = " . $value['format']; |
---|
2372 | $values[] = $value['value']; |
---|
2373 | } |
---|
2374 | |
---|
2375 | $conditions = implode( ' AND ', $conditions ); |
---|
2376 | |
---|
2377 | $sql = "DELETE FROM `$table` WHERE $conditions"; |
---|
2378 | |
---|
2379 | $this->check_current_query = false; |
---|
2380 | return $this->query( $this->prepare( $sql, $values ) ); |
---|
2381 | } |
---|
2382 | |
---|
2383 | /** |
---|
2384 | * Processes arrays of field/value pairs and field formats. |
---|
2385 | * |
---|
2386 | * This is a helper method for wpdb's CRUD methods, which take field/value |
---|
2387 | * pairs for inserts, updates, and where clauses. This method first pairs |
---|
2388 | * each value with a format. Then it determines the charset of that field, |
---|
2389 | * using that to determine if any invalid text would be stripped. If text is |
---|
2390 | * stripped, then field processing is rejected and the query fails. |
---|
2391 | * |
---|
2392 | * @since 4.2.0 |
---|
2393 | * |
---|
2394 | * @param string $table Table name. |
---|
2395 | * @param array $data Field/value pair. |
---|
2396 | * @param mixed $format Format for each field. |
---|
2397 | * @return array|false Returns an array of fields that contain paired values |
---|
2398 | * and formats. Returns false for invalid values. |
---|
2399 | */ |
---|
2400 | protected function process_fields( $table, $data, $format ) { |
---|
2401 | $data = $this->process_field_formats( $data, $format ); |
---|
2402 | if ( false === $data ) { |
---|
2403 | return false; |
---|
2404 | } |
---|
2405 | |
---|
2406 | $data = $this->process_field_charsets( $data, $table ); |
---|
2407 | if ( false === $data ) { |
---|
2408 | return false; |
---|
2409 | } |
---|
2410 | |
---|
2411 | $data = $this->process_field_lengths( $data, $table ); |
---|
2412 | if ( false === $data ) { |
---|
2413 | return false; |
---|
2414 | } |
---|
2415 | |
---|
2416 | $converted_data = $this->strip_invalid_text( $data ); |
---|
2417 | |
---|
2418 | if ( $data !== $converted_data ) { |
---|
2419 | return false; |
---|
2420 | } |
---|
2421 | |
---|
2422 | return $data; |
---|
2423 | } |
---|
2424 | |
---|
2425 | /** |
---|
2426 | * Prepares arrays of value/format pairs as passed to wpdb CRUD methods. |
---|
2427 | * |
---|
2428 | * @since 4.2.0 |
---|
2429 | * |
---|
2430 | * @param array $data Array of fields to values. |
---|
2431 | * @param mixed $format Formats to be mapped to the values in $data. |
---|
2432 | * @return array Array, keyed by field names with values being an array |
---|
2433 | * of 'value' and 'format' keys. |
---|
2434 | */ |
---|
2435 | protected function process_field_formats( $data, $format ) { |
---|
2436 | $formats = (array) $format; |
---|
2437 | $original_formats = $formats; |
---|
2438 | |
---|
2439 | foreach ( $data as $field => $value ) { |
---|
2440 | $value = array( |
---|
2441 | 'value' => $value, |
---|
2442 | 'format' => '%s', |
---|
2443 | ); |
---|
2444 | |
---|
2445 | if ( ! empty( $format ) ) { |
---|
2446 | $value['format'] = array_shift( $formats ); |
---|
2447 | if ( ! $value['format'] ) { |
---|
2448 | $value['format'] = reset( $original_formats ); |
---|
2449 | } |
---|
2450 | } elseif ( isset( $this->field_types[ $field ] ) ) { |
---|
2451 | $value['format'] = $this->field_types[ $field ]; |
---|
2452 | } |
---|
2453 | |
---|
2454 | $data[ $field ] = $value; |
---|
2455 | } |
---|
2456 | |
---|
2457 | return $data; |
---|
2458 | } |
---|
2459 | |
---|
2460 | /** |
---|
2461 | * Adds field charsets to field/value/format arrays generated by |
---|
2462 | * the wpdb::process_field_formats() method. |
---|
2463 | * |
---|
2464 | * @since 4.2.0 |
---|
2465 | * |
---|
2466 | * @param array $data As it comes from the wpdb::process_field_formats() method. |
---|
2467 | * @param string $table Table name. |
---|
2468 | * @return array|false The same array as $data with additional 'charset' keys. |
---|
2469 | */ |
---|
2470 | protected function process_field_charsets( $data, $table ) { |
---|
2471 | foreach ( $data as $field => $value ) { |
---|
2472 | if ( '%d' === $value['format'] || '%f' === $value['format'] ) { |
---|
2473 | /* |
---|
2474 | * We can skip this field if we know it isn't a string. |
---|
2475 | * This checks %d/%f versus ! %s because its sprintf() could take more. |
---|
2476 | */ |
---|
2477 | $value['charset'] = false; |
---|
2478 | } else { |
---|
2479 | $value['charset'] = $this->get_col_charset( $table, $field ); |
---|
2480 | if ( is_wp_error( $value['charset'] ) ) { |
---|
2481 | return false; |
---|
2482 | } |
---|
2483 | } |
---|
2484 | |
---|
2485 | $data[ $field ] = $value; |
---|
2486 | } |
---|
2487 | |
---|
2488 | return $data; |
---|
2489 | } |
---|
2490 | |
---|
2491 | /** |
---|
2492 | * For string fields, record the maximum string length that field can safely save. |
---|
2493 | * |
---|
2494 | * @since 4.2.1 |
---|
2495 | * |
---|
2496 | * @param array $data As it comes from the wpdb::process_field_charsets() method. |
---|
2497 | * @param string $table Table name. |
---|
2498 | * @return array|false The same array as $data with additional 'length' keys, or false if |
---|
2499 | * any of the values were too long for their corresponding field. |
---|
2500 | */ |
---|
2501 | protected function process_field_lengths( $data, $table ) { |
---|
2502 | foreach ( $data as $field => $value ) { |
---|
2503 | if ( '%d' === $value['format'] || '%f' === $value['format'] ) { |
---|
2504 | /* |
---|
2505 | * We can skip this field if we know it isn't a string. |
---|
2506 | * This checks %d/%f versus ! %s because its sprintf() could take more. |
---|
2507 | */ |
---|
2508 | $value['length'] = false; |
---|
2509 | } else { |
---|
2510 | $value['length'] = $this->get_col_length( $table, $field ); |
---|
2511 | if ( is_wp_error( $value['length'] ) ) { |
---|
2512 | return false; |
---|
2513 | } |
---|
2514 | } |
---|
2515 | |
---|
2516 | $data[ $field ] = $value; |
---|
2517 | } |
---|
2518 | |
---|
2519 | return $data; |
---|
2520 | } |
---|
2521 | |
---|
2522 | /** |
---|
2523 | * Retrieve one variable from the database. |
---|
2524 | * |
---|
2525 | * Executes a SQL query and returns the value from the SQL result. |
---|
2526 | * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified. |
---|
2527 | * If $query is null, this function returns the value in the specified column and row from the previous SQL result. |
---|
2528 | * |
---|
2529 | * @since 0.71 |
---|
2530 | * |
---|
2531 | * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query. |
---|
2532 | * @param int $x Optional. Column of value to return. Indexed from 0. |
---|
2533 | * @param int $y Optional. Row of value to return. Indexed from 0. |
---|
2534 | * @return string|null Database query result (as string), or null on failure |
---|
2535 | */ |
---|
2536 | public function get_var( $query = null, $x = 0, $y = 0 ) { |
---|
2537 | $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; |
---|
2538 | |
---|
2539 | if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
---|
2540 | $this->check_current_query = false; |
---|
2541 | } |
---|
2542 | |
---|
2543 | if ( $query ) { |
---|
2544 | $this->query( $query ); |
---|
2545 | } |
---|
2546 | |
---|
2547 | // Extract var out of cached results based x,y vals |
---|
2548 | if ( ! empty( $this->last_result[ $y ] ) ) { |
---|
2549 | $values = array_values( get_object_vars( $this->last_result[ $y ] ) ); |
---|
2550 | } |
---|
2551 | |
---|
2552 | // If there is a value return it else return null |
---|
2553 | return ( isset( $values[ $x ] ) && $values[ $x ] !== '' ) ? $values[ $x ] : null; |
---|
2554 | } |
---|
2555 | |
---|
2556 | /** |
---|
2557 | * Retrieve one row from the database. |
---|
2558 | * |
---|
2559 | * Executes a SQL query and returns the row from the SQL result. |
---|
2560 | * |
---|
2561 | * @since 0.71 |
---|
2562 | * |
---|
2563 | * @param string|null $query SQL query. |
---|
2564 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
---|
2565 | * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT. |
---|
2566 | * @param int $y Optional. Row to return. Indexed from 0. |
---|
2567 | * @return array|object|null|void Database query result in format specified by $output or null on failure |
---|
2568 | */ |
---|
2569 | public function get_row( $query = null, $output = OBJECT, $y = 0 ) { |
---|
2570 | $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
---|
2571 | |
---|
2572 | if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
---|
2573 | $this->check_current_query = false; |
---|
2574 | } |
---|
2575 | |
---|
2576 | if ( $query ) { |
---|
2577 | $this->query( $query ); |
---|
2578 | } else { |
---|
2579 | return null; |
---|
2580 | } |
---|
2581 | |
---|
2582 | if ( ! isset( $this->last_result[ $y ] ) ) { |
---|
2583 | return null; |
---|
2584 | } |
---|
2585 | |
---|
2586 | if ( $output == OBJECT ) { |
---|
2587 | return $this->last_result[ $y ] ? $this->last_result[ $y ] : null; |
---|
2588 | } elseif ( $output == ARRAY_A ) { |
---|
2589 | return $this->last_result[ $y ] ? get_object_vars( $this->last_result[ $y ] ) : null; |
---|
2590 | } elseif ( $output == ARRAY_N ) { |
---|
2591 | return $this->last_result[ $y ] ? array_values( get_object_vars( $this->last_result[ $y ] ) ) : null; |
---|
2592 | } elseif ( strtoupper( $output ) === OBJECT ) { |
---|
2593 | // Back compat for OBJECT being previously case insensitive. |
---|
2594 | return $this->last_result[ $y ] ? $this->last_result[ $y ] : null; |
---|
2595 | } else { |
---|
2596 | $this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' ); |
---|
2597 | } |
---|
2598 | } |
---|
2599 | |
---|
2600 | /** |
---|
2601 | * Retrieve one column from the database. |
---|
2602 | * |
---|
2603 | * Executes a SQL query and returns the column from the SQL result. |
---|
2604 | * If the SQL result contains more than one column, this function returns the column specified. |
---|
2605 | * If $query is null, this function returns the specified column from the previous SQL result. |
---|
2606 | * |
---|
2607 | * @since 0.71 |
---|
2608 | * |
---|
2609 | * @param string|null $query Optional. SQL query. Defaults to previous query. |
---|
2610 | * @param int $x Optional. Column to return. Indexed from 0. |
---|
2611 | * @return array Database query result. Array indexed from 0 by SQL result row number. |
---|
2612 | */ |
---|
2613 | public function get_col( $query = null, $x = 0 ) { |
---|
2614 | if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
---|
2615 | $this->check_current_query = false; |
---|
2616 | } |
---|
2617 | |
---|
2618 | if ( $query ) { |
---|
2619 | $this->query( $query ); |
---|
2620 | } |
---|
2621 | |
---|
2622 | $new_array = array(); |
---|
2623 | // Extract the column values |
---|
2624 | if ( $this->last_result ) { |
---|
2625 | for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { |
---|
2626 | $new_array[ $i ] = $this->get_var( null, $x, $i ); |
---|
2627 | } |
---|
2628 | } |
---|
2629 | return $new_array; |
---|
2630 | } |
---|
2631 | |
---|
2632 | /** |
---|
2633 | * Retrieve an entire SQL result set from the database (i.e., many rows) |
---|
2634 | * |
---|
2635 | * Executes a SQL query and returns the entire SQL result. |
---|
2636 | * |
---|
2637 | * @since 0.71 |
---|
2638 | * |
---|
2639 | * @param string $query SQL query. |
---|
2640 | * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
---|
2641 | * With one of the first three, return an array of rows indexed from 0 by SQL result row number. |
---|
2642 | * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. |
---|
2643 | * With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. |
---|
2644 | * Duplicate keys are discarded. |
---|
2645 | * @return array|object|null Database query results |
---|
2646 | */ |
---|
2647 | public function get_results( $query = null, $output = OBJECT ) { |
---|
2648 | $this->func_call = "\$db->get_results(\"$query\", $output)"; |
---|
2649 | |
---|
2650 | if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
---|
2651 | $this->check_current_query = false; |
---|
2652 | } |
---|
2653 | |
---|
2654 | if ( $query ) { |
---|
2655 | $this->query( $query ); |
---|
2656 | } else { |
---|
2657 | return null; |
---|
2658 | } |
---|
2659 | |
---|
2660 | $new_array = array(); |
---|
2661 | if ( $output == OBJECT ) { |
---|
2662 | // Return an integer-keyed array of row objects |
---|
2663 | return $this->last_result; |
---|
2664 | } elseif ( $output == OBJECT_K ) { |
---|
2665 | // Return an array of row objects with keys from column 1 |
---|
2666 | // (Duplicates are discarded) |
---|
2667 | if ( $this->last_result ) { |
---|
2668 | foreach ( $this->last_result as $row ) { |
---|
2669 | $var_by_ref = get_object_vars( $row ); |
---|
2670 | $key = array_shift( $var_by_ref ); |
---|
2671 | if ( ! isset( $new_array[ $key ] ) ) { |
---|
2672 | $new_array[ $key ] = $row; |
---|
2673 | } |
---|
2674 | } |
---|
2675 | } |
---|
2676 | return $new_array; |
---|
2677 | } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { |
---|
2678 | // Return an integer-keyed array of... |
---|
2679 | if ( $this->last_result ) { |
---|
2680 | foreach ( (array) $this->last_result as $row ) { |
---|
2681 | if ( $output == ARRAY_N ) { |
---|
2682 | // ...integer-keyed row arrays |
---|
2683 | $new_array[] = array_values( get_object_vars( $row ) ); |
---|
2684 | } else { |
---|
2685 | // ...column name-keyed row arrays |
---|
2686 | $new_array[] = get_object_vars( $row ); |
---|
2687 | } |
---|
2688 | } |
---|
2689 | } |
---|
2690 | return $new_array; |
---|
2691 | } elseif ( strtoupper( $output ) === OBJECT ) { |
---|
2692 | // Back compat for OBJECT being previously case insensitive. |
---|
2693 | return $this->last_result; |
---|
2694 | } |
---|
2695 | return null; |
---|
2696 | } |
---|
2697 | |
---|
2698 | /** |
---|
2699 | * Retrieves the character set for the given table. |
---|
2700 | * |
---|
2701 | * @since 4.2.0 |
---|
2702 | * |
---|
2703 | * @param string $table Table name. |
---|
2704 | * @return string|WP_Error Table character set, WP_Error object if it couldn't be found. |
---|
2705 | */ |
---|
2706 | protected function get_table_charset( $table ) { |
---|
2707 | $tablekey = strtolower( $table ); |
---|
2708 | |
---|
2709 | /** |
---|
2710 | * Filters the table charset value before the DB is checked. |
---|
2711 | * |
---|
2712 | * Passing a non-null value to the filter will effectively short-circuit |
---|
2713 | * checking the DB for the charset, returning that value instead. |
---|
2714 | * |
---|
2715 | * @since 4.2.0 |
---|
2716 | * |
---|
2717 | * @param string|null $charset The character set to use. Default null. |
---|
2718 | * @param string $table The name of the table being checked. |
---|
2719 | */ |
---|
2720 | $charset = apply_filters( 'pre_get_table_charset', null, $table ); |
---|
2721 | if ( null !== $charset ) { |
---|
2722 | return $charset; |
---|
2723 | } |
---|
2724 | |
---|
2725 | if ( isset( $this->table_charset[ $tablekey ] ) ) { |
---|
2726 | return $this->table_charset[ $tablekey ]; |
---|
2727 | } |
---|
2728 | |
---|
2729 | $charsets = array(); |
---|
2730 | $columns = array(); |
---|
2731 | |
---|
2732 | $table_parts = explode( '.', $table ); |
---|
2733 | $table = '`' . implode( '`.`', $table_parts ) . '`'; |
---|
2734 | $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" ); |
---|
2735 | if ( ! $results ) { |
---|
2736 | return new WP_Error( 'wpdb_get_table_charset_failure' ); |
---|
2737 | } |
---|
2738 | |
---|
2739 | foreach ( $results as $column ) { |
---|
2740 | $columns[ strtolower( $column->Field ) ] = $column; |
---|
2741 | } |
---|
2742 | |
---|
2743 | $this->col_meta[ $tablekey ] = $columns; |
---|
2744 | |
---|
2745 | foreach ( $columns as $column ) { |
---|
2746 | if ( ! empty( $column->Collation ) ) { |
---|
2747 | list( $charset ) = explode( '_', $column->Collation ); |
---|
2748 | |
---|
2749 | // If the current connection can't support utf8mb4 characters, let's only send 3-byte utf8 characters. |
---|
2750 | if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) { |
---|
2751 | $charset = 'utf8'; |
---|
2752 | } |
---|
2753 | |
---|
2754 | $charsets[ strtolower( $charset ) ] = true; |
---|
2755 | } |
---|
2756 | |
---|
2757 | list( $type ) = explode( '(', $column->Type ); |
---|
2758 | |
---|
2759 | // A binary/blob means the whole query gets treated like this. |
---|
2760 | if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ) ) ) { |
---|
2761 | $this->table_charset[ $tablekey ] = 'binary'; |
---|
2762 | return 'binary'; |
---|
2763 | } |
---|
2764 | } |
---|
2765 | |
---|
2766 | // utf8mb3 is an alias for utf8. |
---|
2767 | if ( isset( $charsets['utf8mb3'] ) ) { |
---|
2768 | $charsets['utf8'] = true; |
---|
2769 | unset( $charsets['utf8mb3'] ); |
---|
2770 | } |
---|
2771 | |
---|
2772 | // Check if we have more than one charset in play. |
---|
2773 | $count = count( $charsets ); |
---|
2774 | if ( 1 === $count ) { |
---|
2775 | $charset = key( $charsets ); |
---|
2776 | } elseif ( 0 === $count ) { |
---|
2777 | // No charsets, assume this table can store whatever. |
---|
2778 | $charset = false; |
---|
2779 | } else { |
---|
2780 | // More than one charset. Remove latin1 if present and recalculate. |
---|
2781 | unset( $charsets['latin1'] ); |
---|
2782 | $count = count( $charsets ); |
---|
2783 | if ( 1 === $count ) { |
---|
2784 | // Only one charset (besides latin1). |
---|
2785 | $charset = key( $charsets ); |
---|
2786 | } elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) { |
---|
2787 | // Two charsets, but they're utf8 and utf8mb4, use utf8. |
---|
2788 | $charset = 'utf8'; |
---|
2789 | } else { |
---|
2790 | // Two mixed character sets. ascii. |
---|
2791 | $charset = 'ascii'; |
---|
2792 | } |
---|
2793 | } |
---|
2794 | |
---|
2795 | $this->table_charset[ $tablekey ] = $charset; |
---|
2796 | return $charset; |
---|
2797 | } |
---|
2798 | |
---|
2799 | /** |
---|
2800 | * Retrieves the character set for the given column. |
---|
2801 | * |
---|
2802 | * @since 4.2.0 |
---|
2803 | * |
---|
2804 | * @param string $table Table name. |
---|
2805 | * @param string $column Column name. |
---|
2806 | * @return string|false|WP_Error Column character set as a string. False if the column has no |
---|
2807 | * character set. WP_Error object if there was an error. |
---|
2808 | */ |
---|
2809 | public function get_col_charset( $table, $column ) { |
---|
2810 | $tablekey = strtolower( $table ); |
---|
2811 | $columnkey = strtolower( $column ); |
---|
2812 | |
---|
2813 | /** |
---|
2814 | * Filters the column charset value before the DB is checked. |
---|
2815 | * |
---|
2816 | * Passing a non-null value to the filter will short-circuit |
---|
2817 | * checking the DB for the charset, returning that value instead. |
---|
2818 | * |
---|
2819 | * @since 4.2.0 |
---|
2820 | * |
---|
2821 | * @param string|null $charset The character set to use. Default null. |
---|
2822 | * @param string $table The name of the table being checked. |
---|
2823 | * @param string $column The name of the column being checked. |
---|
2824 | */ |
---|
2825 | $charset = apply_filters( 'pre_get_col_charset', null, $table, $column ); |
---|
2826 | if ( null !== $charset ) { |
---|
2827 | return $charset; |
---|
2828 | } |
---|
2829 | |
---|
2830 | // Skip this entirely if this isn't a MySQL database. |
---|
2831 | if ( empty( $this->is_mysql ) ) { |
---|
2832 | return false; |
---|
2833 | } |
---|
2834 | |
---|
2835 | if ( empty( $this->table_charset[ $tablekey ] ) ) { |
---|
2836 | // This primes column information for us. |
---|
2837 | $table_charset = $this->get_table_charset( $table ); |
---|
2838 | if ( is_wp_error( $table_charset ) ) { |
---|
2839 | return $table_charset; |
---|
2840 | } |
---|
2841 | } |
---|
2842 | |
---|
2843 | // If still no column information, return the table charset. |
---|
2844 | if ( empty( $this->col_meta[ $tablekey ] ) ) { |
---|
2845 | return $this->table_charset[ $tablekey ]; |
---|
2846 | } |
---|
2847 | |
---|
2848 | // If this column doesn't exist, return the table charset. |
---|
2849 | if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) { |
---|
2850 | return $this->table_charset[ $tablekey ]; |
---|
2851 | } |
---|
2852 | |
---|
2853 | // Return false when it's not a string column. |
---|
2854 | if ( empty( $this->col_meta[ $tablekey ][ $columnkey ]->Collation ) ) { |
---|
2855 | return false; |
---|
2856 | } |
---|
2857 | |
---|
2858 | list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation ); |
---|
2859 | return $charset; |
---|
2860 | } |
---|
2861 | |
---|
2862 | /** |
---|
2863 | * Retrieve the maximum string length allowed in a given column. |
---|
2864 | * The length may either be specified as a byte length or a character length. |
---|
2865 | * |
---|
2866 | * @since 4.2.1 |
---|
2867 | * |
---|
2868 | * @param string $table Table name. |
---|
2869 | * @param string $column Column name. |
---|
2870 | * @return array|false|WP_Error array( 'length' => (int), 'type' => 'byte' | 'char' ) |
---|
2871 | * false if the column has no length (for example, numeric column) |
---|
2872 | * WP_Error object if there was an error. |
---|
2873 | */ |
---|
2874 | public function get_col_length( $table, $column ) { |
---|
2875 | $tablekey = strtolower( $table ); |
---|
2876 | $columnkey = strtolower( $column ); |
---|
2877 | |
---|
2878 | // Skip this entirely if this isn't a MySQL database. |
---|
2879 | if ( empty( $this->is_mysql ) ) { |
---|
2880 | return false; |
---|
2881 | } |
---|
2882 | |
---|
2883 | if ( empty( $this->col_meta[ $tablekey ] ) ) { |
---|
2884 | // This primes column information for us. |
---|
2885 | $table_charset = $this->get_table_charset( $table ); |
---|
2886 | if ( is_wp_error( $table_charset ) ) { |
---|
2887 | return $table_charset; |
---|
2888 | } |
---|
2889 | } |
---|
2890 | |
---|
2891 | if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) { |
---|
2892 | return false; |
---|
2893 | } |
---|
2894 | |
---|
2895 | $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type ); |
---|
2896 | |
---|
2897 | $type = strtolower( $typeinfo[0] ); |
---|
2898 | if ( ! empty( $typeinfo[1] ) ) { |
---|
2899 | $length = trim( $typeinfo[1], ')' ); |
---|
2900 | } else { |
---|
2901 | $length = false; |
---|
2902 | } |
---|
2903 | |
---|
2904 | switch ( $type ) { |
---|
2905 | case 'char': |
---|
2906 | case 'varchar': |
---|
2907 | return array( |
---|
2908 | 'type' => 'char', |
---|
2909 | 'length' => (int) $length, |
---|
2910 | ); |
---|
2911 | |
---|
2912 | case 'binary': |
---|
2913 | case 'varbinary': |
---|
2914 | return array( |
---|
2915 | 'type' => 'byte', |
---|
2916 | 'length' => (int) $length, |
---|
2917 | ); |
---|
2918 | |
---|
2919 | case 'tinyblob': |
---|
2920 | case 'tinytext': |
---|
2921 | return array( |
---|
2922 | 'type' => 'byte', |
---|
2923 | 'length' => 255, // 2^8 - 1 |
---|
2924 | ); |
---|
2925 | |
---|
2926 | case 'blob': |
---|
2927 | case 'text': |
---|
2928 | return array( |
---|
2929 | 'type' => 'byte', |
---|
2930 | 'length' => 65535, // 2^16 - 1 |
---|
2931 | ); |
---|
2932 | |
---|
2933 | case 'mediumblob': |
---|
2934 | case 'mediumtext': |
---|
2935 | return array( |
---|
2936 | 'type' => 'byte', |
---|
2937 | 'length' => 16777215, // 2^24 - 1 |
---|
2938 | ); |
---|
2939 | |
---|
2940 | case 'longblob': |
---|
2941 | case 'longtext': |
---|
2942 | return array( |
---|
2943 | 'type' => 'byte', |
---|
2944 | 'length' => 4294967295, // 2^32 - 1 |
---|
2945 | ); |
---|
2946 | |
---|
2947 | default: |
---|
2948 | return false; |
---|
2949 | } |
---|
2950 | } |
---|
2951 | |
---|
2952 | /** |
---|
2953 | * Check if a string is ASCII. |
---|
2954 | * |
---|
2955 | * The negative regex is faster for non-ASCII strings, as it allows |
---|
2956 | * the search to finish as soon as it encounters a non-ASCII character. |
---|
2957 | * |
---|
2958 | * @since 4.2.0 |
---|
2959 | * |
---|
2960 | * @param string $string String to check. |
---|
2961 | * @return bool True if ASCII, false if not. |
---|
2962 | */ |
---|
2963 | protected function check_ascii( $string ) { |
---|
2964 | if ( function_exists( 'mb_check_encoding' ) ) { |
---|
2965 | if ( mb_check_encoding( $string, 'ASCII' ) ) { |
---|
2966 | return true; |
---|
2967 | } |
---|
2968 | } elseif ( ! preg_match( '/[^\x00-\x7F]/', $string ) ) { |
---|
2969 | return true; |
---|
2970 | } |
---|
2971 | |
---|
2972 | return false; |
---|
2973 | } |
---|
2974 | |
---|
2975 | /** |
---|
2976 | * Check if the query is accessing a collation considered safe on the current version of MySQL. |
---|
2977 | * |
---|
2978 | * @since 4.2.0 |
---|
2979 | * |
---|
2980 | * @param string $query The query to check. |
---|
2981 | * @return bool True if the collation is safe, false if it isn't. |
---|
2982 | */ |
---|
2983 | protected function check_safe_collation( $query ) { |
---|
2984 | if ( $this->checking_collation ) { |
---|
2985 | return true; |
---|
2986 | } |
---|
2987 | |
---|
2988 | // We don't need to check the collation for queries that don't read data. |
---|
2989 | $query = ltrim( $query, "\r\n\t (" ); |
---|
2990 | if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $query ) ) { |
---|
2991 | return true; |
---|
2992 | } |
---|
2993 | |
---|
2994 | // All-ASCII queries don't need extra checking. |
---|
2995 | if ( $this->check_ascii( $query ) ) { |
---|
2996 | return true; |
---|
2997 | } |
---|
2998 | |
---|
2999 | $table = $this->get_table_from_query( $query ); |
---|
3000 | if ( ! $table ) { |
---|
3001 | return false; |
---|
3002 | } |
---|
3003 | |
---|
3004 | $this->checking_collation = true; |
---|
3005 | $collation = $this->get_table_charset( $table ); |
---|
3006 | $this->checking_collation = false; |
---|
3007 | |
---|
3008 | // Tables with no collation, or latin1 only, don't need extra checking. |
---|
3009 | if ( false === $collation || 'latin1' === $collation ) { |
---|
3010 | return true; |
---|
3011 | } |
---|
3012 | |
---|
3013 | $table = strtolower( $table ); |
---|
3014 | if ( empty( $this->col_meta[ $table ] ) ) { |
---|
3015 | return false; |
---|
3016 | } |
---|
3017 | |
---|
3018 | // If any of the columns don't have one of these collations, it needs more sanity checking. |
---|
3019 | foreach ( $this->col_meta[ $table ] as $col ) { |
---|
3020 | if ( empty( $col->Collation ) ) { |
---|
3021 | continue; |
---|
3022 | } |
---|
3023 | |
---|
3024 | if ( ! in_array( $col->Collation, array( 'utf8_general_ci', 'utf8_bin', 'utf8mb4_general_ci', 'utf8mb4_bin' ), true ) ) { |
---|
3025 | return false; |
---|
3026 | } |
---|
3027 | } |
---|
3028 | |
---|
3029 | return true; |
---|
3030 | } |
---|
3031 | |
---|
3032 | /** |
---|
3033 | * Strips any invalid characters based on value/charset pairs. |
---|
3034 | * |
---|
3035 | * @since 4.2.0 |
---|
3036 | * |
---|
3037 | * @param array $data Array of value arrays. Each value array has the keys |
---|
3038 | * 'value' and 'charset'. An optional 'ascii' key can be |
---|
3039 | * set to false to avoid redundant ASCII checks. |
---|
3040 | * @return array|WP_Error The $data parameter, with invalid characters removed from |
---|
3041 | * each value. This works as a passthrough: any additional keys |
---|
3042 | * such as 'field' are retained in each value array. If we cannot |
---|
3043 | * remove invalid characters, a WP_Error object is returned. |
---|
3044 | */ |
---|
3045 | protected function strip_invalid_text( $data ) { |
---|
3046 | $db_check_string = false; |
---|
3047 | |
---|
3048 | foreach ( $data as &$value ) { |
---|
3049 | $charset = $value['charset']; |
---|
3050 | |
---|
3051 | if ( is_array( $value['length'] ) ) { |
---|
3052 | $length = $value['length']['length']; |
---|
3053 | $truncate_by_byte_length = 'byte' === $value['length']['type']; |
---|
3054 | } else { |
---|
3055 | $length = false; |
---|
3056 | // Since we have no length, we'll never truncate. |
---|
3057 | // Initialize the variable to false. true would take us |
---|
3058 | // through an unnecessary (for this case) codepath below. |
---|
3059 | $truncate_by_byte_length = false; |
---|
3060 | } |
---|
3061 | |
---|
3062 | // There's no charset to work with. |
---|
3063 | if ( false === $charset ) { |
---|
3064 | continue; |
---|
3065 | } |
---|
3066 | |
---|
3067 | // Column isn't a string. |
---|
3068 | if ( ! is_string( $value['value'] ) ) { |
---|
3069 | continue; |
---|
3070 | } |
---|
3071 | |
---|
3072 | $needs_validation = true; |
---|
3073 | if ( |
---|
3074 | // latin1 can store any byte sequence |
---|
3075 | 'latin1' === $charset |
---|
3076 | || |
---|
3077 | // ASCII is always OK. |
---|
3078 | ( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) ) |
---|
3079 | ) { |
---|
3080 | $truncate_by_byte_length = true; |
---|
3081 | $needs_validation = false; |
---|
3082 | } |
---|
3083 | |
---|
3084 | if ( $truncate_by_byte_length ) { |
---|
3085 | mbstring_binary_safe_encoding(); |
---|
3086 | if ( false !== $length && strlen( $value['value'] ) > $length ) { |
---|
3087 | $value['value'] = substr( $value['value'], 0, $length ); |
---|
3088 | } |
---|
3089 | reset_mbstring_encoding(); |
---|
3090 | |
---|
3091 | if ( ! $needs_validation ) { |
---|
3092 | continue; |
---|
3093 | } |
---|
3094 | } |
---|
3095 | |
---|
3096 | // utf8 can be handled by regex, which is a bunch faster than a DB lookup. |
---|
3097 | if ( ( 'utf8' === $charset || 'utf8mb3' === $charset || 'utf8mb4' === $charset ) && function_exists( 'mb_strlen' ) ) { |
---|
3098 | $regex = '/ |
---|
3099 | ( |
---|
3100 | (?: [\x00-\x7F] # single-byte sequences 0xxxxxxx |
---|
3101 | | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
---|
3102 | | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
---|
3103 | | [\xE1-\xEC][\x80-\xBF]{2} |
---|
3104 | | \xED[\x80-\x9F][\x80-\xBF] |
---|
3105 | | [\xEE-\xEF][\x80-\xBF]{2}'; |
---|
3106 | |
---|
3107 | if ( 'utf8mb4' === $charset ) { |
---|
3108 | $regex .= ' |
---|
3109 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
---|
3110 | | [\xF1-\xF3][\x80-\xBF]{3} |
---|
3111 | | \xF4[\x80-\x8F][\x80-\xBF]{2} |
---|
3112 | '; |
---|
3113 | } |
---|
3114 | |
---|
3115 | $regex .= '){1,40} # ...one or more times |
---|
3116 | ) |
---|
3117 | | . # anything else |
---|
3118 | /x'; |
---|
3119 | $value['value'] = preg_replace( $regex, '$1', $value['value'] ); |
---|
3120 | |
---|
3121 | if ( false !== $length && mb_strlen( $value['value'], 'UTF-8' ) > $length ) { |
---|
3122 | $value['value'] = mb_substr( $value['value'], 0, $length, 'UTF-8' ); |
---|
3123 | } |
---|
3124 | continue; |
---|
3125 | } |
---|
3126 | |
---|
3127 | // We couldn't use any local conversions, send it to the DB. |
---|
3128 | $value['db'] = true; |
---|
3129 | $db_check_string = true; |
---|
3130 | } |
---|
3131 | unset( $value ); // Remove by reference. |
---|
3132 | |
---|
3133 | if ( $db_check_string ) { |
---|
3134 | $queries = array(); |
---|
3135 | foreach ( $data as $col => $value ) { |
---|
3136 | if ( ! empty( $value['db'] ) ) { |
---|
3137 | // We're going to need to truncate by characters or bytes, depending on the length value we have. |
---|
3138 | if ( isset( $value['length']['type'] ) && 'byte' === $value['length']['type'] ) { |
---|
3139 | // Using binary causes LEFT() to truncate by bytes. |
---|
3140 | $charset = 'binary'; |
---|
3141 | } else { |
---|
3142 | $charset = $value['charset']; |
---|
3143 | } |
---|
3144 | |
---|
3145 | if ( $this->charset ) { |
---|
3146 | $connection_charset = $this->charset; |
---|
3147 | } else { |
---|
3148 | if ( $this->use_mysqli ) { |
---|
3149 | $connection_charset = mysqli_character_set_name( $this->dbh ); |
---|
3150 | } else { |
---|
3151 | $connection_charset = mysql_client_encoding(); |
---|
3152 | } |
---|
3153 | } |
---|
3154 | |
---|
3155 | if ( is_array( $value['length'] ) ) { |
---|
3156 | $length = sprintf( '%.0f', $value['length']['length'] ); |
---|
3157 | $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING $connection_charset )", $value['value'] ); |
---|
3158 | } elseif ( 'binary' !== $charset ) { |
---|
3159 | // If we don't have a length, there's no need to convert binary - it will always return the same result. |
---|
3160 | $queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING $connection_charset )", $value['value'] ); |
---|
3161 | } |
---|
3162 | |
---|
3163 | unset( $data[ $col ]['db'] ); |
---|
3164 | } |
---|
3165 | } |
---|
3166 | |
---|
3167 | $sql = array(); |
---|
3168 | foreach ( $queries as $column => $query ) { |
---|
3169 | if ( ! $query ) { |
---|
3170 | continue; |
---|
3171 | } |
---|
3172 | |
---|
3173 | $sql[] = $query . " AS x_$column"; |
---|
3174 | } |
---|
3175 | |
---|
3176 | $this->check_current_query = false; |
---|
3177 | $row = $this->get_row( 'SELECT ' . implode( ', ', $sql ), ARRAY_A ); |
---|
3178 | if ( ! $row ) { |
---|
3179 | return new WP_Error( 'wpdb_strip_invalid_text_failure' ); |
---|
3180 | } |
---|
3181 | |
---|
3182 | foreach ( array_keys( $data ) as $column ) { |
---|
3183 | if ( isset( $row[ "x_$column" ] ) ) { |
---|
3184 | $data[ $column ]['value'] = $row[ "x_$column" ]; |
---|
3185 | } |
---|
3186 | } |
---|
3187 | } |
---|
3188 | |
---|
3189 | return $data; |
---|
3190 | } |
---|
3191 | |
---|
3192 | /** |
---|
3193 | * Strips any invalid characters from the query. |
---|
3194 | * |
---|
3195 | * @since 4.2.0 |
---|
3196 | * |
---|
3197 | * @param string $query Query to convert. |
---|
3198 | * @return string|WP_Error The converted query, or a WP_Error object if the conversion fails. |
---|
3199 | */ |
---|
3200 | protected function strip_invalid_text_from_query( $query ) { |
---|
3201 | // We don't need to check the collation for queries that don't read data. |
---|
3202 | $trimmed_query = ltrim( $query, "\r\n\t (" ); |
---|
3203 | if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $trimmed_query ) ) { |
---|
3204 | return $query; |
---|
3205 | } |
---|
3206 | |
---|
3207 | $table = $this->get_table_from_query( $query ); |
---|
3208 | if ( $table ) { |
---|
3209 | $charset = $this->get_table_charset( $table ); |
---|
3210 | if ( is_wp_error( $charset ) ) { |
---|
3211 | return $charset; |
---|
3212 | } |
---|
3213 | |
---|
3214 | // We can't reliably strip text from tables containing binary/blob columns |
---|
3215 | if ( 'binary' === $charset ) { |
---|
3216 | return $query; |
---|
3217 | } |
---|
3218 | } else { |
---|
3219 | $charset = $this->charset; |
---|
3220 | } |
---|
3221 | |
---|
3222 | $data = array( |
---|
3223 | 'value' => $query, |
---|
3224 | 'charset' => $charset, |
---|
3225 | 'ascii' => false, |
---|
3226 | 'length' => false, |
---|
3227 | ); |
---|
3228 | |
---|
3229 | $data = $this->strip_invalid_text( array( $data ) ); |
---|
3230 | if ( is_wp_error( $data ) ) { |
---|
3231 | return $data; |
---|
3232 | } |
---|
3233 | |
---|
3234 | return $data[0]['value']; |
---|
3235 | } |
---|
3236 | |
---|
3237 | /** |
---|
3238 | * Strips any invalid characters from the string for a given table and column. |
---|
3239 | * |
---|
3240 | * @since 4.2.0 |
---|
3241 | * |
---|
3242 | * @param string $table Table name. |
---|
3243 | * @param string $column Column name. |
---|
3244 | * @param string $value The text to check. |
---|
3245 | * @return string|WP_Error The converted string, or a WP_Error object if the conversion fails. |
---|
3246 | */ |
---|
3247 | public function strip_invalid_text_for_column( $table, $column, $value ) { |
---|
3248 | if ( ! is_string( $value ) ) { |
---|
3249 | return $value; |
---|
3250 | } |
---|
3251 | |
---|
3252 | $charset = $this->get_col_charset( $table, $column ); |
---|
3253 | if ( ! $charset ) { |
---|
3254 | // Not a string column. |
---|
3255 | return $value; |
---|
3256 | } elseif ( is_wp_error( $charset ) ) { |
---|
3257 | // Bail on real errors. |
---|
3258 | return $charset; |
---|
3259 | } |
---|
3260 | |
---|
3261 | $data = array( |
---|
3262 | $column => array( |
---|
3263 | 'value' => $value, |
---|
3264 | 'charset' => $charset, |
---|
3265 | 'length' => $this->get_col_length( $table, $column ), |
---|
3266 | ), |
---|
3267 | ); |
---|
3268 | |
---|
3269 | $data = $this->strip_invalid_text( $data ); |
---|
3270 | if ( is_wp_error( $data ) ) { |
---|
3271 | return $data; |
---|
3272 | } |
---|
3273 | |
---|
3274 | return $data[ $column ]['value']; |
---|
3275 | } |
---|
3276 | |
---|
3277 | /** |
---|
3278 | * Find the first table name referenced in a query. |
---|
3279 | * |
---|
3280 | * @since 4.2.0 |
---|
3281 | * |
---|
3282 | * @param string $query The query to search. |
---|
3283 | * @return string|false $table The table name found, or false if a table couldn't be found. |
---|
3284 | */ |
---|
3285 | protected function get_table_from_query( $query ) { |
---|
3286 | // Remove characters that can legally trail the table name. |
---|
3287 | $query = rtrim( $query, ';/-#' ); |
---|
3288 | |
---|
3289 | // Allow (select...) union [...] style queries. Use the first query's table name. |
---|
3290 | $query = ltrim( $query, "\r\n\t (" ); |
---|
3291 | |
---|
3292 | // Strip everything between parentheses except nested selects. |
---|
3293 | $query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', $query ); |
---|
3294 | |
---|
3295 | // Quickly match most common queries. |
---|
3296 | if ( preg_match( |
---|
3297 | '/^\s*(?:' |
---|
3298 | . 'SELECT.*?\s+FROM' |
---|
3299 | . '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?' |
---|
3300 | . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?' |
---|
3301 | . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?' |
---|
3302 | . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:.+?FROM)?' |
---|
3303 | . ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is', |
---|
3304 | $query, |
---|
3305 | $maybe |
---|
3306 | ) ) { |
---|
3307 | return str_replace( '`', '', $maybe[1] ); |
---|
3308 | } |
---|
3309 | |
---|
3310 | // SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts' |
---|
3311 | if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) { |
---|
3312 | return $maybe[2]; |
---|
3313 | } |
---|
3314 | |
---|
3315 | // SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%' |
---|
3316 | // This quoted LIKE operand seldom holds a full table name. |
---|
3317 | // It is usually a pattern for matching a prefix so we just |
---|
3318 | // strip the trailing % and unescape the _ to get 'wp_123_' |
---|
3319 | // which drop-ins can use for routing these SQL statements. |
---|
3320 | if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) { |
---|
3321 | return str_replace( '\\_', '_', $maybe[2] ); |
---|
3322 | } |
---|
3323 | |
---|
3324 | // Big pattern for the rest of the table-related queries. |
---|
3325 | if ( preg_match( |
---|
3326 | '/^\s*(?:' |
---|
3327 | . '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM' |
---|
3328 | . '|DESCRIBE|DESC|EXPLAIN|HANDLER' |
---|
3329 | . '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?' |
---|
3330 | . '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE' |
---|
3331 | . '|TRUNCATE(?:\s+TABLE)?' |
---|
3332 | . '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?' |
---|
3333 | . '|ALTER(?:\s+IGNORE)?\s+TABLE' |
---|
3334 | . '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?' |
---|
3335 | . '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON' |
---|
3336 | . '|DROP\s+INDEX.*\s+ON' |
---|
3337 | . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE' |
---|
3338 | . '|(?:GRANT|REVOKE).*ON\s+TABLE' |
---|
3339 | . '|SHOW\s+(?:.*FROM|.*TABLE)' |
---|
3340 | . ')\s+\(*\s*((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', |
---|
3341 | $query, |
---|
3342 | $maybe |
---|
3343 | ) ) { |
---|
3344 | return str_replace( '`', '', $maybe[1] ); |
---|
3345 | } |
---|
3346 | |
---|
3347 | return false; |
---|
3348 | } |
---|
3349 | |
---|
3350 | /** |
---|
3351 | * Load the column metadata from the last query. |
---|
3352 | * |
---|
3353 | * @since 3.5.0 |
---|
3354 | */ |
---|
3355 | protected function load_col_info() { |
---|
3356 | if ( $this->col_info ) { |
---|
3357 | return; |
---|
3358 | } |
---|
3359 | |
---|
3360 | if ( $this->use_mysqli ) { |
---|
3361 | $num_fields = mysqli_num_fields( $this->result ); |
---|
3362 | for ( $i = 0; $i < $num_fields; $i++ ) { |
---|
3363 | $this->col_info[ $i ] = mysqli_fetch_field( $this->result ); |
---|
3364 | } |
---|
3365 | } else { |
---|
3366 | $num_fields = mysql_num_fields( $this->result ); |
---|
3367 | for ( $i = 0; $i < $num_fields; $i++ ) { |
---|
3368 | $this->col_info[ $i ] = mysql_fetch_field( $this->result, $i ); |
---|
3369 | } |
---|
3370 | } |
---|
3371 | } |
---|
3372 | |
---|
3373 | /** |
---|
3374 | * Retrieve column metadata from the last query. |
---|
3375 | * |
---|
3376 | * @since 0.71 |
---|
3377 | * |
---|
3378 | * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill |
---|
3379 | * @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type |
---|
3380 | * @return mixed Column Results |
---|
3381 | */ |
---|
3382 | public function get_col_info( $info_type = 'name', $col_offset = -1 ) { |
---|
3383 | $this->load_col_info(); |
---|
3384 | |
---|
3385 | if ( $this->col_info ) { |
---|
3386 | if ( $col_offset == -1 ) { |
---|
3387 | $i = 0; |
---|
3388 | $new_array = array(); |
---|
3389 | foreach ( (array) $this->col_info as $col ) { |
---|
3390 | $new_array[ $i ] = $col->{$info_type}; |
---|
3391 | $i++; |
---|
3392 | } |
---|
3393 | return $new_array; |
---|
3394 | } else { |
---|
3395 | return $this->col_info[ $col_offset ]->{$info_type}; |
---|
3396 | } |
---|
3397 | } |
---|
3398 | } |
---|
3399 | |
---|
3400 | /** |
---|
3401 | * Starts the timer, for debugging purposes. |
---|
3402 | * |
---|
3403 | * @since 1.5.0 |
---|
3404 | * |
---|
3405 | * @return true |
---|
3406 | */ |
---|
3407 | public function timer_start() { |
---|
3408 | $this->time_start = microtime( true ); |
---|
3409 | return true; |
---|
3410 | } |
---|
3411 | |
---|
3412 | /** |
---|
3413 | * Stops the debugging timer. |
---|
3414 | * |
---|
3415 | * @since 1.5.0 |
---|
3416 | * |
---|
3417 | * @return float Total time spent on the query, in seconds |
---|
3418 | */ |
---|
3419 | public function timer_stop() { |
---|
3420 | return ( microtime( true ) - $this->time_start ); |
---|
3421 | } |
---|
3422 | |
---|
3423 | /** |
---|
3424 | * Wraps errors in a nice header and footer and dies. |
---|
3425 | * |
---|
3426 | * Will not die if wpdb::$show_errors is false. |
---|
3427 | * |
---|
3428 | * @since 1.5.0 |
---|
3429 | * |
---|
3430 | * @param string $message The Error message |
---|
3431 | * @param string $error_code Optional. A Computer readable string to identify the error. |
---|
3432 | * @return false|void |
---|
3433 | */ |
---|
3434 | public function bail( $message, $error_code = '500' ) { |
---|
3435 | if ( $this->show_errors ) { |
---|
3436 | $error = ''; |
---|
3437 | |
---|
3438 | if ( $this->use_mysqli ) { |
---|
3439 | if ( $this->dbh instanceof mysqli ) { |
---|
3440 | $error = mysqli_error( $this->dbh ); |
---|
3441 | } elseif ( mysqli_connect_errno() ) { |
---|
3442 | $error = mysqli_connect_error(); |
---|
3443 | } |
---|
3444 | } else { |
---|
3445 | if ( is_resource( $this->dbh ) ) { |
---|
3446 | $error = mysql_error( $this->dbh ); |
---|
3447 | } else { |
---|
3448 | $error = mysql_error(); |
---|
3449 | } |
---|
3450 | } |
---|
3451 | |
---|
3452 | if ( $error ) { |
---|
3453 | $message = '<p><code>' . $error . "</code></p>\n" . $message; |
---|
3454 | } |
---|
3455 | |
---|
3456 | wp_die( $message ); |
---|
3457 | } else { |
---|
3458 | if ( class_exists( 'WP_Error', false ) ) { |
---|
3459 | $this->error = new WP_Error( $error_code, $message ); |
---|
3460 | } else { |
---|
3461 | $this->error = $message; |
---|
3462 | } |
---|
3463 | |
---|
3464 | return false; |
---|
3465 | } |
---|
3466 | } |
---|
3467 | |
---|
3468 | |
---|
3469 | /** |
---|
3470 | * Closes the current database connection. |
---|
3471 | * |
---|
3472 | * @since 4.5.0 |
---|
3473 | * |
---|
3474 | * @return bool True if the connection was successfully closed, false if it wasn't, |
---|
3475 | * or the connection doesn't exist. |
---|
3476 | */ |
---|
3477 | public function close() { |
---|
3478 | if ( ! $this->dbh ) { |
---|
3479 | return false; |
---|
3480 | } |
---|
3481 | |
---|
3482 | if ( $this->use_mysqli ) { |
---|
3483 | $closed = mysqli_close( $this->dbh ); |
---|
3484 | } else { |
---|
3485 | $closed = mysql_close( $this->dbh ); |
---|
3486 | } |
---|
3487 | |
---|
3488 | if ( $closed ) { |
---|
3489 | $this->dbh = null; |
---|
3490 | $this->ready = false; |
---|
3491 | $this->has_connected = false; |
---|
3492 | } |
---|
3493 | |
---|
3494 | return $closed; |
---|
3495 | } |
---|
3496 | |
---|
3497 | /** |
---|
3498 | * Whether MySQL database is at least the required minimum version. |
---|
3499 | * |
---|
3500 | * @since 2.5.0 |
---|
3501 | * |
---|
3502 | * @global string $wp_version |
---|
3503 | * @global string $required_mysql_version |
---|
3504 | * |
---|
3505 | * @return WP_Error|void |
---|
3506 | */ |
---|
3507 | public function check_database_version() { |
---|
3508 | global $wp_version, $required_mysql_version; |
---|
3509 | // Make sure the server has the required MySQL version |
---|
3510 | if ( version_compare( $this->db_version(), $required_mysql_version, '<' ) ) { |
---|
3511 | /* translators: 1: WordPress version number, 2: Minimum required MySQL version number. */ |
---|
3512 | return new WP_Error( 'database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ) ); |
---|
3513 | } |
---|
3514 | } |
---|
3515 | |
---|
3516 | /** |
---|
3517 | * Whether the database supports collation. |
---|
3518 | * |
---|
3519 | * Called when WordPress is generating the table scheme. |
---|
3520 | * |
---|
3521 | * Use `wpdb::has_cap( 'collation' )`. |
---|
3522 | * |
---|
3523 | * @since 2.5.0 |
---|
3524 | * @deprecated 3.5.0 Use wpdb::has_cap() |
---|
3525 | * |
---|
3526 | * @return bool True if collation is supported, false if version does not |
---|
3527 | */ |
---|
3528 | public function supports_collation() { |
---|
3529 | _deprecated_function( __FUNCTION__, '3.5.0', 'wpdb::has_cap( \'collation\' )' ); |
---|
3530 | return $this->has_cap( 'collation' ); |
---|
3531 | } |
---|
3532 | |
---|
3533 | /** |
---|
3534 | * The database character collate. |
---|
3535 | * |
---|
3536 | * @since 3.5.0 |
---|
3537 | * |
---|
3538 | * @return string The database character collate. |
---|
3539 | */ |
---|
3540 | public function get_charset_collate() { |
---|
3541 | $charset_collate = ''; |
---|
3542 | |
---|
3543 | if ( ! empty( $this->charset ) ) { |
---|
3544 | $charset_collate = "DEFAULT CHARACTER SET $this->charset"; |
---|
3545 | } |
---|
3546 | if ( ! empty( $this->collate ) ) { |
---|
3547 | $charset_collate .= " COLLATE $this->collate"; |
---|
3548 | } |
---|
3549 | |
---|
3550 | return $charset_collate; |
---|
3551 | } |
---|
3552 | |
---|
3553 | /** |
---|
3554 | * Determine if a database supports a particular feature. |
---|
3555 | * |
---|
3556 | * @since 2.7.0 |
---|
3557 | * @since 4.1.0 Added support for the 'utf8mb4' feature. |
---|
3558 | * @since 4.6.0 Added support for the 'utf8mb4_520' feature. |
---|
3559 | * |
---|
3560 | * @see wpdb::db_version() |
---|
3561 | * |
---|
3562 | * @param string $db_cap The feature to check for. Accepts 'collation', |
---|
3563 | * 'group_concat', 'subqueries', 'set_charset', |
---|
3564 | * 'utf8mb4', or 'utf8mb4_520'. |
---|
3565 | * @return int|false Whether the database feature is supported, false otherwise. |
---|
3566 | */ |
---|
3567 | public function has_cap( $db_cap ) { |
---|
3568 | $version = $this->db_version(); |
---|
3569 | |
---|
3570 | switch ( strtolower( $db_cap ) ) { |
---|
3571 | case 'collation': // @since 2.5.0 |
---|
3572 | case 'group_concat': // @since 2.7.0 |
---|
3573 | case 'subqueries': // @since 2.7.0 |
---|
3574 | return version_compare( $version, '4.1', '>=' ); |
---|
3575 | case 'set_charset': |
---|
3576 | return version_compare( $version, '5.0.7', '>=' ); |
---|
3577 | case 'utf8mb4': // @since 4.1.0 |
---|
3578 | if ( version_compare( $version, '5.5.3', '<' ) ) { |
---|
3579 | return false; |
---|
3580 | } |
---|
3581 | if ( $this->use_mysqli ) { |
---|
3582 | $client_version = mysqli_get_client_info(); |
---|
3583 | } else { |
---|
3584 | $client_version = mysql_get_client_info(); |
---|
3585 | } |
---|
3586 | |
---|
3587 | /* |
---|
3588 | * libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server. |
---|
3589 | * mysqlnd has supported utf8mb4 since 5.0.9. |
---|
3590 | */ |
---|
3591 | if ( false !== strpos( $client_version, 'mysqlnd' ) ) { |
---|
3592 | $client_version = preg_replace( '/^\D+([\d.]+).*/', '$1', $client_version ); |
---|
3593 | return version_compare( $client_version, '5.0.9', '>=' ); |
---|
3594 | } else { |
---|
3595 | return version_compare( $client_version, '5.5.3', '>=' ); |
---|
3596 | } |
---|
3597 | case 'utf8mb4_520': // @since 4.6.0 |
---|
3598 | return version_compare( $version, '5.6', '>=' ); |
---|
3599 | } |
---|
3600 | |
---|
3601 | return false; |
---|
3602 | } |
---|
3603 | |
---|
3604 | /** |
---|
3605 | * Retrieve the name of the function that called wpdb. |
---|
3606 | * |
---|
3607 | * Searches up the list of functions until it reaches |
---|
3608 | * the one that would most logically had called this method. |
---|
3609 | * |
---|
3610 | * @since 2.5.0 |
---|
3611 | * |
---|
3612 | * @return string Comma separated list of the calling functions. |
---|
3613 | */ |
---|
3614 | public function get_caller() { |
---|
3615 | return wp_debug_backtrace_summary( __CLASS__ ); |
---|
3616 | } |
---|
3617 | |
---|
3618 | /** |
---|
3619 | * Retrieves the MySQL server version. |
---|
3620 | * |
---|
3621 | * @since 2.7.0 |
---|
3622 | * |
---|
3623 | * @return null|string Null on failure, version number on success. |
---|
3624 | */ |
---|
3625 | public function db_version() { |
---|
3626 | if ( $this->use_mysqli ) { |
---|
3627 | $server_info = mysqli_get_server_info( $this->dbh ); |
---|
3628 | } else { |
---|
3629 | $server_info = mysql_get_server_info( $this->dbh ); |
---|
3630 | } |
---|
3631 | return preg_replace( '/[^0-9.].*/', '', $server_info ); |
---|
3632 | } |
---|
3633 | } |
---|