PluginProbe
Redirection / 4.3.3
Redirection v4.3.3
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 / fileio / apache.php

apache.php in Redirection 4.3.3, at fileio/apache.php

183 lines 4.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_Apache_File extends Red_FileIO {
4 public function force_download() {
5 parent::force_download();
6
7 header( 'Content-Type: application/octet-stream' );
8 header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'htaccess' ) . '"' );
9 }
10
11 public function get_data( array $items, array $groups ) {
12 include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php';
13
14 $htaccess = new Red_Htaccess();
15
16 foreach ( $items as $item ) {
17 $htaccess->add( $item );
18 }
19
20 return $htaccess->get() . PHP_EOL;
21 }
22
23 public function load( $group, $filename, $data ) {
24 // Remove any comments
25 $data = str_replace( "\n", "\r", $data );
26
27 // Split it into lines
28 $lines = array_filter( explode( "\r", $data ) );
29 $count = 0;
30
31 foreach ( (array) $lines as $line ) {
32 $item = $this->get_as_item( $line );
33
34 if ( $item ) {
35 $item['group_id'] = $group;
36 $redirect = Red_Item::create( $item );
37
38 if ( ! is_wp_error( $redirect ) ) {
39 $count++;
40 }
41 }
42 }
43
44 return $count;
45 }
46
47 public function get_as_item( $line ) {
48 $item = false;
49
50 if ( preg_match( '@rewriterule\s+(.*?)\s+(.*?)\s+(\[.*\])*@i', $line, $matches ) > 0 ) {
51 $item = array(
52 'url' => $this->regex_url( $matches[1] ),
53 'match_type' => 'url',
54 'action_type' => 'url',
55 'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ),
56 'action_code' => $this->get_code( $matches[3] ),
57 'regex' => $this->is_regex( $matches[1] ),
58 );
59 } elseif ( preg_match( '@Redirect\s+(.*?)\s+"(.*?)"\s+(.*)@i', $line, $matches ) > 0 || preg_match( '@Redirect\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
60 $item = array(
61 'url' => $this->decode_url( $matches[2] ),
62 'match_type' => 'url',
63 'action_type' => 'url',
64 'action_data' => array( 'url' => $this->decode_url( $matches[3] ) ),
65 'action_code' => $this->get_code( $matches[1] ),
66 );
67 } elseif ( preg_match( '@Redirect\s+"(.*?)"\s+(.*)@i', $line, $matches ) > 0 || preg_match( '@Redirect\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
68 $item = array(
69 'url' => $this->decode_url( $matches[1] ),
70 'match_type' => 'url',
71 'action_type' => 'url',
72 'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ),
73 'action_code' => 302,
74 );
75 } elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
76 $item = array(
77 'url' => $this->decode_url( $matches[2] ),
78 'match_type' => 'url',
79 'action_type' => 'url',
80 'action_data' => array( 'url' => $this->decode_url( $matches[3] ) ),
81 'action_code' => $this->get_code( $matches[1] ),
82 'regex' => true,
83 );
84 } elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
85 $item = array(
86 'url' => $this->decode_url( $matches[1] ),
87 'match_type' => 'url',
88 'action_type' => 'url',
89 'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ),
90 'action_code' => 302,
91 'regex' => true,
92 );
93 }
94
95 if ( $item ) {
96 $item['action_type'] = 'url';
97 $item['match_type'] = 'url';
98
99 if ( $item['action_code'] === 0 ) {
100 $item['action_type'] = 'pass';
101 }
102
103 return $item;
104 }
105
106 return false;
107 }
108
109 private function decode_url( $url ) {
110 $url = rawurldecode( $url );
111
112 // Replace quoted slashes
113 $url = preg_replace( '@\\\/@', '/', $url );
114
115 // Ensure escaped '.' is still escaped
116 $url = preg_replace( '@\\\\.@', '\\\\.', $url );
117 return $url;
118 }
119
120 private function is_str_regex( $url ) {
121 $regex = '()[]$^?+.';
122 $escape = false;
123
124 for ( $x = 0; $x < strlen( $url ); $x++ ) {
125 $escape = false;
126
127 if ( $url{$x} === '\\' ) {
128 $escape = true;
129 } elseif ( strpos( $regex, $url{$x} ) !== false && ! $escape ) {
130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 private function is_regex( $url ) {
138 if ( $this->is_str_regex( $url ) ) {
139 $tmp = ltrim( $url, '^' );
140 $tmp = rtrim( $tmp, '$' );
141
142 if ( $this->is_str_regex( $tmp ) ) {
143 return true;
144 }
145 }
146
147 return false;
148 }
149
150 private function regex_url( $url ) {
151 $url = $this->decode_url( $url );
152
153 if ( $this->is_str_regex( $url ) ) {
154 $tmp = ltrim( $url, '^' );
155 $tmp = rtrim( $tmp, '$' );
156
157 if ( $this->is_str_regex( $tmp ) ) {
158 return '^/' . ltrim( $tmp, '/' );
159 }
160
161 return '/' . ltrim( $tmp, '/' );
162 }
163
164 return $this->decode_url( $url );
165 }
166
167 private function get_code( $code ) {
168 if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false ) {
169 return 301;
170 } elseif ( strpos( $code, '302' ) !== false ) {
171 return 302;
172 } elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false ) {
173 return 307;
174 } elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false ) {
175 return 404;
176 } elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false ) {
177 return 410;
178 }
179
180 return 302;
181 }
182 }
183