| @@ -35,23 +35,35 @@ | ||
| 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 ) { | |
| 62 | + public function __construct( $query = null, $chart_id = null, $params = null ) { | |
| 53 | 63 | $this->_query = $query; |
| 64 | + $this->_chart_id = $chart_id; | |
| 65 | + $this->_params = $params; | |
| 54 | 66 | } |
| 55 | 67 | |
| 56 | 68 | /** |
| 57 | 69 | * Fetches information from source, parses it and builds series and data arrays. |
| @@ -66,52 +78,83 @@ | ||
| 66 | 78 | if ( empty( $this->_query ) ) { |
| 67 | 79 | return false; |
| 68 | 80 | } |
| 69 | 81 | |
| 82 | + // only select queries allowed. | |
| 83 | + if ( preg_match( '/^\s*(insert|delete|update|replace|create|alter|drop|truncate)\s/i', $this->_query ) ) { | |
| 84 | + $this->_error = __( 'Only SELECT queries are allowed', 'visualizer' ); | |
| 85 | + return false; | |
| 86 | + } | |
| 87 | + | |
| 70 | 88 | // impose a limit if no limit clause is provided. |
| 71 | 89 | if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) { |
| 72 | - $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000 ); | |
| 90 | + $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000, $this->_chart_id ); | |
| 73 | 91 | } |
| 74 | 92 | |
| 75 | - global $wpdb; | |
| 76 | - $wpdb->hide_errors(); | |
| 77 | - // @codingStandardsIgnoreStart | |
| 78 | - $rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A ); | |
| 79 | - 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__ ); | |
| 80 | - // @codingStandardsIgnoreEnd | |
| 81 | - $wpdb->show_errors(); | |
| 93 | + $this->_query = apply_filters( 'visualizer_db_query', $this->_query, $this->_chart_id, $this->_params ); | |
| 82 | 94 | |
| 83 | - if ( $raw_results ) { | |
| 84 | - return $rows; | |
| 95 | + $results = array(); | |
| 96 | + $headers = array(); | |
| 97 | + | |
| 98 | + // short circuit results for remote dbs. | |
| 99 | + 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 ) ) ) { | |
| 100 | + $error = $remote_results['error']; | |
| 101 | + if ( empty( $error ) ) { | |
| 102 | + $results = $remote_results['results']; | |
| 103 | + $headers = $remote_results['headers']; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $this->_error = $error; | |
| 107 | + | |
| 108 | + if ( $raw_results ) { | |
| 109 | + return $results; | |
| 110 | + } | |
| 85 | 111 | } |
| 86 | 112 | |
| 87 | - if ( $rows ) { | |
| 88 | - $results = array(); | |
| 89 | - $headers = array(); | |
| 113 | + if ( ! ( $results && $headers ) ) { | |
| 114 | + global $wpdb; | |
| 115 | + $wpdb->hide_errors(); | |
| 116 | + // @codingStandardsIgnoreStart | |
| 117 | + $rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A ); | |
| 118 | + // @codingStandardsIgnoreEnd | |
| 119 | + $wpdb->show_errors(); | |
| 120 | + | |
| 121 | + if ( $raw_results ) { | |
| 122 | + return $rows; | |
| 123 | + } | |
| 124 | + | |
| 90 | 125 | if ( $rows ) { |
| 91 | - $row_num = 0; | |
| 92 | - foreach ( $rows as $row ) { | |
| 93 | - $result = array(); | |
| 94 | - $col_num = 0; | |
| 95 | - foreach ( $row as $k => $v ) { | |
| 96 | - $result[] = $v; | |
| 97 | - if ( 0 === $row_num ) { | |
| 98 | - $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k ); | |
| 126 | + $results = array(); | |
| 127 | + $headers = array(); | |
| 128 | + if ( $rows ) { | |
| 129 | + $row_num = 0; | |
| 130 | + foreach ( $rows as $row ) { | |
| 131 | + $result = array(); | |
| 132 | + $col_num = 0; | |
| 133 | + foreach ( $row as $k => $v ) { | |
| 134 | + $result[] = $v; | |
| 135 | + if ( 0 === $row_num ) { | |
| 136 | + $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k ); | |
| 137 | + } | |
| 99 | 138 | } |
| 139 | + $results[] = $result; | |
| 140 | + $row_num++; | |
| 100 | 141 | } |
| 101 | - $results[] = $result; | |
| 102 | - $row_num++; | |
| 103 | 142 | } |
| 143 | + | |
| 144 | + $this->_error = $wpdb->last_error; | |
| 104 | 145 | } |
| 146 | + } | |
| 147 | + // Query log. | |
| 148 | + 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__ ); | |
| 105 | 149 | |
| 106 | - if ( $as_html ) { | |
| 107 | - return $this->html( $headers, $results ); | |
| 108 | - } | |
| 109 | - return $this->object( $headers, $results ); | |
| 150 | + if ( $as_html ) { | |
| 151 | + $results = $this->html( $headers, $results ); | |
| 152 | + } else { | |
| 153 | + $results = $this->object( $headers, $results ); | |
| 110 | 154 | } |
| 111 | 155 | |
| 112 | - $this->_error = $wpdb->last_error; | |
| 113 | - return null; | |
| 156 | + return apply_filters( 'visualizer_db_query_results', $results, $headers, $as_html, $results_as_numeric_array, $raw_results, $this->_query, $this->_chart_id, $this->_params ); | |
| 114 | 157 | } |
| 115 | 158 | |
| 116 | 159 | /** |
| 117 | 160 | * Get the data type of the column. |
| @@ -173,20 +216,9 @@ | ||
| 173 | 216 | foreach ( $results as $row ) { |
| 174 | 217 | $data[] = $this->_normalizeData( $row ); |
| 175 | 218 | } |
| 176 | 219 | $this->_data = $data; |
| 177 | - | |
| 178 | - return true; | |
| 179 | - } | |
| 180 | - | |
| 181 | - /** | |
| 182 | - * Returns the error, if any. | |
| 183 | - * | |
| 184 | - * @access public | |
| 185 | - * @return string | |
| 186 | - */ | |
| 187 | - public function get_error() { | |
| 188 | - return $this->_error; | |
| 220 | + return $this->_data; | |
| 189 | 221 | } |
| 190 | 222 | |
| 191 | 223 | /** |
| 192 | 224 | * Returns the final query. |