<?php

define('MTS_LOGIN', 1);		// User login
define('MTS_USER', 2);		// Changes to user information

define('MTS_PAGE', 3);		// Modifying static pages
define('MTS_RANT', 4);		// Modifying rants
define('MTS_SCRATCH', 5);	// Additions to the scratchpad
define('MTS_STRIP', 6);		// Modifying strips
define('MTS_TWITTER', 9);	// Modifications to Twitter presets
define('MTS_TYPE', 7);		// Changes in the type manager
define('MTS_TYPE_META', 8);	// Changes in the metatype manager
define('MTS_TUMBLR', 10);



define('MTA_ADD', 'create');	// Creation action
define('MTA_INSERT', 'create');	// Creation action
define('MTA_DELETE', 'delete');	// Deletion action
define('MTA_REMOVE', 'delete');	// Deletion action
define('MTA_MODIFY', 'update');	// Modification action
define('MTA_UPDATE', 'update');	// Modification action
define('MTA_CHANGE', 'update');	// Modification action

function adminlog($msg, $section, $action, $level=E_USER_NOTICE, $email=false)
{
	global $mtdb, $currentuser;

	$sql = sprintf('INSERT INTO admin_log (contributor, section, action, level, message) VALUES (%s, %d, "%s", %d, "%s")',
	               (is_numeric($currentuser->id) ? $currentuser->id : "NULL"), $section, mysqli_real_escape_string($mtdb->link, $action), $level, mysqli_real_escape_string($mtdb->link, $msg));
	$mtdb->query( $sql ) or die($sql."<br>".mysqli_error()."<br>\n".var_export(debug_backtrace()));

	// Log all important sorts of messages in the Apache log
	if( $level & (E_USER_WARNING | E_USER_ERROR) ) {
		error_log($msg, 0);
	}

	// Email critical messages and those for which email is requested
	if($email || E_USER_ERROR == $level || E_ERROR == $level) {
		// Pretty printing
		switch($level) {
			case E_USER_NOTICE:
			case E_NOTICE:
				$importance = 'Notice';
				break;
			case E_USER_WARNING:
			case E_WARNING:
				$importance = 'Warning';
				break;
			case E_USER_ERROR:
			case E_ERROR:
				$importance = 'Error';
				break;
			default:
				$importance = "Other - $level";
				break;
		}

		switch($section) {
			case MTS_LOGIN:		$area = 'User login'; break;
			case MTS_USER:		$area = 'Modify user'; break;
			case MTS_PAGE:		$area = 'Modify page'; break;
			case MTS_RANT:		$area = 'Modify rant'; break;
			case MTS_SCRATCH:	$area = 'Modify scratchpd'; break;
			case MTS_STRIP:		$area = 'Modify strips'; break;
			case MTS_TYPE:		$area = 'Modify strip types'; break;
			case MTS_TYPE_META:	$area = 'Modify strip metatypes'; break;
			case MTS_TWITTER:	$area = 'Modify Twitter'; break;
			default:
				$area = 'Undefined Area'; break;
		}

		error_log("Megatokyo Administrative Notice\r\nPriority level: $importance\r\nReported by: ".$currentuser->name."\r\nSection: $area\r\n$msg", 1, SITE_CONTACT);
	}
}

?>
