<commit_before><?php
/**
 * The file that defines the Backend class.
 *
 * @author <NAME> (JadeIT cc) <<EMAIL>> - initial API and implementation
 * @copyright Copyright (c) 2009 JadeIT cc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available {@link http://www.eclipse.org/legal/epl-v10.html here}
 * @license http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0
 * @package Core
 */

/**
 * The Backend class
 */
class Backend {
	static private $initialized = false;
	static private $vars = array();
	static private $DB = array();
	static private $config = false;
	static private $options = array();

	static private function checkSelf() {
		$toret = false;
		if (!self::$initialized) {
			self::init();
		}
		$toret = self::$initialized;
		return $toret;
	}
	
	static public function init(array $options = array()) {
		self::$initialized = true;
		self::$options = $options;
		if (!defined('SITE_STATE')) {
			define('SITE_STATE', 'production');
		}
		require(BACKEND_FOLDER . '/functions.inc.php');
		require(BACKEND_FOLDER . '/modifiers.inc.php');
		spl_autoload_register(array('Backend', '__autoload'));
		set_error_handler    (array('Backend', '__error_handler'));
		set_exception_handler(array('Backend', '__exception_handler'));
		
		//Some constants
		$url = parse_url(get_current_url());
		$url = $url['host'] . dirname($url['path']);
		if (substr($url, strlen($url) - 1) != '/') {
			$url .= '/';
		}

		//Configs
		self::initConfigs();

		define('SITE_LINK', 'http://' . $url);
		if (Backend::getConfig('backend.application.use_ssl', false)) {
			define('S_SITE_LINK', 'https://' . $url);
		} else {
			define('S_SITE_LINK', 'http://' . $url);
		}
		Backend::add('SITE_LINK', SITE_LINK);
		Backend::add('S_SITE_LINK', S_SITE_LINK);
		
		//Application Values
		$values = self::$config->getValue('application');
		if ($values) {
			foreach($values as $name => $value) {
				self::add($name, $value);
			}
		}

		//Backend Values
		$values = self::$config->getValue('backend.values');
		if ($values) {
			foreach($values as $name => $value) {
				self::add($name, $value);
			}
		}
		
		//DBs
		$dbs = self::$config->getValue('backend.dbs');
		if ($dbs) {
			self::initDBs($dbs);
		}
	}
	
	static private function initConfigs() {
		$ini_file = array_key_exists('config_file', self::$options) ? self::$options['config_file'] : BACKEND_FOLDER . '/configs/configure.ini';
		self::$config = new Config($ini_file, SITE_STATE);
	}
		
	static private function initDBs($dbs) {
		if (is_array($dbs)) {
			foreach($dbs as $name => $db) {
				self::addDB($name, $db);
			}
		}
	}
	
	static public function __error_handler($number, $string, $file = false, $line = false, $context = false) {
		switch ($number) {
		case 2:
			preg_match_all('/Missing argument ([0-9]+) for ([^:]+)::([^\(\)]+)\(\), called in ([\S]+) on line ([0-9]+)/', $string, $vars, PREG_SET_ORDER);
			if (!empty($vars)) {
				list($matches, $arg_num, $class, $method, $file, $line) = current($vars);
			}
			Controller::addError("Missing parameter $arg_num for $class::$method, called in $file line $line");
			return true;
			break;
		}
		return false;
	}
	
	private static function __exception_handler($exception) {
		echo "Uncaught exception: " , $exception->getMessage(), "\n";
	}

