00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 class aliroErrorRecorder extends aliroDatabaseRow {
00046 protected static $instance = null;
00047 protected $DBclass = 'aliroCoreDatabase';
00048 protected $tableName = '#__error_log';
00049 protected $rowKey = 'id';
00050
00051 public static function getInstance ($request=null) {
00052 if (null == self::$instance) self::$instance = new aliroErrorRecorder();
00053 return self::$instance;
00054 }
00055
00056 public function PHPerror ($errno, $errstr, $errfile, $errline, $errcontext) {
00057 if (!($errno & error_reporting())) return;
00058 $rawmessage = function_exists('T_') ? T_('PHP Error %s: %s in %s at line %s') : 'PHP Error %s: %s in %s at line %s';
00059 $message = sprintf($rawmessage, $errno, $errstr, $errfile, $errline);
00060 $lmessage = $message;
00061 if (is_array($errcontext)) {
00062 foreach ($errcontext as $key=>$value) if (!is_object($value) AND !(is_array($value))) $lmessage .= "; $key=$value";
00063 }
00064 $errorkey = "PHP/$errno/$errfile/$errline/$errstr";
00065 $this->recordError($message, $errorkey, $lmessage);
00066 aliroRequest::getInstance()->setErrorMessage(T_('A PHP error has been recorded in the log'), _ALIRO_ERROR_WARN);
00067 if ($errno & (E_USER_ERROR|E_COMPILE_ERROR|E_CORE_ERROR|E_ERROR)) die (T_('Serious PHP error - processing halted - see error log for details'));
00068 }
00069
00070 public function recordError ($smessage, $errorkey, $lmessage='', $exception=null) {
00071 $this->id = 0;
00072 $this->timestamp = date ('Y-m-d H:i:s');
00073 $this->smessage = $smessage;
00074 $this->lmessage = $lmessage ? $lmessage : $smessage;
00075 $this->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
00076 $database = aliroCoreDatabase::getInstance();
00077 $this->errorkey = $database->getEscaped($errorkey);
00078 $this->get = $_SERVER['REQUEST_URI'];
00079 $this->post = base64_encode(serialize($_POST));
00080 $this->trace = aliroRequest::trace();
00081 if ($exception instanceof databaseException) {
00082 $this->dbname = $exception->dbname;
00083 $this->sql = $exception->sql;
00084 $this->dberror = $exception->getCode();
00085 $this->dbmessage = $exception->getMessage();
00086 $this->dbtrace = $exception->dbtrace;
00087 }
00088 else $this->dbname = $this->sql = $this->dberror = $this->dbmessage = null;
00089 $database->setQuery("SELECT id FROM #__error_log WHERE errorkey = '$this->errorkey'");
00090 $id = $database->loadResult();
00091 if (!$id) $this->store();
00092 else $database->doSQL("UPDATE #__error_log SET timestamp = NOW() WHERE id = $id");
00093
00094 $database = call_user_func(array($this->DBclass, 'getInstance'));
00095 $database->doSQL("DELETE LOW_PRIORITY FROM $this->tableName WHERE timestamp < SUBDATE(NOW(), INTERVAL 7 DAY)");
00096 }
00097 }