<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, 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.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://www.gekkoware.org
	*/

	// this constant must be checked in each included file that is supposed to be loaded here, like a library
	define("IN-GEKKO", true);

	// unicode output
	header("Content-Type: text/html; charset=UTF-8");

	// redirecting to install.php when dbconf.php is empty
	if (!filesize("dbconf.php"))
		die(header("Location: install.php"));

	require_once "conf.php";

	// initializing gziped output
	require_once "lib/gzenc.lib.php";
	gzinit();

	require_once "lib/template.lib.php";

	require_once "lib/format.lib.php";

	// database connection
	db_init();

	require_once "lib/auth.inc.php";

	// $mod variable will store the main requested module
	unset($mod);

	$module = null;
	if (isset($_SERVER["PATH_INFO"])) {
		// http://www.example.com/index.php/module/parameters
		$inf = explode("/", trim($_SERVER["PATH_INFO"], "/"));
		$module = $inf[0];
	} elseif (isset($_GET["sec"]) || isset($_GET["module"])) {
		$module = isset($_GET["sec"]) ? $_GET["sec"] : $_GET["module"];
	} elseif (isset($_SERVER["argv"]) && count($_SERVER["argv"])) {
		// http://www.example.com/index.php?section
		$module = $_SERVER["argv"][0];
	}

	$mc = new ModCore();

	// checking module availability
	if (!($module && $mc->check($module)))
		// using default module when provided module string is incorrect or doesn't exists
		$module = Conf::getKey("modules", "default");

	define("MAIN_MODULE", $module);

	// loading core.inc.php script from each allowed module.
	$modules = $mc->getModules();
	foreach ($modules as $mod) {
		$core = GEKKO_ROOT."modules/$mod/core.inc.php";
		if (file_exists($core))
			require $core;
	}

	if ($module) {

		// checking for main module's execution permissions
		$db->query("SELECT auth_Exec FROM ".db_table("module")." WHERE Module = '$module'", __FILE__, __LINE__);
		$row = $db->fetch_row();

		if (gauth($row["auth_Exec"])) {
			// access to this module is granted, cleaning module's data buffer,
			// loading main language and configuration options
			$mcBuff = "";
			$Conf->Load($module);
			loadLang($module);
			require $mc->root($module)."main.inc.php";
		} else {
			// access denied, this user has not enough privileges
			//$mc->save(draw_message("error", _l("E_ACCESS_DENIED")));
		}
	} else {
		$mc->save("<h1>Gekko | Web Development Framework</h1>
		"._l("L_GEKKO_WELCOME"));
	}


	/*
		bugfixing
	*/
	$L["M_BLOCKC"] = "";
	// when working in ssl mode
	if (isset($_SERVER["HTTPS"]))
		$L["M_BLOCKC"] .= draw_message("warning", _l("L_SSL_ENABLED"), "network.png");

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

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

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

	// invoking our template engine
	$t = new gtEngine(true);

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

	// some interesting variables
	$L["V_SCRIPT_EXEC_TIME"] = substr((getmicrotime() - GEKKO_START_TIME), 0, 5);
	$L["V_DB_QUERIES"] = $db->counter();

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

	// showing data and closing connections

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

	db_exit();

	gzoutput();
?>
