| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Htaccess { |
| 4 |
private $items = array(); |
| 5 |
const INSERT_REGEX = '@\n?# Created by Redirection(.*?)# End of Redirection\n?@sm'; |
| 6 |
|
| 7 |
private function encode_from( $url ) { |
| 8 |
return '^'.$this->encode( $url ).'$'; |
| 9 |
} |
| 10 |
|
| 11 |
private function encode2nd( $url ) { |
| 12 |
$url = urlencode( $url ); |
| 13 |
$url = str_replace( '%2F', '/', $url ); |
| 14 |
$url = str_replace( '%3A', ':', $url ); |
| 15 |
$url = str_replace( '+', '%20', $url ); |
| 16 |
$url = str_replace( '%24', '$', $url ); |
| 17 |
return $url; |
| 18 |
} |
| 19 |
|
| 20 |
private function encode( $url ) { |
| 21 |
$url = urlencode( $url ); |
| 22 |
$url = str_replace( '%2F', '/', $url ); |
| 23 |
$url = str_replace( '+', '%20', $url ); |
| 24 |
$url = str_replace( '.', '\\.', $url ); |
| 25 |
return $url; |
| 26 |
} |
| 27 |
|
| 28 |
private function encode_regex( $url ) { |
| 29 |
$url = str_replace( ' ', '%20', $url ); |
| 30 |
$url = str_replace( '.', '\\.', $url ); |
| 31 |
$url = str_replace( '\\.*', '.*', $url ); |
| 32 |
$url = str_replace( '%24', '$', $url ); |
| 33 |
return $url; |
| 34 |
} |
| 35 |
|
| 36 |
private function add_referrer( $item, $match ) { |
| 37 |
$from = $this->encode_from( ltrim( $item->get_url(), '/' ) ); |
| 38 |
if ( $item->is_regex() ) |
| 39 |
$from = $this->encode_regex( ltrim( $item->get_url(), '/' ) ); |
| 40 |
|
| 41 |
if ( ( $match->url_from || $match->url_notfrom ) && $match->referrer ) { |
| 42 |
$this->items[] = sprintf( 'RewriteCond %%{HTTP_REFERER} %s [NC]', ( $match->regex ? $this->encode_regex( $match->referrer ) : $this->encode_from( $match->referrer ) ) ); |
| 43 |
|
| 44 |
if ( $match->url_from ) { |
| 45 |
$to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->is_regex() ); |
| 46 |
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( $match->url_notfrom ) { |
| 50 |
$to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->is_regex() ); |
| 51 |
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
private function add_agent( $item, $match ) { |
| 57 |
$from = $this->encode( ltrim( $item->get_url(), '/' ) ); |
| 58 |
if ( $item->is_regex() ) |
| 59 |
$from = $this->encode_regex( ltrim( $item->get_url(), '/' ) ); |
| 60 |
|
| 61 |
if ( ( $match->url_from || $match->url_notfrom ) && $match->user_agent ) { |
| 62 |
$this->items[] = sprintf( 'RewriteCond %%{HTTP_USER_AGENT} %s [NC]', ( $match->regex ? $this->encode_regex( $match->user_agent ) : $this->encode2nd( $match->user_agent ) ) ); |
| 63 |
|
| 64 |
if ( $match->url_from ) { |
| 65 |
$to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->is_regex() ); |
| 66 |
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $match->url_notfrom ) { |
| 70 |
$to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->is_regex() ); |
| 71 |
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
private function add_url( $item, $match ) { |
| 77 |
$url = $item->get_url(); |
| 78 |
|
| 79 |
if ( $item->is_regex() == false && strpos( $url, '?') !== false || strpos( $url, '&' ) !== false ) { |
| 80 |
$url_parts = parse_url( $url ); |
| 81 |
$url = $url_parts['path']; |
| 82 |
$this->items[] = sprintf( 'RewriteCond %%{QUERY_STRING} ^%s$', $url_parts['query'] ); |
| 83 |
} |
| 84 |
|
| 85 |
$to = $this->target( $item->get_action_type(), $match->url, $item->get_action_code(), $item->is_regex() ); |
| 86 |
$from = $this->encode_from( $url ); |
| 87 |
|
| 88 |
if ( $item->is_regex() ) |
| 89 |
$from = $this->encode_regex( $item->get_url() ); |
| 90 |
|
| 91 |
if ( $to ) |
| 92 |
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to ); |
| 93 |
} |
| 94 |
|
| 95 |
private function action_random( $data, $code, $regex ) { |
| 96 |
// Pick a WP post at random |
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$post = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} ORDER BY RAND() LIMIT 0,1" ); |
| 100 |
$url = parse_url( get_permalink( $post ) ); |
| 101 |
|
| 102 |
return sprintf( '%s [R=%d,L]', $this->encode( $url['path'] ), $code ); |
| 103 |
} |
| 104 |
|
| 105 |
private function action_pass( $data, $code, $regex ) { |
| 106 |
if ( $regex ) |
| 107 |
return sprintf( '%s [L]', $this->encode2nd( $data ), $code ); |
| 108 |
return sprintf( '%s [L]', $this->encode2nd( $data ), $code ); |
| 109 |
} |
| 110 |
|
| 111 |
private function action_error( $data, $code, $regex) { |
| 112 |
if ( $code == '410' ) |
| 113 |
return '/ [G,L]'; |
| 114 |
return '/ [F,L]'; |
| 115 |
} |
| 116 |
|
| 117 |
private function action_url( $data, $code, $regex ) { |
| 118 |
if ( $regex ) |
| 119 |
return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code ); |
| 120 |
return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code ); |
| 121 |
} |
| 122 |
|
| 123 |
private function target( $action, $data, $code, $regex ) { |
| 124 |
$target = 'action_'.$action; |
| 125 |
|
| 126 |
if ( method_exists( $this, $target ) ) |
| 127 |
return $this->$target( $data, $code, $regex ); |
| 128 |
return ''; |
| 129 |
} |
| 130 |
|
| 131 |
private function generate() { |
| 132 |
if ( count( $this->items ) === 0 ) |
| 133 |
return ''; |
| 134 |
|
| 135 |
$version = get_plugin_data( dirname( dirname( __FILE__ ) ).'/redirection.php' ); |
| 136 |
|
| 137 |
$text[] = '# Created by Redirection'; |
| 138 |
$text[] = '# '.date ('r'); |
| 139 |
$text[] = '# Redirection '.trim( $version['Version'] ).' - http://urbangiraffe.com/plugins/redirection/'; |
| 140 |
$text[] = ''; |
| 141 |
|
| 142 |
// mod_rewrite section |
| 143 |
$text[] = '<IfModule mod_rewrite.c>'; |
| 144 |
|
| 145 |
// Add redirects |
| 146 |
$text = array_merge( $text, $this->items ); |
| 147 |
|
| 148 |
// End of mod_rewrite |
| 149 |
$text[] = '</IfModule>'; |
| 150 |
$text[] = ''; |
| 151 |
|
| 152 |
// End of redirection section |
| 153 |
$text[] = '# End of Redirection'; |
| 154 |
|
| 155 |
$text = implode( "\r\n", $text ); |
| 156 |
return "\n".$text."\n"; |
| 157 |
} |
| 158 |
|
| 159 |
public function add( $item ) { |
| 160 |
$target = 'add_'.$item->get_match_type(); |
| 161 |
|
| 162 |
if ( method_exists( $this, $target ) ) |
| 163 |
$this->$target( $item, $item->match ); |
| 164 |
} |
| 165 |
|
| 166 |
public function get( $existing = false ) { |
| 167 |
$text = $this->generate(); |
| 168 |
|
| 169 |
if ( $existing ) { |
| 170 |
if ( preg_match( self::INSERT_REGEX, $existing ) > 0 ) |
| 171 |
$text = preg_replace( self::INSERT_REGEX, $text, $existing ); |
| 172 |
else |
| 173 |
$text = trim( $existing )."\n".$text; |
| 174 |
} |
| 175 |
|
| 176 |
return trim( $text ); |
| 177 |
} |
| 178 |
|
| 179 |
public function save( $filename, $content_to_save = false ) { |
| 180 |
$existing = false; |
| 181 |
|
| 182 |
if ( file_exists( $filename ) ) |
| 183 |
$existing = @file_get_contents( $filename ); |
| 184 |
|
| 185 |
return @file_put_contents( $filename, $this->get( $existing ) ); |
| 186 |
} |
| 187 |
} |
| 188 |
|