| 1 |
<?php |
| 2 |
|
| 3 |
class WordPress_Module extends Red_Module { |
| 4 |
private $matched = false; |
| 5 |
|
| 6 |
public function start() { |
| 7 |
// Setup the various filters and actions that allow Redirection to happen |
| 8 |
add_action( 'init', array( &$this, 'init' ) ); |
| 9 |
add_action( 'send_headers', array( &$this, 'send_headers' ) ); |
| 10 |
add_filter( 'permalink_redirect_skip', array( &$this, 'permalink_redirect_skip' ) ); |
| 11 |
add_filter( 'wp_redirect', array( &$this, 'wp_redirect' ), 1, 2 ); |
| 12 |
add_action( 'template_redirect', array( &$this, 'template_redirect' ) ); |
| 13 |
|
| 14 |
// Remove WordPress 2.3 redirection |
| 15 |
remove_action( 'template_redirect', 'wp_old_slug_redirect' ); |
| 16 |
remove_action( 'edit_form_advanced', 'wp_remember_old_slug' ); |
| 17 |
} |
| 18 |
|
| 19 |
public function init() { |
| 20 |
$url = $_SERVER['REQUEST_URI']; |
| 21 |
|
| 22 |
// Make sure we don't try and redirect something essential |
| 23 |
if ( !$this->protected_url( $url ) && $this->matched === false ) { |
| 24 |
do_action( 'redirection_first', $url, $this ); |
| 25 |
|
| 26 |
$redirects = Red_Item::get_for_url( $url, 'wp' ); |
| 27 |
|
| 28 |
foreach ( (array)$redirects AS $item ) { |
| 29 |
if ( $item->matches( $url ) ) { |
| 30 |
$this->matched = $item; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
do_action( 'redirection_last', $url, $this ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
private function protected_url( $url ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
public function template_redirect() { |
| 44 |
if ( is_404() ) { |
| 45 |
$options = red_get_options(); |
| 46 |
|
| 47 |
if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 ) { |
| 48 |
RE_404::create( $this->get_url(), $this->get_user_agent(), $this->get_ip(), $this->get_referrer() ); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public function status_header( $status ) { |
| 54 |
// Fix for incorrect headers sent when using FastCGI/IIS |
| 55 |
if ( substr( php_sapi_name(), 0, 3 ) == 'cgi' ) |
| 56 |
return str_replace( 'HTTP/1.1', 'Status:', $status ); |
| 57 |
return $status; |
| 58 |
} |
| 59 |
|
| 60 |
public function send_headers( $obj ) { |
| 61 |
if ( !empty( $this->matched ) && $this->matched->match->action_code == '410' ) { |
| 62 |
add_filter( 'status_header', array( &$this, 'set_header_410' ) ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function set_header_410() { |
| 67 |
return 'HTTP/1.1 410 Gone'; |
| 68 |
} |
| 69 |
|
| 70 |
public function wp_redirect( $url, $status ) { |
| 71 |
global $wp_version, $is_IIS; |
| 72 |
|
| 73 |
if ( $is_IIS ) { |
| 74 |
header( "Refresh: 0;url=$url" ); |
| 75 |
return $url; |
| 76 |
} |
| 77 |
elseif ( $status == 301 && php_sapi_name() == 'cgi-fcgi' ) { |
| 78 |
$servers_to_check = array( 'lighttpd', 'nginx' ); |
| 79 |
foreach ( $servers_to_check as $name ) { |
| 80 |
if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) { |
| 81 |
status_header( $status ); |
| 82 |
header( "Location: $url" ); |
| 83 |
exit( 0 ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
status_header( $status ); |
| 89 |
return $url; |
| 90 |
} |
| 91 |
|
| 92 |
public function save( $data ) { |
| 93 |
return array(); |
| 94 |
} |
| 95 |
|
| 96 |
public function permalink_redirect_skip( $skip ) { |
| 97 |
// only want this if we've matched using redirection |
| 98 |
if ( $this->matched ) |
| 99 |
$skip[] = $_SERVER['REQUEST_URI']; |
| 100 |
return $skip; |
| 101 |
} |
| 102 |
|
| 103 |
public function is_valid() { |
| 104 |
$perm = get_option( 'permalink_structure' ); |
| 105 |
if ( $perm === false || $perm == '' ) |
| 106 |
return false; |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
public function options() { |
| 111 |
if ( !$this->is_valid() ) |
| 112 |
echo __( '<strong>Disabled: You must enable <a href="options-permalink.php">permalinks</a> before using this</strong>', 'redirection' ); |
| 113 |
} |
| 114 |
|
| 115 |
public function get_type_string() { |
| 116 |
return __( 'WordPress', 'redirection' ); |
| 117 |
} |
| 118 |
|
| 119 |
private function get_url() { |
| 120 |
if ( isset( $_SERVER['REQUEST_URI'] ) ) |
| 121 |
return $_SERVER['REQUEST_URI']; |
| 122 |
return ''; |
| 123 |
} |
| 124 |
|
| 125 |
private function get_user_agent() { |
| 126 |
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) |
| 127 |
return $_SERVER['HTTP_USER_AGENT']; |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
private function get_referrer() { |
| 132 |
if ( isset( $_SERVER['HTTP_REFERER'] ) ) |
| 133 |
return $_SERVER['HTTP_REFERER']; |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
private function get_ip() { |
| 138 |
if ( isset( $_SERVER['REMOTE_ADDR'] ) ) |
| 139 |
return $_SERVER['REMOTE_ADDR']; |
| 140 |
elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) |
| 141 |
return $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 142 |
return ''; |
| 143 |
} |
| 144 |
} |
| 145 |
|