<?

include_once('class/Exception.class.php');
include_once('class/Config.class.php');

/**
 * $Id: core.php 1184 2008-08-28 13:54:26Z pawy $
 * Klasa jadra aplikacji
 *
 */
class Core {

	/**
	 * Dopuszczalne typy stron.. tylko po co?
	 *
	 * @var array
	 */
	private static $site = array('Strona', 'Admin');
	public static $template = 'index.tpl';
	public static $appSafeMode = false;

	/**
	 * Konstruktor klasy
	 *
	 * @param PageType $site Typ strony
	 */
	public function __construct() {
		
	}

	/**
	 * Inicjator aplikacji
	 *
	 * @param integer $site
	 */
	public static function Init($site) {
		include('class/DB.class.php');
		include('class/Registry.class.php');
		self::RequireOnce('class/DbSqlLite.class.php');

		self::LoadConfig($site);
		include('ErrorHandler.php');
		try {
			$db = new DBProd();
			$dbTemp = new DBTemp();
		} catch (MysqlException $e) {
			MFLog::Fatal("Line: " . $e->getLine() . " Message: " . $e->getMessage() . " Referer: " . getenv('HTTP_REFERER'));
		}
		Registry::Set('db', $db);
		Registry::Set('dbTemp', $dbTemp);
		Config::Set('site', $site);
		Registry::Set('smartyTemplate', self::$template);
		$title = array();
		Registry::Set('title', $title);
		self::PageConfig();
	}

	public static function SetAppSafeMode() {
		MFLog::Error('Switching into the SafeMod.');
		if (Config::Get('ERROR_REDIRECT') == true) {
			self::$appSafeMode = true;
		}
	}

	public static function GetAppSafeMode() {
		return self::$appSafeMode;
	}

	/**
	 * Parametry konfiguracyjne strony zmieniane przez autora lub administratora
	 *
	 */
	public static function PageConfig() {

		try {
			// zaczytuje do rejestru dane z tabeli Setup
			$config = SetupDAL::GetAllVariables();
			$rConfig = array();

			foreach ($config as $key => $value) {
				//Utils::ArrayDisplay($config);
				$rConfig[$value['variable']] = $value['value'];
			}

			Config::SetDbConfig($rConfig);
		} catch (MysqlException $e) {
			self::SetAppSafeMode();
		}
		/*
		  $captchaStatus = true;
		  $moderateStatus = false;
		  Registry::Set('captchaStatus', $captchaStatus);
		  Registry::Set('moderateStatus', $moderateStatus);
		 */

		// ustawienia local dla czasu
		setlocale(LC_TIME, 'pl_PL', 'pl', 'Polish_Poland.28592');
	}

	/**
	 * Ladowanie plikow konfiguracyjnych
	 *
	 * @param PageType $site Typ strony
	 */
	public static function LoadConfig($site) {
		include_once('config/' . $site . '/path.config.php');
		//include_once('config/'.$site.'/db.config.php');
		include_once('config/' . $site . '/param.config.php');
		Config::LoadIniConfig(Config::Get('PATH_CORE') . '/config/' . $site . '/db.config.ini', true);
	}

