PluginProbe
Redirection / 2.7
Redirection v2.7
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
← All changes | models/log.php +92 -136 4.22.7 View file →
@@ -10,9 +10,9 @@
10 10 public $redirection_id;
11 11
12 12 function __construct( $values ) {
13 13 foreach ( $values as $key => $value ) {
14 - $this->$key = $value;
14 + $this->$key = $value;
15 15 }
16 16
17 17 $this->created = mysql2date( 'U', $this->created );
18 18 $this->url = stripslashes( $this->url );
@@ -21,31 +21,27 @@
21 21 static function get_by_id( $id ) {
22 22 global $wpdb;
23 23
24 24 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
25 - if ( $row ) {
25 + if ( $row )
26 26 return new RE_Log( $row );
27 - }
28 -
29 27 return false;
30 28 }
31 29
32 - static function create( $url, $target, $agent, $ip, $referrer, $extra = array() ) {
30 + static function create( $url, $target, $agent, $ip, $referrer, $extra = array()) {
33 31 global $wpdb, $redirection;
34 32
35 33 $insert = array(
36 34 'url' => urldecode( $url ),
37 35 'created' => current_time( 'mysql' ),
38 - 'ip' => substr( $ip, 0, 45 ),
36 + 'ip' => $ip,
39 37 );
40 38
41 - if ( ! empty( $agent ) ) {
39 + if ( ! empty( $agent ) )
42 40 $insert['agent'] = $agent;
43 - }
44 41
45 - if ( ! empty( $referrer ) ) {
42 + if ( ! empty( $referrer ) )
46 43 $insert['referrer'] = $referrer;
47 - }
48 44
49 45 $insert['sent_to'] = $target;
50 46 $insert['redirection_id'] = isset( $extra['redirect_id'] ) ? $extra['redirect_id'] : 0;
51 47 $insert['group_id'] = isset( $extra['group_id'] ) ? $extra['group_id'] : 0;
@@ -53,9 +49,9 @@
53 49 $insert = apply_filters( 'redirection_log_data', $insert );
54 50 if ( $insert ) {
55 51 do_action( 'redirection_log', $insert );
56 52
57 - $wpdb->insert( $wpdb->prefix . 'redirection_logs', $insert );
53 + $wpdb->insert( $wpdb->prefix.'redirection_logs', $insert );
58 54 }
59 55
60 56 return $wpdb->insert_id;
61 57 }
@@ -78,42 +74,49 @@
78 74 global $wpdb;
79 75 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) );
80 76 }
81 77
82 - static function delete_all( $filter_by = '', $filter = '' ) {
78 + static function delete_all( $type = 'all', $id = 0 ) {
83 79 global $wpdb;
84 80
85 81 $where = array();
82 + if ( $type === 'module' )
83 + $where[] = $wpdb->prepare( 'module_id=%d', $id );
84 + elseif ( $type === 'group' )
85 + $where[] = $wpdb->prepare( 'group_id=%d AND redirection_id IS NOT NULL', $id );
86 + elseif ( $type === 'redirect' )
87 + $where[] = $wpdb->prepare( 'redirection_id=%d', $id );
86 88
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 - }
89 + // if ( isset( $_REQUEST['s'] ) )
90 + // $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
92 91
93 92 $where_cond = '';
94 - if ( count( $where ) > 0 ) {
95 - $where_cond = ' WHERE ' . implode( ' AND ', $where );
96 - }
93 + if ( count( $where ) > 0 )
94 + $where_cond = ' WHERE '.implode( ' AND ', $where );
97 95
98 - $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs" . $where_cond );
96 + $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs ".$where_cond );
99 97 }
100 98
101 99 static function export_to_csv() {
102 100 global $wpdb;
103 101
104 - $filename = 'redirection-log-' . date_i18n( get_option( 'date_format' ) ) . '.csv';
102 + $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
105 103
106 104 header( 'Content-Type: text/csv' );
107 105 header( 'Cache-Control: no-cache, must-revalidate' );
108 106 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
109 - header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
107 + header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
110 108
111 109 $stdout = fopen( 'php://output', 'w' );
112 110
113 111 fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) );
114 112
115 - $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs" );
113 + $extra = '';
114 + $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs";
115 + // if ( isset( $_REQUEST['s'] ) )
116 + // $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
117 +
118 + $total_items = $wpdb->get_var( $sql.$extra );
116 119 $exported = 0;
117 120
118 121 while ( $exported < $total_items ) {
119 122 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) );
@@ -131,11 +134,10 @@
131 134
132 135 fputcsv( $stdout, $csv );
133 136 }
134 137
135 - if ( count( $rows ) < 100 ) {
138 + if ( count( $rows ) < 100 )
136 139 break;
137 - }
138 140 }
139 141 }
140 142
141 143 public function to_json() {
@@ -155,9 +157,9 @@
155 157 public $ip;
156 158
157 159 function __construct( $values ) {
158 160 foreach ( $values as $key => $value ) {
159 - $this->$key = $value;
161 + $this->$key = $value;
160 162 }
161 163
162 164 $this->created = mysql2date( 'U', $this->created );
163 165 }
@@ -176,31 +178,25 @@
176 178 static function create( $url, $agent, $ip, $referrer ) {
177 179 global $wpdb, $redirection;
178 180
179 181 $insert = array(
180 - 'url' => substr( urldecode( $url ), 0, 255 ),
182 + 'url' => urldecode( $url ),
181 183 'created' => current_time( 'mysql' ),
182 - 'ip' => substr( $ip, 0, 45 ),
184 + 'ip' => ip2long( $ip ),
183 185 );
184 186
185 187 if ( ! empty( $agent ) ) {
186 - $insert['agent'] = substr( $agent, 0, 255 );
188 + $insert['agent'] = $agent;
187 189 }
188 190
189 191 if ( ! empty( $referrer ) ) {
190 - $insert['referrer'] = substr( $referrer, 0, 255 );
192 + $insert['referrer'] = $referrer;
191 193 }
192 194
193 195 $insert = apply_filters( 'redirection_404_data', $insert );
194 196 if ( $insert ) {
195 - $wpdb->insert( $wpdb->prefix . 'redirection_404', $insert );
196 -
197 - if ( $wpdb->insert_id ) {
198 - return $wpdb->insert_id;
199 - }
197 + $wpdb->insert( $wpdb->prefix.'redirection_404', $insert );
200 198 }
201 -
202 - return false;
203 199 }
204 200
205 201 static function delete( $id ) {
206 202 global $wpdb;
@@ -207,44 +203,42 @@
207 203
208 204 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
209 205 }
210 206
211 - static function delete_all( $filter_by = '', $filter = '' ) {
207 + static function delete_all() {
212 208 global $wpdb;
213 209
214 210 $where = array();
211 + // if ( isset( $_REQUEST['s'] ) )
212 + // $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
215 213
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 214 $where_cond = '';
225 - if ( count( $where ) > 0 ) {
226 - $where_cond = ' WHERE ' . implode( ' AND ', $where );
227 - }
215 + if ( count( $where ) > 0 )
216 + $where_cond = ' WHERE '.implode( ' AND ', $where );
228 217
229 - $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404" . $where_cond );
218 + $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404 ".$where_cond );
230 219 }
231 220
232 221 static function export_to_csv() {
233 222 global $wpdb;
234 223
235 - $filename = 'redirection-404-' . date_i18n( get_option( 'date_format' ) ) . '.csv';
224 + $filename = 'redirection-404-'.date_i18n( get_option( 'date_format' ) ).'.csv';
236 225
237 226 header( 'Content-Type: text/csv' );
238 227 header( 'Cache-Control: no-cache, must-revalidate' );
239 228 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
240 - header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
229 + header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
241 230
242 231 $stdout = fopen( 'php://output', 'w' );
243 232
244 - fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer', 'useragent' ) );
233 + fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) );
245 234
246 - $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404" );
235 + $extra = '';
236 + $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404";
237 + // if ( isset( $_REQUEST['s'] ) )
238 + // $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
239 +
240 + $total_items = $wpdb->get_var( $sql.$extra );
247 241 $exported = 0;
248 242
249 243 while ( $exported < $total_items ) {
250 244 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
@@ -253,39 +247,70 @@
253 247 foreach ( $rows as $row ) {
254 248 $csv = array(
255 249 $row->created,
256 250 $row->url,
257 - $row->ip,
251 + long2ip( $row->ip ),
258 252 $row->referrer,
259 - $row->agent,
260 253 );
261 254
262 255 fputcsv( $stdout, $csv );
263 256 }
264 257
265 - if ( count( $rows ) < 100 ) {
258 + if ( count( $rows ) < 100 )
266 259 break;
267 - }
268 260 }
269 261 }
270 262
271 263 public function to_json() {
272 264 return array(
273 - 'ip' => $this->ip,
265 + 'ip' => long2ip( $this->ip ),
274 266 );
275 267 }
276 268 }
277 269
278 270 class RE_Filter_Log {
279 - static public function get( $table, $construct, array $params ) {
271 + static function get( $table, $construct, array $params ) {
280 272 global $wpdb;
281 273
282 - $query = self::get_query( $params );
274 + $orderby = 'id';
275 + $direction = 'DESC';
276 + $limit = RED_DEFAULT_PER_PAGE;
277 + $offset = 0;
278 + $where = '';
283 279
284 - $rows = $wpdb->get_results(
285 - "SELECT * FROM {$wpdb->prefix}$table {$query['where']}" . $wpdb->prepare( ' ORDER BY ' . $query['orderby'] . ' ' . $query['direction'] . ' LIMIT %d,%d', $query['offset'], $query['limit'] )
286 - );
287 - $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}$table " . $query['where'] );
280 + if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'ip', 'url' ), true ) ) {
281 + $orderby = $params['orderBy'];
282 + }
283 +
284 + if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
285 + $direction = strtoupper( $params['direction'] );
286 + }
287 +
288 + if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
289 + if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) {
290 + $where = $wpdb->prepare( "WHERE ip=%s", $params['filter'] );
291 + } else {
292 + $where = $wpdb->prepare( 'WHERE url LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' );
293 + }
294 + }
295 +
296 + if ( isset( $params['perPage'] ) ) {
297 + $limit = intval( $params['perPage'], 10 );
298 + $limit = min( 100, $limit );
299 + $limit = max( 5, $limit );
300 + }
301 +
302 + if ( isset( $params['page'] ) ) {
303 + $offset = intval( $params['page'], 10 );
304 + $offset = max( 0, $offset );
305 + $offset *= $limit;
306 + }
307 +
308 + $table = $wpdb->prefix.$table;
309 + $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit );
310 +
311 + $rows = $wpdb->get_results( $sql );
312 + $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where );
288 313 $items = array();
289 314
290 315 foreach ( $rows as $row ) {
291 316 $item = new $construct( $row );
@@ -290,10 +315,9 @@
290 315 foreach ( $rows as $row ) {
291 316 $item = new $construct( $row );
292 317 $items[] = array_merge( $item->to_json(), array(
293 318 'id' => intval( $item->id, 10 ),
294 - 'created' => date_i18n( get_option( 'date_format' ), $item->created ),
295 - 'created_time' => gmdate( get_option( 'time_format' ), $item->created ),
319 + 'created' => date_i18n( get_option( 'date_format' ), $item->created ).' '.gmdate( get_option( 'time_format' ), $item->created ),
296 320 'url' => $item->url,
297 321 'agent' => $item->agent,
298 322 'referrer' => $item->referrer,
299 323 ) );
@@ -302,74 +326,6 @@
302 326 return array(
303 327 'items' => $items,
304 328 'total' => intval( $total_items, 10 ),
305 329 );
306 - }
307 -
308 - static public function get_grouped( $table, $group, array $params ) {
309 - global $wpdb;
310 -
311 - $query = self::get_query( $params );
312 -
313 - if ( ! in_array( $group, array( 'ip', 'url' ), true ) ) {
314 - $group = 'url';
315 - }
316 -
317 - $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'] );
318 - $rows = $wpdb->get_results( $sql );
319 - $total_items = $wpdb->get_var( "SELECT COUNT(DISTINCT $group) FROM {$wpdb->prefix}$table" );
320 -
321 - foreach ( $rows as $row ) {
322 - $row->count = intval( $row->count, 10 );
323 - $row->id = isset( $row->url ) ? $row->url : $row->ip;
324 - }
325 -
326 - return array(
327 - 'items' => $rows,
328 - 'total' => intval( $total_items, 10 ),
329 - );
330 - }
331 -
332 - static private function get_query( array $params ) {
333 - global $wpdb;
334 -
335 - $query = array(
336 - 'orderby' => 'id',
337 - 'direction' => 'DESC',
338 - 'limit' => RED_DEFAULT_PER_PAGE,
339 - 'offset' => 0,
340 - 'where' => '',
341 - );
342 -
343 - if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'ip', 'url' ), true ) ) {
344 - $query['orderby'] = $params['orderby'];
345 - }
346 -
347 - if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
348 - $query['direction'] = strtoupper( $params['direction'] );
349 - }
350 -
351 - if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
352 - if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'ip' ) {
353 - $query['where'] = $wpdb->prepare( 'WHERE ip=%s', $params['filter'] );
354 - } elseif ( isset( $params['filterBy'] ) && $params['filterBy'] === 'url-exact' ) {
355 - $query['where'] = $wpdb->prepare( 'WHERE url=%s', $params['filter'] );
356 - } else {
357 - $query['where'] = $wpdb->prepare( 'WHERE url LIKE %s', '%' . $wpdb->esc_like( trim( $params['filter'] ) ) . '%' );
358 - }
359 - }
360 -
361 - if ( isset( $params['per_page'] ) ) {
362 - $query['limit'] = intval( $params['per_page'], 10 );
363 - $query['limit'] = min( RED_MAX_PER_PAGE, $query['limit'] );
364 - $query['limit'] = max( 5, $query['limit'] );
365 - }
366 -
367 - if ( isset( $params['page'] ) ) {
368 - $query['offset'] = intval( $params['page'], 10 );
369 - $query['offset'] = max( 0, $query['offset'] );
370 - $query['offset'] *= $query['limit'];
371 - }
372 -
373 - return $query;
374 330 }
375 331 }