<?php 
class curl
{		
	###########################################################
	private $Headers = array();
	private $Methode = 'GET';
	private $Succes = false;
	private $Break = false;
	
	function __construct()
	{
		
	}
	function addHeader($header,$value)
	{
		$value = trim($value);
		$head = "$header:$value";
		$this->Headers[]=$head;
	}
	function setUrl($url)
	{
		$this->Url = $url;
	}
	function setMethode($meth)
	{
		$this->Methode = strtoupper($meth);
	}
	function setData($data)
	{
		$data = iso_encode($data);
		if(is_object($data) or is_array($data)) $data = json_encode($data);
		$this->Data = $data;
	}
	function setDataUrl($data)
	{
		if(!is_array($data)) $this->Data = "";
		foreach($data as $k=>$v)
		{
			@$vars[]= $k."=".urlencode($v);
		}
		$this->Data = implode('&',$vars);
		
		
		if($this->Methode=='POST')
		{
			$this->addHeader("Content-Type","application/x-www-form-urlencoded");
		}
		elseif($this->Methode=='GET')
		{
			$this->Url.='?'.$this->Data;
			$this->Data = "";
		}
		
		//
	}
	function setDataJSON($data)
	{
		$this->setData($data);
	}
	function make()
	{
		$this->Succes = false;
		if($this->Methode=='PUT')
		{
			$this->makeput();
			return 1;
		}
		if(empty($this->Url)) $this->setError("Vous devez preciser l'Url");	
		$this->CH = curl_init();
		curl_setopt($this->CH,CURLOPT_URL, $this->Url);
		if(!empty($this->Headers)) curl_setopt($this->CH, CURLOPT_HTTPHEADER, $this->Headers);
		curl_setopt($this->CH, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($this->CH, CURLOPT_RETURNTRANSFER, 1);
		
		curl_setopt($this->CH, CURLOPT_SSL_VERIFYPEER, FALSE);
		if($this->Methode=='POST')
		{
			curl_setopt($this->CH, CURLOPT_POST, TRUE);
			curl_setopt($this->CH, CURLOPT_POSTFIELDS, $this->Data);
		}
		
		if($this->Methode=='PUT')
		{
			curl_setopt($this->CH, CURLOPT_POST, TRUE);
			curl_setopt($this->CH, CURLOPT_POSTFIELDS, $this->Data);
		}

		$result = curl_exec($this->CH);
		
		$this->Result = $result;
		if (!curl_errno($this->CH)) 
		{
			$this->ErrorNumber = 
			$this->ErroNumber = curl_getinfo($this->CH, CURLINFO_HTTP_CODE);
		}
		$reponse = json_decode($result, true);
	
		if(is_array($reponse['error']))
		{
			$this->Reponse['error'] = $reponse['error'] ;
		}
		elseif(is_array($reponse['data']))
		{
			$this->Reponse['data'] = $reponse['data'] ;
		}
		 
		elseif($this->ErrorNumber>=300)
		{
			$this->Reponse['error'] = $reponse;
		}
		elseif($this->ErrorNumber<300)
		{
			$this->Reponse['data'] = $reponse;
		}
		foreach($reponse as $k=>$v)
		{
			if(empty($this->Reponse[$k])) $this->Reponse[$k] = $v;
		}
		$this->Response = json_decode(json_encode($this->Reponse,true));

		
		
		if (!curl_errno($this->CH)) 
		{
			$this->ErrorNumber = 
			$this->ErroNumber = curl_getinfo($this->CH, CURLINFO_HTTP_CODE);
		}

		curl_close($this->CH);
		
		if($this->ErroNumber==403)
		{
			$this->Result = strip_tags($this->Result);
		}
		$query = "INSERT INTO `curls_logs`( `url`, `data`, `response`) VALUES ('".addslashes($this->Url)."','".addslashes($this->Data)."','".addslashes($this->Result)."')";
		
		global $log_sgdb;
		
		$log_sgdb->query($query);
			
		if(empty($this->ErrorNumber)) $this->ErrorNumber = 404;
	}
	function makeput()
	{
		$this->CH = curl_init();
		global $sgbd;
		curl_setopt_array($this->CH, array(
		  CURLOPT_URL => $this->Url,
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 0,
		  CURLOPT_FOLLOWLOCATION => true,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "PUT",
		  
		));
		if(!empty($this->Headers)) curl_setopt($this->CH, CURLOPT_HTTPHEADER, $this->Headers);

		$result = curl_exec($this->CH);
		
		$this->Result = $result;
		$query = "";
		if (!curl_errno($this->CH)) 
		{
		  $this->ErroNumber = curl_getinfo($this->CH, CURLINFO_HTTP_CODE);
		}

		curl_close($this->CH);
		if(!$response = json_decode($result)) $response = json_decode(json_encode(array("message"  => strval($result),"error"  => 1)));
		
		if(!empty($response->message)) $this->setError("CURL SAY : ".$response->message."");
		else 
		{
			$response->error = 0;
			$this->Succes = true;
		}
		$this->Response = $response;
		$this->Reponse = json_decode(json_encode($response),true);
	}
	function setError($err)
	{
		$this->Return['error']   = 1;
		$this->Return['message'] = mb_encode($err);
		
	}
}