<?php

	/**
	 * Watchdog class to log errors on database.
	 * 
	 * @link http://www.oldauntie.org
	 * @author Maggie Marple <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
    class Watchdog {
        const WATCHDOG_NOTICE	= 0;
        const WATCHDOG_WARNING	= 1;
        const WATCHDOG_ERROR 	= 2;
        
        const TYPE_ACCESS_DENIED 	= 'access denied';
        const TYPE_DATABASE		 	= 'database';
        const TYPE_CONTENT		 	= 'content';
        const TYPE_PHP		 		= 'php';
        const TYPE_SEARCH		 	= 'search';
        const TYPE_USER			 	= 'user';
        
        /**
         * Creates a new Security object.
         *
         */
        public function __construct() {
			
        }
        
        /**
		 * Log a system message.
		 *
		 * @param $type The
		 *        	category to which this message belongs.
		 *        	access denied
		 *        	content
		 *        	php
		 *        	search
		 *        	user
		 * @param $message The
		 *        	message to store in the log.
		 * @param $uid user The
		 * 			User unique ID or null
		 * @param $severity The
		 *        	severity of the message. One of the following values:
		 *        	- WATCHDOG_NOTICE
		 *        	- WATCHDOG_WARNING
		 *        	- WATCHDOG_ERROR
		 * @param $link A
		 *        	link to associate with the message.
		 */
		public static function write($type, $message, $uid = null, $severity = self::WATCHDOG_NOTICE, $link = null) {
			$con = new Connection ();
			
			
			// Note: log the exact, entire absolute URL.
			$message 		= $con->escape( $message );
			$request_uri 	= 'http://' . _APPLICATION_URL . request_uri ();
			$referer_uri 	= referer_uri ();
			$timestamp 		= time ();
			
			$sql = "INSERT INTO watchdog (uid, 
											type, 
											message, 
											severity, 
											link, 
											location, 
											referer, 
											hostname, 
											timestamp) 
												VALUES ('$uid', 
														'$type', 
														'$message', 
														$severity, 
														'$link', 
														'$request_uri', 
														'$referer_uri', 
														'" . $_SERVER ['REMOTE_ADDR'] . "', 
														$timestamp)";
			
			$aRV = $con->execute ( $sql );
			
			// d($sql);
			
			$con->commit ();
			$con->close ();
		}

        
    }

?>
