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

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