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