This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
redir/application/classes/HTTP/Exception/404.php

86 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2013-04-13 06:17:56 +00:00
<?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();
// Update our referrer info
$rfo = $ro->referrer->where('referrer','=',$this->request()->referrer())->find();
if (! $rfo->loaded()) {
$rfo->redir_id = $ro->id;
$rfo->referrer = $this->request()->referrer();
}
$rfo->count++;
$rfo->date_last_refer = $ro->date_last_refer;
$rfo->save();
2013-04-13 06:17:56 +00:00
// Prepare the response object.
$response = Response::factory();
// Process our redirection options
2013-04-14 07:43:06 +00:00
if (preg_match('/^http[s]?:\/\//',$ro->redirect)) {
if ($ro->http_status == 300)
$response->body(View::factory('300')->set('redir',$ro->redirect));
2013-04-27 23:30:31 +00:00
$response->status($ro->http_status ? $ro->http_status : 302);
2013-04-14 07:43:06 +00:00
$response->headers('Location',$ro->redirect);
2013-04-13 06:17:56 +00:00
2013-04-14 07:43:06 +00:00
} elseif (preg_match('/^file:\/\/(.*)$/',$ro->redirect,$matches) AND file_exists($matches[1])) {
2013-04-13 06:17:56 +00:00
$response->status(200);
2013-04-14 07:43:06 +00:00
$response->body(file_get_contents($matches[1]));
2013-04-13 07:53:50 +00:00
HTTP::check_cache($this->request(),$response,sha1($response->body()));
$response->headers('Content-Type',File::mime_by_ext(pathinfo($matches[1],PATHINFO_EXTENSION)));
$response->headers('Content-Length',(string)$response->content_length());
$response->headers('Last-Modified',date('r',$matches[1] ? filemtime($matches[1]) : time()));
2013-04-14 07:43:06 +00:00
} else {
$response->status(404);
$response->body(View::factory('404'));
HTTP::check_cache($this->request(),$response,sha1($response->body()));
2013-04-13 06:17:56 +00:00
}
return $response;
} catch (Exception $e) {
2013-04-13 07:53:50 +00:00
// Incase we are thrown in this exception.
if ($e instanceof HTTP_Exception AND $e->getCode() != 404) {
$x = new $e;
return $x->get_response();
}
2013-04-13 06:17:56 +00:00
return parent::get_response();
}
}
}
?>