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

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