| 1 |
<?php |
| 2 |
|
| 3 |
class WordPress_Module extends Red_Module |
| 4 |
{ |
| 5 |
var $canonical = 'default'; |
| 6 |
var $strip_index = 'default'; |
| 7 |
var $error_level = 'default'; |
| 8 |
var $time_limit = 0; |
| 9 |
var $matched; |
| 10 |
|
| 11 |
function start () |
| 12 |
{ |
| 13 |
// Setup the various filters and actions that allow Redirection to h appen |
| 14 |
add_action ('template_redirect', array (&$this, 'template_redirect')); |
| 15 |
add_action ('init', array (&$this, 'init')); |
| 16 |
add_action ('send_headers', array (&$this, 'send_headers')); |
| 17 |
add_filter ('permalink_redirect_skip', array (&$this, 'permalink_redirect_skip')); |
| 18 |
add_filter ('wp_redirect', array (&$this, 'wp_redirect'), 1, 2); |
| 19 |
|
| 20 |
// Remove WordPress 2.3 redirection |
| 21 |
remove_action ('template_redirect', 'wp_old_slug_redirect'); |
| 22 |
remove_action ('edit_form_advanced', 'wp_remember_old_slug'); |
| 23 |
|
| 24 |
// A WP < 2.3 fix |
| 25 |
global $wp_db_version; |
| 26 |
if ($wp_db_version < 6000) |
| 27 |
add_filter ('status_header', array (&$this, 'status_header')); |
| 28 |
} |
| 29 |
|
| 30 |
function init () |
| 31 |
{ |
| 32 |
global $redirection; |
| 33 |
|
| 34 |
$url = $_SERVER['REQUEST_URI']; |
| 35 |
|
| 36 |
// Make sure we don't try and redirect something essential |
| 37 |
if (!$this->protected_url ($url) && !$redirection->hasMatched ()) { |
| 38 |
do_action ('redirection_first', $url, $this); |
| 39 |
|
| 40 |
$redirects = Red_Item::get_for_url( $url, 'wp' ); |
| 41 |
|
| 42 |
if ( !empty( $redirects) ) { |
| 43 |
foreach ($redirects AS $key => $item) { |
| 44 |
if ( $item->matches( $url ) ) { |
| 45 |
global $redirection; |
| 46 |
|
| 47 |
$redirection->setMatched( true ); |
| 48 |
$this->matched = $item; |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
do_action ('redirection_last', $url, $this); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
function protected_url ($url) |
| 59 |
{ |
| 60 |
global $redirection; |
| 61 |
$part = explode ('?', $url); |
| 62 |
|
| 63 |
if ($part[0] == str_replace (get_bloginfo ('url'), '', $redirection->url ()).'/ajax.php' || strpos($url, 'wp-cron.php') !== false) |
| 64 |
return true; |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
function template_redirect () |
| 69 |
{ |
| 70 |
// Load data |
| 71 |
$modules = Red_Module::get_by_type ('wp'); |
| 72 |
|
| 73 |
if (count ($modules) > 0) |
| 74 |
{ |
| 75 |
foreach ($modules AS $wp) |
| 76 |
{ |
| 77 |
// Timeout |
| 78 |
if ($wp->time_limit != 'default') |
| 79 |
set_time_limit ($wp->time_limit); |
| 80 |
|
| 81 |
// Error level |
| 82 |
if ($wp->error_level == 'none') |
| 83 |
error_reporting (0); |
| 84 |
else if ($wp->error_level == 'show') |
| 85 |
error_reporting (E_ALL); |
| 86 |
|
| 87 |
// Mangle the URL, if needed |
| 88 |
$url = (isset ($_SERVER['HTTPS']) && strtolower ($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://'; |
| 89 |
$url .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 90 |
$original = $url; |
| 91 |
|
| 92 |
if ($wp->canonical == 'www') |
| 93 |
$url = preg_replace ('@(https?)://(www)?\.?@', '$1://www.', $url); |
| 94 |
else if ($wp->canonical == 'nowww') |
| 95 |
$url = preg_replace ('@(https?)://(www)?\.?@', '$1://', $url); |
| 96 |
|
| 97 |
if ($wp->strip_index == 'yes') |
| 98 |
$url = preg_replace ('@index.(htm|html|php|asp|aspx|jsp)@', '', $url); |
| 99 |
|
| 100 |
if ($url != $original) |
| 101 |
wp_redirect ($url, 301); |
| 102 |
|
| 103 |
if ($this->matched) |
| 104 |
$this->matched->action->process_after ($this->matched->action_code, $original); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
function status_header ($status) |
| 110 |
{ |
| 111 |
// Fix for incorrect headers sent when using FastCGI/IIS |
| 112 |
if (substr (php_sapi_name (), 0, 3) == 'cgi') |
| 113 |
return str_replace ('HTTP/1.1', 'Status:', $status); |
| 114 |
return $status; |
| 115 |
} |
| 116 |
|
| 117 |
function send_headers ($obj) |
| 118 |
{ |
| 119 |
if ( !empty($this->matched) && $this->matched->type == '410') |
| 120 |
status_header (410); |
| 121 |
} |
| 122 |
|
| 123 |
function wp_redirect ($url, $status) |
| 124 |
{ |
| 125 |
global $wp_version, $is_IIS; |
| 126 |
if ( $wp_version < '2.1' ) { |
| 127 |
status_header( $status ); |
| 128 |
return $url; |
| 129 |
} elseif ( $is_IIS ) { |
| 130 |
header( "Refresh: 0;url=$url" ); |
| 131 |
return $url; |
| 132 |
} else { |
| 133 |
if ( $status == 301 && php_sapi_name() == 'cgi-fcgi' ) { |
| 134 |
$servers_to_check = array( 'lighttpd', 'nginx' ); |
| 135 |
foreach ( $servers_to_check as $name ) { |
| 136 |
if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) { |
| 137 |
status_header( $status ); |
| 138 |
header( "Location: $url" ); |
| 139 |
exit( 0 ); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
status_header( $status ); |
| 145 |
return $url; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// XXX |
| 150 |
function permalink_redirect_skip ($skip) |
| 151 |
{ |
| 152 |
// only want this if we:ve matched using redirection |
| 153 |
if ($this->matched) |
| 154 |
$skip[] = $_SERVER['REQUEST_URI']; |
| 155 |
return $skip; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
function load ($data) |
| 160 |
{ |
| 161 |
$mine = array ('canonical', 'strip_index', 'error_level', 'time_limit'); |
| 162 |
foreach ($mine AS $key) |
| 163 |
{ |
| 164 |
if (isset ($data[$key])) |
| 165 |
$this->$key = $data[$key]; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
function save ($data) |
| 170 |
{ |
| 171 |
$save = array |
| 172 |
( |
| 173 |
'canonical' => $data['canonical'], |
| 174 |
'strip_index' => $data['strip_index'], |
| 175 |
'error_level' => $data['error_level'], |
| 176 |
'time_limit' => $data['time_limit'], |
| 177 |
); |
| 178 |
|
| 179 |
$this->load ($save); |
| 180 |
return $save; |
| 181 |
} |
| 182 |
|
| 183 |
function config () |
| 184 |
{ |
| 185 |
?> |
| 186 |
<tr> |
| 187 |
<th><?php _e ('Canonical', 'redirection'); ?>:</th> |
| 188 |
<td> |
| 189 |
<select name="canonical"> |
| 190 |
<?php echo $this->select (array ('default' => __ ('Leave as is', 'redirection'), 'nowww' => sprintf (__ ('Strip WWW (%s)', 'redirection'), preg_replace ('@https?://(www)?@', '', get_bloginfo ('home'))), 'www' => sprintf (__ ('Force WWW (www.%s)', 'redirection'), preg_replace ('@https?://(www)?@', '', get_bloginfo ('home')))), $this->canonical); ?> |
| 191 |
</select> |
| 192 |
|
| 193 |
<strong><?php _e ('Strip Index', 'redirection'); ?>:</strong> |
| 194 |
<select name="strip_index"> |
| 195 |
<?php echo $this->select (array ('default' => __ ('Leave as is', 'redirection'), 'yes' => __ ('Strip index files (html,php,asp)', 'redirection')), $this->strip_index); ?> |
| 196 |
</select> |
| 197 |
</td> |
| 198 |
</tr> |
| 199 |
<tr> |
| 200 |
<th><?php _e ('Time Limit', 'redirection') ?>:</th> |
| 201 |
<td> |
| 202 |
<select name="time_limit"> |
| 203 |
<?php echo $this->select (array ('default' => __ ('Server default', 'redirection'), '30' => __ ('30 seconds', 'redirection'), '60' => __ ('1 minute', 'redirection'), '120' => __ ('2 minutes', 'redirection'), (5 * 60) => __ ('5 minutes', 'redirection'), '0' => __ ('As long as possible', 'redirection')), $this->time_limit); ?> |
| 204 |
</select> |
| 205 |
|
| 206 |
<strong><?php _e ('Error Level', 'redirection'); ?>:</strong> |
| 207 |
<select name="error_level"> |
| 208 |
<?php echo $this->select (array ('default' => __ ('Server default', 'redirection'), 'none' => 'No errors', 'error' => 'Show errors'), $this->error_level); ?> |
| 209 |
</select> |
| 210 |
</td> |
| 211 |
</tr> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
function is_valid () |
| 216 |
{ |
| 217 |
$perm = get_option ('permalink_structure'); |
| 218 |
if ($perm === false || $perm == '') |
| 219 |
return false; |
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
function options () |
| 224 |
{ |
| 225 |
if (!$this->is_valid ()) |
| 226 |
echo __ ('<strong>Disabled: You must enable <a href="options-permalink.php">permalinks</a> before using this</strong>', 'redirection'); |
| 227 |
else |
| 228 |
{ |
| 229 |
$options = array (); |
| 230 |
if ($this->canonical != 'default' && !empty ($this->canonical)) |
| 231 |
$options[] = ($this->canonical == 'nowww') ? 'strip WWW' : 'force WWW'; |
| 232 |
|
| 233 |
if ($this->strip_index != 'default' && !empty ($this->strip_index)) |
| 234 |
$options[] = __ ('strip index', 'redirection'); |
| 235 |
|
| 236 |
if ($this->time_limit != 'default') |
| 237 |
{ |
| 238 |
if ($this->time_limit == 0) |
| 239 |
$options[] = __ ('time limit set as long as possible', 'redirection'); |
| 240 |
else |
| 241 |
$options[] = sprintf (__ ('time limit at %ss', 'redirection'), $this->time_limit); |
| 242 |
} |
| 243 |
|
| 244 |
if ($this->error_level != 'default' && !empty ($this->error_level)) |
| 245 |
$options[] = ($this->error_level == 'none') ? __ ('no errors', 'redirection') : __ ('show errors', 'redirection'); |
| 246 |
|
| 247 |
if (count ($options) > 0) |
| 248 |
echo '<small>'.ucfirst (implode (', ', $options)).'</small>'; |
| 249 |
else |
| 250 |
echo __ ('<small>No options have been set</small>', 'redirection'); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
?> |