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

$myJSON['message'] = "0";
$myJSON['mmm'] = array();
$name="123";
if(isset($_POST["pnam"]) && isset($_POST["an1d"])
	&& isset($_POST["pr1se"])&& isset($_POST["pr1se1"])
	&& isset($_POST["nekot"])&& isset($_POST["stack"])
	&& isset($_POST["kate1"])&& isset($_POST["kote2"])
	&& isset($_POST["pdi"])&& isset($_POST["cmnt"])
	&& isset($_POST["mino"])&& isset($_POST["maxo"])
	&& isset($_POST["up"])&& isset($_POST["off"]))
{
	$target_dir = "proimages/";
	$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["pnam"]);

    $price =str_replace('"',"", $_POST["pr1se"]);
	$price1 =str_replace('"',"", $_POST["pr1se1"]);
	$toke = str_replace('"',"", $_POST["nekot"]);
	$stack =str_replace('"',"",  $_POST["stack"]);
	$pid =str_replace('"',"",  $_POST["pdi"]);
	$cat1 =str_replace('"',"",  $_POST["kate1"]);
	$cat2 =str_replace('"',"",  $_POST["kote2"]);
	$cm =str_replace('"',"",  $_POST["cmnt"]);
	$up =str_replace('"',"",  $_POST["up"]);
	$mino =str_replace('"',"",  $_POST["mino"]);
	$maxo =str_replace('"',"",  $_POST["maxo"]);
	$off =str_replace('"',"",  $_POST["off"]);
	
	$img = "";
	if(basename($_FILES["image"]["name"]) !="")
			$img = $target_file_name;
	$user_id =str_replace('"',"", $_POST["an1d"]);

    $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{
				
				$inser_q = "INSERT INTO ".$tbl_products." (P_name,Price,Price_1,Stack,P_Code,P_id,off,img,cat1,cat2,P_comment,mino,maxo,up)
				VALUES ('".$name."','".$price."','".$price1."','".$stack."','".$pid."','".$pid."','".$off."','".$img."','".$cat1."','".$cat2."','".$cm."','".$mino."','".$maxo."','".$up."')";
				$insres=@mysqli_query($connect,$inser_q);
				if(!$insres)
					$myJSON['message']= "404" ;
				else{
					$myJSON['message']= "200" ;
					if($img!=""){
						$lastid = @mysqli_insert_id($connect);
						$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 = "UPDATE ".$tbl_products." SET img = '".$img."' WHERE id = '".$lastid."'";
								$updateres=@mysqli_query($connect,$update_q);
								if(!$updateres)
				            	$myJSON['message']= "2000";
						}

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

?>
