PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.9.5
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Database / DbInfo.php
wp-staging / Framework / Database Last commit date
Exporter 1 month ago QueryBuilder 2 years ago CustomTable.php 3 months ago DbInfo.php 5 days ago ExcludedTables.php 6 months ago ExternalDatabaseConfiguration.php 5 days ago OptionPreservationHandler.php 2 years ago SearchReplace.php 3 months ago SelectedTables.php 1 month ago TableDto.php 1 year ago TableService.php 10 months ago TablesRenamer.php 5 days ago WpDbInfo.php 10 months ago WpOptionsInfo.php 1 year ago iDbInfo.php 2 years ago
DbInfo.php
114 lines
1 <?php
2
3 namespace WPStaging\Framework\Database;
4
5 use wpdb;
6 use WPStaging\Framework\Adapter\Database\DatabaseException;
7
8 /**
9 * Creates database metadata access for an external WordPress database.
10 */
11 class DbInfo extends WpDbInfo
12 {
13 /**
14 * @var string
15 */
16 protected $server;
17
18 /**
19 * @var string
20 */
21 protected $user;
22
23 /**
24 * @var string
25 */
26 protected $password;
27
28 /**
29 * @var string
30 */
31 protected $database;
32
33 /**
34 * @var bool
35 */
36 protected $useSsl;
37
38 /**
39 * @param string $hostServer
40 * @param string $user
41 * @param string $password
42 * @param string $database
43 * @param bool $useSsl
44 * @throws DatabaseException
45 */
46 public function __construct(string $hostServer, string $user, string $password, string $database, bool $useSsl = false)
47 {
48 $this->server = $hostServer;
49 $this->user = $user;
50 $this->password = $password;
51 $this->database = $database;
52 $this->useSsl = $useSsl;
53
54 parent::__construct($this->connect());
55 }
56
57 /**
58 * @return wpdb
59 * @throws DatabaseException
60 */
61 public function connect()
62 {
63 (new ExternalDatabaseConfiguration())->validateConnectionTarget([
64 'databaseServer' => $this->server,
65 'databaseUser' => $this->user,
66 'databaseDatabase' => $this->database,
67 ]);
68
69 $db = mysqli_init();
70 if ($db === false) {
71 throw new DatabaseException('Unable to initialize MySQL connection.');
72 }
73
74 $connected = false;
75 $connectionFailure = null;
76
77 set_error_handler(function () {
78 return true;
79 });
80
81 try {
82 if ($this->useSsl) {
83 // wpdb requires this constant for SSL use
84 if (!defined('MYSQL_CLIENT_FLAGS')) {
85 // phpcs:disable PHPCompatibility.Constants.NewConstants.mysqli_client_ssl_dont_verify_server_certFound
86 define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);
87 }
88
89 // @phpstan-ignore-next-line - null is valid for port and socket parameters
90 $connected = $db->real_connect($this->server, $this->user, $this->password, $this->database, null, null, MYSQL_CLIENT_FLAGS);
91 } else {
92 $connected = $db->real_connect($this->server, $this->user, $this->password, $this->database);
93 }
94 } catch (\Throwable $e) {
95 $connectionFailure = $e;
96 } finally {
97 restore_error_handler();
98 }
99
100 if ($connectionFailure !== null) {
101 throw new DatabaseException('Connect Error (' . $connectionFailure->getCode() . ') ' . $connectionFailure->getMessage());
102 }
103
104 if (!$connected || $db->connect_error) {
105 throw new DatabaseException('Connect Error (' . $db->connect_errno . ') ' . $db->connect_error);
106 }
107
108 $db->close();
109
110 $wpdb = new wpdb($this->user, $this->password, $this->database, $this->server);
111 return $wpdb;
112 }
113 }
114