<?php
/*
	DevSaver web framework
	copyright (c) 2013-2016 DevSaver
		web:  www.devsaver.com
		mail: support@devsaver.com
*/


class CHTTP {


	/**
	* description
	*
	* @var type
	*
	* @access type
	*/
	var $result;
	
	function __construct() {
		$this->connection = curl_init();
	}

	public static function newInstance() {
		return new self();
	}
	
	
	function __destruct() {
		curl_close(
			$this->connection
		);
	}

	public function Get($url , $params = array()  , $ret = "object" , $timeout = 0) {

		$this->setopt(CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setopt(CURLOPT_URL, $url . $this->prepareVars($params)); 
		$this->setOpt(CURLOPT_CONNECTTIMEOUT , $timeout);

		if ($this->result  = $this->exec()) {
			switch ($ret) {
				case "object":
					return $this;
				break;
				
				default:
					return $this->result;
				break;
			}
		} 

		return $this;
	
		
	}

	public function Post($url , $params = array() , $ret = "object" , $timeout =0 ) {

		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setOpt(CURLOPT_POSTFIELDS,$params); 
		$this->setopt(CURLOPT_URL, $url . $this->prepareVars($params)); 
		$this->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
		$this->setOpt(CURLOPT_HEADER, 0);
		$this->setOpt(CURLOPT_CONNECTTIMEOUT , $timeout);

		$this->result = $this->exec();

		switch ($ret) {
			case "object":
				return $this;
			break;
			
			default:
				return $this->result;
			break;
		}
	}

	public function Head($url , $params = array()) {
	}

	public function Opt($url , $params = array()) {
	}

	public function Del($url , $params = array()) {
	}

	public function Put($url , $params = array()) {
	}


	public function Json($type = 1) {
		return json_decode($this->result , $type);
	}

	public function Unserialize(){
		return unserialize($this->result);
	}

	public function Raw(){
		return $this->result;
	}

	public function XML() {
		$xml = new CConfig($this->result , "string");
		return $xml;
	}
	

	private function prepareVars($vars = array()) {
		if (is_array($vars) && count($vars)) {
			foreach ($vars as $key => $val) {

				$query[] = $key . "=" . urlencode($val);
			}		


			return "?" . implode("&" , $query);			
		}
		
	}
	
	private function setOpt($option , $value) {
		return curl_setopt(
			$this->connection , 
			$option , 
			$value
		);
	}

	private function exec(){
		$return  = curl_exec(
			$this->connection
		);

		return $return;
	}
	



}


?>