PluginProbe
Redirection / 3.1.1
Redirection v3.1.1
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 3.1.1, at models/log.php

344 lines 8.4 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( $filterBy = '', $filter = '' ) {
83 global $wpdb;
84
85 $where = array();
86
87 if ( $filterBy === 'url' && $filter ) {
88 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $filter ).'%' );
89 } else if ( $filterBy === '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 $extra = '';
116 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs";
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' => substr( urldecode( $url ), 0, 255 ),
183 'created' => current_time( 'mysql' ),
184 'ip' => substr( $ip, 0, 45 ),
185 );
186
187 if ( ! empty( $agent ) ) {
188 $insert['agent'] = substr( $agent, 0, 255 );
189 }
190
191 if ( ! empty( $referrer ) ) {
192 $insert['referrer'] = substr( $referrer, 0, 255 );
193 }
194
195 $insert = apply_filters( 'redirection_404_data', $insert );
196 if ( $insert ) {
197 $wpdb->insert( $wpdb->prefix.'redirection_404', $insert );
198
199 if ( $wpdb->insert_id ) {
200 return $wpdb->insert_id;
201 }
202 }
203
204 return false;
205 }
206
207 static function delete( $id ) {
208 global $wpdb;
209
210 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
211 }
212
213 static function delete_all( $filterBy = '', $filter = '' ) {
214 global $wpdb;
215
216 $where = array();
217
218 if ( $filterBy === 'url-exact' ) {
219 $where[] = $wpdb->prepare( 'url=%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=%s', $filter );
224 }
225
226 $where_cond = '';
227 if ( count( $where ) > 0 ) {
228 $where_cond = ' WHERE '.implode( ' AND ', $where );
229 }
230
231 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404".$where_cond );
232 }
233
234 static function export_to_csv() {
235 global $wpdb;
236
237 $filename = 'redirection-404-'.date_i18n( get_option( 'date_format' ) ).'.csv';
238
239 header( 'Content-Type: text/csv' );
240 header( 'Cache-Control: no-cache, must-revalidate' );
241 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
242 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
243
244 $stdout = fopen( 'php://output', 'w' );
245
246 fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) );
247
248 $extra = '';
249 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404";
250
251 $total_items = $wpdb->get_var( $sql.$extra );
252 $exported = 0;
253
254 while ( $exported < $total_items ) {
255 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
256 $exported += count( $rows );
257
258 foreach ( $rows as $row ) {
259 $csv = array(
260 $row->created,
261 $row->url,
262 $row->ip,
263 $row->referrer,
264 );
265
266 fputcsv( $stdout, $csv );
267 }
268
269 if ( count( $rows ) < 100 )
270 break;
271 }
272 }
273
274 public function to_json() {
275 return array(
276 'ip' => $this->ip,
277 );
278 }
279 }
280
281 class RE_Filter_Log {
282 static function get( $table, $construct, array $params ) {
283 global $wpdb;
284
285 $orderby = 'id';
286 $direction = 'DESC';
287 $limit = RED_DEFAULT_PER_PAGE;
288 $offset = 0;
289 $where = '';
290
291 if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'ip', 'url' ), true ) ) {
292 $orderby = $params['orderby'];
293 }
294
295 if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
296 $direction = strtoupper( $params['direction'] );
297 }
298
299 if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
300 if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) {
301 $where = $wpdb->prepare( "WHERE ip=%s", $params['filter'] );
302 } else {
303 $where = $wpdb->prepare( 'WHERE url LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' );
304 }
305 }
306
307 if ( isset( $params['per_page'] ) ) {
308 $limit = intval( $params['per_page'], 10 );
309 $limit = min( RED_MAX_PER_PAGE, $limit );
310 $limit = max( 5, $limit );
311 }
312
313 if ( isset( $params['page'] ) ) {
314 $offset = intval( $params['page'], 10 );
315 $offset = max( 0, $offset );
316 $offset *= $limit;
317 }
318
319 $table = $wpdb->prefix.$table;
320 $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit );
321
322 $rows = $wpdb->get_results( $sql );
323 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where );
324 $items = array();
325
326 foreach ( $rows as $row ) {
327 $item = new $construct( $row );
328 $items[] = array_merge( $item->to_json(), array(
329 'id' => intval( $item->id, 10 ),
330 'created' => date_i18n( get_option( 'date_format' ), $item->created ),
331 'created_time' => gmdate( get_option( 'time_format' ), $item->created ),
332 'url' => $item->url,
333 'agent' => $item->agent,
334 'referrer' => $item->referrer,
335 ) );
336 }
337
338 return array(
339 'items' => $items,
340 'total' => intval( $total_items, 10 ),
341 );
342 }
343 }
344