htaccess.php
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
* Convert redirects to .htaccess format
*
* Ignores:
* - Trailing slash flag
* - Query flags
*/
class Red_Htaccess {
/**
* Array of redirect lines
*
* @var array<string>
*/
private $items = array();
const INSERT_REGEX = '@\n?# Created by Redirection(?:.*?)# End of Redirection\n?@sm';
/**
* Encode the 'from' URL
*
* @param string $url From URL.
* @param bool $ignore_trailing Ignore trailing slashes.
* @return string
*/
private function encode_from( $url, $ignore_trailing ) {
$url = $this->encode( $url );
// Apache 2 does not need a leading slashing
$url = ltrim( $url, '/' );
if ( $ignore_trailing ) {
$url = rtrim( $url, '/' ) . '/?';
}
// Exactly match the URL
return '^' . $url . '$';
}
/**
* URL encode some things, but other things can be passed through
*
* @param string $url URL.
* @return string
*/
private function encode2nd( $url ) {
$allowed = [
'%2F' => '/',
'%3F' => '?',
'%3A' => ':',
'%3D' => '=',
'%26' => '&',
'%25' => '%',
'+' => '%20',
'%24' => '$',
'%23' => '#',
];
$url = rawurlencode( $url );
return $this->replace_encoding( $url, $allowed );
}
/**
* Replace encoded characters in a URL
*
* @param string $str Source string.
* @param array $allowed Allowed encodings.
* @return string
*/
private function replace_encoding( $str, $allowed ) {
foreach ( $allowed as $before => $after ) {
$str = str_replace( $before, $after, $str );
}
return $str;
}
/**
* Encode a URL
*
* @param string $url URL.
* @return string
*/
private function encode( $url ) {
$allowed = [
'%2F' => '/',
'%3F' => '?',
'+' => '%20',
'.' => '\\.',
];
return $this->replace_encoding( rawurlencode( $url ), $allowed );
}
/**
* Encode a regex URL
*
* @param string $url URL.
* @return string
*/
private function encode_regex( $url ) {
// Remove any newlines
$url = preg_replace( "/[\r\n\t].*?$/s", '', $url );
// Remove invalid characters
$url = preg_replace( '/[^\PC\s]/u', '', $url );
// Make sure spaces are quoted
$url = str_replace( ' ', '%20', $url );
$url = str_replace( '%24', '$', $url );
// No leading slash
$url = ltrim( $url, '/' );
// If pattern has a ^ at the start then ensure we don't have a slash immediatley after
$url = preg_replace( '@^\^/@', '^', $url );
return $url;
}
/**
* Add a referrer redirect
*
* @param Red_Item $item Redirect item.
* @param Referrer_Match $match Redirect match.
* @return void
*/
private function add_referrer( $item, $match ) {
$from = $this->encode_from( ltrim( $item->get_url(), '/' ), $item->source_flags && $item->source_flags->is_ignore_trailing() );
if ( $item->is_regex() ) {
$from = $this->encode_regex( ltrim( $item->get_url(), '/' ) );
}
if ( ( $match->url_from || $match->url_notfrom ) && $match->referrer ) {
$referrer = $match->regex ? $this->encode_regex( $match->referrer ) : $this->encode_from( $match->referrer, false );
$to = false;
if ( $match->url_from ) {
$to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->get_match_data() );
}
if ( $match->url_notfrom ) {
$to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->get_match_data() );
}
$this->items[] = sprintf( 'RewriteCond %%{HTTP_REFERER} %s [NC]', $referrer );
if ( $to ) {
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
}
}
}
/**
* Add a useragent redirect
*
* @param Red_Item $item Redirect item.
* @param Agent_Match $match Redirect match.
* @return void
*/
private function add_agent( $item, $match ) {
$from = $this->encode( ltrim( $item->get_url(), '/' ) );
if ( $item->is_regex() ) {
$from = $this->encode_regex( ltrim( $item->get_url(), '/' ) );
}
if ( ( $match->url_from || $match->url_notfrom ) && $match->agent ) {
$agent = ( $match->regex ? $this->encode_regex( $match->agent ) : $this->encode2nd( $match->agent ) );
$to = false;
if ( $match->url_from ) {
$to = $this->target( $item->get_action_type(), $match->url_from, $item->get_action_code(), $item->get_match_data() );
}
if ( $match->url_notfrom ) {
$to = $this->target( $item->get_action_type(), $match->url_notfrom, $item->get_action_code(), $item->get_match_data() );
}
$this->items[] = sprintf( 'RewriteCond %%{HTTP_USER_AGENT} %s [NC]', $agent );
if ( $to ) {
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
}
}
}
/**
* Add a server redirect
*
* @param Red_Item $item Redirect item.
* @param Server_Match $match Redirect match.
* @return void
*/
private function add_server( $item, $match ) {
$match->url = $match->url_from;
$this->items[] = sprintf( 'RewriteCond %%{HTTP_HOST} ^%s$ [NC]', preg_quote( wp_parse_url( $match->server, PHP_URL_HOST ), '/' ) );
$this->add_url( $item, $match );
}
/**
* Add a redirect
*
* @param Red_Item $item Redirect item.
* @param Red_Match $match Redirect match.
* @return void
*/
private function add_url( $item, $match ) {
$url = $item->get_url();
if ( $item->is_regex() === false && strpos( $url, '?' ) !== false ) {
$url_parts = wp_parse_url( $url );
if ( isset( $url_parts['path'] ) ) {
$url = $url_parts['path'];
$query = isset( $url_parts['query'] ) ? $url_parts['query'] : '';
$this->items[] = sprintf( 'RewriteCond %%{QUERY_STRING} ^%s$', $query );
}
}
$to = $this->target( $item->get_action_type(), $match->url, $item->get_action_code(), $item->get_match_data() );
$from = $this->encode_from( $url, $item->source_flags && $item->source_flags->is_ignore_trailing() );
if ( $item->is_regex() ) {
$from = $this->encode_regex( $item->get_url() );
}
if ( $to ) {
$this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
}
}
/**
* Add a redirect flags
*
* @return string
*/
private function add_flags( $current, array $flags ) {
return $current . ' [' . implode( ',', $flags ) . ']';
}
/**
* Get source flags
*
* @param array<string> $existing Existing flags.
* @param array<string> $source Source flags.
* @param string $url URL.
* @return array<string>
*/
private function get_source_flags( array $existing, array $source, $url ) {
$flags = [];
if ( isset( $source['flag_case'] ) && $source['flag_case'] ) {
$flags[] = 'NC';
}
if ( isset( $source['flag_query'] ) && $source['flag_query'] === 'pass' ) {
$flags[] = 'QSA';
}
if ( strpos( $url, '#' ) !== false || strpos( $url, '%' ) !== false ) {
$flags[] = 'NE';
}
return array_merge( $existing, $flags );
}
/**
* Add a random target.
*
* @param [type] $data
* @param [type] $code
* @param [type] $match_data
* @return string
*/
private function action_random( $data, $code, $match_data ) {
// Pick a WP post at random
global $wpdb;
$post = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} ORDER BY RAND() LIMIT 0,1" );
$url = wp_parse_url( get_permalink( $post ) );
$flags = [ sprintf( 'R=%d', $code ) ];
$flags[] = 'L';
$flags = $this->get_source_flags( $flags, $match_data['source'], $data );
return $this->add_flags( $this->encode( $url['path'] ), $flags );
}
/**
* Add a passthrough target.
*
* @param [type] $data
* @param [type] $code
* @param [type] $match_data
* @return string
*/
private function action_pass( $data, $code, $match_data ) {
$flags = $this->get_source_flags( [ 'L' ], $match_data['source'], $data );
return $this->add_flags( $this->encode2nd( $data ), $flags );
}
/**
* Add an error target.
*
* @param [type] $data
* @param [type] $code
* @param [type] $match_data
* @return string
*/
private function action_error( $data, $code, $match_data ) {
$flags = $this->get_source_flags( [ 'F' ], $match_data['source'], $data );
if ( $code === 410 ) {
$flags = $this->get_source_flags( [ 'G' ], $match_data['source'], $data );
}
return $this->add_flags( '/', $flags );
}
/**
* Add a URL target.
*
* @param [type] $data
* @param [type] $code
* @param [type] $match_data
* @return string
*/
private function action_url( $data, $code, $match_data ) {
$flags = [ sprintf( 'R=%d', $code ) ];
$flags[] = 'L';
$flags = $this->get_source_flags( $flags, $match_data['source'], $data );
return $this->add_flags( $this->encode2nd( $data ), $flags );
}
/**
* Return URL target
*
* @param [type] $data
* @param [type] $code
* @param [type] $match_data
* @return string
*/
private function target( $action, $data, $code, $match_data ) {
$target = 'action_' . $action;
if ( method_exists( $this, $target ) ) {
return $this->$target( $data, $code, $match_data );
}
return '';
}
/**
* Generate the .htaccess file in memory
*
* @return string
*/
private function generate() {
$version = red_get_plugin_data( dirname( dirname( __FILE__ ) ) . '/redirection.php' );
if ( count( $this->items ) === 0 ) {
return '';
}
$text = [
'# Created by Redirection',
'# ' . date( 'r' ),
'# Redirection ' . trim( $version['Version'] ) . ' - https://redirection.me',
'',
'<IfModule mod_rewrite.c>',
];
// Add http => https option
$options = red_get_options();
if ( $options['https'] ) {
$text[] = 'RewriteCond %{HTTPS} off';
$text[] = 'RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]';
}
// Add redirects
$text = array_merge( $text, array_filter( array_map( [ $this, 'sanitize_redirect' ], $this->items ) ) );
// End of mod_rewrite
$text[] = '</IfModule>';
$text[] = '';
// End of redirection section
$text[] = '# End of Redirection';
$text = implode( "\n", $text );
return "\n" . $text . "\n";
}
/**
* Add a redirect to the file
*
* @param Red_Item $item Redirect.
* @return void
*/
public function add( $item ) {
$target = 'add_' . $item->get_match_type();
if ( method_exists( $this, $target ) && $item->is_enabled() ) {
$this->$target( $item, $item->match );
}
}
/**
* Get the .htaccess file
*
* @param boolean $existing Existing .htaccess data.
* @return string
*/
public function get( $existing = false ) {
$text = $this->generate();
if ( $existing ) {
if ( preg_match( self::INSERT_REGEX, $existing ) > 0 ) {
$text = preg_replace( self::INSERT_REGEX, str_replace( '$', '\\$', $text ), $existing );
} else {
$text = $text . "\n" . trim( $existing );
}
}
return trim( $text );
}
/**
* Sanitize the redirect
*
* @param string $text Text.
* @return string
*/
public function sanitize_redirect( $text ) {
return str_replace( [ '<?', '>' ], '', $text );
}
/**
* Sanitize the filename
*
* @param string $filename Filename.
* @return string
*/
public function sanitize_filename( $filename ) {
return str_replace( '.php', '', $filename );
}
/**
* Save the .htaccess to a file
*
* @param string $filename Filename to save.
* @param boolean $content_to_save Content to save.
* @return bool
*/
public function save( $filename, $content_to_save = false ) {
$existing = false;
$filename = $this->sanitize_filename( $filename );
if ( file_exists( $filename ) ) {
$existing = file_get_contents( $filename );
}
$file = @fopen( $filename, 'w' );
if ( $file ) {
$result = fwrite( $file, $this->get( $existing ) );
fclose( $file );
return $result !== false;
}
return false;
}
}