← All changes
|
WPDataAccess/Query_Builder/WPDA_Query_Builder.php
+13
-534
5.5.76
→
5.5.84
View file →
| @@ -32,378 +32,9 @@ | ||
| 32 | 32 | protected $wpnonce = ''; |
| 33 | 33 | |
| 34 | 34 | protected $sql_hints = 'off'; |
| 35 | 35 | |
| 36 | - public function show() { | |
| 37 | - $dbs = WPDA_Dictionary_Lists::get_db_schemas(); | |
| 38 | - foreach ( $dbs as $db ) { | |
| 39 | - $this->databases[] = $db['schema_name']; | |
| 40 | - } | |
| 41 | - $this->default_database = WPDA::get_user_default_scheme(); | |
| 42 | - $sql_hints = get_option( self::QUERY_BUILDER_HINTS ); | |
| 43 | - if ( false !== $sql_hints ) { | |
| 44 | - $this->sql_hints = $sql_hints; | |
| 45 | - } | |
| 46 | - $this->wpnonce = wp_create_nonce( 'wpda-query-builder-' . WPDA::get_current_user_id() ); | |
| 47 | - $this->html(); | |
| 48 | - $this->css(); | |
| 49 | - $this->js(); | |
| 50 | - } | |
| 51 | - | |
| 52 | - protected function html() { | |
| 53 | - $title = 'Query Builder'; | |
| 54 | - ?> | |
| 55 | - <div class="wrap"> | |
| 56 | - <span style="float:right"> | |
| 57 | - <label style="font-weight:normal"><input type="checkbox" id="wpda_sql_hints" <?php | |
| 58 | - echo ( 'on' === $this->sql_hints ? 'checked' : '' ); | |
| 59 | - ?>> Enable SQL hints</label> | |
| 60 | - </span> | |
| 61 | - <h1 class="wp-heading-inline"> | |
| 62 | - <?php | |
| 63 | - echo esc_attr( $title ); | |
| 64 | - ?> | |
| 65 | - </h1> | |
| 66 | - <div id="wpda_query_builder_open" style="display: none"> | |
| 67 | - <fieldset class="wpda_fieldset" style="position:relative"> | |
| 68 | - <legend> | |
| 69 | - Queries stored for user <?php | |
| 70 | - echo esc_attr( WPDA::get_current_user_login() ); | |
| 71 | - ?> | |
| 72 | - </legend> | |
| 73 | - <label> | |
| 74 | - Select | |
| 75 | - </label> | |
| 76 | - <select id="wpda_query_builder_open_select"></select> | |
| 77 | - <a id="wpda_query_builder_open_open" | |
| 78 | - href="javascript:void(0)" | |
| 79 | - onclick="tabOpen()" | |
| 80 | - class="wpda_tooltip button button-primary" | |
| 81 | - title="Open query in new tab"> | |
| 82 | - <i class="fas fa-folder-open wpda_icon_on_button"></i> Open selected</a> | |
| 83 | - <a id="wpda_query_builder_open_openall" | |
| 84 | - href="javascript:void(0)" | |
| 85 | - onclick="tabOpenAll()" | |
| 86 | - class="wpda_tooltip button button-secondary" | |
| 87 | - title="Open all stored queries in new tabs"> | |
| 88 | - <i class="fas fa-folder-open wpda_icon_on_button"></i> Open all</a> | |
| 89 | - <a id="wpda_query_builder_open_delete" | |
| 90 | - href="javascript:void(0)" | |
| 91 | - onclick="deleteQuery()" | |
| 92 | - class="wpda_tooltip button button-secondary" | |
| 93 | - title="Delete query"> | |
| 94 | - <i class="fas fa-trash wpda_icon_on_button"></i> Delete selected</a> | |
| 95 | - <a href="javascript:void(0)" | |
| 96 | - onclick="closeQuery()" | |
| 97 | - class="wpda_tooltip button button-secondary" | |
| 98 | - title="Close panel"> | |
| 99 | - <i class="fas fa-times-circle wpda_icon_on_button"></i> Cancel</a> | |
| 100 | - </fieldset> | |
| 101 | - </div> | |
| 102 | - <div id="wpda_query_builder"> | |
| 103 | - <nav class="nav-tab-wrapper"> | |
| 104 | - </nav> | |
| 105 | - </div> | |
| 106 | - </div> | |
| 107 | - <?php | |
| 108 | - } | |
| 109 | - | |
| 110 | - protected function css() { | |
| 111 | - // Overwrite default style | |
| 112 | - ?> | |
| 113 | - <style> | |
| 114 | - .ui-corner-all, .ui-corner-top, .ui-corner-right, .ui-corner-tr, .ui-corner-left, .ui-corner-tl { | |
| 115 | - border-radius: 0 !important; | |
| 116 | - } | |
| 117 | - .ui-widget.ui-widget-content { | |
| 118 | - border: none; | |
| 119 | - } | |
| 120 | - .wpda_query_builder_tabs .ui-widget-content { | |
| 121 | - background: none; | |
| 122 | - color: inherit; | |
| 123 | - } | |
| 124 | - /*.ui-widget-header {*/ | |
| 125 | - /* font-weight: normal;*/ | |
| 126 | - /* background: none;*/ | |
| 127 | - /* border: none;*/ | |
| 128 | - /* border-bottom: 1px solid #c3c4c7;*/ | |
| 129 | - /*}*/ | |
| 130 | - .ui-tabs .ui-tabs-nav li.ui-tabs-active .ui-tabs-anchor, .ui-tabs .ui-tabs-nav li.ui-state-disabled .ui-tabs-anchor, .ui-tabs .ui-tabs-nav li.ui-tabs-loading .ui-tabs-anchor { | |
| 131 | - cursor: pointer; | |
| 132 | - } | |
| 133 | - .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default, .ui-button, html .ui-button.ui-state-disabled:hover, html .ui-button.ui-state-disabled:active { | |
| 134 | - border: 1px solid #c3c4c7; | |
| 135 | - font-weight: bold; | |
| 136 | - color: #000 !important; | |
| 137 | - background: #dcdcde !important; | |
| 138 | - } | |
| 139 | - .ui-state-default:hover { | |
| 140 | - background: #f0f0f1 !important; | |
| 141 | - } | |
| 142 | - .ui-state-active, .ui-widget-content .ui-state-active, .ui-widget-header .ui-state-active, a.ui-button:active, .ui-button:active, .ui-button.ui-state-active:hover { | |
| 143 | - border: 1px solid #c3c4c7; | |
| 144 | - border-bottom: none; | |
| 145 | - font-weight: bold; | |
| 146 | - color: #000 !important; | |
| 147 | - background: #f0f0f1 !important; | |
| 148 | - } | |
| 149 | - .ui-tabs-anchor { | |
| 150 | - color: #50575e !important; | |
| 151 | - } | |
| 152 | - .ui-tabs-anchor span.dashicons { | |
| 153 | - padding-left: 5px; | |
| 154 | - } | |
| 155 | - .ui-tabs .ui-tabs-panel { | |
| 156 | - padding: 0; | |
| 157 | - font-family: inherit; | |
| 158 | - font-size: inherit; | |
| 159 | - } | |
| 160 | - .ui-tabs .ui-tabs-nav li { | |
| 161 | - margin-left: 4px; | |
| 162 | - } | |
| 163 | - .ui-tabs .ui-tabs-nav .ui-tabs-anchor { | |
| 164 | - line-height: 20px; | |
| 165 | - } | |
| 166 | - </style> | |
| 167 | - <?php | |
| 168 | - } | |
| 169 | - | |
| 170 | - protected function js() { | |
| 171 | - ?> | |
| 172 | - <script type="application/javascript"> | |
| 173 | - var wpda_default_database = '<?php | |
| 174 | - echo esc_attr( $this->default_database ); | |
| 175 | - ?>'; | |
| 176 | - var wpda_databases = ''; | |
| 177 | - <?php | |
| 178 | - global $wpdb; | |
| 179 | - $database_options = ''; | |
| 180 | - foreach ( $this->databases as $database ) { | |
| 181 | - if ( $wpdb->dbname === $database ) { | |
| 182 | - $database_printed = "WordPress database ({$database})"; | |
| 183 | - } else { | |
| 184 | - $database_printed = $database; | |
| 185 | - } | |
| 186 | - $database_options .= '<option value="' . $database . '">' . $database_printed . '</option>'; | |
| 187 | - } | |
| 188 | - echo "wpda_databases = '{$database_options}';"; | |
| 189 | - // phpcs:ignore WordPress.Security.EscapeOutput | |
| 190 | - ?> | |
| 191 | - var wpda_home_url = "<?php | |
| 192 | - echo admin_url( 'admin.php' ); | |
| 193 | - // phpcs:ignore WordPress.Security.EscapeOutput | |
| 194 | - ?>"; | |
| 195 | - var wpda_wpnonce = "<?php | |
| 196 | - echo esc_attr( $this->wpnonce ); | |
| 197 | - ?>"; | |
| 198 | - var wpda_loader_url = "<?php | |
| 199 | - echo plugins_url( '../../assets/images/loading.gif', __FILE__ ); | |
| 200 | - // phpcs:ignore WordPress.Security.EscapeOutput | |
| 201 | - ?>"; | |
| 202 | - var vqbInstalled = false; | |
| 203 | - <?php | |
| 204 | - ?> | |
| 205 | - | |
| 206 | - function getColumns(schema, table, activeIndex, callback, rebuild = false) { | |
| 207 | - const url = location.pathname + '?action=wpda_get_table_widget_info'; | |
| 208 | - const data = { | |
| 209 | - wpdaschema_name: schema, | |
| 210 | - table_name: table, | |
| 211 | - wpda_wpnonce: '<?php | |
| 212 | - echo esc_attr( wp_create_nonce( 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ); | |
| 213 | - ?>' | |
| 214 | - }; | |
| 215 | - jQuery.post( | |
| 216 | - url, | |
| 217 | - data, | |
| 218 | - function (data) { | |
| 219 | - callback(schema, table, activeIndex, JSON.parse(data), rebuild); | |
| 220 | - } | |
| 221 | - ); | |
| 222 | - } | |
| 223 | - | |
| 224 | - jQuery( | |
| 225 | - function() { | |
| 226 | - tabNew(); | |
| 227 | - jQuery('#wpda_query_builder nav').sortable(); | |
| 228 | - jQuery('.wpda_tooltip').tooltip(); | |
| 229 | - jQuery("#wpda_sql_hints").on('change', function() { | |
| 230 | - jQuery.ajax({ | |
| 231 | - method: 'POST', | |
| 232 | - url: wpda_home_url + "?action=wpda_query_builder_set_db_hints", | |
| 233 | - data: { | |
| 234 | - wpda_wpnonce: wpda_wpnonce, | |
| 235 | - wpda_sqlhints: jQuery(this).is(":checked") ? 'on' : 'off' | |
| 236 | - } | |
| 237 | - }).done( | |
| 238 | - function (msg) {} | |
| 239 | - ).fail( | |
| 240 | - function (msg) {} | |
| 241 | - ); | |
| 242 | - | |
| 243 | - }); | |
| 244 | - } | |
| 245 | - ); | |
| 246 | - </script> | |
| 247 | - <?php | |
| 248 | - } | |
| 249 | - | |
| 250 | - public function execute() { | |
| 251 | - $response = array( | |
| 252 | - 'tabs' => array(), | |
| 253 | - 'status' => null, | |
| 254 | - ); | |
| 255 | - WPDA::sent_header( 'application/json' ); | |
| 256 | - ob_start(); | |
| 257 | - // Only for admins | |
| 258 | - if ( WPDA::current_user_is_admin() && isset( | |
| 259 | - $_POST['wpda_wpnonce'], | |
| 260 | - $_POST['wpda_schemaname'], | |
| 261 | - $_POST['wpda_sqlquery'], | |
| 262 | - $_POST['wpda_sqllimit'], | |
| 263 | - $_POST['wpda_protect'] | |
| 264 | - ) ) { | |
| 265 | - $wpda_wpnonce = sanitize_text_field( wp_unslash( $_POST['wpda_wpnonce'] ) ); | |
| 266 | - // input var okay. | |
| 267 | - $wpda_schemaname = sanitize_text_field( wp_unslash( $_POST['wpda_schemaname'] ) ); | |
| 268 | - // input var okay. | |
| 269 | - $wpda_sqlquery = html_entity_decode( wp_unslash( $_POST['wpda_sqlquery'] ), ENT_QUOTES ); | |
| 270 | - // input var okay. | |
| 271 | - $wpda_sqllimit = sanitize_text_field( wp_unslash( $_POST['wpda_sqllimit'] ) ); | |
| 272 | - // input var okay. | |
| 273 | - $wpda_protect = sanitize_text_field( wp_unslash( $_POST['wpda_protect'] ) ); | |
| 274 | - // input var okay. | |
| 275 | - // Check query | |
| 276 | - if ( $this->check_query( $wpda_protect, $wpda_schemaname, $wpda_sqlquery ) ) { | |
| 277 | - if ( wp_verify_nonce( $wpda_wpnonce, 'wpda-query-builder-' . WPDA::get_current_user_id() ) ) { | |
| 278 | - // Execute query | |
| 279 | - $wpdadb = WPDADB::get_db_connection( $wpda_schemaname ); | |
| 280 | - if ( null !== $wpdadb ) { | |
| 281 | - $wpdadb->suppress_errors( true ); | |
| 282 | - } | |
| 283 | - $sqllines = explode( "\n", $wpda_sqlquery ); | |
| 284 | - //phpcs:ignore - 8.1 proof | |
| 285 | - $sqlcmds = array(); | |
| 286 | - $start_i = 0; | |
| 287 | - for ($i = 0; $i < count( $sqllines ); $i++) { | |
| 288 | - //phpcs:ignore - 8.1 proof | |
| 289 | - if ( '/' === trim( $sqllines[$i] ) ) { | |
| 290 | - $sql = ''; | |
| 291 | - for ($j = $start_i; $j < $i; $j++) { | |
| 292 | - if ( '' !== trim( $sqllines[$j] ) ) { | |
| 293 | - $sql .= "{$sqllines[$j]} "; | |
| 294 | - } | |
| 295 | - } | |
| 296 | - $sqlcmds[] = $sql; | |
| 297 | - $start_i = $i + 1; | |
| 298 | - } | |
| 299 | - } | |
| 300 | - if ( count( $sqlcmds ) > 0 ) { | |
| 301 | - //phpcs:ignore - 8.1 proof | |
| 302 | - $tabs = array(); | |
| 303 | - $vars = array(); | |
| 304 | - $tmps = array(); | |
| 305 | - // Process multiple SQL commands | |
| 306 | - for ($i = 0; $i < count( $sqlcmds ); $i++) { | |
| 307 | - //phpcs:ignore - 8.1 proof | |
| 308 | - if ( '' !== $wpda_sqllimit && 'select' === strtolower( substr( $sqlcmds[$i], 0, 6 ) ) ) { | |
| 309 | - $sqlcmds[$i] .= " limit {$wpda_sqllimit} "; | |
| 310 | - } | |
| 311 | - $columns = WPDA::get_columns_from_query( $wpda_schemaname, $sqlcmds[$i] ); | |
| 312 | - // Need to reconnect when switching from local to remote and vice versa | |
| 313 | - $reconnected = false; | |
| 314 | - if ( 'use' === substr( strtolower( trim( $sqlcmds[$i] ) ), 0, 3 ) ) { | |
| 315 | - $use_cmd = explode( ' ', trim( $sqlcmds[$i] ) ); | |
| 316 | - //phpcs:ignore - 8.1 proof | |
| 317 | - if ( 2 === count( $use_cmd ) && (strtolower( trim( $use_cmd[1] ) ) !== strtolower( trim( $wpda_schemaname ) ) && 'rdb:' === substr( strtolower( trim( $use_cmd[1] ) ), 0, 4 ) || 'rdb:' === substr( strtolower( trim( $wpda_schemaname ) ), 0, 4 )) ) { | |
| 318 | - $wpda_schemaname = $use_cmd[1]; | |
| 319 | - $wpdadb = WPDADB::get_db_connection( $wpda_schemaname ); | |
| 320 | - if ( null !== $wpdadb ) { | |
| 321 | - $wpdadb->suppress_errors( true ); | |
| 322 | - } | |
| 323 | - $reconnected = true; | |
| 324 | - } | |
| 325 | - } | |
| 326 | - $exequery = true; | |
| 327 | - $var_name = null; | |
| 328 | - $wpdadb_saved = null; | |
| 329 | - switch ( strtolower( substr( trim( $sqlcmds[$i] ), 0, 7 ) ) ) { | |
| 330 | - case 'wpdavar': | |
| 331 | - $use_cmd = explode( ' ', trim( $sqlcmds[$i] ) ); | |
| 332 | - //phpcs:ignore - 8.1 proof | |
| 333 | - $var_name = ( isset( $use_cmd[1] ) ? $use_cmd[1] : null ); | |
| 334 | - if ( $var_name !== null ) { | |
| 335 | - $vars[$var_name] = $tabs; | |
| 336 | - $tmps[$var_name] = $this->wpdavar_table( $wpdadb, $tabs[$i - 1], $var_name ); | |
| 337 | - $exequery = false; | |
| 338 | - } | |
| 339 | - break; | |
| 340 | - case 'wpdatmp': | |
| 341 | - $use_cmd = explode( ' ', trim( $sqlcmds[$i] ) ); | |
| 342 | - $var_name = ( isset( $use_cmd[1] ) ? $use_cmd[1] : null ); | |
| 343 | - if ( $var_name !== null ) { | |
| 344 | - $wpdadb_saved = $this->wpdatmp_table( | |
| 345 | - $wpdadb, | |
| 346 | - $tmps[$var_name], | |
| 347 | - $tmps[$var_name]['data'], | |
| 348 | - $tmps[$var_name]['table_name'], | |
| 349 | - $var_name | |
| 350 | - ); | |
| 351 | - $exequery = false; | |
| 352 | - } | |
| 353 | - $var_name = null; | |
| 354 | - // reset | |
| 355 | - break; | |
| 356 | - } | |
| 357 | - if ( !$reconnected && $exequery && null !== $wpdadb ) { | |
| 358 | - $wpdadb->query( $sqlcmds[$i] ); | |
| 359 | - } | |
| 360 | - if ( null === $var_name ) { | |
| 361 | - $status = ( null !== $wpdadb_saved ? $wpdadb_saved : (( null !== $wpdadb ? clone $wpdadb : null )) ); | |
| 362 | - $tabs[] = array( | |
| 363 | - 'cmd' => $sqlcmds[$i], | |
| 364 | - 'status' => $status, | |
| 365 | - 'columns' => $columns, | |
| 366 | - ); | |
| 367 | - } else { | |
| 368 | - $tabs[] = array( | |
| 369 | - 'cmd' => $sqlcmds[$i], | |
| 370 | - 'status' => ( isset( $vars[$var_name]['status'] ) ? $vars[$var_name]['status'] : null ), | |
| 371 | - 'wpdavar' => ( isset( $tmps[$var_name] ) ? $tmps[$var_name] : null ), | |
| 372 | - 'columns' => null, | |
| 373 | - ); | |
| 374 | - } | |
| 375 | - if ( null === $wpdadb || '' !== $wpdadb->last_error ) { | |
| 376 | - break; | |
| 377 | - } | |
| 378 | - } | |
| 379 | - $response['tabs'] = $tabs; | |
| 380 | - } else { | |
| 381 | - $response['columns'] = WPDA::get_columns_from_query( $wpda_schemaname, $wpda_sqlquery ); | |
| 382 | - // Process single SQL command | |
| 383 | - if ( '' !== $wpda_sqllimit && 'select' === strtolower( substr( $wpda_sqlquery, 0, 6 ) ) ) { | |
| 384 | - $wpda_sqlquery .= " limit {$wpda_sqllimit} "; | |
| 385 | - } | |
| 386 | - if ( null !== $wpdadb ) { | |
| 387 | - $wpdadb->query( $wpda_sqlquery ); | |
| 388 | - } | |
| 389 | - $response['cmd'] = $wpda_sqlquery; | |
| 390 | - $response['status'] = $wpdadb; | |
| 391 | - } | |
| 392 | - } else { | |
| 393 | - $response['status'] = '<strong>WP Data Access error:</strong> Token expired, please refresh page'; | |
| 394 | - } | |
| 395 | - } else { | |
| 396 | - $response['status'] = '<strong>WP Data Access error:</strong> Query not allowed - WordPress tables are protected'; | |
| 397 | - } | |
| 398 | - } else { | |
| 399 | - $response['status'] = '<strong>WP Data Access error:</strong> Query execution failed'; | |
| 400 | - } | |
| 401 | - ob_end_clean(); | |
| 402 | - echo json_encode( $response ); | |
| 403 | - } | |
| 404 | - | |
| 405 | - public function wpdatmp_table( | |
| 36 | + private function wpdatmp_table( | |
| 406 | 37 | $wpdadb, |
| 407 | 38 | $sql, |
| 408 | 39 | $data, |
| 409 | 40 | $table_name, |
| @@ -422,9 +53,9 @@ | ||
| 422 | 53 | } |
| 423 | 54 | return $wpdadb_created; |
| 424 | 55 | } |
| 425 | 56 | |
| 426 | - public function wpdavar_table( $wpdadb, $query, $var_name ) { | |
| 57 | + private function wpdavar_table( $wpdadb, $query, $var_name ) { | |
| 427 | 58 | $wpdadb->query( "set sql_quote_show_create = 'ON'" ); |
| 428 | 59 | if ( null === $query['status'] ) { |
| 429 | 60 | return null; |
| 430 | 61 | } |
| @@ -448,9 +79,9 @@ | ||
| 448 | 79 | } |
| 449 | 80 | |
| 450 | 81 | public function check_query( $wpda_protect, $wpda_schemaname, $wpda_sqlquery ) { |
| 451 | 82 | $sql_parts = explode( ' ', trim( $wpda_sqlquery ) ); |
| 452 | - //phpcs:ignore - 8.1 proof | |
| 83 | + // phpcs:ignore -- 8.1 proof | |
| 453 | 84 | if ( isset( $sql_parts[0] ) && isset( $sql_parts[2] ) && WPDA::is_wp_table( $sql_parts[2] ) && ('drop' === strtolower( $sql_parts[0] ) || 'alter' === strtolower( $sql_parts[0] ) || 'rename' === strtolower( $sql_parts[0] ) || 'truncate' === strtolower( $sql_parts[0] )) ) { |
| 454 | 85 | return false; |
| 455 | 86 | } |
| 456 | 87 | if ( isset( $sql_parts[0] ) && isset( $sql_parts[1] ) && WPDA::is_wp_table( $sql_parts[1] ) && 'truncate' === strtolower( $sql_parts[0] ) ) { |
| @@ -469,116 +100,16 @@ | ||
| 469 | 100 | } |
| 470 | 101 | return true; |
| 471 | 102 | } |
| 472 | 103 | |
| 473 | - public function save() { | |
| 474 | - $response = array( | |
| 475 | - 'status' => null, | |
| 476 | - ); | |
| 477 | - WPDA::sent_header( 'application/json' ); | |
| 478 | - // Only for admins | |
| 479 | - if ( WPDA::current_user_is_admin() && isset( | |
| 480 | - $_POST['wpda_wpnonce'], | |
| 481 | - $_POST['wpda_schemaname'], | |
| 482 | - $_POST['wpda_sqlqueryname'], | |
| 483 | - $_POST['wpda_sqlqueryname_old'], | |
| 484 | - $_POST['wpda_sqlquery'] | |
| 485 | - ) ) { | |
| 486 | - $wpda_wpnonce = sanitize_text_field( wp_unslash( $_POST['wpda_wpnonce'] ) ); | |
| 487 | - // input var okay. | |
| 488 | - $wpda_schemaname = sanitize_text_field( wp_unslash( $_POST['wpda_schemaname'] ) ); | |
| 489 | - // input var okay. | |
| 490 | - $wpda_sqlqueryname = sanitize_text_field( wp_unslash( $_POST['wpda_sqlqueryname'] ) ); | |
| 491 | - // input var okay. | |
| 492 | - $wpda_sqlqueryname_old = sanitize_text_field( wp_unslash( $_POST['wpda_sqlqueryname_old'] ) ); | |
| 493 | - // input var okay. | |
| 494 | - $wpda_sqlquery = sanitize_textarea_field( wp_unslash( $_POST['wpda_sqlquery'] ) ); | |
| 495 | - // input var okay. | |
| 496 | - $wpda_vqb = null; | |
| 497 | - if ( wp_verify_nonce( $wpda_wpnonce, 'wpda-query-builder-' . WPDA::get_current_user_id() ) ) { | |
| 498 | - // Save query | |
| 499 | - if ( false === $this->update_query_old( | |
| 500 | - $wpda_schemaname, | |
| 501 | - $wpda_sqlqueryname, | |
| 502 | - $wpda_sqlquery, | |
| 503 | - $wpda_sqlqueryname_old, | |
| 504 | - $wpda_vqb | |
| 505 | - ) ) { | |
| 506 | - $response['status'] = 'Could not save query'; | |
| 507 | - } else { | |
| 508 | - $response['status'] = 'Query saved'; | |
| 509 | - } | |
| 510 | - } else { | |
| 511 | - $response['status'] = 'Token expired, please refresh page'; | |
| 512 | - } | |
| 513 | - } else { | |
| 514 | - $response['status'] = 'Could not save query'; | |
| 515 | - } | |
| 516 | - echo json_encode( $response ); | |
| 517 | - } | |
| 518 | - | |
| 519 | - public function open() { | |
| 520 | - $response = array( | |
| 521 | - 'data' => array(), | |
| 522 | - 'status' => null, | |
| 523 | - ); | |
| 524 | - WPDA::sent_header( 'application/json' ); | |
| 525 | - // Only for admins | |
| 526 | - if ( WPDA::current_user_is_admin() && isset( $_POST['wpda_wpnonce'], $_POST['wpda_exclude'] ) ) { | |
| 527 | - $wpda_wpnonce = sanitize_text_field( wp_unslash( $_POST['wpda_wpnonce'] ) ); | |
| 528 | - // input var okay. | |
| 529 | - $wpda_exclude = sanitize_text_field( wp_unslash( $_POST['wpda_exclude'] ) ); | |
| 530 | - // input var okay. | |
| 531 | - if ( wp_verify_nonce( $wpda_wpnonce, 'wpda-query-builder-' . WPDA::get_current_user_id() ) ) { | |
| 532 | - // Send list of available queries | |
| 533 | - $wpda_query_builder_data = $this->get_query_list( $wpda_exclude ); | |
| 534 | - uksort( $wpda_query_builder_data, 'strnatcasecmp' ); | |
| 535 | - //phpcs:ignore - 8.1 proof | |
| 536 | - $response['data'] = $wpda_query_builder_data; | |
| 537 | - } else { | |
| 538 | - $response['status'] = 'Token expired, please refresh page'; | |
| 539 | - } | |
| 540 | - } else { | |
| 541 | - $response['status'] = 'Query list not available'; | |
| 542 | - } | |
| 543 | - echo json_encode( $response ); | |
| 544 | - } | |
| 545 | - | |
| 546 | - public function delete() { | |
| 547 | - $response = array( | |
| 548 | - 'status' => null, | |
| 549 | - ); | |
| 550 | - WPDA::sent_header( 'application/json' ); | |
| 551 | - // Only for admins | |
| 552 | - if ( WPDA::current_user_is_admin() && isset( $_POST['wpda_wpnonce'], $_POST['wpda_sqlqueryname'] ) ) { | |
| 553 | - $wpda_wpnonce = sanitize_text_field( wp_unslash( $_POST['wpda_wpnonce'] ) ); | |
| 554 | - // input var okay. | |
| 555 | - $wpda_sqlqueryname = sanitize_text_field( wp_unslash( $_POST['wpda_sqlqueryname'] ) ); | |
| 556 | - // input var okay. | |
| 557 | - if ( wp_verify_nonce( $wpda_wpnonce, 'wpda-query-builder-' . WPDA::get_current_user_id() ) ) { | |
| 558 | - // Save query | |
| 559 | - if ( false === $this->delete_query( $wpda_sqlqueryname ) ) { | |
| 560 | - $response['status'] = 'Could not delete query'; | |
| 561 | - } else { | |
| 562 | - $response['status'] = 'Query deleted'; | |
| 563 | - } | |
| 564 | - } else { | |
| 565 | - $response['status'] = 'Token expired, please refresh page'; | |
| 566 | - } | |
| 567 | - } else { | |
| 568 | - $response['status'] = 'Could not save query'; | |
| 569 | - } | |
| 570 | - echo json_encode( $response ); | |
| 571 | - } | |
| 572 | - | |
| 573 | 104 | public function get_query_list( $exclude = '', $user_id = null ) { |
| 574 | 105 | $wpda_query_builder_data = get_user_meta( ( null === $user_id ? WPDA::get_current_user_id() : $user_id ), self::QUERY_BUILDER_OPTIONS ); |
| 575 | 106 | if ( is_array( $wpda_query_builder_data ) && count( $wpda_query_builder_data ) > 0 ) { |
| 576 | - //phpcs:ignore - 8.1 proof | |
| 107 | + // phpcs:ignore -- 8.1 proof | |
| 577 | 108 | $queries = $wpda_query_builder_data[0]; |
| 578 | 109 | if ( null !== $exclude && '' !== $exclude ) { |
| 579 | 110 | $exclude_array = explode( ',', $exclude ); |
| 580 | - //phpcs:ignore - 8.1 proof | |
| 111 | + // phpcs:ignore -- 8.1 proof | |
| 581 | 112 | foreach ( $exclude_array as $exclude_item ) { |
| 582 | 113 | unset($queries[$exclude_item]); |
| 583 | 114 | } |
| 584 | 115 | } |
| @@ -590,13 +121,13 @@ | ||
| 590 | 121 | |
| 591 | 122 | public function get_query_list_global( $exclude = '' ) { |
| 592 | 123 | $wpda_query_builder_data = get_option( self::QUERY_BUILDER_OPTIONS ); |
| 593 | 124 | if ( is_array( $wpda_query_builder_data ) && count( $wpda_query_builder_data ) > 0 ) { |
| 594 | - //phpcs:ignore - 8.1 proof | |
| 125 | + // phpcs:ignore -- 8.1 proof | |
| 595 | 126 | $queries = $wpda_query_builder_data; |
| 596 | 127 | if ( null !== $exclude && '' !== $exclude ) { |
| 597 | 128 | $exclude_array = explode( ',', $exclude ); |
| 598 | - //phpcs:ignore - 8.1 proof | |
| 129 | + // phpcs:ignore -- 8.1 proof | |
| 599 | 130 | foreach ( $exclude_array as $exclude_item ) { |
| 600 | 131 | unset($queries[$exclude_item]); |
| 601 | 132 | } |
| 602 | 133 | } |
| @@ -720,11 +251,8 @@ | ||
| 720 | 251 | $this->update_query_list_global( $wpda_query_builder_data ); |
| 721 | 252 | $this->del_visual_query_global( $query_name ); |
| 722 | 253 | } |
| 723 | 254 | |
| 724 | - public function get_visual_query_ajax() { | |
| 725 | - } | |
| 726 | - | |
| 727 | 255 | public function get_visual_query( $query_name ) { |
| 728 | 256 | return get_user_meta( WPDA::get_current_user_id(), 'wpda_query_builder_' . $this->rewrite_query_name( $query_name ), true ); |
| 729 | 257 | } |
| 730 | 258 | |
| @@ -751,57 +279,8 @@ | ||
| 751 | 279 | protected function rewrite_query_name( $query_name ) { |
| 752 | 280 | return str_replace( ' ', '_', $query_name ); |
| 753 | 281 | } |
| 754 | 282 | |
| 755 | - public function get_db_hints() { | |
| 756 | - $response = array( | |
| 757 | - 'status' => 'OK', | |
| 758 | - ); | |
| 759 | - WPDA::sent_header( 'application/json' ); | |
| 760 | - if ( WPDA::current_user_is_admin() && isset( $_REQUEST['wpda_wpnonce'], $_REQUEST['wpda_schemaname'] ) ) { | |
| 761 | - $wpda_wpnonce = sanitize_text_field( wp_unslash( $_REQUEST['wpda_wpnonce'] ) ); | |
| 762 | - // input var okay. | |
| 763 | - $wpda_schemaname = sanitize_text_field( wp_unslash( $_REQUEST['wpda_schemaname'] ) ); | |
| 764 | - // input var okay. | |
| 765 | - if ( wp_verify_nonce( $wpda_wpnonce, 'wpda-query-builder-' . WPDA::get_current_user_id() ) ) { | |
| 766 | - $tables = WPDA_Dictionary_Lists::get_tables( true, $wpda_schemaname ); | |
| 767 | - $hints = array(); | |
| 768 | - foreach ( $tables as $table ) { | |
| 769 | - $columns = WPDA_Dictionary_Lists::get_table_columns( $table['table_name'], $wpda_schemaname ); | |
| 770 | - $hint_cols = array(); | |
| 771 | - foreach ( $columns as $column ) { | |
| 772 | - $hint_cols[] = $column['column_name']; | |
| 773 | - } | |
| 774 | - $hints[$table['table_name']] = $hint_cols; | |
| 775 | - } | |
| 776 | - $response['tables'] = $hints; | |
| 777 | - } else { | |
| 778 | - $response['status'] = 'Token expired, please refresh page'; | |
| 779 | - } | |
| 780 | - } else { | |
| 781 | - $response['status'] = 'Invalid arguments'; | |
| 782 | - } | |
| 783 | - echo json_encode( $response ); | |
| 784 | - } | |
| 785 | - | |
| 786 | - public function set_db_hints() { | |
| 787 | - $response = array( | |
| 788 | - 'status' => 'OK', | |
| 789 | - ); | |
| 790 | - WPDA::sent_header( 'application/json' ); | |
| 791 | - if ( WPDA::current_user_is_admin() && isset( $_REQUEST['wpda_wpnonce'], $_REQUEST['wpda_sqlhints'] ) ) { | |
| 792 | - $wpda_wpnonce = sanitize_text_field( wp_unslash( $_REQUEST['wpda_wpnonce'] ) ); | |
| 793 | - // input var okay. | |
| 794 | - $wpda_sqlhints = sanitize_text_field( wp_unslash( $_REQUEST['wpda_sqlhints'] ) ); | |
| 795 | - // input var okay. | |
| 796 | - if ( wp_verify_nonce( $wpda_wpnonce, 'wpda-query-builder-' . WPDA::get_current_user_id() ) ) { | |
| 797 | - update_option( self::QUERY_BUILDER_HINTS, $wpda_sqlhints ); | |
| 798 | - } | |
| 799 | - } | |
| 800 | - $response['hint'] = $wpda_sqlhints; | |
| 801 | - echo json_encode( $response ); | |
| 802 | - } | |
| 803 | - | |
| 804 | 283 | private function substitute_sql_params( $wpdadb, $query, $params ) { |
| 805 | 284 | } |
| 806 | 285 | |
| 807 | 286 | public function execute_query( |
| @@ -821,13 +300,13 @@ | ||
| 821 | 300 | if ( null !== $wpdadb ) { |
| 822 | 301 | $wpdadb->suppress_errors( true ); |
| 823 | 302 | } |
| 824 | 303 | $sqllines = explode( "\n", $query ); |
| 825 | - //phpcs:ignore - 8.1 proof | |
| 304 | + // phpcs:ignore -- 8.1 proof | |
| 826 | 305 | $sqlcmds = array(); |
| 827 | 306 | $start_i = 0; |
| 828 | 307 | for ($i = 0; $i < count( $sqllines ); $i++) { |
| 829 | - //phpcs:ignore - 8.1 proof | |
| 308 | + // phpcs:ignore -- 8.1 proof | |
| 830 | 309 | if ( '/' === trim( $sqllines[$i] ) ) { |
| 831 | 310 | $sql = ''; |
| 832 | 311 | for ($j = $start_i; $j < $i; $j++) { |
| 833 | 312 | if ( '' !== trim( $sqllines[$j] ) ) { |
| @@ -838,15 +317,15 @@ | ||
| 838 | 317 | $start_i = $i + 1; |
| 839 | 318 | } |
| 840 | 319 | } |
| 841 | 320 | if ( count( $sqlcmds ) > 0 ) { |
| 842 | - //phpcs:ignore - 8.1 proof | |
| 321 | + // phpcs:ignore -- 8.1 proof | |
| 843 | 322 | $tabs = array(); |
| 844 | 323 | $vars = array(); |
| 845 | 324 | $tmps = array(); |
| 846 | 325 | // Process multiple SQL commands |
| 847 | 326 | for ($i = 0; $i < count( $sqlcmds ); $i++) { |
| 848 | - //phpcs:ignore - 8.1 proof | |
| 327 | + // phpcs:ignore -- 8.1 proof | |
| 849 | 328 | if ( '' !== $limit && 'select' === strtolower( substr( $sqlcmds[$i], 0, 6 ) ) ) { |
| 850 | 329 | $sqlcmds[$i] .= " limit {$limit} "; |
| 851 | 330 | } |
| 852 | 331 | $columns = WPDA::get_columns_from_query( $dbs, $sqlcmds[$i] ); |
| @@ -853,9 +332,9 @@ | ||
| 853 | 332 | // Need to reconnect when switching from local to remote and vice versa |
| 854 | 333 | $reconnected = false; |
| 855 | 334 | if ( 'use' === substr( strtolower( trim( $sqlcmds[$i] ) ), 0, 3 ) ) { |
| 856 | 335 | $use_cmd = explode( ' ', trim( $sqlcmds[$i] ) ); |
| 857 | - //phpcs:ignore - 8.1 proof | |
| 336 | + // phpcs:ignore -- 8.1 proof | |
| 858 | 337 | if ( 2 === count( $use_cmd ) && (strtolower( trim( $use_cmd[1] ) ) !== strtolower( trim( $dbs ) ) && 'rdb:' === substr( strtolower( trim( $use_cmd[1] ) ), 0, 4 ) || 'rdb:' === substr( strtolower( trim( $dbs ) ), 0, 4 )) ) { |
| 859 | 338 | $dbs = $use_cmd[1]; |
| 860 | 339 | $wpdadb = WPDADB::get_db_connection( $dbs ); |
| 861 | 340 | if ( null !== $wpdadb ) { |
| @@ -869,9 +348,9 @@ | ||
| 869 | 348 | $wpdadb_saved = null; |
| 870 | 349 | switch ( strtolower( substr( trim( $sqlcmds[$i] ), 0, 7 ) ) ) { |
| 871 | 350 | case 'wpdavar': |
| 872 | 351 | $use_cmd = explode( ' ', trim( $sqlcmds[$i] ) ); |
| 873 | - //phpcs:ignore - 8.1 proof | |
| 352 | + // phpcs:ignore -- 8.1 proof | |
| 874 | 353 | $var_name = ( isset( $use_cmd[1] ) ? $use_cmd[1] : null ); |
| 875 | 354 | if ( $var_name !== null ) { |
| 876 | 355 | $vars[$var_name] = $tabs; |
| 877 | 356 | $tmps[$var_name] = $this->wpdavar_table( $wpdadb, $tabs[$i - 1], $var_name ); |