| @@ -35,76 +35,187 @@ | ||
| 35 | 35 | */ |
| 36 | 36 | protected $_query; |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | - * The error message. | |
| 39 | + * The chart id. | |
| 40 | 40 | * |
| 41 | 41 | * @access protected |
| 42 | - * @var string | |
| 42 | + * @var int | |
| 43 | 43 | */ |
| 44 | - protected $_error; | |
| 44 | + protected $_chart_id; | |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | + * Any additional parameters (e.g. for connecting to a remote db). | |
| 48 | + * | |
| 49 | + * @access protected | |
| 50 | + * @var array | |
| 51 | + */ | |
| 52 | + protected $_params; | |
| 53 | + | |
| 54 | + /** | |
| 47 | 55 | * Constructor. |
| 48 | 56 | * |
| 49 | 57 | * @access public |
| 50 | 58 | * @param string $query The query. |
| 59 | + * @param int $chart_id The chart id. | |
| 60 | + * @param array $params Any additional parameters (e.g. for connecting to a remote db). | |
| 51 | 61 | */ |
| 52 | - public function __construct( $query = null ) { | |
| 53 | - $this->_query = $query; | |
| 62 | + public function __construct( $query = null, $chart_id = null, $params = null ) { | |
| 63 | + $this->_query = $this->strip_sql_comments( $query ); | |
| 64 | + $this->_chart_id = $chart_id; | |
| 65 | + $this->_params = $params; | |
| 54 | 66 | } |
| 55 | 67 | |
| 56 | 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 | + /** | |
| 57 | 86 | * Fetches information from source, parses it and builds series and data arrays. |
| 58 | 87 | * |
| 59 | 88 | * @param bool $as_html Should the result be fetched as an HTML table or as an object. |
| 89 | + * @param bool $results_as_numeric_array Should the result be fetched as ARRAY_N instead of ARRAY_A. | |
| 90 | + * @param bool $raw_results Should the result be returned without processing. | |
| 60 | 91 | * @access public |
| 61 | 92 | * @return boolean TRUE on success, otherwise FALSE. |
| 62 | 93 | */ |
| 63 | - public function fetch( $as_html = false ) { | |
| 94 | + public function fetch( $as_html = false, $results_as_numeric_array = false, $raw_results = false ) { | |
| 64 | 95 | if ( empty( $this->_query ) ) { |
| 65 | 96 | return false; |
| 66 | 97 | } |
| 67 | 98 | |
| 99 | + // only select queries allowed. must start with SELECT keyword. | |
| 100 | + if ( ! preg_match( '/^(\bselect\b)\s/i', $this->_query ) ) { | |
| 101 | + $this->_error = __( 'Only SELECT queries are allowed', 'visualizer' ); | |
| 102 | + return false; | |
| 103 | + } | |
| 104 | + | |
| 105 | + // if previous check passed, check for disallowed query parts to prevent subqueries and other harmful queries. | |
| 106 | + $disallow_query_parts = array( | |
| 107 | + 'CREATE', | |
| 108 | + 'ALTER', | |
| 109 | + 'TRUNCATE', | |
| 110 | + 'DROP', | |
| 111 | + | |
| 112 | + 'INSERT', | |
| 113 | + 'DELETE', | |
| 114 | + 'UPDATE', | |
| 115 | + 'REPLACE', | |
| 116 | + | |
| 117 | + 'RENAME', | |
| 118 | + 'COMMIT', | |
| 119 | + 'ROLLBACK', | |
| 120 | + 'MERGE', | |
| 121 | + 'CALL', | |
| 122 | + 'EXPLAIN', | |
| 123 | + 'LOCK', | |
| 124 | + 'GRANT', | |
| 125 | + 'REVOKE', | |
| 126 | + 'SAVEPOINT', | |
| 127 | + 'TRANSACTION', | |
| 128 | + 'SET', | |
| 129 | + ); | |
| 130 | + $disallow_regex = implode( | |
| 131 | + '|', | |
| 132 | + array_map( | |
| 133 | + function ( $value ) { | |
| 134 | + return '\b' . $value . '\b'; | |
| 135 | + }, $disallow_query_parts | |
| 136 | + ) | |
| 137 | + ); | |
| 138 | + | |
| 139 | + if ( preg_match( '/(' . $disallow_regex . ')/i', $this->_query) !== 0 ) { | |
| 140 | + $this->_error = __( 'Only SELECT queries are allowed', 'visualizer' ); | |
| 141 | + return false; | |
| 142 | + } | |
| 143 | + | |
| 68 | 144 | // impose a limit if no limit clause is provided. |
| 69 | 145 | if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) { |
| 70 | - $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 300 ); | |
| 146 | + $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000, $this->_chart_id ); | |
| 71 | 147 | } |
| 72 | 148 | |
| 73 | - global $wpdb; | |
| 74 | - $wpdb->hide_errors(); | |
| 75 | - // @codingStandardsIgnoreStart | |
| 76 | - $rows = $wpdb->get_results( $this->_query, ARRAY_A ); | |
| 77 | - // @codingStandardsIgnoreEnd | |
| 78 | - $wpdb->show_errors(); | |
| 149 | + $this->_query = apply_filters( 'visualizer_db_query', $this->_query, $this->_chart_id, $this->_params ); | |
| 79 | 150 | |
| 80 | - if ( $rows ) { | |
| 81 | - $results = array(); | |
| 82 | - $headers = array(); | |
| 151 | + $results = array(); | |
| 152 | + $headers = array(); | |
| 153 | + | |
| 154 | + // short circuit results for remote dbs. | |
| 155 | + 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 ) ) ) { | |
| 156 | + $error = $remote_results['error']; | |
| 157 | + if ( empty( $error ) ) { | |
| 158 | + $results = $remote_results['results']; | |
| 159 | + $headers = $remote_results['headers']; | |
| 160 | + } | |
| 161 | + | |
| 162 | + $this->_error = $error; | |
| 163 | + | |
| 164 | + if ( $raw_results ) { | |
| 165 | + return $results; | |
| 166 | + } | |
| 167 | + } | |
| 168 | + | |
| 169 | + if ( ! ( $results && $headers ) ) { | |
| 170 | + global $wpdb; | |
| 171 | + $wpdb->hide_errors(); | |
| 172 | + // @codingStandardsIgnoreStart | |
| 173 | + $rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A ); | |
| 174 | + // @codingStandardsIgnoreEnd | |
| 175 | + $wpdb->show_errors(); | |
| 176 | + | |
| 177 | + if ( $raw_results ) { | |
| 178 | + return $rows; | |
| 179 | + } | |
| 180 | + | |
| 181 | + if ( $wpdb->last_error ) { | |
| 182 | + $this->_error = $wpdb->last_error; | |
| 183 | + return []; | |
| 184 | + } | |
| 185 | + | |
| 83 | 186 | if ( $rows ) { |
| 84 | - $row_num = 0; | |
| 85 | - foreach ( $rows as $row ) { | |
| 86 | - $result = array(); | |
| 87 | - $col_num = 0; | |
| 88 | - foreach ( $row as $k => $v ) { | |
| 89 | - $result[] = $v; | |
| 90 | - if ( 0 === $row_num ) { | |
| 91 | - $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k ); | |
| 187 | + $results = array(); | |
| 188 | + $headers = array(); | |
| 189 | + if ( $rows ) { | |
| 190 | + $row_num = 0; | |
| 191 | + foreach ( $rows as $row ) { | |
| 192 | + $result = array(); | |
| 193 | + $col_num = 0; | |
| 194 | + foreach ( $row as $k => $v ) { | |
| 195 | + $result[] = $v; | |
| 196 | + if ( 0 === $row_num ) { | |
| 197 | + $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k ); | |
| 198 | + } | |
| 92 | 199 | } |
| 200 | + $results[] = $result; | |
| 201 | + $row_num++; | |
| 93 | 202 | } |
| 94 | - $results[] = $result; | |
| 95 | - $row_num++; | |
| 96 | 203 | } |
| 204 | + | |
| 205 | + $this->_error = $wpdb->last_error; | |
| 97 | 206 | } |
| 207 | + } | |
| 208 | + // Query log. | |
| 209 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Firing query %s to get results %s with error %s', $this->_query, print_r( $rows, true ), print_r( $wpdb->last_error, true ) ), 'debug', __FILE__, __LINE__ ); | |
| 98 | 210 | |
| 99 | - if ( $as_html ) { | |
| 100 | - return $this->html( $headers, $results ); | |
| 101 | - } | |
| 102 | - return $this->object( $headers, $results ); | |
| 211 | + if ( $as_html ) { | |
| 212 | + $results = $this->html( $headers, $results ); | |
| 213 | + } else { | |
| 214 | + $results = $this->object( $headers, $results ); | |
| 103 | 215 | } |
| 104 | 216 | |
| 105 | - $this->_error = $wpdb->last_error; | |
| 106 | - return null; | |
| 217 | + return apply_filters( 'visualizer_db_query_results', $results, $headers, $as_html, $results_as_numeric_array, $raw_results, $this->_query, $this->_chart_id, $this->_params ); | |
| 107 | 218 | } |
| 108 | 219 | |
| 109 | 220 | /** |
| 110 | 221 | * Get the data type of the column. |
| @@ -166,20 +277,9 @@ | ||
| 166 | 277 | foreach ( $results as $row ) { |
| 167 | 278 | $data[] = $this->_normalizeData( $row ); |
| 168 | 279 | } |
| 169 | 280 | $this->_data = $data; |
| 170 | - | |
| 171 | - return true; | |
| 172 | - } | |
| 173 | - | |
| 174 | - /** | |
| 175 | - * Returns the error, if any. | |
| 176 | - * | |
| 177 | - * @access public | |
| 178 | - * @return string | |
| 179 | - */ | |
| 180 | - public function get_error() { | |
| 181 | - return $this->_error; | |
| 281 | + return $this->_data; | |
| 182 | 282 | } |
| 183 | 283 | |
| 184 | 284 | /** |
| 185 | 285 | * Returns the final query. |