Location: PHPKode > projects > Community Learning Network > cln/lib/CLN/Cln_Error_Handler.php
<?php
/*
 * Error Handler
 *
 * Copyright (c) 2003-4 St. Christopher House
 *
 * Developed by The Working Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @version $Id: Cln_Error_Handler.php,v 1.27 2004/11/19 04:41:21 cbooth7575 Exp $
 *
 */

/*
 *
 * Function:  clnErrorHandler()
 *
 * Main Error Handler.  All errors go through this first
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnErrorHandler($errno, $errstr = '', $errfile = '', $errline = '', $errdump = '')
{
	/* If it's a PEAR error: */
	if (PEAR::isError($errno)) {
		$errObj = $errno; // The error number is actually a PEAR_ERROR object
		$errno = $errObj->getCode();
		if ($errno == null) {
			$errno = E_WARNING;
		}
		$errstr = $errObj->getMessage();
		$errfile = $errObj->backtrace[1]['file'];
		$errline = $errObj->backtrace[1]['line'];
		$errdump = $errObj->backtrace;
	}

	/* Then do stuff, depending on the level of the error */

	/* Put user errors into a variable for display by the template */
	if ($errno == E_USER_WARNING || $errno == E_USER_ERROR) {
		clnCaptureUserError($errstr);
	}

	/* Put user messages into a variable for display by the template */
	if ($errno == E_USER_NOTICE) {
		clnCaptureUserMessage($errstr);
	}

	/* Display some errors */
	if (($errno | CLN_ERROR_DISPLAY_HANDLER) == CLN_ERROR_DISPLAY_HANDLER) {
		clnDisplayError($errno, $errstr, $errfile, $errline, $errdump);
	}

	/* Log some errors */
	if (($errno | CLN_ERROR_LOG_HANDLER) == CLN_ERROR_LOG_HANDLER) {
		clnLogError($errno, $errstr, $errfile, $errline, $errdump);
	}

	/* Email some errors */
	if (($errno | CLN_ERROR_EMAIL_HANDLER) == CLN_ERROR_EMAIL_HANDLER) {
	    clnEmailError($errno, $errstr, $errfile, $errline, $errdump);
	}

}



/*
 * Handling functions
 *
 * These are the functions that actually do the work of displaying, logging, or emailing errors
 *
 */


