PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.41
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.41
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Connection / WPDADB_WPDB.php

WPDADB_WPDB.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.41, at WPDataAccess/Connection/WPDADB_WPDB.php

307 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:ignore Standard.Category.SniffName.ErrorCode
4 namespace WPDataAccess\Connection;
5
6 /**
7 * This class extends WodPress class wpdb to enable remote connections and connections to local databases.
8 */
9 class WPDADB_WPDB extends \wpdb {
10 /**
11 * Indicator: ON = use SSL
12 *
13 * @var string
14 */
15 private $ssl;
16
17 /**
18 * SSL key
19 *
20 * @var string
21 */
22 private $ssl_key;
23
24 /**
25 * SSL certificate
26 *
27 * @var string
28 */
29 private $ssl_cert;
30
31 /**
32 * SSL CA
33 *
34 * @var string
35 */
36 private $ssl_ca;
37
38 /**
39 * SSL path
40 *
41 * @var string
42 */
43 private $ssl_path;
44
45 /**
46 * SSL cipher
47 *
48 * @var string
49 */
50 private $ssl_cipher;
51
52 /**
53 * Overwrite WPDADB_WPDB constructor
54 *
55 * Save schema_name
56 *
57 * @param string $dbuser Username (original wpdb parameter).
58 * @param string $dbpassword Password (original wpdb parameter).
59 * @param string $dbname Database schema name (original wpdb parameter).
60 * @param string $dbhost Host name (original wpdb parameter).
61 * @param string $ssl SSL on|off.
62 * @param string $ssl_key SSL key.
63 * @param string $ssl_cert SSL certificate.
64 * @param string $ssl_ca SSL CA.
65 * @param string $ssl_path SSL path.
66 * @param string $ssl_cipher SSL cipher.
67 */
68 public function __construct(
69 $dbuser,
70 $dbpassword,
71 $dbname,
72 $dbhost,
73 // Original WordPress wpdb parameters
74 $ssl = 'OFF',
75 $ssl_key = null,
76 $ssl_cert = null,
77 $ssl_ca = null,
78 $ssl_path = null,
79 $ssl_cipher = null
80 ) {
81 $this->ssl = $ssl;
82 $this->ssl_key = $ssl_key;
83 $this->ssl_cert = $ssl_cert;
84 $this->ssl_ca = $ssl_ca;
85 $this->ssl_path = $ssl_path;
86 $this->ssl_cipher = $ssl_cipher;
87 if ( '' === trim( (string) $this->ssl_path ) ) {
88 $this->ssl_path = null;
89 }
90 if ( '' === trim( (string) $this->ssl_cipher ) ) {
91 $this->ssl_cipher = null;
92 }
93 parent::__construct(
94 $dbuser,
95 $dbpassword,
96 $dbname,
97 $dbhost
98 );
99 }
100
101 /**
102 * Is connection established?
103 *
104 * @return bool
105 */
106 public function is_connected() {
107 return 0 === $this->dbh->connect_errno;
108 }
109
110 /**
111 * Overwrite method
112 *
113 * This just adds a call to private method @ssl()
114 *
115 * @param bool $allow_bail Allow bail.
116 *
117 * @return bool
118 */
119 public function db_connect( $allow_bail = false ) {
120 $this->is_mysql = true;
121 $client_flags = ( defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0 );
122 /*
123 * Set the MySQLi error reporting off because WordPress handles its own.
124 * This is due to the default value change from `MYSQLI_REPORT_OFF`
125 * to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1.
126 */
127 mysqli_report( MYSQLI_REPORT_OFF );
128 $this->dbh = mysqli_init();
129 $host = $this->dbhost;
130 $port = null;
131 $socket = null;
132 $is_ipv6 = false;
133 $host_data = $this->parse_db_host( $this->dbhost );
134 if ( $host_data ) {
135 list( $host, $port, $socket, $is_ipv6 ) = $host_data;
136 // phpcs:ignore - 8.1 proof
137 }
138 /*
139 * If using the `mysqlnd` library, the IPv6 address needs to be enclosed
140 * in square brackets, whereas it doesn't while using the `libmysqlclient` library.
141 * @see https://bugs.php.net/bug.php?id=67563
142 */
143 if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) {
144 $host = "[{$host}]";
145 }
146 if ( 'on' === $this->ssl ) {
147 mysqli_ssl_set(
148 // phpcs:ignore
149 $this->dbh,
150 $this->ssl_key,
151 $this->ssl_cert,
152 $this->ssl_ca,
153 $this->ssl_path,
154 $this->ssl_cipher
155 );
156 }
157 mysqli_report( MYSQLI_REPORT_STRICT );
158 // phpcs:ignore
159 try {
160 if ( WP_DEBUG ) {
161 if ( !mysqli_real_connect(
162 $this->dbh,
163 $host,
164 $this->dbuser,
165 $this->dbpassword,
166 null,
167 $port,
168 $socket,
169 $client_flags
170 ) ) {
171 // phpcs:ignore
172 mysqli_report( MYSQLI_REPORT_OFF );
173 // phpcs:ignore
174 return false;
175 }
176 } else {
177 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
178 if ( !@mysqli_real_connect(
179 $this->dbh,
180 $host,
181 $this->dbuser,
182 $this->dbpassword,
183 null,
184 $port,
185 $socket,
186 $client_flags
187 ) ) {
188 // phpcs:ignore
189 mysqli_report( MYSQLI_REPORT_OFF );
190 // phpcs:ignore
191 return false;
192 }
193 }
194 } catch ( \mysqli_sql_exception $e ) {
195 mysqli_report( MYSQLI_REPORT_OFF );
196 // phpcs:ignore
197 return false;
198 }
199 mysqli_report( MYSQLI_REPORT_OFF );
200 // phpcs:ignore
201 if ( $this->dbh->connect_errno ) {
202 $this->dbh = null;
203 /*
204 * It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if:
205 * - We haven't previously connected, and
206 * - WP_USE_EXT_MYSQL isn't set to false, and
207 * - ext/mysql is loaded.
208 */
209 $attempt_fallback = true;
210 if ( $this->has_connected ) {
211 $attempt_fallback = false;
212 } elseif ( defined( 'WP_USE_EXT_MYSQL' ) && !WP_USE_EXT_MYSQL ) {
213 $attempt_fallback = false;
214 } elseif ( !function_exists( 'mysql_connect' ) ) {
215 $attempt_fallback = false;
216 }
217 if ( $attempt_fallback ) {
218 $this->use_mysqli = false;
219 return $this->db_connect( $allow_bail );
220 }
221 }
222 if ( !$this->dbh && $allow_bail ) {
223 wp_load_translations_early();
224 // Load custom DB error template, if present.
225 if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
226 require_once WP_CONTENT_DIR . '/db-error.php';
227 die;
228 }
229 $message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n";
230 $message .= '<p>' . sprintf(
231 /* translators: 1: wp-config.php, 2: Database host. */
232 __( 'This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.' ),
233 '<code>wp-config.php</code>',
234 '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
235 ) . "</p>\n";
236 $message .= "<ul>\n";
237 $message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n";
238 $message .= '<li>' . __( 'Are you sure you have typed the correct hostname?' ) . "</li>\n";
239 $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n";
240 $message .= "</ul>\n";
241 $message .= '<p>' . sprintf(
242 /* translators: %s: Support forums URL. */
243 __( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
244 __( 'https://wordpress.org/support/forums/' )
245 ) . "</p>\n";
246 $this->bail( $message, 'db_connect_fail' );
247 return false;
248 } elseif ( $this->dbh ) {
249 if ( !$this->has_connected ) {
250 $this->init_charset();
251 }
252 $this->has_connected = true;
253 $this->set_charset( $this->dbh );
254 $this->ready = true;
255 $this->set_sql_mode();
256 $this->select( $this->dbname, $this->dbh );
257 return true;
258 }
259 return false;
260 }
261
262 /**
263 * Get connection error no
264 *
265 * @return mixed
266 */
267 public function get_connect_errno() {
268 return $this->dbh->connect_errno;
269 }
270
271 /**
272 * Get connection error message
273 *
274 * @return mixed
275 */
276 public function get_connect_error() {
277 return $this->dbh->connect_error;
278 }
279
280 public function wpda_open_cursor( $query ) {
281 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
282 $this->timer_start();
283 }
284 if ( !empty( $this->dbh ) && $this->use_mysqli ) {
285 $this->result = mysqli_query( $this->dbh, $query );
286 }
287 $this->num_queries++;
288 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
289 $this->log_query(
290 $query,
291 $this->timer_stop(),
292 $this->get_caller(),
293 $this->time_start,
294 array()
295 );
296 }
297 }
298
299 public function wpda_fetch_from_cursor() {
300 if ( $this->use_mysqli && 'mysqli_result' === get_class( $this->result ) ) {
301 return mysqli_fetch_object( $this->result );
302 }
303 return null;
304 }
305
306 }
307