PluginProbe
Redirection / 2.6
Redirection v2.6
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.6, at fileio/apache.php

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