/*
 *
 * Function:  clnDisplayError()
 *
 * Displays errors to page
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnDisplayError($errno, $errstr = '', $errfile = '', $errline = '', $errdump = '')
{
	global $display_errors;
	//$display_errors .= "<p><b>Error: $errno</b></p><p> $errstr</p><p> $errfile Line: $errline</p>";
	$fileArray = explode('/', $errfile);
	$file = array_pop($fileArray);
	$display_errors .= "<p><b>$errstr</b> <span>($file - $errline)</span></p>";
}


/*
 *
 * Function:  clnLogError()
 *
 * stores errors in memory for one time write out of log
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnLogError($errno, $errstr = '', $errfile = '', $errline = '', $errdump = '')
{
	require_once('Log.php');
	$logString = "$errstr";
	if (CLN_LOG_FILE_INFO) {
		$logString .= " ($errfile - $errline)";
	}
	$GLOBALS['logs'][] = array($logString, LOG_ERR);
}


/*
 *
 * Function:  clnWriteLog()
 *
 * Writes error log out to all file (all errors at once)
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnWriteLog()
{
	if (isset($GLOBALS['logs'])) {
		require_once('Log.php');
		$log  = &Log::singleton('file', CLN_LOG_FILE);

		// Make sure the log file is writable, or
		// if the log file doesn't exist yet, make sure
		// the directory it's in is writable
		if ((file_exists(CLN_LOG_FILE) && is_writable(CLN_LOG_FILE)) ||
			(!file_exists(CLN_LOG_FILE) && is_writable(dirname(CLN_LOG_FILE)))) {
			foreach($GLOBALS['logs'] as $logMessage) {
				$log->log($logMessage[0], $logMessage[1]);
			}
			// write new line to separate page cycles
			$log->log('-----------------------------','3');

			unset($GLOBALS['logs']);
		}
		else {
			clnDisplayError('1', 'Error Log File Not Writable, either turn off the error logging in your config local, or make the file writable', 'N/A', 'N/A', 'N/A');
		}
	}
}


/*
 *
 * Function:  clnEmailError()
 *
 * Sends errors via email
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnEmailError($errno, $errstr = '', $errfile = '', $errline = '', $errdump = '')
{
	if(!(defined('CLN_ERROR_EMAIL_ADDRESS') && defined('CLN_SERVER_EMAIL_ADDRESS'))) return FALSE;

	$errorEmailString = "Error: $errno \n$errstr \n$errfile Line: $errline\n\n";
	$errorEmailString .= 'Date:	  ' . date('d-m-Y H:i:s', mktime()) . "\n\n";
	$errorEmailString .= "Data Dump:\n\n";
	ob_start();
	print_r($errdump);
	$errorEmailString .= ob_get_contents();
	ob_end_clean();

	// Send a message to the admin
	mail(
		CLN_ERROR_EMAIL_ADDRESS,
		'Error on the CLN Site',
		"$errorEmailString",
		'From: CLN <'.CLN_SERVER_EMAIL_ADDRESS.">\nX-Mailer: PHP/" . phpversion());

}


/*
 *
 * Function:  clnCaptureUserError()
 *
 * Stores the user error message in session for display on next page
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnCaptureUserError ($errstr) {
	if (!isset($_SESSION['userErrorMessages'])) {
		$_SESSION['userErrorMessages'] = Array();
	}
	$_SESSION['userErrorMessages'][] = $errstr;
}


/*
 *
 * Function:  clnCaptureUserMessage()
 *
 * Stores the user message in session for display on next page
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnCaptureUserMessage ($errstr) {
	if (!isset($_SESSION['userMessages'])) {
		$_SESSION['userMessages'] = Array();
	}
	$_SESSION['userMessages'][] = $errstr;
}


/*
 *
 * Function:  clnGetUserError()
 *
 * Formats the user error messages
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnGetUserError () {
	if (isset($_SESSION['userErrorMessages']) && count($_SESSION['userErrorMessages']) > 0) {
		includeLangFile('lib/CLN/lang/General');
		$returnVal = '<div class="errorMessages">' . CLN_ERROR_MSG_HEADER . ":\n";
		$returnVal .= '<ul>';
		foreach ($_SESSION['userErrorMessages'] as $error) {
			$returnVal .= "<li class=\"errorMessage\">$error</li>";
		}
		$returnVal .= "</ul>\n";
		$returnVal .= '</div>';
		$_SESSION['userErrorMessages'] = Array();
		return $returnVal;
	}
	else {
		return '';
	}
}


/*
 *
 * Function:  clnGetUserMessage()
 *
 * Formats the user notice messages
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnGetUserMessage () {
	if (isset($_SESSION['userMessages']) && count($_SESSION['userMessages']) > 0) {
		$returnVal = '';
		$returnVal .= '<div class="userMessages">';
		foreach ($_SESSION['userMessages'] as $message) {
			$returnVal .= "<p>$message</p>";
		}
		$returnVal .= '</div>';
		$_SESSION['userMessages'] = Array();
		return $returnVal;
	}
	else {
		return '';
	}
}


/*
 *
 * Function:  clnErrorShutdown()
 *
 * This function is called after shutdown, and displays the errors to the bottom of the screen
 *
 *
 * @access public
 * @return TBD
 *
 */
function clnErrorShutdown () {
	global $display_errors;
	if (isset($display_errors)) {
		echo '<div class="phpError">';
		echo $display_errors;
		echo '</div>';
	}
}

?>
Return current item: Community Learning Network