PluginProbe
Redirection / 2.9.2
Redirection v2.9.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 2.9.2, at models/log.php

345 lines 8.6 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' => $ip,
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' => ip2long( $ip ),
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=INET_ATON(%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 // if ( isset( $_REQUEST['s'] ) )
251 // $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
252
253 $total_items = $wpdb->get_var( $sql.$extra );
254 $exported = 0;
255
256 while ( $exported < $total_items ) {
257 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
258 $exported += count( $rows );
259
260 foreach ( $rows as $row ) {
261 $csv = array(
262 $row->created,
263 $row->url,
264 long2ip( $row->ip ),
265 $row->referrer,
266 );
267
268 fputcsv( $stdout, $csv );
269 }
270
271 if ( count( $rows ) < 100 )
272 break;
273 }
274 }
275
276 public function to_json() {
277 return array(
278 'ip' => long2ip( $this->ip ),
279 );
280 }
281 }
282
283 class RE_Filter_Log {
284 static function get( $table, $construct, array $params ) {
285 global $wpdb;
286
287 $orderby = 'id';
288 $direction = 'DESC';
289 $limit = RED_DEFAULT_PER_PAGE;
290 $offset = 0;
291 $where = '';
292
293 if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'ip', 'url' ), true ) ) {
294 $orderby = $params['orderBy'];
295 }
296
297 if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
298 $direction = strtoupper( $params['direction'] );
299 }
300
301 if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
302 if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) {
303 $where = $wpdb->prepare( "WHERE ip=%s", $params['filter'] );
304 } else {
305 $where = $wpdb->prepare( 'WHERE url LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' );
306 }
307 }
308
309 if ( isset( $params['perPage'] ) ) {
310 $limit = intval( $params['perPage'], 10 );
311 $limit = min( RED_MAX_PER_PAGE, $limit );
312 $limit = max( 5, $limit );
313 }
314
315 if ( isset( $params['page'] ) ) {
316 $offset = intval( $params['page'], 10 );
317 $offset = max( 0, $offset );
318 $offset *= $limit;
319 }
320
321 $table = $wpdb->prefix.$table;
322 $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit );
323
324 $rows = $wpdb->get_results( $sql );
325 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where );
326 $items = array();
327
328 foreach ( $rows as $row ) {
329 $item = new $construct( $row );
330 $items[] = array_merge( $item->to_json(), array(
331 'id' => intval( $item->id, 10 ),
332 'created' => date_i18n( get_option( 'date_format' ), $item->created ).' '.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