<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Request {

	public $CI;

	public function __construct() {
		$this->CI = & get_instance();
	}

	public function get_type() {
		// check request from ajax
		if ($this->CI->input->is_ajax_request()){
			return 'AJAX';
		}

		$this->CI->load->library('user_agent');
		// check request from browser
		if ($this->CI->agent->is_browser()) {
			return 'BROWSER';
		}

		/*if (preg_match("|".preg_quote('NuSOAP').".*?([0-9\.]+)|i", $this->CI->agent, $match))
		{
			return 'NUSOAP';
		}*/

		return '';
	}

	
	public function get_ip() {
		return $this->CI->input->ip_address();
	}

	public function get_method() {
		return $this->CI->input->server('REQUEST_METHOD');
	}

	public function isAjax(){
		return $this->CI->input->is_ajax_request();
	}

	public function isAPI(){
		return $this->CI->input->is_ajax_request();
	}

	public function isCli(){
		return $this->CI->input->is_cli_request();
	}

	public function is_post_method() {
		return ($this->get_method() == 'POST' || !empty($_POST)) ? true : false;
	}

	public function post()
	{
		$ret = array();
		if (!$this->is_post_method())
		{
			$ret = (array)$this->CI->input->get();
			return $ret;
		}

		$contentType = $this->CI->input->server('CONTENT_TYPE');
		if ($contentType && stripos($contentType, 'application/json') === 0)
		{
			$ret = json_decode(file_get_contents("php://input"), true);
		}

		if (empty($ret))
		{
			$ret = (array)$this->CI->input->post();
		}

		return $ret;
	}

	public function get_data() {
		if ($this->get_method() == 'POST' || !empty($_POST)) {
			return $this->CI->input->post();
		}

/*
		if ($this->get_method() == 'PUT') {
			return file_get_contents("php://input");
		}
*/

		return $this->CI->input->get();
	}
}
