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 / Utilities / WPDA_Remote_Database.php

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

1,169 lines 41.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 ( 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 }
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">Manage 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-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 <a id="manage_db_create_wp_user_access"
486 class="dashicons dashicons-admin-plugins wpda_tooltip"
487 style="display: none"
488 href="javascript:void(0)"
489 style="vertical-align:middle;"
490 title="<?php
491 echo __( "Create function wpda_get_wp_user_id() to access the WordPress user ID from database views", 'wp-data-access' );
492 ?>">&nbsp;</a>
493
494 </div>
495
496 <?php
497 $this->edit_local_database();
498 $this->edit_remote_database();
499 }
500
501 private function edit_local_database() {
502 ?>
503
504 <div id="edit_local_database_form">
505 <label for="edit_local_database" class="database_item_label">Database name:</label>
506 <input type="text" name="edit_local_database" id="edit_local_database" readonly>
507 <a href="javascript:void(0)"
508 id="edit_local_database_action"
509 class="button button-primary"><i
510 class="fas fa-trash wpda_icon_on_button"></i> Drop database
511 </a>
512 </div>
513
514 <?php
515 }
516
517 private function edit_remote_database() {
518 ?>
519
520 <form method="post"
521 action="?page=<?php
522 echo esc_attr( $this->page );
523 ?>"
524 onsubmit="return editdb_validate_form();"
525 id="edit_remote_database_form"
526 >
527
528 <div>
529 <label for="edit_remote_database" class="database_item_label">Database name:</label>
530 <input type="text"
531 name="edit_remote_database"
532 id="edit_remote_database">
533 <input type="hidden"
534 name="edit_remote_database_old"
535 id="edit_remote_database_old">
536 <a href="javascript:void(0)"
537 id="remote_local_database_action"
538 class="button button-secondary"><i
539 class="fas fa-trash wpda_icon_on_button"></i> Drop database
540 </a>
541 </div>
542
543 <div>
544 <label for="edit_remote_host" class="database_item_label">MySQL host:</label>
545 <input type="text"
546 name="edit_remote_host"
547 id="edit_remote_host">
548 </div>
549
550 <div>
551 <label for="edit_remote_user" class="database_item_label">MySQL username:</label>
552 <input type="text"
553 name="edit_remote_user"
554 id="edit_remote_user">
555 </div>
556
557 <div>
558 <label for="edit_remote_passwd" class="database_item_label">MySQL password:</label>
559 <input type="password"
560 name="edit_remote_passwd"
561 id="edit_remote_passwd"
562 autocomplete="new-password">
563 <i class="fas fa-eye"
564 onclick="wpda_toggle_password('edit_remote_passwd', event)"
565 style="cursor:pointer"></i>
566 </div>
567
568 <div>
569 <label for="edit_remote_port" class="database_item_label">MySQL port:</label>
570 <input type="text"
571 name="edit_remote_port"
572 id="edit_remote_port">
573 </div>
574
575 <div>
576 <label for="edit_remote_schema" class="database_item_label">MySQL schema:</label>
577 <input type="text"
578 name="edit_remote_schema"
579 id="edit_remote_schema">
580 </div>
581
582 <div class="wpda_db_ssl">
583 <label for="edit_remote_ssl" class="database_item_label">SSL:</label>
584 <input type="checkbox"
585 name="edit_remote_ssl"
586 id="edit_remote_ssl"
587 onclick="jQuery('#edit_remote_database_block_ssl').toggle()">
588 </div>
589
590 <div id="edit_remote_database_block_ssl">
591 <div>
592 <label for="edit_remote_client_key" class="database_item_label">Client key:</label>
593 <input type="text"
594 name="edit_remote_client_key"
595 id="edit_remote_client_key">
596 </div>
597
598 <div>
599 <label for="edit_remote_client_certificate" class="database_item_label">Client
600 certificate:</label>
601 <input type="text"
602 name="edit_remote_client_certificate"
603 id="edit_remote_client_certificate">
604 </div>
605
606 <div>
607 <label for="edit_remote_ca_certificate" class="database_item_label">CA certificate:</label>
608 <input type="text"
609 name="edit_remote_ca_certificate"
610 id="edit_remote_ca_certificate">
611 </div>
612
613 <div>
614 <label for="edit_remote_certificate_path" class="database_item_label">Certificate path:</label>
615 <input type="text"
616 name="edit_remote_certificate_path"
617 id="edit_remote_certificate_path">
618 </div>
619
620 <div>
621 <label for="edit_remote_specified_cipher" class="database_item_label">Specified Cipher:</label>
622 <input type="text"
623 name="edit_remote_specified_cipher"
624 id="edit_remote_specified_cipher">
625 </div>
626 </div>
627
628 <div>
629 <label class="database_item_label"></label>
630 <input type="button"
631 value="Test"
632 onclick="test_remote_connection('edit_'); return false;"
633 id="edit_remote_test_button"
634 class="button">
635 <input type="button"
636 value="Clear"
637 onclick="test_remote_clear('edit_'); return false;"
638 id="edit_remote_clear_button"
639 class="button">
640 </div>
641
642 <div id="edit_remote_database_block_test">
643 <div id="edit_remote_database_block_test_content"
644 class="remote_database_block_test_content"></div>
645 </div>
646
647 <input type="hidden" name="action" value="edit_db">
648 <?php
649 wp_nonce_field( 'wpda-edit-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnonceeditdb', false );
650 ?>
651
652 <?php
653 $this->buttons();
654 ?>
655
656 </form>
657
658 <?php
659 }
660
661 private function create_local_database() {
662 ?>
663
664 <h3 class="wpda_db_title">
665 <?php
666 echo __( 'Create local database', 'wp-data-access' );
667 ?>
668 </h3>
669
670 <?php
671 if ( !$this->user_can_create_db ) {
672 echo __( 'You are not authorized to create local databases', 'wp-data-access' );
673 } else {
674 ?>
675
676 <form method="post"
677 action="?page=<?php
678 echo esc_attr( $this->page );
679 ?>"
680 onsubmit="return createdb_validate_form_local();"
681 >
682
683 <div>
684 <label for="local_database" class="database_item_label">Database name:</label>
685 <input type="text" name="local_database" id="local_database">
686 </div>
687
688 <input type="hidden" name="action" value="create_db">
689 <input type="hidden" name="database_location" value="local">
690 <?php
691 wp_nonce_field( 'wpda-create-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncecreatedb', false );
692 ?>
693
694 <?php
695 $this->buttons();
696 ?>
697
698 </form>
699
700 <?php
701 }
702 }
703
704 private function create_remote_database() {
705 ?>
706
707 <form method="post" action="?page=<?php
708 echo esc_attr( $this->page );
709 ?>"
710 onsubmit="return createdb_validate_form_remote();">
711
712 <h3 class="wpda_db_title">
713 <?php
714 echo __( 'Create remote database connection', 'wp-data-access' );
715 ?>
716 </h3>
717
718 <div>
719 <label for="remote_database"
720 class="database_item_label">Database name:</label>
721 <input type="text" name="remote_database" id="remote_database" value="rdb:">
722 </div>
723
724 <div>
725 <label for="remote_host" class="database_item_label">MySQL host:</label>
726 <input type="text" name="remote_host" id="remote_host">
727 </div>
728
729 <div>
730 <label for="remote_user" class="database_item_label">MySQL username:</label>
731 <input type="text" name="remote_user" id="remote_user">
732 </div>
733
734 <div>
735 <label for="remote_passwd" class="database_item_label">MySQL password:</label>
736 <input type="password" name="remote_passwd" id="remote_passwd" autocomplete="new-password">
737 <i class="fas fa-eye" onclick="wpda_toggle_password('remote_passwd', event)"
738 style="cursor:pointer"></i>
739 </div>
740
741 <div>
742 <label for="remote_port" class="database_item_label">MySQL port:</label>
743 <input type="text" name="remote_port" id="remote_port" value="3306">
744 </div>
745
746 <div>
747 <label for="remote_schema" class="database_item_label">MySQL schema:</label>
748 <input type="text" name="remote_schema" id="remote_schema">
749 </div>
750
751 <div class="wpda_db_ssl">
752 <label for="remote_ssl"
753 class="database_item_label">SSL:</label>
754 <input type="checkbox" name="remote_ssl" id="remote_ssl" unchecked
755 onclick="jQuery('#remote_database_block_ssl').toggle()">
756 </div>
757
758 <div id="remote_database_block_ssl">
759 <div>
760 <label for="remote_client_key" class="database_item_label">Client key:</label>
761 <input type="text" name="remote_client_key" id="remote_client_key">
762 </div>
763
764 <div>
765 <label for="remote_client_certificate" class="database_item_label">Client certificate:</label>
766 <input type="text" name="remote_client_certificate" id="remote_client_certificate">
767 </div>
768
769 <div>
770 <label for="remote_ca_certificate" class="database_item_label">CA certificate:</label>
771 <input type="text" name="remote_ca_certificate" id="remote_ca_certificate">
772 </div>
773
774 <div>
775 <label for="remote_certificate_path" class="database_item_label">Certificate path:</label>
776 <input type="text" name="remote_certificate_path" id="remote_certificate_path">
777 </div>
778
779 <div>
780 <label for="remote_specified_cipher" class="database_item_label">Specified Cipher:</label>
781 <input type="text" name="remote_specified_cipher" id="remote_specified_cipher">
782 </div>
783 </div>
784
785 <div>
786 <label class="database_item_label"></label>
787 <input type="button" value="Test" onclick="test_remote_connection(); return false;"
788 id="remote_test_button" class="button">
789 <input type="button" value="Clear" onclick="test_remote_clear(); return false;"
790 id="remote_clear_button" class="button" style="display:none;">
791 </div>
792
793 <div id="remote_database_block_test">
794 <div id="remote_database_block_test_content"
795 class="remote_database_block_test_content"></div>
796 </div>
797
798 <input type="hidden" name="action" value="create_db">
799 <input type="hidden" name="database_location" value="remote">
800 <?php
801 wp_nonce_field( 'wpda-create-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncecreatedb', false );
802 ?>
803
804 <?php
805 $this->buttons();
806 ?>
807
808 </form>
809
810 <?php
811 }
812
813 private function buttons() {
814 ?>
815
816 <div class="wpda_db_buttons">
817 <a href="javascript:void(0)"
818 onclick="jQuery(this).closest('form').submit()"
819 class="button button-primary"><i
820 class="fas fa-cloud-upload wpda_icon_on_button"></i> <?php
821 echo __( 'Save', 'wp-data-access' );
822 ?>
823 </a>
824 <a href="javascript:void(0)"
825 onclick="jQuery('#wpda_manage_databases').hide()"
826 class="button button-secondary"><i
827 class="fas fa-times-circle wpda_icon_on_button"></i> <?php
828 echo __( 'Cancel', 'wp-data-access' );
829 ?>
830 </a>
831 </div>
832
833 <?php
834 }
835
836 private function drop_database() {
837 ?>
838
839 <form id="wpda_form_drop_db"
840 method="post" action="?page=<?php
841 echo esc_attr( $this->page );
842 ?>"
843 >
844 <input type="hidden" name="database" id="drop_database">
845 <input type="hidden" name="action" value="drop_db">
846 <?php
847 wp_nonce_field( 'wpda-drop-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncedropdb', false );
848 ?>
849 </form>
850
851 <?php
852 }
853
854 private function js() {
855 ?>
856
857 <script>
858 function editdb_validate_form() {
859 if (jQuery('#edit_remote_database').val() === '' || jQuery('#edit_remote_database').val() === 'rdb:') {
860 alert('Database name must be entered');
861 return false;
862 }
863
864 if (jQuery('#edit_remote_host').val() === '') {
865 alert('MySQL host must be entered');
866 return false;
867 }
868
869 if (jQuery('#edit_remote_user').val() === '') {
870 alert('MySQL username must be entered');
871 return false;
872 }
873
874 if (jQuery('#edit_remote_schema').val() === '') {
875 alert('MySQL schema must be entered');
876 return false;
877 }
878
879 return true;
880 }
881
882 function createdb_validate_form_remote() {
883 if (jQuery('#remote_database').val() === '' || jQuery('#remote_database').val() === 'rdb:') {
884 alert('Database name must be entered');
885 return false;
886 }
887
888 if (jQuery('#remote_host').val() === '') {
889 alert('MySQL host must be entered');
890 return false;
891 }
892
893 if (jQuery('#remote_user').val() === '') {
894 alert('MySQL username must be entered');
895 return false;
896 }
897
898 if (jQuery('#remote_schema').val() === '') {
899 alert('MySQL schema must be entered');
900 return false;
901 }
902
903 return true;
904 }
905
906 function createdb_validate_form_local() {
907 if (jQuery('#local_database').val() === '') {
908 alert('Database name must be entered');
909 return false;
910 }
911
912 return true;
913 }
914
915 function test_remote_clear(mode = '') {
916 jQuery('#' + mode + 'remote_database_block_test_content').html('');
917 jQuery('#' + mode + 'remote_database_block_test').hide();
918 jQuery('#' + mode + 'remote_clear_button').hide();
919 }
920
921 function test_remote_connection(mode = '') {
922 host = jQuery('#' + mode + 'remote_host').val();
923 user = jQuery('#' + mode + 'remote_user').val();
924 pass = jQuery('#' + mode + 'remote_passwd').val();
925 port = jQuery('#' + mode + 'remote_port').val();
926 dbs = jQuery('#' + mode + 'remote_schema').val();
927 ssl = jQuery('#' + mode + 'remote_ssl').val();
928 ssl_key = jQuery('#' + mode + 'remote_client_key').val();
929 ssl_cert = jQuery('#' + mode + 'remote_client_certificate').val();
930 ssl_ca = jQuery('#' + mode + 'remote_ca_certificate').val();
931 ssl_path = jQuery('#' + mode + 'remote_certificate_path').val();
932 ssl_cipher = jQuery('#' + mode + 'remote_specified_cipher').val();
933
934 url = '//' + window.location.host + window.location.pathname +
935 '?action=wpda_check_remote_database_connection';
936
937 jQuery('#' + mode + 'remote_test_button').val('Testing...');
938
939 jQuery.ajax({
940 method: 'POST',
941 url: url,
942 data: {
943 host: host,
944 user: user,
945 passwd: pass,
946 port: port,
947 schema: dbs,
948 ssl: ssl,
949 ssl_key: ssl_key,
950 ssl_cert: ssl_cert,
951 ssl_ca: ssl_ca,
952 ssl_path: ssl_path,
953 ssl_cipher: ssl_cipher
954 }
955 }).done(
956 function (msg) {
957 jQuery('#' + mode + 'remote_database_block_test_content').html(msg);
958 jQuery('#' + mode + 'remote_database_block_test').show();
959 }
960 ).fail(
961 function () {
962 jQuery('#' + mode + 'remote_database_block_test_content').html('Preparing connection...<br/>Establishing connection...<br/><br/><strong>Remote database connection invalid</strong>');
963 jQuery('#' + mode + 'remote_database_block_test').show();
964 }
965 ).always(
966 function () {
967 jQuery('#' + mode + 'remote_test_button').val('Test');
968 jQuery('#' + mode + 'remote_clear_button').show();
969 }
970 );
971 }
972
973 jQuery(function () {
974 jQuery('#database_location').on('change', function () {
975 if (jQuery(this).val() === 'remote') {
976 jQuery('#local_database_block').hide();
977 jQuery('#remote_database_block').show();
978 } else {
979 jQuery('#remote_database_block').hide();
980 jQuery('#local_database_block').show();
981 }
982 });
983
984 jQuery('#remote_database, #edit_remote_database').keydown(function () {
985 var field = this;
986 setTimeout(function () {
987 if (field.value.indexOf('rdb:') !== 0) {
988 jQuery(field).val('rdb:');
989 }
990 }, 1);
991 });
992
993 jQuery('#wpda_db_tabs').tabs();
994
995 jQuery("#manage_db_selection").on('change', function () {
996 const db_name = this.value;
997 const db_type = jQuery("#manage_db_selection").find(':selected').data("type");
998
999 if (db_type === 'local') {
1000 jQuery("#edit_remote_database_form").hide();
1001 jQuery("#edit_local_database_form").show();
1002
1003 jQuery("#edit_local_database").val(db_name);
1004 } else {
1005 jQuery("#edit_local_database_form").hide();
1006 jQuery("#edit_remote_database_form").show();
1007
1008 if (wpda_rdb[db_name]) {
1009 // Load remote database info
1010 const rdb = wpda_rdb[db_name];
1011 jQuery("#edit_remote_database").val(db_name)
1012 jQuery("#edit_remote_database_old").val(db_name)
1013
1014 jQuery("#edit_remote_host").val(rdb.host)
1015 jQuery("#edit_remote_user").val(rdb.username)
1016 jQuery("#edit_remote_passwd").val(rdb.password)
1017 jQuery("#edit_remote_port").val(rdb.port)
1018 jQuery("#edit_remote_schema").val(rdb.database)
1019
1020 jQuery("#edit_remote_ssl").prop("checked", rdb.ssl === "on")
1021 jQuery("#edit_remote_client_key").val(rdb.ssl_key)
1022 jQuery("#edit_remote_client_certificate").val(rdb.ssl_cert)
1023 jQuery("#edit_remote_ca_certificate").val(rdb.ssl_ca)
1024 jQuery("#edit_remote_certificate_path").val(rdb.ssl_path)
1025 jQuery("#edit_remote_specified_cipher").val(rdb.ssl_cipher)
1026
1027 if (rdb.ssl === "on") {
1028 jQuery('#edit_remote_database_block_ssl').show();
1029 } else {
1030 jQuery('#edit_remote_database_block_ssl').hide();
1031 }
1032 } else {
1033 if (db_name !== "") {
1034 alert("Database not found");
1035 jQuery("#edit_remote_database_form").hide();
1036 } else {
1037 jQuery("#edit_remote_database_form").hide();
1038 jQuery("#edit_local_database_form").hide();
1039 }
1040 }
1041 }
1042
1043 if (db_name === '') {
1044 jQuery("#manage_db_create_wp_user_access").hide();
1045 } else {
1046 jQuery("#manage_db_create_wp_user_access").show();
1047 }
1048 })
1049
1050 jQuery("#manage_db_create_wp_user_access").on('click', function() {
1051 const selectedDatabase = jQuery("#manage_db_selection").val()
1052 wpda_dbinit_admin( selectedDatabase, '<?php
1053 echo wp_create_nonce( 'wpda_dbinit_admin_' . WPDA::get_current_user_login() );
1054 ?>' )
1055 })
1056
1057 jQuery("#edit_local_database_action").on('click', function () {
1058 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?')) {
1059 jQuery("#drop_database").val(jQuery("#edit_local_database").val())
1060 jQuery("#wpda_form_drop_db").submit();
1061 }
1062 })
1063
1064 jQuery("#remote_local_database_action").on('click', function () {
1065 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?')) {
1066 jQuery("#drop_database").val(jQuery("#edit_remote_database").val())
1067 jQuery("#wpda_form_drop_db").submit();
1068 }
1069 })
1070 });
1071
1072 var wpda_rdb = <?php
1073 echo json_encode( $this->rdb );
1074 ?>;
1075 </script>
1076
1077 <?php
1078 }
1079
1080 private function css() {
1081 ?>
1082
1083 <style>
1084 #wpda_manage_databases {
1085 position: relative;
1086 display: none;
1087 padding: 24px;
1088 margin-bottom: 5px;
1089 border-radius: 4px;
1090 background-color: #fff;
1091 color: rgba(0, 0, 0, 0.87);
1092 -webkit-transition: box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
1093 transition: box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
1094 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);
1095 }
1096
1097 #wpda_db_tabs {
1098 border: none;
1099 }
1100
1101 #wpda_db_tabs ul {
1102 background: none;
1103 border-top: none;
1104 border-right: none;
1105 border-left: none;
1106 border-radius: 0;
1107 }
1108
1109 #edit_remote_clear_button,
1110 #edit_remote_database_block_test,
1111 #wpda_form_drop_db,
1112 #remote_database_block_ssl,
1113 #remote_database_block_test,
1114 #edit_remote_database_form,
1115 #edit_local_database_form {
1116 display: none;
1117 }
1118
1119 #edit_remote_database_block_ssl,
1120 #remote_database_block_ssl {
1121 margin-bottom: 10px;
1122 }
1123
1124 #edit_remote_database_block_test,
1125 #remote_database_block_test {
1126 margin-top: 10px;
1127 }
1128
1129 .wpda_manage_dbs,
1130 .wpda_db_title {
1131 margin-bottom: 30px;
1132 }
1133
1134 .database_item_label {
1135 vertical-align: baseline;
1136 }
1137
1138 .wpda_db_ssl {
1139 margin-top: 10px;
1140 margin-bottom: 10px;
1141 }
1142
1143 .wpda_db_buttons {
1144 margin-top: 30px;
1145 }
1146 .wpda_manage_databases_close {
1147 position: absolute;
1148 top: 22px;
1149 right: 22px;
1150 z-index: 9999999;
1151 font-size: 1rem;
1152 }
1153
1154 .wpda_manage_databases_close .fas {
1155 color: rgb(60, 67, 74);
1156 }
1157
1158 .restyle_link a {
1159 color: #2271b1;
1160 text-decoration: none;
1161 font-weight: 700;
1162 }
1163 </style>
1164
1165 <?php
1166 }
1167
1168 }
1169