| 1 |
<?php |
| 2 |
|
| 3 |
include dirname( __FILE__ ).'/models/group.php'; |
| 4 |
include dirname( __FILE__ ).'/models/monitor.php'; |
| 5 |
include dirname( __FILE__ ).'/models/file-io.php'; |
| 6 |
include dirname( __FILE__ ).'/redirection-api.php'; |
| 7 |
|
| 8 |
define( 'RED_DEFAULT_PER_PAGE', 25 ); |
| 9 |
define( 'RED_MAX_PER_PAGE', 200 ); |
| 10 |
|
| 11 |
class Redirection_Admin { |
| 12 |
private static $instance = null; |
| 13 |
private $monitor; |
| 14 |
|
| 15 |
static function init() { |
| 16 |
if ( is_null( self::$instance ) ) { |
| 17 |
self::$instance = new Redirection_Admin(); |
| 18 |
|
| 19 |
load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' ); |
| 20 |
} |
| 21 |
|
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
|
| 25 |
function __construct() { |
| 26 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 27 |
add_action( 'load-tools_page_redirection', array( $this, 'redirection_head' ) ); |
| 28 |
add_action( 'plugin_action_links_'.basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), array( $this, 'plugin_settings' ), 10, 4 ); |
| 29 |
add_filter( 'redirection_save_options', array( $this, 'flush_schedule' ) ); |
| 30 |
add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 ); |
| 31 |
|
| 32 |
if ( defined( 'REDIRECTION_FLYING_SOLO' ) && REDIRECTION_FLYING_SOLO ) { |
| 33 |
add_filter( 'script_loader_src', array( $this, 'flying_solo' ), 10, 2 ); |
| 34 |
} |
| 35 |
|
| 36 |
register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) ); |
| 37 |
register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) ); |
| 38 |
|
| 39 |
$this->monitor = new Red_Monitor( red_get_options() ); |
| 40 |
$this->api = new Redirection_Api(); |
| 41 |
} |
| 42 |
|
| 43 |
public static function plugin_activated() { |
| 44 |
Redirection_Admin::update(); |
| 45 |
Red_Flusher::schedule(); |
| 46 |
red_set_options(); |
| 47 |
} |
| 48 |
|
| 49 |
public static function plugin_deactivated() { |
| 50 |
Red_Flusher::clear(); |
| 51 |
} |
| 52 |
|
| 53 |
public static function plugin_uninstall() { |
| 54 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 55 |
|
| 56 |
$db = new RE_Database(); |
| 57 |
$db->remove( REDIRECTION_FILE ); |
| 58 |
|
| 59 |
delete_option( 'redirection_options' ); |
| 60 |
} |
| 61 |
|
| 62 |
// So it finally came to this... some plugins include their JS in all pages, whether they are needed or not. If there is an error |
| 63 |
// then this can prevent Redirection running, it's a little sensitive about that. We use the nuclear option here to disable |
| 64 |
// all other JS while viewing Redirection |
| 65 |
public function flying_solo( $src, $handle ) { |
| 66 |
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'page=redirection.php' ) !== false ) { |
| 67 |
if ( substr( $src, 0, 4 ) === 'http' && $handle !== 'redirection' && strpos( $src, 'plugins' ) !== false ) { |
| 68 |
if ( $this->ignore_this_plugin( $src ) ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $src; |
| 75 |
} |
| 76 |
|
| 77 |
private function ignore_this_plugin( $src ) { |
| 78 |
if ( strpos( $src, 'mootools' ) !== false ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
if ( strpos( $src, 'wp-seo-' ) !== false ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
public function flush_schedule( $options ) { |
| 90 |
Red_Flusher::schedule(); |
| 91 |
return $options; |
| 92 |
} |
| 93 |
|
| 94 |
private static function update() { |
| 95 |
$version = get_option( 'redirection_version' ); |
| 96 |
|
| 97 |
Red_Flusher::schedule(); |
| 98 |
|
| 99 |
if ( $version !== REDIRECTION_DB_VERSION ) { |
| 100 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 101 |
|
| 102 |
$database = new RE_Database(); |
| 103 |
|
| 104 |
if ( $version === false ) { |
| 105 |
$database->install(); |
| 106 |
} |
| 107 |
|
| 108 |
return $database->upgrade( $version, REDIRECTION_DB_VERSION ); |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
function set_per_page( $status, $option, $value ) { |
| 115 |
if ( $option === 'redirection_log_per_page' ) { |
| 116 |
return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) ); |
| 117 |
} |
| 118 |
|
| 119 |
return $status; |
| 120 |
} |
| 121 |
|
| 122 |
function plugin_settings( $links ) { |
| 123 |
$settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&sub=options">'.__( 'Settings', 'redirection' ).'</a>'; |
| 124 |
array_unshift( $links, $settings_link ); |
| 125 |
return $links; |
| 126 |
} |
| 127 |
|
| 128 |
function redirection_head() { |
| 129 |
global $wp_version; |
| 130 |
|
| 131 |
$build = REDIRECTION_VERSION.'-'.REDIRECTION_BUILD; |
| 132 |
$options = red_get_options(); |
| 133 |
$versions = array( |
| 134 |
'Plugin: '.REDIRECTION_VERSION, |
| 135 |
'WordPress: '.$wp_version, |
| 136 |
'PHP: '.phpversion(), |
| 137 |
'Browser: '.Redirection_Request::get_user_agent(), |
| 138 |
); |
| 139 |
|
| 140 |
$this->inject(); |
| 141 |
|
| 142 |
if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) { |
| 143 |
add_screen_option( 'per_page', array( 'label' => sprintf( __( 'Log entries (%d max)', 'redirection' ), RED_MAX_PER_PAGE ), 'default' => RED_DEFAULT_PER_PAGE, 'option' => 'redirection_log_per_page' ) ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) { |
| 147 |
wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $build, true ); |
| 148 |
} else { |
| 149 |
wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array(), $build, true ); |
| 150 |
} |
| 151 |
|
| 152 |
wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.css', array(), $build ); |
| 153 |
|
| 154 |
wp_localize_script( 'redirection', 'Redirectioni10n', array( |
| 155 |
'WP_API_root' => admin_url( 'admin-ajax.php' ), |
| 156 |
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
| 157 |
'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ), |
| 158 |
'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ), |
| 159 |
'per_page' => $this->get_per_page(), |
| 160 |
'locale' => $this->get_i18n_data(), |
| 161 |
'localeSlug' => get_locale(), |
| 162 |
'token' => $options['token'], |
| 163 |
'autoGenerate' => $options['auto_target'], |
| 164 |
'versions' => implode( "\n", $versions ), |
| 165 |
'version' => REDIRECTION_VERSION, |
| 166 |
) ); |
| 167 |
} |
| 168 |
|
| 169 |
private function get_per_page() { |
| 170 |
$per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 ); |
| 171 |
|
| 172 |
return $per_page > 0 ? $per_page : RED_DEFAULT_PER_PAGE; |
| 173 |
} |
| 174 |
|
| 175 |
private function get_i18n_data() { |
| 176 |
$i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json'; |
| 177 |
|
| 178 |
if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
| 179 |
$locale_data = @file_get_contents( $i18n_json ); |
| 180 |
|
| 181 |
if ( $locale_data ) { |
| 182 |
return $locale_data; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Return empty if we have nothing to return so it doesn't fail when parsed in JS |
| 187 |
return '{}'; |
| 188 |
} |
| 189 |
|
| 190 |
function admin_menu() { |
| 191 |
add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) ); |
| 192 |
} |
| 193 |
|
| 194 |
private function check_minimum_wp() { |
| 195 |
$wp_version = get_bloginfo( 'version' ); |
| 196 |
|
| 197 |
if ( version_compare( $wp_version, REDIRECTION_MIN_WP, '<' ) ) { |
| 198 |
?> |
| 199 |
<div class="react-error"> |
| 200 |
<h1><?php _e( 'Unable to load Redirection', 'redirection' ); ?></h1> |
| 201 |
<p style="text-align: left"><?php printf( __( 'Redirection requires WordPress v%1s, you are using v%2s - please update your WordPress', 'redirection' ), REDIRECTION_MIN_WP, $wp_version ); ?></p> |
| 202 |
</div> |
| 203 |
<?php |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
private function check_tables_exist() { |
| 211 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 212 |
|
| 213 |
$database = new RE_Database(); |
| 214 |
$status = $database->get_status(); |
| 215 |
|
| 216 |
if ( $status['status'] !== 'good' ) { |
| 217 |
?> |
| 218 |
<div class="error"> |
| 219 |
<h3><?php _e( 'Redirection not installed properly', 'redirection' ); ?></h3> |
| 220 |
<p style="text-align: left"><?php printf( __( 'Problems were detected with your database tables. Please visit the <a href="%s">support page</a> for more details.', 'redirection' ), 'tools.php?page=redirection.php&sub=support' ); ?></p> |
| 221 |
</div> |
| 222 |
<?php |
| 223 |
|
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
function admin_screen() { |
| 231 |
$version = get_plugin_data( REDIRECTION_FILE ); |
| 232 |
$version = $version['Version']; |
| 233 |
|
| 234 |
Redirection_Admin::update(); |
| 235 |
|
| 236 |
if ( $this->check_minimum_wp() === false ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
if ( $this->check_tables_exist() === false && ( ! isset( $_GET['sub'] ) || $_GET['sub'] !== 'support' ) ) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
?> |
| 244 |
<div id="react-ui"> |
| 245 |
<div class="react-loading"> |
| 246 |
<h1><?php _e( 'Loading, please wait...', 'redirection' ); ?></h1> |
| 247 |
|
| 248 |
<span class="react-loading-spinner"></span> |
| 249 |
</div> |
| 250 |
<noscript>Please enable JavaScript</noscript> |
| 251 |
|
| 252 |
<div class="react-error" style="display: none"> |
| 253 |
<h1><?php _e( 'Unable to load Redirection', 'redirection' ); ?> v<?php echo esc_html( $version ); ?></h1> |
| 254 |
<p><?php _e( "This may be caused by another plugin - look at your browser's error console for more details.", 'redirection' ); ?></p> |
| 255 |
<p><?php _e( 'If you are using a page caching plugin or service (CloudFlare, OVH, etc) then you can also try clearing that cache.', 'redirection' ); ?></p> |
| 256 |
<p><?php _e( 'Also check if your browser is able to load <code>redirection.js</code>:', 'redirection' ); ?></p> |
| 257 |
<p><code><?php echo esc_html( plugin_dir_url( REDIRECTION_FILE ).'redirection.js?ver='.urlencode( REDIRECTION_VERSION ).'-'.urlencode( REDIRECTION_BUILD ) ); ?></code></p> |
| 258 |
<p><?php _e( "If you think Redirection is at fault then create an issue.", 'redirection' ); ?></p> |
| 259 |
<p class="versions"><?php _e( '<code>Redirectioni10n</code> is not defined. This usually means another plugin is blocking Redirection from loading. Please disable all plugins and try again.', 'redirection' ); ?></p> |
| 260 |
<p> |
| 261 |
<a class="button-primary" target="_blank" href="https://github.com/johngodley/redirection/issues/new?title=Problem%20starting%20Redirection%20<?php echo esc_attr( $version ) ?>"> |
| 262 |
<?php _e( 'Create Issue', 'redirection' ); ?> |
| 263 |
</a> |
| 264 |
</p> |
| 265 |
</div> |
| 266 |
</div> |
| 267 |
|
| 268 |
<script> |
| 269 |
var prevError = window.onerror; |
| 270 |
var errors = []; |
| 271 |
var timeout = 0; |
| 272 |
var timer = setInterval( function() { |
| 273 |
if ( isRedirectionLoaded() ) { |
| 274 |
resetAll(); |
| 275 |
} else if ( errors.length > 0 || timeout++ === 5 ) { |
| 276 |
showError(); |
| 277 |
} |
| 278 |
}, 5000 ); |
| 279 |
|
| 280 |
function isRedirectionLoaded() { |
| 281 |
return typeof redirection !== 'undefined'; |
| 282 |
} |
| 283 |
|
| 284 |
function showError() { |
| 285 |
var errorText = ""; |
| 286 |
|
| 287 |
if ( errors.length > 0 ) { |
| 288 |
errorText = "```\n" + errors.join( ',' ) + "\n```\n\n"; |
| 289 |
} |
| 290 |
|
| 291 |
resetAll(); |
| 292 |
document.querySelector( '.react-loading' ).style.display = 'none'; |
| 293 |
document.querySelector( '.react-error' ).style.display = 'block'; |
| 294 |
|
| 295 |
if ( typeof Redirectioni10n !== 'undefined' ) { |
| 296 |
document.querySelector( '.versions' ).innerHTML = Redirectioni10n.versions.replace( /\n/g, '<br />' ); |
| 297 |
document.querySelector( '.react-error .button-primary' ).href += '&body=' + encodeURIComponent( errorText ) + encodeURIComponent( Redirectioni10n.versions ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
function resetAll() { |
| 302 |
clearInterval( timer ); |
| 303 |
window.onerror = prevError; |
| 304 |
} |
| 305 |
|
| 306 |
window.onerror = function( error, url, line ) { |
| 307 |
console.error( error ); |
| 308 |
errors.push( error + ' ' + url + ' ' + line ); |
| 309 |
}; |
| 310 |
</script> |
| 311 |
<?php |
| 312 |
} |
| 313 |
|
| 314 |
private function user_has_access() { |
| 315 |
return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); |
| 316 |
} |
| 317 |
|
| 318 |
function inject() { |
| 319 |
if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) { |
| 320 |
$this->tryExportLogs(); |
| 321 |
$this->tryExportRedirects(); |
| 322 |
$this->tryExportRSS(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
function tryExportRSS() { |
| 327 |
if ( isset( $_GET['token'] ) && $_GET['sub'] === 'rss' ) { |
| 328 |
$options = red_get_options(); |
| 329 |
|
| 330 |
if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) { |
| 331 |
$items = Red_Item::get_all_for_module( intval( $_GET['module'] ) ); |
| 332 |
|
| 333 |
$exporter = Red_FileIO::create( 'rss' ); |
| 334 |
$exporter->force_download(); |
| 335 |
echo $exporter->get_data( $items, array() ); |
| 336 |
die(); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
private function tryExportLogs() { |
| 342 |
if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) { |
| 343 |
if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) { |
| 344 |
RE_Log::export_to_csv(); |
| 345 |
} else { |
| 346 |
RE_404::export_to_csv(); |
| 347 |
} |
| 348 |
|
| 349 |
die(); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
private function tryExportRedirects() { |
| 354 |
if ( $this->user_has_access() && $_GET['sub'] === 'io' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) { |
| 355 |
$export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] ); |
| 356 |
|
| 357 |
if ( $export !== false ) { |
| 358 |
$export['exporter']->force_download(); |
| 359 |
echo $export['data']; |
| 360 |
die(); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) ); |
| 367 |
|
| 368 |
add_action( 'init', array( 'Redirection_Admin', 'init' ) ); |
| 369 |
|