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

$myJSON['message'] = "کمبود منابع";
$myJSON['mmm'] = array();

if(isset($_POST["url"]) && isset($_POST["an1d"]) 
	&& isset($_POST["nekot"]) && isset($_POST["banner"]) )
{
	$target_dir = "";
	$target_file_name = $target_dir . basename($_FILES["image"]["name"]);
	$imageFileType = pathinfo($target_file_name,PATHINFO_EXTENSION);

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

	$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 = "UPDATE tbl_company SET banner = '".$img."' ,bennerurl ='".$url."' WHERE id ='1'";
						$insres=@mysqli_query($connect,$inser_q);
						if(!$insres)
							$myJSON['message']= "404".@mysqli_error($connect);
						else{
							$myJSON['message']= "200" ;
							unlink($banner);
						}
					}
				}
			}
		}
	}
	 else
		 $myJSON['message']="402";
}
    echo json_encode($myJSON);
?>
