| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 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 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 239 |
$panel_dbms = ( isset( $_REQUEST['wpda_panel_dbms'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_dbms'] ) ) : '' ); |
| 240 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 241 |
$panel_column = ( isset( $_REQUEST['wpda_panel_column'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_column'] ) ) : '1' ); |
| 242 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 243 |
$column_position = ( isset( $_REQUEST['wpda_column_position'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_column_position'] ) ) : 'prepend' ); |
| 244 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 245 |
$widget_sequence_nr = ( isset( $_REQUEST['wpda_widget_sequence_nr'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_widget_sequence_nr'] ) ) : '1' ); |
| 246 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 247 |
$wdg = new WPDA_Widget_Dbms(array( |
| 248 |
'name' => $panel_name, |
| 249 |
'schema_name' => $panel_dbms, |
| 250 |
'column' => $panel_column, |
| 251 |
'position' => $column_position, |
| 252 |
'widget_id' => $widget_sequence_nr, |
| 253 |
)); |
| 254 |
WPDA::sent_header( 'text/html; charset=UTF-8' ); |
| 255 |
echo $wdg->container(); |
| 256 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 257 |
wp_die(); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Refresh widget via ajax |
| 262 |
* |
| 263 |
* @return void |
| 264 |
*/ |
| 265 |
public static function refresh() { |
| 266 |
$is_header_send = false; |
| 267 |
$schema_name = null; |
| 268 |
$widget_name = ( isset( $_POST['wpda_name'] ) ? sanitize_text_field( wp_unslash( $_POST['wpda_name'] ) ) : null ); |
| 269 |
if ( !$is_header_send ) { |
| 270 |
WPDA::sent_header( 'application/json' ); |
| 271 |
} |
| 272 |
if ( !isset( $_REQUEST['wpda_action'] ) ) { |
| 273 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 274 |
echo static::msg( 'ERROR', 'Invalid arguments' ); |
| 275 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 276 |
wp_die(); |
| 277 |
} |
| 278 |
if ( null === $schema_name ) { |
| 279 |
$widgets = get_user_meta( WPDA::get_current_user_id(), WPDA_Dashboard::USER_DASHBOARD ); |
| 280 |
$widget_found = false; |
| 281 |
foreach ( $widgets as $widget ) { |
| 282 |
if ( isset( $widget[$widget_name] ) ) { |
| 283 |
$schema_name = $widgets[$widget_name]['dbsDbms']; |
| 284 |
$widget_found = true; |
| 285 |
} |
| 286 |
} |
| 287 |
if ( !$widget_found ) { |
| 288 |
echo static::msg( 'ERROR', 'Invalid arguments' ); |
| 289 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 290 |
wp_die(); |
| 291 |
} |
| 292 |
} |
| 293 |
switch ( $_REQUEST['wpda_action'] ) { |
| 294 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 295 |
case 'vars': |
| 296 |
static::get_vars( $schema_name ); |
| 297 |
break; |
| 298 |
case 'status': |
| 299 |
static::get_status( $schema_name ); |
| 300 |
break; |
| 301 |
case 'info': |
| 302 |
static::get_info( $schema_name ); |
| 303 |
break; |
| 304 |
default: |
| 305 |
echo static::msg( 'ERROR', 'Invalid arguments' ); |
| 306 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 307 |
wp_die(); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Get DBMS data |
| 313 |
* |
| 314 |
* @param string $schema_name Database schema name. |
| 315 |
* @return array |
| 316 |
*/ |
| 317 |
protected static function get_data( $schema_name ) { |
| 318 |
return array( |
| 319 |
'hostname' => WPDA::get_dbms_var( 'hostname', $schema_name ), |
| 320 |
'ssl' => WPDA::get_dbms_var( 'have_ssl', $schema_name ), |
| 321 |
'port' => WPDA::get_dbms_var( 'port', $schema_name ), |
| 322 |
'version' => WPDA::get_dbms_var( 'version', $schema_name ), |
| 323 |
'version_comment' => WPDA::get_dbms_var( 'version_comment', $schema_name ), |
| 324 |
'version_compile_os' => WPDA::get_dbms_var( 'version_compile_os', $schema_name ), |
| 325 |
'version_compile_machine' => WPDA::get_dbms_var( 'version_compile_machine', $schema_name ), |
| 326 |
'uptime' => WPDA::secondsToTime( WPDA::get_dbms_global( 'uptime', $schema_name ) ), |
| 327 |
'basedir' => WPDA::get_dbms_var( 'basedir', $schema_name ), |
| 328 |
'datadir' => WPDA::get_dbms_var( 'datadir', $schema_name ), |
| 329 |
'plugin_dir' => WPDA::get_dbms_var( 'plugin_dir', $schema_name ), |
| 330 |
'tmpdir' => WPDA::get_dbms_var( 'tmpdir', $schema_name ), |
| 331 |
'log_error' => WPDA::get_dbms_var( 'log_error', $schema_name ), |
| 332 |
'general_log' => WPDA::get_dbms_var( 'general_log', $schema_name ), |
| 333 |
'general_log_file' => WPDA::get_dbms_var( 'general_log_file', $schema_name ), |
| 334 |
'slow_query_log' => WPDA::get_dbms_var( 'slow_query_log', $schema_name ), |
| 335 |
'slow_query_log_file' => WPDA::get_dbms_var( 'slow_query_log_file', $schema_name ), |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Get DBMS vars |
| 341 |
* |
| 342 |
* @param string $schema_name Database schema name. |
| 343 |
* @return void |
| 344 |
*/ |
| 345 |
protected static function get_vars( $schema_name ) { |
| 346 |
$vars = WPDA::get_dbms_var( null, $schema_name ); |
| 347 |
$json = array(); |
| 348 |
foreach ( $vars as $var ) { |
| 349 |
$json[$var[0]] = $var[1]; |
| 350 |
} |
| 351 |
echo json_encode( $json ); |
| 352 |
// phpcs:ignore - 8.1 proof |
| 353 |
wp_die(); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Get DBMS status |
| 358 |
* |
| 359 |
* @param string $schema_name Database schema name. |
| 360 |
* @return void |
| 361 |
*/ |
| 362 |
protected static function get_status( $schema_name ) { |
| 363 |
$vars = WPDA::get_dbms_global( null, $schema_name ); |
| 364 |
$json = array(); |
| 365 |
foreach ( $vars as $var ) { |
| 366 |
$json[$var[0]] = $var[1]; |
| 367 |
} |
| 368 |
echo json_encode( $json ); |
| 369 |
// phpcs:ignore - 8.1 proof |
| 370 |
wp_die(); |
| 371 |
} |
| 372 |
|
| 373 |
/**\ |
| 374 |
* Get DBMS info |
| 375 |
* |
| 376 |
* @param string $schema_name Database schema name. |
| 377 |
* @return void |
| 378 |
*/ |
| 379 |
protected static function get_info( $schema_name ) { |
| 380 |
$schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_schemaname'] ) ); |
| 381 |
// phpcs:ignore - 8.1 proof |
| 382 |
echo json_encode( static::get_data( $schema_name ) ); |
| 383 |
// phpcs:ignore - 8.1 proof |
| 384 |
wp_die(); |
| 385 |
} |
| 386 |
|
| 387 |
} |
| 388 |
|