| @@ -1,490 +1,300 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace WPDataAccess\API { | |
| 3 | +namespace WPDataAccess\API; | |
| 4 | 4 | |
| 5 | - use WPDataAccess\Connection\WPDADB; | |
| 5 | +use WPDataAccess\Connection\WPDADB; | |
| 6 | +use WPDataAccess\WPDA; | |
| 7 | +class WPDA_Tree extends WPDA_API_Core { | |
| 8 | + public function register_rest_routes() { | |
| 9 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/dbs', array( | |
| 10 | + 'methods' => array('POST'), | |
| 11 | + 'callback' => array($this, 'tree_dbs'), | |
| 12 | + 'permission_callback' => '__return_true', | |
| 13 | + ) ); | |
| 14 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/tbl', array( | |
| 15 | + 'methods' => array('POST'), | |
| 16 | + 'callback' => array($this, 'tree_tbl'), | |
| 17 | + 'permission_callback' => '__return_true', | |
| 18 | + 'args' => array( | |
| 19 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 20 | + ), | |
| 21 | + ) ); | |
| 22 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/vws', array( | |
| 23 | + 'methods' => array('POST'), | |
| 24 | + 'callback' => array($this, 'tree_vws'), | |
| 25 | + 'permission_callback' => '__return_true', | |
| 26 | + 'args' => array( | |
| 27 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 28 | + ), | |
| 29 | + ) ); | |
| 30 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/cls', array( | |
| 31 | + 'methods' => array('POST'), | |
| 32 | + 'callback' => array($this, 'tree_cls'), | |
| 33 | + 'permission_callback' => '__return_true', | |
| 34 | + 'args' => array( | |
| 35 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 36 | + 'tbl' => $this->get_param( 'tbl' ), | |
| 37 | + ), | |
| 38 | + ) ); | |
| 39 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/idx', array( | |
| 40 | + 'methods' => array('POST'), | |
| 41 | + 'callback' => array($this, 'tree_idx'), | |
| 42 | + 'permission_callback' => '__return_true', | |
| 43 | + 'args' => array( | |
| 44 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 45 | + 'tbl' => $this->get_param( 'tbl' ), | |
| 46 | + ), | |
| 47 | + ) ); | |
| 48 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/trg', array( | |
| 49 | + 'methods' => array('POST'), | |
| 50 | + 'callback' => array($this, 'tree_trg'), | |
| 51 | + 'permission_callback' => '__return_true', | |
| 52 | + 'args' => array( | |
| 53 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 54 | + 'tbl' => $this->get_param( 'tbl' ), | |
| 55 | + ), | |
| 56 | + ) ); | |
| 57 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/frk', array( | |
| 58 | + 'methods' => array('POST'), | |
| 59 | + 'callback' => array($this, 'tree_frk'), | |
| 60 | + 'permission_callback' => '__return_true', | |
| 61 | + 'args' => array( | |
| 62 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 63 | + 'tbl' => $this->get_param( 'tbl' ), | |
| 64 | + ), | |
| 65 | + ) ); | |
| 66 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/fnc', array( | |
| 67 | + 'methods' => array('POST'), | |
| 68 | + 'callback' => array($this, 'tree_fnc'), | |
| 69 | + 'permission_callback' => '__return_true', | |
| 70 | + 'args' => array( | |
| 71 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 72 | + ), | |
| 73 | + ) ); | |
| 74 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'tree/prc', array( | |
| 75 | + 'methods' => array('POST'), | |
| 76 | + 'callback' => array($this, 'tree_prc'), | |
| 77 | + 'permission_callback' => '__return_true', | |
| 78 | + 'args' => array( | |
| 79 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 80 | + ), | |
| 81 | + ) ); | |
| 82 | + } | |
| 6 | 83 | |
| 7 | - class WPDA_Tree extends WPDA_API_Core { | |
| 84 | + public function tree_dbs( $request ) { | |
| 85 | + if ( !$this->current_user_can_access() ) { | |
| 86 | + return $this->unauthorized(); | |
| 87 | + } | |
| 88 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 89 | + return $this->invalid_nonce(); | |
| 90 | + } | |
| 91 | + return $this->get_dbs(); | |
| 92 | + } | |
| 8 | 93 | |
| 9 | - public function register_rest_routes() { | |
| 94 | + public function tree_tbl( $request ) { | |
| 95 | + if ( !$this->current_user_can_access() ) { | |
| 96 | + return $this->unauthorized(); | |
| 97 | + } | |
| 98 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 99 | + return $this->invalid_nonce(); | |
| 100 | + } | |
| 101 | + $dbs = $request->get_param( 'dbs' ); | |
| 102 | + return $this->get_tbl( $dbs ); | |
| 103 | + } | |
| 10 | 104 | |
| 11 | - register_rest_route( | |
| 12 | - WPDA_API::WPDA_NAMESPACE, | |
| 13 | - 'tree/dbs', | |
| 14 | - array( | |
| 15 | - 'methods' => array( 'POST' ), | |
| 16 | - 'callback' => array( $this, 'tree_dbs' ), | |
| 17 | - 'permission_callback' => '__return_true', | |
| 18 | - ) | |
| 19 | - ); | |
| 105 | + public function tree_vws( $request ) { | |
| 106 | + if ( !$this->current_user_can_access() ) { | |
| 107 | + return $this->unauthorized(); | |
| 108 | + } | |
| 109 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 110 | + return $this->invalid_nonce(); | |
| 111 | + } | |
| 112 | + $dbs = $request->get_param( 'dbs' ); | |
| 113 | + return $this->get_vws( $dbs ); | |
| 114 | + } | |
| 20 | 115 | |
| 21 | - register_rest_route( | |
| 22 | - WPDA_API::WPDA_NAMESPACE, | |
| 23 | - 'tree/tbl', | |
| 24 | - array( | |
| 25 | - 'methods' => array( 'POST' ), | |
| 26 | - 'callback' => array( $this, 'tree_tbl' ), | |
| 27 | - 'permission_callback' => '__return_true', | |
| 28 | - 'args' => array( | |
| 29 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 30 | - ) | |
| 31 | - ) | |
| 32 | - ); | |
| 116 | + public function tree_cls( $request ) { | |
| 117 | + if ( !$this->current_user_can_access() ) { | |
| 118 | + return $this->unauthorized(); | |
| 119 | + } | |
| 120 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 121 | + return $this->invalid_nonce(); | |
| 122 | + } | |
| 123 | + $dbs = $request->get_param( 'dbs' ); | |
| 124 | + $tbl = $request->get_param( 'tbl' ); | |
| 125 | + return $this->get_cls( $dbs, $tbl ); | |
| 126 | + } | |
| 33 | 127 | |
| 34 | - register_rest_route( | |
| 35 | - WPDA_API::WPDA_NAMESPACE, | |
| 36 | - 'tree/vws', | |
| 37 | - array( | |
| 38 | - 'methods' => array( 'POST' ), | |
| 39 | - 'callback' => array( $this, 'tree_vws' ), | |
| 40 | - 'permission_callback' => '__return_true', | |
| 41 | - 'args' => array( | |
| 42 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 43 | - ) | |
| 44 | - ) | |
| 45 | - ); | |
| 128 | + public function tree_idx( $request ) { | |
| 129 | + if ( !$this->current_user_can_access() ) { | |
| 130 | + return $this->unauthorized(); | |
| 131 | + } | |
| 132 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 133 | + return $this->invalid_nonce(); | |
| 134 | + } | |
| 135 | + $dbs = $request->get_param( 'dbs' ); | |
| 136 | + $tbl = $request->get_param( 'tbl' ); | |
| 137 | + return $this->get_idx( $dbs, $tbl ); | |
| 138 | + } | |
| 46 | 139 | |
| 47 | - register_rest_route( | |
| 48 | - WPDA_API::WPDA_NAMESPACE, | |
| 49 | - 'tree/cls', | |
| 50 | - array( | |
| 51 | - 'methods' => array( 'POST' ), | |
| 52 | - 'callback' => array( $this, 'tree_cls' ), | |
| 53 | - 'permission_callback' => '__return_true', | |
| 54 | - 'args' => array( | |
| 55 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 56 | - 'tbl' => $this->get_param( 'tbl' ), | |
| 57 | - ) | |
| 58 | - ) | |
| 59 | - ); | |
| 140 | + public function tree_trg( $request ) { | |
| 141 | + if ( !$this->current_user_can_access() ) { | |
| 142 | + return $this->unauthorized(); | |
| 143 | + } | |
| 144 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 145 | + return $this->invalid_nonce(); | |
| 146 | + } | |
| 147 | + $dbs = $request->get_param( 'dbs' ); | |
| 148 | + $tbl = $request->get_param( 'tbl' ); | |
| 149 | + return $this->get_trg( $dbs, $tbl ); | |
| 150 | + } | |
| 60 | 151 | |
| 61 | - register_rest_route( | |
| 62 | - WPDA_API::WPDA_NAMESPACE, | |
| 63 | - 'tree/idx', | |
| 64 | - array( | |
| 65 | - 'methods' => array( 'POST' ), | |
| 66 | - 'callback' => array( $this, 'tree_idx' ), | |
| 67 | - 'permission_callback' => '__return_true', | |
| 68 | - 'args' => array( | |
| 69 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 70 | - 'tbl' => $this->get_param( 'tbl' ), | |
| 71 | - ) | |
| 72 | - ) | |
| 73 | - ); | |
| 152 | + public function tree_frk( $request ) { | |
| 153 | + if ( !$this->current_user_can_access() ) { | |
| 154 | + return $this->unauthorized(); | |
| 155 | + } | |
| 156 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 157 | + return $this->invalid_nonce(); | |
| 158 | + } | |
| 159 | + $dbs = $request->get_param( 'dbs' ); | |
| 160 | + $tbl = $request->get_param( 'tbl' ); | |
| 161 | + return $this->get_frk( $dbs, $tbl ); | |
| 162 | + } | |
| 74 | 163 | |
| 75 | - register_rest_route( | |
| 76 | - WPDA_API::WPDA_NAMESPACE, | |
| 77 | - 'tree/trg', | |
| 78 | - array( | |
| 79 | - 'methods' => array( 'POST' ), | |
| 80 | - 'callback' => array( $this, 'tree_trg' ), | |
| 81 | - 'permission_callback' => '__return_true', | |
| 82 | - 'args' => array( | |
| 83 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 84 | - 'tbl' => $this->get_param( 'tbl' ), | |
| 85 | - ) | |
| 86 | - ) | |
| 87 | - ); | |
| 164 | + public function tree_fnc( $request ) { | |
| 165 | + if ( !$this->current_user_can_access() ) { | |
| 166 | + return $this->unauthorized(); | |
| 167 | + } | |
| 168 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 169 | + return $this->invalid_nonce(); | |
| 170 | + } | |
| 171 | + $dbs = $request->get_param( 'dbs' ); | |
| 172 | + return $this->get_fnc( $dbs ); | |
| 173 | + } | |
| 88 | 174 | |
| 89 | - register_rest_route( | |
| 90 | - WPDA_API::WPDA_NAMESPACE, | |
| 91 | - 'tree/frk', | |
| 92 | - array( | |
| 93 | - 'methods' => array( 'POST' ), | |
| 94 | - 'callback' => array( $this, 'tree_frk' ), | |
| 95 | - 'permission_callback' => '__return_true', | |
| 96 | - 'args' => array( | |
| 97 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 98 | - 'tbl' => $this->get_param( 'tbl' ), | |
| 99 | - ) | |
| 100 | - ) | |
| 101 | - ); | |
| 175 | + public function tree_prc( $request ) { | |
| 176 | + if ( !$this->current_user_can_access() ) { | |
| 177 | + return $this->unauthorized(); | |
| 178 | + } | |
| 179 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 180 | + return $this->invalid_nonce(); | |
| 181 | + } | |
| 182 | + $dbs = $request->get_param( 'dbs' ); | |
| 183 | + return $this->get_prc( $dbs ); | |
| 184 | + } | |
| 102 | 185 | |
| 103 | - register_rest_route( | |
| 104 | - WPDA_API::WPDA_NAMESPACE, | |
| 105 | - 'tree/fnc', | |
| 106 | - array( | |
| 107 | - 'methods' => array( 'POST' ), | |
| 108 | - 'callback' => array( $this, 'tree_fnc' ), | |
| 109 | - 'permission_callback' => '__return_true', | |
| 110 | - 'args' => array( | |
| 111 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 112 | - ) | |
| 113 | - ) | |
| 114 | - ); | |
| 186 | + public function get_dbs( $include_disabled = false ) { | |
| 187 | + // Local databases | |
| 188 | + global $wpdb; | |
| 189 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 190 | + $local = $wpdb->get_results( "\n\t\t\t\t\tSELECT schema_name AS dbs,\n\t\t\t\t\t\t\t'local' AS dbs_type,\n\t\t\t\t\t\t\t'false' AS pds\n\t\t\t\t\t FROM information_schema.schemata\n\t\t\t\t\t ORDER BY schema_name\n\t\t\t \t", 'ARRAY_A' ); | |
| 191 | + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 192 | + for ($i = 0; $i < count( $local ); $i++) { | |
| 193 | + if ( $wpdb->dbname === $local[$i]['dbs'] ) { | |
| 194 | + // WordPress database | |
| 195 | + $local[$i]['dbs_type'] = 'wp'; | |
| 196 | + } | |
| 197 | + // System databases | |
| 198 | + if ( 'information_schema' === $local[$i]['dbs'] || 'mysql' === $local[$i]['dbs'] || 'performance_schema' === $local[$i]['dbs'] || 'sys' === $local[$i]['dbs'] ) { | |
| 199 | + // WordPress database | |
| 200 | + $local[$i]['dbs_type'] = 'system'; | |
| 201 | + } | |
| 202 | + $local[$i]['pds'] = false; | |
| 203 | + } | |
| 204 | + // Remote databases | |
| 205 | + $remote = array(); | |
| 206 | + $pds = array(); | |
| 207 | + $rdb = WPDADB::get_remote_databases( $include_disabled ); | |
| 208 | + foreach ( $rdb as $key => $val ) { | |
| 209 | + $remote[] = array( | |
| 210 | + 'dbs' => $key, | |
| 211 | + 'dbs_type' => 'remote', | |
| 212 | + 'pds' => isset( $pds[substr( $key, 4 )] ), | |
| 213 | + ); | |
| 214 | + } | |
| 215 | + global $wpdb; | |
| 216 | + $context = array( | |
| 217 | + 'env' => $this->get_env(), | |
| 218 | + 'wp' => [ | |
| 219 | + 'roles' => $this->get_wp_roles(), | |
| 220 | + 'users' => $this->get_wp_users(), | |
| 221 | + 'home' => admin_url( 'admin.php' ), | |
| 222 | + 'tables' => array_values( $wpdb->tables() ), | |
| 223 | + 'zip' => class_exists( '\\ZipArchive' ), | |
| 224 | + 'upload' => @ini_get( 'upload_max_filesize' ), | |
| 225 | + ], | |
| 226 | + ); | |
| 227 | + if ( 'anonymous' !== WPDA::get_current_user_login() ) { | |
| 228 | + $context['wp']['nonce'] = implode( '-', array(wp_create_nonce( 'wpda-export-' . WPDA::get_current_user_login() )) ); | |
| 229 | + } | |
| 230 | + return $this->WPDA_Rest_Response( | |
| 231 | + '', | |
| 232 | + array_merge( $local, $remote ), | |
| 233 | + // phpcs:ignore -- 8.1 proof | |
| 234 | + $context | |
| 235 | + ); | |
| 236 | + } | |
| 115 | 237 | |
| 116 | - register_rest_route( | |
| 117 | - WPDA_API::WPDA_NAMESPACE, | |
| 118 | - 'tree/prc', | |
| 119 | - array( | |
| 120 | - 'methods' => array( 'POST' ), | |
| 121 | - 'callback' => array( $this, 'tree_prc' ), | |
| 122 | - 'permission_callback' => '__return_true', | |
| 123 | - 'args' => array( | |
| 124 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 125 | - ) | |
| 126 | - ) | |
| 127 | - ); | |
| 238 | + public function get_tbl_vws( $dbs ) { | |
| 239 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 240 | + if ( $wpdadb === null ) { | |
| 241 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 242 | + } | |
| 243 | + $query = $wpdadb->prepare( "\n\t\t\t\t\tselect table_name as table_name\n\t\t\t\t\t from information_schema.tables\n\t\t\t\t\t where table_schema = %s\n\t\t\t\t\t order by table_name\n\t\t\t\t", array($wpdadb->dbname) ); | |
| 244 | + return $this->WPDA_Rest_Response( '', array_column( $wpdadb->get_results( $query, 'ARRAY_A' ), 'table_name' ) ); | |
| 245 | + } | |
| 128 | 246 | |
| 129 | - } | |
| 247 | + private function get_tbl( $dbs ) { | |
| 248 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 249 | + if ( $wpdadb === null ) { | |
| 250 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 251 | + } | |
| 252 | + $query = $wpdadb->prepare( "\n\t\t\t\t\tselect table_name as table_name\n\t\t\t\t\t from information_schema.tables\n\t\t\t\t\t where table_schema = %s\n\t\t\t\t\t and table_type not in ('VIEW', 'SYSTEM VIEW')\n\t\t\t\t\t order by table_name\n\t\t\t\t", array($wpdadb->dbname) ); | |
| 253 | + $context = null; | |
| 254 | + global $wpdb; | |
| 255 | + if ( $wpdadb->dbname === $wpdb->dbname ) { | |
| 256 | + $context = array( | |
| 257 | + 'wp_tables' => array_keys( WPDA::get_wp_tables() ), | |
| 258 | + 'wpda_tables' => WPDA::get_wpda_tables(), | |
| 259 | + ); | |
| 260 | + } | |
| 261 | + return $this->WPDA_Rest_Response( '', array_column( $wpdadb->get_results( $query, 'ARRAY_A' ), 'table_name' ), $context ); | |
| 262 | + } | |
| 130 | 263 | |
| 131 | - public function tree_dbs( $request ) { | |
| 132 | - if ( ! $this->current_user_can_access() ) { | |
| 133 | - return $this->unauthorized(); | |
| 134 | - } | |
| 264 | + private function get_vws( $dbs ) { | |
| 265 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 266 | + if ( $wpdadb === null ) { | |
| 267 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 268 | + } | |
| 269 | + $query = $wpdadb->prepare( "\n\t\t\t\t\tselect table_name as view_name\n\t\t\t\t\t from information_schema.tables\n\t\t\t\t\t where table_schema = %s\n\t\t\t\t\t and table_type in ('VIEW', 'SYSTEM VIEW')\n\t\t\t\t\t order by table_name\n\t\t\t\t", array($wpdadb->dbname) ); | |
| 270 | + return $this->WPDA_Rest_Response( '', array_column( $wpdadb->get_results( $query, 'ARRAY_A' ), 'view_name' ) ); | |
| 271 | + } | |
| 135 | 272 | |
| 136 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 137 | - return $this->invalid_nonce(); | |
| 138 | - } | |
| 273 | + public function get_cls( $dbs, $tbl ) { | |
| 274 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 275 | + if ( $wpdadb === null ) { | |
| 276 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 277 | + } | |
| 278 | + $query = $wpdadb->prepare( "\n\t SELECT column_name AS column_name,\n\t extra AS extra,\n\t column_type AS column_type,\n\t is_nullable AS is_nullable,\n\t IF(LEFT(column_default,1)='\\'' AND RIGHT(column_default,1)='\\'', SUBSTR(column_default,2,LENGTH(column_default)-2), column_default) AS column_default,\n\t \t\t character_maximum_length AS character_maximum_length,\n\t numeric_scale AS numeric_scale,\n\t numeric_precision AS numeric_precision,\n\t ordinal_position AS ordinal_position\n\t FROM information_schema.columns\n\t WHERE table_schema = %s\n\t AND table_name = %s\n\t ORDER BY ordinal_position\n\t ", array($wpdadb->dbname, $tbl) ); | |
| 279 | + return $this->WPDA_Rest_Response( '', $wpdadb->get_results( $query, 'ARRAY_A' ) ); | |
| 280 | + } | |
| 139 | 281 | |
| 140 | - return $this->get_dbs(); | |
| 141 | - } | |
| 282 | + private function get_idx( $dbs, $tbl ) { | |
| 283 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 284 | + if ( $wpdadb === null ) { | |
| 285 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 286 | + } | |
| 287 | + $query = $wpdadb->prepare( "\n\t\t\t\t\tSELECT index_name AS index_name,\n\t\t\t\t\t\t IF(non_unique=0, 'YES', 'NO') AS non_unique,\n\t\t\t\t\t\t seq_in_index AS seq_in_index,\n\t\t\t\t\t\t column_name AS column_name,\n\t\t\t\t\t\t collation AS collation,\n\t\t\t\t\t\t nullable AS nullable,\n\t\t\t\t\t\t index_type AS index_type,\n\t\t\t\t\t\t cardinality AS cardinality\n\t\t\t\t\tFROM information_schema.statistics\n\t\t\t\t\tWHERE table_schema = %s\n\t\t\t\t\t AND table_name = %s\n\t\t\t\t\tORDER BY index_name, seq_in_index\n\t\t\t\t", array($wpdadb->dbname, $tbl) ); | |
| 288 | + return $this->WPDA_Rest_Response( '', $wpdadb->get_results( $query, 'ARRAY_A' ) ); | |
| 289 | + } | |
| 142 | 290 | |
| 143 | - public function tree_tbl( $request ) { | |
| 144 | - if ( ! $this->current_user_can_access() ) { | |
| 145 | - return $this->unauthorized(); | |
| 146 | - } | |
| 147 | - | |
| 148 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 149 | - return $this->invalid_nonce(); | |
| 150 | - } | |
| 151 | - | |
| 152 | - $dbs = $request->get_param( 'dbs' ); | |
| 153 | - | |
| 154 | - return $this->get_tbl( $dbs ); | |
| 155 | - } | |
| 156 | - | |
| 157 | - public function tree_vws( $request ) { | |
| 158 | - if ( ! $this->current_user_can_access() ) { | |
| 159 | - return $this->unauthorized(); | |
| 160 | - } | |
| 161 | - | |
| 162 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 163 | - return $this->invalid_nonce(); | |
| 164 | - } | |
| 165 | - | |
| 166 | - $dbs = $request->get_param( 'dbs' ); | |
| 167 | - | |
| 168 | - return $this->get_vws( $dbs ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - public function tree_cls( $request ) { | |
| 172 | - if ( ! $this->current_user_can_access() ) { | |
| 173 | - return $this->unauthorized(); | |
| 174 | - } | |
| 175 | - | |
| 176 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 177 | - return $this->invalid_nonce(); | |
| 178 | - } | |
| 179 | - | |
| 180 | - $dbs = $request->get_param( 'dbs' ); | |
| 181 | - $tbl = $request->get_param( 'tbl' ); | |
| 182 | - | |
| 183 | - return $this->get_cls( $dbs, $tbl ); | |
| 184 | - } | |
| 185 | - | |
| 186 | - public function tree_idx( $request ) { | |
| 187 | - if ( ! $this->current_user_can_access() ) { | |
| 188 | - return $this->unauthorized(); | |
| 189 | - } | |
| 190 | - | |
| 191 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 192 | - return $this->invalid_nonce(); | |
| 193 | - } | |
| 194 | - | |
| 195 | - $dbs = $request->get_param( 'dbs' ); | |
| 196 | - $tbl = $request->get_param( 'tbl' ); | |
| 197 | - | |
| 198 | - return $this->get_idx( $dbs, $tbl ); | |
| 199 | - } | |
| 200 | - | |
| 201 | - public function tree_trg( $request ) { | |
| 202 | - if ( ! $this->current_user_can_access() ) { | |
| 203 | - return $this->unauthorized(); | |
| 204 | - } | |
| 205 | - | |
| 206 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 207 | - return $this->invalid_nonce(); | |
| 208 | - } | |
| 209 | - | |
| 210 | - $dbs = $request->get_param( 'dbs' ); | |
| 211 | - $tbl = $request->get_param( 'tbl' ); | |
| 212 | - | |
| 213 | - return $this->get_trg( $dbs, $tbl ); | |
| 214 | - } | |
| 215 | - | |
| 216 | - public function tree_frk( $request ) { | |
| 217 | - if ( ! $this->current_user_can_access() ) { | |
| 218 | - return $this->unauthorized(); | |
| 219 | - } | |
| 220 | - | |
| 221 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 222 | - return $this->invalid_nonce(); | |
| 223 | - } | |
| 224 | - | |
| 225 | - $dbs = $request->get_param( 'dbs' ); | |
| 226 | - $tbl = $request->get_param( 'tbl' ); | |
| 227 | - | |
| 228 | - return $this->get_frk( $dbs, $tbl ); | |
| 229 | - } | |
| 230 | - | |
| 231 | - public function tree_fnc( $request ) { | |
| 232 | - if ( ! $this->current_user_can_access() ) { | |
| 233 | - return $this->unauthorized(); | |
| 234 | - } | |
| 235 | - | |
| 236 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 237 | - return $this->invalid_nonce(); | |
| 238 | - } | |
| 239 | - | |
| 240 | - $dbs = $request->get_param( 'dbs' ); | |
| 241 | - | |
| 242 | - return $this->get_fnc( $dbs ); | |
| 243 | - } | |
| 244 | - | |
| 245 | - public function tree_prc( $request ) { | |
| 246 | - if ( ! $this->current_user_can_access() ) { | |
| 247 | - return $this->unauthorized(); | |
| 248 | - } | |
| 249 | - | |
| 250 | - if ( ! $this->current_user_token_valid( $request ) ) { | |
| 251 | - return $this->invalid_nonce(); | |
| 252 | - } | |
| 253 | - | |
| 254 | - $dbs = $request->get_param( 'dbs' ); | |
| 255 | - | |
| 256 | - return $this->get_prc( $dbs ); | |
| 257 | - } | |
| 258 | - | |
| 259 | - public function get_dbs() { | |
| 260 | - // Local databases | |
| 261 | - global $wpdb; | |
| 262 | - $local = $wpdb->get_results(" | |
| 263 | - SELECT schema_name AS dbs, | |
| 264 | - 'local' AS dbs_type | |
| 265 | - FROM information_schema.schemata | |
| 266 | - ORDER BY schema_name | |
| 267 | - ", | |
| 268 | - 'ARRAY_A' | |
| 269 | - ); | |
| 270 | - | |
| 271 | - for ( $i=0; $i < count( $local ); $i++ ) { | |
| 272 | - if ( $wpdb->dbname === $local[ $i ]['dbs'] ) { | |
| 273 | - // WordPress database | |
| 274 | - $local[ $i ]['dbs_type'] = 'wp'; | |
| 275 | - } | |
| 276 | - // System databases | |
| 277 | - if ( | |
| 278 | - 'information_schema' === $local[ $i ]['dbs'] || | |
| 279 | - 'mysql' === $local[ $i ]['dbs'] || | |
| 280 | - 'performance_schema' === $local[ $i ]['dbs'] || | |
| 281 | - 'sys' === $local[ $i ]['dbs'] | |
| 282 | - ) { | |
| 283 | - // WordPress database | |
| 284 | - $local[ $i ]['dbs_type'] = 'system'; | |
| 285 | - } | |
| 286 | - } | |
| 287 | - | |
| 288 | - // Remote databases | |
| 289 | - $remote = array(); | |
| 290 | - $rdb = WPDADB::get_remote_databases(); | |
| 291 | - foreach ( $rdb as $key => $val ) { | |
| 292 | - $remote[] = array( | |
| 293 | - 'dbs' => $key, | |
| 294 | - 'dbs_type' => 'remote', | |
| 295 | - ); | |
| 296 | - } | |
| 297 | - | |
| 298 | - return $this->WPDA_Rest_Response( | |
| 299 | - '', | |
| 300 | - array_merge( $local, $remote ) //phpcs:ignore - 8.1 proof | |
| 301 | - ); | |
| 302 | - } | |
| 303 | - | |
| 304 | - public function get_tbl_vws( $dbs ) { | |
| 305 | - | |
| 306 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 307 | - if ( $wpdadb === null ) { | |
| 308 | - return $this->WPDA_Rest_Response( | |
| 309 | - '', | |
| 310 | - array() | |
| 311 | - ); | |
| 312 | - } | |
| 313 | - | |
| 314 | - $query = $wpdadb->prepare( | |
| 315 | - " | |
| 316 | - select table_name as table_name | |
| 317 | - from information_schema.tables | |
| 318 | - where table_schema = %s | |
| 319 | - order by table_name | |
| 320 | - ", | |
| 321 | - array( | |
| 322 | - $wpdadb->dbname, | |
| 323 | - ) | |
| 324 | - ); | |
| 325 | - | |
| 326 | - return $this->WPDA_Rest_Response( | |
| 327 | - '', | |
| 328 | - array_column( | |
| 329 | - $wpdadb->get_results( $query, 'ARRAY_A' ), // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 330 | - 'table_name' | |
| 331 | - ) | |
| 332 | - ); | |
| 333 | - | |
| 334 | - } | |
| 335 | - | |
| 336 | - private function get_tbl( $dbs ) { | |
| 337 | - | |
| 338 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 339 | - if ( $wpdadb === null ) { | |
| 340 | - return $this->WPDA_Rest_Response( | |
| 341 | - '', | |
| 342 | - array() | |
| 343 | - ); | |
| 344 | - } | |
| 345 | - | |
| 346 | - $query = $wpdadb->prepare( | |
| 347 | - " | |
| 348 | - select table_name as table_name | |
| 349 | - from information_schema.tables | |
| 350 | - where table_schema = %s | |
| 351 | - and table_type not in ('VIEW', 'SYSTEM VIEW') | |
| 352 | - order by table_name | |
| 353 | - ", | |
| 354 | - array( | |
| 355 | - $wpdadb->dbname, | |
| 356 | - ) | |
| 357 | - ); | |
| 358 | - | |
| 359 | - return $this->WPDA_Rest_Response( | |
| 360 | - '', | |
| 361 | - array_column( | |
| 362 | - $wpdadb->get_results( $query, 'ARRAY_A' ), // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 363 | - 'table_name' | |
| 364 | - ) | |
| 365 | - ); | |
| 366 | - | |
| 367 | - } | |
| 368 | - | |
| 369 | - private function get_vws( $dbs ) { | |
| 370 | - | |
| 371 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 372 | - if ( $wpdadb === null ) { | |
| 373 | - return $this->WPDA_Rest_Response( | |
| 374 | - '', | |
| 375 | - array() | |
| 376 | - ); | |
| 377 | - } | |
| 378 | - | |
| 379 | - $query = $wpdadb->prepare(" | |
| 380 | - select table_name as view_name | |
| 381 | - from information_schema.tables | |
| 382 | - where table_schema = %s | |
| 383 | - and table_type in ('VIEW', 'SYSTEM VIEW') | |
| 384 | - order by table_name | |
| 385 | - ", | |
| 386 | - array( | |
| 387 | - $wpdadb->dbname, | |
| 388 | - ) | |
| 389 | - ); | |
| 390 | - | |
| 391 | - return $this->WPDA_Rest_Response( | |
| 392 | - '', | |
| 393 | - array_column( | |
| 394 | - $wpdadb->get_results( $query, 'ARRAY_A' ), // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 395 | - 'view_name' | |
| 396 | - ) | |
| 397 | - ); | |
| 398 | - | |
| 399 | - } | |
| 400 | - | |
| 401 | - public function get_cls( $dbs, $tbl ) { | |
| 402 | - | |
| 403 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 404 | - if ( $wpdadb === null ) { | |
| 405 | - return $this->WPDA_Rest_Response( | |
| 406 | - '', | |
| 407 | - array() | |
| 408 | - ); | |
| 409 | - } | |
| 410 | - | |
| 411 | - $query = $wpdadb->prepare(" | |
| 412 | - SELECT column_name AS column_name, | |
| 413 | - extra AS extra, | |
| 414 | - column_type AS column_type, | |
| 415 | - is_nullable AS is_nullable, | |
| 416 | - IF(LEFT(column_default,1)='\'' AND RIGHT(column_default,1)='\'', SUBSTR(column_default,2,LENGTH(column_default)-2), column_default) AS column_default, | |
| 417 | - character_maximum_length AS character_maximum_length, | |
| 418 | - numeric_scale AS numeric_scale, | |
| 419 | - numeric_precision AS numeric_precision, | |
| 420 | - ordinal_position AS ordinal_position | |
| 421 | - FROM information_schema.columns | |
| 422 | - WHERE table_schema = %s | |
| 423 | - AND table_name = %s | |
| 424 | - ORDER BY ordinal_position | |
| 425 | - ", | |
| 426 | - array( | |
| 427 | - $wpdadb->dbname, | |
| 428 | - $tbl, | |
| 429 | - ) | |
| 430 | - ); | |
| 431 | - | |
| 432 | - return $this->WPDA_Rest_Response( | |
| 433 | - '', | |
| 434 | - $wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 435 | - ); | |
| 436 | - | |
| 437 | - } | |
| 438 | - | |
| 439 | - private function get_idx( $dbs, $tbl ) { | |
| 440 | - | |
| 441 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 442 | - if ( $wpdadb === null ) { | |
| 443 | - return $this->WPDA_Rest_Response( | |
| 444 | - '', | |
| 445 | - array() | |
| 446 | - ); | |
| 447 | - } | |
| 448 | - | |
| 449 | - $query = $wpdadb->prepare(" | |
| 450 | - SELECT index_name AS index_name, | |
| 451 | - IF(non_unique=0, 'YES', 'NO') AS non_unique, | |
| 452 | - seq_in_index AS seq_in_index, | |
| 453 | - column_name AS column_name, | |
| 454 | - collation AS collation, | |
| 455 | - nullable AS nullable, | |
| 456 | - index_type AS index_type, | |
| 457 | - cardinality AS cardinality | |
| 458 | - FROM information_schema.statistics | |
| 459 | - WHERE table_schema = %s | |
| 460 | - AND table_name = %s | |
| 461 | - ORDER BY index_name, seq_in_index | |
| 462 | - ", | |
| 463 | - array( | |
| 464 | - $wpdadb->dbname, | |
| 465 | - $tbl, | |
| 466 | - ) | |
| 467 | - ); | |
| 468 | - | |
| 469 | - return $this->WPDA_Rest_Response( | |
| 470 | - '', | |
| 471 | - $wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 472 | - ); | |
| 473 | - | |
| 474 | - } | |
| 475 | - | |
| 476 | - private function get_trg( $dbs, $tbl ) { | |
| 477 | - | |
| 478 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 479 | - if ( $wpdadb === null ) { | |
| 480 | - return $this->WPDA_Rest_Response( | |
| 481 | - '', | |
| 482 | - array() | |
| 483 | - ); | |
| 484 | - } | |
| 485 | - | |
| 486 | - $query = $wpdadb->prepare(' | |
| 291 | + private function get_trg( $dbs, $tbl ) { | |
| 292 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 293 | + if ( $wpdadb === null ) { | |
| 294 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 295 | + } | |
| 296 | + $query = $wpdadb->prepare( ' | |
| 487 | 297 | SELECT trigger_name AS trigger_name, |
| 488 | 298 | event_manipulation AS event_manipulation, |
| 489 | 299 | action_timing AS action_timing, |
| 490 | 300 | action_statement AS action_statement |
| @@ -491,33 +301,18 @@ | ||
| 491 | 301 | FROM information_schema.triggers |
| 492 | 302 | WHERE event_object_schema = %s |
| 493 | 303 | AND event_object_table = %s |
| 494 | 304 | ORDER BY trigger_name |
| 495 | - ', | |
| 496 | - array( | |
| 497 | - $wpdadb->dbname, | |
| 498 | - $tbl, | |
| 499 | - ) | |
| 500 | - ); | |
| 305 | + ', array($wpdadb->dbname, $tbl) ); | |
| 306 | + return $this->WPDA_Rest_Response( '', $wpdadb->get_results( $query, 'ARRAY_A' ) ); | |
| 307 | + } | |
| 501 | 308 | |
| 502 | - return $this->WPDA_Rest_Response( | |
| 503 | - '', | |
| 504 | - $wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 505 | - ); | |
| 506 | - | |
| 507 | - } | |
| 508 | - | |
| 509 | - private function get_frk( $dbs, $tbl ) { | |
| 510 | - | |
| 511 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 512 | - if ( $wpdadb === null ) { | |
| 513 | - return $this->WPDA_Rest_Response( | |
| 514 | - '', | |
| 515 | - array() | |
| 516 | - ); | |
| 517 | - } | |
| 518 | - | |
| 519 | - $query = $wpdadb->prepare(' | |
| 309 | + private function get_frk( $dbs, $tbl ) { | |
| 310 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 311 | + if ( $wpdadb === null ) { | |
| 312 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 313 | + } | |
| 314 | + $query = $wpdadb->prepare( ' | |
| 520 | 315 | select constraint_name AS constraint_name, |
| 521 | 316 | column_name AS column_name, |
| 522 | 317 | referenced_table_name AS referenced_table_name, |
| 523 | 318 | referenced_column_name AS referenced_column_name |
| @@ -525,61 +320,32 @@ | ||
| 525 | 320 | where table_schema = %s |
| 526 | 321 | and table_name = %s |
| 527 | 322 | and referenced_table_name is not null |
| 528 | 323 | order by ordinal_position |
| 529 | - ', | |
| 530 | - array( | |
| 531 | - $wpdadb->dbname, | |
| 532 | - $tbl, | |
| 533 | - ) | |
| 534 | - ); | |
| 324 | + ', array($wpdadb->dbname, $tbl) ); | |
| 325 | + return $this->WPDA_Rest_Response( '', $wpdadb->get_results( $query, 'ARRAY_A' ) ); | |
| 326 | + } | |
| 535 | 327 | |
| 536 | - return $this->WPDA_Rest_Response( | |
| 537 | - '', | |
| 538 | - $wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 539 | - ); | |
| 328 | + private function get_prc( $dbs ) { | |
| 329 | + return $this->get_rtn( $dbs, 'PROCEDURE' ); | |
| 330 | + } | |
| 540 | 331 | |
| 541 | - } | |
| 332 | + private function get_fnc( $dbs ) { | |
| 333 | + return $this->get_rtn( $dbs, 'FUNCTION' ); | |
| 334 | + } | |
| 542 | 335 | |
| 543 | - private function get_prc( $dbs ) { | |
| 544 | - | |
| 545 | - return $this->get_rtn( $dbs, 'PROCEDURE' ); | |
| 546 | - } | |
| 547 | - | |
| 548 | - private function get_fnc( $dbs ) { | |
| 549 | - | |
| 550 | - return $this->get_rtn( $dbs, 'FUNCTION' ); | |
| 551 | - } | |
| 552 | - | |
| 553 | - private function get_rtn( $dbs, $typ ) { | |
| 554 | - | |
| 555 | - $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 556 | - if ( $wpdadb === null ) { | |
| 557 | - return $this->WPDA_Rest_Response( | |
| 558 | - '', | |
| 559 | - array() | |
| 560 | - ); | |
| 561 | - } | |
| 562 | - | |
| 563 | - $query = $wpdadb->prepare(' | |
| 336 | + private function get_rtn( $dbs, $typ ) { | |
| 337 | + $wpdadb = WPDADB::get_db_connection( $dbs ); | |
| 338 | + if ( $wpdadb === null ) { | |
| 339 | + return $this->WPDA_Rest_Response( '', array() ); | |
| 340 | + } | |
| 341 | + $query = $wpdadb->prepare( ' | |
| 564 | 342 | SELECT routine_name AS routine_name, |
| 565 | 343 | routine_definition AS routine_definition |
| 566 | 344 | FROM information_schema.routines |
| 567 | 345 | WHERE routine_schema = %s |
| 568 | 346 | AND routine_type = %s |
| 569 | - ', | |
| 570 | - array( | |
| 571 | - $wpdadb->dbname, | |
| 572 | - $typ, | |
| 573 | - ) | |
| 574 | - ); | |
| 347 | + ', array($wpdadb->dbname, $typ) ); | |
| 348 | + return $this->WPDA_Rest_Response( '', $wpdadb->get_results( $query, 'ARRAY_A' ) ); | |
| 349 | + } | |
| 575 | 350 | |
| 576 | - return $this->WPDA_Rest_Response( | |
| 577 | - '', | |
| 578 | - $wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode | |
| 579 | - ); | |
| 580 | - | |
| 581 | - } | |
| 582 | - | |
| 583 | - } | |
| 584 | - | |
| 585 | -} | |
| 351 | +} | |