<?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
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	function getmicrotime() {
		$mtime = explode(" ",microtime());
		return ((float)$mtime[0] + (float)$mtime[1]);
	}

	// registering Gekko's initiation time
	define ("GEKKO_START_TIME", getmicrotime());

	// we don't need such a 'feature'
	ini_set("register_globals", false);

	// Gekko's root (path to the source directory)
	define("GEKKO_SOURCE_DIR", dirname(__FILE__)."/");

	// nothing for this...
	define("GEKKO_LANGUAGE_ID", "");

	// If you're planning to use Gekko in a vhost.example.org scheme, then
	// you may find useful this feature.
	define("GEKKO_SUBDOMAIN_MODE", false);

	if (GEKKO_SUBDOMAIN_MODE) {

		preg_match("/([^.]*)\..*/", $_SERVER["HTTP_HOST"], $subdomain);
		if (isset($subdomain[1])) {
			define ("GEKKO_SUBDOMAIN", $subdomain[1]);

			define ("GEKKO_USER_DIR", GEKKO_SOURCE_DIR."virtual/".GEKKO_SUBDOMAIN."/");

			$requires = array("dbconf.php", "cgdb.php", "data", "temp");

			foreach ($requires as $require)
				if (!file_exists(GEKKO_USER_DIR."/{$require}"))
					trigger_error("File '/virtual/".GEKKO_SUBDOMAIN."/{$require}' for '".GEKKO_SUBDOMAIN."' does not exist.", E_USER_ERROR);

		} else {
			trigger_error("Please make sure that you're inside a subdomain.example.org environment or that GEKKO_SUBDOMAIN_MODE is set to 'false' in conf.php file.");
		}
	}

	if (!defined("GEKKO_USER_DIR"))
		define("GEKKO_USER_DIR", dirname(__FILE__)."/");

	define("GEKKO_MODULE_DIR", GEKKO_SOURCE_DIR."modules/");
	define("GEKKO_TEMP_DIR", GEKKO_USER_DIR."temp/");
	define("GEKKO_DATA_DIR", GEKKO_USER_DIR."data/");
	define("GEKKO_LIB_DIR", GEKKO_USER_DIR."lib/");
	define("GEKKO_TEMPLATE_DIR", GEKKO_SOURCE_DIR."templates/");

	// enable this for showing Gekko detailed errors (useful when you're
	// developing modules or the core)
	define("GEKKO_ENABLE_DEBUG", false);

	// current version of gekko (as distribution)
	define("GEKKO_VERSION", "0.7");

	// let Gekko save a cache of everithing possible?
	define("GEKKO_VORAZ_CACHE", true);

	// default language files to load
	define("GEKKO_DEFAULT_LANG", "es");

	// seconds that a nasty user must be banned when trying to perform
	// common attacks such as d.o.s. against Gekko engine
	define("GEKKO_BAN_TIME", 600);

	// use 'real ip' (not 'real' since it can be faked) instead of the
	// proxy ip
	define("GEKKO_REAL_IP", false);

	// enables gekko demo mode (all module "action" requests are denied for
	// privileged users)
	define("GEKKO_DEMO_MODE", false);

	// set this as 'true' when you're having 'connection timeout' problems
	// or if you know your host is not allowing secure http (shttp)
	// connections.
	define("GEKKO_NO_SSL", true);

	// Do you want to use pretty urls of the classical ones?
	// pretty: index.php/module/action
	// classic: index.php?module=foo&action=bar
	define("GEKKO_USE_PRETTY_URLS", true);

	if (GEKKO_ENABLE_DEBUG) {
		error_reporting(E_ALL);
		ini_set("display_errors", true);
	}

	function getIP() {
		$ip = false;
		$ips = array();
		if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		}
		if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
			$ips = explode(", ", $_SERVER["HTTP_X_FORWARDED_FOR"]);
			if ($ip) { array_unshift($ips, $ip); $ip = false; }
		}
		for ($i = 0; $i < count($ips); $i++) {
			if (!eregi("^(10|172\.16|192\.168)\.", $ips[$i])) {
				$ip = $ips[$i];
				break;
			}
		}
		return (($ip && GEKKO_REAL_IP == true) ? $ip : $_SERVER["REMOTE_ADDR"]);
	}

	define ("IP_ADDR", getIP());

	// trying to solve some problems between different PHP versions
	if (isset($_SERVER["ORIG_PATH_INFO"]) && $_SERVER["ORIG_PATH_INFO"])
		$_SERVER["PATH_INFO"] = $_SERVER["ORIG_PATH_INFO"];

	if (isset($_ENV["ORIG_PATH_INFO"]) && $_ENV["ORIG_PATH_INFO"])
		$_SERVER["PATH_INFO"] = $_ENV["ORIG_PATH_INFO"];

	if (isset($_ENV["PATH_INFO"]) && $_ENV["PATH_INFO"])
		$_SERVER["PATH_INFO"] = $_ENV["PATH_INFO"];

	if (!isset($_SERVER["SCRIPT_FILENAME"]) && isset($_ENV["PATH_TRANSLATED"]))
		$_SERVER["SCRIPT_FILENAME"] = $_ENV["PATH_TRANSLATED"];

	if (isset($_SERVER["HTTP_REFERER"])) {
		$_SERVER["HTTP_REFERER"] = explode(", ", $_SERVER["HTTP_REFERER"]);
		$_SERVER["HTTP_REFERER"] = array_pop($_SERVER["HTTP_REFERER"]);
	}

	$_SERVER["SCRIPT_NAME"] = urldecode(preg_replace("/\?.*$/", "", $_SERVER["REQUEST_URI"]));

	if (isset($_SERVER["PATH_INFO"]) && $_SERVER["PATH_INFO"])
		$_SERVER["SCRIPT_NAME"] = substr($_SERVER["SCRIPT_NAME"], 0, -1*strlen($_SERVER["PATH_INFO"]));

	// --------------- begin blacklist ---------------
	define("GEKKO_BLACKLIST", GEKKO_TEMP_DIR."ip.blacklist");

	$is_banned = false;

	$ban_reason = "You've been a bad, bad girl! I though I told you to not bother me!";

	// this array will be used in lib/auth.inc.php, please don't mess with it!
	$banlist = array();

	if (file_exists(GEKKO_BLACKLIST) && (($fh = fopen(GEKKO_BLACKLIST, "rw")) !== false)) {

		$banlist = unserialize(fread($fh, filesize(GEKKO_BLACKLIST)));

		for ($i = 0; isset($banlist[$i]); $i++) {

			$entry = &$banlist[$i];

			$ban_time = ($entry['ban_time'] ? $entry['ban_time'] : GEKKO_BAN_TIME);
			if ($entry['ban_start']+$ban_time < time()) {
				unset($entry);
			} else {
				if ($entry['ip_addr'] == IP_ADDR) {
					$is_banned = true;
					if ($entry['ban_reason'])
						$ban_reason = $entry['ban_reason'];
					$ban_seconds_left = ($entry['ban_start']+$ban_time)-time();
				}
			}
		}

		fwrite($fh, serialize($banlist));

		fclose($fh);
	}

	if ($is_banned) {
		header("HTTP/1.1 403 Forbidden");
		echo "
		<h1>Forbidden</h1>
		{$ban_reason} ({$ban_seconds_left} seconds left)
		<hr /><address>This site is powered by <a href=\"http://www.gekkoware.org\">Gekko</a></address>
		";
		die;
	}
	// --------------- end blacklist ---------------

	require(GEKKO_LIB_DIR."core.lib.php");

	gekko_session_start();

	if (!defined("IN-INSTALL")) {

		db_init();

		// getting language
		if (isset($_GET["lang"])) {
			$lang = $_GET["lang"];
		} else {
			$lang = (($user_lang = get_userconf('lang')) != false) ? $user_lang : conf::getkey("core", "site.lang");
		}

		$lang = preg_replace("/[^a-zA-Z\-]/", '', $lang);

		if ($lang && isset($_GET["lang"]))
			set_userconf("lang", $lang);

		$Lang = new Lang($lang);

		// creating an access table
		$AccessTable = new AccessControl();

		// loading core configuration variables
		$Conf = new Conf();
		$Conf->Load("core");

		// note that we're requesting both 'core' and 'lang' modules, when 'lang' module
		// is specified, it's key and value will overwrite the default definition.
		$q = $db->query("SELECT module, keyname, keyvalue
		FROM ".db_table("conf")." WHERE module = 'core' OR module = 'lang'", __FILE__, __LINE__);

		// you can access core configuration variables by using _l('C_KEYNAME')
		while ($row = $db->fetchRow($q))
			_l((($row["module"] == 'core') ? 'C' : 'L').'_'.strtoupper($row["keyname"]), $row["keyvalue"]);

		// style
		if (isset($_GET["style"])) {
			$style = $_GET["style"];
		} else {
			$style = (($user_style = get_userconf('style')) != false) ? $user_style : $L["C_SITE.STYLESHEET"];
		}

		$style = preg_replace("/[^a-zA-Z0-9\-_]/", '', $style);

		// validating stylesheet
		if (!file_exists(GEKKO_TEMPLATE_DIR."{$L['C_SITE.TEMPLATE']}/{$L['C_SITE.STYLESHEET']}.css"))
			$style = "default";

		_l("C_SITE.STYLESHEET", $style);
	} else {
		// language for installations
		define("GEKKO_LANG", (isset($_SESSION["sess_lang"]) && (file_exists("install/lang/{$_SESSION["sess_lang"]}/main.php"))) ? $_SESSION["sess_lang"] : GEKKO_DEFAULT_LANG);
	}

	_l("C_SITE.LANG", GEKKO_LANG);

	_l("C_SITE.REQUEST_PATH", preg_replace("/\?(.*)/", '', $_SERVER["REQUEST_URI"]));

	_l("C_SITE_ROOT", rtrim(dirname($_SERVER["SCRIPT_NAME"]), "/"));

	$script_path = dirname($_SERVER["SCRIPT_NAME"]);
	if (isset($_SERVER["SCRIPT_URI"])) {
		preg_match("/[a-z]*:\/\/[^\/]*/", $_SERVER["SCRIPT_URI"], $match);
		$gekko_uri = "{$match[0]}$script_path";
	} else {
		$gekko_uri = "http://{$_SERVER["HTTP_HOST"]}{$script_path}";
	}

	if (ereg("/modules/", $gekko_uri)) {
		// guessing invoked module when a form is submitted
		$match = Array();
		preg_match("/(.*)\/modules\/([^\/]*)/", $gekko_uri, $match);
		$gekko_uri = $match[1];
		$invoked_mod = $match[2];
		define("ACTION_MODULE", $invoked_mod);
		// autoloading language for the invoked module
		$Lang->loadFromModule($invoked_mod);
	}

	$gekko_uri = rtrim($gekko_uri, "/")."/";

	_l("C_SITE.URL", $gekko_uri);

	preg_match("/^.*index\.php([^?]*)\??.*?$/", $_SERVER["REQUEST_URI"], $path);

	_l("C_SITE.REL_URL", isset($path[1]) ? rtrim(str_repeat("../", substr_count($path[1], "/")), "/") : "");

	if ($L["C_SITE.REL_URL"])
		$L["C_SITE.REL_URL"] .= '/';

	if (!isset($L["C_SITE.ICONTHEME"]) || !file_exists(GEKKO_SOURCE_DIR."media/icons/{$L["C_SITE.ICONTHEME"]}"))
		$L["C_SITE.ICONTHEME"] = "default";

	if (!isset($L["C_SITE.TEMPLATE"]))
		$L["C_SITE.TEMPLATE"] = "default";

	_l("C_TEMPLATE_DIR", "{$L["C_SITE.URL"]}templates/{$L["C_SITE.TEMPLATE"]}/");
	_l("C_ICON_DIR", "{$L["C_SITE.URL"]}media/icons/{$L["C_SITE.ICONTHEME"]}/");

	_l("C_GEKKO_VERSION", GEKKO_VERSION);

	_l("C_IP_ADDR", IP_ADDR);

	register_shutdown_function("shutdown");
?>
