<?php

	function api_post($url, $data) {
	    $config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		$response = curl_exec($ch);
		curl_close($ch);
		return $response;
	}

	function send_order_default($url, $key, $service_id, $link, $quantity, $debug = false) {
		$params = array(
			"key" => $key,
			"action" => "add",
			"service" => $service_id,
			"link" => $link,
			"quantity" => $quantity
		);
		$post = json_decode(api_post($url, $params));
		return $post;
	}
	
	function send_order_comments($url, $key, $service_id, $link, $comments) {

		$_comments = explode("\n", $comments);
		$commnets_quantity = count($_comments);

		$params = array(
			"key" => $key,
			"action" => "add",
			"service" => $service_id, 
			"link" => $link,
			"quantity" => $commnets_quantity,
			"comments" => $comments
		);
		$post = json_decode(api_post($url, $params));
		return $post;
	}

	function api_order_status($url, $key, $order_id){

		$params = array(
			"key" => $key,
			"action" => "status",
			"order" => $order_id
		);
		
		$post = json_decode(api_post($url, $params));
		return $post;

	}
	
	function update_orders($url, $key, $orders) {

		$islem = "order";
		if(count(explode(',', $orders)) > 1){
			$islem = "orders";
		}

		$params = array(
			"key" => $key,
			"action" => "status",
			$islem => $orders
		);
		$post = json_decode(api_post($url, $params), true);
	
		if(isset($post["error"])) {
			return false;
		} else {

			if($islem == "order"){
				return array(
					$orders => $post
				);
			}
			return $post;
		}
	}

	function create_refill($url, $key, $order) {
		$params = array(
			"key" => $key,
			"action" => "refill",
			"order" => $order
		);
		$post = json_decode(api_post($url, $params), true);
		if(isset($post["error"])) {
			return false;
		} else {
			return $post;
		}
	}

	function update_refill($url, $key, $order) {
		$params = array(
			"key" => $key,
			"action" => "refill_status",
			"order" => $order
		);
		$post = json_decode(api_post($url, $params), true);
		if(isset($post["error"])) {
			return false;
		} else {
			return $post;
		}
	}
	
	function services($url, $key) {
		$params = array(
			"key" => $key,
			"action" => "services"
		);
		$post = json_decode(api_post($url, $params), true);
		if(isset($post["error"])) {
			return false;
		} else {
			return $post;
		}
	}
	
	function api_balance($url, $key) {
		$params = array(
			"key" => $key,
			"action" => "balance"
		);
		$post = json_decode(api_post($url, $params), true);
		if(isset($post["error"])) {
			return null;
		} else {
			return $post;
		}
	}