	/**
	 * Ladowanie i konfiguracja Smarty
	 *
	 */
	public static function LoadSmarty() {
		require_once('lib/Smarty3/Smarty.class.php');
		require_once('lib/SmartyValidate/libs/SmartyValidate.class.php'); 
		$smarty = new Smarty();
		$smarty = new TemplateMaster();
//
		$smarty->template_dir = Config::Get('PATH_SMARTY_TEMPLATE');
		$smarty->compile_dir = Config::Get('PATH_SMARTY_COMPILE');
		$smarty->cache_dir = Config::Get('PATH_SMARTY_CACHE');
		$smarty->addPluginsDir(Config::Get('PATH_SMARTY_PLUGINS'));
		$smarty->config_dir = Config::Get('PATH_CONFIG');
		$smarty->cache_lifetime = 999999999;
		//Utils::ArrayDisplay($smarty);
		if(Config::Get('SMARTY_INTERNAL_CACHING') == true) {
			$smarty->caching = Config::Get('SMARTY_CACHING_METHOD');
		} else {
			$smarty->caching = 0;
		}
		//$smarty->error_reporting = 0;
		$smarty->assign('urlStatic', Config::Get('URL_STATIC_CONTENT'));
		//$smarty->assign('commentIndent', COMMENT_INDENT);
//		$smarty->assign('captchaStatus', Registry::Get('captchaStatus'));
//		$smarty->assign('commentAmateur', Registry::Get('commentAmateur'));
//		$smarty->assign('commentModel', Registry::Get('commentModel'));
//		$smarty->assign('commentShowGalery', Registry::Get('commentShowGalery'));
//		$smarty->assign('commentShowVideo', Registry::Get('commentShowVideo'));
//		$smarty->assign('commentBackstageGalery', Registry::Get('commentBackstageGalery'));
//		$smarty->assign('commentBackstageVideo', Registry::Get('commentBackstageVideo'));
		
		$smarty->assign('dynamicContent', null);
		$smarty->assign("pageStyle", 'style.css');
		
		$smarty->assign('urlMain', Config::Get('URL_MAIN'));
		$smarty->assign('pageDescription', null);
		MFLog::Warn(serialize($smarty));
//		//Utils::ArrayDisplay($smarty);
		//$smarty->cache
		//$smarty->testInstall();
		Registry::Set('smarty', $smarty);
	}

	/**
	 * Ladowanie i konfiguracja Smarty
	 *
	 */
	public static function _LoadSmarty() {
		require_once('lib/Smarty/Smarty.class.php');
		$smarty = new Smarty();

		$smarty = new TemplateMaster();

		$smarty->template_dir = Config::Get('PATH_SMARTY_TEMPLATE');
		$smarty->compile_dir = Config::Get('PATH_SMARTY_COMPILE');
		$smarty->cache_dir = Config::Get('PATH_SMARTY_CACHE');
		$smarty->plugins_dir[] = Config::Get('PATH_SMARTY_PLUGINS');
		$smarty->cache_lifetime = 999999999;
		if (Config::Get('SMARTY_INTERNAL_CACHING') == true) {
			$smarty->caching = Config::Get('SMARTY_CACHING_METHOD');
		} else {
			$smarty->caching = 0;
		}
		$smarty->register_block('dynamic', 'SmartyBlockDynamic', false);
		$smarty->assign('urlStatic', Config::Get('URL_STATIC_CONTENT'));
		//$smarty->assign('commentIndent', COMMENT_INDENT);
//		$smarty->assign('captchaStatus', Registry::Get('captchaStatus'));
//		$smarty->assign('commentAmateur', Registry::Get('commentAmateur'));
//		$smarty->assign('commentModel', Registry::Get('commentModel'));
//		$smarty->assign('commentShowGalery', Registry::Get('commentShowGalery'));
//		$smarty->assign('commentShowVideo', Registry::Get('commentShowVideo'));
//		$smarty->assign('commentBackstageGalery', Registry::Get('commentBackstageGalery'));
//		$smarty->assign('commentBackstageVideo', Registry::Get('commentBackstageVideo'));

		$smarty->assign('dynamicContent', null);
		$smarty->assign("pageStyle", 'style.css');

		$smarty->assign('urlMain', Config::Get('URL_MAIN'));
		$smarty->assign('pageDescription', null);
		MFLog::Warn(serialize($smarty));
		//Utils::ArrayDisplay($smarty);
		Registry::Set('smarty', $smarty);
	}

	/**
	 * Wyswietla szablon Smarty
	 *
	 * @return Wgenerowana strona
	 */
	public static function SmartyDisplay() {
		$smarty = Registry::Get('smarty');
		return $smarty->display(self::$template);
	}

