<?php
/*
 * Project: NodeMap2.0
 * File: LogHandler.class.php
 * Purpose: Handling our log file containing log entries generated by the application
 */

/*
 * Log levels:
 *   SYSLOG_EMERG		System is unusable
 *   SYSLOG_ALERT		Action must be taken immediately
 *   SYSLOG_CRIT		Critical conditions
 *   SYSLOG_ERR		Error conditions
 *   SYSLOG_WARNING	Warning conditions
 *   SYSLOG_NOTICE		Normal, but significant, condition
 *   SYSLOG_INFO		Informational message
 *   SYSLOG_DEBUG		Debug-level message
 */

define('SYSLOG_NONE', 0);
define('SYSLOG_EMERG', 1);
define('SYSLOG_ALERT', 2);
define('SYSLOG_CRIT', 3);
define('SYSLOG_ERR', 4);
define('SYSLOG_WARNING', 5);
define('SYSLOG_NOTICE', 6);
define('SYSLOG_INFO', 7);
define('SYSLOG_DEBUG', 8);

/*
 * XXX: Actions on log levels, needs to go to the config
 */
define('LOG_LEVEL_ECHO', SYSLOG_DEBUG);
define('LOG_LEVEL_WRITE', SYSLOG_DEBUG);
define('LOG_LEVEL_MAIL', SYSLOG_EMERG);

class LogHandler {
	private $logString;		// For caching purposes

	/*
	 * Function: __construct (constructor)
	 * Parameters: -
	 * Function: Creating a LogHandler object
	 */
	public function __construct() {
		$logString = '';
	}

	/*
	 * Function: logEntry
	 * Parameters: int $logno, string $logstr, string $logfile, int $logline
	 * Function: Handling with log entries, decides what to do with it
	 */
	public function logEntry($logno, $logstr, $logfile, $logline) {
		$errorString = 'SysLog ';

		switch ($logno) {
			case SYSLOG_EMERG:
				$errorString = 'Emergency';
				break;
			case SYSLOG_ALERT:
				$errorString = 'Alert';
				break;
			case SYSLOG_CRIT:
				$errorString = 'Critical';
				break;
			case SYSLOG_ERR:
				$errorString = 'Error';
				break;
			case SYSLOG_WARNING:
				$errorString = 'Warning';
				break;
			case SYSLOG_NOTICE:
				$errorString = 'Notice';
				break;
			case SYSLOG_INFO:
				$errorString = 'Information';
				break;
			case SYSLOG_DEBUG:
				$errorString = 'Debug';
				break;
			default:
				$errorString = 'SYSLOG_DEFAULT';
				break;
		}

		$errorString .= ' (' . $logno . ' | ' . date('Y/d/m H:i:s') . '): ' . $logstr . "\r\n\t" . 'In file "' . $logfile . '" on line "' . $logline . '"' . "\r\n";

		if ($logno <= LOG_LEVEL_ECHO) {
			$this->logString .= $errorString;
		}
		if ((!defined('IGNORE_LOG_LEVEL_WRITE')) && ($logno <= LOG_LEVEL_WRITE)) {
			global $config;
			$filename = $config['log_file'];
			//XXX: Trying to write to a file that a the file cannot be opened 
			//     what could possible go wrong
			//Check if the file can be opened
			if (!$handle = fopen($filename, 'a+')) {
				define('IGNORE_LOG_LEVEL_WRITE', true);
				trigger_log(SYSLOG_EMERG, 'Cannot open file "' . $filename . '"', __FILE__, __LINE__);
				exit;
			}
				
			// Write $errorString to file.
			if (fwrite($handle, $errorString) === FALSE) {
				define('LOG_LEVEL_WRITE', LOG_LEVEL_NONE);
				trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
				exit;
			}
				
			fclose($handle);
		}

		/* XXX: Sends email out for every line, instead of all the lines of the current output */
		if ($logno <= LOG_LEVEL_MAIL) {
			global $config;
			@mail($config['mail'], 'Nodemap error', $errorString);
		}
	}

	function __destruct() {
		echo '<!-- ' . $this->logString . ' -->';
	}
}

$LOG_HANDLER = new LogHandler();

/*
 * Function: trigger_log
 * Parameters: int $logno, string $logstr, string $logfile, int $logline
 * Function: Handling with log entries, forwarding them to our $LOG_HANDLER object.
 */
function trigger_log($logno, $logstr, $logfile, $logline) {
	global $LOG_HANDLER;

	$LOG_HANDLER->logEntry($logno, $logstr, $logfile, $logline);
}
?>
