aliroErrorRecorder.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * aliroErrorRecorder provides a simple way to log errors to the database.  It
00038  * will accept a short message, a long message, and optionally an exception as
00039  * parameters.  It derives for itself the POST and GET data, and also a trace
00040  * of execution.  The whole is stored as a database record, in a table which
00041  * is pruned to keep it to a maximum of 7 days so it will not grow too large.
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         // code to prune error log - limit to max items, max days
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 }

Generated on Thu Apr 17 13:03:26 2008 for ALIRO by  doxygen 1.5.5