<?php
/*
 * KMLeague 2.1.1
 * Copyright (c) 2007, 2008, 2009 Krystian Zbikowski (kmleague.net)
 * $Date: 2009-01-25 01:03:00 -0500 (Sun, 25 Jan 2008) $
 */

#error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ERROR);//TODO this will be in final release

#start session if visitor is not webspider, help for SEO
if(!preg_match('/(google|msn|yahoo|onet|wp|szukacz|interia|WebCrawler|WebSpider)/i', $_SERVER['HTTP_USER_AGENT'])) session_start();

#used to define weather we should load full language file or just for frontend
define('KML_SITE', 'frontend');
if(!isset($_REQUEST['mod'])) $_REQUEST['mod'] = 'start'; else $_REQUEST['mod'] = strtolower($_REQUEST['mod']);
if(!isset($_REQUEST['view'])) $_REQUEST['view'] = ''; else $_REQUEST['view'] = strtolower($_REQUEST['view']);
if(!ereg('^([0-9a-zA-Z_]+)$', $_REQUEST['mod'])) die('Not apropriate name of module <strong>'.$_REQUEST['mod'].'</strong>.');
if(!ereg('^([0-9a-zA-Z_]+)$', $_REQUEST['view']) && $_REQUEST['view']) die('Not apropriate name of view <strong>'.$_REQUEST['view'].'</strong>.');
define('KML_PATH', dirname(__FILE__).'/');

#load config file
#if config file doesnt exist then redirect to install script
if(!file_exists(KML_PATH.'files/config.php')){
	header('Location: install/');
	exit;
}elseif(file_exists(KML_PATH.'install')){
	echo 'Remove installation directory from server first (install/).';
	exit;
}
require_once(KML_PATH.'files/config.php');

#define session vars
define('KML_SID', 'kml_login'.KML_SESSION_HASH);
define('KML_SLOGIN', 'kml_name'.KML_SESSION_HASH);
define('KML_SGRANT', 'kml_permission'.KML_SESSION_HASH);
define('KML_STEAM', 'kml_team'.KML_SESSION_HASH);
define('KML_SCONF', 'kml_config'.KML_SESSION_HASH);
define('KML_SSEC', 'kml_secure'.KML_SESSION_HASH);

#cleanup variables
$ints = array('l', 's', 'no', 'wrap_kml', 'blank', 'ajax');
foreach($ints as $int){
	if(!isset($_REQUEST[$int])) $_REQUEST[$int] = 0;
	else $_REQUEST[$int] = (int)$_REQUEST[$int];
}
if(!isset($_SESSION[KML_SID])) $_SESSION[KML_SID] = 0;
if(!isset($_SESSION[KML_SGRANT])) $_SESSION[KML_SGRANT] = 0;
if(!isset($_SESSION[KML_SCONF]['id_team'])) $_SESSION[KML_SCONF]['id_team'] = 0;
if(!isset($_REQUEST['act'])) $_REQUEST['act'] = '';

#load error handler if setup turn it on
if(KML_ERR_MONITOR==1){
	require_once(KML_PATH.'lib/class/error_handler.php');
	$handler = new error_handler('127.0.0.1',0,0,NULL,'files/temp/errlog.txt');
	set_error_handler(array(&$handler, "handler"));
}

#load ADODB
require_once(KML_PATH.'lib/adodblite/adodb.inc.php');
#initiate DB connection
$db = ADONewConnection(KML_DB_TYPE);
if(!$db->Connect(KML_DB_HOST, KML_DB_USER, KML_DB_PASSW, KML_DB_NAME)){
	die('Can\'t connect with '.KML_DB_TYPE.' database. Please recheck settings in files/config.php file.');
};
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

#load DB performance tool if setup turn it on (set it in files/config.php)
if(KML_DB_MONITOR==1){
	$db->LogSQL();
	$perf =& NewPerfMonitor($db);
	if((isset($_REQUEST['do']) || isset($_REQUEST['sql'])) && $_SESSION[KML_SGRANT]>2){
		$perf->UI($pollsecs=5);
		exit;
	}
}

