PluginProbe
Redirection / 2.3.5
Redirection v2.3.5
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.3.5, at models/htaccess.php

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