<?php

	if(key_exists('login', $_POST)) $login = $_POST["login"];
	else $login = '';
	if(key_exists('login', $_POST))	$pass = $_POST["pass"];
	else $pass = '';
	$pass = md5($pass);
	$pass = md5($pass);
	$result = $dba->GetResult("SELECT * FROM mobile_users WHERE login='".$login."' limit 0, 1"); 
	$row = mysql_fetch_array($result);
	if($row["pass"]==$pass && $row["active"] != 'n')
	{
		$result = $dba->GetResult("SELECT * FROM mobile_logged_users WHERE user_id=".$row['ID']);
		$num = mysql_num_rows($result);
		if($num>0)
		{
			$row1 = mysql_fetch_array($result);
		}
		if($num==0 || $row1['ip']==$_SERVER['REMOTE_ADDR'])
		{
			$now = time()-1000000000;
			// zmazanie uzivatela ktory sa nekorektne odhlasil a pokusa sa opat prihlasit
			$query = "DELETE FROM mobile_logged_users WHERE user_id = ".$row['ID']." limit 1";
			$dba->ExecuteQuery($query);
			$query = "INSERT INTO mobile_logged_users (user_id, last_act, ip) VALUES ('".$row['ID']."', '".$now."', '".$_SERVER['REMOTE_ADDR']."')";
			$dba->ExecuteQuery($query);
			
			$query = "SELECT last_kredit_time, moj_mobil, page_pozadia, page_melody, page_hry, page_users, page_posta, page_video, page_sms, avatar, active FROM mobile_users WHERE ID=".$row["ID"]." limit 0, 1";
			$result = $dba->GetResult($query);
			$user_row = mysql_fetch_array($result);
			
			foreach($user_row as $k => $v)
			{
//				session_register("mobile_".$k);
				$_SESSION["mobile_".$k] = $v;
			}
			session_register("mobile_logged_in");
			$_SESSION["mobile_logged_in"] = true;
			session_register("log");
			$_SESSION["log"] = $row["login"];
			session_register("my_id");
			$_SESSION["my_id"]=$row["ID"];
			$_SESSION["avatar"]=$row["avatar"];
			
			if($now > ($user_row['last_kredit_time']) + 3600)
			{
				fnPridajKred($row['ID'], 1);
				$query = "UPDATE mobile_users SET last_kredit_time='$now' WHERE ID=".$row['ID']." limit 1"; 
				$dba->ExecuteQuery($query);
				$_SESSION['test_query'] = $query;
			}
			
			$dba->WriteLog($_SESSION['log'].': '.$_SERVER['REMOTE_ADDR']);
			
			$prihlaseni = $row["prihlaseni"]+1;
			$datum = date("d.m.Y H:i:s");
			$query = "UPDATE mobile_users SET prihlaseni=$prihlaseni, last_login='".$datum."', last_login_time=$now WHERE login='".$login."'";
			$dba->ExecuteQuery($query);
			
			$visits = $dba->GetOne('mobile_counter', 1, 'status');
            if(($visits % 100 == 0) && !session_is_registered('reward'))
            {
                fnPridajKred($_SESSION['my_id'], 5);
                $_SESSION['show_reward'] = true;
                session_register('reward');
            }
			
			$mod = (key_exists('incoming_module', $_POST) && in_array($_POST['incoming_module'], $modules_body)) ? $_POST['incoming_module'] : '';
			
			header("location: index.php?module=$mod");
		}
		else
		{
			$login_error_message = 'Toto konto sa práve používa z inej IP adresy!';
		}
	}
	else
	{
		if($row['active'] == 'n')
		{
			$login_error_message = 'Toto konto ešte nie je aktivované!';
		}
		else 
		{
			$login_error_message = 'Zadali ste nesprávne používateľské meno alebo heslo!';	
		}
//			fnWriteLog("login","Login incorrect! User: $login | password: $pass | IP:".$_SERVER['REMOTE_ADDR']);
	}
	

?>