<commit_before><?php

class CM_Bootloader {

	public function defaults() {
		date_default_timezone_set(CM_Config::get()->timeZone);
		mb_internal_encoding('UTF-8');
		umask(0);
	}

	public function autoloader() {
		spl_autoload_register(function($className) {
			$path = DIR_ROOT . 'library/' . str_replace('_', '/', $className) . '.php';
			if (is_file($path)) {
				require_once $path;
				return;
			}
		});
	}

	public function exceptionHandler() {
		set_exception_handler(function(Exception $exception) {
			$showError = IS_DEBUG || IS_CRON || IS_TEST;

			if (!IS_CRON && !IS_TEST) {
				header('HTTP/1.1 500 Internal Server Error');
			}

			$class = get_class($exception);
			$code = $exception->getCode();

			$logMsg = $class . ' (' . $code . '): ' . $exception->getMessage() . PHP_EOL;
			$logMsg .= '## ' . $exception->getFile() . '(' . $exception->getLine() . '):' . PHP_EOL;
			$logMsg .= $exception->getTraceAsString() . PHP_EOL;
			try {
				$log = new CM_Paging_Log_Error();
				$log->add($logMsg);
			} catch (Exception $e) {
				echo 'Cannot log exception (' . $e->getMessage() . ')' . PHP_EOL;
			}

			if ($showError) {
				echo $class . ' (' . $code . '): <b>' . $exception->getMessage() . '</b><br/>';
				echo 'Thrown in: <b>' . $exception->getFile() . '</b> on line <b>' . $exception->getLine() . '</b>:<br/>';
				echo '<div style="margin: 2px 6px;">' . nl2br($exception->getTraceAsString()) . '</div>';
			} else {
				echo 'Internal server error';
			}
			exit(1);
		});
	}

	public function errorHandler() {
		error_reporting((E_ALL | E_STRICT) & ~(E_NOTICE | E_USER_NOTICE));
		set_error_handler(function($errno, $errstr, $errfile, $errline) {
			if (!(error_reporting() & $errno)) {
				// This error code is not included in error_reporting
				$atSign = (0 === error_reporting()); // http://php.net/manual/en/function.set-error-handler.php
				if (!$atSign && IS_DEBUG) {
					$errorCodes = array(E_ERROR => 'E_ERROR', E_WARNING => 'E_WARNING', E_PARSE => 'E_PARSE', E_NOTICE => 'E_NOTICE',
						E_CORE_ERROR => 'E_CORE_ERROR', E_CORE_WARNING => 'E_CORE_WARNING', E_COMPILE_ERROR => 'E_COMPILE_ERROR',
						E_COMPILE_WARNING => 'E_COMPILE_WARNING', E_USER_ERROR => 'E_USER_ERROR', E_USER_WARNING => 'E_USER_WARNING',
						E_USER_NOTICE => 'E_USER_NOTICE', E_STRICT => 'E_STRICT', E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
						E_DEPRECATED => 'E_DEPRECATED', E_USER_DEPRECATED => 'E_USER_DEPRECATED', E_ALL => 'E_ALL');
					$errstr = $errorCodes[$errno] . ': ' . $errstr;
					CM_Debug::get()->addError($errfile, $errline, $errstr);
				}
				return true;
			}
			throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
		});
	}

