PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.73
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.73
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 5.5.43 All 159 releases
wp-data-access / WPDataAccess / Utilities / WPDA_Remote_Database.php

WPDA_Remote_Database.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.73, at WPDataAccess/Utilities/WPDA_Remote_Database.php

1,288 lines 47.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\Utilities;
4
5 use WPDataAccess\API\WPDA_Tree;
6 use WPDataAccess\Connection\WPDADB;
7 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Access;
8 use WPDataAccess\WPDA;
9 class WPDA_Remote_Database {
10 private $page = null;
11
12 private $user_can_create_db = false;
13
14 private $rdb = array();
15
16 public function __construct() {
17 wp_enqueue_style( 'wp-jquery-ui-core' );
18 wp_enqueue_style( 'wp-jquery-ui-dialog' );
19 wp_enqueue_style( 'wp-jquery-ui-sortable' );
20 wp_enqueue_style( 'wp-jquery-ui-tabs' );
21 wp_enqueue_script( 'jquery-ui-core' );
22 wp_enqueue_script( 'jquery-ui-dialog' );
23 wp_enqueue_script( 'jquery-ui-tabs' );
24 wp_enqueue_script( 'jquery-ui-sortable' );
25 wp_enqueue_script( 'jquery-ui-tooltip' );
26 wp_enqueue_script( 'jquery-ui-autocomplete' );
27 if ( isset( $_REQUEST['page'] ) ) {
28 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) );
29 // input var okay.
30 }
31 $this->user_can_create_db = WPDA_Dictionary_Access::can_create_db();
32 if ( WPDA::current_user_is_admin() ) {
33 if ( isset( $_REQUEST['action'] ) ) {
34 // Process create, drop and alter database actions.
35 if ( 'create_db' === $_REQUEST['action'] ) {
36 $this->create_db();
37 } elseif ( 'drop_db' === $_REQUEST['action'] ) {
38 $this->drop_db();
39 } elseif ( 'edit_db' === $_REQUEST['action'] ) {
40 $this->edit_db();
41 } elseif ( 'toggle_db' === $_REQUEST['action'] ) {
42 $this->toggle_db();
43 }
44 }
45 }
46 }
47
48 private function create_db() {
49 if ( !$this->check_wpnonce( 'wpda-create-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncecreatedb' ) ) {
50 return;
51 }
52 if ( isset( $_REQUEST['database_location'] ) && 'local' === $_REQUEST['database_location'] ) {
53 // Add local database
54 if ( !isset( $_REQUEST['local_database'] ) ) {
55 $msg = new WPDA_Message_Box(array(
56 'message_text' => sprintf( __( 'Cannot create database [missing argument]', 'wp-data-access' ) ),
57 'message_type' => 'error',
58 'message_is_dismissible' => false,
59 ));
60 $msg->box();
61 return;
62 }
63 $database = str_replace( '`', '', sanitize_text_field( wp_unslash( $_REQUEST['local_database'] ) ) );
64 // input var okay.
65 global $wpdb;
66 if ( false === $wpdb->query( $wpdb->prepare(
67 'create database `%1s`',
68 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
69 array(WPDA::remove_backticks( $database ))
70 ) ) ) {
71 // db call ok; no-cache ok.
72 $msg = new WPDA_Message_Box(array(
73 'message_text' => sprintf( __( 'Error creating database `%s`', 'wp-data-access' ), $database ),
74 'message_type' => 'error',
75 'message_is_dismissible' => false,
76 ));
77 $msg->box();
78 } else {
79 $msg = new WPDA_Message_Box(array(
80 'message_text' => sprintf( __( 'Database `%s` created', 'wp-data-access' ), $database ),
81 ));
82 $msg->box();
83 $this->switch_schema_name = $database;
84 }
85 } else {
86 // Add remote database
87 $database = ( isset( $_REQUEST['remote_database'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_database'] ) ) : '' );
88 // input var okay.
89 if ( false !== WPDADB::get_remote_database( $database ) ) {
90 $msg = new WPDA_Message_Box(array(
91 'message_text' => sprintf( __( 'Remote database connection already exists', 'wp-data-access' ) ),
92 'message_type' => 'error',
93 'message_is_dismissible' => false,
94 ));
95 $msg->box();
96 return;
97 }
98 $host = ( isset( $_REQUEST['remote_host'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_host'] ) ) : '' );
99 // input var okay.
100 $user = ( isset( $_REQUEST['remote_user'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_user'] ) ) : '' );
101 // input var okay.
102 $passwd = ( isset( $_REQUEST['remote_passwd'] ) ? wp_unslash( $_REQUEST['remote_passwd'] ) : '' );
103 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
104 $port = ( isset( $_REQUEST['remote_port'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_port'] ) ) : '' );
105 // input var okay.
106 $schema = ( isset( $_REQUEST['remote_schema'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_schema'] ) ) : '' );
107 // input var okay.
108 $ssl = ( isset( $_REQUEST['remote_ssl'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_ssl'] ) ) : 'off' );
109 // input var okay.
110 $ssl_key = ( isset( $_REQUEST['remote_client_key'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_client_key'] ) ) : '' );
111 // input var okay.
112 $ssl_cert = ( isset( $_REQUEST['remote_client_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_client_certificate'] ) ) : '' );
113 // input var okay.
114 $ssl_ca = ( isset( $_REQUEST['remote_ca_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_ca_certificate'] ) ) : '' );
115 // input var okay.
116 $ssl_path = ( isset( $_REQUEST['remote_certificate_path'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_certificate_path'] ) ) : '' );
117 // input var okay.
118 $ssl_cipher = ( isset( $_REQUEST['remote_specified_cipher'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_specified_cipher'] ) ) : '' );
119 // input var okay.
120 if ( '' === $database || '' === $host || '' === $user || '' === $schema ) {
121 $msg = new WPDA_Message_Box(array(
122 'message_text' => sprintf( __( 'Cannot add remote database connection [missing argument]', 'wp-data-access' ) ),
123 'message_type' => 'error',
124 'message_is_dismissible' => false,
125 ));
126 $msg->box();
127 return;
128 }
129 if ( 'rdb:' === $database ) {
130 $msg = new WPDA_Message_Box(array(
131 'message_text' => sprintf( __( 'Invalid database name [enter a valid database name, for example rdb:remotedb]', 'wp-data-access' ) ),
132 'message_type' => 'error',
133 'message_is_dismissible' => false,
134 ));
135 $msg->box();
136 return;
137 }
138 if ( !WPDADB::add_remote_database(
139 $database,
140 $host,
141 $user,
142 $passwd,
143 $port,
144 $schema,
145 $ssl,
146 $ssl_key,
147 $ssl_cert,
148 $ssl_ca,
149 $ssl_path,
150 $ssl_cipher
151 ) ) {
152 $msg = new WPDA_Message_Box(array(
153 'message_text' => sprintf( __( 'Cannot add remote database connection', 'wp-data-access' ) ),
154 'message_type' => 'error',
155 'message_is_dismissible' => false,
156 ));
157 $msg->box();
158 } else {
159 $msg = new WPDA_Message_Box(array(
160 'message_text' => sprintf( __( 'Remote database connection `%s` added', 'wp-data-access' ), $database ),
161 ));
162 $msg->box();
163 $this->switch_schema_name = $database;
164 }
165 }
166 }
167
168 private function toggle_db() {
169 if ( !$this->check_wpnonce( 'wpda-toggle-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncetoggledb' ) ) {
170 return;
171 }
172 if ( !isset( $_REQUEST['database'], $_REQUEST['disabled'] ) ) {
173 $msg = new WPDA_Message_Box(array(
174 'message_text' => sprintf( __( 'Cannot drop database [missing argument]', 'wp-data-access' ) ),
175 'message_type' => 'error',
176 'message_is_dismissible' => false,
177 ));
178 $msg->box();
179 return;
180 }
181 global $wpdb;
182 $database = str_replace( '`', '', sanitize_text_field( wp_unslash( $_REQUEST['database'] ) ) );
183 // input var okay.
184 $disabled = $_REQUEST['disabled'] === 'true';
185 // input var okay.
186 if ( 'rdb:' === substr( $database, 0, 4 ) ) {
187 // Toogle remote database
188 if ( false === WPDADB::get_remote_database( $database, true ) ) {
189 $msg = new WPDA_Message_Box(array(
190 'message_text' => sprintf( __( 'Cannot disable remote database connection `%s` [remote database connection not found]', 'wp-data-access' ), $database ),
191 'message_type' => 'error',
192 'message_is_dismissible' => false,
193 ));
194 $msg->box();
195 } else {
196 if ( false === WPDADB::dis_remote_database( $database, $disabled ) ) {
197 $msg = new WPDA_Message_Box(array(
198 'message_text' => sprintf( __( 'Cannot disable remote database connection `%s`', 'wp-data-access' ), $database ),
199 'message_type' => 'error',
200 'message_is_dismissible' => false,
201 ));
202 $msg->box();
203 } else {
204 $msg = new WPDA_Message_Box(array(
205 'message_text' => sprintf( __( 'Remote database connection `%s` disabled', 'wp-data-access' ), $database ),
206 ));
207 $msg->box();
208 $this->switch_schema_name = $wpdb->dbname;
209 }
210 }
211 }
212 }
213
214 private function drop_db() {
215 if ( !$this->check_wpnonce( 'wpda-drop-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncedropdb' ) ) {
216 return;
217 }
218 if ( !isset( $_REQUEST['database'] ) ) {
219 $msg = new WPDA_Message_Box(array(
220 'message_text' => sprintf( __( 'Cannot drop database [missing argument]', 'wp-data-access' ) ),
221 'message_type' => 'error',
222 'message_is_dismissible' => false,
223 ));
224 $msg->box();
225 return;
226 }
227 global $wpdb;
228 $database = str_replace( '`', '', sanitize_text_field( wp_unslash( $_REQUEST['database'] ) ) );
229 // input var okay.
230 if ( 'rdb:' === substr( $database, 0, 4 ) ) {
231 // Delete remote database
232 if ( false === WPDADB::get_remote_database( $database ) ) {
233 $msg = new WPDA_Message_Box(array(
234 'message_text' => sprintf( __( 'Cannot delete remote database connection `%s` [remote database connection not found]', 'wp-data-access' ), $database ),
235 'message_type' => 'error',
236 'message_is_dismissible' => false,
237 ));
238 $msg->box();
239 } else {
240 if ( false === WPDADB::del_remote_database( $database ) ) {
241 $msg = new WPDA_Message_Box(array(
242 'message_text' => sprintf( __( 'Cannot delete remote database connection `%s`', 'wp-data-access' ), $database ),
243 'message_type' => 'error',
244 'message_is_dismissible' => false,
245 ));
246 $msg->box();
247 } else {
248 $msg = new WPDA_Message_Box(array(
249 'message_text' => sprintf( __( 'Remote database connection `%s` deleted', 'wp-data-access' ), $database ),
250 ));
251 $msg->box();
252 $this->switch_schema_name = $wpdb->dbname;
253 }
254 }
255 } else {
256 // Drop local database
257 if ( $wpdb->dbname === $database ) {
258 $msg = new WPDA_Message_Box(array(
259 'message_text' => __( 'Cannot drop WordPress database', 'wp-data-access' ),
260 'message_type' => 'error',
261 'message_is_dismissible' => false,
262 ));
263 $msg->box();
264 return;
265 }
266 if ( 'sys' === $database || 'mysql' === $database || 'information_schema' === $database || 'performance_schema' === $database || '' === $database ) {
267 $msg = new WPDA_Message_Box(array(
268 'message_text' => __( 'Cannot drop MySQL database', 'wp-data-access' ),
269 'message_type' => 'error',
270 'message_is_dismissible' => false,
271 ));
272 $msg->box();
273 return;
274 }
275 if ( false === $wpdb->query( $wpdb->prepare(
276 'drop database `%1s`',
277 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
278 array(WPDA::remove_backticks( $database ))
279 ) ) ) {
280 // db call ok; no-cache ok.
281 $msg = new WPDA_Message_Box(array(
282 'message_text' => sprintf( __( 'Error dropping database `%s`', 'wp-data-access' ), $database ),
283 'message_type' => 'error',
284 'message_is_dismissible' => false,
285 ));
286 $msg->box();
287 } else {
288 $msg = new WPDA_Message_Box(array(
289 'message_text' => sprintf( __( 'Database `%s` dropped', 'wp-data-access' ), $database ),
290 ));
291 $msg->box();
292 $this->switch_schema_name = $wpdb->dbname;
293 }
294 }
295 }
296
297 private function edit_db() {
298 if ( !$this->check_wpnonce( 'wpda-edit-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnonceeditdb' ) ) {
299 return;
300 }
301 if ( !isset( $_REQUEST['edit_remote_database'] ) ) {
302 $msg = new WPDA_Message_Box(array(
303 'message_text' => sprintf( __( 'Cannot update remote database connection [missing argument]', 'wp-data-access' ) ),
304 'message_type' => 'error',
305 'message_is_dismissible' => false,
306 ));
307 $msg->box();
308 return;
309 }
310 $database = sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_database'] ) );
311 // input var okay.
312 $database_old = sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_database_old'] ) );
313 // input var okay.
314 if ( $database !== $database_old ) {
315 // Update database connection name
316 if ( false === WPDADB::get_remote_database( $database_old ) ) {
317 $msg = new WPDA_Message_Box(array(
318 'message_text' => sprintf( __( 'Cannot update remote database connection [remote database connection not found]', 'wp-data-access' ) ),
319 'message_type' => 'error',
320 'message_is_dismissible' => false,
321 ));
322 $msg->box();
323 return;
324 }
325 } else {
326 // Update database connection information
327 if ( false === WPDADB::get_remote_database( $database ) ) {
328 $msg = new WPDA_Message_Box(array(
329 'message_text' => sprintf( __( 'Cannot update remote database connection [remote database connection not found]', 'wp-data-access' ) ),
330 'message_type' => 'error',
331 'message_is_dismissible' => false,
332 ));
333 $msg->box();
334 return;
335 }
336 }
337 $host = ( isset( $_REQUEST['edit_remote_host'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_host'] ) ) : '' );
338 // input var okay.
339 $user = ( isset( $_REQUEST['edit_remote_user'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_user'] ) ) : '' );
340 // input var okay.
341 $passwd = ( isset( $_REQUEST['edit_remote_passwd'] ) ? wp_unslash( $_REQUEST['edit_remote_passwd'] ) : '' );
342 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
343 $port = ( isset( $_REQUEST['edit_remote_port'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_port'] ) ) : '' );
344 // input var okay.
345 $schema = ( isset( $_REQUEST['edit_remote_schema'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_schema'] ) ) : '' );
346 // input var okay.
347 $ssl = ( isset( $_REQUEST['edit_remote_ssl'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_ssl'] ) ) : 'off' );
348 // input var okay.
349 $ssl_key = ( isset( $_REQUEST['edit_remote_client_key'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_client_key'] ) ) : '' );
350 // input var okay.
351 $ssl_cert = ( isset( $_REQUEST['edit_remote_client_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_client_certificate'] ) ) : '' );
352 // input var okay.
353 $ssl_ca = ( isset( $_REQUEST['edit_remote_ca_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_ca_certificate'] ) ) : '' );
354 // input var okay.
355 $ssl_path = ( isset( $_REQUEST['edit_remote_certificate_path'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_certificate_path'] ) ) : '' );
356 // input var okay.
357 $ssl_cipher = ( isset( $_REQUEST['edit_remote_specified_cipher'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_specified_cipher'] ) ) : '' );
358 // input var okay.
359 if ( '' === $database || '' === $host || '' === $user || '' === $schema ) {
360 $msg = new WPDA_Message_Box(array(
361 'message_text' => sprintf( __( 'Cannot edit remote database connection [missing arguments]', 'wp-data-access' ) ),
362 'message_type' => 'error',
363 'message_is_dismissible' => false,
364 ));
365 $msg->box();
366 return;
367 }
368 if ( !WPDADB::upd_remote_database(
369 $database,
370 $host,
371 $user,
372 $passwd,
373 $port,
374 $schema,
375 false,
376 $database_old,
377 $ssl,
378 $ssl_key,
379 $ssl_cert,
380 $ssl_ca,
381 $ssl_path,
382 $ssl_cipher
383 ) ) {
384 $msg = new WPDA_Message_Box(array(
385 'message_text' => sprintf( __( 'Cannot update remote database connection `%s`', 'wp-data-access' ), $database ),
386 'message_type' => 'error',
387 'message_is_dismissible' => false,
388 ));
389 $msg->box();
390 } else {
391 $msg = new WPDA_Message_Box(array(
392 'message_text' => sprintf( __( 'Remote database connection `%s` updated', 'wp-data-access' ), $database ),
393 ));
394 $msg->box();
395 if ( $database !== $database_old ) {
396 $this->switch_schema_name = $database;
397 }
398 }
399 }
400
401 private function check_wpnonce( $wp_nonce_action, $wp_nonce_arg ) {
402 $wp_nonce = ( isset( $_REQUEST[$wp_nonce_arg] ) ? sanitize_text_field( wp_unslash( $_REQUEST[$wp_nonce_arg] ) ) : '' );
403 // input var okay.
404 if ( !wp_verify_nonce( $wp_nonce, $wp_nonce_action ) ) {
405 $msg = new WPDA_Message_Box(array(
406 'message_text' => __( 'Not authorized', 'wp-data-access' ),
407 'message_type' => 'error',
408 'message_is_dismissible' => false,
409 ));
410 $msg->box();
411 return false;
412 }
413 return true;
414 }
415
416 public function show() {
417 if ( null !== $this->page ) {
418 $this->add_containers();
419 }
420 }
421
422 private function add_containers() {
423 ?>
424
425 <div id="wpda_manage_databases">
426 <div id="wpda_db_tabs">
427 <ul>
428 <li><a href="#wpda_tab-1">Manage Databases</a></li>
429 <li><a href="#wpda_tab-2">Create Remote Database Connection</a></li>
430 <li><a href="#wpda_tab-3">Create Local Database</a></li>
431 </ul>
432
433 <div class="container" id="wpda_tab-1">
434 <?php
435 $this->manage_databases();
436 ?>
437 </div>
438
439 <div class="container" id="wpda_tab-2">
440 <?php
441 $this->create_remote_database();
442 ?>
443 </div>
444
445 <div class="container" id="wpda_tab-3">
446 <?php
447 $this->create_local_database();
448 ?>
449 </div>
450 </div>
451
452 <div class="wpda_manage_databases_close">
453 <a href="javascript:void(0)"
454 onclick="jQuery('#wpda_manage_databases').hide()"><i
455 class="fas fa-xmark wpda_icon_on_button"></i>
456 </a>
457 </div>
458 </div>
459
460 <?php
461 $this->drop_database();
462 ?>
463 <?php
464 $this->toggle_database();
465 ?>
466
467 <?php
468 $this->js();
469 $this->css();
470 }
471
472 private function manage_databases() {
473 $tree = new WPDA_Tree();
474 $dbs = $tree->get_dbs( true );
475 $this->rdb = array();
476 $mdbs = array(
477 '' => '',
478 );
479 if ( isset( $dbs->data['data'] ) ) {
480 $dbs_list = $dbs->data['data'];
481 foreach ( $dbs_list as $db ) {
482 if ( 'local' === $db['dbs_type'] || 'remote' === $db['dbs_type'] ) {
483 // Admins can manage local and remote database only.
484 $mdbs[$db['dbs']] = $db['dbs_type'];
485 if ( 'remote' === $db['dbs_type'] ) {
486 // Store remote database info.
487 $this->rdb[$db['dbs']] = WPDADB::get_remote_database( $db['dbs'], true );
488 }
489 }
490 }
491 }
492 ?>
493
494 <h3 class="wpda_db_title">
495 <?php
496 echo __( 'Manage Databases', 'wp-data-access' );
497 ?>
498 </h3>
499
500 <?php
501 if ( 1 === count( $mdbs ) ) {
502 $this->no_database();
503 } else {
504 $this->list_databases( $mdbs );
505 }
506 }
507
508 private function activate_pds() {
509 ?>
510 <div class="restyle_link">
511 <strong>NOTE</strong>
512 Please activate your Premium Data Services access <a href="options-general.php?page=wpdataaccess&tab=pds">here</a> to remotely connect to foreign DBMSs and remote files.
513 <a href="https://docs.remote.wpdataaccess.com/pds/remote-wizard.html" class="restyle_link" target="_blank">(read more...)</a>
514 </div>
515 <?php
516 }
517
518 private function no_database() {
519 echo __( 'No manageable local databases or remote database connections found', 'wp-data-access' );
520 }
521
522 private function list_databases( $dbs ) {
523 ?>
524
525 <div class="wpda_manage_dbs">
526
527 <label for="edit_remote_database" class="database_item_label">Select database:</label>
528 <select id="manage_db_selection">
529 <?php
530 foreach ( $dbs as $db => $db_type ) {
531 echo "<option value='{$db}' data-type='{$db_type}'>{$db}</option>";
532 }
533 ?>
534 </select>
535
536 <a id="manage_db_create_wp_user_access"
537 class="dashicons dashicons-admin-plugins wpda_tooltip"
538 style="display: none"
539 href="javascript:void(0)"
540 style="vertical-align:middle;"
541 title="<?php
542 echo __( "Create function wpda_get_wp_user_id() to access the WordPress user ID from database views", 'wp-data-access' );
543 ?>"></a>
544
545 </div>
546
547 <?php
548 $this->edit_local_database();
549 $this->edit_remote_database();
550 }
551
552 private function edit_local_database() {
553 ?>
554
555 <div id="edit_local_database_form">
556 <label for="edit_local_database" class="database_item_label">Database name:</label>
557 <input type="text" name="edit_local_database" id="edit_local_database" readonly>
558 <a href="javascript:void(0)"
559 id="edit_local_database_action"
560 title="Drop Database"
561 class="button button-secondary wpda_tooltip"><i
562 class="fas fa-trash wpda_icon_on_button"></i></a>
563 </div>
564
565 <?php
566 }
567
568 private function edit_remote_database() {
569 ?>
570
571 <form method="post"
572 action="?page=<?php
573 echo esc_attr( $this->page );
574 ?>"
575 onsubmit="return editdb_validate_form();"
576 id="edit_remote_database_form"
577 >
578
579 <div>
580 <label for="edit_remote_database" class="database_item_label">Database name:</label>
581 <input type="text"
582 name="edit_remote_database"
583 id="edit_remote_database">
584 <input type="hidden"
585 name="edit_remote_database_old"
586 id="edit_remote_database_old">
587 <a href="javascript:void(0)"
588 id="disable_remote_database"
589 style="display: none"
590 title="Disable Remote Connection"
591 class="button button-secondary wpda_tooltip"><i
592 class="fas fa-ban wpda_icon_on_button"></i></a>
593 <a href="javascript:void(0)"
594 id="enable_remote_database"
595 style="display: none"
596 title="Enabled Remote Connection"
597 class="button button-secondary wpda_tooltip"><i
598 class="fas fa-check wpda_icon_on_button"></i></a>
599 <a href="javascript:void(0)"
600 id="remote_local_database_action"
601 title="Drop Database"
602 class="button button-secondary wpda_tooltip"><i
603 class="fas fa-trash wpda_icon_on_button"></i></a>
604 </div>
605
606 <div
607 id="remote_connection_is_disabled"
608 style="display: none"
609 >
610 <label class="database_item_label"></label>
611 <span style="color: red; line-height: 30px">
612 This remote database connection is currently disabled.
613 </span>
614 </div>
615
616 <div>
617 <label for="edit_remote_host" class="database_item_label">MySQL host:</label>
618 <input type="text"
619 name="edit_remote_host"
620 id="edit_remote_host">
621 </div>
622
623 <div>
624 <label for="edit_remote_user" class="database_item_label">MySQL username:</label>
625 <input type="text"
626 name="edit_remote_user"
627 id="edit_remote_user">
628 </div>
629
630 <div>
631 <label for="edit_remote_passwd" class="database_item_label">MySQL password:</label>
632 <input type="password"
633 name="edit_remote_passwd"
634 id="edit_remote_passwd"
635 autocomplete="new-password">
636 <i class="fas fa-eye"
637 onclick="wpda_toggle_password('edit_remote_passwd', event)"
638 style="cursor:pointer"></i>
639 </div>
640
641 <div>
642 <label for="edit_remote_port" class="database_item_label">MySQL port:</label>
643 <input type="text"
644 name="edit_remote_port"
645 id="edit_remote_port">
646 </div>
647
648 <div>
649 <label for="edit_remote_schema" class="database_item_label">MySQL schema:</label>
650 <input type="text"
651 name="edit_remote_schema"
652 id="edit_remote_schema">
653 </div>
654
655 <div class="wpda_db_ssl">
656 <label for="edit_remote_ssl" class="database_item_label">SSL:</label>
657 <input type="checkbox"
658 name="edit_remote_ssl"
659 id="edit_remote_ssl"
660 onclick="jQuery('#edit_remote_database_block_ssl').toggle()">
661 </div>
662
663 <div id="edit_remote_database_block_ssl">
664 <div>
665 <label for="edit_remote_client_key" class="database_item_label">Client key:</label>
666 <input type="text"
667 name="edit_remote_client_key"
668 id="edit_remote_client_key">
669 </div>
670
671 <div>
672 <label for="edit_remote_client_certificate" class="database_item_label">Client
673 certificate:</label>
674 <input type="text"
675 name="edit_remote_client_certificate"
676 id="edit_remote_client_certificate">
677 </div>
678
679 <div>
680 <label for="edit_remote_ca_certificate" class="database_item_label">CA certificate:</label>
681 <input type="text"
682 name="edit_remote_ca_certificate"
683 id="edit_remote_ca_certificate">
684 </div>
685
686 <div>
687 <label for="edit_remote_certificate_path" class="database_item_label">Certificate path:</label>
688 <input type="text"
689 name="edit_remote_certificate_path"
690 id="edit_remote_certificate_path">
691 </div>
692
693 <div>
694 <label for="edit_remote_specified_cipher" class="database_item_label">Specified Cipher:</label>
695 <input type="text"
696 name="edit_remote_specified_cipher"
697 id="edit_remote_specified_cipher">
698 </div>
699 </div>
700
701 <div>
702 <label class="database_item_label"></label>
703 <input type="button"
704 value="Test"
705 onclick="test_remote_connection('edit_'); return false;"
706 id="edit_remote_test_button"
707 class="button">
708 <input type="button"
709 value="Clear"
710 onclick="test_remote_clear('edit_'); return false;"
711 id="edit_remote_clear_button"
712 class="button">
713 </div>
714
715 <div id="edit_remote_database_block_test">
716 <div id="edit_remote_database_block_test_content"
717 class="remote_database_block_test_content"></div>
718 </div>
719
720 <input type="hidden" name="action" value="edit_db">
721 <?php
722 wp_nonce_field( 'wpda-edit-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnonceeditdb', false );
723 ?>
724
725 <?php
726 $this->buttons();
727 ?>
728
729 </form>
730
731 <?php
732 }
733
734 private function create_local_database() {
735 ?>
736
737 <h3 class="wpda_db_title">
738 <?php
739 echo __( 'Create local database', 'wp-data-access' );
740 ?>
741 </h3>
742
743 <?php
744 if ( !$this->user_can_create_db ) {
745 echo __( 'You are not authorized to create local databases', 'wp-data-access' );
746 } else {
747 ?>
748
749 <form method="post"
750 action="?page=<?php
751 echo esc_attr( $this->page );
752 ?>"
753 onsubmit="return createdb_validate_form_local();"
754 >
755
756 <div>
757 <label for="local_database" class="database_item_label">Database name:</label>
758 <input type="text" name="local_database" id="local_database">
759 </div>
760
761 <input type="hidden" name="action" value="create_db">
762 <input type="hidden" name="database_location" value="local">
763 <?php
764 wp_nonce_field( 'wpda-create-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncecreatedb', false );
765 ?>
766
767 <?php
768 $this->buttons();
769 ?>
770
771 </form>
772
773 <?php
774 }
775 }
776
777 private function create_remote_database() {
778 ?>
779
780 <form method="post" action="?page=<?php
781 echo esc_attr( $this->page );
782 ?>"
783 onsubmit="return createdb_validate_form_remote();">
784
785 <h3 class="wpda_db_title">
786 <?php
787 echo __( 'Create remote database connection', 'wp-data-access' );
788 ?>
789 </h3>
790
791 <div>
792 <label for="remote_database"
793 class="database_item_label">Database name:</label>
794 <input type="text" name="remote_database" id="remote_database" value="rdb:">
795 </div>
796
797 <div>
798 <label for="remote_host" class="database_item_label">MySQL host:</label>
799 <input type="text" name="remote_host" id="remote_host">
800 </div>
801
802 <div>
803 <label for="remote_user" class="database_item_label">MySQL username:</label>
804 <input type="text" name="remote_user" id="remote_user">
805 </div>
806
807 <div>
808 <label for="remote_passwd" class="database_item_label">MySQL password:</label>
809 <input type="password" name="remote_passwd" id="remote_passwd" autocomplete="new-password">
810 <i class="fas fa-eye" onclick="wpda_toggle_password('remote_passwd', event)"
811 style="cursor:pointer"></i>
812 </div>
813
814 <div>
815 <label for="remote_port" class="database_item_label">MySQL port:</label>
816 <input type="text" name="remote_port" id="remote_port" value="3306">
817 </div>
818
819 <div>
820 <label for="remote_schema" class="database_item_label">MySQL schema:</label>
821 <input type="text" name="remote_schema" id="remote_schema">
822 </div>
823
824 <div class="wpda_db_ssl">
825 <label for="remote_ssl"
826 class="database_item_label">SSL:</label>
827 <input type="checkbox" name="remote_ssl" id="remote_ssl" unchecked
828 onclick="jQuery('#remote_database_block_ssl').toggle()">
829 </div>
830
831 <div id="remote_database_block_ssl">
832 <div>
833 <label for="remote_client_key" class="database_item_label">Client key:</label>
834 <input type="text" name="remote_client_key" id="remote_client_key">
835 </div>
836
837 <div>
838 <label for="remote_client_certificate" class="database_item_label">Client certificate:</label>
839 <input type="text" name="remote_client_certificate" id="remote_client_certificate">
840 </div>
841
842 <div>
843 <label for="remote_ca_certificate" class="database_item_label">CA certificate:</label>
844 <input type="text" name="remote_ca_certificate" id="remote_ca_certificate">
845 </div>
846
847 <div>
848 <label for="remote_certificate_path" class="database_item_label">Certificate path:</label>
849 <input type="text" name="remote_certificate_path" id="remote_certificate_path">
850 </div>
851
852 <div>
853 <label for="remote_specified_cipher" class="database_item_label">Specified Cipher:</label>
854 <input type="text" name="remote_specified_cipher" id="remote_specified_cipher">
855 </div>
856 </div>
857
858 <div>
859 <label class="database_item_label"></label>
860 <input type="button" value="Test" onclick="test_remote_connection(); return false;"
861 id="remote_test_button" class="button">
862 <input type="button" value="Clear" onclick="test_remote_clear(); return false;"
863 id="remote_clear_button" class="button" style="display:none;">
864 </div>
865
866 <div id="remote_database_block_test">
867 <div id="remote_database_block_test_content"
868 class="remote_database_block_test_content"></div>
869 </div>
870
871 <input type="hidden" name="action" value="create_db">
872 <input type="hidden" name="database_location" value="remote">
873 <?php
874 wp_nonce_field( 'wpda-create-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncecreatedb', false );
875 ?>
876
877 <?php
878 $this->buttons();
879 ?>
880
881 </form>
882
883 <?php
884 }
885
886 private function buttons() {
887 ?>
888
889 <div class="wpda_db_buttons">
890 <a href="javascript:void(0)"
891 onclick="jQuery(this).closest('form').submit()"
892 class="button button-primary"><i
893 class="fas fa-cloud-upload wpda_icon_on_button"></i> <?php
894 echo __( 'Save', 'wp-data-access' );
895 ?>
896 </a>
897 <a href="javascript:void(0)"
898 onclick="jQuery('#wpda_manage_databases').hide()"
899 class="button button-secondary"><i
900 class="fas fa-times-circle wpda_icon_on_button"></i> <?php
901 echo __( 'Cancel', 'wp-data-access' );
902 ?>
903 </a>
904 </div>
905
906 <?php
907 }
908
909 private function drop_database() {
910 ?>
911
912 <form id="wpda_form_drop_db"
913 method="post" action="?page=<?php
914 echo esc_attr( $this->page );
915 ?>"
916 >
917 <input type="hidden" name="database" id="drop_database">
918 <input type="hidden" name="action" value="drop_db">
919 <?php
920 wp_nonce_field( 'wpda-drop-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncedropdb', false );
921 ?>
922 </form>
923
924 <?php
925 }
926
927 private function toggle_database() {
928 ?>
929
930 <form id="wpda_form_toggle_db"
931 method="post" action="?page=<?php
932 echo esc_attr( $this->page );
933 ?>"
934 >
935 <input type="hidden" name="database" id="toggle_database">
936 <input type="hidden" name="action" value="toggle_db">
937 <input type="hidden" name="disabled" id="toggle_database_value">
938 <?php
939 wp_nonce_field( 'wpda-toggle-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncetoggledb', false );
940 ?>
941 </form>
942
943 <?php
944 }
945
946 private function js() {
947 ?>
948
949 <script>
950 function editdb_validate_form() {
951 if (jQuery('#edit_remote_database').val() === '' || jQuery('#edit_remote_database').val() === 'rdb:') {
952 alert('Database name must be entered');
953 return false;
954 }
955
956 if (jQuery('#edit_remote_host').val() === '') {
957 alert('MySQL host must be entered');
958 return false;
959 }
960
961 if (jQuery('#edit_remote_user').val() === '') {
962 alert('MySQL username must be entered');
963 return false;
964 }
965
966 if (jQuery('#edit_remote_schema').val() === '') {
967 alert('MySQL schema must be entered');
968 return false;
969 }
970
971 return true;
972 }
973
974 function createdb_validate_form_remote() {
975 if (jQuery('#remote_database').val() === '' || jQuery('#remote_database').val() === 'rdb:') {
976 alert('Database name must be entered');
977 return false;
978 }
979
980 if (jQuery('#remote_host').val() === '') {
981 alert('MySQL host must be entered');
982 return false;
983 }
984
985 if (jQuery('#remote_user').val() === '') {
986 alert('MySQL username must be entered');
987 return false;
988 }
989
990 if (jQuery('#remote_schema').val() === '') {
991 alert('MySQL schema must be entered');
992 return false;
993 }
994
995 return true;
996 }
997
998 function createdb_validate_form_local() {
999 if (jQuery('#local_database').val() === '') {
1000 alert('Database name must be entered');
1001 return false;
1002 }
1003
1004 return true;
1005 }
1006
1007 function test_remote_clear(mode = '') {
1008 jQuery('#' + mode + 'remote_database_block_test_content').html('');
1009 jQuery('#' + mode + 'remote_database_block_test').hide();
1010 jQuery('#' + mode + 'remote_clear_button').hide();
1011 }
1012
1013 function test_remote_connection(mode = '') {
1014 host = jQuery('#' + mode + 'remote_host').val();
1015 user = jQuery('#' + mode + 'remote_user').val();
1016 pass = jQuery('#' + mode + 'remote_passwd').val();
1017 port = jQuery('#' + mode + 'remote_port').val();
1018 dbs = jQuery('#' + mode + 'remote_schema').val();
1019 ssl = jQuery('#' + mode + 'remote_ssl').val();
1020 ssl_key = jQuery('#' + mode + 'remote_client_key').val();
1021 ssl_cert = jQuery('#' + mode + 'remote_client_certificate').val();
1022 ssl_ca = jQuery('#' + mode + 'remote_ca_certificate').val();
1023 ssl_path = jQuery('#' + mode + 'remote_certificate_path').val();
1024 ssl_cipher = jQuery('#' + mode + 'remote_specified_cipher').val();
1025
1026 url = '//' + window.location.host + window.location.pathname +
1027 '?action=wpda_check_remote_database_connection';
1028
1029 jQuery('#' + mode + 'remote_test_button').val('Testing...');
1030
1031 jQuery.ajax({
1032 method: 'POST',
1033 url: url,
1034 data: {
1035 host: host,
1036 user: user,
1037 passwd: pass,
1038 port: port,
1039 schema: dbs,
1040 ssl: ssl,
1041 ssl_key: ssl_key,
1042 ssl_cert: ssl_cert,
1043 ssl_ca: ssl_ca,
1044 ssl_path: ssl_path,
1045 ssl_cipher: ssl_cipher
1046 }
1047 }).done(
1048 function (msg) {
1049 jQuery('#' + mode + 'remote_database_block_test_content').html(msg);
1050 jQuery('#' + mode + 'remote_database_block_test').show();
1051 }
1052 ).fail(
1053 function () {
1054 jQuery('#' + mode + 'remote_database_block_test_content').html('Preparing connection...<br/>Establishing connection...<br/><br/><strong>Remote database connection invalid</strong>');
1055 jQuery('#' + mode + 'remote_database_block_test').show();
1056 }
1057 ).always(
1058 function () {
1059 jQuery('#' + mode + 'remote_test_button').val('Test');
1060 jQuery('#' + mode + 'remote_clear_button').show();
1061 }
1062 );
1063 }
1064
1065 jQuery(function () {
1066 jQuery('#database_location').on('change', function () {
1067 if (jQuery(this).val() === 'remote') {
1068 jQuery('#local_database_block').hide();
1069 jQuery('#remote_database_block').show();
1070 } else {
1071 jQuery('#remote_database_block').hide();
1072 jQuery('#local_database_block').show();
1073 }
1074 });
1075
1076 jQuery('#remote_database, #edit_remote_database').keydown(function () {
1077 var field = this;
1078 setTimeout(function () {
1079 if (field.value.indexOf('rdb:') !== 0) {
1080 jQuery(field).val('rdb:');
1081 }
1082 }, 1);
1083 });
1084
1085 jQuery('#wpda_db_tabs').tabs();
1086
1087 jQuery("#manage_db_selection").on('change', function () {
1088 const db_name = this.value;
1089 const db_type = jQuery("#manage_db_selection").find(':selected').data("type");
1090
1091 if (db_type === 'local') {
1092 jQuery("#edit_remote_database_form").hide();
1093 jQuery("#edit_local_database_form").show();
1094
1095 jQuery("#edit_local_database").val(db_name);
1096 } else {
1097 jQuery("#edit_local_database_form").hide();
1098 jQuery("#edit_remote_database_form").show();
1099
1100 if (wpda_rdb[db_name]) {
1101 // Load remote database info
1102 const rdb = wpda_rdb[db_name];
1103 jQuery("#edit_remote_database").val(db_name)
1104 jQuery("#edit_remote_database_old").val(db_name)
1105
1106 jQuery("#edit_remote_host").val(rdb.host)
1107 jQuery("#edit_remote_user").val(rdb.username)
1108 jQuery("#edit_remote_passwd").val(rdb.password)
1109 jQuery("#edit_remote_port").val(rdb.port)
1110 jQuery("#edit_remote_schema").val(rdb.database)
1111
1112 jQuery("#edit_remote_ssl").prop("checked", rdb.ssl === "on")
1113 jQuery("#edit_remote_client_key").val(rdb.ssl_key)
1114 jQuery("#edit_remote_client_certificate").val(rdb.ssl_cert)
1115 jQuery("#edit_remote_ca_certificate").val(rdb.ssl_ca)
1116 jQuery("#edit_remote_certificate_path").val(rdb.ssl_path)
1117 jQuery("#edit_remote_specified_cipher").val(rdb.ssl_cipher)
1118
1119 if (rdb.disabled === true) {
1120 jQuery("#enable_remote_database").show();
1121 jQuery("#disable_remote_database").hide();
1122 jQuery("#remote_connection_is_disabled").show();
1123 } else {
1124 jQuery("#disable_remote_database").show();
1125 jQuery("#enable_remote_database").hide();
1126 }
1127
1128 if (rdb.ssl === "on") {
1129 jQuery('#edit_remote_database_block_ssl').show();
1130 } else {
1131 jQuery('#edit_remote_database_block_ssl').hide();
1132 }
1133 } else {
1134 if (db_name !== "") {
1135 alert("Database not found");
1136 jQuery("#edit_remote_database_form").hide();
1137 } else {
1138 jQuery("#edit_remote_database_form").hide();
1139 jQuery("#edit_local_database_form").hide();
1140 }
1141 }
1142 }
1143
1144 if (db_name === '') {
1145 jQuery("#manage_db_create_wp_user_access").hide();
1146 } else {
1147 jQuery("#manage_db_create_wp_user_access").show();
1148 }
1149 })
1150
1151 jQuery("#manage_db_create_wp_user_access").on('click', function() {
1152 const selectedDatabase = jQuery("#manage_db_selection").val()
1153 wpda_dbinit_admin( selectedDatabase, '<?php
1154 echo wp_create_nonce( 'wpda_dbinit_admin_' . WPDA::get_current_user_login() );
1155 ?>' )
1156 })
1157
1158 jQuery("#edit_local_database_action").on('click', function () {
1159 if (confirm('Warning! All your tables and views will be deleted. This action cannot be undone! Are you sure you want to delete this database?')) {
1160 jQuery("#drop_database").val(jQuery("#edit_local_database").val())
1161 jQuery("#wpda_form_drop_db").submit();
1162 }
1163 })
1164
1165 jQuery("#remote_local_database_action").on('click', function () {
1166 if (confirm('Warning! All your tables and views will be deleted. This action cannot be undone! Are you sure you want to delete this database?')) {
1167 jQuery("#drop_database").val(jQuery("#edit_remote_database").val())
1168 jQuery("#wpda_form_drop_db").submit();
1169 }
1170 })
1171
1172 jQuery("#disable_remote_database").on('click', function() {
1173 if (confirm('Are you sure you want to disable this database?')) {
1174 jQuery("#toggle_database").val(jQuery("#edit_remote_database").val())
1175 jQuery("#toggle_database_value").val(true);
1176 jQuery("#wpda_form_toggle_db").submit();
1177 }
1178 })
1179
1180 jQuery("#enable_remote_database").on('click', function() {
1181 jQuery("#toggle_database").val(jQuery("#edit_remote_database").val())
1182 jQuery("#toggle_database_value").val(false);
1183 jQuery("#wpda_form_toggle_db").submit();
1184 })
1185 });
1186
1187 var wpda_rdb = <?php
1188 echo json_encode( $this->rdb );
1189 ?>;
1190 </script>
1191
1192 <?php
1193 }
1194
1195 private function css() {
1196 ?>
1197
1198 <style>
1199 #wpda_manage_databases {
1200 position: relative;
1201 display: none;
1202 padding: 24px;
1203 margin-bottom: 5px;
1204 border-radius: 4px;
1205 background-color: #fff;
1206 color: rgba(0, 0, 0, 0.87);
1207 -webkit-transition: box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
1208 transition: box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
1209 box-shadow: 0px 2px 4px -1px rgba(0, 0, 0, 0.2), 0px 4px 5px 0px rgba(0, 0, 0, 0.14), 0px 1px 10px 0px rgba(0, 0, 0, 0.12);
1210 }
1211
1212 #wpda_db_tabs {
1213 border: none;
1214 }
1215
1216 #wpda_db_tabs ul {
1217 background: none;
1218 border-top: none;
1219 border-right: none;
1220 border-left: none;
1221 border-radius: 0;
1222 }
1223
1224 #edit_remote_clear_button,
1225 #edit_remote_database_block_test,
1226 #wpda_form_drop_db,
1227 #remote_database_block_ssl,
1228 #remote_database_block_test,
1229 #edit_remote_database_form,
1230 #edit_local_database_form {
1231 display: none;
1232 }
1233
1234 #edit_remote_database_block_ssl,
1235 #remote_database_block_ssl {
1236 margin-bottom: 10px;
1237 }
1238
1239 #edit_remote_database_block_test,
1240 #remote_database_block_test {
1241 margin-top: 10px;
1242 }
1243
1244 .wpda_manage_dbs,
1245 .wpda_db_title {
1246 margin-bottom: 30px;
1247 }
1248
1249 .database_item_label {
1250 vertical-align: baseline;
1251 }
1252
1253 .wpda_db_ssl {
1254 margin-top: 10px;
1255 margin-bottom: 10px;
1256 }
1257
1258 .wpda_db_buttons {
1259 margin-top: 30px;
1260 }
1261 .wpda_manage_databases_close {
1262 position: absolute;
1263 top: 22px;
1264 right: 22px;
1265 z-index: 9999999;
1266 font-size: 1rem;
1267 }
1268
1269 .wpda_manage_databases_close .fas {
1270 color: rgb(60, 67, 74);
1271 }
1272
1273 .restyle_link a {
1274 color: #2271b1;
1275 text-decoration: none;
1276 font-weight: 700;
1277 }
1278
1279 #manage_db_create_wp_user_access:before {
1280 line-height: 28px;
1281 }
1282 </style>
1283
1284 <?php
1285 }
1286
1287 }
1288