Passed
Push — master ( d5c6e8...acf3eb )
by Jean-Christophe
06:08
created

URequest::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\controllers\Startup;
6
use Ubiquity\utils\base\UString;
7
8
/**
9
 * Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
10
 * Ubiquity\utils\http$URequest
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.0
15
 *
16
 */
17
class URequest {
18
19
	/**
20
	 * Affects member to member the values of the associative array $values to the members of the object $object
21
	 * Used for example to retrieve the variables posted and assign them to the members of an object
22
	 *
23
	 * @param object $object
24
	 * @param associative array $values
25
	 */
26 3
	public static function setValuesToObject($object, $values = null) {
27 3
		if (! isset ( $values ))
28
			$values = $_POST;
29 3
		foreach ( $values as $key => $value ) {
30 3
			$accessor = "set" . ucfirst ( $key );
31 3
			if (method_exists ( $object, $accessor )) {
32 3
				$object->$accessor ( $value );
33 3
				$object->_rest [$key] = $value;
34
			}
35
		}
36 3
	}
37
38
	/**
39
	 * Affects member to member the values of $_GET to the members of the object $object
40
	 * $object must have accessors to each members
41
	 *
42
	 * @param object $object
43
	 */
44
	public static function setGetValuesToObject($object) {
45
		self::setValuesToObject ( $object, $_GET );
46
	}
47
48
	/**
49
	 * Affects member to member the values of $_POST to the members of the object $object
50
	 * $object must have accessors to each members
51
	 *
52
	 * @param object $object
53
	 */
54
	public static function setPostValuesToObject($object) {
55
		self::setValuesToObject ( $object, $_POST );
56
	}
57
58
	/**
59
	 * Call a cleaning function on the post
60
	 *
61
	 * @param string $function the cleaning function, default htmlentities
62
	 * @return array
63
	 */
64
	public static function getPost($function = "htmlentities") {
65
		return array_map ( $function, $_POST );
66
	}
67
68
	/**
69
	 * Returns the query data, for PUT, DELETE PATCH methods
70
	 */
71
	public static function getInput() {
72
		$put = array ();
73
		\parse_str ( \file_get_contents ( 'php://input' ), $put );
74
		return $put;
75
	}
76
77
	/**
78
	 * Returns the query data, regardless of the method
79
	 *
80
	 * @return array
81
	 */
82
	public static function getDatas() {
83
		$method = \strtolower ( $_SERVER ['REQUEST_METHOD'] );
84
		switch ($method) {
85
			case 'post' :
86
				return $_POST;
87
			case 'get' :
88
				return $_GET;
89
			default :
90
				return self::getInput ();
91
		}
92
	}
93
94
	/**
95
	 * Returns the request content-type header
96
	 *
97
	 * @return string
98
	 */
99
	public static function getContentType() {
100
		$headers = getallheaders ();
101
		if (isset ( $headers ["content-type"] )) {
102
			return $headers ["content-type"];
103
		}
104
		return null;
105
	}
106
107
	/**
108
	 * Copyright © 2008 Darrin Yeager
109
	 * https://www.dyeager.org/
110
	 * Licensed under BSD license.
111
	 * https://www.dyeager.org/downloads/license-bsd.txt
112
	 */
113 1
	public static function getDefaultLanguage() {
114 1
		if (isset ( $_SERVER ["HTTP_ACCEPT_LANGUAGE"] ))
115 1
			return self::parseDefaultLanguage ( $_SERVER ["HTTP_ACCEPT_LANGUAGE"] );
116
		else
117
			return self::parseDefaultLanguage ( NULL );
118
	}
119
120 1
	private static function parseDefaultLanguage($http_accept, $deflang = "en") {
121 1
		if (isset ( $http_accept ) && strlen ( $http_accept ) > 1) {
122 1
			$x = explode ( ",", $http_accept );
123 1
			$lang = [ ];
124 1
			foreach ( $x as $val ) {
125 1
				if (preg_match ( "/(.*);q=([0-1]{0,1}.\d{0,4})/i", $val, $matches ))
126 1
					$lang [$matches [1]] = ( float ) $matches [2];
127
				else
128 1
					$lang [$val] = 1.0;
129
			}
130
131 1
			$qval = 0.0;
132 1
			foreach ( $lang as $key => $value ) {
133 1
				if ($value > $qval) {
134 1
					$qval = ( float ) $value;
135 1
					$deflang = $key;
136
				}
137
			}
138
		}
139 1
		return $deflang;
140
	}
141
142
	public static function setLocale(string $locale) {
143
		try {
144
			if (class_exists ( 'Locale', false )) {
145
				\Locale::setDefault ( $locale );
146
			}
147
		} catch ( \Exception $e ) {
148
			// Nothing to do
149
		}
150
	}
151
152
	/**
153
	 * Returns true if the request is an Ajax request
154
	 *
155
	 * @return boolean
156
	 */
157 18
	public static function isAjax() {
158 18
		return (isset ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && ! empty ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && strtolower ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest');
159
	}
160
161
	/**
162
	 * Returns true if the request is sent by the POST method
163
	 *
164
	 * @return boolean
165
	 */
166 5
	public static function isPost() {
167 5
		return $_SERVER ['REQUEST_METHOD'] === 'POST';
168
	}
169
170
	/**
171
	 * Returns true if the request is cross site
172
	 *
173
	 * @return boolean
174
	 */
175
	public static function isCrossSite() {
176
		return stripos ( $_SERVER ['HTTP_REFERER'], $_SERVER ['SERVER_NAME'] ) === FALSE;
177
	}
178
179
	/**
180
	 * Returns true if request contentType is set to json
181
	 *
182
	 * @return boolean
183
	 */
184
	public static function isJSON() {
185
		$contentType = self::getContentType ();
186
		return \stripos ( $contentType, "json" ) !== false;
187
	}
188
189
	/**
190
	 * Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist
191
	 *
192
	 * @param string $key
193
	 * @param string $default return value by default
194
	 * @return string
195
	 */
196
	public static function get($key, $default = NULL) {
197
		return isset ( $_GET [$key] ) ? $_GET [$key] : $default;
198
	}
199
200
	/**
201
	 * Returns a boolean at the key position in request
202
	 *
203
	 * @param string $key the key to add or set
204
	 * @return boolean
205
	 */
206
	public static function getBoolean($key) {
207
		$ret = false;
208
		if (isset ( $_REQUEST [$key] )) {
209
			$ret = UString::isBooleanTrue ( $_REQUEST [$key] );
210
		}
211
		return $ret;
212
	}
213
214
	/**
215
	 * Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist
216
	 *
217
	 * @param string $key
218
	 * @param string $default return value by default
219
	 * @return string
220
	 */
221 5
	public static function post($key, $default = NULL) {
222 5
		return isset ( $_POST [$key] ) ? $_POST [$key] : $default;
223
	}
224
225 19
	public static function getUrl($url) {
226 19
		$config = Startup::getConfig ();
227 19
		$siteUrl = \rtrim ( $config ["siteUrl"], '/' );
228 19
		if (UString::startswith ( $url, "/" ) === false) {
229 18
			$url = "/" . $url;
230
		}
231 19
		return $siteUrl . $url;
232
	}
233
234
	public static function getUrlParts() {
235
		return \explode ( "/", $_GET ["c"] );
236
	}
237
238
	/**
239
	 * Returns the http method
240
	 *
241
	 * @return string
242
	 */
243 6
	public static function getMethod() {
244 6
		return \strtolower ( $_SERVER ['REQUEST_METHOD'] );
245
	}
246
247
	public static function cleanUrl($url) {
248
		$url = \str_replace ( "\\", "/", $url );
249
		return \str_replace ( "//", "/", $url );
250
	}
251
252
	/**
253
	 * Fix up PHP's messing up input containing dots, etc.
254
	 * `$source` can be either 'post' or 'get'
255
	 *
256
	 * @param string $source
257
	 * @return string[]
258
	 * @see https://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays#68667
259
	 */
260
	public static function getRealInput($source = 'post') {
261
		$pairs = explode ( "&", strtolower ( $source ) === 'post' ? file_get_contents ( "php://input" ) : $_SERVER ['QUERY_STRING'] );
262
		$vars = array ();
263
		foreach ( $pairs as $pair ) {
264
			$nv = explode ( "=", $pair );
265
			$name = urldecode ( $nv [0] );
266
			$value = urldecode ( $nv [1] ?? '');
267
			$vars [$name] = $value;
268
		}
269
		return $vars;
270
	}
271
272
	public static function getRealGET() {
273
		return self::getRealInput ( 'get' );
274
	}
275
276
	public static function getRealPOST() {
277
		return self::getRealInput ( 'post' );
278
	}
279
}
280