	/**
	 * Laduje pliki z klasami
	 *
	 * @param $name Nazwa pozadanej klasy
	 */
	public static function LoadClass($name) {
		//echo $name." <br>";


		if (file_exists(Config::Get('PATH_CORE') . '/class/' . $name . '.class.php')) {
			self::RequireOnce(Config::Get('PATH_CORE') . '/class/' . $name . '.class.php');
		} else if (file_exists('module/' . $name . '.mod.php')) {
			self::RequireOnce('module/' . $name . '.mod.php');
		} else if (file_exists(Config::Get('PATH_CORE') . '/model/' . $name . '.class.php')) {
			self::RequireOnce(Config::Get('PATH_CORE') . '/model/' . $name . '.class.php');
		} else if (file_exists(Config::Get('PATH_CORE') . '/lib/' . $name . '.class.php')) {
			self::RequireOnce(Config::Get('PATH_CORE') . '/lib/' . $name . '.class.php');
		} else if (file_exists(PATH_CORE . '/model/' . strtolower(str_replace('DAL', '', $name)) . '/' . $name . '.class.php')) {
			include(PATH_CORE . '/model/' . strtolower(str_replace('DAL', '', $name)) . '/' . $name . '.class.php');
		} else {


			//echo $name."<br>";
			preg_match_all('/[A-Z]/', $name, $matches);

			if (isset($matches[0][1])) {


				$firstChar = substr($name, 0, 1);
				$fullpathArray = explode($matches[0][1], substr($name, 1), 2);
				$fullpathFile = PATH_CORE . '/model/' . strtolower($firstChar) . $fullpathArray[0] . '/' . $matches[0][1] . $fullpathArray[1] . '.class.php';
				if (file_exists($fullpathFile)) {
					include($fullpathFile);
				}
			}

			$path = str_replace('_', '/', $name);

//			$filePath = array_shift($filePath);
//
//			if(is_array($filePath)) {
//				$path = implode('/', $filePath);
//			} else {
//				$path = $filePath;
//			}
//			$name = str_replace($path.'_', '', $name);

			if (file_exists(Config::Get('PATH_CORE') . '/' . $path . '.class.php')) {
				self::RequireOnce(Config::Get('PATH_CORE') . '/' . $path . '.class.php');
			} else if (file_exists(Config::Get('PATH_CORE') . '/class/' . $path . '.class.php')) {
				self::RequireOnce(Config::Get('PATH_CORE') . '/class/' . $path . '.class.php');
			} else if (file_exists(Config::Get('PATH_CORE') . '/model/' . $path . '.class.php')) {
				self::RequireOnce(Config::Get('PATH_CORE') . '/model/' . $path . '.class.php');
			} else if (file_exists(Config::Get('PATH_CORE') . '/lib/' . $path . '.class.php')) {
				self::RequireOnce(Config::Get('PATH_CORE') . '/lib/' . $path . '.class.php');
			}
		}
	}

	/**
	 * Zamyka aplikacje
	 *
	 */
	public static function Garbage() {
		$db = Registry::Get('db');
		$db->__destruct();
	}

	public static function RequireOnce($file) {
		require_once($file);
	}

	public static function LoadPlugin($plugin) {
		self::RequireOnce('plugins/' . $plugin . '.php');
	}

}

/**
 * Klasa wyboru typu aplikacji
 *
 */
class PageType {

	/**
	 * Część użytkowa
	 *
	 */
	const STRONA = 'Strona';

	/**
	 * Część użytkowa
	 *
	 */
	const PLAY = 'Play';

	/**
	 * Część użytkowa
	 *
	 */
	const Lubricant = 'Lubricant';

	/**
	 * Część administracyjna
	 */
	const Company = 'Company';

	/**
	 * Część administracyjna
	 */
	const ADMIN = 'Admin';

	/**
	 * Część serwerowa
	 */
	const SERVER = 'Server';

}

/**
 * Bloka dynamiczny jako funkcja do smarty
 *
 * @param unknown_type $param
 * @param unknown_type $content
 * @param unknown_type $smarty
 * @return unknown
 */
function SmartyBlockDynamic($param, $content, &$smarty) {
	return $content;
}

/**
 * Zwraca biezacy url
 *
 * @return unknown
 */
function SelfUrl() {
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$protocol = StrLeft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/") . $s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":" . $_SERVER["SERVER_PORT"]);
	return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}

/**
 * Funkcja pomocnicza do SelfUrl()
 *
 * @param unknown_type $s1
 * @param unknown_type $s2
 * @return unknown
 */
function StrLeft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2));
}

?>