PluginProbe
Redirection / 2.3.8
Redirection v2.3.8
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.8, at models/htaccess.php

192 lines 5.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 var $items;
5
6 function encode_from( $url ) {
7 return '^'.$this->encode( $url ).'$';
8 }
9
10 function encode2nd( $url ) {
11 $url = urlencode( $url );
12 $url = str_replace( '%2F', '/', $url );
13 $url = str_replace( '%3A', ':', $url );
14 $url = str_replace( '+', '%20', $url );
15 $url = str_replace( '%24', '$', $url );
16 return $url;
17 }
18
19 function encode( $url ) {
20 $url = urlencode( $url );
21 $url = str_replace( '%2F', '/', $url );
22 $url = str_replace( '+', '%20', $url );
23 $url = str_replace( '.', '\\.', $url );
24 return $url;
25 }
26
27 function encode_regex( $url ) {
28 $url = str_replace( ' ', '%20', $url );
29 $url = str_replace( '.', '\\.', $url );
30 $url = str_replace( '\\.*', '.*', $url );
31 $url = str_replace( '%24', '$', $url );
32 return $url;
33 }
34
35 function add_referrer( $item, $match ) {
36 $from = $this->encode_from( ltrim( $item->url, '/' ) );
37 if ( $item->regex )
38 $from = $this->encode_regex( ltrim( $item->url, '/' ) );
39
40 if ( ( $match->url_from || $match->url_notfrom ) && $match->referrer ) {
41 $this->items[] = sprintf( 'RewriteCond %%{HTTP_REFERER} %s [NC]', ( $match->regex ? $this->encode_regex( $match->referrer ) : $this->encode_from( $match->referrer ) ) );
42
43 if ( $match->url_from ) {
44 $to = $this->target( $item->action_type, $match->url_from, $item->action_code, $item->regex );
45 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
46 }
47
48 if ( $match->url_notfrom ) {
49 $to = $this->target( $item->action_type, $match->url_notfrom, $item->action_code, $item->regex );
50 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
51 }
52 }
53 }
54
55 function add_agent( $item, $match ) {
56 $from = $this->encode( ltrim( $item->url, '/' ) );
57 if ( $item->regex )
58 $from = $this->encode_regex( ltrim( $item->url, '/' ) );
59
60 if ( ( $match->url_from || $match->url_notfrom ) && $match->user_agent ) {
61 $this->items[] = sprintf( 'RewriteCond %%{HTTP_USER_AGENT} %s [NC]', ( $match->regex ? $this->encode_regex( $match->user_agent ) : $this->encode2nd( $match->user_agent ) ) );
62
63 if ( $match->url_from ) {
64 $to = $this->target( $item->action_type, $match->url_from, $item->action_code, $item->regex );
65 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
66 }
67
68 if ( $match->url_notfrom ) {
69 $to = $this->target( $item->action_type, $match->url_notfrom, $item->action_code, $item->regex );
70 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
71 }
72 }
73 }
74
75 function add_url( $item, $match ) {
76 $to = $this->target( $item->action_type, $match->url, $item->action_code, $item->regex );
77 $from = $this->encode_from( ltrim( $item->url, '/' ) );
78 if ( $item->regex )
79 $from = $this->encode_regex( ltrim ($item->url, '/' ) );
80
81 if ( $to )
82 $this->items[] = sprintf( 'RewriteRule %s %s', $from, $to );
83 }
84
85 function action_random( $data, $code, $regex ) {
86 // Pick a WP post at random
87 global $wpdb;
88
89 $post = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} ORDER BY RAND() LIMIT 0,1" );
90 $url = parse_url( get_permalink( $post ) );
91
92 return sprintf( '%s [R=%d,L]', $this->encode( $url['path'] ), $code );
93 }
94
95 function action_pass( $data, $code, $regex ) {
96 if ( $regex )
97 return sprintf( '%s [L]', $this->encode2nd( $data ), $code );
98 return sprintf( '%s [L]', $this->encode2nd( $data ), $code );
99 }
100
101 function action_error( $data, $code, $regex) {
102 if ( $code == '410' )
103 return '/ [G,L]';
104 return '/ [F,L]';
105 }
106
107 function action_url( $data, $code, $regex ) {
108 if ( $regex )
109 return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code );
110 return sprintf( '%s [R=%d,L]', $this->encode2nd( $data ), $code );
111 }
112
113 function target( $action, $data, $code, $regex ) {
114 $target = 'action_'.$action;
115
116 if ( method_exists( $this, $target ) )
117 return $this->$target( $data, $code, $regex );
118 return '';
119 }
120
121 function add( $item ) {
122 $target = 'add_'.$item->match_type;
123
124 if ( method_exists( $this, $target ) )
125 $this->$target( $item, $item->match );
126 }
127
128 function generate() {
129 // Head of redirection section - do not localize this
130 global $redirection;
131
132 $text[] = '# Created by Redirection';
133 $text[] = '# '.date ('r');
134 $text[] = '# Redirection '.$redirection->version().' - http://urbangiraffe.com/plugins/redirection/';
135 $text[] = '';
136
137 // Default blocked files - I can't think of a reason not to block these
138 $text[] = '<Files .htaccess,.svn>';
139 $text[] = 'order allow,deny';
140 $text[] = 'deny from all';
141 $text[] = '</Files>';
142 $text[] = '';
143
144 // mod_rewrite section
145 $text[] = '';
146 $text[] = 'Options +FollowSymlinks';
147 $text[] = '';
148 $text[] = '<IfModule mod_rewrite.c>';
149
150 // Add redirects
151 if ( is_array( $this->items ) )
152 $text = array_merge( $text, $this->items );
153
154 // End of mod_rewrite
155 $text[] = '</IfModule>';
156 $text[] = '';
157
158 // End of redirection section
159 $text[] = '# End of Redirection';
160 $text[] = '';
161
162 $text = implode( "\r\n", $text );
163 $text = str_replace( "\r\n\r\n\r\n", "\r\n", $text );
164 $text = str_replace( "\r\n\r\n\r\n", "\r\n", $text );
165 return $text;
166 }
167
168 function save( $filename, $name ) {
169 $text = $this->generate( $name );
170
171 // Does the file already exist?
172 if ( file_exists( $filename) ) {
173 $existing = @file_get_contents( $filename );
174
175 // Remove any existing Redirection module
176 $text .= preg_replace( '@# Created by Redirection Module: '.$name.'(.*?)# End of Redirection@s', '', $existing );
177 }
178
179 $file = @fopen( $filename, 'w' );
180 if ( $file ) {
181 $text = str_replace( "\r\n\r\n\r\n", "\r\n", $text );
182 $text = str_replace( "\r\n\r\n\r\n", "\r\n", $text );
183
184 fwrite( $file, $text );
185 fclose( $file );
186 return true;
187 }
188
189 return false;
190 }
191 }
192