| @@ -10,9 +10,9 @@ | ||
| 10 | 10 | public $redirection_id; |
| 11 | 11 | |
| 12 | 12 | function __construct( $values ) { |
| 13 | 13 | foreach ( $values as $key => $value ) { |
| 14 | - $this->$key = $value; | |
| 14 | + $this->$key = $value; | |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | $this->created = mysql2date( 'U', $this->created ); |
| 18 | 18 | $this->url = stripslashes( $this->url ); |
| @@ -28,15 +28,15 @@ | ||
| 28 | 28 | |
| 29 | 29 | return false; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - static function create( $url, $target, $agent, $ip, $referrer, $extra = array() ) { | |
| 32 | + static function create( $url, $target, $agent, $ip, $referrer, $extra = array()) { | |
| 33 | 33 | global $wpdb, $redirection; |
| 34 | 34 | |
| 35 | 35 | $insert = array( |
| 36 | 36 | 'url' => urldecode( $url ), |
| 37 | 37 | 'created' => current_time( 'mysql' ), |
| 38 | - 'ip' => substr( $ip, 0, 45 ), | |
| 38 | + 'ip' => $ip, | |
| 39 | 39 | ); |
| 40 | 40 | |
| 41 | 41 | if ( ! empty( $agent ) ) { |
| 42 | 42 | $insert['agent'] = $agent; |
| @@ -53,9 +53,9 @@ | ||
| 53 | 53 | $insert = apply_filters( 'redirection_log_data', $insert ); |
| 54 | 54 | if ( $insert ) { |
| 55 | 55 | do_action( 'redirection_log', $insert ); |
| 56 | 56 | |
| 57 | - $wpdb->insert( $wpdb->prefix . 'redirection_logs', $insert ); | |
| 57 | + $wpdb->insert( $wpdb->prefix.'redirection_logs', $insert ); | |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | return $wpdb->insert_id; |
| 61 | 61 | } |
| @@ -78,42 +78,45 @@ | ||
| 78 | 78 | global $wpdb; |
| 79 | 79 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) ); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - static function delete_all( $filter_by = '', $filter = '' ) { | |
| 82 | + static function delete_all( $filterBy = '', $filter = '' ) { | |
| 83 | 83 | global $wpdb; |
| 84 | 84 | |
| 85 | 85 | $where = array(); |
| 86 | 86 | |
| 87 | - if ( $filter_by === 'url' && $filter ) { | |
| 88 | - $where[] = $wpdb->prepare( 'url LIKE %s', '%' . $wpdb->esc_like( $filter ) . '%' ); | |
| 89 | - } elseif ( $filter_by === 'ip' ) { | |
| 87 | + if ( $filterBy === 'url' && $filter ) { | |
| 88 | + $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $filter ).'%' ); | |
| 89 | + } else if ( $filterBy === 'ip' ) { | |
| 90 | 90 | $where[] = $wpdb->prepare( 'ip=%s', $filter ); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | $where_cond = ''; |
| 94 | 94 | if ( count( $where ) > 0 ) { |
| 95 | - $where_cond = ' WHERE ' . implode( ' AND ', $where ); | |
| 95 | + $where_cond = ' WHERE '.implode( ' AND ', $where ); | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs" . $where_cond ); | |
| 98 | + $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs".$where_cond ); | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | static function export_to_csv() { |
| 102 | 102 | global $wpdb; |
| 103 | 103 | |
| 104 | - $filename = 'redirection-log-' . date_i18n( get_option( 'date_format' ) ) . '.csv'; | |
| 104 | + $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv'; | |
| 105 | 105 | |
| 106 | 106 | header( 'Content-Type: text/csv' ); |
| 107 | 107 | header( 'Cache-Control: no-cache, must-revalidate' ); |
| 108 | 108 | header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 109 | - header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); | |
| 109 | + header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); | |
| 110 | 110 | |
| 111 | 111 | $stdout = fopen( 'php://output', 'w' ); |
| 112 | 112 | |
| 113 | 113 | fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) ); |
| 114 | 114 | |
| 115 | - $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs" ); | |
| 115 | + $extra = ''; | |
| 116 | + $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs"; | |
| 117 | + | |
| 118 | + $total_items = $wpdb->get_var( $sql.$extra ); | |
| 116 | 119 | $exported = 0; |
| 117 | 120 | |
| 118 | 121 | while ( $exported < $total_items ) { |
| 119 | 122 | $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) ); |
| @@ -131,11 +134,10 @@ | ||
| 131 | 134 | |
| 132 | 135 | fputcsv( $stdout, $csv ); |
| 133 | 136 | } |
| 134 | 137 | |
| 135 | - if ( count( $rows ) < 100 ) { | |
| 138 | + if ( count( $rows ) < 100 ) | |
| 136 | 139 | break; |
| 137 | - } | |
| 138 | 140 | } |
| 139 | 141 | } |
| 140 | 142 | |
| 141 | 143 | public function to_json() { |
| @@ -155,9 +157,9 @@ | ||
| 155 | 157 | public $ip; |
| 156 | 158 | |
| 157 | 159 | function __construct( $values ) { |
| 158 | 160 | foreach ( $values as $key => $value ) { |
| 159 | - $this->$key = $value; | |
| 161 | + $this->$key = $value; | |
| 160 | 162 | } |
| 161 | 163 | |
| 162 | 164 | $this->created = mysql2date( 'U', $this->created ); |
| 163 | 165 | } |
| @@ -178,9 +180,9 @@ | ||
| 178 | 180 | |
| 179 | 181 | $insert = array( |
| 180 | 182 | 'url' => substr( urldecode( $url ), 0, 255 ), |
| 181 | 183 | 'created' => current_time( 'mysql' ), |
| 182 | - 'ip' => substr( $ip, 0, 45 ), | |
| 184 | + 'ip' => ip2long( $ip ), | |
| 183 | 185 | ); |
| 184 | 186 | |
| 185 | 187 | if ( ! empty( $agent ) ) { |
| 186 | 188 | $insert['agent'] = substr( $agent, 0, 255 ); |
| @@ -191,9 +193,9 @@ | ||
| 191 | 193 | } |
| 192 | 194 | |
| 193 | 195 | $insert = apply_filters( 'redirection_404_data', $insert ); |
| 194 | 196 | if ( $insert ) { |
| 195 | - $wpdb->insert( $wpdb->prefix . 'redirection_404', $insert ); | |
| 197 | + $wpdb->insert( $wpdb->prefix.'redirection_404', $insert ); | |
| 196 | 198 | |
| 197 | 199 | if ( $wpdb->insert_id ) { |
| 198 | 200 | return $wpdb->insert_id; |
| 199 | 201 | } |
| @@ -207,44 +209,49 @@ | ||
| 207 | 209 | |
| 208 | 210 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) ); |
| 209 | 211 | } |
| 210 | 212 | |
| 211 | - static function delete_all( $filter_by = '', $filter = '' ) { | |
| 213 | + static function delete_all( $filterBy = '', $filter = '' ) { | |
| 212 | 214 | global $wpdb; |
| 213 | 215 | |
| 214 | 216 | $where = array(); |
| 215 | 217 | |
| 216 | - if ( $filter_by === 'url-exact' ) { | |
| 218 | + if ( $filterBy === 'url-exact' ) { | |
| 217 | 219 | $where[] = $wpdb->prepare( 'url=%s', $filter ); |
| 218 | - } if ( $filter_by === 'url' && $filter ) { | |
| 219 | - $where[] = $wpdb->prepare( 'url LIKE %s', '%' . $wpdb->esc_like( $filter ) . '%' ); | |
| 220 | - } elseif ( $filter_by === 'ip' ) { | |
| 221 | - $where[] = $wpdb->prepare( 'ip=%s', $filter ); | |
| 220 | + } if ( $filterBy === 'url' && $filter ) { | |
| 221 | + $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $filter ).'%' ); | |
| 222 | + } else if ( $filterBy === 'ip' ) { | |
| 223 | + $where[] = $wpdb->prepare( 'ip=INET_ATON(%s)', $filter ); | |
| 222 | 224 | } |
| 223 | 225 | |
| 224 | 226 | $where_cond = ''; |
| 225 | 227 | if ( count( $where ) > 0 ) { |
| 226 | - $where_cond = ' WHERE ' . implode( ' AND ', $where ); | |
| 228 | + $where_cond = ' WHERE '.implode( ' AND ', $where ); | |
| 227 | 229 | } |
| 228 | 230 | |
| 229 | - $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404" . $where_cond ); | |
| 231 | + $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404".$where_cond ); | |
| 230 | 232 | } |
| 231 | 233 | |
| 232 | 234 | static function export_to_csv() { |
| 233 | 235 | global $wpdb; |
| 234 | 236 | |
| 235 | - $filename = 'redirection-404-' . date_i18n( get_option( 'date_format' ) ) . '.csv'; | |
| 237 | + $filename = 'redirection-404-'.date_i18n( get_option( 'date_format' ) ).'.csv'; | |
| 236 | 238 | |
| 237 | 239 | header( 'Content-Type: text/csv' ); |
| 238 | 240 | header( 'Cache-Control: no-cache, must-revalidate' ); |
| 239 | 241 | header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 240 | - header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); | |
| 242 | + header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); | |
| 241 | 243 | |
| 242 | 244 | $stdout = fopen( 'php://output', 'w' ); |
| 243 | 245 | |
| 244 | 246 | fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) ); |
| 245 | 247 | |
| 246 | - $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404" ); | |
| 248 | + $extra = ''; | |
| 249 | + $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404"; | |
| 250 | + // if ( isset( $_REQUEST['s'] ) ) | |
| 251 | + // $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' ); | |
| 252 | + | |
| 253 | + $total_items = $wpdb->get_var( $sql.$extra ); | |
| 247 | 254 | $exported = 0; |
| 248 | 255 | |
| 249 | 256 | while ( $exported < $total_items ) { |
| 250 | 257 | $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) ); |
| @@ -253,9 +260,9 @@ | ||
| 253 | 260 | foreach ( $rows as $row ) { |
| 254 | 261 | $csv = array( |
| 255 | 262 | $row->created, |
| 256 | 263 | $row->url, |
| 257 | - $row->ip, | |
| 264 | + long2ip( $row->ip ), | |
| 258 | 265 | $row->referrer, |
| 259 | 266 | ); |
| 260 | 267 | |
| 261 | 268 | fputcsv( $stdout, $csv ); |
| @@ -260,31 +267,63 @@ | ||
| 260 | 267 | |
| 261 | 268 | fputcsv( $stdout, $csv ); |
| 262 | 269 | } |
| 263 | 270 | |
| 264 | - if ( count( $rows ) < 100 ) { | |
| 271 | + if ( count( $rows ) < 100 ) | |
| 265 | 272 | break; |
| 266 | - } | |
| 267 | 273 | } |
| 268 | 274 | } |
| 269 | 275 | |
| 270 | 276 | public function to_json() { |
| 271 | 277 | return array( |
| 272 | - 'ip' => $this->ip, | |
| 278 | + 'ip' => long2ip( $this->ip ), | |
| 273 | 279 | ); |
| 274 | 280 | } |
| 275 | 281 | } |
| 276 | 282 | |
| 277 | 283 | class RE_Filter_Log { |
| 278 | - static public function get( $table, $construct, array $params ) { | |
| 284 | + static function get( $table, $construct, array $params ) { | |
| 279 | 285 | global $wpdb; |
| 280 | 286 | |
| 281 | - $query = self::get_query( $params ); | |
| 287 | + $orderby = 'id'; | |
| 288 | + $direction = 'DESC'; | |
| 289 | + $limit = RED_DEFAULT_PER_PAGE; | |
| 290 | + $offset = 0; | |
| 291 | + $where = ''; | |
| 282 | 292 | |
| 283 | - $rows = $wpdb->get_results( | |
| 284 | - "SELECT * FROM {$wpdb->prefix}$table {$query['where']}" . $wpdb->prepare( ' ORDER BY ' . $query['orderby'] . ' ' . $query['direction'] . ' LIMIT %d,%d', $query['offset'], $query['limit'] ) | |
| 285 | - ); | |
| 286 | - $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}$table " . $query['where'] ); | |
| 293 | + if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'ip', 'url' ), true ) ) { | |
| 294 | + $orderby = $params['orderBy']; | |
| 295 | + } | |
| 296 | + | |
| 297 | + if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) { | |
| 298 | + $direction = strtoupper( $params['direction'] ); | |
| 299 | + } | |
| 300 | + | |
| 301 | + if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) { | |
| 302 | + if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) { | |
| 303 | + $where = $wpdb->prepare( "WHERE ip=%s", $params['filter'] ); | |
| 304 | + } else { | |
| 305 | + $where = $wpdb->prepare( 'WHERE url LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' ); | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 309 | + if ( isset( $params['perPage'] ) ) { | |
| 310 | + $limit = intval( $params['perPage'], 10 ); | |
| 311 | + $limit = min( RED_MAX_PER_PAGE, $limit ); | |
| 312 | + $limit = max( 5, $limit ); | |
| 313 | + } | |
| 314 | + | |
| 315 | + if ( isset( $params['page'] ) ) { | |
| 316 | + $offset = intval( $params['page'], 10 ); | |
| 317 | + $offset = max( 0, $offset ); | |
| 318 | + $offset *= $limit; | |
| 319 | + } | |
| 320 | + | |
| 321 | + $table = $wpdb->prefix.$table; | |
| 322 | + $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit ); | |
| 323 | + | |
| 324 | + $rows = $wpdb->get_results( $sql ); | |
| 325 | + $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where ); | |
| 287 | 326 | $items = array(); |
| 288 | 327 | |
| 289 | 328 | foreach ( $rows as $row ) { |
| 290 | 329 | $item = new $construct( $row ); |
| @@ -289,10 +328,9 @@ | ||
| 289 | 328 | foreach ( $rows as $row ) { |
| 290 | 329 | $item = new $construct( $row ); |
| 291 | 330 | $items[] = array_merge( $item->to_json(), array( |
| 292 | 331 | 'id' => intval( $item->id, 10 ), |
| 293 | - 'created' => date_i18n( get_option( 'date_format' ), $item->created ), | |
| 294 | - 'created_time' => gmdate( get_option( 'time_format' ), $item->created ), | |
| 332 | + 'created' => date_i18n( get_option( 'date_format' ), $item->created ).' '.gmdate( get_option( 'time_format' ), $item->created ), | |
| 295 | 333 | 'url' => $item->url, |
| 296 | 334 | 'agent' => $item->agent, |
| 297 | 335 | 'referrer' => $item->referrer, |
| 298 | 336 | ) ); |
| @@ -301,74 +339,6 @@ | ||
| 301 | 339 | return array( |
| 302 | 340 | 'items' => $items, |
| 303 | 341 | 'total' => intval( $total_items, 10 ), |
| 304 | 342 | ); |
| 305 | - } | |
| 306 | - | |
| 307 | - static public function get_grouped( $table, $group, array $params ) { | |
| 308 | - global $wpdb; | |
| 309 | - | |
| 310 | - $query = self::get_query( $params ); | |
| 311 | - | |
| 312 | - if ( ! in_array( $group, array( 'ip', 'url' ), true ) ) { | |
| 313 | - $group = 'url'; | |
| 314 | - } | |
| 315 | - | |
| 316 | - $sql = $wpdb->prepare( "SELECT COUNT(*) as count,$group FROM {$wpdb->prefix}$table " . $query['where'] . ' GROUP BY ' . $group . ' ORDER BY count ' . $query['direction'] . ' LIMIT %d,%d', $query['offset'], $query['limit'] ); | |
| 317 | - $rows = $wpdb->get_results( $sql ); | |
| 318 | - $total_items = $wpdb->get_var( "SELECT COUNT(DISTINCT $group) FROM {$wpdb->prefix}$table" ); | |
| 319 | - | |
| 320 | - foreach ( $rows as $row ) { | |
| 321 | - $row->count = intval( $row->count, 10 ); | |
| 322 | - $row->id = isset( $row->url ) ? $row->url : $row->ip; | |
| 323 | - } | |
| 324 | - | |
| 325 | - return array( | |
| 326 | - 'items' => $rows, | |
| 327 | - 'total' => intval( $total_items, 10 ), | |
| 328 | - ); | |
| 329 | - } | |
| 330 | - | |
| 331 | - static private function get_query( array $params ) { | |
| 332 | - global $wpdb; | |
| 333 | - | |
| 334 | - $query = array( | |
| 335 | - 'orderby' => 'id', | |
| 336 | - 'direction' => 'DESC', | |
| 337 | - 'limit' => RED_DEFAULT_PER_PAGE, | |
| 338 | - 'offset' => 0, | |
| 339 | - 'where' => '', | |
| 340 | - ); | |
| 341 | - | |
| 342 | - if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'ip', 'url' ), true ) ) { | |
| 343 | - $query['orderby'] = $params['orderby']; | |
| 344 | - } | |
| 345 | - | |
| 346 | - if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) { | |
| 347 | - $query['direction'] = strtoupper( $params['direction'] ); | |
| 348 | - } | |
| 349 | - | |
| 350 | - if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) { | |
| 351 | - if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) { | |
| 352 | - $query['where'] = $wpdb->prepare( 'WHERE ip=%s', $params['filter'] ); | |
| 353 | - } elseif ( isset( $params['filterBy'] ) && $params['filterBy'] === 'url-exact' ) { | |
| 354 | - $query['where'] = $wpdb->prepare( 'WHERE url=%s', $params['filter'] ); | |
| 355 | - } else { | |
| 356 | - $query['where'] = $wpdb->prepare( 'WHERE url LIKE %s', '%' . $wpdb->esc_like( trim( $params['filter'] ) ) . '%' ); | |
| 357 | - } | |
| 358 | - } | |
| 359 | - | |
| 360 | - if ( isset( $params['per_page'] ) ) { | |
| 361 | - $query['limit'] = intval( $params['per_page'], 10 ); | |
| 362 | - $query['limit'] = min( RED_MAX_PER_PAGE, $query['limit'] ); | |
| 363 | - $query['limit'] = max( 5, $query['limit'] ); | |
| 364 | - } | |
| 365 | - | |
| 366 | - if ( isset( $params['page'] ) ) { | |
| 367 | - $query['offset'] = intval( $params['page'], 10 ); | |
| 368 | - $query['offset'] = max( 0, $query['offset'] ); | |
| 369 | - $query['offset'] *= $query['limit']; | |
| 370 | - } | |
| 371 | - | |
| 372 | - return $query; | |
| 373 | 343 | } |
| 374 | 344 | } |