<?php
require_once ("varables.php");

$myJSON['success'] = "0";
$myJSON['token']="0";
$myJSON['id']="0";
$myJSON['name']="0";
$myJSON['isAdmin']="0";
$user_id=0;
if(isset($_POST["uzermane"]) && isset($_POST["wasport"]) 
	&& isset($_POST["an1d"]) && isset($_POST["netok"]) 
	&& isset($_POST["di"]) && isset($_POST["name"])){
		
    $uzername = $_POST["uzermane"];
    $passwor = $_POST["wasport"];
	$name = $_POST["name"];
	$myJSON['name'] = $name;
	$myJSON['id'] = $_POST["di"];
	$user_id=$_POST["di"];
	$myJSON['token']=$_POST["netok"];
	
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
		@mysqli_query($connect,"SET CHARACTER SET utf8;");
		$query_token = "SELECT id FROM ".$tbl_sessions." WHERE token = '".$myJSON['token']."' AND uid ='".$myJSON['id']."' AND type ='A' LIMIT 1";
		$tokres=@mysqli_query($connect,$query_token);
        if (!$tokres) {
            $myJSON['success']= "400" ;
			$myJSON['token']="";
			$myJSON['id']="";
			$myJSON['name']="";
			$myJSON['isAdmin']="";
        }
		else{
			$countoft = 0;
            while($rowt = @mysqli_fetch_assoc($tokres))
                $countoft++;
			if($countoft == 0){
				$myJSON['success']= "405" ;
				$myJSON['token']="";
				$myJSON['id']="";
				$myJSON['name']="";
				$myJSON['isAdmin']="";
			}
			else{
				 @mysqli_query($connect,"SET CHARACTER SET utf8;");
				 $query_insert="UPDATE ".$tbl_admin." SET  name = '".$name."',username = '".$uzername."',pass = '".$passwor."' WHERE id = '".$user_id."'";        
				 $result=@mysqli_query($connect,$query_insert);
				if(!$result){
					echo @mysqli_error($connect);{
						$myJSON['success']= "404" ;
						$myJSON['token']="";
						$myJSON['id']="";
						$myJSON['name']="";
						$myJSON['isAdmin']="";
					}
				}
				else{
					$myJSON['success']= "200" ;
				}
			}
       
		}
	}
	 else{
		$myJSON['success']="402";
		$myJSON['token']="";
		$myJSON['id']="";
		$myJSON['name']="";
		$myJSON['isAdmin']="";
	}

    echo json_encode($myJSON);
}
?>