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

133 lines 4.1 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 var $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 $htaccess->generate( $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 function load( $group, $data, $filename = '' ) {
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 if ( count( $lines ) > 0 ) {
38 foreach ( $lines AS $line ) {
39 if ( preg_match ('@rewriterule\s+(.*?)\s+(.*?)\s+(\[.*\])*@i', $line, $matches) > 0 )
40 $items[] = array('source' => $this->regex_url ($matches[1]), 'target' => $this->decode_url ($matches[2]), 'code' => $this->get_code ($matches[3]), 'regex' => $this->is_regex ($matches[1]) );
41 elseif ( preg_match( '@Redirect\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches) > 0 )
42 $items[] = array( 'source' => $this->decode_url ($matches[2]), 'target' => $this->decode_url ($matches[3]), 'code' => $this->get_code ($matches[1]) );
43 elseif ( preg_match( '@Redirect\s+(.*?)\s+(.*?)@i', $line, $matches) > 0 )
44 $items[] = array( 'source' => $this->decode_url ($matches[1]), 'target' => $this->decode_url ($matches[2]), 'code' => 302);
45 elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches) > 0 )
46 $items[] = array( 'source' => $this->decode_url ($matches[2]), 'target' => $this->decode_url ($matches[3]), 'code' => $this->get_code ($matches[1]), 'regex' => true );
47 elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)@i', $line, $matches) > 0 )
48 $items[] = array( 'source' => $this->decode_url ($matches[1]), 'target' => $this->decode_url ($matches[2]), 'code' => 302, 'regex' => true );
49 }
50
51 // Add items to group
52 if ( count( $items ) > 0 ) {
53 foreach ( $items AS $item ) {
54 $item['group'] = $group;
55 $item['red_action'] = 'url';
56 $item['match'] = 'url';
57
58 if ( $item['code'] == 0 )
59 $item['red_action'] = 'pass';
60
61 Red_Item::create( $item );
62 }
63
64 return count( $items );
65 }
66 }
67
68 return 0;
69 }
70
71 function decode_url( $url ) {
72 $url = rawurldecode( $url );
73 $url = str_replace( '\\.', '.', $url );
74 return $url;
75 }
76
77 function is_str_regex( $url ) {
78 $regex = '()[]$^?+.';
79 $escape = false;
80
81 for ( $x = 0; $x < strlen( $url ); $x++ ) {
82 if ( $url{$x} == '\\' )
83 $escape = true;
84 elseif ( strpos( $regex, $url{$x} ) !== false && !$escape )
85 return true;
86 else
87 $escape = false;
88 }
89
90 return false;
91 }
92
93 function is_regex( $url ) {
94 if ( $this->is_str_regex( $url ) ) {
95 $tmp = ltrim( $url, '^' );
96 $tmp = rtrim( $tmp, '$' );
97
98 if ( $this->is_str_regex( $tmp ) )
99 return true;
100 }
101
102 return false;
103 }
104
105 function regex_url ($url) {
106 if ( $this->is_str_regex( $url ) ) {
107 $tmp = ltrim( $url, '^' );
108 $tmp = rtrim( $tmp, '$' );
109
110 if ( $this->is_str_regex( $tmp ) == false )
111 return '/'.$this->decode_url( $tmp );
112
113 return '/'.$this->decode_url( $url );
114 }
115
116 return $this->decode_url( $url );
117 }
118
119 function get_code ($code) {
120 if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false )
121 return 301;
122 elseif ( strpos( $code, '302' ) !== false )
123 return 302;
124 elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false )
125 return 307;
126 elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false )
127 return 404;
128 elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false )
129 return 410;
130 return 0;
131 }
132 }
133