	static public function __autoload($classname) {
		$included = false;
		//TODO eventually cache / determine by class name exactly where the file should be to improve performance
		$folders = array(
			APP_FOLDER . '/models/' => 'model',
			BACKEND_FOLDER . '/models/' => 'model',
			APP_FOLDER . '/controllers/' => 'controller',
			BACKEND_FOLDER . '/controllers/' => 'controller',<commit_msg>More errors caught<commit_after><?php
/**
 * The file that defines the Backend class.
 *
 * @author <NAME> (JadeIT cc) <<EMAIL>> - initial API and implementation
 * @copyright Copyright (c) 2009 JadeIT cc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available {@link http://www.eclipse.org/legal/epl-v10.html here}
 * @license http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0
 * @package Core
 */

/**
 * The Backend class
 */
class Backend {
	static private $initialized = false;
	static private $vars = array();
	static private $DB = array();
	static private $config = false;
	static private $options = array();

	static private function checkSelf() {
		$toret = false;
		if (!self::$initialized) {
			self::init();
		}
		$toret = self::$initialized;
		return $toret;
	}
	
	static public function init(array $options = array()) {
		self::$initialized = true;
		self::$options = $options;
		if (!defined('SITE_STATE')) {
			define('SITE_STATE', 'production');
		}
		require(BACKEND_FOLDER . '/functions.inc.php');
		require(BACKEND_FOLDER . '/modifiers.inc.php');
		spl_autoload_register(array('Backend', '__autoload'));
		set_error_handler    (array('Backend', '__error_handler'));
		set_exception_handler(array('Backend', '__exception_handler'));
		
		//Some constants
		$url = parse_url(get_current_url());
		$url = $url['host'] . dirname($url['path']);
		if (substr($url, strlen($url) - 1) != '/') {
			$url .= '/';
		}

		//Configs
		self::initConfigs();

		define('SITE_LINK', 'http://' . $url);
		if (Backend::getConfig('backend.application.use_ssl', false)) {
			define('S_SITE_LINK', 'https://' . $url);
		} else {
			define('S_SITE_LINK', 'http://' . $url);
		}
		Backend::add('SITE_LINK', SITE_LINK);
		Backend::add('S_SITE_LINK', S_SITE_LINK);
		
		//Application Values
		$values = self::$config->getValue('application');
		if ($values) {
			foreach($values as $name => $value) {
				self::add($name, $value);
			}
		}

		//Backend Values
		$values = self::$config->getValue('backend.values');
		if ($values) {
			foreach($values as $name => $value) {
				self::add($name, $value);
			}
		}
		
		//DBs
		$dbs = self::$config->getValue('backend.dbs');
		if ($dbs) {
			self::initDBs($dbs);
		}
	}
	
	static private function initConfigs() {
		$ini_file = array_key_exists('config_file', self::$options) ? self::$options['config_file'] : BACKEND_FOLDER . '/configs/configure.ini';
		self::$config = new Config($ini_file, SITE_STATE);
	}
		
	static private function initDBs($dbs) {
		if (is_array($dbs)) {
			foreach($dbs as $name => $db) {
				self::addDB($name, $db);
			}
		}
	}
	
	static public function __error_handler($number, $string, $file = false, $line = false, $context = false) {
		switch ($number) {
		case 2:
			preg_match_all('/Missing argument ([0-9]+) for ([\S]+)::([^\(\)]+)\(\), called in ([\S]+) on line ([0-9]+)/', $string, $vars, PREG_SET_ORDER);
			if (!empty($vars)) {
				list($matches, $arg_num, $class, $method, $call_file, $call_line) = current($vars);
				Controller::addError("Missing parameter $arg_num for $class::$method, called in $call_file line $call_line, defined in $file line $line");
				return true;
			}
			preg_match_all('/Missing argument ([0-9]+) for ([\S]+)::([^\(\)]+)\(\)/', $string, $vars, PREG_SET_ORDER);
			if (!empty($vars)) {
				list($matches, $arg_num, $class, $method) = current($vars);
				Controller::addError("Missing parameter $arg_num for $class::$method, defined in $file line $line");
				return true;
			}
			break;
		}
		return false;
	}
	
	private static function __exception_handler($exception) {
		echo "Uncaught exception: " , $exception->getMessage(), "\n";
	}

	static public function __autoload($classname) {
		$included = false;
		//TODO eventually cache / determine by class name exactly where the file should be to improve performance
		$folders = array(
			APP_FOLDER . '/models/' => 'model',
			BACKEND_FOLDER . '/models/' => 'model',
			APP_FOLDER . '/controllers/' => 'controller',
			BACKEND_FOLDER . '/controllers/' => 'controller',
{'fraction_non_alphanumeric': 0.18161350844277674, 'fraction_numerical': 0.004752970606629143, 'mean_word_length': 4.060759493670886, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '776096', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2696, 'n_words': 652}