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