	public function constants() {
		defined('IS_TEST') || define('IS_TEST', false);
		defined('IS_CRON') || define('IS_CRON', false);
		define('IS_DEBUG', (bool) CM_Config::get()->debug && !IS_TEST);

		define('DIR_SITE_ROOT', dirname(dirname(__DIR__)) . '/');
		define('DIR_LIBRARY', DIR_SITE_ROOT . 'library' . DIRECTORY_SEPARATOR);
		define('DIR_PUBLIC', DIR_SITE_ROOT . 'public' . DIRECTORY_SEPARATOR);
		define('DIR_LAYOUT', DIR_SITE_ROOT . 'layout' . DIRECTORY_SEPARATOR);

		define('DIR_DATA', !empty(CM_Config::get()->dirData) ? CM_Config::get()->dirData : DIR_SITE_ROOT . 'data' . DIRECTORY_SEPARATOR);
		define('DIR_DATA_LOCKS', DIR_DATA . 'locks' . DIRECTORY_SEPARATOR);

		define('DIR_TMP', !empty(CM_Config::get()->dirTmp) ? CM_Config::get()->dirTmp : DIR_SITE_ROOT . 'tmp' . DIRECTORY_SEPARATOR);
		define('DIR_TMP_SMARTY', DIR_TMP . 'smarty' . DIRECTORY_SEPARATOR);

		define('DIR_SMARTY', DIR_LIBRARY . 'Smarty' . DIRECTORY_SEPARATOR);

		define('DIR_USERFILES', !empty(CM_Config::get()->dirUserfiles) ? CM_Config::get()->dirUserfiles :
				DIR_PUBLIC . 'userfiles' . DIRECTORY_SEPARATOR);

		define('DIR_PHPMAILER', DIR_LIBRARY . 'phpmailer' . DIRECTORY_SEPARATOR);

		define('TBL_CM_SMILEY', 'cm_smiley');
		define('TBL_CM_SMILEYSET', 'cm_smileySet');
		define('TBL_CM_USER', 'cm_user');
		define('TBL_CM_USER_ONLINE', 'cm_user_online');
		define('TBL_CM_USER_PREFERENCE', 'cm_user_preference');
		define('TBL_CM_USER_PREFERENCEDEFAULT', 'cm_user_preferenceDefault');
		define('TBL_CM_USERAGENT', 'cm_useragent');
		define('TBL_CM_LOG', 'cm_log');
		define('TBL_CM_IPBLOCKED', 'cm_ipBlocked');

		define('TBL_CM_LANGUAGE', 'cm_language');
		define('TBL_CM_LANGUAGEKEY', 'cm_languageKey');
		define('TBL_CM_LANGUAGEKEY_VARIABLE', 'cm_languageKey_variable');
		define('TBL_CM_LANGUAGEVALUE', 'cm_languageValue');

		define('TBL_CM_LOCATIONCOUNTRY', 'cm_locationCountry');
		define('TBL_CM_LOCATIONSTATE', 'cm_locationState');
		define('TBL_CM_LOCATIONCITY', 'cm_locationCity');
		define('TBL_CM_LOCATIONZIP', 'cm_locationZip');<commit_msg>t353: Refactored error handler in order to log items that failed to get written to the database<commit_after><?php

class CM_Bootloader {

	public function defaults() {
		date_default_timezone_set(CM_Config::get()->timeZone);
		mb_internal_encoding('UTF-8');
		umask(0);
	}

	public function autoloader() {
		spl_autoload_register(function($className) {
			$path = DIR_ROOT . 'library/' . str_replace('_', '/', $className) . '.php';
			if (is_file($path)) {
				require_once $path;
				return;
			}
		});
	}

	public function exceptionHandler() {
		set_exception_handler(function(Exception $exception) {
			$showError = IS_DEBUG || IS_CRON || IS_TEST;

			if (!IS_CRON && !IS_TEST) {
				header('HTTP/1.1 500 Internal Server Error');
			}

			$class = get_class($exception);
			$code = $exception->getCode();

			$logMsg = $class . ' (' . $code . '): ' . $exception->getMessage() . PHP_EOL;
			$logMsg .= '## ' . $exception->getFile() . '(' . $exception->getLine() . '):' . PHP_EOL;
			$logMsg .= $exception->getTraceAsString() . PHP_EOL;
			try {
				$log = new CM_Paging_Log_Error();
				$log->add($logMsg);
			} catch (Exception $e) {
				$log_Entry = '[' . date('d.m.Y - H:i:s', time()) . ']' . PHP_EOL .
								'(' . $e->getCode() . ')' . $e->getMessage() . PHP_EOL .
								'Log Exception: ' . $exception->getFile() . '(' . $exception->getLine() . '):' . PHP_EOL .
								$exception->getTraceAsString() . PHP_EOL .
								'### Original Exception: ' . PHP_EOL .
								$logMsg . PHP_EOL;
				file_put_contents(DIR_DATA_LOG . 'not_logged.log', $log_Entry, FILE_APPEND);
			}

			if ($showError) {
				echo $class . ' (' . $code . '): <b>' . $exception->getMessage() . '</b><br/>';
				echo 'Thrown in: <b>' . $exception->getFile() . '</b> on line <b>' . $exception->getLine() . '</b>:<br/>';
				echo '<div style="margin: 2px 6px;">' . nl2br($exception->getTraceAsString()) . '</div>';
			} else {
				echo 'Internal server error';
			}
			exit(1);
		});
	}

	public function errorHandler() {
		error_reporting((E_ALL | E_STRICT) & ~(E_NOTICE | E_USER_NOTICE));
		set_error_handler(function($errno, $errstr, $errfile, $errline) {
			if (!(error_reporting() & $errno)) {
				// This error code is not included in error_reporting
				$atSign = (0 === error_reporting()); // http://php.net/manual/en/function.set-error-handler.php
				if (!$atSign && IS_DEBUG) {
					$errorCodes = array(E_ERROR => 'E_ERROR', E_WARNING => 'E_WARNING', E_PARSE => 'E_PARSE', E_NOTICE => 'E_NOTICE',
						E_CORE_ERROR => 'E_CORE_ERROR', E_CORE_WARNING => 'E_CORE_WARNING', E_COMPILE_ERROR => 'E_COMPILE_ERROR',
						E_COMPILE_WARNING => 'E_COMPILE_WARNING', E_USER_ERROR => 'E_USER_ERROR', E_USER_WARNING => 'E_USER_WARNING',
						E_USER_NOTICE => 'E_USER_NOTICE', E_STRICT => 'E_STRICT', E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
						E_DEPRECATED => 'E_DEPRECATED', E_USER_DEPRECATED => 'E_USER_DEPRECATED', E_ALL => 'E_ALL');
					$errstr = $errorCodes[$errno] . ': ' . $errstr;
					CM_Debug::get()->addError($errfile, $errline, $errstr);
				}
				return true;
			}
			throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
		});
	}

	public function constants() {
		defined('IS_TEST') || define('IS_TEST', false);
		defined('IS_CRON') || define('IS_CRON', false);
		define('IS_DEBUG', (bool) CM_Config::get()->debug && !IS_TEST);

		define('DIR_SITE_ROOT', dirname(dirname(__DIR__)) . '/');
		define('DIR_LIBRARY', DIR_SITE_ROOT . 'library' . DIRECTORY_SEPARATOR);
		define('DIR_PUBLIC', DIR_SITE_ROOT . 'public' . DIRECTORY_SEPARATOR);
		define('DIR_LAYOUT', DIR_SITE_ROOT . 'layout' . DIRECTORY_SEPARATOR);

		define('DIR_DATA', !empty(CM_Config::get()->dirData) ? CM_Config::get()->dirData : DIR_SITE_ROOT . 'data' . DIRECTORY_SEPARATOR);
		define('DIR_DATA_LOCKS', DIR_DATA . 'locks' . DIRECTORY_SEPARATOR);
		define ('DIR_DATA_LOG', DIR_DATA . 'logs' . DIRECTORY_SEPARATOR);

		define('DIR_TMP', !empty(CM_Config::get()->dirTmp) ? CM_Config::get()->dirTmp : DIR_SITE_ROOT . 'tmp' . DIRECTORY_SEPARATOR);
		define('DIR_TMP_SMARTY', DIR_TMP . 'smarty' . DIRECTORY_SEPARATOR);

		define('DIR_SMARTY', DIR_LIBRARY . 'Smarty' . DIRECTORY_SEPARATOR);

		define('DIR_USERFILES', !empty(CM_Config::get()->dirUserfiles) ? CM_Config::get()->dirUserfiles :
				DIR_PUBLIC . 'userfiles' . DIRECTORY_SEPARATOR);

		define('DIR_PHPMAILER', DIR_LIBRARY . 'phpmailer' . DIRECTORY_SEPARATOR);

		define('TBL_CM_SMILEY', 'cm_smiley');
		define('TBL_CM_SMILEYSET', 'cm_smileySet');
		define('TBL_CM_USER', 'cm_user');
		define('TBL_CM_USER_ONLINE', 'cm_user_online');
		define('TBL_CM_USER_PREFERENCE', 'cm_user_preference');
		define('TBL_CM_USER_PREFERENCEDEFAULT', 'cm_user_preferenceDefault');
		define('TBL_CM_USERAGENT', 'cm_useragent');
		define('TBL_CM_LOG', 'cm_log');
		define('TBL_CM_IPBLOCKED', 'cm_ipBlocked');

		define('TBL_CM_LANGUAGE', 'cm_language');
		define('TBL_CM_LANGUAGEKEY', 'cm_languageKey');
		define('TBL_CM_LANGUAGEKEY_VARIABLE', 'cm_languageKey_variable');
		define('TBL_CM_LANGUAGEVALUE', 'cm_languageValue');

		define('TBL_CM_LOCATIONCOUNTRY', 'cm_locationCountry');
		define('TBL_CM_LOCATIONSTATE', 'cm_locationState');
		define('TBL_CM_LOCATIONCITY', 'cm_locationCity');
		define('TBL_CM_LOCATIONZIP', 'cm_locationZip');
{'fraction_non_alphanumeric': 0.18173625713427455, 'fraction_numerical': 0.0029037749073795934, 'mean_word_length': 4.75014392630973, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4605581', 'n_tokens_mistral': 4189, 'n_tokens_neox': 3751, 'n_words': 589}