PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.23
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.23
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.23, at WPDataAccess/Utilities/WPDA_Remote_Database.php

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