PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.85
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.85
5.5.85 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 All 161 releases
wp-data-access / WPDataAccess / Dashboard / WPDA_Widget_Dbms.php

WPDA_Widget_Dbms.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.85, at WPDataAccess/Dashboard/WPDA_Widget_Dbms.php

386 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- verified on page
4 namespace WPDataAccess\Dashboard;
5
6 use WPDataAccess\WPDA;
7 /**
8 * DBMS widget implementation
9 */
10 class WPDA_Widget_Dbms extends WPDA_Widget {
11 /**
12 * DBMS widget id
13 *
14 * @var string
15 */
16 protected $id;
17
18 /**
19 * Database schema name
20 *
21 * @var mixed
22 */
23 protected $schema_name;
24
25 /**
26 * Constructor
27 *
28 * @param array $args Arguments.
29 */
30 public function __construct( $args = array() ) {
31 parent::__construct( $args );
32 $this->can_share = true;
33 $this->can_refresh = true;
34 global $wpdb;
35 if ( isset( $args['schema_name'] ) ) {
36 $schema_name = $args['schema_name'];
37 if ( 'wpdb' === $schema_name ) {
38 $schema_name = $wpdb->dbname;
39 $this->title = "WordPress database ({$schema_name})";
40 } else {
41 $this->title = "Remote database {$schema_name}";
42 }
43 } else {
44 $schema_name = $wpdb->dbname;
45 $this->title = "WordPress database ({$schema_name})";
46 }
47 $this->id = 'dbms' . $this->widget_id . str_replace( ':', '_', $schema_name );
48 $this->schema_name = $schema_name;
49 $info = static::get_data( $schema_name );
50 $dbms_status = "\n\t\t\t\t<div id='{$this->id}' class='wpda-dbms-container'>\n\t\t\t\t\t<ul>\n\t\t\t\t\t\t<li><a href='#{$this->id}-1'>Instance</a></li>\n\t\t\t\t\t\t<li><a href='#{$this->id}-2'>Variables</a></li>\n\t\t\t\t\t\t<li><a href='#{$this->id}-3'>Status</a></li>\n\t\t\t\t\t</ul>\n\t\t\t\t\t<div id='{$this->id}-1'>\n\t\t\t\t\t\t<table class='wpda_dbms_table' class='ui-widget-content'>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Host</th>\n\t\t\t\t\t\t\t\t<td class='hostname'>{$info['hostname']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Port</th>\n\t\t\t\t\t\t\t\t<td class='post'>{$info['port']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>SSL</th>\n\t\t\t\t\t\t\t\t<td class='ssl'>{$info['ssl']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Version</th>\n\t\t\t\t\t\t\t\t<td class='version'>{$info['version']} {$info['version_comment']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Compiled For</th>\n\t\t\t\t\t\t\t\t<td class='compiled'>{$info['version_compile_os']} ({$info['version_compile_machine']})</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th>Uptime</th>\n\t\t\t\t\t\t\t\t<td class='uptime'>{$info['uptime']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th class='wpda-widget-dbms-separator' colspan='2' id='{$this->id}-dir' class='wpda-dbms-link'><i class='fas fa-caret-right'></i> Server Directories</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='dir' style='display: none'>\n\t\t\t\t\t\t\t\t<th>Base Directory</th>\n\t\t\t\t\t\t\t\t<td class='basedir'>{$info['basedir']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='dir' style='display: none'>\n\t\t\t\t\t\t\t\t<th>Data Directory</th>\n\t\t\t\t\t\t\t\t<td class='datadir'>{$info['datadir']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='dir' style='display: none'>\n\t\t\t\t\t\t\t\t<th>Plugins Directory</th>\n\t\t\t\t\t\t\t\t<td class='plugin_dir'>{$info['plugin_dir']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='dir' style='display: none'>\n\t\t\t\t\t\t\t\t<th class='wpda-widget-dbms-separator-end'>Tmp Directory<br/></th>\n\t\t\t\t\t\t\t\t<td class='tmpdir'>{$info['tmpdir']}</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2' id='{$this->id}-log' class='wpda-dbms-link'><i class='fas fa-caret-right'></i> Log Files</th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='log' style='display: none'>\n\t\t\t\t\t\t\t\t<th>Error Log</th>\n\t\t\t\t\t\t\t\t<td class='log_error'>{$info['log_error']} [ON]</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='log' style='display: none'>\n\t\t\t\t\t\t\t\t<th>General Log</th>\n\t\t\t\t\t\t\t\t<td class='general_log'>{$info['general_log_file']} [{$info['general_log']}]</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr class='log' style='display: none'>\n\t\t\t\t\t\t\t\t<th>Slow Query Log</th>\n\t\t\t\t\t\t\t\t<td class='slow_query'>{$info['slow_query_log_file']} [{$info['slow_query_log']}]</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</div>\n\t\t\t\t\t<div id='{$this->id}-2' style='display: none'></div>\n\t\t\t\t\t<div id='{$this->id}-3' style='display: none'></div>\n\t\t\t\t</div>\n\t\t\t";
51 $this->content = $dbms_status;
52 }
53
54 /**
55 * DBMS widget shortcode
56 *
57 * @param WPDA_Widget_Dbms $widget Widget.
58 * @return void
59 */
60 public function do_shortcode( $widget ) {
61 }
62
63 /**
64 * Embed DBMS widget
65 *
66 * @param WPDA_Widget_Dbms $widget Widget.
67 * @param string $target_element HTML element id.
68 * @return void
69 */
70 public function do_embed( $widget, $target_element ) {
71 }
72
73 /**
74 * DBMS widget javascript code
75 *
76 * @param boolean $is_backend Running in back-end.
77 * @return void
78 */
79 protected function js( $is_backend = true ) {
80 ?>
81 <script type='application/javascript' class="wpda-widget-<?php
82 echo esc_attr( $this->widget_id );
83 ?>">
84 jQuery(function() {
85 <?php
86 $this->js_init( $is_backend );
87 ?>
88
89 jQuery("#<?php
90 echo esc_attr( $this->id );
91 ?>").closest(".wpda-widget").find(".wpda-widget-refresh").on("click", function(e, doaction = null) {
92 if (doaction===null) {
93 getDbmsInfo(
94 jQuery("#<?php
95 echo esc_attr( $this->id );
96 ?>"),
97 action,
98 args_info
99 );
100
101 getDbmsVars(
102 jQuery("#<?php
103 echo esc_attr( $this->id );
104 ?>-2"),
105 action,
106 args_vars,
107 true
108 );
109
110 getDbmsVars(
111 jQuery("#<?php
112 echo esc_attr( $this->id );
113 ?>-3"),
114 action,
115 args_status,
116 true
117 );
118 }
119 });
120
121 <?php
122 ?>
123 });
124 </script>
125 <?php
126 }
127
128 /**
129 * Javascript initialization code
130 *
131 * @param boolean $is_backend Running in back-end.
132 * @param string $wpda_caller Caller.
133 * @return void
134 */
135 protected function js_init( $is_backend = true, $wpda_caller = '' ) {
136 // Do not add script tags: this code is injected in JS code.
137 ?>
138 var action = "wpda_widget_dbms_refresh";
139 var args_vars = {};
140 args_vars.wpda_name = "<?php
141 echo esc_attr( $this->name );
142 ?>";
143 <?php
144 if ( $is_backend ) {
145 ?>
146 args_vars.wp_nonce = "<?php
147 echo esc_attr( $this->wp_nonce );
148 ?>";
149 <?php
150 } else {
151 ?>
152 args_vars.wpda_sonce = "<?php
153 echo esc_attr( WPDA::wpda_create_sonce() );
154 ?>";
155 <?php
156 }
157 ?>
158 args_vars.wpda_action = "vars";
159 args_vars.wpda_caller = "<?php
160 echo esc_attr( $wpda_caller );
161 ?>";
162 console.log(args_vars);
163
164 var args_status = Object.assign({}, args_vars);
165 args_status.wpda_action = "status";
166
167 var args_info = Object.assign({}, args_vars);
168 args_info.wpda_action = "info";
169
170 setTimeout(waitForJUI_<?php
171 echo esc_attr( $this->id );
172 ?>, 1000);
173 function waitForJUI_<?php
174 echo esc_attr( $this->id );
175 ?>() {
176 if (window.jQuery && typeof jQuery.ui!=="undefined" && jQuery("#<?php
177 echo esc_attr( $this->id );
178 ?>")) {
179 jQuery("#<?php
180 echo esc_attr( $this->id );
181 ?>").tabs();
182 // console.log("jQuery UI loaded...");
183 } else {
184 setTimeout(waitForJUI_<?php
185 echo esc_attr( $this->id );
186 ?>, 1000);
187 console.log("Waiting for jQuery UI to be loaded...");
188 }
189 }
190
191 jQuery("#<?php
192 echo esc_attr( $this->id );
193 ?> :nth-child(2)").on("click", function() {
194 getDbmsVars(
195 jQuery("#<?php
196 echo esc_attr( $this->id );
197 ?>-2"),
198 action,
199 args_vars
200 );
201 });
202
203 jQuery("#<?php
204 echo esc_attr( $this->id );
205 ?> :nth-child(3)").on("click", function() {
206 getDbmsVars(
207 jQuery("#<?php
208 echo esc_attr( $this->id );
209 ?>-3"),
210 action,
211 args_status
212 );
213 });
214
215 jQuery("#<?php
216 echo esc_attr( $this->id );
217 ?>-dir").on("click", function(e) {
218 toggleIcon(jQuery(this));
219 jQuery(this).closest("tbody").find(".dir").toggle();
220 });
221
222 jQuery("#<?php
223 echo esc_attr( $this->id );
224 ?>-log").on("click", function() {
225 toggleIcon(jQuery(this));
226 jQuery(this).closest("tbody").find(".log").toggle();
227 });
228 <?php
229 }
230
231 /**
232 * Construct widget via ajax
233 *
234 * @return void
235 */
236 public static function widget() {
237 $panel_name = ( isset( $_REQUEST['wpda_panel_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_name'] ) ) : '' );
238 $panel_dbms = ( isset( $_REQUEST['wpda_panel_dbms'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_dbms'] ) ) : '' );
239 $panel_column = ( isset( $_REQUEST['wpda_panel_column'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_column'] ) ) : '1' );
240 $column_position = ( isset( $_REQUEST['wpda_column_position'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_column_position'] ) ) : 'prepend' );
241 $widget_sequence_nr = ( isset( $_REQUEST['wpda_widget_sequence_nr'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_widget_sequence_nr'] ) ) : '1' );
242 $wdg = new WPDA_Widget_Dbms(array(
243 'name' => $panel_name,
244 'schema_name' => $panel_dbms,
245 'column' => $panel_column,
246 'position' => $column_position,
247 'widget_id' => $widget_sequence_nr,
248 ));
249 WPDA::sent_header( 'text/html; charset=UTF-8' );
250 // phpcs:disable WordPress.Security.EscapeOutput
251 echo $wdg->container();
252 // phpcs:enable WordPress.Security.EscapeOutput
253 wp_die();
254 }
255
256 /**
257 * Refresh widget via ajax
258 *
259 * @return void
260 */
261 public static function refresh() {
262 $is_header_send = false;
263 $schema_name = null;
264 $widget_name = ( isset( $_POST['wpda_name'] ) ? sanitize_text_field( wp_unslash( $_POST['wpda_name'] ) ) : null );
265 if ( !$is_header_send ) {
266 WPDA::sent_header( 'application/json' );
267 }
268 if ( !isset( $_REQUEST['wpda_action'] ) ) {
269 // phpcs:disable WordPress.Security.EscapeOutput
270 echo static::msg( 'ERROR', 'Invalid arguments' );
271 // phpcs:enable WordPress.Security.EscapeOutput
272 wp_die();
273 }
274 if ( null === $schema_name ) {
275 $widgets = get_user_meta( WPDA::get_current_user_id(), WPDA_Dashboard::USER_DASHBOARD );
276 $widget_found = false;
277 foreach ( $widgets as $widget ) {
278 if ( isset( $widget[$widget_name] ) ) {
279 $schema_name = $widgets[$widget_name]['dbsDbms'];
280 $widget_found = true;
281 }
282 }
283 if ( !$widget_found ) {
284 // phpcs:disable WordPress.Security.EscapeOutput
285 echo static::msg( 'ERROR', 'Invalid arguments' );
286 // phpcs:enable WordPress.Security.EscapeOutput
287 wp_die();
288 }
289 }
290 switch ( $_REQUEST['wpda_action'] ) {
291 case 'vars':
292 static::get_vars( $schema_name );
293 break;
294 case 'status':
295 static::get_status( $schema_name );
296 break;
297 case 'info':
298 static::get_info( $schema_name );
299 break;
300 default:
301 // phpcs:disable WordPress.Security.EscapeOutput
302 echo static::msg( 'ERROR', 'Invalid arguments' );
303 // phpcs:enable WordPress.Security.EscapeOutput
304 wp_die();
305 }
306 }
307
308 /**
309 * Get DBMS data
310 *
311 * @param string $schema_name Database schema name.
312 * @return array
313 */
314 protected static function get_data( $schema_name ) {
315 return array(
316 'hostname' => WPDA::get_dbms_var( 'hostname', $schema_name ),
317 'ssl' => WPDA::get_dbms_var( 'have_ssl', $schema_name ),
318 'port' => WPDA::get_dbms_var( 'port', $schema_name ),
319 'version' => WPDA::get_dbms_var( 'version', $schema_name ),
320 'version_comment' => WPDA::get_dbms_var( 'version_comment', $schema_name ),
321 'version_compile_os' => WPDA::get_dbms_var( 'version_compile_os', $schema_name ),
322 'version_compile_machine' => WPDA::get_dbms_var( 'version_compile_machine', $schema_name ),
323 'uptime' => WPDA::secondsToTime( WPDA::get_dbms_global( 'uptime', $schema_name ) ),
324 'basedir' => WPDA::get_dbms_var( 'basedir', $schema_name ),
325 'datadir' => WPDA::get_dbms_var( 'datadir', $schema_name ),
326 'plugin_dir' => WPDA::get_dbms_var( 'plugin_dir', $schema_name ),
327 'tmpdir' => WPDA::get_dbms_var( 'tmpdir', $schema_name ),
328 'log_error' => WPDA::get_dbms_var( 'log_error', $schema_name ),
329 'general_log' => WPDA::get_dbms_var( 'general_log', $schema_name ),
330 'general_log_file' => WPDA::get_dbms_var( 'general_log_file', $schema_name ),
331 'slow_query_log' => WPDA::get_dbms_var( 'slow_query_log', $schema_name ),
332 'slow_query_log_file' => WPDA::get_dbms_var( 'slow_query_log_file', $schema_name ),
333 );
334 }
335
336 /**
337 * Get DBMS vars
338 *
339 * @param string $schema_name Database schema name.
340 * @return void
341 */
342 protected static function get_vars( $schema_name ) {
343 $vars = WPDA::get_dbms_var( null, $schema_name );
344 $json = array();
345 foreach ( $vars as $var ) {
346 $json[$var[0]] = $var[1];
347 }
348 echo json_encode( $json );
349 // phpcs:ignore
350 wp_die();
351 }
352
353 /**
354 * Get DBMS status
355 *
356 * @param string $schema_name Database schema name.
357 * @return void
358 */
359 protected static function get_status( $schema_name ) {
360 $vars = WPDA::get_dbms_global( null, $schema_name );
361 $json = array();
362 foreach ( $vars as $var ) {
363 $json[$var[0]] = $var[1];
364 }
365 echo json_encode( $json );
366 // phpcs:ignore
367 wp_die();
368 }
369
370 /**\
371 * Get DBMS info
372 *
373 * @param string $schema_name Database schema name.
374 * @return void
375 */
376 protected static function get_info( $schema_name ) {
377 // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
378 $schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_schemaname'] ) );
379 echo json_encode( static::get_data( $schema_name ) );
380 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
381 wp_die();
382 }
383
384 }
385
386 // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing