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

329 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 return false;
28 }
29
30 static function create( $url, $target, $agent, $ip, $referrer, $extra = array()) {
31 global $wpdb, $redirection;
32
33 $insert = array(
34 'url' => urldecode( $url ),
35 'created' => current_time( 'mysql' ),
36 'ip' => $ip,
37 );
38
39 if ( ! empty( $agent ) )
40 $insert['agent'] = $agent;
41
42 if ( ! empty( $referrer ) )
43 $insert['referrer'] = $referrer;
44
45 $insert['sent_to'] = $target;
46 $insert['redirection_id'] = isset( $extra['redirect_id'] ) ? $extra['redirect_id'] : 0;
47 $insert['group_id'] = isset( $extra['group_id'] ) ? $extra['group_id'] : 0;
48
49 $insert = apply_filters( 'redirection_log_data', $insert );
50 do_action( 'redirection_log', $insert );
51
52 $wpdb->insert( $wpdb->prefix.'redirection_logs', $insert );
53
54 return $wpdb->insert_id;
55 }
56
57 static function show_url( $url ) {
58 return $url;
59 }
60
61 static function delete( $id ) {
62 global $wpdb;
63 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
64 }
65
66 static function delete_for_id( $id ) {
67 global $wpdb;
68 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE redirection_id=%d", $id ) );
69 }
70
71 static function delete_for_group( $id ) {
72 global $wpdb;
73 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) );
74 }
75
76 static function delete_all( $type = 'all', $id = 0 ) {
77 global $wpdb;
78
79 $where = array();
80 if ( $type === 'module' )
81 $where[] = $wpdb->prepare( 'module_id=%d', $id );
82 elseif ( $type === 'group' )
83 $where[] = $wpdb->prepare( 'group_id=%d AND redirection_id IS NOT NULL', $id );
84 elseif ( $type === 'redirect' )
85 $where[] = $wpdb->prepare( 'redirection_id=%d', $id );
86
87 // if ( isset( $_REQUEST['s'] ) )
88 // $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
89
90 $where_cond = '';
91 if ( count( $where ) > 0 )
92 $where_cond = ' WHERE '.implode( ' AND ', $where );
93
94 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs ".$where_cond );
95 }
96
97 static function export_to_csv() {
98 global $wpdb;
99
100 $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
101
102 header( 'Content-Type: text/csv' );
103 header( 'Cache-Control: no-cache, must-revalidate' );
104 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
105 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
106
107 $stdout = fopen( 'php://output', 'w' );
108
109 fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) );
110
111 $extra = '';
112 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs";
113 // if ( isset( $_REQUEST['s'] ) )
114 // $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
115
116 $total_items = $wpdb->get_var( $sql.$extra );
117 $exported = 0;
118
119 while ( $exported < $total_items ) {
120 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) );
121 $exported += count( $rows );
122
123 foreach ( $rows as $row ) {
124 $csv = array(
125 $row->created,
126 $row->url,
127 $row->sent_to,
128 $row->ip,
129 $row->referrer,
130 $row->agent,
131 );
132
133 fputcsv( $stdout, $csv );
134 }
135
136 if ( count( $rows ) < 100 )
137 break;
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' => urldecode( $url ),
181 'created' => current_time( 'mysql' ),
182 'ip' => ip2long( $ip ),
183 );
184
185 if ( ! empty( $agent ) ) {
186 $insert['agent'] = $agent;
187 }
188
189 if ( ! empty( $referrer ) ) {
190 $insert['referrer'] = $referrer;
191 }
192
193 $insert = apply_filters( 'redirection_404_data', $insert );
194
195 $wpdb->insert( $wpdb->prefix.'redirection_404', $insert );
196 }
197
198 static function delete( $id ) {
199 global $wpdb;
200
201 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
202 }
203
204 static function delete_all() {
205 global $wpdb;
206
207 $where = array();
208 // if ( isset( $_REQUEST['s'] ) )
209 // $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
210
211 $where_cond = '';
212 if ( count( $where ) > 0 )
213 $where_cond = ' WHERE '.implode( ' AND ', $where );
214
215 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404 ".$where_cond );
216 }
217
218 static function export_to_csv() {
219 global $wpdb;
220
221 $filename = 'redirection-404-'.date_i18n( get_option( 'date_format' ) ).'.csv';
222
223 header( 'Content-Type: text/csv' );
224 header( 'Cache-Control: no-cache, must-revalidate' );
225 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
226 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
227
228 $stdout = fopen( 'php://output', 'w' );
229
230 fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) );
231
232 $extra = '';
233 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404";
234 // if ( isset( $_REQUEST['s'] ) )
235 // $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
236
237 $total_items = $wpdb->get_var( $sql.$extra );
238 $exported = 0;
239
240 while ( $exported < $total_items ) {
241 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
242 $exported += count( $rows );
243
244 foreach ( $rows as $row ) {
245 $csv = array(
246 $row->created,
247 $row->url,
248 long2ip( $row->ip ),
249 $row->referrer,
250 );
251
252 fputcsv( $stdout, $csv );
253 }
254
255 if ( count( $rows ) < 100 )
256 break;
257 }
258 }
259
260 public function to_json() {
261 return array(
262 'ip' => long2ip( $this->ip ),
263 );
264 }
265 }
266
267 class RE_Filter_Log {
268 static function get( $table, $construct, array $params ) {
269 global $wpdb;
270
271 $orderby = 'id';
272 $direction = 'DESC';
273 $limit = 20;
274 $offset = 0;
275 $where = '';
276
277 if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'ip', 'url' ), true ) ) {
278 $orderby = $params['orderBy'];
279 }
280
281 if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
282 $direction = strtoupper( $params['direction'] );
283 }
284
285 if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
286 if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) {
287 $where = $wpdb->prepare( "WHERE ip=%s", $params['filter'] );
288 } else {
289 $where = $wpdb->prepare( 'WHERE url LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' );
290 }
291 }
292
293 if ( isset( $params['perPage'] ) ) {
294 $limit = intval( $params['perPage'], 10 );
295 $limit = min( 100, $limit );
296 $limit = max( 10, $limit );
297 }
298
299 if ( isset( $params['page'] ) ) {
300 $offset = intval( $params['page'], 10 );
301 $offset = max( 0, $offset );
302 $offset *= $limit;
303 }
304
305 $table = $wpdb->prefix.$table;
306 $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit );
307
308 $rows = $wpdb->get_results( $sql );
309 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where );
310 $items = array();
311
312 foreach ( $rows as $row ) {
313 $item = new $construct( $row );
314 $items[] = array_merge( $item->to_json(), array(
315 'id' => intval( $item->id, 10 ),
316 'created' => date_i18n( get_option( 'date_format' ), $item->created ).' '.gmdate( get_option( 'time_format' ), $item->created ),
317 'url' => $item->url,
318 'agent' => $item->agent,
319 'referrer' => $item->referrer,
320 ) );
321 }
322
323 return array(
324 'items' => $items,
325 'total' => intval( $total_items, 10 ),
326 );
327 }
328 }
329