1<?php
2
3namespace GO\Base;
4
5class Request {
6
7	public $post;
8
9	public $get;
10
11	/**
12	 * The request headers
13	 *
14	 * @var string[]
15	 */
16	private $_headers;
17
18	public function __construct() {
19		if($this->isJson()){
20			$this->post = json_decode(file_get_contents('php://input'), true);
21
22			// Check if the post is filled with an array. Otherwise make it an empty array.
23			if(!is_array($this->post))
24				$this->post = array();
25
26		}else
27		{
28			$this->post=$_POST;
29		}
30
31		$this->get=$_GET;
32	}
33
34	public function getContentType() {
35		if (PHP_SAPI == 'cli') {
36			return 'cli';
37		} else {
38			return isset($_SERVER["CONTENT_TYPE"]) ? $_SERVER["CONTENT_TYPE"] : '';
39		}
40	}
41
42	public function isJson() {
43		return isset($_SERVER["CONTENT_TYPE"]) && strpos($_SERVER["CONTENT_TYPE"], 'application/json') !== false;
44	}
45
46	/**
47	 * Check if this request SSL secured
48	 *
49	 * @return boolean
50	 */
51	public function isHttps() {
52		if(!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') {
53			return true;
54		}
55
56		if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
57			return true;
58		}
59
60		return false;
61	}
62
63	/**
64	 * Check if this request is an XmlHttpRequest
65	 *
66	 * @return boolean
67	 */
68	public function isAjax(){
69		return Util\Http::isAjaxRequest();
70	}
71
72	/**
73	 * Return true if this is a HTTP post
74	 *
75	 * @return boolean
76	 */
77	public function isPost(){
78		return $_SERVER['REQUEST_METHOD']==='POST';
79	}
80
81	/**
82	 * Get the request method
83	 *
84	 * @param string PUT, POST, DELETE, GET, PATCH, HEAD
85	 */
86	public function getMethod() {
87		return strtoupper($_SERVER['REQUEST_METHOD']);
88	}
89
90	/**
91	 * Get the route for the router
92	 *
93	 * This is the path between index.php and the query parameters with trailing and leading slashes trimmed.
94	 *
95	 * In this example:
96	 *
97	 * /index.php/some/route?queryParam=value
98	 *
99	 * The route would be "some/route"
100	 *
101	 * @param string|null
102	 */
103	public function getRoute() {
104		return isset($_SERVER['PATH_INFO']) ? ltrim($_SERVER['PATH_INFO'], '/') : null;
105	}
106
107		/**
108	 * Get the request headers as a key value array. The header names are in lower case.
109	 *
110	 * Example:
111	 *
112	 * <code>
113	 * [
114	 * 'accept' => 'application/json',
115	 * 'accept-aanguage' => 'en-us'
116	 * ]
117	 * </code>
118	 *
119	 * @return array
120	 */
121	public function getHeaders() {
122
123		if(!function_exists('apache_request_headers'))
124		{
125			return [];
126		}
127
128		if (!isset($this->headers)) {
129			$this->_headers = array_change_key_case(apache_request_headers(),CASE_LOWER);
130		}
131		return $this->_headers;
132	}
133
134	/**
135	 * Get request header value
136	 *
137	 * @param string $name
138	 * @param string
139	 */
140	public function getHeader($name) {
141		$name = strtolower($name);
142		$headers = $this->getHeaders();
143		return isset($headers[$name]) ? $headers[$name] : null;
144	}
145
146}
147