58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||
|
|
||
|
/**
|
||
|
* This class overrides Kohana's 404 Exception
|
||
|
*
|
||
|
* @package Redir/Modifications
|
||
|
* @category Classes
|
||
|
* @author Deon George
|
||
|
* @copyright (c) 2010-2013 Deon George
|
||
|
* @license http://dev.leenooks.net/license.html
|
||
|
*/
|
||
|
class HTTP_Exception_404 extends Kohana_HTTP_Exception_404 {
|
||
|
/**
|
||
|
* Before returning our file not found, lets see if we are redirecting this URI
|
||
|
*/
|
||
|
public function get_response() {
|
||
|
try {
|
||
|
// Try and find the path
|
||
|
$ro = ORM::factory('Redir',array('servername'=>$_SERVER['SERVER_NAME'],'path'=>$this->request()->uri()));
|
||
|
|
||
|
if (! $ro->loaded()) {
|
||
|
$ro = ORM::factory('Redir',array('servername'=>$_SERVER['SERVER_NAME'],'path'=>NULL));
|
||
|
|
||
|
if (! $ro->loaded())
|
||
|
$ro = ORM::factory('Redir',0);
|
||
|
}
|
||
|
|
||
|
$ro->count++;
|
||
|
$ro->date_last_refer = time();
|
||
|
$ro->save();
|
||
|
|
||
|
// Prepare the response object.
|
||
|
$response = Response::factory();
|
||
|
$redirect = $ro->redirect ? trim($ro->redirect) : Kohana::$config->load('config')->defaultfile;
|
||
|
|
||
|
// Process our redirection options
|
||
|
if (is_null($redirect) OR ! trim($redirect)) {
|
||
|
return parent::get_response();
|
||
|
|
||
|
} elseif (preg_match('/^http[s]?:\/\//',$redirect)) {
|
||
|
$response->status(302);
|
||
|
$response->headers('Location',$redirect);
|
||
|
|
||
|
} elseif (preg_match('/^file:\/\/(.*)$/',$redirect,$matches) AND file_exists($matches[1])) {
|
||
|
$response->status(200);
|
||
|
$response->headers('Content-Type', Kohana_Exception::$error_view_content_type.'; charset='.Kohana::$charset);
|
||
|
$response->body(file_get_contents($redirect));
|
||
|
}
|
||
|
|
||
|
return $response;
|
||
|
|
||
|
} catch (Exception $e) {
|
||
|
return parent::get_response();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
?>
|