<?php
/**
* ÐлаÑÑ BaseException
*
* @package energine
* @subpackage core
* @author 1m.dm
* @copyright ColoCall 2006
* @version $Id: BaseException.class.php,v 1.11 2008/04/01 14:06:09 pavka Exp $
*/
//require_once('core/framework/DBWorker.class.php');
//require_once('core/framework/Transformer.class.php');
//require_once('core/framework/Response.class.php');
//require_once('core/framework/Mail.class.php');
//require_once('core/framework/Language.class.php');
/**
* Ðазовое иÑклÑÑение.
*
* @package energine
* @subpackage core
*/
class BaseException extends Exception {
/**
* XSLT-докÑÐ¼ÐµÐ½Ñ Ð´Ð»Ñ ÑÑÑаниÑÑ Ð¾Ñибки
*/
const ERROR_TRANSFORMER = 'error_page.xslt';
/**
* ÐÑиÑиÑеÑÐºÐ°Ñ Ð¾Ñибка
*/
const ERR_CRITICAL = 0;
/**
* ÐÑибка 404 - ÑÑÑаниÑÑ Ð½Ðµ ÑÑÑеÑÑвÑеÑ
*/
const ERR_404 = 1;
/**
* ÐÑибка 403 - Ð½ÐµÑ Ð¿Ñав на пÑоÑмоÑÑ ÑÑÑаниÑÑ
*/
const ERR_403 = 2;
/**
* ÐÑибка пÑи ÑабоÑе Ñ ÐÐ
*/
const ERR_DB = 3;
/**
* ÐÑибка ÑазÑабоÑÑика, где-Ñо ÑÑо-Ñо невеÑно напиÑано :)
*/
const ERR_DEVELOPER = 4;
/**
* ÐÑедÑпÑеждение
*/
const ERR_WARNING = 10;
/**
* ÐамеÑание
*/
const ERR_NOTICE = 20;
/**
* ÐÑибка, ÑвÑÐ·Ð°Ð½Ð½Ð°Ñ Ñ Ð¼ÑлÑÑиÑзÑÑноÑÑÑÑ. ÐÐ¾Ð·Ð½Ð¸ÐºÐ°ÐµÑ Ð¿Ñи обÑабоÑке дÑÑгой
* оÑибки и оÑÑÑÑÑÑÐ²Ð¸Ñ Ð´Ð»Ñ Ð½ÐµÑ Ð¿ÐµÑеводов. Ðез ERR_LANG Ð²Ð¾Ð·Ð¼Ð¾Ð¶ÐµÑ ÑÑ
од
* в ÑекÑÑÑÐ¸Ñ Ð¸ полнÑй пиздеÑ.
*
* ÐÐ°Ð½Ð½Ð°Ñ Ð¾Ñибка каÑаеÑÑÑ Ð¸ÑклÑÑиÑелÑно ÑазÑабоÑÑиков ÑиÑÑемÑ.
*
* @todo ÑделаÑÑ Ñ
оÑÑ ÑÑо-нибÑдÑ! :)
*/
const ERR_LANG = 5;
/**
* Ðогда Ñежим оÑладки вклÑÑен:
*
* 1. Ðожно вÑвеÑÑи XML-докÑÐ¼ÐµÐ½Ñ ÑÑÑаниÑÑ Ð´Ð¾Ð±Ð°Ð²Ð¸Ð² к query-ÑаÑÑи URI
* паÑамеÑÑ 'debug'.
* 2. ÐÑи обÑабоÑке ÑиÑÑемнÑÑ
оÑибок вÑводиÑÑÑ Ð¼Ð°ÐºÑималÑно подÑобнаÑ
* инÑоÑмаÑÐ¸Ñ Ð¾ возникÑей оÑибке.
*
* @access private
* @var boolean Ñлаг Ñежима оÑладки
*/
private $isDebugEnabled = true;
/**
* @access protected
* @var Response ÑкземплÑÑ Ð¾Ð±ÑекÑа Response
*/
protected $response;
/**
* @access protected
* @var boolean Ñлаг Ñежима вÑвода XML-докÑменÑа ÑÑÑаниÑÑ
* @todo плоÑ
ое имÑ
*/
protected $isXML;
/**
* @access protected
* @var DOMDocument
*/
protected $doc;
/**
* @access protected
* @var mixed дополниÑелÑÐ½Ð°Ñ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾Ð± оÑибке
*/
protected $customMessages = array();
/**
* ÐонÑÑÑÑкÑÐ¾Ñ ÐºÐ»Ð°ÑÑа.
*
* @access public
* @param string $message
* @param int $code
* @param mixed $customMessages
* @return void
* @todo опÑеделиÑÑÑÑ Ñ $customMessages: ÑÑо mixed или array?
*/
public function __construct($message, $code = self::ERR_CRITICAL, $customMessages = null) {
$this->isDebugEnabled = (bool)Object::_getConfigValue('site.debug');
$this->response = Response::getInstance();
$this->isXML = isset($_GET['debug']);
$this->doc = new DOMDocument('1.0', 'UTF-8');
if (isset($customMessages)) {
if (!is_array($customMessages)) {
$this->customMessages = array($customMessages);
}
else {
$this->customMessages = $customMessages;
}
}
if ($code == self::ERR_LANG) {
$message = DBWorker::_translate($message, Language::getInstance()->getDefault());
}
elseif ($code != self::ERR_DB ) {
$message = DBWorker::_translate($message, Language::getInstance()->getCurrent());
}
parent::__construct($message, $code);
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð´Ð¾Ð¿Ð¾Ð»Ð½Ð¸ÑелÑнÑÑ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾Ð± оÑибке.
*
* @access public
* @return string
* @todo пеÑеименоваÑÑ Ð² getCustomMessages
*/
public function getCustomMessage() {
return $this->customMessages;
}
/**
* ÐÑпÑавлÑÐµÑ Ñведомление о оÑибке
*
* @return void
* @access private
*/
private function sendNotification() {
$mail = new Mail();
$projectName = ($fake = Object::_getConfigValue('project.name'))?$fake:$_SERVER['HTTP_HOST'];
$from = Object::_getConfigValue('mail.from');
$customMessage = implode("\r\n", $this->getCustomMessage());
$body = sprintf("Project:%s\r\nCode:%s\r\nMessage:%s\r\nCustomMessage:%s\r\n,File:%s\r\nLine:%s\r\nTrace:%s", $projectName, $this->getCode(), $this->getMessage(), $customMessage,$this->getFile(), $this->getLine(), $this->getTraceAsString());
$mail->setText($body)
->setFrom($from, $from)
->addTo(Object::_getConfigValue('mail.feedback'))
->setSubject($projectName.' Error Notification:'.$this->getMessage())
->send();
}
/**
* ФоÑмиÑÑÐµÑ XML-пÑедÑÑавление оÑибки.
*
* @access protected
* @return void
*/
protected function build() {
$request = Request::getInstance();
$dom_errors = $this->doc->createElement('errors');
$dom_errors->setAttribute('uri', $request->getPath(Request::PATH_WHOLE, true));
$dom_errors->setAttribute('base', $request->getBasePath());
$dom_errors->setAttribute('debug', $this->isDebugEnabled);
$dom_error = $this->doc->createElement('error');
$dom_error->setAttribute('code', $this->getCode());
$dom_error->setAttribute('file', $this->getFile());
$dom_error->setAttribute('line', $this->getLine());
$dom_error->appendChild(
$this->doc->createElement('message', $this->getMessage())
);
$customMessages = $this->getCustomMessage();
if ($customMessages) {
$dom_customMessages = $this->doc->createElement('customMessages');
if (is_array($customMessages)) {
foreach ($customMessages as $customMessage) {
$dom_customMessages->appendChild(
$this->doc->createElement('customMessage', $customMessage)
);
}
}
else {
$dom_customMessages->nodeValue = $customMessages;
}
$dom_error->appendChild($dom_customMessages);
}
$dom_errors->appendChild($dom_error);
$this->doc->appendChild($dom_errors);
}
/**
* ÐбÑабаÑÑÐ²Ð°ÐµÑ Ð¾ÑÐ¸Ð±ÐºÑ Ð¿ÑÑÑм ÐµÑ Ð²Ñвода :)
*
* @access public
* @return void
*/
public function handle() {
if (!in_array($this->getCode(), array(self::ERR_403, self::ERR_404, self::ERR_NOTICE, self::ERR_WARNING)) && !$this->isDebugEnabled) {
$this->sendNotification();
}
$this->build();
if ($this->isDebugEnabled && $this->isXML) {
$this->response->setHeader('Content-Type', 'text/xml; charset=UTF-8');
$result = $this->doc->saveXML();
}
else {
$this->response->setHeader('Content-Type', 'text/html; charset=UTF-8');
$transformer = new Transformer;
$result = $transformer->transform($this->doc, self::ERROR_TRANSFORMER);
}
$this->response->write($result);
$this->response->commit();
}
}