<?php

date_default_timezone_set("Asia/Tehran");

check();

function connectToDatabase(){
	require_once('db.php');
    $connection = new database();
        return $connection;

}


function check(){
    $connection = connectToDatabase();

    $id = $_REQUEST['id'];
    $username = $_REQUEST['username'];
    $AndroidID = $_REQUEST['AndroidID'];

    $Ozv = date("Y/m/d");
    $Timee = date("Y/m/d G:i");
    
    if($AndroidID!="" && $username!="" && $connection->checkParameters($id) && $connection->checkParameters($username) && strlen($id) >= 9){
    	$result1 = $connection->q("select Username,AndroidID from users where ID='$id'");
        $row=$connection->a($result1);
        if($row['Username']!=""){
            if($row['AndroidID']==""){
                $connection->q("update users set Username='$username',AndroidID='$AndroidID' where ID='$id'");
            }else {
                $connection->q("update users set Username='$username' where ID='$id'");
            }
    		print "ok_old";
    	}else {
    		$connection->q("insert into users (ID,Username,AndroidID,Hash,LastLogin,DateGetAds) VALUES('$id','$username','$AndroidID','','$Timee','$Timee')");
    		print "ok_new";
    	}
    }else {
    	echo "null";
    }
    
}


?>