PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/wp-db.php

https://gitlab.com/math4youbyusgroupillinois/WordPress
PHP | 2811 lines | 1300 code | 286 blank | 1225 comment | 261 complexity | 31d1c64e40a6f82a76370b7af5675e94 MD5 | raw file
  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. * @since 0.71
  13. */
  14. define( 'EZSQL_VERSION', 'WP1.25' );
  15. /**
  16. * @since 0.71
  17. */
  18. define( 'OBJECT', 'OBJECT' );
  19. define( 'object', 'OBJECT' ); // Back compat.
  20. /**
  21. * @since 2.5.0
  22. */
  23. define( 'OBJECT_K', 'OBJECT_K' );
  24. /**
  25. * @since 0.71
  26. */
  27. define( 'ARRAY_A', 'ARRAY_A' );
  28. /**
  29. * @since 0.71
  30. */
  31. define( 'ARRAY_N', 'ARRAY_N' );
  32. /**
  33. * WordPress Database Access Abstraction Object
  34. *
  35. * It is possible to replace this class with your own
  36. * by setting the $wpdb global variable in wp-content/db.php
  37. * file to your class. The wpdb class will still be included,
  38. * so you can extend it or simply use your own.
  39. *
  40. * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
  41. *
  42. * @package WordPress
  43. * @subpackage Database
  44. * @since 0.71
  45. */
  46. class wpdb {
  47. /**
  48. * Whether to show SQL/DB errors.
  49. *
  50. * Default behavior is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY
  51. * evaluated to true.
  52. *
  53. * @since 0.71
  54. * @access private
  55. * @var bool
  56. */
  57. var $show_errors = false;
  58. /**
  59. * Whether to suppress errors during the DB bootstrapping.
  60. *
  61. * @access private
  62. * @since 2.5.0
  63. * @var bool
  64. */
  65. var $suppress_errors = false;
  66. /**
  67. * The last error during query.
  68. *
  69. * @since 2.5.0
  70. * @var string
  71. */
  72. public $last_error = '';
  73. /**
  74. * Amount of queries made
  75. *
  76. * @since 1.2.0
  77. * @access private
  78. * @var int
  79. */
  80. var $num_queries = 0;
  81. /**
  82. * Count of rows returned by previous query
  83. *
  84. * @since 0.71
  85. * @access private
  86. * @var int
  87. */
  88. var $num_rows = 0;
  89. /**
  90. * Count of affected rows by previous query
  91. *
  92. * @since 0.71
  93. * @access private
  94. * @var int
  95. */
  96. var $rows_affected = 0;
  97. /**
  98. * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
  99. *
  100. * @since 0.71
  101. * @access public
  102. * @var int
  103. */
  104. var $insert_id = 0;
  105. /**
  106. * Last query made
  107. *
  108. * @since 0.71
  109. * @access private
  110. * @var array
  111. */
  112. var $last_query;
  113. /**
  114. * Results of the last query made
  115. *
  116. * @since 0.71
  117. * @access private
  118. * @var array|null
  119. */
  120. var $last_result;
  121. /**
  122. * MySQL result, which is either a resource or boolean.
  123. *
  124. * @since 0.71
  125. * @access protected
  126. * @var mixed
  127. */
  128. protected $result;
  129. /**
  130. * Cached column info, for sanity checking data before inserting
  131. *
  132. * @since 4.2.0
  133. * @access protected
  134. * @var array
  135. */
  136. protected $col_meta = array();
  137. /**
  138. * Calculated character sets on tables
  139. *
  140. * @since 4.2.0
  141. * @access protected
  142. * @var array
  143. */
  144. protected $table_charset = array();
  145. /**
  146. * Whether text fields in the current query need to be sanity checked.
  147. *
  148. * @since 4.2.0
  149. * @access protected
  150. * @var bool
  151. */
  152. protected $check_current_query = true;
  153. /**
  154. * Saved info on the table column
  155. *
  156. * @since 0.71
  157. * @access protected
  158. * @var array
  159. */
  160. protected $col_info;
  161. /**
  162. * Saved queries that were executed
  163. *
  164. * @since 1.5.0
  165. * @access private
  166. * @var array
  167. */
  168. var $queries;
  169. /**
  170. * The number of times to retry reconnecting before dying.
  171. *
  172. * @since 3.9.0
  173. * @access protected
  174. * @see wpdb::check_connection()
  175. * @var int
  176. */
  177. protected $reconnect_retries = 5;
  178. /**
  179. * WordPress table prefix
  180. *
  181. * You can set this to have multiple WordPress installations
  182. * in a single database. The second reason is for possible
  183. * security precautions.
  184. *
  185. * @since 2.5.0
  186. * @access private
  187. * @var string
  188. */
  189. var $prefix = '';
  190. /**
  191. * WordPress base table prefix.
  192. *
  193. * @since 3.0.0
  194. * @access public
  195. * @var string
  196. */
  197. public $base_prefix;
  198. /**
  199. * Whether the database queries are ready to start executing.
  200. *
  201. * @since 2.3.2
  202. * @access private
  203. * @var bool
  204. */
  205. var $ready = false;
  206. /**
  207. * {@internal Missing Description}}
  208. *
  209. * @since 3.0.0
  210. * @access public
  211. * @var int
  212. */
  213. public $blogid = 0;
  214. /**
  215. * {@internal Missing Description}}
  216. *
  217. * @since 3.0.0
  218. * @access public
  219. * @var int
  220. */
  221. public $siteid = 0;
  222. /**
  223. * List of WordPress per-blog tables
  224. *
  225. * @since 2.5.0
  226. * @access private
  227. * @see wpdb::tables()
  228. * @var array
  229. */
  230. var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
  231. 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
  232. /**
  233. * List of deprecated WordPress tables
  234. *
  235. * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
  236. *
  237. * @since 2.9.0
  238. * @access private
  239. * @see wpdb::tables()
  240. * @var array
  241. */
  242. var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
  243. /**
  244. * List of WordPress global tables
  245. *
  246. * @since 3.0.0
  247. * @access private
  248. * @see wpdb::tables()
  249. * @var array
  250. */
  251. var $global_tables = array( 'users', 'usermeta' );
  252. /**
  253. * List of Multisite global tables
  254. *
  255. * @since 3.0.0
  256. * @access private
  257. * @see wpdb::tables()
  258. * @var array
  259. */
  260. var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
  261. 'sitecategories', 'registration_log', 'blog_versions' );
  262. /**
  263. * WordPress Comments table
  264. *
  265. * @since 1.5.0
  266. * @access public
  267. * @var string
  268. */
  269. public $comments;
  270. /**
  271. * WordPress Comment Metadata table
  272. *
  273. * @since 2.9.0
  274. * @access public
  275. * @var string
  276. */
  277. public $commentmeta;
  278. /**
  279. * WordPress Links table
  280. *
  281. * @since 1.5.0
  282. * @access public
  283. * @var string
  284. */
  285. public $links;
  286. /**
  287. * WordPress Options table
  288. *
  289. * @since 1.5.0
  290. * @access public
  291. * @var string
  292. */
  293. public $options;
  294. /**
  295. * WordPress Post Metadata table
  296. *
  297. * @since 1.5.0
  298. * @access public
  299. * @var string
  300. */
  301. public $postmeta;
  302. /**
  303. * WordPress Posts table
  304. *
  305. * @since 1.5.0
  306. * @access public
  307. * @var string
  308. */
  309. public $posts;
  310. /**
  311. * WordPress Terms table
  312. *
  313. * @since 2.3.0
  314. * @access public
  315. * @var string
  316. */
  317. public $terms;
  318. /**
  319. * WordPress Term Relationships table
  320. *
  321. * @since 2.3.0
  322. * @access public
  323. * @var string
  324. */
  325. public $term_relationships;
  326. /**
  327. * WordPress Term Taxonomy table
  328. *
  329. * @since 2.3.0
  330. * @access public
  331. * @var string
  332. */
  333. public $term_taxonomy;
  334. /*
  335. * Global and Multisite tables
  336. */
  337. /**
  338. * WordPress User Metadata table
  339. *
  340. * @since 2.3.0
  341. * @access public
  342. * @var string
  343. */
  344. public $usermeta;
  345. /**
  346. * WordPress Users table
  347. *
  348. * @since 1.5.0
  349. * @access public
  350. * @var string
  351. */
  352. public $users;
  353. /**
  354. * Multisite Blogs table
  355. *
  356. * @since 3.0.0
  357. * @access public
  358. * @var string
  359. */
  360. public $blogs;
  361. /**
  362. * Multisite Blog Versions table
  363. *
  364. * @since 3.0.0
  365. * @access public
  366. * @var string
  367. */
  368. public $blog_versions;
  369. /**
  370. * Multisite Registration Log table
  371. *
  372. * @since 3.0.0
  373. * @access public
  374. * @var string
  375. */
  376. public $registration_log;
  377. /**
  378. * Multisite Signups table
  379. *
  380. * @since 3.0.0
  381. * @access public
  382. * @var string
  383. */
  384. public $signups;
  385. /**
  386. * Multisite Sites table
  387. *
  388. * @since 3.0.0
  389. * @access public
  390. * @var string
  391. */
  392. public $site;
  393. /**
  394. * Multisite Sitewide Terms table
  395. *
  396. * @since 3.0.0
  397. * @access public
  398. * @var string
  399. */
  400. public $sitecategories;
  401. /**
  402. * Multisite Site Metadata table
  403. *
  404. * @since 3.0.0
  405. * @access public
  406. * @var string
  407. */
  408. public $sitemeta;
  409. /**
  410. * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
  411. *
  412. * Keys are column names, values are format types: 'ID' => '%d'
  413. *
  414. * @since 2.8.0
  415. * @see wpdb::prepare()
  416. * @see wpdb::insert()
  417. * @see wpdb::update()
  418. * @see wpdb::delete()
  419. * @see wp_set_wpdb_vars()
  420. * @access public
  421. * @var array
  422. */
  423. public $field_types = array();
  424. /**
  425. * Database table columns charset
  426. *
  427. * @since 2.2.0
  428. * @access public
  429. * @var string
  430. */
  431. public $charset;
  432. /**
  433. * Database table columns collate
  434. *
  435. * @since 2.2.0
  436. * @access public
  437. * @var string
  438. */
  439. public $collate;
  440. /**
  441. * Database Username
  442. *
  443. * @since 2.9.0
  444. * @access protected
  445. * @var string
  446. */
  447. protected $dbuser;
  448. /**
  449. * Database Password
  450. *
  451. * @since 3.1.0
  452. * @access protected
  453. * @var string
  454. */
  455. protected $dbpassword;
  456. /**
  457. * Database Name
  458. *
  459. * @since 3.1.0
  460. * @access protected
  461. * @var string
  462. */
  463. protected $dbname;
  464. /**
  465. * Database Host
  466. *
  467. * @since 3.1.0
  468. * @access protected
  469. * @var string
  470. */
  471. protected $dbhost;
  472. /**
  473. * Database Handle
  474. *
  475. * @since 0.71
  476. * @access protected
  477. * @var string
  478. */
  479. protected $dbh;
  480. /**
  481. * A textual description of the last query/get_row/get_var call
  482. *
  483. * @since 3.0.0
  484. * @access public
  485. * @var string
  486. */
  487. public $func_call;
  488. /**
  489. * Whether MySQL is used as the database engine.
  490. *
  491. * Set in WPDB::db_connect() to true, by default. This is used when checking
  492. * against the required MySQL version for WordPress. Normally, a replacement
  493. * database drop-in (db.php) will skip these checks, but setting this to true
  494. * will force the checks to occur.
  495. *
  496. * @since 3.3.0
  497. * @access public
  498. * @var bool
  499. */
  500. public $is_mysql = null;
  501. /**
  502. * A list of incompatible SQL modes.
  503. *
  504. * @since 3.9.0
  505. * @access protected
  506. * @var array
  507. */
  508. protected $incompatible_modes = array( 'NO_ZERO_DATE', 'ONLY_FULL_GROUP_BY',
  509. 'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
  510. /**
  511. * Whether to use mysqli over mysql.
  512. *
  513. * @since 3.9.0
  514. * @access private
  515. * @var bool
  516. */
  517. private $use_mysqli = false;
  518. /**
  519. * Whether we've managed to successfully connect at some point
  520. *
  521. * @since 3.9.0
  522. * @access private
  523. * @var bool
  524. */
  525. private $has_connected = false;
  526. /**
  527. * Connects to the database server and selects a database
  528. *
  529. * PHP5 style constructor for compatibility with PHP5. Does
  530. * the actual setting up of the class properties and connection
  531. * to the database.
  532. *
  533. * @link https://core.trac.wordpress.org/ticket/3354
  534. * @since 2.0.8
  535. *
  536. * @param string $dbuser MySQL database user
  537. * @param string $dbpassword MySQL database password
  538. * @param string $dbname MySQL database name
  539. * @param string $dbhost MySQL database host
  540. */
  541. public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
  542. register_shutdown_function( array( $this, '__destruct' ) );
  543. if ( WP_DEBUG && WP_DEBUG_DISPLAY )
  544. $this->show_errors();
  545. /* Use ext/mysqli if it exists and:
  546. * - WP_USE_EXT_MYSQL is defined as false, or
  547. * - We are a development version of WordPress, or
  548. * - We are running PHP 5.5 or greater, or
  549. * - ext/mysql is not loaded.
  550. */
  551. if ( function_exists( 'mysqli_connect' ) ) {
  552. if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
  553. $this->use_mysqli = ! WP_USE_EXT_MYSQL;
  554. } elseif ( version_compare( phpversion(), '5.5', '>=' ) || ! function_exists( 'mysql_connect' ) ) {
  555. $this->use_mysqli = true;
  556. } elseif ( false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
  557. $this->use_mysqli = true;
  558. }
  559. }
  560. $this->init_charset();
  561. $this->dbuser = $dbuser;
  562. $this->dbpassword = $dbpassword;
  563. $this->dbname = $dbname;
  564. $this->dbhost = $dbhost;
  565. // wp-config.php creation will manually connect when ready.
  566. if ( defined( 'WP_SETUP_CONFIG' ) ) {
  567. return;
  568. }
  569. $this->db_connect();
  570. }
  571. /**
  572. * PHP5 style destructor and will run when database object is destroyed.
  573. *
  574. * @see wpdb::__construct()
  575. * @since 2.0.8
  576. * @return bool true
  577. */
  578. public function __destruct() {
  579. return true;
  580. }
  581. /**
  582. * PHP5 style magic getter, used to lazy-load expensive data.
  583. *
  584. * @since 3.5.0
  585. *
  586. * @param string $name The private member to get, and optionally process
  587. * @return mixed The private member
  588. */
  589. public function __get( $name ) {
  590. if ( 'col_info' === $name )
  591. $this->load_col_info();
  592. return $this->$name;
  593. }
  594. /**
  595. * Magic function, for backwards compatibility.
  596. *
  597. * @since 3.5.0
  598. *
  599. * @param string $name The private member to set
  600. * @param mixed $value The value to set
  601. */
  602. public function __set( $name, $value ) {
  603. $protected_members = array(
  604. 'col_meta',
  605. 'table_charset',
  606. 'check_current_query',
  607. );
  608. if ( in_array( $name, $protected_members, true ) ) {
  609. return;
  610. }
  611. $this->$name = $value;
  612. }
  613. /**
  614. * Magic function, for backwards compatibility.
  615. *
  616. * @since 3.5.0
  617. *
  618. * @param string $name The private member to check
  619. *
  620. * @return bool If the member is set or not
  621. */
  622. public function __isset( $name ) {
  623. return isset( $this->$name );
  624. }
  625. /**
  626. * Magic function, for backwards compatibility.
  627. *
  628. * @since 3.5.0
  629. *
  630. * @param string $name The private member to unset
  631. */
  632. public function __unset( $name ) {
  633. unset( $this->$name );
  634. }
  635. /**
  636. * Set $this->charset and $this->collate
  637. *
  638. * @since 3.1.0
  639. */
  640. public function init_charset() {
  641. if ( function_exists('is_multisite') && is_multisite() ) {
  642. $this->charset = 'utf8';
  643. if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
  644. $this->collate = DB_COLLATE;
  645. else
  646. $this->collate = 'utf8_general_ci';
  647. } elseif ( defined( 'DB_COLLATE' ) ) {
  648. $this->collate = DB_COLLATE;
  649. }
  650. if ( defined( 'DB_CHARSET' ) )
  651. $this->charset = DB_CHARSET;
  652. }
  653. /**
  654. * Sets the connection's character set.
  655. *
  656. * @since 3.1.0
  657. *
  658. * @param resource $dbh The resource given by mysql_connect
  659. * @param string $charset Optional. The character set. Default null.
  660. * @param string $collate Optional. The collation. Default null.
  661. */
  662. public function set_charset( $dbh, $charset = null, $collate = null ) {
  663. if ( ! isset( $charset ) )
  664. $charset = $this->charset;
  665. if ( ! isset( $collate ) )
  666. $collate = $this->collate;
  667. if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
  668. if ( $this->use_mysqli ) {
  669. if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
  670. mysqli_set_charset( $dbh, $charset );
  671. } else {
  672. $query = $this->prepare( 'SET NAMES %s', $charset );
  673. if ( ! empty( $collate ) )
  674. $query .= $this->prepare( ' COLLATE %s', $collate );
  675. mysqli_query( $query, $dbh );
  676. }
  677. } else {
  678. if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
  679. mysql_set_charset( $charset, $dbh );
  680. } else {
  681. $query = $this->prepare( 'SET NAMES %s', $charset );
  682. if ( ! empty( $collate ) )
  683. $query .= $this->prepare( ' COLLATE %s', $collate );
  684. mysql_query( $query, $dbh );
  685. }
  686. }
  687. }
  688. }
  689. /**
  690. * Change the current SQL mode, and ensure its WordPress compatibility.
  691. *
  692. * If no modes are passed, it will ensure the current MySQL server
  693. * modes are compatible.
  694. *
  695. * @since 3.9.0
  696. *
  697. * @param array $modes Optional. A list of SQL modes to set.
  698. */
  699. public function set_sql_mode( $modes = array() ) {
  700. if ( empty( $modes ) ) {
  701. if ( $this->use_mysqli ) {
  702. $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
  703. } else {
  704. $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
  705. }
  706. if ( empty( $res ) ) {
  707. return;
  708. }
  709. if ( $this->use_mysqli ) {
  710. $modes_array = mysqli_fetch_array( $res );
  711. if ( empty( $modes_array[0] ) ) {
  712. return;
  713. }
  714. $modes_str = $modes_array[0];
  715. } else {
  716. $modes_str = mysql_result( $res, 0 );
  717. }
  718. if ( empty( $modes_str ) ) {
  719. return;
  720. }
  721. $modes = explode( ',', $modes_str );
  722. }
  723. $modes = array_change_key_case( $modes, CASE_UPPER );
  724. /**
  725. * Filter the list of incompatible SQL modes to exclude.
  726. *
  727. * @since 3.9.0
  728. *
  729. * @param array $incompatible_modes An array of incompatible modes.
  730. */
  731. $incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
  732. foreach( $modes as $i => $mode ) {
  733. if ( in_array( $mode, $incompatible_modes ) ) {
  734. unset( $modes[ $i ] );
  735. }
  736. }
  737. $modes_str = implode( ',', $modes );
  738. if ( $this->use_mysqli ) {
  739. mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" );
  740. } else {
  741. mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh );
  742. }
  743. }
  744. /**
  745. * Sets the table prefix for the WordPress tables.
  746. *
  747. * @since 2.5.0
  748. *
  749. * @param string $prefix Alphanumeric name for the new prefix.
  750. * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not.
  751. * @return string|WP_Error Old prefix or WP_Error on error
  752. */
  753. public function set_prefix( $prefix, $set_table_names = true ) {
  754. if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
  755. return new WP_Error('invalid_db_prefix', 'Invalid database prefix' );
  756. $old_prefix = is_multisite() ? '' : $prefix;
  757. if ( isset( $this->base_prefix ) )
  758. $old_prefix = $this->base_prefix;
  759. $this->base_prefix = $prefix;
  760. if ( $set_table_names ) {
  761. foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
  762. $this->$table = $prefixed_table;
  763. if ( is_multisite() && empty( $this->blogid ) )
  764. return $old_prefix;
  765. $this->prefix = $this->get_blog_prefix();
  766. foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
  767. $this->$table = $prefixed_table;
  768. foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
  769. $this->$table = $prefixed_table;
  770. }
  771. return $old_prefix;
  772. }
  773. /**
  774. * Sets blog id.
  775. *
  776. * @since 3.0.0
  777. * @access public
  778. * @param int $blog_id
  779. * @param int $site_id Optional.
  780. * @return int previous blog id
  781. */
  782. public function set_blog_id( $blog_id, $site_id = 0 ) {
  783. if ( ! empty( $site_id ) )
  784. $this->siteid = $site_id;
  785. $old_blog_id = $this->blogid;
  786. $this->blogid = $blog_id;
  787. $this->prefix = $this->get_blog_prefix();
  788. foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
  789. $this->$table = $prefixed_table;
  790. foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
  791. $this->$table = $prefixed_table;
  792. return $old_blog_id;
  793. }
  794. /**
  795. * Gets blog prefix.
  796. *
  797. * @since 3.0.0
  798. * @param int $blog_id Optional.
  799. * @return string Blog prefix.
  800. */
  801. public function get_blog_prefix( $blog_id = null ) {
  802. if ( is_multisite() ) {
  803. if ( null === $blog_id )
  804. $blog_id = $this->blogid;
  805. $blog_id = (int) $blog_id;
  806. if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
  807. return $this->base_prefix;
  808. else
  809. return $this->base_prefix . $blog_id . '_';
  810. } else {
  811. return $this->base_prefix;
  812. }
  813. }
  814. /**
  815. * Returns an array of WordPress tables.
  816. *
  817. * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
  818. * override the WordPress users and usermeta tables that would otherwise
  819. * be determined by the prefix.
  820. *
  821. * The scope argument can take one of the following:
  822. *
  823. * 'all' - returns 'all' and 'global' tables. No old tables are returned.
  824. * 'blog' - returns the blog-level tables for the queried blog.
  825. * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite.
  826. * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
  827. * 'old' - returns tables which are deprecated.
  828. *
  829. * @since 3.0.0
  830. * @uses wpdb::$tables
  831. * @uses wpdb::$old_tables
  832. * @uses wpdb::$global_tables
  833. * @uses wpdb::$ms_global_tables
  834. *
  835. * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
  836. * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
  837. * prefix is requested, then the custom users and usermeta tables will be mapped.
  838. * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
  839. * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
  840. */
  841. public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
  842. switch ( $scope ) {
  843. case 'all' :
  844. $tables = array_merge( $this->global_tables, $this->tables );
  845. if ( is_multisite() )
  846. $tables = array_merge( $tables, $this->ms_global_tables );
  847. break;
  848. case 'blog' :
  849. $tables = $this->tables;
  850. break;
  851. case 'global' :
  852. $tables = $this->global_tables;
  853. if ( is_multisite() )
  854. $tables = array_merge( $tables, $this->ms_global_tables );
  855. break;
  856. case 'ms_global' :
  857. $tables = $this->ms_global_tables;
  858. break;
  859. case 'old' :
  860. $tables = $this->old_tables;
  861. break;
  862. default :
  863. return array();
  864. }
  865. if ( $prefix ) {
  866. if ( ! $blog_id )
  867. $blog_id = $this->blogid;
  868. $blog_prefix = $this->get_blog_prefix( $blog_id );
  869. $base_prefix = $this->base_prefix;
  870. $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
  871. foreach ( $tables as $k => $table ) {
  872. if ( in_array( $table, $global_tables ) )
  873. $tables[ $table ] = $base_prefix . $table;
  874. else
  875. $tables[ $table ] = $blog_prefix . $table;
  876. unset( $tables[ $k ] );
  877. }
  878. if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
  879. $tables['users'] = CUSTOM_USER_TABLE;
  880. if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
  881. $tables['usermeta'] = CUSTOM_USER_META_TABLE;
  882. }
  883. return $tables;
  884. }
  885. /**
  886. * Selects a database using the current database connection.
  887. *
  888. * The database name will be changed based on the current database
  889. * connection. On failure, the execution will bail and display an DB error.
  890. *
  891. * @since 0.71
  892. *
  893. * @param string $db MySQL database name
  894. * @param resource $dbh Optional link identifier.
  895. * @return null Always null.
  896. */
  897. public function select( $db, $dbh = null ) {
  898. if ( is_null($dbh) )
  899. $dbh = $this->dbh;
  900. if ( $this->use_mysqli ) {
  901. $success = @mysqli_select_db( $dbh, $db );
  902. } else {
  903. $success = @mysql_select_db( $db, $dbh );
  904. }
  905. if ( ! $success ) {
  906. $this->ready = false;
  907. if ( ! did_action( 'template_redirect' ) ) {
  908. wp_load_translations_early();
  909. $this->bail( sprintf( __( '<h1>Can&#8217;t select database</h1>
  910. <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
  911. <ul>
  912. <li>Are you sure it exists?</li>
  913. <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
  914. <li>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?</li>
  915. </ul>
  916. <p>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="https://wordpress.org/support/">WordPress Support Forums</a>.</p>' ), htmlspecialchars( $db, ENT_QUOTES ), htmlspecialchars( $this->dbuser, ENT_QUOTES ) ), 'db_select_fail' );
  917. }
  918. return;
  919. }
  920. }
  921. /**
  922. * Do not use, deprecated.
  923. *
  924. * Use esc_sql() or wpdb::prepare() instead.
  925. *
  926. * @since 2.8.0
  927. * @deprecated 3.6.0
  928. * @see wpdb::prepare
  929. * @see esc_sql()
  930. * @access private
  931. *
  932. * @param string $string
  933. * @return string
  934. */
  935. function _weak_escape( $string ) {
  936. if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) )
  937. _deprecated_function( __METHOD__, '3.6', 'wpdb::prepare() or esc_sql()' );
  938. return addslashes( $string );
  939. }
  940. /**
  941. * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string()
  942. *
  943. * @see mysqli_real_escape_string()
  944. * @see mysql_real_escape_string()
  945. * @since 2.8.0
  946. * @access private
  947. *
  948. * @param string $string to escape
  949. * @return string escaped
  950. */
  951. function _real_escape( $string ) {
  952. if ( $this->dbh ) {
  953. if ( $this->use_mysqli ) {
  954. return mysqli_real_escape_string( $this->dbh, $string );
  955. } else {
  956. return mysql_real_escape_string( $string, $this->dbh );
  957. }
  958. }
  959. $class = get_class( $this );
  960. if ( function_exists( '__' ) ) {
  961. _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), E_USER_NOTICE );
  962. } else {
  963. _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), E_USER_NOTICE );
  964. }
  965. return addslashes( $string );
  966. }
  967. /**
  968. * Escape data. Works on arrays.
  969. *
  970. * @uses wpdb::_real_escape()
  971. * @since 2.8.0
  972. * @access private
  973. *
  974. * @param string|array $data
  975. * @return string|array escaped
  976. */
  977. function _escape( $data ) {
  978. if ( is_array( $data ) ) {
  979. foreach ( $data as $k => $v ) {
  980. if ( is_array($v) )
  981. $data[$k] = $this->_escape( $v );
  982. else
  983. $data[$k] = $this->_real_escape( $v );
  984. }
  985. } else {
  986. $data = $this->_real_escape( $data );
  987. }
  988. return $data;
  989. }
  990. /**
  991. * Do not use, deprecated.
  992. *
  993. * Use esc_sql() or wpdb::prepare() instead.
  994. *
  995. * @since 0.71
  996. * @deprecated 3.6.0
  997. * @see wpdb::prepare()
  998. * @see esc_sql()
  999. *
  1000. * @param mixed $data
  1001. * @return mixed
  1002. */
  1003. public function escape( $data ) {
  1004. if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) )
  1005. _deprecated_function( __METHOD__, '3.6', 'wpdb::prepare() or esc_sql()' );
  1006. if ( is_array( $data ) ) {
  1007. foreach ( $data as $k => $v ) {
  1008. if ( is_array( $v ) )
  1009. $data[$k] = $this->escape( $v, 'recursive' );
  1010. else
  1011. $data[$k] = $this->_weak_escape( $v, 'internal' );
  1012. }
  1013. } else {
  1014. $data = $this->_weak_escape( $data, 'internal' );
  1015. }
  1016. return $data;
  1017. }
  1018. /**
  1019. * Escapes content by reference for insertion into the database, for security
  1020. *
  1021. * @uses wpdb::_real_escape()
  1022. * @since 2.3.0
  1023. * @param string $string to escape
  1024. * @return void
  1025. */
  1026. public function escape_by_ref( &$string ) {
  1027. if ( ! is_float( $string ) )
  1028. $string = $this->_real_escape( $string );
  1029. }
  1030. /**
  1031. * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
  1032. *
  1033. * The following directives can be used in the query format string:
  1034. * %d (integer)
  1035. * %f (float)
  1036. * %s (string)
  1037. * %% (literal percentage sign - no argument needed)
  1038. *
  1039. * All of %d, %f, and %s are to be left unquoted in the query string and they need an argument passed for them.
  1040. * Literals (%) as parts of the query must be properly written as %%.
  1041. *
  1042. * This function only supports a small subset of the sprintf syntax; it only supports %d (integer), %f (float), and %s (string).
  1043. * Does not support sign, padding, alignment, width or precision specifiers.
  1044. * Does not support argument numbering/swapping.
  1045. *
  1046. * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}.
  1047. *
  1048. * Both %d and %s should be left unquoted in the query string.
  1049. *
  1050. * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
  1051. * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
  1052. *
  1053. * @link http://php.net/sprintf Description of syntax.
  1054. * @since 2.3.0
  1055. *
  1056. * @param string $query Query statement with sprintf()-like placeholders
  1057. * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like
  1058. * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if
  1059. * being called like {@link http://php.net/sprintf sprintf()}.
  1060. * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
  1061. * {@link http://php.net/sprintf sprintf()}.
  1062. * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
  1063. * if there was something to prepare
  1064. */
  1065. public function prepare( $query, $args ) {
  1066. if ( is_null( $query ) )
  1067. return;
  1068. // This is not meant to be foolproof -- but it will catch obviously incorrect usage.
  1069. if ( strpos( $query, '%' ) === false ) {
  1070. _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9' );
  1071. }
  1072. $args = func_get_args();
  1073. array_shift( $args );
  1074. // If args were passed as an array (as in vsprintf), move them up
  1075. if ( isset( $args[0] ) && is_array($args[0]) )
  1076. $args = $args[0];
  1077. $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
  1078. $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
  1079. $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
  1080. $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
  1081. array_walk( $args, array( $this, 'escape_by_ref' ) );
  1082. return @vsprintf( $query, $args );
  1083. }
  1084. /**
  1085. * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
  1086. *
  1087. * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
  1088. *
  1089. * Example Prepared Statement:
  1090. * $wild = '%';
  1091. * $find = 'only 43% of planets';
  1092. * $like = $wild . $wpdb->esc_like( $find ) . $wild;
  1093. * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like );
  1094. *
  1095. * Example Escape Chain:
  1096. * $sql = esc_sql( $wpdb->esc_like( $input ) );
  1097. *
  1098. * @since 4.0.0
  1099. * @access public
  1100. *
  1101. * @param string $text The raw text to be escaped. The input typed by the user should have no
  1102. * extra or deleted slashes.
  1103. * @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare()
  1104. * or real_escape next.
  1105. */
  1106. public function esc_like( $text ) {
  1107. return addcslashes( $text, '_%\\' );
  1108. }
  1109. /**
  1110. * Print SQL/DB error.
  1111. *
  1112. * @since 0.71
  1113. * @global array $EZSQL_ERROR Stores error information of query and error string
  1114. *
  1115. * @param string $str The error to display
  1116. * @return false|null False if the showing of errors is disabled.
  1117. */
  1118. public function print_error( $str = '' ) {
  1119. global $EZSQL_ERROR;
  1120. if ( !$str ) {
  1121. if ( $this->use_mysqli ) {
  1122. $str = mysqli_error( $this->dbh );
  1123. } else {
  1124. $str = mysql_error( $this->dbh );
  1125. }
  1126. }
  1127. $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
  1128. if ( $this->suppress_errors )
  1129. return false;
  1130. wp_load_translations_early();
  1131. if ( $caller = $this->get_caller() )
  1132. $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller );
  1133. else
  1134. $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query );
  1135. error_log( $error_str );
  1136. // Are we showing errors?
  1137. if ( ! $this->show_errors )
  1138. return false;
  1139. // If there is an error then take note of it
  1140. if ( is_multisite() ) {
  1141. $msg = "WordPress database error: [$str]\n{$this->last_query}\n";
  1142. if ( defined( 'ERRORLOGFILE' ) )
  1143. error_log( $msg, 3, ERRORLOGFILE );
  1144. if ( defined( 'DIEONDBERROR' ) )
  1145. wp_die( $msg );
  1146. } else {
  1147. $str = htmlspecialchars( $str, ENT_QUOTES );
  1148. $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
  1149. print "<div id='error'>
  1150. <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
  1151. <code>$query</code></p>
  1152. </div>";
  1153. }
  1154. }
  1155. /**
  1156. * Enables showing of database errors.
  1157. *
  1158. * This function should be used only to enable showing of errors.
  1159. * wpdb::hide_errors() should be used instead for hiding of errors. However,
  1160. * this function can be used to enable and disable showing of database
  1161. * errors.
  1162. *
  1163. * @since 0.71
  1164. * @see wpdb::hide_errors()
  1165. *
  1166. * @param bool $show Whether to show or hide errors
  1167. * @return bool Old value for showing errors.
  1168. */
  1169. public function show_errors( $show = true ) {
  1170. $errors = $this->show_errors;
  1171. $this->show_errors = $show;
  1172. return $errors;
  1173. }
  1174. /**
  1175. * Disables showing of database errors.
  1176. *
  1177. * By default database errors are not shown.
  1178. *
  1179. * @since 0.71
  1180. * @see wpdb::show_errors()
  1181. *
  1182. * @return bool Whether showing of errors was active
  1183. */
  1184. public function hide_errors() {
  1185. $show = $this->show_errors;
  1186. $this->show_errors = false;
  1187. return $show;
  1188. }
  1189. /**
  1190. * Whether to suppress database errors.
  1191. *
  1192. * By default database errors are suppressed, with a simple
  1193. * call to this function they can be enabled.
  1194. *
  1195. * @since 2.5.0
  1196. * @see wpdb::hide_errors()
  1197. * @param bool $suppress Optional. New value. Defaults to true.
  1198. * @return bool Old value
  1199. */
  1200. public function suppress_errors( $suppress = true ) {
  1201. $errors = $this->suppress_errors;
  1202. $this->suppress_errors = (bool) $suppress;
  1203. return $errors;
  1204. }
  1205. /**
  1206. * Kill cached query results.
  1207. *
  1208. * @since 0.71
  1209. * @return void
  1210. */
  1211. public function flush() {
  1212. $this->last_result = array();
  1213. $this->col_info = null;
  1214. $this->last_query = null;
  1215. $this->rows_affected = $this->num_rows = 0;
  1216. $this->last_error = '';
  1217. if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
  1218. mysqli_free_result( $this->result );
  1219. $this->result = null;
  1220. // Sanity check before using the handle
  1221. if ( empty( $this->dbh ) || !( $this->dbh instanceof mysqli ) ) {
  1222. return;
  1223. }
  1224. // Clear out any results from a multi-query
  1225. while ( mysqli_more_results( $this->dbh ) ) {
  1226. mysqli_next_result( $this->dbh );
  1227. }
  1228. } elseif ( is_resource( $this->result ) ) {
  1229. mysql_free_result( $this->result );
  1230. }
  1231. }
  1232. /**
  1233. * Connect to and select database.
  1234. *
  1235. * If $allow_bail is false, the lack of database connection will need
  1236. * to be handled manually.
  1237. *
  1238. * @since 3.0.0
  1239. * @since 3.9.0 $allow_bail parameter added.
  1240. *
  1241. * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  1242. * @return null|bool True with a successful connection, false on failure.
  1243. */
  1244. public function db_connect( $allow_bail = true ) {
  1245. $this->is_mysql = true;
  1246. /*
  1247. * Deprecated in 3.9+ when using MySQLi. No equivalent
  1248. * $new_link parameter exists for mysqli_* functions.
  1249. */
  1250. $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
  1251. $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
  1252. if ( $this->use_mysqli ) {
  1253. $this->dbh = mysqli_init();
  1254. // mysqli_real_connect doesn't support the host param including a port or socket
  1255. // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
  1256. $port = null;
  1257. $socket = null;
  1258. $host = $this->dbhost;
  1259. $port_or_socket = strstr( $host, ':' );
  1260. if ( ! empty( $port_or_socket ) ) {
  1261. $host = substr( $host, 0, strpos( $host, ':' ) );
  1262. $port_or_socket = substr( $port_or_socket, 1 );
  1263. if ( 0 !== strpos( $port_or_socket, '/' ) ) {
  1264. $port = intval( $port_or_socket );
  1265. $maybe_socket = strstr( $port_or_socket, ':' );
  1266. if ( ! empty( $maybe_socket ) ) {
  1267. $socket = substr( $maybe_socket, 1 );
  1268. }
  1269. } else {
  1270. $socket = $port_or_socket;
  1271. }
  1272. }
  1273. if ( WP_DEBUG ) {
  1274. mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
  1275. } else {
  1276. @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
  1277. }
  1278. if ( $this->dbh->connect_errno ) {
  1279. $this->dbh = null;
  1280. /* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if:
  1281. * - We haven't previously connected, and
  1282. * - WP_USE_EXT_MYSQL isn't set to false, and
  1283. * - ext/mysql is loaded.
  1284. */
  1285. $attempt_fallback = true;
  1286. if ( $this->has_connected ) {
  1287. $attempt_fallback = false;
  1288. } elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) {
  1289. $attempt_fallback = false;
  1290. } elseif ( ! function_exists( 'mysql_connect' ) ) {
  1291. $attempt_fallback = false;
  1292. }
  1293. if ( $attempt_fallback ) {
  1294. $this->use_mysqli = false;
  1295. $this->db_connect();
  1296. }
  1297. }
  1298. } else {
  1299. if ( WP_DEBUG ) {
  1300. $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1301. } else {
  1302. $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1303. }
  1304. }
  1305. if ( ! $this->dbh && $allow_bail ) {
  1306. wp_load_translations_early();
  1307. // Load custom DB error template, if present.
  1308. if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
  1309. require_once( WP_CONTENT_DIR . '/db-error.php' );
  1310. die();
  1311. }
  1312. $this->bail( sprintf( __( "
  1313. <h1>Error establishing a database connection</h1>
  1314. <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>
  1315. <ul>
  1316. <li>Are you sure you have the correct username and password?</li>
  1317. <li>Are you sure that you have typed the correct hostname?</li>
  1318. <li>Are you sure that the database server is running?</li>
  1319. </ul>
  1320. <p>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='https://wordpress.org/support/'>WordPress Support Forums</a>.</p>
  1321. " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
  1322. return false;
  1323. } elseif ( $this->dbh ) {
  1324. $this->has_connected = true;
  1325. $this->set_charset( $this->dbh );
  1326. $this->ready = true;
  1327. $this->set_sql_mode();
  1328. $this->select( $this->dbname, $this->dbh );
  1329. return true;
  1330. }
  1331. return false;
  1332. }
  1333. /**
  1334. * Check that the connection to the database is still up. If not, try to reconnect.
  1335. *
  1336. * If this function is unable to reconnect, it will forcibly die, or if after the
  1337. * the template_redirect hook has been fired, return false instead.
  1338. *
  1339. * If $allow_bail is false, the lack of database connection will need
  1340. * to be handled manually.
  1341. *
  1342. * @since 3.9.0
  1343. *
  1344. * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  1345. * @return bool|null True if the connection is up.
  1346. */
  1347. public function check_connection( $allow_bail = true ) {
  1348. if ( $this->use_mysqli ) {
  1349. if ( @mysqli_ping( $this->dbh ) ) {
  1350. return true;
  1351. }
  1352. } else {
  1353. if ( @mysql_ping( $this->dbh ) ) {
  1354. return true;
  1355. }
  1356. }
  1357. $error_reporting = false;
  1358. // Disable warnings, as we don't want to see a multitude of "unable to connect" messages
  1359. if ( WP_DEBUG ) {
  1360. $error_reporting = error_reporting();
  1361. error_reporting( $error_reporting & ~E_WARNING );
  1362. }
  1363. for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
  1364. // On the last try, re-enable warnings. We want to see a single instance of the
  1365. // "unable to connect" message on the bail() screen, if it appears.
  1366. if ( $this->reconnect_retries === $tries && WP_DEBUG ) {
  1367. error_reporting( $error_reporting );
  1368. }
  1369. if ( $this->db_connect( false ) ) {
  1370. if ( $error_reporting ) {
  1371. error_reporting( $error_reporting );
  1372. }
  1373. return true;
  1374. }
  1375. sleep( 1 );
  1376. }
  1377. // If template_redirect has already happened, it's too late for wp_die()/dead_db().
  1378. // Let's just return and hope for the best.
  1379. if ( did_action( 'template_redirect' ) ) {
  1380. return false;
  1381. }
  1382. if ( ! $allow_bail ) {
  1383. return false;
  1384. }
  1385. // We weren't able to reconnect, so we better bail.
  1386. $this->bail( sprintf( ( "
  1387. <h1>Error reconnecting to the database</h1>
  1388. <p>This means that we lost contact with the database server at <code>%s</code>. This could mean your host's database server is down.</p>
  1389. <ul>
  1390. <li>Are you sure that the database server is running?</li>
  1391. <li>Are you sure that the database server is not under particularly heavy load?</li>
  1392. </ul>
  1393. <p>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='https://wordpress.org/support/'>WordPress Support Forums</a>.</p>
  1394. " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
  1395. // Call dead_db() if bail didn't die, because this database is no more. It has ceased to be (at least temporarily).
  1396. dead_db();
  1397. }
  1398. /**
  1399. * Perform a MySQL database query, using current database connection.
  1400. *
  1401. * More information can be found on the codex page.
  1402. *
  1403. * @since 0.71
  1404. *
  1405. * @param string $query Database query
  1406. * @return int|false Number of rows affected/selected or false on error
  1407. */
  1408. public function query( $query ) {
  1409. if ( ! $this->ready ) {
  1410. $this->check_current_query = true;
  1411. return false;
  1412. }
  1413. /**
  1414. * Filter the database query.
  1415. *
  1416. * Some queries are made before the plugins have been loaded,
  1417. * and thus cannot be filtered with this method.
  1418. *
  1419. * @since 2.1.0
  1420. *
  1421. * @param string $query Database query.
  1422. */
  1423. $query = apply_filters( 'query', $query );
  1424. $this->flush();
  1425. // Log how the function was called
  1426. $this->func_call = "\$db->query(\"$query\")";
  1427. // If we're writing to the database, make sure the query will write safely.
  1428. if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
  1429. $stripped_query = $this->strip_invalid_text_from_query( $query );
  1430. // strip_invalid_text_from_query() can perform queries, so we need
  1431. // to flush again, just to make sure everything is clear.
  1432. $this->flush();
  1433. if ( $stripped_query !== $query ) {
  1434. $this->insert_id = 0;
  1435. return false;
  1436. }
  1437. }
  1438. $this->check_current_query = true;
  1439. // Keep track of the last query for debug..
  1440. $this->last_query = $query;
  1441. $this->_do_query( $query );
  1442. // MySQL server has gone away, try to reconnect
  1443. $mysql_errno = 0;
  1444. if ( ! empty( $this->dbh ) ) {
  1445. if ( $this->use_mysqli ) {
  1446. $mysql_errno = mysqli_errno( $this->dbh );
  1447. } else {
  1448. $mysql_errno = mysql_errno( $this->dbh );
  1449. }
  1450. }
  1451. if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
  1452. if ( $this->check_connection() ) {
  1453. $this->_do_query( $query );
  1454. } else {
  1455. $this->insert_id = 0;
  1456. return false;
  1457. }
  1458. }
  1459. // If there is an error then take note of it..
  1460. if ( $this->use_mysqli ) {
  1461. $this->last_error = mysqli_error( $this->dbh );
  1462. } else {
  1463. $this->last_error = mysql_error( $this->dbh );
  1464. }
  1465. if ( $this->last_error ) {
  1466. // Clear insert_id on a subsequent failed insert.
  1467. if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
  1468. $this->insert_id = 0;
  1469. $this->print_error();
  1470. return false;
  1471. }
  1472. if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
  1473. $return_val = $this->result;
  1474. } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
  1475. if ( $this->use_mysqli ) {
  1476. $this->rows_affected = mysqli_affected_rows( $this->dbh );
  1477. } else {
  1478. $this->rows_affected = mysql_affected_rows( $this->dbh );
  1479. }
  1480. // Take note of the insert_id
  1481. if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
  1482. if ( $this->use_mysqli ) {
  1483. $this->insert_id = mysqli_insert_id( $this->dbh );
  1484. } else {
  1485. $this->insert_id = mysql_insert_id( $this->dbh );
  1486. }
  1487. }
  1488. // Return number of rows affected
  1489. $return_val = $this->rows_affected;
  1490. } else {
  1491. $num_rows = 0;
  1492. if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
  1493. while ( $row = @mysqli_fetch_object( $this->result ) ) {
  1494. $this->last_result[$num_rows] = $row;
  1495. $num_rows++;
  1496. }
  1497. } elseif ( is_resource( $this->result ) ) {
  1498. while ( $row = @mysql_fetch_object( $this->result ) ) {
  1499. $this->last_result[$num_rows] = $row;
  1500. $num_rows++;
  1501. }
  1502. }
  1503. // Log number of rows the query returned
  1504. // and return number of rows selected
  1505. $this->num_rows = $num_rows;
  1506. $return_val = $num_rows;
  1507. }
  1508. return $return_val;
  1509. }
  1510. /**
  1511. * Internal function to perform the mysql_query() call.
  1512. *
  1513. * @since 3.9.0
  1514. *
  1515. * @access private
  1516. * @see wpdb::query()
  1517. *
  1518. * @param string $query The query to run.
  1519. */
  1520. private function _do_query( $query ) {
  1521. if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
  1522. $this->timer_start();
  1523. }
  1524. if ( $this->use_mysqli ) {
  1525. $this->result = @mysqli_query( $this->dbh, $query );
  1526. } else {
  1527. $this->result = @mysql_query( $query, $this->dbh );
  1528. }
  1529. $this->num_queries++;
  1530. if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
  1531. $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
  1532. }
  1533. }
  1534. /**
  1535. * Insert a row into a table.
  1536. *
  1537. * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  1538. * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  1539. *
  1540. * @since 2.5.0
  1541. * @see wpdb::prepare()
  1542. * @see wpdb::$field_types
  1543. * @see wp_set_wpdb_vars()
  1544. *
  1545. * @param string $table table name
  1546. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1547. * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
  1548. * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  1549. * @return int|false The number of rows inserted, or false on error.
  1550. */
  1551. public function insert( $table, $data, $format = null ) {
  1552. return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
  1553. }
  1554. /**
  1555. * Replace a row into a table.
  1556. *
  1557. * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  1558. * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  1559. *
  1560. * @since 3.0.0
  1561. * @see wpdb::prepare()
  1562. * @see wpdb::$field_types
  1563. * @see wp_set_wpdb_vars()
  1564. *
  1565. * @param string $table table name
  1566. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1567. * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
  1568. * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  1569. * @return int|false The number of rows affected, or false on error.
  1570. */
  1571. public function replace( $table, $data, $format = null ) {
  1572. return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
  1573. }
  1574. /**
  1575. * Helper function for insert and replace.
  1576. *
  1577. * Runs an insert or replace query based on $type argument.
  1578. *
  1579. * @access private
  1580. * @since 3.0.0
  1581. * @see wpdb::prepare()
  1582. * @see wpdb::$field_types
  1583. * @see wp_set_wpdb_vars()
  1584. *
  1585. * @param string $table table name
  1586. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1587. * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
  1588. * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  1589. * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
  1590. * @return int|false The number of rows affected, or false on error.
  1591. */
  1592. function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
  1593. if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) {
  1594. return false;
  1595. }
  1596. $data = $this->process_fields( $table, $data, $format );
  1597. if ( false === $data ) {
  1598. return false;
  1599. }
  1600. $formats = $values = array();
  1601. foreach ( $data as $value ) {
  1602. $formats[] = $value['format'];
  1603. $values[] = $value['value'];
  1604. }
  1605. $fields = '`' . implode( '`, `', array_keys( $data ) ) . '`';
  1606. $formats = implode( ', ', $formats );
  1607. $sql = "$type INTO `$table` ($fields) VALUES ($formats)";
  1608. $this->insert_id = 0;
  1609. $this->check_current_query = false;
  1610. return $this->query( $this->prepare( $sql, $values ) );
  1611. }
  1612. /**
  1613. * Update a row in the table
  1614. *
  1615. * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
  1616. * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
  1617. *
  1618. * @since 2.5.0
  1619. * @see wpdb::prepare()
  1620. * @see wpdb::$field_types
  1621. * @see wp_set_wpdb_vars()
  1622. *
  1623. * @param string $table table name
  1624. * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1625. * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
  1626. * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
  1627. * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  1628. * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings.
  1629. * @return int|false The number of rows updated, or false on error.
  1630. */
  1631. public function update( $table, $data, $where, $format = null, $where_format = null ) {
  1632. if ( ! is_array( $data ) || ! is_array( $where ) ) {
  1633. return false;
  1634. }
  1635. $data = $this->process_fields( $table, $data, $format );
  1636. if ( false === $data ) {
  1637. return false;
  1638. }
  1639. $where = $this->process_fields( $table, $where, $where_format );
  1640. if ( false === $where ) {
  1641. return false;
  1642. }
  1643. $fields = $conditions = $values = array();
  1644. foreach ( $data as $field => $value ) {
  1645. $fields[] = "`$field` = " . $value['format'];
  1646. $values[] = $value['value'];
  1647. }
  1648. foreach ( $where as $field => $value ) {
  1649. $conditions[] = "`$field` = " . $value['format'];
  1650. $values[] = $value['value'];
  1651. }
  1652. $fields = implode( ', ', $fields );
  1653. $conditions = implode( ' AND ', $conditions );
  1654. $sql = "UPDATE `$table` SET $fields WHERE $conditions";
  1655. $this->check_current_query = false;
  1656. return $this->query( $this->prepare( $sql, $values ) );
  1657. }
  1658. /**
  1659. * Delete a row in the table
  1660. *
  1661. * wpdb::delete( 'table', array( 'ID' => 1 ) )
  1662. * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
  1663. *
  1664. * @since 3.4.0
  1665. * @see wpdb::prepare()
  1666. * @see wpdb::$field_types
  1667. * @see wp_set_wpdb_vars()
  1668. *
  1669. * @param string $table table name
  1670. * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
  1671. * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
  1672. * @return int|false The number of rows updated, or false on error.
  1673. */
  1674. public function delete( $table, $where, $where_format = null ) {
  1675. if ( ! is_array( $where ) ) {
  1676. return false;
  1677. }
  1678. $where = $this->process_fields( $table, $where, $where_format );
  1679. if ( false === $where ) {
  1680. return false;
  1681. }
  1682. $conditions = $values = array();
  1683. foreach ( $where as $field => $value ) {
  1684. $conditions[] = "`$field` = " . $value['format'];
  1685. $values[] = $value['value'];
  1686. }
  1687. $conditions = implode( ' AND ', $conditions );
  1688. $sql = "DELETE FROM `$table` WHERE $conditions";
  1689. $this->check_current_query = false;
  1690. return $this->query( $this->prepare( $sql, $values ) );
  1691. }
  1692. /**
  1693. * Processes arrays of field/value pairs and field formats.
  1694. *
  1695. * This is a helper method for wpdb's CRUD methods, which take field/value
  1696. * pairs for inserts, updates, and where clauses. This method first pairs
  1697. * each value with a format. Then it determines the charset of that field,
  1698. * using that to determine if any invalid text would be stripped. If text is
  1699. * stripped, then field processing is rejected and the query fails.
  1700. *
  1701. * @since 4.2.0
  1702. * @access protected
  1703. *
  1704. * @param string $table Table name.
  1705. * @param array $data Field/value pair.
  1706. * @param mixed $format Format for each field.
  1707. * @return array|bool Returns an array of fields that contain paired values
  1708. * and formats. Returns false for invalid values.
  1709. */
  1710. protected function process_fields( $table, $data, $format ) {
  1711. $data = $this->process_field_formats( $data, $format );
  1712. $data = $this->process_field_charsets( $data, $table );
  1713. if ( false === $data ) {
  1714. return false;
  1715. }
  1716. $converted_data = $this->strip_invalid_text( $data );
  1717. if ( $data !== $converted_data ) {
  1718. return false;
  1719. }
  1720. return $data;
  1721. }
  1722. /**
  1723. * Prepares arrays of value/format pairs as passed to wpdb CRUD methods.
  1724. *
  1725. * @since 4.2.0
  1726. * @access protected
  1727. *
  1728. * @param array $data Array of fields to values.
  1729. * @param mixed $format Formats to be mapped to the values in $data.
  1730. * @return array Array, keyed by field names with values being an array
  1731. * of 'value' and 'format' keys.
  1732. */
  1733. protected function process_field_formats( $data, $format ) {
  1734. $formats = $original_formats = (array) $format;
  1735. foreach ( $data as $field => $value ) {
  1736. $value = array(
  1737. 'value' => $value,
  1738. 'format' => '%s',
  1739. );
  1740. if ( ! empty( $format ) ) {
  1741. $value['format'] = array_shift( $formats );
  1742. if ( ! $value['format'] ) {
  1743. $value['format'] = reset( $original_formats );
  1744. }
  1745. } elseif ( isset( $this->field_types[ $field ] ) ) {
  1746. $value['format'] = $this->field_types[ $field ];
  1747. }
  1748. $data[ $field ] = $value;
  1749. }
  1750. return $data;
  1751. }
  1752. /**
  1753. * Adds field charsets to field/value/format arrays generated by
  1754. * the {@see wpdb::process_field_formats()} method.
  1755. *
  1756. * @since 4.2.0
  1757. * @access protected
  1758. *
  1759. * @param array $data As it comes from the {@see wpdb::process_field_formats()} method.
  1760. * @param string $table Table name.
  1761. * @return The same array as $data with additional 'charset' keys.
  1762. */
  1763. protected function process_field_charsets( $data, $table ) {
  1764. foreach ( $data as $field => $value ) {
  1765. if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
  1766. // We can skip this field if we know it isn't a string.
  1767. // This checks %d/%f versus ! %s because it's sprintf() could take more.
  1768. $value['charset'] = false;
  1769. } elseif ( $this->check_ascii( $value['value'] ) ) {
  1770. // If it's ASCII, then we don't need the charset. We can skip this field.
  1771. $value['charset'] = false;
  1772. } else {
  1773. $value['charset'] = $this->get_col_charset( $table, $field );
  1774. if ( is_wp_error( $value['charset'] ) ) {
  1775. return false;
  1776. }
  1777. // This isn't ASCII. Don't have strip_invalid_text() re-check.
  1778. $value['ascii'] = false;
  1779. }
  1780. $data[ $field ] = $value;
  1781. }
  1782. return $data;
  1783. }
  1784. /**
  1785. * Retrieve one variable from the database.
  1786. *
  1787. * Executes a SQL query and returns the value from the SQL result.
  1788. * 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.
  1789. * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
  1790. *
  1791. * @since 0.71
  1792. *
  1793. * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
  1794. * @param int $x Optional. Column of value to return. Indexed from 0.
  1795. * @param int $y Optional. Row of value to return. Indexed from 0.
  1796. * @return string|null Database query result (as string), or null on failure
  1797. */
  1798. public function get_var( $query = null, $x = 0, $y = 0 ) {
  1799. $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
  1800. if ( $query ) {
  1801. $this->query( $query );
  1802. }
  1803. // Extract var out of cached results based x,y vals
  1804. if ( !empty( $this->last_result[$y] ) ) {
  1805. $values = array_values( get_object_vars( $this->last_result[$y] ) );
  1806. }
  1807. // If there is a value return it else return null
  1808. return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
  1809. }
  1810. /**
  1811. * Retrieve one row from the database.
  1812. *
  1813. * Executes a SQL query and returns the row from the SQL result.
  1814. *
  1815. * @since 0.71
  1816. *
  1817. * @param string|null $query SQL query.
  1818. * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...),
  1819. * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
  1820. * @param int $y Optional. Row to return. Indexed from 0.
  1821. * @return mixed Database query result in format specified by $output or null on failure
  1822. */
  1823. public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
  1824. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  1825. if ( $query ) {
  1826. $this->query( $query );
  1827. } else {
  1828. return null;
  1829. }
  1830. if ( !isset( $this->last_result[$y] ) )
  1831. return null;
  1832. if ( $output == OBJECT ) {
  1833. return $this->last_result[$y] ? $this->last_result[$y] : null;
  1834. } elseif ( $output == ARRAY_A ) {
  1835. return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
  1836. } elseif ( $output == ARRAY_N ) {
  1837. return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
  1838. } elseif ( strtoupper( $output ) === OBJECT ) {
  1839. // Back compat for OBJECT being previously case insensitive.
  1840. return $this->last_result[$y] ? $this->last_result[$y] : null;
  1841. } else {
  1842. $this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" );
  1843. }
  1844. }
  1845. /**
  1846. * Retrieve one column from the database.
  1847. *
  1848. * Executes a SQL query and returns the column from the SQL result.
  1849. * If the SQL result contains more than one column, this function returns the column specified.
  1850. * If $query is null, this function returns the specified column from the previous SQL result.
  1851. *
  1852. * @since 0.71
  1853. *
  1854. * @param string|null $query Optional. SQL query. Defaults to previous query.
  1855. * @param int $x Optional. Column to return. Indexed from 0.
  1856. * @return array Database query result. Array indexed from 0 by SQL result row number.
  1857. */
  1858. public function get_col( $query = null , $x = 0 ) {
  1859. if ( $query ) {
  1860. $this->query( $query );
  1861. }
  1862. $new_array = array();
  1863. // Extract the column values
  1864. for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
  1865. $new_array[$i] = $this->get_var( null, $x, $i );
  1866. }
  1867. return $new_array;
  1868. }
  1869. /**
  1870. * Retrieve an entire SQL result set from the database (i.e., many rows)
  1871. *
  1872. * Executes a SQL query and returns the entire SQL result.
  1873. *
  1874. * @since 0.71
  1875. *
  1876. * @param string $query SQL query.
  1877. * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number.
  1878. * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively.
  1879. * With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded.
  1880. * @return mixed Database query results
  1881. */
  1882. public function get_results( $query = null, $output = OBJECT ) {
  1883. $this->func_call = "\$db->get_results(\"$query\", $output)";
  1884. if ( $query ) {
  1885. $this->query( $query );
  1886. } else {
  1887. return null;
  1888. }
  1889. $new_array = array();
  1890. if ( $output == OBJECT ) {
  1891. // Return an integer-keyed array of row objects
  1892. return $this->last_result;
  1893. } elseif ( $output == OBJECT_K ) {
  1894. // Return an array of row objects with keys from column 1
  1895. // (Duplicates are discarded)
  1896. foreach ( $this->last_result as $row ) {
  1897. $var_by_ref = get_object_vars( $row );
  1898. $key = array_shift( $var_by_ref );
  1899. if ( ! isset( $new_array[ $key ] ) )
  1900. $new_array[ $key ] = $row;
  1901. }
  1902. return $new_array;
  1903. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  1904. // Return an integer-keyed array of...
  1905. if ( $this->last_result ) {
  1906. foreach( (array) $this->last_result as $row ) {
  1907. if ( $output == ARRAY_N ) {
  1908. // ...integer-keyed row arrays
  1909. $new_array[] = array_values( get_object_vars( $row ) );
  1910. } else {
  1911. // ...column name-keyed row arrays
  1912. $new_array[] = get_object_vars( $row );
  1913. }
  1914. }
  1915. }
  1916. return $new_array;
  1917. } elseif ( strtoupper( $output ) === OBJECT ) {
  1918. // Back compat for OBJECT being previously case insensitive.
  1919. return $this->last_result;
  1920. }
  1921. return null;
  1922. }
  1923. /**
  1924. * Retrieves the character set for the given table.
  1925. *
  1926. * @since 4.2.0
  1927. * @access protected
  1928. *
  1929. * @param string $table Table name.
  1930. * @return string|WP_Error Table character set, {@see WP_Error} object if it couldn't be found.
  1931. */
  1932. protected function get_table_charset( $table ) {
  1933. $tablekey = strtolower( $table );
  1934. /**
  1935. * Filter the table charset value before the DB is checked.
  1936. *
  1937. * Passing a non-null value to the filter will effectively short-circuit
  1938. * checking the DB for the charset, returning that value instead.
  1939. *
  1940. * @since 4.2.0
  1941. *
  1942. * @param string $charset The character set to use. Default null.
  1943. * @param string $table The name of the table being checked.
  1944. */
  1945. $charset = apply_filters( 'pre_get_table_charset', null, $table );
  1946. if ( null !== $charset ) {
  1947. return $charset;
  1948. }
  1949. if ( isset( $this->table_charset[ $tablekey ] ) ) {
  1950. return $this->table_charset[ $tablekey ];
  1951. }
  1952. $charsets = $columns = array();
  1953. $results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
  1954. if ( ! $results ) {
  1955. return new WP_Error( 'wpdb_get_table_charset_failure' );
  1956. }
  1957. foreach ( $results as $column ) {
  1958. $columns[ strtolower( $column->Field ) ] = $column;
  1959. }
  1960. $this->col_meta[ $tablekey ] = $columns;
  1961. foreach ( $columns as $column ) {
  1962. if ( ! empty( $column->Collation ) ) {
  1963. list( $charset ) = explode( '_', $column->Collation );
  1964. $charsets[ strtolower( $charset ) ] = true;
  1965. }
  1966. list( $type ) = explode( '(', $column->Type );
  1967. // A binary/blob means the whole query gets treated like this.
  1968. if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ) ) ) {
  1969. $this->table_charset[ $tablekey ] = 'binary';
  1970. return 'binary';
  1971. }
  1972. }
  1973. // utf8mb3 is an alias for utf8.
  1974. if ( isset( $charsets['utf8mb3'] ) ) {
  1975. $charsets['utf8'] = true;
  1976. unset( $charsets['utf8mb3'] );
  1977. }
  1978. // Check if we have more than one charset in play.
  1979. $count = count( $charsets );
  1980. if ( 1 === $count ) {
  1981. $charset = key( $charsets );
  1982. } elseif ( 0 === $count ) {
  1983. // No charsets, assume this table can store whatever.
  1984. $charset = false;
  1985. } else {
  1986. // More than one charset. Remove latin1 if present and recalculate.
  1987. unset( $charsets['latin1'] );
  1988. $count = count( $charsets );
  1989. if ( 1 === $count ) {
  1990. // Only one charset (besides latin1).
  1991. $charset = key( $charsets );
  1992. } elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) {
  1993. // Two charsets, but they're utf8 and utf8mb4, use utf8.
  1994. $charset = 'utf8';
  1995. } else {
  1996. // Two mixed character sets. ascii.
  1997. $charset = 'ascii';
  1998. }
  1999. }
  2000. $this->table_charset[ $tablekey ] = $charset;
  2001. return $charset;
  2002. }
  2003. /**
  2004. * Retrieves the character set for the given column.
  2005. *
  2006. * @since 4.2.0
  2007. * @access protected
  2008. *
  2009. * @param string $table Table name.
  2010. * @param string $column Column name.
  2011. * @return mixed Column character set as a string. False if the column has no
  2012. * character set. {@see WP_Error} object if there was an error.
  2013. */
  2014. protected function get_col_charset( $table, $column ) {
  2015. $tablekey = strtolower( $table );
  2016. $columnkey = strtolower( $column );
  2017. /**
  2018. * Filter the column charset value before the DB is checked.
  2019. *
  2020. * Passing a non-null value to the filter will short-circuit
  2021. * checking the DB for the charset, returning that value instead.
  2022. *
  2023. * @since 4.2.0
  2024. *
  2025. * @param string $charset The character set to use. Default null.
  2026. * @param string $table The name of the table being checked.
  2027. * @param string $column The name of the column being checked.
  2028. */
  2029. $charset = apply_filters( 'pre_get_col_charset', null, $table, $column );
  2030. if ( null !== $charset ) {
  2031. return $charset;
  2032. }
  2033. // Skip this entirely if this isn't a MySQL database.
  2034. if ( false === $this->is_mysql ) {
  2035. return false;
  2036. }
  2037. if ( empty( $this->table_charset[ $tablekey ] ) ) {
  2038. // This primes column information for us.
  2039. $table_charset = $this->get_table_charset( $table );
  2040. if ( is_wp_error( $table_charset ) ) {
  2041. return $table_charset;
  2042. }
  2043. }
  2044. // If still no column information, return the table charset.
  2045. if ( empty( $this->col_meta[ $tablekey ] ) ) {
  2046. return $this->table_charset[ $tablekey ];
  2047. }
  2048. // If this column doesn't exist, return the table charset.
  2049. if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
  2050. return $this->table_charset[ $tablekey ];
  2051. }
  2052. // Return false when it's not a string column.
  2053. if ( empty( $this->col_meta[ $tablekey ][ $columnkey ]->Collation ) ) {
  2054. return false;
  2055. }
  2056. list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation );
  2057. return $charset;
  2058. }
  2059. /**
  2060. * Check if a string is ASCII.
  2061. *
  2062. * The negative regex is faster for non-ASCII strings, as it allows
  2063. * the search to finish as soon as it encounters a non-ASCII character.
  2064. *
  2065. * @since 4.2.0
  2066. * @access protected
  2067. *
  2068. * @param string $string String to check.
  2069. * @return bool True if ASCII, false if not.
  2070. */
  2071. protected function check_ascii( $string ) {
  2072. if ( function_exists( 'mb_check_encoding' ) ) {
  2073. if ( mb_check_encoding( $string, 'ASCII' ) ) {
  2074. return true;
  2075. }
  2076. } elseif ( ! preg_match( '/[^\x00-\x7F]/', $string ) ) {
  2077. return true;
  2078. }
  2079. return false;
  2080. }
  2081. /**
  2082. * Strips any invalid characters based on value/charset pairs.
  2083. *
  2084. * @since 4.2.0
  2085. * @access protected
  2086. *
  2087. * @param array $data Array of value arrays. Each value array has the keys
  2088. * 'value' and 'charset'. An optional 'ascii' key can be
  2089. * set to false to avoid redundant ASCII checks.
  2090. * @return array|WP_Error The $data parameter, with invalid characters removed from
  2091. * each value. This works as a passthrough: any additional keys
  2092. * such as 'field' are retained in each value array. If we cannot
  2093. * remove invalid characters, a {@see WP_Error} object is returned.
  2094. */
  2095. protected function strip_invalid_text( $data ) {
  2096. // Some multibyte character sets that we can check in PHP.
  2097. $mb_charsets = array(
  2098. 'ascii' => 'ASCII',
  2099. 'big5' => 'BIG-5',
  2100. 'eucjpms' => 'eucJP-win',
  2101. 'gb2312' => 'EUC-CN',
  2102. 'ujis' => 'EUC-JP',
  2103. 'utf32' => 'UTF-32',
  2104. 'utf8mb4' => 'UTF-8',
  2105. );
  2106. $supported_charsets = array();
  2107. if ( function_exists( 'mb_list_encodings' ) ) {
  2108. $supported_charsets = mb_list_encodings();
  2109. }
  2110. $db_check_string = false;
  2111. foreach ( $data as &$value ) {
  2112. $charset = $value['charset'];
  2113. // Column isn't a string, or is latin1, which will will happily store anything.
  2114. if ( false === $charset || 'latin1' === $charset ) {
  2115. continue;
  2116. }
  2117. if ( ! is_string( $value['value'] ) ) {
  2118. continue;
  2119. }
  2120. // ASCII is always OK.
  2121. if ( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) ) {
  2122. continue;
  2123. }
  2124. // Convert the text locally.
  2125. if ( $supported_charsets ) {
  2126. if ( isset( $mb_charsets[ $charset ] ) && in_array( $mb_charsets[ $charset ], $supported_charsets ) ) {
  2127. $value['value'] = mb_convert_encoding( $value['value'], $mb_charsets[ $charset ], $mb_charsets[ $charset ] );
  2128. continue;
  2129. }
  2130. }
  2131. // utf8(mb3) can be handled by regex, which is a bunch faster than a DB lookup.
  2132. if ( 'utf8' === $charset || 'utf8mb3' === $charset ) {
  2133. $regex = '/
  2134. (
  2135. (?: [\x00-\x7F] # single-byte sequences 0xxxxxxx
  2136. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  2137. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
  2138. | [\xE1-\xEC][\x80-\xBF]{2}
  2139. | \xED[\x80-\x9F][\x80-\xBF]
  2140. | [\xEE-\xEF][\x80-\xBF]{2}
  2141. ){1,50} # ...one or more times
  2142. )
  2143. | . # anything else
  2144. /x';
  2145. $value['value'] = preg_replace( $regex, '$1', $value['value'] );
  2146. continue;
  2147. }
  2148. // We couldn't use any local conversions, send it to the DB.
  2149. $value['db'] = $db_check_string = true;
  2150. }
  2151. unset( $value ); // Remove by reference.
  2152. if ( $db_check_string ) {
  2153. $queries = array();
  2154. foreach ( $data as $col => $value ) {
  2155. if ( ! empty( $value['db'] ) ) {
  2156. if ( ! isset( $queries[ $value['charset'] ] ) ) {
  2157. $queries[ $value['charset'] ] = array();
  2158. }
  2159. // Split the CONVERT() calls by charset, so we can make sure the connection is right
  2160. $queries[ $value['charset'] ][ $col ] = $this->prepare( "CONVERT( %s USING {$value['charset']} )", $value['value'] );
  2161. }
  2162. }
  2163. $connection_charset = $this->charset;
  2164. foreach ( $queries as $charset => $query ) {
  2165. if ( ! $query ) {
  2166. continue;
  2167. }
  2168. // Change the charset to match the string(s) we're converting
  2169. if ( $charset !== $this->charset ) {
  2170. $this->set_charset( $this->dbh, $charset );
  2171. }
  2172. $this->check_current_query = false;
  2173. $row = $this->get_row( "SELECT " . implode( ', ', $query ), ARRAY_N );
  2174. if ( ! $row ) {
  2175. $this->set_charset( $this->dbh, $connection_charset );
  2176. return new WP_Error( 'wpdb_strip_invalid_text_failure' );
  2177. }
  2178. $cols = array_keys( $query );
  2179. $col_count = count( $cols );
  2180. for ( $ii = 0; $ii < $col_count; $ii++ ) {
  2181. $data[ $cols[ $ii ] ]['value'] = $row[ $ii ];
  2182. }
  2183. }
  2184. // Don't forget to change the charset back!
  2185. if ( $connection_charset !== $this->charset ) {
  2186. $this->set_charset( $this->dbh, $connection_charset );
  2187. }
  2188. }
  2189. return $data;
  2190. }
  2191. /**
  2192. * Strips any invalid characters from the query.
  2193. *
  2194. * @since 4.2.0
  2195. * @access protected
  2196. *
  2197. * @param string $query Query to convert.
  2198. * @return string|WP_Error The converted query, or a {@see WP_Error} object if the conversion fails.
  2199. */
  2200. protected function strip_invalid_text_from_query( $query ) {
  2201. $table = $this->get_table_from_query( $query );
  2202. if ( $table ) {
  2203. $charset = $this->get_table_charset( $table );
  2204. if ( is_wp_error( $charset ) ) {
  2205. return $charset;
  2206. }
  2207. // We can't reliably strip text from tables containing binary/blob columns
  2208. if ( 'binary' === $charset ) {
  2209. return $query;
  2210. }
  2211. } else {
  2212. $charset = $this->charset;
  2213. }
  2214. $data = array(
  2215. 'value' => $query,
  2216. 'charset' => $charset,
  2217. 'ascii' => false,
  2218. );
  2219. $data = $this->strip_invalid_text( array( $data ) );
  2220. if ( is_wp_error( $data ) ) {
  2221. return $data;
  2222. }
  2223. return $data[0]['value'];
  2224. }
  2225. /**
  2226. * Strips any invalid characters from the string for a given table and column.
  2227. *
  2228. * @since 4.2.0
  2229. * @access public
  2230. *
  2231. * @param string $table Table name.
  2232. * @param string $column Column name.
  2233. * @param string $value The text to check.
  2234. * @return string|WP_Error The converted string, or a `WP_Error` object if the conversion fails.
  2235. */
  2236. public function strip_invalid_text_for_column( $table, $column, $value ) {
  2237. if ( ! is_string( $value ) || $this->check_ascii( $value ) ) {
  2238. return $value;
  2239. }
  2240. $charset = $this->get_col_charset( $table, $column );
  2241. if ( ! $charset ) {
  2242. // Not a string column.
  2243. return $value;
  2244. } elseif ( is_wp_error( $charset ) ) {
  2245. // Bail on real errors.
  2246. return $charset;
  2247. }
  2248. $data = array(
  2249. $column => array(
  2250. 'value' => $value,
  2251. 'charset' => $charset,
  2252. 'ascii' => false,
  2253. )
  2254. );
  2255. $data = $this->strip_invalid_text( $data );
  2256. if ( is_wp_error( $data ) ) {
  2257. return $data;
  2258. }
  2259. return $data[ $column ]['value'];
  2260. }
  2261. /**
  2262. * Find the first table name referenced in a query.
  2263. *
  2264. * @since 4.2.0
  2265. * @access protected
  2266. *
  2267. * @param string $query The query to search.
  2268. * @return string|false $table The table name found, or false if a table couldn't be found.
  2269. */
  2270. protected function get_table_from_query( $query ) {
  2271. // Remove characters that can legally trail the table name.
  2272. $query = rtrim( $query, ';/-#' );
  2273. // Allow (select...) union [...] style queries. Use the first query's table name.
  2274. $query = ltrim( $query, "\r\n\t (" );
  2275. /*
  2276. * Strip everything between parentheses except nested selects and use only 1,000
  2277. * chars of the query.
  2278. */
  2279. $query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', substr( $query, 0, 1000 ) );
  2280. // Quickly match most common queries.
  2281. if ( preg_match( '/^\s*(?:'
  2282. . 'SELECT.*?\s+FROM'
  2283. . '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
  2284. . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
  2285. . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
  2286. . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
  2287. . ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) {
  2288. return $maybe[1];
  2289. }
  2290. // SHOW TABLE STATUS and SHOW TABLES
  2291. if ( preg_match( '/^\s*(?:'
  2292. . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
  2293. . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
  2294. . ')\W([\w-]+)\W/is', $query, $maybe ) ) {
  2295. return $maybe[1];
  2296. }
  2297. // Big pattern for the rest of the table-related queries.
  2298. if ( preg_match( '/^\s*(?:'
  2299. . '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
  2300. . '|DESCRIBE|DESC|EXPLAIN|HANDLER'
  2301. . '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
  2302. . '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE'
  2303. . '|TRUNCATE(?:\s+TABLE)?'
  2304. . '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?'
  2305. . '|ALTER(?:\s+IGNORE)?\s+TABLE'
  2306. . '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?'
  2307. . '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON'
  2308. . '|DROP\s+INDEX.*\s+ON'
  2309. . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
  2310. . '|(?:GRANT|REVOKE).*ON\s+TABLE'
  2311. . '|SHOW\s+(?:.*FROM|.*TABLE)'
  2312. . ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) {
  2313. return $maybe[1];
  2314. }
  2315. return false;
  2316. }
  2317. /**
  2318. * Load the column metadata from the last query.
  2319. *
  2320. * @since 3.5.0
  2321. *
  2322. * @access protected
  2323. */
  2324. protected function load_col_info() {
  2325. if ( $this->col_info )
  2326. return;
  2327. if ( $this->use_mysqli ) {
  2328. for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) {
  2329. $this->col_info[ $i ] = @mysqli_fetch_field( $this->result );
  2330. }
  2331. } else {
  2332. for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
  2333. $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
  2334. }
  2335. }
  2336. }
  2337. /**
  2338. * Retrieve column metadata from the last query.
  2339. *
  2340. * @since 0.71
  2341. *
  2342. * @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
  2343. * @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
  2344. * @return mixed Column Results
  2345. */
  2346. public function get_col_info( $info_type = 'name', $col_offset = -1 ) {
  2347. $this->load_col_info();
  2348. if ( $this->col_info ) {
  2349. if ( $col_offset == -1 ) {
  2350. $i = 0;
  2351. $new_array = array();
  2352. foreach( (array) $this->col_info as $col ) {
  2353. $new_array[$i] = $col->{$info_type};
  2354. $i++;
  2355. }
  2356. return $new_array;
  2357. } else {
  2358. return $this->col_info[$col_offset]->{$info_type};
  2359. }
  2360. }
  2361. }
  2362. /**
  2363. * Starts the timer, for debugging purposes.
  2364. *
  2365. * @since 1.5.0
  2366. *
  2367. * @return bool
  2368. */
  2369. public function timer_start() {
  2370. $this->time_start = microtime( true );
  2371. return true;
  2372. }
  2373. /**
  2374. * Stops the debugging timer.
  2375. *
  2376. * @since 1.5.0
  2377. *
  2378. * @return float Total time spent on the query, in seconds
  2379. */
  2380. public function timer_stop() {
  2381. return ( microtime( true ) - $this->time_start );
  2382. }
  2383. /**
  2384. * Wraps errors in a nice header and footer and dies.
  2385. *
  2386. * Will not die if wpdb::$show_errors is false.
  2387. *
  2388. * @since 1.5.0
  2389. *
  2390. * @param string $message The Error message
  2391. * @param string $error_code Optional. A Computer readable string to identify the error.
  2392. * @return false|void
  2393. */
  2394. public function bail( $message, $error_code = '500' ) {
  2395. if ( !$this->show_errors ) {
  2396. if ( class_exists( 'WP_Error' ) )
  2397. $this->error = new WP_Error($error_code, $message);
  2398. else
  2399. $this->error = $message;
  2400. return false;
  2401. }
  2402. wp_die($message);
  2403. }
  2404. /**
  2405. * Whether MySQL database is at least the required minimum version.
  2406. *
  2407. * @since 2.5.0
  2408. * @uses $wp_version
  2409. * @uses $required_mysql_version
  2410. *
  2411. * @return WP_Error
  2412. */
  2413. public function check_database_version() {
  2414. global $wp_version, $required_mysql_version;
  2415. // Make sure the server has the required MySQL version
  2416. if ( version_compare($this->db_version(), $required_mysql_version, '<') )
  2417. return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
  2418. }
  2419. /**
  2420. * Whether the database supports collation.
  2421. *
  2422. * Called when WordPress is generating the table scheme.
  2423. *
  2424. * @since 2.5.0
  2425. * @deprecated 3.5.0
  2426. * @deprecated Use wpdb::has_cap( 'collation' )
  2427. *
  2428. * @return bool True if collation is supported, false if version does not
  2429. */
  2430. public function supports_collation() {
  2431. _deprecated_function( __FUNCTION__, '3.5', 'wpdb::has_cap( \'collation\' )' );
  2432. return $this->has_cap( 'collation' );
  2433. }
  2434. /**
  2435. * The database character collate.
  2436. *
  2437. * @since 3.5.0
  2438. *
  2439. * @return string The database character collate.
  2440. */
  2441. public function get_charset_collate() {
  2442. $charset_collate = '';
  2443. if ( ! empty( $this->charset ) )
  2444. $charset_collate = "DEFAULT CHARACTER SET $this->charset";
  2445. if ( ! empty( $this->collate ) )
  2446. $charset_collate .= " COLLATE $this->collate";
  2447. return $charset_collate;
  2448. }
  2449. /**
  2450. * Determine if a database supports a particular feature.
  2451. *
  2452. * @since 2.7.0
  2453. * @since 4.1.0 Support was added for the 'utf8mb4' feature.
  2454. *
  2455. * @see wpdb::db_version()
  2456. *
  2457. * @param string $db_cap The feature to check for. Accepts 'collation',
  2458. * 'group_concat', 'subqueries', 'set_charset',
  2459. * or 'utf8mb4'.
  2460. * @return bool Whether the database feature is supported, false otherwise.
  2461. */
  2462. public function has_cap( $db_cap ) {
  2463. $version = $this->db_version();
  2464. switch ( strtolower( $db_cap ) ) {
  2465. case 'collation' : // @since 2.5.0
  2466. case 'group_concat' : // @since 2.7.0
  2467. case 'subqueries' : // @since 2.7.0
  2468. return version_compare( $version, '4.1', '>=' );
  2469. case 'set_charset' :
  2470. return version_compare( $version, '5.0.7', '>=' );
  2471. case 'utf8mb4' : // @since 4.1.0
  2472. return version_compare( $version, '5.5.3', '>=' );
  2473. }
  2474. return false;
  2475. }
  2476. /**
  2477. * Retrieve the name of the function that called wpdb.
  2478. *
  2479. * Searches up the list of functions until it reaches
  2480. * the one that would most logically had called this method.
  2481. *
  2482. * @since 2.5.0
  2483. *
  2484. * @return string The name of the calling function
  2485. */
  2486. public function get_caller() {
  2487. return wp_debug_backtrace_summary( __CLASS__ );
  2488. }
  2489. /**
  2490. * The database version number.
  2491. *
  2492. * @since 2.7.0
  2493. *
  2494. * @return null|string Null on failure, version number on success.
  2495. */
  2496. public function db_version() {
  2497. if ( $this->use_mysqli ) {
  2498. $server_info = mysqli_get_server_info( $this->dbh );
  2499. } else {
  2500. $server_info = mysql_get_server_info( $this->dbh );
  2501. }
  2502. return preg_replace( '/[^0-9.].*/', '', $server_info );
  2503. }
  2504. }