PluginProbe
Redirection / 2.4.5
Redirection v2.4.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.4.5, at models/htaccess.php

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