| 1 |
<?php |
| 2 |
|
| 3 |
class Error404_Module extends Red_Module |
| 4 |
{ |
| 5 |
var $log_404 = true; |
| 6 |
|
| 7 |
function start () |
| 8 |
{ |
| 9 |
add_action ('template_redirect', array (&$this, 'template_redirect')); |
| 10 |
} |
| 11 |
|
| 12 |
function load ($data) |
| 13 |
{ |
| 14 |
$mine = array ('log_404'); |
| 15 |
foreach ($mine AS $key) |
| 16 |
{ |
| 17 |
if (isset ($data[$key])) |
| 18 |
$this->$key = $data[$key]; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
function save ($data) |
| 23 |
{ |
| 24 |
$save = array |
| 25 |
( |
| 26 |
'log_404' => isset ($data['log_404']) ? true : false |
| 27 |
); |
| 28 |
|
| 29 |
$this->load ($save); |
| 30 |
return $save; |
| 31 |
} |
| 32 |
|
| 33 |
function config () |
| 34 |
{ |
| 35 |
?> |
| 36 |
<tr> |
| 37 |
<th><label for="log_<?php echo $this->log_404 ?>"><?php _e ('Log 404s', 'redirection'); ?>:</label></th> |
| 38 |
<td><input id="log_<?php echo $this->log_404 ?>" type="checkbox" name="log_404" <?php if ($this->log_404) echo ' checked="checked"' ?>/></td> |
| 39 |
</tr> |
| 40 |
<?php |
| 41 |
} |
| 42 |
|
| 43 |
function options () |
| 44 |
{ |
| 45 |
if (!$this->is_valid ()) |
| 46 |
echo __ ('<strong>Disabled: You must enable <a href="options-permalink.php">permalinks</a> before using this</strong>', 'redirection'); |
| 47 |
else |
| 48 |
{ |
| 49 |
$options = array (); |
| 50 |
|
| 51 |
if (!empty ($this->log_404)) |
| 52 |
$options[] = '404s are logged'; |
| 53 |
|
| 54 |
if (count ($options) > 0) |
| 55 |
echo '<small>'.ucfirst (implode (', ', $options)).'</small>'; |
| 56 |
else |
| 57 |
echo __ ('<small>No options have been set</small>', 'redirection'); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
function template_redirect () |
| 62 |
{ |
| 63 |
global $redirection; |
| 64 |
|
| 65 |
if (is_404 () && !$redirection->hasMatched ()) |
| 66 |
{ |
| 67 |
$url = $_SERVER['REQUEST_URI']; |
| 68 |
$redirects = Red_Item::get_for_url ($url, '404'); |
| 69 |
if (!empty ($redirects)) |
| 70 |
{ |
| 71 |
foreach ($redirects AS $key => $item) |
| 72 |
{ |
| 73 |
if ($item->matches ($url)) |
| 74 |
{ |
| 75 |
$redirection->setMatched (true); |
| 76 |
$this->matched = $item; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (empty ($this->matched)) |
| 83 |
{ |
| 84 |
$modules = Red_Module::get_by_type ('404'); |
| 85 |
|
| 86 |
if (count ($modules) > 0) |
| 87 |
{ |
| 88 |
foreach ($modules AS $module) |
| 89 |
{ |
| 90 |
// Log 404 errors |
| 91 |
if ($module->log_404) |
| 92 |
{ |
| 93 |
if (isset ($_SERVER['REMOTE_ADDR'])) |
| 94 |
$myip = $_SERVER['REMOTE_ADDR']; |
| 95 |
else if (isset ($_SERVER['HTTP_X_FORWARDED_FOR'])) |
| 96 |
$myip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 97 |
|
| 98 |
$options = $redirection->get_options (); |
| 99 |
if ($options['log_404s']) |
| 100 |
$log = RE_Log::create ($_SERVER['REQUEST_URI'], '', $_SERVER['HTTP_USER_AGENT'], $myip, isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '', 'NULL', $module->id); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
?> |
| 109 |
|