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

245 lines 6.3 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
55 static function show_url( $url ) {
56 return implode( '&#8203;/', explode( '/', substr( $url, 0, 80 ) ) ).( strlen( $url ) > 80 ? '...' : '' );
57 }
58
59 static function delete( $id ) {
60 global $wpdb;
61 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
62 }
63
64 static function delete_for_id( $id ) {
65 global $wpdb;
66 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE redirection_id=%d", $id ) );
67 }
68
69 static function delete_for_group( $id ) {
70 global $wpdb;
71 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) );
72 }
73
74 static function delete_all( $type = 'all', $id = 0 ) {
75 global $wpdb;
76
77 $where = array();
78 if ( $type === 'module' )
79 $where[] = $wpdb->prepare( 'module_id=%d', $id );
80 elseif ( $type === 'group' )
81 $where[] = $wpdb->prepare( 'group_id=%d AND redirection_id IS NOT NULL', $id );
82 elseif ( $type === 'redirect' )
83 $where[] = $wpdb->prepare( 'redirection_id=%d', $id );
84
85 if ( isset( $_REQUEST['s'] ) )
86 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
87
88 $where_cond = '';
89 if ( count( $where ) > 0 )
90 $where_cond = ' WHERE '.implode( ' AND ', $where );
91
92 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs ".$where_cond );
93 }
94
95 static function export_to_csv() {
96 global $wpdb;
97
98 $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
99
100 header( 'Content-Type: text/csv' );
101 header( 'Cache-Control: no-cache, must-revalidate' );
102 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
103 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
104
105 $stdout = fopen( 'php://output', 'w' );
106
107 fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) );
108
109 $extra = '';
110 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs";
111 if ( isset( $_REQUEST['s'] ) )
112 $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
113
114 $total_items = $wpdb->get_var( $sql.$extra );
115 $exported = 0;
116
117 while ( $exported < $total_items ) {
118 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) );
119 $exported += count( $rows );
120
121 foreach ( $rows as $row ) {
122 $csv = array(
123 $row->created,
124 $row->url,
125 $row->sent_to,
126 $row->ip,
127 $row->referrer,
128 $row->agent,
129 );
130
131 fputcsv( $stdout, $csv );
132 }
133
134 if ( count( $rows ) < 100 )
135 break;
136 }
137 }
138 }
139
140 class RE_404 {
141 public $id;
142 public $created;
143 public $url;
144 public $agent;
145 public $referrer;
146 public $ip;
147
148 function __construct( $values ) {
149 foreach ( $values as $key => $value ) {
150 $this->$key = $value;
151 }
152
153 $this->created = mysql2date( 'U', $this->created );
154 }
155
156 static function get_by_id( $id ) {
157 global $wpdb;
158
159 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
160 if ( $row )
161 return new RE_404( $row );
162 return false;
163 }
164
165 static function create( $url, $agent, $ip, $referrer ) {
166 global $wpdb, $redirection;
167
168 $insert = array(
169 'url' => urldecode( $url ),
170 'created' => current_time( 'mysql' ),
171 'ip' => ip2long( $ip ),
172 );
173
174 if ( ! empty( $agent ) )
175 $insert['agent'] = $agent;
176
177 if ( ! empty( $referrer ) )
178 $insert['referrer'] = $referrer;
179
180 $wpdb->insert( $wpdb->prefix.'redirection_404', $insert );
181 }
182
183 static function delete( $id ) {
184 global $wpdb;
185
186 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
187 }
188
189 static function delete_all() {
190 global $wpdb;
191
192 $where = array();
193 if ( isset( $_REQUEST['s'] ) )
194 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
195
196 $where_cond = '';
197 if ( count( $where ) > 0 )
198 $where_cond = ' WHERE '.implode( ' AND ', $where );
199
200 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404 ".$where_cond );
201 }
202
203 static function export_to_csv() {
204 global $wpdb;
205
206 $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
207
208 header( 'Content-Type: text/csv' );
209 header( 'Cache-Control: no-cache, must-revalidate' );
210 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
211 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
212
213 $stdout = fopen( 'php://output', 'w' );
214
215 fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) );
216
217 $extra = '';
218 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404";
219 if ( isset( $_REQUEST['s'] ) )
220 $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
221
222 $total_items = $wpdb->get_var( $sql.$extra );
223 $exported = 0;
224
225 while ( $exported < $total_items ) {
226 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
227 $exported += count( $rows );
228
229 foreach ( $rows as $row ) {
230 $csv = array(
231 $row->created,
232 $row->url,
233 long2ip( $row->ip ),
234 $row->referrer,
235 );
236
237 fputcsv( $stdout, $csv );
238 }
239
240 if ( count( $rows ) < 100 )
241 break;
242 }
243 }
244 }
245