| 1 |
<?php |
| 2 |
|
| 3 |
class RE_Log { |
| 4 |
public $id; |
| 5 |
public $created; |
| 6 |
public $url; |
| 7 |
public $agent; |
| 8 |
public $referrer; |
| 9 |
public $ip; |
| 10 |
public $redirection_id; |
| 11 |
|
| 12 |
function __construct( $values ) { |
| 13 |
foreach ( $values as $key => $value ) { |
| 14 |
$this->$key = $value; |
| 15 |
} |
| 16 |
|
| 17 |
$this->created = mysql2date( 'U', $this->created ); |
| 18 |
$this->url = stripslashes( $this->url ); |
| 19 |
} |
| 20 |
|
| 21 |
static function get_by_id( $id ) { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) ); |
| 25 |
if ( $row ) |
| 26 |
return new RE_Log( $row ); |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
static function create( $url, $target, $agent, $ip, $referrer, $extra = array()) { |
| 31 |
global $wpdb, $redirection; |
| 32 |
|
| 33 |
$insert = array( |
| 34 |
'url' => urldecode( $url ), |
| 35 |
'created' => current_time( 'mysql' ), |
| 36 |
'ip' => $ip, |
| 37 |
); |
| 38 |
|
| 39 |
if ( ! empty( $agent ) ) |
| 40 |
$insert['agent'] = $agent; |
| 41 |
|
| 42 |
if ( ! empty( $referrer ) ) |
| 43 |
$insert['referrer'] = $referrer; |
| 44 |
|
| 45 |
$insert['sent_to'] = $target; |
| 46 |
$insert['redirection_id'] = isset( $extra['redirect_id'] ) ? $extra['redirect_id'] : 0; |
| 47 |
$insert['group_id'] = isset( $extra['group_id'] ) ? $extra['group_id'] : 0; |
| 48 |
|
| 49 |
$insert = apply_filters( 'redirection_log_data', $insert ); |
| 50 |
if ( $insert ) { |
| 51 |
do_action( 'redirection_log', $insert ); |
| 52 |
|
| 53 |
$wpdb->insert( $wpdb->prefix.'redirection_logs', $insert ); |
| 54 |
} |
| 55 |
|
| 56 |
return $wpdb->insert_id; |
| 57 |
} |
| 58 |
|
| 59 |
static function show_url( $url ) { |
| 60 |
return $url; |
| 61 |
} |
| 62 |
|
| 63 |
static function delete( $id ) { |
| 64 |
global $wpdb; |
| 65 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) ); |
| 66 |
} |
| 67 |
|
| 68 |
static function delete_for_id( $id ) { |
| 69 |
global $wpdb; |
| 70 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE redirection_id=%d", $id ) ); |
| 71 |
} |
| 72 |
|
| 73 |
static function delete_for_group( $id ) { |
| 74 |
global $wpdb; |
| 75 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) ); |
| 76 |
} |
| 77 |
|
| 78 |
static function delete_all( $type = 'all', $id = 0 ) { |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$where = array(); |
| 82 |
if ( $type === 'module' ) |
| 83 |
$where[] = $wpdb->prepare( 'module_id=%d', $id ); |
| 84 |
elseif ( $type === 'group' ) |
| 85 |
$where[] = $wpdb->prepare( 'group_id=%d AND redirection_id IS NOT NULL', $id ); |
| 86 |
elseif ( $type === 'redirect' ) |
| 87 |
$where[] = $wpdb->prepare( 'redirection_id=%d', $id ); |
| 88 |
|
| 89 |
// if ( isset( $_REQUEST['s'] ) ) |
| 90 |
// $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' ); |
| 91 |
|
| 92 |
$where_cond = ''; |
| 93 |
if ( count( $where ) > 0 ) |
| 94 |
$where_cond = ' WHERE '.implode( ' AND ', $where ); |
| 95 |
|
| 96 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs ".$where_cond ); |
| 97 |
} |
| 98 |
|
| 99 |
static function export_to_csv() { |
| 100 |
global $wpdb; |
| 101 |
|
| 102 |
$filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv'; |
| 103 |
|
| 104 |
header( 'Content-Type: text/csv' ); |
| 105 |
header( 'Cache-Control: no-cache, must-revalidate' ); |
| 106 |
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 107 |
header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
| 108 |
|
| 109 |
$stdout = fopen( 'php://output', 'w' ); |
| 110 |
|
| 111 |
fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) ); |
| 112 |
|
| 113 |
$extra = ''; |
| 114 |
$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs"; |
| 115 |
// if ( isset( $_REQUEST['s'] ) ) |
| 116 |
// $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' ); |
| 117 |
|
| 118 |
$total_items = $wpdb->get_var( $sql.$extra ); |
| 119 |
$exported = 0; |
| 120 |
|
| 121 |
while ( $exported < $total_items ) { |
| 122 |
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) ); |
| 123 |
$exported += count( $rows ); |
| 124 |
|
| 125 |
foreach ( $rows as $row ) { |
| 126 |
$csv = array( |
| 127 |
$row->created, |
| 128 |
$row->url, |
| 129 |
$row->sent_to, |
| 130 |
$row->ip, |
| 131 |
$row->referrer, |
| 132 |
$row->agent, |
| 133 |
); |
| 134 |
|
| 135 |
fputcsv( $stdout, $csv ); |
| 136 |
} |
| 137 |
|
| 138 |
if ( count( $rows ) < 100 ) |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public function to_json() { |
| 144 |
return array( |
| 145 |
'sent_to' => $this->sent_to, |
| 146 |
'ip' => $this->ip, |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
class RE_404 { |
| 152 |
public $id; |
| 153 |
public $created; |
| 154 |
public $url; |
| 155 |
public $agent; |
| 156 |
public $referrer; |
| 157 |
public $ip; |
| 158 |
|
| 159 |
function __construct( $values ) { |
| 160 |
foreach ( $values as $key => $value ) { |
| 161 |
$this->$key = $value; |
| 162 |
} |
| 163 |
|
| 164 |
$this->created = mysql2date( 'U', $this->created ); |
| 165 |
} |
| 166 |
|
| 167 |
static function get_by_id( $id ) { |
| 168 |
global $wpdb; |
| 169 |
|
| 170 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) ); |
| 171 |
if ( $row ) { |
| 172 |
return new RE_404( $row ); |
| 173 |
} |
| 174 |
|
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
static function create( $url, $agent, $ip, $referrer ) { |
| 179 |
global $wpdb, $redirection; |
| 180 |
|
| 181 |
$insert = array( |
| 182 |
'url' => urldecode( $url ), |
| 183 |
'created' => current_time( 'mysql' ), |
| 184 |
'ip' => ip2long( $ip ), |
| 185 |
); |
| 186 |
|
| 187 |
if ( ! empty( $agent ) ) { |
| 188 |
$insert['agent'] = $agent; |
| 189 |
} |
| 190 |
|
| 191 |
if ( ! empty( $referrer ) ) { |
| 192 |
$insert['referrer'] = $referrer; |
| 193 |
} |
| 194 |
|
| 195 |
$insert = apply_filters( 'redirection_404_data', $insert ); |
| 196 |
if ( $insert ) { |
| 197 |
$wpdb->insert( $wpdb->prefix.'redirection_404', $insert ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
static function delete( $id ) { |
| 202 |
global $wpdb; |
| 203 |
|
| 204 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) ); |
| 205 |
} |
| 206 |
|
| 207 |
static function delete_all() { |
| 208 |
global $wpdb; |
| 209 |
|
| 210 |
$where = array(); |
| 211 |
// if ( isset( $_REQUEST['s'] ) ) |
| 212 |
// $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' ); |
| 213 |
|
| 214 |
$where_cond = ''; |
| 215 |
if ( count( $where ) > 0 ) |
| 216 |
$where_cond = ' WHERE '.implode( ' AND ', $where ); |
| 217 |
|
| 218 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404 ".$where_cond ); |
| 219 |
} |
| 220 |
|
| 221 |
static function export_to_csv() { |
| 222 |
global $wpdb; |
| 223 |
|
| 224 |
$filename = 'redirection-404-'.date_i18n( get_option( 'date_format' ) ).'.csv'; |
| 225 |
|
| 226 |
header( 'Content-Type: text/csv' ); |
| 227 |
header( 'Cache-Control: no-cache, must-revalidate' ); |
| 228 |
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 229 |
header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
| 230 |
|
| 231 |
$stdout = fopen( 'php://output', 'w' ); |
| 232 |
|
| 233 |
fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) ); |
| 234 |
|
| 235 |
$extra = ''; |
| 236 |
$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404"; |
| 237 |
// if ( isset( $_REQUEST['s'] ) ) |
| 238 |
// $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' ); |
| 239 |
|
| 240 |
$total_items = $wpdb->get_var( $sql.$extra ); |
| 241 |
$exported = 0; |
| 242 |
|
| 243 |
while ( $exported < $total_items ) { |
| 244 |
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) ); |
| 245 |
$exported += count( $rows ); |
| 246 |
|
| 247 |
foreach ( $rows as $row ) { |
| 248 |
$csv = array( |
| 249 |
$row->created, |
| 250 |
$row->url, |
| 251 |
long2ip( $row->ip ), |
| 252 |
$row->referrer, |
| 253 |
); |
| 254 |
|
| 255 |
fputcsv( $stdout, $csv ); |
| 256 |
} |
| 257 |
|
| 258 |
if ( count( $rows ) < 100 ) |
| 259 |
break; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
public function to_json() { |
| 264 |
return array( |
| 265 |
'ip' => long2ip( $this->ip ), |
| 266 |
); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
class RE_Filter_Log { |
| 271 |
static function get( $table, $construct, array $params ) { |
| 272 |
global $wpdb; |
| 273 |
|
| 274 |
$orderby = 'id'; |
| 275 |
$direction = 'DESC'; |
| 276 |
$limit = RED_DEFAULT_PER_PAGE; |
| 277 |
$offset = 0; |
| 278 |
$where = ''; |
| 279 |
|
| 280 |
if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'ip', 'url' ), true ) ) { |
| 281 |
$orderby = $params['orderBy']; |
| 282 |
} |
| 283 |
|
| 284 |
if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) { |
| 285 |
$direction = strtoupper( $params['direction'] ); |
| 286 |
} |
| 287 |
|
| 288 |
if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) { |
| 289 |
if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) { |
| 290 |
$where = $wpdb->prepare( "WHERE ip=%s", $params['filter'] ); |
| 291 |
} else { |
| 292 |
$where = $wpdb->prepare( 'WHERE url LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' ); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if ( isset( $params['perPage'] ) ) { |
| 297 |
$limit = intval( $params['perPage'], 10 ); |
| 298 |
$limit = min( 100, $limit ); |
| 299 |
$limit = max( 5, $limit ); |
| 300 |
} |
| 301 |
|
| 302 |
if ( isset( $params['page'] ) ) { |
| 303 |
$offset = intval( $params['page'], 10 ); |
| 304 |
$offset = max( 0, $offset ); |
| 305 |
$offset *= $limit; |
| 306 |
} |
| 307 |
|
| 308 |
$table = $wpdb->prefix.$table; |
| 309 |
$sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit ); |
| 310 |
|
| 311 |
$rows = $wpdb->get_results( $sql ); |
| 312 |
$total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where ); |
| 313 |
$items = array(); |
| 314 |
|
| 315 |
foreach ( $rows as $row ) { |
| 316 |
$item = new $construct( $row ); |
| 317 |
$items[] = array_merge( $item->to_json(), array( |
| 318 |
'id' => intval( $item->id, 10 ), |
| 319 |
'created' => date_i18n( get_option( 'date_format' ), $item->created ).' '.gmdate( get_option( 'time_format' ), $item->created ), |
| 320 |
'url' => $item->url, |
| 321 |
'agent' => $item->agent, |
| 322 |
'referrer' => $item->referrer, |
| 323 |
) ); |
| 324 |
} |
| 325 |
|
| 326 |
return array( |
| 327 |
'items' => $items, |
| 328 |
'total' => intval( $total_items, 10 ), |
| 329 |
); |
| 330 |
} |
| 331 |
} |
| 332 |
|