PluginProbe
Redirection / 2.6
Redirection v2.6
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / htaccess.php

htaccess.php in Redirection 2.6, at models/htaccess.php

213 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $url = $this->encode( $url );
9
10 // Apache 2 does not need a leading slashing
11 $url = ltrim( $url, '/' );
12
13 // Exactly match the URL
14 return '^'.$url.'$';
15 }
16
17 private function encode2nd( $url ) {
18 $url = urlencode( $url );
19 $url = str_replace( '%2F', '/', $url );
20 $url = str_replace( '%3F', '?', $url );
21 $url = str_replace( '%3A', ':', $url );
22 $url = str_replace( '%3D', '=', $url );
23 $url = str_replace( '%26', '&', $url );
24 $url = str_replace( '%25', '%', $url );
25 $url = str_replace( '+', '%20', $url );
26 $url = str_replace( '%24', '$', $url );
27 return $url;
28 }
29
30 private function encode( $url ) {
31 $url = urlencode( $url );
32 $url = str_replace( '%2F', '/', $url );
33 $url = str_replace( '%3F', '?', $url );
34 $url = str_replace( '+', '%20', $url );
35 $url = str_replace( '.', '\\.', $url );
36 return $url;
37 }
38
39 private function encode_regex( $url ) {
40 // Remove any newlines
41 $url = preg_replace( "/[\r\n\t].*?$/s", '', $url );
42
43 // Remove invalid characters
44 $url = preg_replace( '/[^\PC\s]/u', '', $url );
45
46 // Make sure spaces are quoted
47 $url = str_replace( ' ', '%20', $url );
48 $url = str_replace( '%24', '$', $url );
49
50 // No leading slash
51 $url = ltrim( $url, '/' );
52
53 // If pattern has a ^ at the start then ensure we don't have a slash immediatley after
54 $url = preg_replace( '@^\^/@', '^', $url );
55
56 return $url;
57 }
58
59 private function add_referrer( $item, $match ) {
60 $from = $this->encode_from( ltrim( $item->get_url(), '/' ) );
61 if ( $item->is_regex() )
62 $from = $this->encode_regex( ltrim( $item->get_url(), '/' ) );
63
64 if ( ( $match->url_from || $match->url_notfrom ) && $match->referrer ) {
65 $this->items[] = sprintf( 'RewriteCond %%{HTTP_REFERER} %s [NC]', ( $match->regex ? $this->encode_regex( $match->referrer ) : $this->encode_from( $match->referrer ) ) );
66
67 if ( $match->url_from ) {
68 $to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->is_regex() );
69 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
70 }
71
72 if ( $match->url_notfrom ) {
73 $to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->is_regex() );
74 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
75 }
76 }
77 }
78
79 private function add_agent( $item, $match ) {
80 $from = $this->encode( ltrim( $item->get_url(), '/' ) );
81 if ( $item->is_regex() )
82 $from = $this->encode_regex( ltrim( $item->get_url(), '/' ) );
83
84 if ( ( $match->url_from || $match->url_notfrom ) && $match->user_agent ) {
85 $this->items[] = sprintf( 'RewriteCond %%{HTTP_USER_AGENT} %s [NC]', ( $match->regex ? $this->encode_regex( $match->user_agent ) : $this->encode2nd( $match->user_agent ) ) );
86
87 if ( $match->url_from ) {
88 $to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->is_regex() );
89 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
90 }
91
92 if ( $match->url_notfrom ) {
93 $to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->is_regex() );
94 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
95 }
96 }
97 }
98
99 private function add_url( $item, $match ) {
100 $url = $item->get_url();
101
102 if ( $item->is_regex() === false && strpos( $url, '?' ) !== false || strpos( $url, '&' ) !== false ) {
103 $url_parts = parse_url( $url );
104 $url = $url_parts['path'];
105 $this->items[] = sprintf( 'RewriteCond %%{QUERY_STRING} ^%s$', $url_parts['query'] );
106 }
107
108 $to = $this->target( $item->get_action_type(), $match->url, $item->get_action_code(), $item->is_regex() );
109 $from = $this->encode_from( $url );
110
111 if ( $item->is_regex() ) {
112 $from = $this->encode_regex( $item->get_url() );
113 }
114
115 if ( $to )
116 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
117 }
118
119 private function action_random( $data, $code, $regex ) {
120 // Pick a WP post at random
121 global $wpdb;
122
123 $post = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} ORDER BY RAND() LIMIT 0,1" );
124 $url = parse_url( get_permalink( $post ) );
125
126 return sprintf( '%s [R=%d,L]', $this->encode( $url['path'] ), $code );
127 }
128
129 private function action_pass( $data, $code, $regex ) {
130 if ( $regex )
131 return sprintf( '%s [L]', $this->encode2nd( $data ), $code );
132 return sprintf( '%s [L]', $this->encode2nd( $data ), $code );
133 }
134
135 private function action_error( $data, $code, $regex ) {
136 if ( $code === 410 )
137 return '/ [G]';
138 return '/ [F]';
139 }
140
141 private function action_url( $data, $code, $regex ) {
142 if ( $regex )
143 return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code );
144 return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code );
145 }
146
147 private function target( $action, $data, $code, $regex ) {
148 $target = 'action_'.$action;
149
150 if ( method_exists( $this, $target ) )
151 return $this->$target( $data, $code, $regex );
152 return '';
153 }
154
155 private function generate() {
156 if ( count( $this->items ) === 0 )
157 return '';
158
159 $version = get_plugin_data( dirname( dirname( __FILE__ ) ).'/redirection.php' );
160
161 $text[] = '# Created by Redirection';
162 $text[] = '# '.date( 'r' );
163 $text[] = '# Redirection '.trim( $version['Version'] ).' - http://urbangiraffe.com/plugins/redirection/';
164 $text[] = '';
165
166 // mod_rewrite section
167 $text[] = '<IfModule mod_rewrite.c>';
168
169 // Add redirects
170 $text = array_merge( $text, $this->items );
171
172 // End of mod_rewrite
173 $text[] = '</IfModule>';
174 $text[] = '';
175
176 // End of redirection section
177 $text[] = '# End of Redirection';
178
179 $text = implode( "\r\n", $text );
180 return "\n".$text."\n";
181 }
182
183 public function add( $item ) {
184 $target = 'add_'.$item->get_match_type();
185
186 if ( method_exists( $this, $target ) )
187 $this->$target( $item, $item->match );
188 }
189
190 public function get( $existing = false ) {
191 $text = $this->generate();
192
193 if ( $existing ) {
194 if ( preg_match( self::INSERT_REGEX, $existing ) > 0 )
195 $text = preg_replace( self::INSERT_REGEX, $text, $existing );
196 else
197 $text = trim( $existing )."\n".$text;
198 }
199
200 return trim( $text );
201 }
202
203 public function save( $filename, $content_to_save = false ) {
204 $existing = false;
205
206 if ( file_exists( $filename ) ) {
207 $existing = file_get_contents( $filename );
208 }
209
210 return file_put_contents( $filename, $this->get( $existing ) );
211 }
212 }
213