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

$myJSON['message'] = "0";
$myJSON['mmm'] = array();
if(isset($_POST["an1d"]) && isset($_POST["nekot"]) && isset($_POST["pId"]))
{
	$target_dir = "proimages/";
	$target_file_name = $target_dir . basename($_FILES["image"]["name"]);
	$imageFileType = pathinfo($target_file_name,PATHINFO_EXTENSION);

	$user_id =str_replace('"',"", $_POST["an1d"]);
	$toke =str_replace('"',"", $_POST["nekot"]);
	$pId =str_replace('"',"", $_POST["pId"]);
	$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 utf8mb4;");
        $query_token = "SELECT id FROM ".$tbl_sessions." WHERE token = '".$toke."' AND uid ='".$user_id."' AND type ='A' 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($lastid . getdate() . time());
				$target_file_name = $target_dir . $iamgename;

				if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file_name)){
						$img = $target_file_name;
						$update_q = "INSERT INTO  tbl_pAlbums (pId,img) VALUES ('".$pId."','".$img."')";
						$updateres=@mysqli_query($connect,$update_q);
						if(!$updateres)
						$myJSON['message']= "خطا در آپلود عکس...!";
						else
						$myJSON['message']= "200";
				}
			}
		}
	}
	 else
		 $myJSON['message']="خطا در برقرای ارتباط با پایگاه داده...!";
}
    echo json_encode($myJSON);

?>
