PluginProbe
Redirection / 4.2
Redirection v4.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / log.php

log.php in Redirection 4.2, at models/log.php

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