<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

	define("IN-GEKKO", true);

	// trying to detect if Gekko is already installed.
	if (!filesize("dbconf.php"))
		die(header("Location: install.php"));

	require_once "conf.php";

	define("GEKKO_XML_OUTPUT", (isset($_POST["ajax"]) && $_POST["ajax"]) || (isset($_GET["ajax"]) && $_GET["ajax"]));

	if (GEKKO_XML_OUTPUT) {
		define ("GEKKO_USE_ABSOLUTE_URLS", true);
		_l("C_SITE.REL_URL", "");
	}

	require_once "lib/template.lib.php";

	require_once "lib/format.lib.php";

	require_once "lib/gzenc.lib.php";

	// initializing database connection
	db_init();

	require_once "lib/auth.inc.php";

	// General URL prototype
	extract (
		urlGetArgs("module=alpha")
	);

	if (!GEKKO_VORAZ_CACHE || (is_user() || !cacheRead(GEKKO_PAGE_CACHE_KEY, $L))) {

		$mc = new gekkoModule();

		// module availability
		if (!isset($module) || !$mc->exists($module)) {
			define("GEKKO_DEFAULT_VIEW", true);
			// when check fails, then uses the default module
			$module = conf::getkey("modules", "default");
		}

		define("MAIN_MODULE", $module);

		// Loading core.inc.php script from each allowed module.
		$allowed_modules = $mc->getList();

		foreach ($allowed_modules as $mod) {
			$core = GEKKO_SOURCE_DIR."modules/{$mod}/core.inc.php";
			if (file_exists($core))
				require_once $core;
		}

		if ($module) {
			if ($mc->auth($module)) {
				// loading main language and configuration options
				$mcBuff = "";

				$Conf->Load($module);

				$Lang->loadFromModule($module);

				if (!defined("GEKKO_DEFAULT_VIEW"))
					pageSetSubtitle(_l("L_".strtoupper($module)));

				require ($mc->srcPath($module)."main.inc.php");

				$mc->save($mcBuff);
				// including main
			} else {
				// access denied, this user has not enough privileges
				$mc->save(mkMessageBox("error", _l("E_ACCESS_DENIED")));
			}

		} else {
			$mc->save("<h1>Gekko | Web Development Framework</h1>
			"._l("L_GEKKO_WELCOME"));
		}

		$L["M_BLOCK_C"] = "";

		// session variables
		$sess_msgs = Array("error", "info", "success", "warning", "hint");
		foreach ($sess_msgs as $msg) {
			if (isset($_SESSION[$msg])) {
				$L["M_BLOCK_C"] .= mkMessageBox($msg, $_SESSION[$msg]);
				unset($_SESSION[$msg]);
			}
		}

		// when working in ssl mode
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off")
			$L["M_BLOCK_C"] .= mkMessageBox("warning", _l("L_SSL_ENABLED"), "network.png");

		// displaying message
		if (isset($_GET["message"]))
			$L["M_BLOCK_C"] .= mkMessageBox((isset($_GET["type"]) ? $_GET["type"] : "info"), preg_replace("/(<|>)/e", "htmlspecialchars('\\1')", $_GET["message"]));

		$L["M_BLOCK_C"] .= $mc->buff ? $mc->buff : mkMessageBox("info", _l("L_EMPTY_PAGE"));

		// setting the correct colspan variables for "framework" table
		$L["V_FW_COLSPAN"] = 1+($L["M_BLOCK_L"] != "")+($L["M_BLOCK_R"] != "");

		// plugin call
		$PluginCore->triggerEvent("index_prepare", $buff);

		// some stylish stuff: title separator
		if ($L["V_DOC_TITLE"]) $L["V_DOC_TITLE"] .= TITLE_SEPARATOR;

		// caching page display (for non-users)
		if (GEKKO_VORAZ_CACHE && !is_user())
			cacheSave(GEKKO_PAGE_CACHE_KEY, $L);
	}


	gekko_session_save();

	gzinit();

	if (is_array($L)) {
		// those variables are different for each user
		$L["C_AUTH_STR"] = mkAuthStr();
		$L["C_ANTIBOT"] = jscriptObfuscateString($L["C_AUTH_STR"]);

		// application statistics
		$L["V_SCRIPT_EXEC_TIME"] = substr((getmicrotime() - GEKKO_START_TIME), 0, 5);
		$L["V_MEMORY_USAGE"] = (function_exists('memory_get_usage') && GEKKO_ENABLE_DEBUG) ? round(memory_get_usage()/pow(1024, 2), 2)."Mb" : "";
		$L["V_DATABASE_QUERIES"] = $db->counter();

		// parsing template
		if (GEKKO_XML_OUTPUT) {

			header("Content-Type: text/xml; charset=UTF-8");

			$buff = new BlockWidget("main/index.xml.tpl");

			foreach ($L as $key => $content) {
				if (substr($key, 0, 2) == "M_") {
					$vars = Array();
					$vars["POSITION"] = strtoupper(substr($key, 2));
					$vars["POSITION"] = str_replace("_", "", $vars["POSITION"]);
					$vars["POSITION"] = strtolower(substr($vars["POSITION"], 0, 5)).substr($vars["POSITION"], 5);
					$vars["CONTENT"] = $content;
					$buff->setArray($vars, "BLOCK");
					$buff->saveBlock("BLOCK");
				}
			}

			$buff = $buff->make();

			$T->output($buff, true);

			echo $buff;
		} else {
			// normal page output
			header("Content-Type: text/html; charset=UTF-8");

			$buff = $T->load("main/index.tpl", true, true);

			$T->output($buff, true);

			echo $buff;
		}

	} else {
		// cached XML document (it might be a feed)
		header("Content-type: text/xml; charset=UTF-8");

		echo $L;
	}

	gzoutput();

	db_exit();
?>