<?php
use work\w\NotesWork;
use \wb\Wb,
	\wb\Runner,
	\wb\HandlerNotFoundException;
use \db\DB;

/*
 * Load library
 */
if (!class_exists('\wb\Wb'))
{
	if ($_SERVER['HTTP_HOST'] == 'localhost')
		require_once('../wb/local.php');
	else
		require_once('../wb/Wb.php');
}
Wb::load('dev', __DIR__.'/core', (defined('LOCAL') ? -1 : null));

/*
 * Constants
 */
define('SITE_ADMIN', 'mail@michaelclerx.com');
define('SITE_TITLE', 'Work');

/*
 * Error reporting
 */
function reportException(\Exception $e)
{
	$reported = false;
	try
	{
		if (class_exists('EmailErrorReporter'))
		{
			@$eml = new EmailErrorReporter('work.michaelclerx.com', 'michael.clerx@gmail.com');
			@$eml->addEmail('michael.clerx@gmail.com');
			@$reported |= $eml->sendException($e);
		}
	}
	catch (\Exception $f)
	{
		// ignore :(
	}
	return $reported;
}

/*
 * Database
 */
try
{
	// Connect to DB
	if (defined('LOCAL'))
	{
		DB::setCacheTimeOut(-1);
		DB::setCacheFile(Wb::getCacheFile('db'));
		// host, user, pass, db_name
		DB::connect('localhost', 'michael', 'michael', 'michael_work');
	}
	else
	{
		DB::setCacheFile(Wb::getCacheFile('db'));
		// host, user, pass, db_name
		$host = 'localhost';
		$user = 'michael';
		$pass = 'fluffyFactReceptacle';
		$base = 'michael_work';
		@DB::connect($host, $user, $pass, $base);
	}
}
catch (\Exception $e)
{
	if (defined('LOCAL'))
		throw $e;
	else
	{
		@$reported = reportException($e);
		if ($reported)
		{
			?><h1>Error</h1>
			<p>The page you requested generated an error. The site administrator has been notified and will fix this as soon as possible</p>
			<?
		}
		else
		{
			?><h1>Error</h1>
			<p>The page you requested generated an error. In addition, the automatic error reporting scheme failed. Please contact the system administrator at <?=SITE_ADMIN?></p>
			<?
		}
		die();
	}
}

/*
 * Date & time
 */
date_default_timezone_set('Europe/Amsterdam');

/*
 * Debug mode
 */
define('DEBUG', defined('LOCAL'));

/*
 * Customize error handlers
 */
class WorkRunner extends Runner
{
	// Email normal exception to dev team
	protected function handleException(\Exception $e)
	{
		@$reported = reportException($e);
		if ($reported)
		{
			?><h1>Error</h1>
			<p>The page you requested generated an error. The site administrator has been notified and will fix this as soon as possible</p>
			<?
			echo $e->getMessage();
			echo "<pre>" . htmlspecialchars($e->getTraceAsString()) . "</pre>";
		}
		else
		{
			?><h1>Error</h1>
			<p>The page you requested generated an error. In addition, the automatic error reporting scheme failed. Please contact the system administrator at <?=SITE_ADMIN?></p>
			<?
		}
		die();
	}

	// Do the same for syntax errors
	protected function handleSyntaxException(\Exception $e)
	{
		$this->handleException($e);
	}

	// Page not found? Show 404 page
	protected function handleNotFoundException(HandlerNotFoundException $e)
	{
		require_once('core/404.php');
		die();
	}
}

/*
 * Run
 */
ob_start();
$runner = new WorkRunner();
$runner->run(new \work\w\Home());
