PluginProbe
Redirection / 3.2
Redirection v3.2
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 3.2, at models/htaccess.php

229 lines 6.5 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
65 if ( ( $match->url_from || $match->url_notfrom ) && $match->referrer ) {
66 $this->items[] = sprintf( 'RewriteCond %%{HTTP_REFERER} %s [NC]', ( $match->regex ? $this->encode_regex( $match->referrer ) : $this->encode_from( $match->referrer ) ) );
67
68 if ( $match->url_from ) {
69 $to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->is_regex() );
70 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
71 }
72
73 if ( $match->url_notfrom ) {
74 $to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->is_regex() );
75 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
76 }
77 }
78 }
79
80 private function add_agent( $item, $match ) {
81 $from = $this->encode( ltrim( $item->get_url(), '/' ) );
82 if ( $item->is_regex() )
83 $from = $this->encode_regex( ltrim( $item->get_url(), '/' ) );
84
85 if ( ( $match->url_from || $match->url_notfrom ) && $match->user_agent ) {
86 $this->items[] = sprintf( 'RewriteCond %%{HTTP_USER_AGENT} %s [NC]', ( $match->regex ? $this->encode_regex( $match->user_agent ) : $this->encode2nd( $match->user_agent ) ) );
87
88 if ( $match->url_from ) {
89 $to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->is_regex() );
90 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
91 }
92
93 if ( $match->url_notfrom ) {
94 $to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->is_regex() );
95 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
96 }
97 }
98 }
99
100 private function add_url( $item, $match ) {
101 $url = $item->get_url();
102
103 if ( $item->is_regex() === false && strpos( $url, '?' ) !== false || strpos( $url, '&' ) !== false ) {
104 $url_parts = parse_url( $url );
105 $url = $url_parts['path'];
106 $this->items[] = sprintf( 'RewriteCond %%{QUERY_STRING} ^%s$', $url_parts['query'] );
107 }
108
109 $to = $this->target( $item->get_action_type(), $match->url, $item->get_action_code(), $item->is_regex() );
110 $from = $this->encode_from( $url );
111
112 if ( $item->is_regex() ) {
113 $from = $this->encode_regex( $item->get_url() );
114 }
115
116 if ( $to ) {
117 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
118 }
119 }
120
121 private function action_random( $data, $code, $regex ) {
122 // Pick a WP post at random
123 global $wpdb;
124
125 $post = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} ORDER BY RAND() LIMIT 0,1" );
126 $url = parse_url( get_permalink( $post ) );
127
128 return sprintf( '%s [R=%d,L]', $this->encode( $url['path'] ), $code );
129 }
130
131 private function action_pass( $data, $code, $regex ) {
132 if ( $regex ) {
133 return sprintf( '%s [L]', $this->encode2nd( $data ), $code );
134 }
135 return sprintf( '%s [L]', $this->encode2nd( $data ), $code );
136 }
137
138 private function action_error( $data, $code, $regex ) {
139 if ( $code === 410 ) {
140 return '/ [G]';
141 }
142 return '/ [F]';
143 }
144
145 private function action_url( $data, $code, $regex ) {
146 if ( $regex ) {
147 return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code );
148 }
149 return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code );
150 }
151
152 private function target( $action, $data, $code, $regex ) {
153 $target = 'action_'.$action;
154
155 if ( method_exists( $this, $target ) ) {
156 return $this->$target( $data, $code, $regex );
157 }
158 return '';
159 }
160
161 private function generate() {
162 $version = get_plugin_data( dirname( dirname( __FILE__ ) ).'/redirection.php' );
163
164 if ( count( $this->items ) === 0 ) {
165 return '';
166 }
167
168 $text[] = '# Created by Redirection';
169 $text[] = '# '.date( 'r' );
170 $text[] = '# Redirection '.trim( $version['Version'] ).' - https://redirection.me';
171 $text[] = '';
172
173 // mod_rewrite section
174 $text[] = '<IfModule mod_rewrite.c>';
175
176 // Add redirects
177 $text = array_merge( $text, $this->items );
178
179 // End of mod_rewrite
180 $text[] = '</IfModule>';
181 $text[] = '';
182
183 // End of redirection section
184 $text[] = '# End of Redirection';
185
186 $text = implode( "\r\n", $text );
187 return "\n".$text."\n";
188 }
189
190 public function add( $item ) {
191 $target = 'add_'.$item->get_match_type();
192
193 if ( method_exists( $this, $target ) )
194 $this->$target( $item, $item->match );
195 }
196
197 public function get( $existing = false ) {
198 $text = $this->generate();
199
200 if ( $existing ) {
201 if ( preg_match( self::INSERT_REGEX, $existing ) > 0 ) {
202 $text = preg_replace( self::INSERT_REGEX, str_replace( '$', '\\$', $text ), $existing );
203 } else {
204 $text = trim( $existing )."\n".$text;
205 }
206 }
207
208 return trim( $text );
209 }
210
211 public function save( $filename, $content_to_save = false ) {
212 $existing = false;
213
214 if ( file_exists( $filename ) ) {
215 $existing = file_get_contents( $filename );
216 }
217
218 $file = @fopen( $filename, 'w' );
219 if ( $file ) {
220 $result = fwrite( $file, $this->get( $existing ) );
221 fclose( $file );
222
223 return $result !== false;
224 }
225
226 return false;
227 }
228 }
229