PluginProbe
Redirection / 2.2.2
Redirection v2.2.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 / redirect.php

redirect.php in Redirection 2.2.2, at models/redirect.php

410 lines 13.1 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 class Red_Item {
23 var $id = null;
24 var $url = null;
25 var $regex = false;
26 var $action_data = null;
27
28 var $last_access = null;
29 var $last_count = 0;
30
31 var $tracking = true;
32
33 function Red_Item( $values, $type = '', $match = '' ) {
34 if ( is_object( $values ) ) {
35 foreach ( $values AS $key => $value ) {
36 $this->$key = $value;
37 }
38
39 if ( $this->match_type ) {
40 $this->match = Red_Match::create( $this->match_type, $this->action_data);
41 $this->match->id = $this->id;
42 $this->match->action_code = $this->action_code;
43 }
44
45 if ( $this->action_type ) {
46 $this->action = Red_Action::create( $this->action_type, $this->action_code);
47 $this->match->action = $this->action;
48 }
49 else
50 $this->action = Red_Action::create( 'nothing', 0 );
51
52 if ( $this->last_access == '0000-00-00 00:00:00' )
53 $this->last_access = 0;
54 else
55 $this->last_access = mysql2date( 'U', $this->last_access);
56 }
57 else {
58 $this->url = $values;
59 $this->type = $type;
60 $this->match = $match;
61 }
62 }
63
64 function get_all_for_module( $module ) {
65 global $wpdb;
66
67 $sql = "SELECT @redirection_items.*,@redirection_groups.tracking FROM @redirection_items INNER JOIN @redirection_groups ON @redirection_groups.id=@redirection_items.group_id AND @redirection_groups.status='enabled' AND @redirection_groups.module_id='$module' WHERE @redirection_items.status='enabled' ORDER BY @redirection_groups.position,@redirection_items.position";
68 $sql = str_replace( '@', $wpdb->prefix, $sql );
69
70 $rows = $wpdb->get_results( $sql );
71 $items = array();
72 if ( count( $rows) > 0 ) {
73 foreach ( $rows AS $row ) {
74 $items[] = new Red_Item( $row );
75 }
76 }
77
78 return $items;
79 }
80
81 function exists( $url ) {
82 global $wpdb;
83
84 if ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$wpdb->prefix}redirection_items WHERE url=%s", $url ) ) > 0 )
85 return true;
86 return false;
87 }
88
89 function get_for_url( $url, $type ) {
90 global $wpdb;
91
92 $sql = $wpdb->prepare( "SELECT @redirection_items.*,@redirection_groups.tracking,@redirection_groups.position AS group_pos,@redirection_modules.id AS module_id FROM @redirection_items INNER JOIN @redirection_groups ON @redirection_groups.id=@redirection_items.group_id AND @redirection_groups.status='enabled' INNER JOIN @redirection_modules ON @redirection_modules.id=@redirection_groups.module_id AND @redirection_modules.type=%s WHERE( @redirection_items.regex=1 OR @redirection_items.url=%s)", $type, $url );
93 $sql = str_replace( '@', $wpdb->prefix, $sql);
94
95 $rows = $wpdb->get_results( $sql ) ;
96 $items = array();
97 if ( count( $rows ) > 0 ) {
98 foreach ( $rows AS $row ) {
99 $items[$row->group_pos.'.'.$row->position] = new Red_Item( $row );
100 }
101 }
102
103 // Sort it in PHP
104 ksort( $items );
105 $items = array_values( $items );
106 return $items;
107 }
108
109 function get_by_module( &$pager, $module ) {
110 global $wpdb;
111
112 $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->prefix}redirection_items INNER JOIN {$wpdb->prefix}redirection_groups ON {$wpdb->prefix}redirection_groups.id={$wpdb->prefix}redirection_items.group_id";
113 $sql .= $pager->to_limits( "{$wpdb->prefix}redirection_groups.module_id=".$module, array( 'url', 'action_data' ));
114
115 $rows = $wpdb->get_results( $sql );
116 $pager->set_total( $wpdb->get_var( "SELECT FOUND_ROWS()" ));
117 $items = array();
118 if ( count( $rows) > 0)
119 {
120 foreach( $rows AS $row)
121 $items[] = new Red_Item( $row);
122 }
123
124 return $items;
125 }
126
127 /**
128 * Get redirection items in a group
129 */
130 function get_by_group( $group, &$pager ) {
131 global $wpdb;
132
133 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->prefix}redirection_items ".$pager->to_limits( 'group_id='.$group, array( 'url', 'action_data' ) ) ) );
134 $pager->set_total( $wpdb->get_var( "SELECT FOUND_ROWS()" ));
135
136 $items = array();
137 if ( count( $rows ) > 0 ) {
138 foreach ( $rows AS $row ) {
139 $items[] = new Red_Item( $row );
140 }
141 }
142
143 return $items;
144 }
145
146 function get_by_id( $id ) {
147 global $wpdb;
148
149 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_items WHERE id=%d", $id ) );
150 if ( $row )
151 return new Red_Item( $row );
152 return false;
153 }
154
155 function auto_generate() {
156 global $redirection;
157
158 $options = $redirection->get_options();
159 $id = time();
160
161 $url = $options['auto_target'];
162 $url = str_replace( '$dec$', $id, $url );
163 $url = str_replace( '$hex$', sprintf( '%x', $id), $url );
164 return $url;
165 }
166
167 function create( $details ) {
168 global $wpdb;
169
170 // Auto generate URLs
171 if ( $details['source'] == '' )
172 $details['source'] = Red_Item::auto_generate();
173
174 if ( $details['target'] == '' )
175 $details['target'] = Red_Item::auto_generate();
176
177 // Make sure we don't redirect to ourself
178 if ( $details['source'] == $details['target'] )
179 $details['target'] .= '-1';
180
181 $matcher = Red_Match::create( $details['match'] );
182 $group_id = intval( $details['group'] );
183
184 if ( $group_id > 0 && $matcher ) {
185 $regex = ( isset( $details['regex']) && $details['regex'] != false) ? true : false;
186 $position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $group_id ) );
187
188 $action = $details['red_action'];
189 $action_code = 0;
190 if ( $action == 'url' || $action == 'random' )
191 $action_code = 301;
192 elseif ( $action == 'error' )
193 $action_code = 404;
194
195 if ( isset( $details['action_code'] ) )
196 $action_code = intval( $details['action_code'] );
197
198 $data = array(
199 'url' => Red_Item::sanitize_url( $details['source'], $regex),
200 'action_type' => $details['red_action'],
201 'regex' => $regex,
202 'position' => $position,
203 'match_type' => $details['match'],
204 'action_data' => $matcher->data( $details ),
205 'action_code' => $action_code,
206 'last_access' => 0,
207 'group_id' => $group_id
208 );
209
210 $wpdb->insert( $wpdb->prefix.'redirection_items', $data );
211
212 $group = Red_Group::get( $group_id );
213 Red_Module::flush( $group->module_id );
214
215 return Red_Item::get_by_id( $wpdb->insert_id );
216 }
217
218 return false;
219 }
220
221 function delete_by_group( $group ) {
222 global $wpdb;
223
224 RE_Log::delete_for_group( $group);
225
226 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $group ) );
227
228 $group = Red_Group::get( $group_id );
229 Red_Module::flush( $group->module_id );
230 }
231
232 function delete( $id ) {
233 global $wpdb;
234
235 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE id=%d", $id ) );
236
237 RE_Log::delete_for_id( $id );
238
239 // Reorder all elements
240 $rows = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}redirection_items ORDER BY position" );
241 if ( count( $rows) > 0 ) {
242 foreach ( $rows AS $pos => $row ) {
243 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'position' => $pos ), array( 'id' => $row->id ) );
244 }
245 }
246 }
247
248
249 function sanitize_url( $url, $regex ) {
250 // Make sure that the old URL is relative
251 $url = preg_replace( '@^https?://(.*?)/@', '/', $url );
252 $url = preg_replace( '@^https?://(.*?)$@', '/', $url );
253
254 if ( substr( $url, 0, 1) != '/' && $regex == false )
255 $url = '/'.$url;
256 return $url;
257 }
258
259
260 function update( $details ) {
261 if ( strlen( $details['old'] ) > 0 ) {
262 global $wpdb;
263
264 $this->regex = isset( $details['regex'] ) ? true : false;
265 $this->url = $this->sanitize_url( $details['old'], $this->regex );
266 $this->title = $details['title'];
267
268 $data = $this->match->data( $details );
269
270 $this->action_code = 0;
271 if ( isset( $details['action_code'] ) )
272 $this->action_code = intval( $details['action_code'] );
273
274 if ( isset( $details['group_id'] ) )
275 $this->group_id = intval( $details['group_id'] );
276
277 // Save this
278 global $wpdb;
279 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'url' => $this->url, 'regex' => $this->regex, 'action_code' => $this->action_code, 'action_data' => $data, 'group_id' => $this->group_id, 'title' => $this->title ), array( 'id' => $this->id ) );
280
281 $group = Red_Group::get( $this->group_id );
282 if ( $group )
283 Red_Module::flush( $group->module_id );
284 }
285 }
286
287 function save_order( $items, $start ) {
288 global $wpdb;
289
290 foreach ( $items AS $pos => $id ) {
291 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'position' => $pos + $start ), array( 'id' => $id ) );
292 }
293
294 $item = Red_Item::get_by_id( $id );
295 $group = Red_Group::get( $item->group_id );
296 if ( $group )
297 Red_Module::flush( $group->module_id );
298 }
299
300 function matches( $url ) {
301 $this->url = str_replace( ' ', '%20', $this->url );
302 $matches = false;
303
304 // Check if we match the URL
305 if ( ( $this->regex == false && ( $this->url == $url || $this->url == rtrim( $url, '/' ) || $this->url == urldecode( $url ) ) ) ||( $this->regex == true && @preg_match( '@'.str_replace( '@', '\\@', $this->url).'@', $url, $matches) > 0) ||( $this->regex == true && @preg_match( '@'.str_replace( '@', '\\@', $this->url).'@', urldecode( $url ), $matches) > 0) ) {
306 // Check if our match wants this URL
307 $target = $this->match->get_target( $url, $this->url, $this->regex);
308
309 if ( $target ) {
310 $target = $this->replaceSpecialTags( $target );
311
312 $this->visit( $url, $target );
313
314 if ( $this->status == 'enabled' )
315 return $this->action->process_before( $this->action_code, $target );
316 }
317 }
318
319 return false;
320 }
321
322 function replaceSpecialTags( $target ) {
323 if ( is_numeric( $target ) )
324 $target = get_permalink( $target );
325 else {
326 $user = wp_get_current_user();
327 if ( !empty( $user ) ) {
328 $target = str_replace( '%userid%', $user->ID, $target );
329 $target = str_replace( '%userlogin%', isset( $user->user_login ) ? $user->user_login : '', $target );
330 $target = str_replace( '%userurl%', isset( $user->user_url ) ? $user->user_url : '', $target );
331 }
332 }
333
334 return $target;
335 }
336
337 function visit( $url, $target ) {
338 if ( $this->tracking && $this->id ) {
339 global $wpdb, $redirection;
340
341 // Update the counters
342 $count = $this->last_count + 1;
343 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}redirection_items SET last_count=%d, last_access=NOW() WHERE id=%d", $count, $this->id ) );
344
345 if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
346 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
347 elseif ( isset( $_SERVER['REMOTE_ADDR'] ) )
348 $ip = $_SERVER['REMOTE_ADDR'];
349
350 $options = $redirection->get_options();
351 if ( $options['log_redirections'])
352 $log = RE_Log::create( $url, $target, $_SERVER['HTTP_USER_AGENT'], $ip, isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', $this->id, $this->module_id, $this->group_id);
353 }
354 }
355
356 function reset() {
357 global $wpdb;
358
359 $this->last_count = 0;
360 $this->last_access = '0000-00-00 00:00:00';
361
362 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'last_count' => 0, 'last_access' => $this->last_access ), array( 'id' => $this->id ) );
363
364 RE_Log::delete_for_id( $this->id );
365 }
366
367 function show_url( $url ) {
368 return implode( '&#8203;/', explode( '/', $url ) );
369 }
370
371 function move_to( $group ) {
372 global $wpdb;
373
374 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'group_id' => $group ), array( 'id' => $this->id ) );
375 }
376
377 function toggle_status() {
378 global $wpdb;
379
380 $this->status = ( $this->status == 'enabled' ) ? 'disabled' : 'enabled';
381 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => $this->status ), array( 'id' => $this->id ) );
382 }
383
384 function actions( $action = '' ) {
385 $actions = array(
386 'url' => __( 'Redirect to URL', 'redirection' ),
387 'random' => __( 'Redirect to random post', 'redirection' ),
388 'pass' => __( 'Pass-through', 'redirection' ),
389 'error' => __( 'Error (404)', 'redirection' ),
390 'nothing' => __( 'Do nothing', 'redirection' ),
391 );
392
393 if ( $action )
394 return $actions[$action];
395 return $actions;
396 }
397
398 function match_name() {
399 return $this->match->match_name();
400 }
401
402 function type() {
403 if ( ( $this->action_type == 'url' || $this->action_type == 'error' || $this->action_type == 'random' ) && $this->action_code > 0 )
404 return $this->action_code;
405 else if ( $this->action_type == 'pass' )
406 return 'pass';
407 return '&mdash;';
408 }
409 }
410