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

272 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Redirection
4 *
5 * @package Redirection
6 * @author John Godley
7 * @copyright Copyright (C) John Godley
8 **/
9
10 /*
11 ============================================================================================================
12 This software is provided "as is" and any express or implied warranties, including, but not limited to, the
13 implied warranties of merchantibility and fitness for a particular purpose are disclaimed. In no event shall
14 the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or
15 consequential damages (including, but not limited to, procurement of substitute goods or services; loss of
16 use, data, or profits; or business interruption) however caused and on any theory of liability, whether in
17 contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of
18 this software, even if advised of the possibility of such damage.
19
20 For full license details see license.txt
21 ============================================================================================================ */
22
23 class RE_Log {
24 var $id;
25 var $created;
26 var $url;
27 var $agent;
28 var $referrer;
29 var $ip;
30 var $redirection_id;
31
32 function RE_Log( $values ) {
33 foreach ( $values AS $key => $value ) {
34 $this->$key = $value;
35 }
36
37 $this->created = mysql2date( 'U', $this->created );
38 $this->url = stripslashes( $this->url );
39 }
40
41 static function get_by_id( $id ) {
42 global $wpdb;
43
44 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
45 if ( $row )
46 return new RE_Log( $row );
47 return false;
48 }
49
50 static function create( $url, $target, $agent, $ip, $referrer, $extra = array()) {
51 global $wpdb, $redirection;
52
53 $insert = array(
54 'url' => urldecode( $url ),
55 'created' => current_time( 'mysql' ),
56 'ip' => $ip,
57 );
58
59 if ( !empty( $agent ) )
60 $insert['agent'] = $agent;
61
62 if ( !empty( $referrer ) )
63 $insert['referrer'] = $referrer;
64
65 $insert['sent_to'] = $target;
66 $insert['redirection_id'] = isset( $extra['redirect_id'] ) ? $extra['redirect_id'] : 0;
67 $insert['module_id'] = isset( $extra['module_id'] ) ? $extra['module_id'] : 0;
68 $insert['group_id'] = isset( $extra['group_id'] ) ? $extra['group_id'] : 0;
69
70 $insert = apply_filters( 'redirection_log_data', $insert );
71 do_action( 'redirection_log', $insert );
72
73 $wpdb->insert( $wpdb->prefix.'redirection_logs', $insert );
74 }
75
76 static function show_url( $url ) {
77 return implode('&#8203;/', explode( '/', substr( $url, 0, 80 ) ) ).( strlen( $url ) > 80 ? '...' : '' );
78 }
79
80 static function delete( $id ) {
81 global $wpdb;
82 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
83 }
84
85 static function delete_for_id( $id ) {
86 global $wpdb;
87 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE redirection_id=%d", $id ) );
88 }
89
90 static function delete_for_group( $id ) {
91 global $wpdb;
92 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) );
93 }
94
95 static function delete_for_module( $id ) {
96 global $wpdb;
97 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE module_id=%d", $id ) );
98 }
99
100 static function delete_all( $type = 'all', $id = 0 ) {
101 global $wpdb;
102
103 $where = array();
104 if ( $type == 'module' )
105 $where[] = $wpdb->prepare( 'module_id=%d', $id );
106 elseif ( $type == 'group' )
107 $where[] = $wpdb->prepare( 'group_id=%d AND redirection_id IS NOT NULL', $id );
108 elseif ( $type == 'redirect' )
109 $where[] = $wpdb->prepare( 'redirection_id=%d', $id );
110
111 if ( isset( $_REQUEST['s'] ) )
112 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.like_escape( $_REQUEST['s'] ).'%' );
113
114 $where_cond = "";
115 if ( count( $where ) > 0 )
116 $where_cond = " WHERE ".implode( ' AND ', $where );
117
118 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs ".$where_cond );
119 }
120
121 static function export_to_csv() {
122 global $wpdb;
123
124 $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
125
126 header( 'Content-Type: text/csv' );
127 header( 'Cache-Control: no-cache, must-revalidate' );
128 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
129 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
130
131 $stdout = fopen( 'php://output', 'w' );
132
133 fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) );
134
135 $extra = '';
136 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs";
137 if ( isset( $_REQUEST['s'] ) )
138 $extra = $wpdb->prepare( " WHERE url LIKE %s", '%'.like_escape( $_REQUEST['s'] ).'%' );
139
140 $total_items = $wpdb->get_var( $sql.$extra );
141 $exported = 0;
142
143 while ( $exported < $total_items ) {
144 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) );
145 $exported += count( $rows );
146
147 foreach ( $rows AS $row ) {
148 $csv = array(
149 $row->created,
150 $row->url,
151 $row->sent_to,
152 $row->ip,
153 $row->referrer,
154 $row->agent,
155 );
156
157 fputcsv( $stdout, $csv );
158 }
159
160 if ( count( $rows ) < 100 )
161 break;
162 }
163 }
164 }
165
166 class RE_404 {
167 var $id;
168 var $created;
169 var $url;
170 var $agent;
171 var $referrer;
172 var $ip;
173
174 function RE_404( $values ) {
175 foreach ( $values AS $key => $value ) {
176 $this->$key = $value;
177 }
178
179 $this->created = mysql2date ('U', $this->created);
180 }
181
182 static function get_by_id( $id ) {
183 global $wpdb;
184
185 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
186 if ( $row )
187 return new RE_404( $row );
188 return false;
189 }
190
191 static function create( $url, $agent, $ip, $referrer ) {
192 global $wpdb, $redirection;
193
194 $insert = array(
195 'url' => urldecode( $url ),
196 'created' => current_time( 'mysql' ),
197 'ip' => ip2long( $ip ),
198 );
199
200 if ( !empty( $agent ) )
201 $insert['agent'] = $agent;
202
203 if ( !empty( $referrer ) )
204 $insert['referrer'] = $referrer;
205
206 $wpdb->insert( $wpdb->prefix.'redirection_404', $insert );
207 }
208
209 static function delete( $id ) {
210 global $wpdb;
211
212 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
213 }
214
215 static function delete_all() {
216 global $wpdb;
217
218 $where = array();
219 if ( isset( $_REQUEST['s'] ) )
220 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.like_escape( $_REQUEST['s'] ).'%' );
221
222 $where_cond = "";
223 if ( count( $where ) > 0 )
224 $where_cond = " WHERE ".implode( ' AND ', $where );
225
226 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404 ".$where_cond );
227 }
228
229 static function export_to_csv() {
230 global $wpdb;
231
232 $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
233
234 header( 'Content-Type: text/csv' );
235 header( 'Cache-Control: no-cache, must-revalidate' );
236 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
237 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
238
239 $stdout = fopen( 'php://output', 'w' );
240
241 fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) );
242
243 $extra = '';
244 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404";
245 if ( isset( $_REQUEST['s'] ) )
246 $extra = $wpdb->prepare( " WHERE url LIKE %s", '%'.like_escape( $_REQUEST['s'] ).'%' );
247
248 $total_items = $wpdb->get_var( $sql.$extra );
249 $exported = 0;
250
251 while ( $exported < $total_items ) {
252 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
253 $exported += count( $rows );
254
255 foreach ( $rows AS $row ) {
256 $csv = array(
257 $row->created,
258 $row->url,
259 long2ip( $row->ip ),
260 $row->referrer,
261 );
262
263 fputcsv( $stdout, $csv );
264 }
265
266 if ( count( $rows ) < 100 )
267 break;
268 }
269 }
270 }
271
272