<?php

	error_reporting(0);
//	ini_set('session.use_trans_sid', '1');
	
	session_start();
	
	if(strstr($HTTP_REFERER, 'hostuju.cz') || strstr($HTTP_REFERER, '1111mb'))
	{
		error_log(date('Y-m-d H:i:s').' '.$HTTP_REFERER."\n", 3, 'lib/log/blocked.log');
		die('
		
		<html>
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
		<link rel="stylesheet" href="style.css" />
		<link rel="stylesheet" href="style_alt.css" />
		<link rel="icon" type="image/png" href="icon.png" />
		<link rel="shortcut icon" type="image/png" href="icon.png" />
		<meta http-equiv="REFRESH" content="5;url=http://4mobile.peterpech.com" />
		<title>4mobile.sk - Pozadia, melódie, hry, smsky a videá na Váš mobil zadarmo!</title>
		</head>
		<body style="color: white">Boli ste k nam presmerovaní z podvrhnutej verzie webu 4mobile.peterpech.com. Ak ste sa z nej pokúšali prihlásiť, Vaše heslo bolo pravdepodobne ukradnuté, čo najskôr si ho zmeňte na oficiálnej stránke <a href="http://4mobile.peterpech.com">4mobile.peterpech.com</a>!</body>
		</html>
		
		');
	}
	
	if($_SESSION['my_id']==1 || $_SESSION['my_id']==3) error_reporting(E_ALL);
	
	require_once('conf/definitions.php');
	require_once('conf/modules.php');
	require_once('conf/objs.php');
	require_once('conf/functions.php');
	
	$start_time = fnExec_time();

	$dba = new db;
	
	//banned users
	if(isset($_SESSION['my_id']))
	{
		if(!isset($_SESSION['banned']))
		{
			$query = "SELECT * FROM mobile_banned_users";
			$result = $dba->GetResult($query);
			$res = array();
			while($row = mysql_fetch_assoc($result))
			{
				$res[] = $row['user_ID'];
			}
			$_SESSION['banned'] = $res;
		}
		
		if(in_array($_SESSION['my_id'], $_SESSION['banned']))
		{
			die('Za to ze si robil/a hovadiny si dostal/a ban. Zruseny bude podla uvazenia..');
		}
	}
	
	$module = (array_key_exists('module', $_GET) && in_array($_GET['module'], $modules_body)) ? $_GET['module'] : 'index';
	
	if(isset($_GET['domains']))
	{
		$module = 'vyhladavanie';
	}
	
	$ref = (array_key_exists('ref', $_GET) && $_GET['ref']<>'') ? $_GET['ref'] : 0;
	
	// ********** STATISTIKA ****************
	 // Zapis pre moduly
	 if(array_key_exists($module, $stats_modules) && !session_is_registered('mobile_stats_'.$module))
	 {
	 	$dba->Increment('mobile_counter', 'status', $stats_modules[$module], 1);
	 	session_register('mobile_stats_'.$module);
	 }
	 
	 // Zapis dnesnych navstev
	 if($module == 'index' && !session_is_registered('mobile_index_dnes') && !isset($_COOKIE['mobile_index']))
	 {
	 	$dat = date("j.m.Y");
	 	$query = "SELECT ID, datum FROM mobile_navstev_dnes ORDER BY ID DESC limit 0, 1";
	 	$result = $dba->GetResult($query);
	 	$row = mysql_fetch_array($result);
		$iid = $row['ID'];
		if($dat==$row["datum"])
		{
			$dba->Increment('mobile_navstev_dnes', 'navstev', $iid, 1);
		}
		else
		{
			$query = "INSERT INTO mobile_navstev_dnes (datum, navstev) VALUES ('".$dat."','1')"; 
			$dba->ExecuteQuery($query);
		}
		session_register('mobile_index_dnes');
		setcookie('mobile_index', 'x', time() + 1800);
		// zapis reklamneho referera
		if($ref!=0)
		{
			$check_referer = $dba->Increment('mobile_referer', 'visits', $ref, 1);
		}
	 }
	 
	 //zapis referera
	 if(!session_is_registered('referer_wrote') && isset($HTTP_REFERER) && $HTTP_REFERER != '')
	 {
	 	session_register('referer_wrote');
	 	$dat = date("j.m.Y");
		$logg = $HTTP_REFERER.' # '.$_SERVER['REMOTE_ADDR'];
		$query = "INSERT INTO mobile_visits_logs (time, log) VALUES ('$dat', '$logg')";
		$dba->ExecuteQuery($query);
	 }
	 
	 $max_id_navstev = $dba->Max_ID('mobile_navstev_dnes');
	 $dba->Increment('mobile_navstev_dnes', 'stranok', $max_id_navstev, 1);

	// **************************************

// zaznamenanie poslednej aktivity prihlaseneho uzivatela
	$viewtime = 300;
	$now = time()-1000000000;
	
	if(session_is_registered('mobile_logged_in') && session_is_registered('my_id'))
	{
		
//		$ip1 = $dba->GetOne2('mobile_logged_users', 'ip', 'user_id', $_SESSION['my_id']);
//		if($ip1 != $_SERVER['REMOTE_ADDR'] && $ip1 != '')
//		{
//			$_SESSION = array();
//			$_SESSION['error1'] = 'Toto konto sa práve používa z inej IP adresy!';
//			$module = 'index';
//		}
//		else 
		{
			$query = "DELETE FROM mobile_logged_users WHERE (last_act<".($now-$viewtime).") or user_id='".$_SESSION['my_id']."'";
			$dba->ExecuteQuery($query);
			$query = "INSERT INTO mobile_logged_users VALUES ('".$_SESSION['my_id']."', '$now', '".$_SERVER['REMOTE_ADDR']."')";
			$dba->ExecuteQuery($query);
			$query = "UPDATE mobile_users SET last_login_time='$now' WHERE ID='".$_SESSION['my_id']."' limit 1";
			$dba->ExecuteQuery($query);
		}
	}
	
// ratanie online navsetvnikov VSETKYCH
	$query = "DELETE FROM mobile_visitors WHERE (sectime<".($now-$viewtime).") or (ip='".$_SERVER['REMOTE_ADDR']."')";
	$dba->ExecuteQuery($query);
	$log = (key_exists('log', $_SESSION)) ? $_SESSION['log'] : '';
	$query = "INSERT INTO mobile_visitors VALUES ('".$_SERVER['REMOTE_ADDR']."','$now', '".$log."')";
	$dba->ExecuteQuery($query);
	$online_all = $dba->GetNums('SELECT ip from mobile_visitors');

// ratanie PRIHLASENYCH navstevnikov
	$query = "SELECT a.ID, b.user_id FROM mobile_users as a, mobile_logged_users as b WHERE a.last_login_time>".($now-$viewtime)." AND a.ID=b.user_id";
//	$query = "SELECT
	$online_logged = $dba->GetNums($query);
	
// update maximalneho poctu online ludi
	$today_online_max = $dba->GetOne('mobile_navstev_dnes', $max_id_navstev, 'online');
	if($online_all>$today_online_max)
	{
		$query = "UPDATE mobile_navstev_dnes SET online=$online_all WHERE ID=$max_id_navstev";
		$dba->ExecuteQuery($query);
	}
	
// ratanie kliknuti
	if(session_is_registered('my_id'))
	{	
	    if(!session_is_registered('kliks'))
	    {
	        $_SESSION['kliks'] = array();
	    }
        if(!session_is_registered('hls'))
        {
            $_SESSION['hls'] = 0;
        }
		if(!session_is_registered('kliknuti'))
		{	
			$_SESSION['kliknuti'] = 1;	
			$_SESSION['kliks'] = array();
		}
		else
		{
			if(!in_array($_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'], $_SESSION['kliks']))
			{
				$_SESSION['kliknuti']++;
				$_SESSION['kliks'][] = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
			}
		}
		if($_SESSION['kliknuti'] == $kliknuti_na_kredit)
		{
			fnPridajKred($_SESSION['my_id'], 1);
			$_SESSION['kliknuti'] = 0;
			$_SESSION['kliks'] = array();
		}
	}
	
// vlozenie top modulu ktory sa vykonava pred vystupom
	if(key_exists('module_top', $_GET) && $_GET['module_top']<>'')
	{
		if (in_array($_GET['module_top'], $modules_top) && (!in_array($_GET['module_top'], $protected_modules) || session_is_registered('mobile_logged_in')))
		{
			include('modules/'.$_GET['module_top'].'.php');
		}
	}
	
	$titles = array
	(
		'index' 	=> '4mobile.sk - Pozadia, melódie a hry na mobil zadarmo!',
		'pozadia' 	=> '4mobile.sk - Pozadia na Váš mobil zadarmo!',
		'melodie' 	=> '4mobile.sk - Melódie na Váš mobil zadarmo!!',
		'hry' 		=> '4mobile.sk - Hry na Váš mobil zadarmo!!',
		'temy' 		=> '4mobile.sk - Témy na Váš mobil zadarmo!!',
		'videa' 	=> '4mobile.sk - Videá na Váš mobil zadarmo!!',
	);
	
	$title = (array_key_exists($module, $titles)) ? $titles[$module] : $titles['index'];
	
?>