PluginProbe
Redirection / 2.3.5
Redirection v2.3.5
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.5, at redirection.php

411 lines 13.9 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.5
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' );
33
34 if ( class_exists( 'Redirection' ) )
35 return;
36
37 class Redirection extends Redirection_Plugin {
38 var $hasMatched = false;
39
40 function Redirection() {
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
50 $this->register_activation( __FILE__ );
51 $this->register_plugin_settings( __FILE__ );
52
53 // Ajax functions
54 if ( defined( 'DOING_AJAX' ) ) {
55 include_once dirname( __FILE__ ).'/ajax.php';
56 $this->ajax = new RedirectionAjax();
57 }
58 }
59 else {
60 $this->update();
61
62 // Create a WordPress exporter and let it handle the load
63 $this->wp = new WordPress_Module();
64 $this->wp->start();
65 }
66
67 $this->monitor = new Red_Monitor( $this->get_options() );
68 $this->add_action ('template_redirect' );
69 }
70
71 function update() {
72 $version = get_option( 'redirection_version' );
73
74 if ( $version != REDIRECTION_VERSION ) {
75 include_once dirname( __FILE__ ).'/models/database.php';
76
77 $db = new RE_Database();
78 return $db->upgrade( $version, REDIRECTION_VERSION );
79 }
80
81 return true;
82 }
83
84 function set_per_page( $status, $option, $value ) {
85 if ( $option == 'redirection_log_per_page' )
86 return $value;
87 return $status;
88 }
89
90 function activate() {
91 if ( $this->update() === false ) {
92 $db = new RE_Database();
93 $db->remove( $version, REDIRECTION_VERSION );
94 exit();
95 }
96 }
97
98 function plugin_settings( $links ) {
99 $settings_link = '<a href="tools.php?page='.basename( __FILE__ ).'">'.__( 'Settings', 'redirection' ).'</a>';
100 array_unshift( $links, $settings_link );
101 return $links;
102 }
103
104 function version() {
105 $plugin_data = implode( '', file( __FILE__ ) );
106
107 if ( preg_match( '|Version:(.*)|i', $plugin_data, $version ) )
108 return trim( $version[1] );
109 return '';
110 }
111
112 function redirection_head() {
113 if ( isset( $_GET['sub'] ) && ( $_GET['sub'] == 'log' || $_GET['sub'] == '404s' ) )
114 add_screen_option( 'per_page', array( 'label' => __( 'Log entries', 'redirection' ), 'default' => 25, 'option' => 'redirection_log_per_page' ) );
115
116 wp_enqueue_script( 'redirection', plugin_dir_url( __FILE__ ).'js/redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $this->version() );
117 wp_enqueue_style( 'redirection', plugin_dir_url( __FILE__ ).'admin.css', $this->version() );
118
119 wp_localize_script( 'redirection', 'Redirectioni10n', array(
120 'please_wait' => __( 'Please wait...', 'redirection' ),
121 'type' => 1,
122 'progress' => '<img src="'.plugin_dir_url( __FILE__ ).'/images/progress.gif" alt="loading" width="50" height="16"/>',
123 'are_you_sure' => __( 'Are you sure?', 'redirection' ),
124 'none_select' => __( 'No items have been selected', 'redirection' )
125 ) );
126 }
127
128 function admin_menu() {
129 add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), apply_filters( 'redirection_role', 'administrator' ), basename( __FILE__ ), array( &$this, "admin_screen" ) );
130 }
131
132 function expire_logs() {
133 global $wpdb;
134
135 // Expire old entries
136 $options = $this->get_options();
137 if ( $options['expire'] != 0 ) {
138 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT 1000", $options['expire'] ) );
139 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT 1000", $options['expire'] ) );
140 }
141 }
142
143 function admin_screen() {
144 $this->update();
145 $this->expire_logs();
146
147 // Decide what to do
148 $sub = isset( $_GET['sub'] ) ? $_GET['sub'] : '';
149
150 $options = $this->get_options();
151
152 if ( isset($_GET['sub']) ) {
153 if ( $_GET['sub'] == 'log' )
154 return $this->admin_screen_log();
155 elseif ( $_GET['sub'] == '404s' )
156 return $this->admin_screen_404();
157 elseif ( $_GET['sub'] == 'options' )
158 return $this->admin_screen_options();
159 elseif ( $_GET['sub'] == 'process' )
160 return $this->admin_screen_process();
161 elseif ( $_GET['sub'] == 'groups' )
162 return $this->admin_groups( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
163 elseif ( $_GET['sub'] == 'modules' )
164 return $this->admin_screen_modules();
165 elseif ( $_GET['sub'] == 'support' )
166 return $this->render_admin('support', array( 'options' => $this->get_options() ) );
167 }
168
169 return $this->admin_redirects( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
170 }
171
172 function admin_screen_modules() {
173 $options = $this->get_options();
174 $this->render_admin( 'module_list', array( 'options' => $this->get_options(), 'modules' => Red_Module::get_all(), 'module_types' => Red_Module::get_types(), 'token' => $options['token'] ) );
175 }
176
177 function get_options() {
178 $options = get_option( 'redirection_options' );
179 if ( $options === false )
180 $options = array();
181
182 $defaults = array (
183 'support' => false,
184 'log_redirections' => true,
185 'log_404s' => true,
186 'expire' => 7,
187 'token' => '',
188 'monitor_new_posts' => false,
189 'monitor_post' => 0,
190 'auto_target' => '',
191 );
192
193 foreach ( $defaults AS $key => $value ) {
194 if ( !isset( $options[$key] ) )
195 $options[$key] = $value;
196 }
197
198 $options['lookup'] = 'http://urbangiraffe.com/map/?ip=';
199
200 return $options;
201 }
202
203 function inject() {
204 $options = $this->get_options();
205
206 if ( isset($_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['token'] == $options['token'] && $_GET['page'] == 'redirection.php' && in_array( $_GET['sub'], array( 'rss', 'xml', 'csv', 'apache' ) ) ) {
207 include dirname( __FILE__ ).'/models/file_io.php';
208
209 $exporter = new Red_FileIO;
210 if ( $exporter->export( $_GET['sub'] ) )
211 die();
212 }
213 }
214
215 function admin_screen_options() {
216 if ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
217 $options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
218 $options['auto_target'] = stripslashes( $_POST['auto_target'] );
219 $options['support'] = isset( $_POST['support'] ) ? true : false;
220 $options['log_redirections'] = (bool) @ $_POST['log_redirections'];
221 $options['log_404s'] = (bool) @ $_POST['log_404s'];
222 $options['monitor_new_posts'] = isset( $_POST['monitor_new_posts'] ) ? true : false;
223 $options['expire'] = intval( $_POST['expire'] );
224 $options['token'] = stripslashes( $_POST['token'] );
225
226 if ( trim( $options['token'] ) == '' )
227 $options['token'] = md5( uniqid() );
228
229 update_option( 'redirection_options', $options );
230
231 $this->render_message( __( 'Your options were updated', 'redirection' ) );
232 }
233 elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
234 include dirname( __FILE__ ).'/models/database.php';
235
236 $db = new RE_Database;
237 $db->remove( __FILE__ );
238
239 $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
240 return;
241 }
242 elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
243 include dirname( __FILE__ ).'/models/file_io.php';
244
245 $importer = new Red_FileIO;
246
247 $count = $importer->import( $_POST['group'], $_FILES['upload'] );
248 if ( $count > 0 )
249 $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
250 else
251 $this->render_message( __( 'No items were imported', 'redirection' ) );
252 }
253
254 $groups = Red_Group::get_for_select();
255 $this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) );
256 }
257
258 function admin_screen_log() {
259 include dirname( __FILE__ ).'/models/pager.php';
260
261 if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) {
262 if ( isset( $_GET['module'] ) )
263 RE_Log::delete_all( 'module', intval( $_GET['module'] ) );
264 else if (isset($_GET['group']))
265 RE_Log::delete_all( 'group', intval( $_GET['group_id'] ) );
266 else
267 RE_Log::delete_all();
268
269 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
270 }
271
272 $options = $this->get_options();
273
274 $table = new Redirection_Log_Table( $options );
275
276 if ( isset( $_GET['module'] ) )
277 $table->prepare_items( 'module', intval( $_GET['module'] ) );
278 else if (isset($_GET['group']))
279 $table->prepare_items( 'group', intval( $_GET['group'] ) );
280 else if (isset($_GET['redirect']))
281 $table->prepare_items( 'redirect', intval( $_GET['redirect'] ) );
282 else
283 $table->prepare_items();
284
285 $this->render_admin( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'] ) );
286 }
287
288 function admin_screen_404() {
289 include dirname( __FILE__ ).'/models/pager.php';
290
291 if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) {
292 RE_404::delete_all();
293 $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
294 }
295
296 $options = $this->get_options();
297
298 $table = new Redirection_404_Table( $options );
299 $table->prepare_items( isset( $_GET['ip'] ) ? $_GET['ip'] : false );
300
301 $this->render_admin( 'log', array( 'options' => $options, 'table' => $table, 'lookup' => $options['lookup'] ) );
302 }
303
304 function admin_groups( $module ) {
305 include dirname( __FILE__ ).'/models/pager.php';
306
307 if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
308 if ( Red_Group::create( stripslashes_deep( $_POST ) ) ) {
309 $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
310 Red_Module::flush( $module );
311 }
312 else
313 $this->render_error( __( 'Please specify a group name', 'redirection' ) );
314 }
315
316 if ( $module == 0 )
317 $module = Red_Module::get_first_id();
318
319 $pager = new RE_Pager( $_GET, admin_url( add_query_arg( array( 'sub' => 'groups' ), 'tools.php?page=redirection.php' ) ), 'position', 'ASC' );
320 $items = Red_Group::get_all( $module, $pager );
321
322 $module = Red_Module::get( $module );
323 if ( $module )
324 $this->render_admin( 'group_list', array( 'options' => $this->get_options(), 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => $module ) );
325 else
326 $this->render_message( __( 'Unknown module', 'redirection' ) );
327 }
328
329 function admin_redirects( $group ) {
330 include dirname( __FILE__ ).'/models/pager.php';
331
332 if ( $group == 0 )
333 $group = Red_Group::get_first_id();
334
335 $pager = new RE_Pager( $_GET, admin_url( add_query_arg( array(), 'tools.php?page=redirection.php' ) ), 'position', 'ASC' );
336 $items = Red_Item::get_by_group( $group, $pager );
337
338 $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' ) ) );
339 }
340
341 function setMatched( $match ) {
342 $this->hasMatched = $match;
343 }
344
345 function hasMatched() {
346 return $this->hasMatched;
347 }
348
349 function locales() {
350 $locales = array();
351 if ( file_exists( dirname( __FILE__ ).'/readme.txt' ) ) {
352 $readme = file_get_contents( dirname( __FILE__ ).'/readme.txt' );
353
354 $start = strpos( $readme, 'Redirection is available in' );
355 $end = strpos( $readme, '==', $start );
356 if ( $start !== false && $end !== false ) {
357 if ( preg_match_all( '/^\* (.*?) by (.*?)/m', substr( $readme, $start, $end ), $matches ) > 0 ) {
358 $locales = $matches[1];
359 }
360 }
361
362 sort( $locales );
363 }
364
365 return $locales;
366 }
367
368 /**
369 * Matches 404s
370 * @return [type] [description]
371 */
372 function template_redirect() {
373 if ( is_404() ) {
374 $options = $this->get_options();
375
376 if ( $options['log_404s'] ) {
377 $log = RE_404::create( red_get_url(), red_user_agent(), red_ip(), red_http_referrer() );
378 }
379 }
380 }
381 }
382
383 function red_get_url() {
384 if ( isset( $_SERVER['REQUEST_URI'] ) )
385 return $_SERVER['REQUEST_URI'];
386 return '';
387 }
388
389 function red_user_agent() {
390 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) )
391 return $_SERVER['HTTP_USER_AGENT'];
392 return false;
393 }
394
395 function red_http_referrer() {
396 if ( isset( $_SERVER['HTTP_REFERER'] ) )
397 return $_SERVER['HTTP_REFERER'];
398 return false;
399 }
400
401 function red_ip() {
402 if ( isset( $_SERVER['REMOTE_ADDR'] ) )
403 return $_SERVER['REMOTE_ADDR'];
404 elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
405 return $_SERVER['HTTP_X_FORWARDED_FOR'];
406 return '';
407 }
408
409 // Instantiate the plugin
410 $redirection = new Redirection;
411