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

$myJSON['message'] = "0";
$myJSON['mmm'] = array();
if(isset($_POST["an1d"]) && isset($_POST["nekot"])&& isset($_POST["cusid"]))
{
	$target_dir = "cusimages/";
	$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'] = "خطای نوع داده";
		 echo  json_encode($myJSON);
		 return;
	}

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


	$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']= "خطای پایگاه داده" ;

        }
		else{
			$countoft = 0;
            while($rowt = @mysqli_fetch_assoc($tokres)){
				array_push($myJSON['mmm'], $rowt);
                $countoft++;
			}
			if($countoft == 0){
				$myJSON['message']= "خطای اعتبارسنجی" ;
			}
			else{
						$iamgename =  md5($cusid);
						$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_customer." SET img = '".$img."' WHERE id = '".$cusid."'";
								$updateres=@mysqli_query($connect,$update_q);
								if(!$updateres)
				            	$myJSON['message']= "خطای ثبت اطلاعات";
								else {
									$myJSON['message']="200";
								}
						}

					}
				}
			}


	 else
		 $myJSON['message']="خطای اتصال به دیتابیس";
}
    echo json_encode($myJSON);

?>
