PluginProbe
Redirection / 2.3.6
Redirection v2.3.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 / redirection.php

redirection.php in Redirection 2.3.6, at redirection.php

453 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Redirection
4 Plugin URI: http://urbangiraffe.com/plugins/redirection/
5 Description: Manage all your 301 redirects and monitor 404 errors
6 Version: 2.3.6
7 Author: John Godley
8 Author URI: http://urbangiraffe.com
9 ============================================================================================================
10 This software is provided "as is" and any express or implied warranties, including, but not limited to, the
11 implied warranties of merchantibility and fitness for a particular purpose are disclaimed. In no event shall
12 the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or
13 consequential damages(including, but not limited to, procurement of substitute goods or services; loss of
14 use, data, or profits; or business interruption) however caused and on any theory of liability, whether in
15 contract, strict liability, or tort(including negligence or otherwise) arising in any way out of the use of
16 this software, even if advised of the possibility of such damage.
17
18 For full license details see license.txt
19 ============================================================================================================
20 */
21
22 include dirname( __FILE__ ).'/plugin.php';
23 include dirname( __FILE__ ).'/models/redirect.php';
24 include dirname( __FILE__ ).'/models/match.php';
25 include dirname( __FILE__ ).'/models/log.php';
26 include dirname( __FILE__ ).'/models/group.php';
27 include dirname( __FILE__ ).'/models/module.php';
28 include dirname( __FILE__ ).'/models/action.php';
29 include dirname( __FILE__ ).'/models/monitor.php';
30 include dirname( __FILE__ ).'/modules/wordpress.php';
31
32 define( 'REDIRECTION_VERSION', '2.3.1' ); // DB schema version. Only change if DB needs changing
33
34 if ( class_exists( 'Redirection' ) )
35 return;
36
37 class Redirection extends Redirection_Plugin {
38 var $hasMatched = false;
39
40 function __construct() {
41 $this->register_plugin( 'redirection', __FILE__ );
42
43 if ( is_admin() ) {
44 $this->add_action( 'admin_menu' );
45 $this->add_action( 'load-tools_page_redirection', 'redirection_head' );
46 $this->add_action( 'init', 'inject' );
47
48 add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 );
49 add_action( 'redirection_log_delete', array( $this, 'expire_logs' ) );
50
51 $this->register_activation( __FILE__ );
52 $this->register_plugin_settings( __FILE__ );
53
54 // Ajax functions
55 if ( defined( 'DOING_AJAX' ) ) {
56 include_once dirname( __FILE__ ).'/ajax.php';
57 $this->ajax = new RedirectionAjax();
58 }
59 }
60 else {
61 $this->update();
62
63 // Create a WordPress exporter and let it handle the load
64 $this->wp = new WordPress_Module();
65 $this->wp->start();
66 }
67
68 $this->monitor = new Red_Monitor( $this->get_options() );
69 $this->add_action ('template_redirect' );
70 }
71
72 function update() {
73 $version = get_option( 'redirection_version' );
74
75 if ( $version != REDIRECTION_VERSION ) {
76 include_once dirname( __FILE__ ).'/models/database.php';
77
78 $db = new RE_Database();
79 return $db->upgrade( $version, REDIRECTION_VERSION );
80 }
81
82 return true;
83 }
84
85 function set_per_page( $status, $option, $value ) {
86 if ( $option == 'redirection_log_per_page' )
87 return $value;
88 return $status;
89 }
90
91 function activate() {
92 if ( $this->update() === false ) {
93 $db = new RE_Database();
94 $db->remove( $version, REDIRECTION_VERSION );
95 exit();
96 }
97 }
98
99 function plugin_settings( $links ) {
100 $settings_link = '<a href="tools.php?page='.basename( __FILE__ ).'">'.__( 'Settings', 'redirection' ).'</a>';
101 array_unshift( $links, $settings_link );
102 return $links;
103 }
104
105 function version() {
106 $plugin_data = implode( '', file( __FILE__ ) );
107
108 if ( preg_match( '|Version:(.*)|i', $plugin_data, $version ) )
109 return trim( $version[1] );
110 return '';
111 }
112
113 function redirection_head() {
114 if ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) )
115 add_screen_option( 'per_page', array( 'label' => __( 'Log entries', 'redirection' ), 'default' => 25, 'option' => 'redirection_log_per_page' ) );
116
117 wp_enqueue_script( 'redirection', plugin_dir_url( __FILE__ ).'js/redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $this->version() );
118 wp_enqueue_style( 'redirection', plugin_dir_url( __FILE__ ).'admin.css', $this->version() );
119
120 wp_localize_script( 'redirection', 'Redirectioni10n', array(
121 'please_wait' => __( 'Please wait...', 'redirection' ),
122 'type' => 1,
123 'progress' => '<img src="'.plugin_dir_url( __FILE__ ).'/images/progress.gif" alt="loading" width="50" height="16"/>',
124 'are_you_sure' => __( 'Are you sure?', 'redirection' ),
125 'none_select' => __( 'No items have been selected', 'redirection' )
126 ) );
127 }
128
129 function admin_menu() {
130 add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), apply_filters( 'redirection_role', 'administrator' ), basename( __FILE__ ), array( &$this, "admin_screen" ) );
131 }
132
133 function expire_logs() {
134 global $wpdb;
135
136 $options = $this->get_options();
137 $cleanup = false;
138
139 if ( $options['expire_redirect'] > 0 ) {
140 $cleanup = true;
141 $logs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY)", $options['expire'] ) );
142
143 if ( $logs > 0 )
144 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT 1000", $options['expire'] ) );
145 }
146
147 if ( $options['expire_404'] > 0 ) {
148 $cleanup = true;
149 $l404 = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404 WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY)", $options['expire'] ) );
150
151 if ( $l404 > 0 )
152 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT 1000", $options['expire'] ) );
153 }
154
155 if ( $cleanup ) {
156 $rand = mt_rand( 1, 5000 );
157
158 if ( $rand == 11 )
159 $wpdb->query( "OPTIMIZE TABLE {$wpdb->prefix}redirection_logs" );
160 elseif ( $rand == 12 )
161 $wpdb->query( "OPTIMIZE TABLE {$wpdb->prefix}redirection_404" );
162 }
163 }
164
165 function admin_screen() {
166 $this->update();
167
168 $options = $this->get_options();
169 if ( ( $options['expire_404'] > 0 || $options['expire_redirect'] > 0 ) && !wp_next_scheduled( 'redirection_log_delete' ) )
170 wp_schedule_event( time(), 'daily', 'redirection_log_delete' );
171
172 if ( isset($_GET['sub']) ) {
173 if ( $_GET['sub'] == 'log' )
174 return $this->admin_screen_log();
175 elseif ( $_GET['sub'] == '404s' )
176 return $this->admin_screen_404();
177 elseif ( $_GET['sub'] == 'options' )
178 return $this->admin_screen_options();
179 elseif ( $_GET['sub'] == 'process' )
180 return $this->admin_screen_process();
181 elseif ( $_GET['sub'] == 'groups' )
182 return $this->admin_groups( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
183 elseif ( $_GET['sub'] == 'modules' )
184 return $this->admin_screen_modules();
185 elseif ( $_GET['sub'] == 'support' )
186 return $this->render_admin('support', array( 'options' => $this->get_options() ) );
187 }
188
189 return $this->admin_redirects( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
190 }
191
192 function admin_screen_modules() {
193 $options = $this->get_options();
194 $this->render_admin( 'module_list', array( 'options' => $this->get_options(), 'modules' => Red_Module::get_all(), 'module_types' => Red_Module::get_types(), 'token' => $options['token'] ) );
195 }
196
197 function get_options() {
198 $options = get_option( 'redirection_options' );
199 if ( $options === false )
200 $options = array();
201
202 $defaults = array(
203 'support' => false,
204 'token' => '',
205 'monitor_post' => 0,
206 'auto_target' => '',
207 'expire_redirect' => 7,
208 'expire_404' => 7,
209 );
210
211 foreach ( $defaults AS $key => $value ) {
212 if ( !isset( $options[$key] ) )
213 $options[$key] = $value;
214 }
215
216 if ( isset( $options['expire'] ) ) {
217 if ( isset( $options['log_redirection'] ) )
218 $options['expire_redirect'] = $options['expire'];
219
220 if ( isset( $options['log_404s'] ) )
221 $options['expire_404'] = $options['expire'];
222
223 unset( $options['expire'] );
224 unset( $options['log_redirection'] );
225 unset( $options['log_404s'] );
226
227 update_option( 'redirection_options', $options );
228 }
229
230 $options['lookup'] = 'http://urbangiraffe.com/map/?ip=';
231
232 return $options;
233 }
234
235 function inject() {
236 $options = $this->get_options();
237
238 if ( isset( $_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['token'] == $options['token'] && $_GET['page'] == 'redirection.php' ) {
239 include dirname( __FILE__ ).'/models/file_io.php';
240
241 $exporter = Red_FileIO::create( $_GET['sub'] );
242 if ( $exporter ) {
243 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
244
245 $exporter->export( $items );
246 die();
247 }
248 }
249 elseif ( isset( $_POST['export-csv'] ) && check_admin_referer( 'redirection-log_management' ) ) {
250 if ( isset( $_GET['sub'] ) && $_GET['sub'] == 'log' )
251 RE_Log::export_to_csv();
252 else
253 RE_404::export_to_csv();
254 die();
255 }
256 }
257
258 function admin_screen_options() {
259 if ( isset( $_POST['regenerate'] ) && check_admin_referer( 'redirection-update_options' ) ) {
260 $options = $this->get_options();
261 $options['token'] = md5( uniqid() );
262
263 update_option( 'redirection_options', $options );
264
265 $this->render_message( __( 'Your options were updated', 'redirection' ) );
266 }
267 elseif ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
268 $options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
269 $options['auto_target'] = stripslashes( $_POST['auto_target'] );
270 $options['support'] = isset( $_POST['support'] ) ? true : false;
271 $options['token'] = stripslashes( $_POST['token'] );
272 $options['expire_redirect'] = min( intval( $_POST['expire_redirect'] ), 31 );
273 $options['expire_404'] = min( intval( $_POST['expire_404'] ), 31 );
274
275 if ( trim( $options['token'] ) == '' )
276 $options['token'] = md5( uniqid() );
277
278 update_option( 'redirection_options', $options );
279
280 $this->render_message( __( 'Your options were updated', 'redirection' ) );
281 }
282 elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
283 include dirname( __FILE__ ).'/models/database.php';
284
285 $db = new RE_Database;
286 $db->remove( __FILE__ );
287
288 $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
289 return;
290 }
291 elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
292 include dirname( __FILE__ ).'/models/file_io.php';
293
294 $importer = new Red_FileIO;
295
296 $count = $importer->import( $_POST['group'], $_FILES['upload'] );
297 if ( $count > 0 )
298 $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
299 else
300 $this->render_message( __( 'No items were imported', 'redirection' ) );
301 }
302
303 $groups = Red_Group::get_for_select();
304 $this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) );
305 }
306
307 function admin_screen_log() {
308 include dirname( __FILE__ ).'/models/pager.php';
309
310 $options = $this->get_options();
311
312 if ( isset( $_POST['delete-all'] ) && check_admin_referer( 'redirection-log_management' ) ) {
313 RE_Log::delete_all();
314 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
315 }
316
317 $table = new Redirection_Log_Table( $options );
318
319 if ( isset( $_GET['module'] ) )
320 $table->prepare_items( 'module', intval( $_GET['module'] ) );
321 else if (isset($_GET['group']))
322 $table->prepare_items( 'group', intval( $_GET['group'] ) );
323 else if (isset($_GET['redirect']))
324 $table->prepare_items( 'redirect', intval( $_GET['redirect'] ) );
325 else
326 $table->prepare_items();
327
328 $this->render_admin( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'], 'type' => 'log' ) );
329 }
330
331 function admin_screen_404() {
332 include dirname( __FILE__ ).'/models/pager.php';
333
334 if ( isset( $_POST['delete-all'] ) && check_admin_referer( 'redirection-log_management' ) ) {
335 RE_404::delete_all();
336 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
337 }
338
339 $options = $this->get_options();
340
341 $table = new Redirection_404_Table( $options );
342 $table->prepare_items( isset( $_GET['ip'] ) ? $_GET['ip'] : false );
343
344 $this->render_admin( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'], 'type' => '404s' ) );
345 }
346
347 function admin_groups( $module ) {
348 include dirname( __FILE__ ).'/models/pager.php';
349
350 if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
351 if ( Red_Group::create( stripslashes_deep( $_POST ) ) ) {
352 $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
353 Red_Module::flush( $module );
354 }
355 else
356 $this->render_error( __( 'Please specify a group name', 'redirection' ) );
357 }
358
359 if ( $module == 0 )
360 $module = Red_Module::get_first_id();
361
362 $pager = new RE_Pager( $_GET, admin_url( add_query_arg( array( 'sub' => 'groups' ), 'tools.php?page=redirection.php' ) ), 'position', 'ASC' );
363 $items = Red_Group::get_all( $module, $pager );
364
365 $module = Red_Module::get( $module );
366 if ( $module )
367 $this->render_admin( 'group_list', array( 'options' => $this->get_options(), 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => $module ) );
368 else
369 $this->render_message( __( 'Unknown module', 'redirection' ) );
370 }
371
372 function admin_redirects( $group ) {
373 include dirname( __FILE__ ).'/models/pager.php';
374
375 if ( $group == 0 )
376 $group = Red_Group::get_first_id();
377
378 $pager = new RE_Pager( $_GET, admin_url( add_query_arg( array(), 'tools.php?page=redirection.php' ) ), 'position', 'ASC' );
379 $items = Red_Item::get_by_group( $group, $pager );
380
381 $this->render_admin( 'item_list', array( 'options' => $this->get_options(), 'items' => $items, 'pager' => $pager, 'group' => Red_Group::get( $group ), 'groups' => Red_Group::get_for_select(), 'date_format' => get_option( 'date_format' ) ) );
382 }
383
384 function setMatched( $match ) {
385 $this->hasMatched = $match;
386 }
387
388 function hasMatched() {
389 return $this->hasMatched;
390 }
391
392 function locales() {
393 $locales = array();
394 if ( file_exists( dirname( __FILE__ ).'/readme.txt' ) ) {
395 $readme = file_get_contents( dirname( __FILE__ ).'/readme.txt' );
396
397 $start = strpos( $readme, 'Redirection is available in' );
398 $end = strpos( $readme, '==', $start );
399 if ( $start !== false && $end !== false ) {
400 if ( preg_match_all( '/^\* (.*?) by (.*?)/m', substr( $readme, $start, $end ), $matches ) > 0 ) {
401 $locales = $matches[1];
402 }
403 }
404
405 sort( $locales );
406 }
407
408 return $locales;
409 }
410
411 /**
412 * Matches 404s
413 */
414 function template_redirect() {
415 if ( is_404() ) {
416 $options = $this->get_options();
417
418 if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 ) {
419 RE_404::create( red_get_url(), red_user_agent(), red_ip(), red_http_referrer() );
420 }
421 }
422 }
423 }
424
425 function red_get_url() {
426 if ( isset( $_SERVER['REQUEST_URI'] ) )
427 return $_SERVER['REQUEST_URI'];
428 return '';
429 }
430
431 function red_user_agent() {
432 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) )
433 return $_SERVER['HTTP_USER_AGENT'];
434 return false;
435 }
436
437 function red_http_referrer() {
438 if ( isset( $_SERVER['HTTP_REFERER'] ) )
439 return $_SERVER['HTTP_REFERER'];
440 return false;
441 }
442
443 function red_ip() {
444 if ( isset( $_SERVER['REMOTE_ADDR'] ) )
445 return $_SERVER['REMOTE_ADDR'];
446 elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
447 return $_SERVER['HTTP_X_FORWARDED_FOR'];
448 return '';
449 }
450
451 // Instantiate the plugin
452 $redirection = new Redirection;
453