PluginProbe
Redirection / 2.8
Redirection v2.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 / fileio / apache.php

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

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