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