| 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.1.29 |
| 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 |
include dirname( __FILE__ ).'/modules/404.php'; |
| 32 |
|
| 33 |
define( 'REDIRECTION_VERSION', '2.1.16' ); |
| 34 |
|
| 35 |
class Redirection extends Redirection_Plugin { |
| 36 |
var $hasMatched = false; |
| 37 |
|
| 38 |
function Redirection() { |
| 39 |
$this->register_plugin('redirection', __FILE__); |
| 40 |
|
| 41 |
if ( is_admin() ) { |
| 42 |
$this->add_action( 'admin_menu' ); |
| 43 |
$this->add_action( 'admin_head' ); |
| 44 |
$this->add_action( 'wp_print_scripts' ); |
| 45 |
$this->add_action( 'wp_print_styles' ); |
| 46 |
$this->add_action( 'admin_head', 'wp_print_styles' ); |
| 47 |
$this->add_action( 'init', 'inject' ); |
| 48 |
$this->add_filter( 'contextual_help', 'contextual_help', 10, 2 ); |
| 49 |
$this->add_action( 'admin_footer' ); |
| 50 |
$this->add_filter( 'print_scripts_array' ); |
| 51 |
|
| 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 |
$this->error = new Error404_Module(); |
| 68 |
$this->error->start(); |
| 69 |
} |
| 70 |
|
| 71 |
$this->monitor = new Red_Monitor($this->get_options()); |
| 72 |
} |
| 73 |
|
| 74 |
function print_scripts_array( $scripts ) { |
| 75 |
$farb = array_search( 'farbtastic', $scripts ); |
| 76 |
|
| 77 |
if ( $farb && isset( $_GET['page'] ) && $_GET['page'] == 'redirection.php' ) |
| 78 |
unset( $scripts[$farb] ); |
| 79 |
|
| 80 |
return $scripts; |
| 81 |
} |
| 82 |
|
| 83 |
function plugin_settings( $links ) { |
| 84 |
$settings_link = '<a href="tools.php?page='.basename( __FILE__ ).'">'.__('Settings', 'redirection').'</a>'; |
| 85 |
array_unshift( $links, $settings_link ); |
| 86 |
return $links; |
| 87 |
} |
| 88 |
|
| 89 |
function contextual_help( $help, $screen ) { |
| 90 |
if ( $screen == 'tools_page_redirection' ) { |
| 91 |
$help .= '<h5>' . __( 'Redirection Help' ) . '</h5><div class="metabox-prefs">'; |
| 92 |
$help .= '<a href="http://urbangiraffe.com/plugins/redirection/">'.__( 'Redirection Documentation', 'redirection' ).'</a><br/>'; |
| 93 |
$help .= '<a href="http://urbangiraffe.com/support/forum/redirection">'.__( 'Redirection Support Forum', 'redirection' ).'</a><br/>'; |
| 94 |
$help .= '<a href="http://urbangiraffe.com/tracker/projects/redirection/issues?set_filter=1&tracker_id=1">'.__( 'Redirection Bug Tracker', 'redirection' ).'</a><br/>'; |
| 95 |
$help .= '<a href="http://urbangiraffe.com/plugins/redirection/faq/">'.__( 'Redirection FAQ', 'redirection' ).'</a><br/>'; |
| 96 |
$help .= __( 'Please read the documentation and FAQ, and check the bug tracker, before asking a question.', 'redirection' ); |
| 97 |
$help .= '</div>'; |
| 98 |
} |
| 99 |
|
| 100 |
return $help; |
| 101 |
} |
| 102 |
|
| 103 |
function is_25() { |
| 104 |
global $wp_version; |
| 105 |
if ( version_compare( '2.5', $wp_version ) <= 0 ) |
| 106 |
return true; |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
function submenu( $inwrap = false ) { |
| 111 |
// Decide what to do |
| 112 |
$sub = isset( $_GET['sub'] ) ? $_GET['sub'] : ''; |
| 113 |
$url = explode( '&', $_SERVER['REQUEST_URI'] ); |
| 114 |
$url = $url[0]; |
| 115 |
|
| 116 |
if ( !$this->is_25() && $inwrap == false ) |
| 117 |
$this->render_admin( 'submenu', array( 'url' => $url, 'sub' => $sub, 'class' => 'id="subsubmenu"' ) ); |
| 118 |
elseif ( $this->is_25() && $inwrap == true ) |
| 119 |
$this->render_admin( 'submenu', array( 'url' => $url, 'sub' => $sub, 'class' => 'class="subsubsub"', 'trail' => ' | ' ) ); |
| 120 |
|
| 121 |
return $sub; |
| 122 |
} |
| 123 |
|
| 124 |
function version() { |
| 125 |
$plugin_data = implode( '', file( __FILE__ ) ); |
| 126 |
|
| 127 |
if ( preg_match( '|Version:(.*)|i', $plugin_data, $version ) ) |
| 128 |
return trim( $version[1] ); |
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
function wp_print_scripts() { |
| 133 |
if ( strpos( $_SERVER['REQUEST_URI'], 'redirection.php' ) ) { |
| 134 |
if (!function_exists ('wp_print_styles')) { |
| 135 |
wp_deregister_script ('jquery'); |
| 136 |
wp_enqueue_script( 'jquery', $this->url ().'/2.3/jquery.js', array(), $this->version () ); |
| 137 |
wp_enqueue_script( 'jquery-ui-core', $this->url ().'/2.3/ui.core.js', array('jquery'), $this->version () ); |
| 138 |
wp_enqueue_script( 'jquery-ui-sortable', $this->url ().'/2.3/ui.sortable.js', array('jquery-ui-core'), $this->version () ); |
| 139 |
} |
| 140 |
|
| 141 |
wp_enqueue_script( 'redirection', $this->url().'/js/redirection.js', array('jquery-form', 'jquery-ui-sortable' ), $this->version() ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
function wp_print_styles() { |
| 146 |
if ( strpos( $_SERVER['REQUEST_URI'], 'redirection.php' ) ) |
| 147 |
echo '<link rel="stylesheet" href="'.$this->url().'/admin.css" type="text/css" media="screen" title="no title" charset="utf-8"/>'; |
| 148 |
} |
| 149 |
|
| 150 |
function admin_head() { |
| 151 |
$sub = isset($_GET['sub']) ? $_GET['sub'] : ''; |
| 152 |
|
| 153 |
if ( isset($_GET['page']) && $_GET['page'] == 'redirection.php' ) |
| 154 |
$this->render_admin( 'head', array( 'type' => $sub == '' ? '301' : $sub ) ); |
| 155 |
} |
| 156 |
|
| 157 |
function admin_menu() { |
| 158 |
add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), "administrator", basename( __FILE__ ), array( &$this, "admin_screen" ) ); |
| 159 |
} |
| 160 |
|
| 161 |
function update() { |
| 162 |
$version = get_option( 'redirection_version' ); |
| 163 |
|
| 164 |
if ( $version != REDIRECTION_VERSION ) { |
| 165 |
include_once dirname( __FILE__ ).'/models/database.php'; |
| 166 |
|
| 167 |
$db = new RE_Database(); |
| 168 |
$db->upgrade( $version, REDIRECTION_VERSION ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
function admin_screen() { |
| 173 |
$this->update(); |
| 174 |
|
| 175 |
$sub = $this->submenu(); |
| 176 |
$options = $this->get_options(); |
| 177 |
|
| 178 |
if ( isset($_GET['sub']) ) { |
| 179 |
if ( $_GET['sub'] == 'log' ) |
| 180 |
return $this->admin_screen_log(); |
| 181 |
elseif ( $_GET['sub'] == 'options' ) |
| 182 |
return $this->admin_screen_options(); |
| 183 |
elseif ( $_GET['sub'] == 'process' ) |
| 184 |
return $this->admin_screen_process(); |
| 185 |
elseif ( $_GET['sub'] == 'groups' ) |
| 186 |
return $this->admin_groups( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0); |
| 187 |
elseif ( $_GET['sub'] == 'modules' ) |
| 188 |
return $this->admin_screen_modules(); |
| 189 |
elseif ( $_GET['sub'] == 'support' ) |
| 190 |
return $this->render_admin('support'); |
| 191 |
} |
| 192 |
|
| 193 |
return $this->admin_redirects(isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0); |
| 194 |
} |
| 195 |
|
| 196 |
function admin_screen_modules() { |
| 197 |
if ( isset( $_POST['create'] ) && check_admin_referer( 'redirection-module_add' ) ) { |
| 198 |
$data = stripslashes_deep( $_POST ); |
| 199 |
|
| 200 |
if ( ( $module = Red_Module::create( $data ) ) ) { |
| 201 |
$moduleid = 0; |
| 202 |
if ( isset($_POST['module'])) |
| 203 |
$moduleid = intval( $_POST['module'] ); |
| 204 |
|
| 205 |
$this->render_message( __( 'Your module was successfully created', 'redirection' ) ); |
| 206 |
Red_Module::flush( $moduleid ); |
| 207 |
} |
| 208 |
else |
| 209 |
$this->render_error( __( 'Your module was not created - did you provide a name?', 'redirection' ) ); |
| 210 |
} |
| 211 |
|
| 212 |
$options = $this->get_options(); |
| 213 |
$this->render_admin( 'module_list', array( 'modules' => Red_Module::get_all(), 'module_types' => Red_Module::get_types(), 'token' => $options['token'] ) ); |
| 214 |
} |
| 215 |
|
| 216 |
function get_options() { |
| 217 |
$options = get_option( 'redirection_options' ); |
| 218 |
if ( $options === false ) |
| 219 |
$options = array(); |
| 220 |
|
| 221 |
$defaults = array ( |
| 222 |
'lookup' => 'http://urbangiraffe.com/map/?from=redirection&ip=', |
| 223 |
'support' => false, |
| 224 |
'log_redirections' => true, |
| 225 |
'log_404s' => true, |
| 226 |
'expire' => 0, |
| 227 |
'token' => '', |
| 228 |
'monitor_new_posts' => false, |
| 229 |
'monitor_post' => 0 |
| 230 |
); |
| 231 |
|
| 232 |
foreach ( $defaults AS $key => $value ){ |
| 233 |
if ( !isset( $options[$key] ) ) |
| 234 |
$options[$key] = $value; |
| 235 |
} |
| 236 |
|
| 237 |
if ($options['lookup'] == 'http://geomaplookup.cinnamonthoughts.org/?ip=' || $options['lookup'] == 'http://geomaplookup.net/?ip=') |
| 238 |
$options['lookup'] = 'http://urbangiraffe.com/map/?from=redirection&ip='; |
| 239 |
|
| 240 |
return $options; |
| 241 |
} |
| 242 |
|
| 243 |
function inject() { |
| 244 |
$options = $this->get_options(); |
| 245 |
|
| 246 |
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' ) ) ) { |
| 247 |
include dirname( __FILE__ ).'/models/file_io.php'; |
| 248 |
|
| 249 |
$exporter = new Red_FileIO; |
| 250 |
if ( $exporter->export( $_GET['sub'] ) ) |
| 251 |
die(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
function admin_screen_options() { |
| 256 |
if ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) { |
| 257 |
$options['lookup'] = stripslashes( $_POST['lookup'] ); |
| 258 |
$options['monitor_post'] = stripslashes( $_POST['monitor_post'] ); |
| 259 |
// $options['monitor_category'] = stripslashes( $_POST['monitor_category'] ); |
| 260 |
$options['auto_target'] = stripslashes( $_POST['auto_target'] ); |
| 261 |
$options['support'] = isset( $_POST['support'] ) ? true : false; |
| 262 |
$options['log_redirections'] = (bool) @ $_POST['log_redirections']; |
| 263 |
$options['log_404s'] = (bool) @ $_POST['log_404s']; |
| 264 |
$options['monitor_new_posts'] = isset( $_POST['monitor_new_posts'] ) ? true : false; |
| 265 |
$options['expire'] = intval( $_POST['expire'] ); |
| 266 |
$options['token'] = stripslashes( $_POST['token'] ); |
| 267 |
|
| 268 |
if ( trim( $options['token'] ) == '' ) |
| 269 |
$options['token'] = md5( uniqid() ); |
| 270 |
|
| 271 |
update_option( 'redirection_options', $options ); |
| 272 |
|
| 273 |
$this->render_message( __( 'Your options were updated', 'redirection' ) ); |
| 274 |
} |
| 275 |
elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) { |
| 276 |
include dirname( __FILE__ ).'/models/database.php'; |
| 277 |
|
| 278 |
$db = new RE_Database; |
| 279 |
$db->remove( __FILE__ ); |
| 280 |
|
| 281 |
$this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) ); |
| 282 |
return; |
| 283 |
} |
| 284 |
elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) { |
| 285 |
include dirname( __FILE__ ).'/models/file_io.php'; |
| 286 |
|
| 287 |
$importer = new Red_FileIO; |
| 288 |
|
| 289 |
$count = $importer->import( $_POST['group'], $_FILES['upload'] ); |
| 290 |
if ( $count > 0 ) |
| 291 |
$this->render_message( sprintf( __ngettext( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) ); |
| 292 |
else |
| 293 |
$this->render_message( __( 'No items were imported', 'redirection' ) ); |
| 294 |
} |
| 295 |
|
| 296 |
$groups = Red_Group::get_for_select(); |
| 297 |
$this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) ); |
| 298 |
} |
| 299 |
|
| 300 |
function admin_screen_log() { |
| 301 |
include dirname( __FILE__ ).'/models/pager.php'; |
| 302 |
|
| 303 |
if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) { |
| 304 |
if ( isset( $_GET['module'] ) ) |
| 305 |
RE_Log::delete_all( array( 'module_id' => intval( $_GET['module'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) ); |
| 306 |
else if (isset($_GET['group'])) |
| 307 |
RE_Log::delete_all( array( 'group_id' => intval( $_GET['group'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) ); |
| 308 |
else |
| 309 |
RE_Log::delete_all( array(), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) ); |
| 310 |
|
| 311 |
$this->render_message( __( 'Your logs have been deleted', 'redirection' ) ); |
| 312 |
} |
| 313 |
|
| 314 |
$pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ); |
| 315 |
|
| 316 |
if ( isset( $_GET['module'] ) ) |
| 317 |
$logs = RE_Log::get_by_module( $pager, intval( $_GET['module'] ) ); |
| 318 |
else if (isset($_GET['group'])) |
| 319 |
$logs = RE_Log::get_by_group( $pager, intval( $_GET['group'] ) ); |
| 320 |
else if (isset($_GET['redirect'])) |
| 321 |
$logs = RE_Log::get_by_redirect( $pager, intval( $_GET['redirect'] ) ); |
| 322 |
else |
| 323 |
$logs = RE_Log::get( $pager ); |
| 324 |
|
| 325 |
$options = $this->get_options(); |
| 326 |
$this->render_admin( 'log', array( 'logs' => $logs, 'pager' => $pager, 'lookup' => $options['lookup'] ) ); |
| 327 |
} |
| 328 |
|
| 329 |
function admin_groups($module) { |
| 330 |
include dirname( __FILE__ ).'/models/pager.php'; |
| 331 |
|
| 332 |
if (isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) { |
| 333 |
if ( Red_Group::create(stripslashes_deep( $_POST ) ) ) { |
| 334 |
$this->render_message( __( 'Your group was added successfully', 'redirection' ) ); |
| 335 |
Red_Module::flush( $module ); |
| 336 |
} |
| 337 |
else |
| 338 |
$this->render_error( __( 'Please specify a group name', 'redirection' ) ); |
| 339 |
} |
| 340 |
|
| 341 |
if ( $module == 0 ) |
| 342 |
$module = Red_Module::get_first_id(); |
| 343 |
|
| 344 |
$pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' ); |
| 345 |
$items = Red_Group::get_all( $module, $pager ); |
| 346 |
|
| 347 |
$this->render_admin( 'group_list', array( 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => Red_Module::get( $module ) ) ); |
| 348 |
} |
| 349 |
|
| 350 |
function admin_redirects( $group ) { |
| 351 |
include dirname( __FILE__ ).'/models/pager.php'; |
| 352 |
|
| 353 |
if ( $group == 0 ) |
| 354 |
$group = Red_Group::get_first_id(); |
| 355 |
|
| 356 |
$pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' ); |
| 357 |
$items = Red_Item::get_by_group( $group, $pager ); |
| 358 |
|
| 359 |
$this->render_admin( 'item_list', array( 'items' => $items, 'modules' => Red_Group::get_for_select(), 'pager' => $pager, 'group' => Red_Group::get( $group ), 'groups' => Red_Group::get_for_select(), 'date_format' => get_option('date_format')) ); |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Displays the nice animated support logo |
| 364 |
* |
| 365 |
* @return void |
| 366 |
**/ |
| 367 |
function admin_footer() { |
| 368 |
if ( isset($_GET['page']) && $_GET['page'] == basename( __FILE__ ) ) { |
| 369 |
$options = $this->get_options(); |
| 370 |
|
| 371 |
if ( !$options['support'] ) { |
| 372 |
?> |
| 373 |
<script type="text/javascript" charset="utf-8"> |
| 374 |
jQuery(function() { |
| 375 |
jQuery('#support-annoy').animate( { opacity: 0.2, backgroundColor: 'red' } ).animate( { opacity: 1, backgroundColor: 'yellow' }); |
| 376 |
}); |
| 377 |
</script> |
| 378 |
<?php |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
function setMatched( $match ) { |
| 384 |
$this->hasMatched = $match; |
| 385 |
} |
| 386 |
|
| 387 |
function hasMatched() { |
| 388 |
return $this->hasMatched; |
| 389 |
} |
| 390 |
|
| 391 |
function locales() { |
| 392 |
$locales = array(); |
| 393 |
$readme = @file_get_contents( dirname( __FILE__ ).'/readme.txt' ); |
| 394 |
if ( $readme ) { |
| 395 |
if ( preg_match_all( '/^\* (.*?) by \[(.*?)\]\((.*?)\)/m', $readme, $matches ) ) { |
| 396 |
foreach ( $matches[1] AS $pos => $match ) { |
| 397 |
$locales[$match] = '<a href="'.$matches[3][$pos].'">'.$matches[2][$pos].'</a>'; |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
ksort( $locales ); |
| 403 |
return $locales; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// Instantiate the plugin |
| 408 |
$redirection = new Redirection; |
| 409 |
|