| @@ -59,31 +59,14 @@ | ||
| 59 | 59 | * @param int $chart_id The chart id. |
| 60 | 60 | * @param array $params Any additional parameters (e.g. for connecting to a remote db). |
| 61 | 61 | */ |
| 62 | 62 | public function __construct( $query = null, $chart_id = null, $params = null ) { |
| 63 | - $this->_query = $this->strip_sql_comments( $query ); | |
| 63 | + $this->_query = $query; | |
| 64 | 64 | $this->_chart_id = $chart_id; |
| 65 | 65 | $this->_params = $params; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | - * Strips SQL comments from the query. | |
| 70 | - * | |
| 71 | - * @param string $query The query. | |
| 72 | - * | |
| 73 | - * @return string | |
| 74 | - */ | |
| 75 | - private function strip_sql_comments( $query = '' ) { | |
| 76 | - if ( empty( $query ) ) { | |
| 77 | - return $query; | |
| 78 | - } | |
| 79 | - | |
| 80 | - // Regex https://regex101.com/r/xd5Vrg/1 | |
| 81 | - $sql_comments_regex = '@(--[^\r\n]*)|(\#[^\r\n]*)|(/\*[\w\W]*?(?=\*/)\*/)@ms'; | |
| 82 | - return trim( preg_replace( $sql_comments_regex, '', $query ) ); | |
| 83 | - } | |
| 84 | - | |
| 85 | - /** | |
| 86 | 69 | * Fetches information from source, parses it and builds series and data arrays. |
| 87 | 70 | * |
| 88 | 71 | * @param bool $as_html Should the result be fetched as an HTML table or as an object. |
| 89 | 72 | * @param bool $results_as_numeric_array Should the result be fetched as ARRAY_N instead of ARRAY_A. |
| @@ -95,10 +78,10 @@ | ||
| 95 | 78 | if ( empty( $this->_query ) ) { |
| 96 | 79 | return false; |
| 97 | 80 | } |
| 98 | 81 | |
| 99 | - // only select queries allowed. must start with SELECT keyword. | |
| 100 | - if ( ! preg_match( '/^(\bselect\b)\s/i', $this->_query ) ) { | |
| 82 | + // only select queries allowed. | |
| 83 | + if ( ! preg_match( '/\s*(\bselect\b)\s/i', $this->_query ) ) { | |
| 101 | 84 | $this->_error = __( 'Only SELECT queries are allowed', 'visualizer' ); |
| 102 | 85 | return false; |
| 103 | 86 | } |
| 104 | 87 | |
| @@ -103,19 +86,16 @@ | ||
| 103 | 86 | } |
| 104 | 87 | |
| 105 | 88 | // if previous check passed, check for disallowed query parts to prevent subqueries and other harmful queries. |
| 106 | 89 | $disallow_query_parts = array( |
| 107 | - 'CREATE', | |
| 108 | - 'ALTER', | |
| 109 | - 'TRUNCATE', | |
| 110 | - 'DROP', | |
| 111 | - | |
| 112 | 90 | 'INSERT', |
| 91 | + 'UPDATE', | |
| 113 | 92 | 'DELETE', |
| 114 | - 'UPDATE', | |
| 115 | - 'REPLACE', | |
| 116 | - | |
| 117 | 93 | 'RENAME', |
| 94 | + 'DROP', | |
| 95 | + 'CREATE', | |
| 96 | + 'TRUNCATE', | |
| 97 | + 'ALTER', | |
| 118 | 98 | 'COMMIT', |
| 119 | 99 | 'ROLLBACK', |
| 120 | 100 | 'MERGE', |
| 121 | 101 | 'CALL', |
| @@ -151,10 +131,9 @@ | ||
| 151 | 131 | $results = array(); |
| 152 | 132 | $headers = array(); |
| 153 | 133 | |
| 154 | 134 | // short circuit results for remote dbs. |
| 155 | - $remote_results = apply_filters( 'visualizer_db_query_execute', false, $this->_query, $as_html, $results_as_numeric_array, $raw_results, $this->_chart_id, $this->_params ); | |
| 156 | - if ( false !== $remote_results ) { | |
| 135 | + if ( false !== ( $remote_results = apply_filters( 'visualizer_db_query_execute', false, $this->_query, $as_html, $results_as_numeric_array, $raw_results, $this->_chart_id, $this->_params ) ) ) { | |
| 157 | 136 | $error = $remote_results['error']; |
| 158 | 137 | if ( empty( $error ) ) { |
| 159 | 138 | $results = $remote_results['results']; |
| 160 | 139 | $headers = $remote_results['headers']; |
| @@ -178,13 +157,8 @@ | ||
| 178 | 157 | if ( $raw_results ) { |
| 179 | 158 | return $rows; |
| 180 | 159 | } |
| 181 | 160 | |
| 182 | - if ( $wpdb->last_error ) { | |
| 183 | - $this->_error = $wpdb->last_error; | |
| 184 | - return array(); | |
| 185 | - } | |
| 186 | - | |
| 187 | 161 | if ( $rows ) { |
| 188 | 162 | $results = array(); |
| 189 | 163 | $headers = array(); |
| 190 | 164 | if ( $rows ) { |
| @@ -198,9 +172,9 @@ | ||
| 198 | 172 | $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k ); |
| 199 | 173 | } |
| 200 | 174 | } |
| 201 | 175 | $results[] = $result; |
| 202 | - ++$row_num; | |
| 176 | + $row_num++; | |
| 203 | 177 | } |
| 204 | 178 | } |
| 205 | 179 | |
| 206 | 180 | $this->_error = $wpdb->last_error; |