PluginProbe
Redirection / 4.0
Redirection v4.0
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.0, at models/log.php

375 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' ) );
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 );
260
261 fputcsv( $stdout, $csv );
262 }
263
264 if ( count( $rows ) < 100 ) {
265 break;
266 }
267 }
268 }
269
270 public function to_json() {
271 return array(
272 'ip' => $this->ip,
273 );
274 }
275 }
276
277 class RE_Filter_Log {
278 static public function get( $table, $construct, array $params ) {
279 global $wpdb;
280
281 $query = self::get_query( $params );
282
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'] );
287 $items = array();
288
289 foreach ( $rows as $row ) {
290 $item = new $construct( $row );
291 $items[] = array_merge( $item->to_json(), array(
292 '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 ),
295 'url' => $item->url,
296 'agent' => $item->agent,
297 'referrer' => $item->referrer,
298 ) );
299 }
300
301 return array(
302 'items' => $items,
303 'total' => intval( $total_items, 10 ),
304 );
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 }
374 }
375