#load basic functions
require_once(KML_PATH.'lib/core/main.php');
if(KML_CHECK_PERFORMANCE==1) $time_start = getmicrotime();
require_once(KML_PATH.'lib/framework/lib/framework_inc.php');

#load league
$leagues = load_league();
#load season
load_season();
#load configuration for chosen league
$modCFG = load_cfg();

#load template engine
require_once(KML_PATH.'lib/class/template.php');
$tpl = & new Template(KML_PATH.KML_LEAGUE_SKIN);

#custom fields library
require_once(KML_PATH.'lib/core/cf.php');
#load fields marked as important used for db queries
customLoadImportant();

#set system links variables
define('KML_LINK_WEB', 'index.php?l='.KML_LEAGUE.'&amp;s='.KML_SEASON);
define('KML_LINK_ADMIN', 'admin.php?l='.KML_LEAGUE.'&amp;s='.KML_SEASON);
define('KML_LINK_FORM', '<input type="hidden" name="l" value="'.KML_LEAGUE.'"/><input type="hidden" name="s" value="'.KML_SEASON.'"/>');

$tpl->set('now', time());
$tpl->set('link_form', KML_LINK_FORM);

#load language
load_language();
require_once(KML_PATH.'lib/lang/'.KML_LANG.'.php');
require_once(KML_PATH.'lib/lang/'.KML_LANG_COUNTRIES.'_countries.php');

#login logout
require_once(KML_PATH.'lib/core/lilo.php');

#load libraries
load_libraries();

#webmaster hacks
if($_SESSION[KML_SGRANT]['main']>1) $_SESSION[KML_SGRANT][KML_LEAGUE] = 3;
#change team
if(isset($_REQUEST['choose_team']) && $_SESSION[KML_SGRANT]['main']>0){
	if(kml_leader_team_access($_REQUEST['choose_team'])==1){
		$_SESSION[KML_SCONF]['id_team'] = $_REQUEST['choose_team'];
		#redirect to team management
		header('Location: '.str_replace('amp;', '', KML_LINK_WEB).'&mod=leader&view=manage');
	}
}

//change requested information if aliases are defined
require(KML_PATH.'alias.php');

define('KML_SKIN_TPL', KML_PATH.KML_LEAGUE_SKIN.'frontend/');
define('KML_SKIN_IMG', KML_LEAGUE_SKIN.'im/');
define('KML_MODULE', $_REQUEST['mod']);

#assign popular variables
$tpl->set('yesNo', array('Y'=>msg('yes'), 'N'=>msg('no')));

#load default module
load_module('layout');

#load chosen module except start page
if($_REQUEST['mod']!='start'){
	load_module(KML_MODULE);
}else{
	load_module('article');
}

#if exists, load custom user function
$loadCustomFunc = 'kml_user_'.$_REQUEST['mod'].'_'.$_REQUEST['view'];
if(function_exists($loadCustomFunc)) $moduleOut = $loadCustomFunc();
else{#if exists load core function
	$loadFunc = 'kml_core_'.$_REQUEST['mod'].'_'.$_REQUEST['view'];
	if(function_exists($loadFunc)) $moduleOut = $loadFunc();
	else $moduleOut = $loadFunc;#or just show name for functions which doesnt exists
}

#module output
$tpl->set('module_output', $moduleOut);

#return just result if its wrapper
if($_REQUEST['ajax']==1 || $_REQUEST['wrap_kml']==1){
	echo $moduleOut;
	exit;
}elseif($_REQUEST['blank']==1){
	echo $tpl->fetch('index.tpl.php');
	exit;
}

#display template
echo $tpl->fetch('index.tpl.php');
if(KML_CHECK_PERFORMANCE==1){
	$time_end = getmicrotime();
	$l = fopen(KML_PATH.'files/temp/loadtime.txt', "a+");
	$time = round(($time_end-$time_start),5);
	fwrite($l, $time."\t".$_SERVER['REQUEST_URI']."\r\n");
	fclose($l);
}
?>
