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

$myJSON['message'] = "کمبود منابع";
$myJSON['mmm'] = array();
$name="123";
if(isset($_POST["bname"]) && isset($_POST["an1d"]) && isset($_POST["nekot"]))
{
	$target_dir = "bnrimage/";
	$target_file_name = $target_dir . basename($_FILES["image"]["name"]);
	$imageFileType = pathinfo($target_file_name,PATHINFO_EXTENSION);
	/*if(basename($_FILES["image"]["name"]) != "")
	if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
		&& $imageFileType != "gif" ){
		$myJSON['message'] = "410";
		 echo  json_encode($myJSON);
		 return;
	}*/

	$name = str_replace('"',"", $_POST["bname"]);
	$user_id =str_replace('"',"", $_POST["an1d"]);
	$toke = str_replace('"',"", $_POST["nekot"]);
	
	$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']= "400" ;

        }
		else{
			$countoft = 0;
            while($rowt = @mysqli_fetch_assoc($tokres)){
				array_push($myJSON['mmm'], $rowt);
                $countoft++;
			}
			if($countoft == 0){
				$myJSON['message']= "405" ;
			}
			else{
				
				if($img!=""){
					$iamgename =  getdate() . time();
					$target_file_name = $target_dir . $iamgename;

					if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file_name)){
						$img = $target_file_name;
						$inser_q = "INSERT INTO tbl_banners (bnrtitle,bnrimg)
						VALUES ('".$name."','".$img."')";
						$insres=@mysqli_query($connect,$inser_q);
						if(!$insres)
							$myJSON['message']= "404" ;
						else{
							$myJSON['message']= "200" ;
						}
					}

				}

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

?>
