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

$myJSON['message'] = "0";
$myJSON['mmm'] = array();
if(isset($_POST["an1d"]) && isset($_POST["nekot"]))
{
	$target_dir = "visimages/";
	$target_file_name = $target_dir . basename($_FILES["image"]["name"]);
	$imageFileType = pathinfo($target_file_name,PATHINFO_EXTENSION);
	if($target_file_name != "")
	if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
		&& $imageFileType != "gif" ){
		$myJSON['message'] = "410";
		 echo  json_encode($myJSON);
		 return;
	}

	$toke = str_replace('"',"", $_POST["nekot"]);
	$user_id =str_replace('"',"", $_POST["an1d"]);


	$img = "";
	if(basename($_FILES["image"]["name"]) !="")
			$img = $target_file_name;

    $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 = '".$toke."' AND uid ='".$user_id."' AND type ='V' LIMIT 1";
		$tokres=@mysqli_query($connect,$query_token);
        if (!$tokres) {
            $myJSON['message']= "400" ;

        }
		else{
			$countoft = 0;
            while($rowt = @mysqli_fetch_assoc($tokres)){
				array_push($myJSON['mmm'], $rowt);
                $countoft++;
			}
			if($countoft == 0){
				$myJSON['message']= "405" ;
			}
			else{
						$iamgename =  md5($user_id);
						$target_file_name = $target_dir . $iamgename.".". basename($_FILES["image"]["type"]);
					
						if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file_name)){
								$img = $target_file_name;
								$update_q = "UPDATE ".$tbl_visitors." SET img = '".$img."' WHERE id = '".$user_id."'";
								$updateres=@mysqli_query($connect,$update_q);
								if(!$updateres)
				            	$myJSON['message']= "12";
								else {
									$myJSON['message']= $img;
								}
						}

					}
				}
			}


	 else
		 $myJSON['message']="402";
}
    echo json_encode($myJSON);

?>
