PluginProbe
Redirection / 3.7.3
Redirection v3.7.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 3.7.3, at fileio/apache.php

176 lines 4.7 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 $url = str_replace( '\\.', '.', $url );
112 return $url;
113 }
114
115 private function is_str_regex( $url ) {
116 $regex = '()[]$^?+.';
117 $escape = false;
118
119 for ( $x = 0; $x < strlen( $url ); $x++ ) {
120 $escape = false;
121
122 if ( $url{$x} === '\\' ) {
123 $escape = true;
124 } elseif ( strpos( $regex, $url{$x} ) !== false && ! $escape ) {
125 return true;
126 }
127 }
128
129 return false;
130 }
131
132 private function is_regex( $url ) {
133 if ( $this->is_str_regex( $url ) ) {
134 $tmp = ltrim( $url, '^' );
135 $tmp = rtrim( $tmp, '$' );
136
137 if ( $this->is_str_regex( $tmp ) ) {
138 return true;
139 }
140 }
141
142 return false;
143 }
144
145 private function regex_url( $url ) {
146 if ( $this->is_str_regex( $url ) ) {
147 $tmp = ltrim( $url, '^' );
148 $tmp = rtrim( $tmp, '$' );
149
150 if ( $this->is_str_regex( $tmp ) === false ) {
151 return '/' . $this->decode_url( $tmp );
152 }
153
154 return '/' . $this->decode_url( $url );
155 }
156
157 return $this->decode_url( $url );
158 }
159
160 private function get_code( $code ) {
161 if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false ) {
162 return 301;
163 } elseif ( strpos( $code, '302' ) !== false ) {
164 return 302;
165 } elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false ) {
166 return 307;
167 } elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false ) {
168 return 404;
169 } elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false ) {
170 return 410;
171 }
172
173 return 302;
174 }
175 }
176