| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Redirect logging. Extends the base log class with specifics for redirects |
| 5 |
*/ |
| 6 |
class Red_Redirect_Log extends Red_Log { |
| 7 |
/** |
| 8 |
* The redirect associated with this log entry. |
| 9 |
* |
| 10 |
* @var integer |
| 11 |
*/ |
| 12 |
protected $redirection_id = 0; |
| 13 |
|
| 14 |
/** |
| 15 |
* The URL the client was redirected to. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $sent_to = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* Who redirected this URL? |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $redirect_by = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get's the table name for this log object |
| 30 |
* |
| 31 |
* @param Object $wpdb WPDB object. |
| 32 |
* @return String |
| 33 |
*/ |
| 34 |
protected static function get_table_name( $wpdb ) { |
| 35 |
return "{$wpdb->prefix}redirection_logs"; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Create a redirect log entry |
| 40 |
* |
| 41 |
* @param string $domain Domain name of request. |
| 42 |
* @param string $url URL of request. |
| 43 |
* @param string $ip IP of client. |
| 44 |
* @param array $details Other log details. |
| 45 |
* @return integer|false Log ID, or false |
| 46 |
*/ |
| 47 |
public static function create( $domain, $url, $ip, $details ) { |
| 48 |
global $wpdb; |
| 49 |
|
| 50 |
$insert = self::sanitize_create( $domain, $url, $ip, $details ); |
| 51 |
$insert['redirection_id'] = 0; |
| 52 |
|
| 53 |
if ( isset( $details['redirect_id'] ) ) { |
| 54 |
$insert['redirection_id'] = intval( $details['redirect_id'], 10 ); |
| 55 |
} |
| 56 |
|
| 57 |
if ( isset( $details['target'] ) ) { |
| 58 |
$insert['sent_to'] = $details['target']; |
| 59 |
} |
| 60 |
|
| 61 |
if ( isset( $details['redirect_by'] ) ) { |
| 62 |
$insert['redirect_by'] = strtolower( substr( $details['redirect_by'], 0, 50 ) ); |
| 63 |
} |
| 64 |
|
| 65 |
$insert = apply_filters( 'redirection_log_data', $insert ); |
| 66 |
if ( $insert ) { |
| 67 |
do_action( 'redirection_log', $insert ); |
| 68 |
|
| 69 |
$wpdb->insert( $wpdb->prefix . 'redirection_logs', $insert ); |
| 70 |
if ( $wpdb->insert_id ) { |
| 71 |
return $wpdb->insert_id; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get query filters as a SQL `WHERE` statement. SQL will be sanitized |
| 80 |
* |
| 81 |
* @param array $filter Array of filter params. |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
protected static function get_query_filter( array $filter ) { |
| 85 |
global $wpdb; |
| 86 |
|
| 87 |
$where = parent::get_query_filter( $filter ); |
| 88 |
|
| 89 |
if ( isset( $filter['target'] ) ) { |
| 90 |
$where[] = $wpdb->prepare( 'sent_to LIKE %s', '%' . $wpdb->esc_like( trim( $filter['target'] ) ) . '%' ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( isset( $filter['redirect_by'] ) ) { |
| 94 |
$where[] = $wpdb->prepare( 'redirect_by = %s', $filter['redirect_by'] ); |
| 95 |
} |
| 96 |
|
| 97 |
return $where; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the CSV filename for this log object |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public static function get_csv_filename() { |
| 106 |
return 'redirection-log'; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get the CSV headers for this log object |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public static function get_csv_header() { |
| 115 |
return [ 'date', 'source', 'target', 'ip', 'referrer', 'agent' ]; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get the CSV headers for this log object |
| 120 |
* |
| 121 |
* @param object $row Log row. |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public static function get_csv_row( $row ) { |
| 125 |
return [ |
| 126 |
$row->created, |
| 127 |
$row->url, |
| 128 |
$row->sent_to, |
| 129 |
$row->ip, |
| 130 |
$row->referrer, |
| 131 |
$row->agent, |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get a displayable name for the originator of the redirect. |
| 137 |
* |
| 138 |
* @param string $agent Redirect agent. |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
private function get_redirect_name( $agent ) { |
| 142 |
// phpcs:ignore |
| 143 |
if ( $agent === 'wordpress' ) { |
| 144 |
return 'WordPress'; |
| 145 |
} |
| 146 |
|
| 147 |
return ucwords( $agent ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Convert a log entry to JSON |
| 152 |
* |
| 153 |
* @return Array |
| 154 |
*/ |
| 155 |
public function to_json() { |
| 156 |
return array_merge( parent::to_json(), [ |
| 157 |
'sent_to' => $this->sent_to, |
| 158 |
'redirection_id' => intval( $this->redirection_id, 10 ), |
| 159 |
'redirect_by_slug' => $this->redirect_by, |
| 160 |
'redirect_by' => $this->get_redirect_name( $this->redirect_by ), |
| 161 |
] ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// phpcs:ignore |
| 166 |
class RE_Log { |
| 167 |
public static function create( $url, $target, $agent, $ip, $referrer, $extra = array() ) { |
| 168 |
_deprecated_function( __FUNCTION__, '4.6', 'Red_Redirect_Log::create( $domain, $url, $ip, $details )' ); |
| 169 |
|
| 170 |
return Red_Redirect_Log::create( Redirection_Request::get_server(), $url, $ip, array_merge( [ |
| 171 |
'agent' => $agent, |
| 172 |
'referrer' => $referrer, |
| 173 |
'target' => $target, |
| 174 |
'request_method' => Redirection_Request::get_request_method(), |
| 175 |
], $extra ) ); |
| 176 |
} |
| 177 |
} |
| 178 |
|