PluginProbe
Redirection / 5.2.2
Redirection v5.2.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 / fileio / apache.php

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

185 lines 4.9 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 $len = strlen( $url );
124
125 for ( $x = 0; $x < $len; $x++ ) {
126 $escape = false;
127 $char = substr( $url, $x, 1 );
128
129 if ( $char === '\\' ) {
130 $escape = true;
131 } elseif ( strpos( $regex, $char ) !== false && ! $escape ) {
132 return true;
133 }
134 }
135
136 return false;
137 }
138
139 private function is_regex( $url ) {
140 if ( $this->is_str_regex( $url ) ) {
141 $tmp = ltrim( $url, '^' );
142 $tmp = rtrim( $tmp, '$' );
143
144 if ( $this->is_str_regex( $tmp ) ) {
145 return true;
146 }
147 }
148
149 return false;
150 }
151
152 private function regex_url( $url ) {
153 $url = $this->decode_url( $url );
154
155 if ( $this->is_str_regex( $url ) ) {
156 $tmp = ltrim( $url, '^' );
157 $tmp = rtrim( $tmp, '$' );
158
159 if ( $this->is_str_regex( $tmp ) ) {
160 return '^/' . ltrim( $tmp, '/' );
161 }
162
163 return '/' . ltrim( $tmp, '/' );
164 }
165
166 return $this->decode_url( $url );
167 }
168
169 private function get_code( $code ) {
170 if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false ) {
171 return 301;
172 } elseif ( strpos( $code, '302' ) !== false ) {
173 return 302;
174 } elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false ) {
175 return 307;
176 } elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false ) {
177 return 404;
178 } elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false ) {
179 return 410;
180 }
181
182 return 302;
183 }
184 }
185