| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Htaccess |
| 4 |
{ |
| 5 |
var $settings; |
| 6 |
var $items; |
| 7 |
|
| 8 |
function Red_Htaccess ($settings) |
| 9 |
{ |
| 10 |
foreach ($settings AS $key => $value) |
| 11 |
$this->settings[$key] = $value; |
| 12 |
} |
| 13 |
|
| 14 |
function encode_from ($url) |
| 15 |
{ |
| 16 |
return '^'.$this->encode ($url).'$'; |
| 17 |
} |
| 18 |
|
| 19 |
function encode2nd ($url) |
| 20 |
{ |
| 21 |
$url = urlencode ($url); |
| 22 |
$url = str_replace ('%2F', '/', $url); |
| 23 |
$url = str_replace ('%3A', ':', $url); |
| 24 |
$url = str_replace ('+', '%20', $url); |
| 25 |
$url = str_replace ('%24', '$', $url); |
| 26 |
return $url; |
| 27 |
} |
| 28 |
|
| 29 |
function encode ($url) |
| 30 |
{ |
| 31 |
$url = urlencode ($url); |
| 32 |
$url = str_replace ('%2F', '/', $url); |
| 33 |
$url = str_replace ('+', '%20', $url); |
| 34 |
$url = str_replace ('.', '\\.', $url); |
| 35 |
return $url; |
| 36 |
} |
| 37 |
|
| 38 |
function encode_regex ($url) |
| 39 |
{ |
| 40 |
$url = str_replace (' ', '%20', $url); |
| 41 |
$url = str_replace ('.', '\\.', $url); |
| 42 |
$url = str_replace ('\\.*', '.*', $url); |
| 43 |
$url = str_replace ('%24', '$', $url); |
| 44 |
return $url; |
| 45 |
} |
| 46 |
|
| 47 |
function add_referrer ($item, $match) |
| 48 |
{ |
| 49 |
$from = $this->encode_from (ltrim ($item->url, '/')); |
| 50 |
if ($item->regex) |
| 51 |
$from = $this->encode_regex (ltrim ($item->url, '/')); |
| 52 |
|
| 53 |
if (($match->url_from || $match->url_notfrom) && $match->referrer) |
| 54 |
{ |
| 55 |
$this->items[] = sprintf ('RewriteCond %%{HTTP_REFERER} %s [NC]', ($match->regex ? $this->encode_regex ($match->referrer) : $this->encode_from ($match->referrer))); |
| 56 |
|
| 57 |
if ($match->url_from) |
| 58 |
{ |
| 59 |
$to = $this->target ($item->action_type, $match->url_from, $item->action_code, $item->regex); |
| 60 |
$this->items[] = sprintf ('RewriteRule %s %s', $from, $to); |
| 61 |
} |
| 62 |
|
| 63 |
if ($match->url_notfrom) |
| 64 |
{ |
| 65 |
$to = $this->target ($item->action_type, $match->url_notfrom, $item->action_code, $item->regex); |
| 66 |
$this->items[] = sprintf ('RewriteRule %s %s', $from, $to); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
function add_agent ($item, $match) |
| 72 |
{ |
| 73 |
$from = $this->encode (ltrim ($item->url, '/')); |
| 74 |
if ($item->regex) |
| 75 |
$from = $this->encode_regex (ltrim ($item->url, '/')); |
| 76 |
|
| 77 |
if (($match->url_from || $match->url_notfrom) && $match->user_agent) |
| 78 |
{ |
| 79 |
$this->items[] = sprintf ('RewriteCond %%{HTTP_USER_AGENT} %s [NC]', ($match->regex ? $this->encode_regex ($match->user_agent) : $this->encode2nd ($match->user_agent))); |
| 80 |
|
| 81 |
if ($match->url_from) |
| 82 |
{ |
| 83 |
$to = $this->target ($item->action_type, $match->url_from, $item->action_code, $item->regex); |
| 84 |
$this->items[] = sprintf ('RewriteRule %s %s', $from, $to); |
| 85 |
} |
| 86 |
|
| 87 |
if ($match->url_notfrom) |
| 88 |
{ |
| 89 |
$to = $this->target ($item->action_type, $match->url_notfrom, $item->action_code, $item->regex); |
| 90 |
$this->items[] = sprintf ('RewriteRule %s %s', $from, $to); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
function add_url ($item, $match) |
| 96 |
{ |
| 97 |
$to = $this->target ($item->action_type, $match->url, $item->action_code, $item->regex); |
| 98 |
$from = $this->encode_from (ltrim ($item->url, '/')); |
| 99 |
if ($item->regex) |
| 100 |
$from = $this->encode_regex (ltrim ($item->url, '/')); |
| 101 |
|
| 102 |
if ($to) |
| 103 |
$this->items[] = sprintf ('RewriteRule %s %s', $from, $to); |
| 104 |
} |
| 105 |
|
| 106 |
function action_random ($data, $code, $regex) |
| 107 |
{ |
| 108 |
// Pick a WP post at random |
| 109 |
global $wpdb; |
| 110 |
|
| 111 |
$post = $wpdb->get_var ("SELECT ID FROM {$wpdb->posts} ORDER BY RAND() LIMIT 0,1"); |
| 112 |
$url = parse_url (get_permalink ($post)); |
| 113 |
|
| 114 |
return sprintf ('%s [R=%d,L]', $this->encode ($url['path']), $code); |
| 115 |
} |
| 116 |
|
| 117 |
function action_pass ($data, $code, $regex) |
| 118 |
{ |
| 119 |
if ($regex) |
| 120 |
return sprintf ('%s [L]', $this->encode2nd ($data), $code); |
| 121 |
else |
| 122 |
return sprintf ('%s [L]', $this->encode2nd ($data), $code); |
| 123 |
} |
| 124 |
|
| 125 |
function action_error ($data, $code, $regex) |
| 126 |
{ |
| 127 |
if ($code == '410') |
| 128 |
return '/ [G,L]'; |
| 129 |
return '/ [F,L]'; |
| 130 |
} |
| 131 |
|
| 132 |
function action_url ($data, $code, $regex) |
| 133 |
{ |
| 134 |
if ($regex) |
| 135 |
return sprintf ('%s [R=%d,L]', $this->encode2nd ($data), $code); |
| 136 |
else |
| 137 |
return sprintf ('%s [R=%d,L]', $this->encode2nd ($data), $code); |
| 138 |
} |
| 139 |
|
| 140 |
function target ($action, $data, $code, $regex) |
| 141 |
{ |
| 142 |
$target = 'action_'.$action; |
| 143 |
|
| 144 |
if (method_exists ($this, $target)) |
| 145 |
return $this->$target ($data, $code, $regex); |
| 146 |
return ''; |
| 147 |
} |
| 148 |
|
| 149 |
function add ($item) |
| 150 |
{ |
| 151 |
$target = 'add_'.$item->match_type; |
| 152 |
|
| 153 |
if (method_exists ($this, $target)) |
| 154 |
$this->$target ($item, $item->match); |
| 155 |
} |
| 156 |
|
| 157 |
function generate ($name) |
| 158 |
{ |
| 159 |
// Head of redirection section - do not localize this |
| 160 |
global $redirection; |
| 161 |
|
| 162 |
$text[] = '# Created by Redirection Module: '.$name; |
| 163 |
$text[] = '# '.date ('r'); |
| 164 |
$text[] = '# Redirection '.$redirection->version ().' - http://urbangiraffe.com/plugins/redirection/'; |
| 165 |
$text[] = ''; |
| 166 |
|
| 167 |
// Default blocked files - I can't think of a reason not to block these |
| 168 |
$text[] = '<Files .htaccess,.svn>'; |
| 169 |
$text[] = 'order allow,deny'; |
| 170 |
$text[] = 'deny from all'; |
| 171 |
$text[] = '</Files>'; |
| 172 |
$text[] = ''; |
| 173 |
|
| 174 |
// PHP options |
| 175 |
if (isset ($this->settings['error_level']) && $this->settings['error_level'] != 'default') |
| 176 |
$text[] = 'php_value error_reporting '.($this->settings == 'none' ? '0' : 'E_ALL'); |
| 177 |
|
| 178 |
if (isset ($this->settings['memory_limit']) && $this->settings['memory_limit'] != 0) |
| 179 |
$text[] = 'php_value memory_limit '.$this->settings['memory_limit'].'M'; |
| 180 |
|
| 181 |
if ($this->settings['allow_ip'] || $this->settings['ban_ip']) |
| 182 |
{ |
| 183 |
$text[] = ''; |
| 184 |
$text[] = 'order allow,deny'; |
| 185 |
if ($this->settings['ban_ip']) |
| 186 |
{ |
| 187 |
$ips = array_filter (explode (',', $this->settings['ban_ip'])); |
| 188 |
if (count ($ips) > 0) |
| 189 |
{ |
| 190 |
foreach ($ips AS $ip) |
| 191 |
$text[] = 'deny from '.$ip; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ($this->settings['allow_ip']) |
| 196 |
{ |
| 197 |
$ips = array_filter (explode (',', $this->settings['allow_ip'])); |
| 198 |
if (count ($ips) > 0) |
| 199 |
{ |
| 200 |
foreach ($ips AS $ip) |
| 201 |
$text[] = 'allow from '.$ip; |
| 202 |
} |
| 203 |
} |
| 204 |
else |
| 205 |
$text[] = 'allow from all'; |
| 206 |
} |
| 207 |
|
| 208 |
// mod_rewrite section |
| 209 |
$text[] = ''; |
| 210 |
$text[] = 'Options +FollowSymlinks'; |
| 211 |
$text[] = ''; |
| 212 |
$text[] = '<IfModule mod_rewrite.c>'; |
| 213 |
|
| 214 |
if ($this->settings['canonical'] != 'default') |
| 215 |
{ |
| 216 |
$text[] = 'RewriteEngine On'; |
| 217 |
$base = $this->settings['site']; |
| 218 |
if ($base == '') |
| 219 |
$base = get_option ('home'); |
| 220 |
|
| 221 |
$parts = parse_url ($base); |
| 222 |
$base = str_replace ('www.', '', $parts['host']); |
| 223 |
|
| 224 |
if ($this->settings['canonical'] == 'nowww') |
| 225 |
{ |
| 226 |
$text[] = 'RewriteCond %{HTTP_HOST} ^www\.'.str_replace ('.', '\\.', $base).'$ [NC]'; |
| 227 |
$text[] = 'RewriteRule ^(.*)$ http://'.$base.'/$1 [R=301,L]'; |
| 228 |
} |
| 229 |
else if ($this->settings['canonical'] == 'www') |
| 230 |
{ |
| 231 |
$text[] = 'RewriteCond %{HTTP_HOST} ^'.str_replace ('.', '\\.', $base).'$ [NC]'; |
| 232 |
$text[] = 'RewriteRule ^(.*)$ http://www.'.$base.'/$1 [R=301,L]'; |
| 233 |
} |
| 234 |
|
| 235 |
$text[] = ''; |
| 236 |
} |
| 237 |
|
| 238 |
if ($this->settings['strip_index'] == 'yes') |
| 239 |
{ |
| 240 |
$text[] = 'RewriteCond %{THE_REQUEST} (.*)index\.(php|htm|html)\ HTTP/'; |
| 241 |
$text[] = 'RewriteRule ^(.*)index\.(php|html|htm)$ $1 [R=301,NC,L]'; |
| 242 |
$text[] = ''; |
| 243 |
} |
| 244 |
|
| 245 |
// Add redirects |
| 246 |
if (is_array ($this->items)) |
| 247 |
$text = array_merge ($text, $this->items); |
| 248 |
|
| 249 |
// End of mod_rewrite |
| 250 |
$text[] = '</IfModule>'; |
| 251 |
$text[] = ''; |
| 252 |
|
| 253 |
if ($this->settings['raw']) |
| 254 |
$text[] = $this->settings['raw']; |
| 255 |
|
| 256 |
// End of redirection section |
| 257 |
$text[] = '# End of Redirection'; |
| 258 |
$text[] = ''; |
| 259 |
|
| 260 |
$text = implode ("\r\n", $text); |
| 261 |
$text = str_replace ("\r\n\r\n\r\n", "\r\n", $text); |
| 262 |
$text = str_replace ("\r\n\r\n\r\n", "\r\n", $text); |
| 263 |
return $text; |
| 264 |
} |
| 265 |
|
| 266 |
function save ($filename, $name) |
| 267 |
{ |
| 268 |
$text = $this->generate ($name); |
| 269 |
|
| 270 |
// Does the file already exist? |
| 271 |
if (file_exists ($filename)) |
| 272 |
{ |
| 273 |
$existing = @file_get_contents ($filename); |
| 274 |
|
| 275 |
// Remove any existing Redirection module |
| 276 |
$text .= preg_replace ('@# Created by Redirection Module: '.$name.'(.*?)# End of Redirection@s', '', $existing); |
| 277 |
} |
| 278 |
|
| 279 |
$file = @fopen ($filename, 'w'); |
| 280 |
if ($file) |
| 281 |
{ |
| 282 |
$text = str_replace ("\r\n\r\n\r\n", "\r\n", $text); |
| 283 |
$text = str_replace ("\r\n\r\n\r\n", "\r\n", $text); |
| 284 |
|
| 285 |
fwrite ($file, $text); |
| 286 |
fclose ($file); |
| 287 |
return true; |
| 288 |
} |
| 289 |
|
| 290 |
return false; |
| 291 |
} |
| 292 |
} |
| 293 |
|