Passed
Push — master ( a02cde...eb9e44 )
by Jean-Christophe
09:52
created

URequest::isCrossSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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.1.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 4
	public static function setValuesToObject($object, $values = null) {
27 4
		if (! isset ( $values ))
28
			$values = $_POST;
29 4
		foreach ( $values as $key => $value ) {
30 4
			$accessor = "set" . ucfirst ( $key );
31 4
			if (method_exists ( $object, $accessor )) {
32 4
				$object->$accessor ( $value );
33 4
				$object->_rest [$key] = $value;
34
			}
35
		}
36 4
	}
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 1
	public static function getInput() {
72 1
		return Startup::getHttpInstance ()->getInput ();
73
	}
74
75
	/**
76
	 * Returns the query data, regardless of the method
77
	 *
78
	 * @return array
79
	 */
80 1
	public static function getDatas() {
81 1
		$method = \strtolower ( $_SERVER ['REQUEST_METHOD'] );
82 1
		switch ($method) {
83 1
			case 'post' :
84 1
				if (self::getContentType () == 'application/x-www-form-urlencoded') {
85 1
					return $_POST;
86
				}
87
				break;
88 1
			case 'get' :
89
				return $_GET;
90
			default :
91 1
				return self::getInput ();
92
		}
93
		return self::getInput ();
94
	}
95
96
	/**
97
	 * Returns the request content-type header
98
	 *
99
	 * @return string
100
	 */
101 1
	public static function getContentType() {
102 1
		$headers = Startup::getHttpInstance ()->getAllHeaders ();
103 1
		if (isset ( $headers ["Content-Type"] )) {
104 1
			return $headers ["Content-Type"];
105
		}
106
		return null;
107
	}
108
109
	/**
110
	 * Copyright © 2008 Darrin Yeager
111
	 * https://www.dyeager.org/
112
	 * Licensed under BSD license.
113
	 * https://www.dyeager.org/downloads/license-bsd.txt
114
	 */
115 1
	public static function getDefaultLanguage() {
116 1
		if (isset ( $_SERVER ["HTTP_ACCEPT_LANGUAGE"] ))
117 1
			return self::parseDefaultLanguage ( $_SERVER ["HTTP_ACCEPT_LANGUAGE"] );
118
		else
119
			return self::parseDefaultLanguage ( NULL );
120
	}
121
122 1
	private static function parseDefaultLanguage($http_accept, $deflang = "en") {
123 1
		if (isset ( $http_accept ) && strlen ( $http_accept ) > 1) {
124 1
			$x = explode ( ",", $http_accept );
125 1
			$lang = [ ];
126 1
			foreach ( $x as $val ) {
127 1
				if (preg_match ( "/(.*);q=([0-1]{0,1}.\d{0,4})/i", $val, $matches ))
128 1
					$lang [$matches [1]] = ( float ) $matches [2];
129
				else
130 1
					$lang [$val] = 1.0;
131
			}
132
133 1
			$qval = 0.0;
134 1
			foreach ( $lang as $key => $value ) {
135 1
				if ($value > $qval) {
136 1
					$qval = ( float ) $value;
137 1
					$deflang = $key;
138
				}
139
			}
140
		}
141 1
		return $deflang;
142
	}
143
144
	public static function setLocale(string $locale) {
145
		try {
146
			if (class_exists ( 'Locale', false )) {
147
				\Locale::setDefault ( $locale );
148
			}
149
		} catch ( \Exception $e ) {
150
			// Nothing to do
151
		}
152
	}
153
154
	/**
155
	 * Returns true if the request is an Ajax request
156
	 *
157
	 * @return boolean
158
	 */
159 23
	public static function isAjax() {
160 23
		return (isset ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && ! empty ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) && strtolower ( $_SERVER ['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest');
161
	}
162
163
	/**
164
	 * Returns true if the request is sent by the POST method
165
	 *
166
	 * @return boolean
167
	 */
168 6
	public static function isPost() {
169 6
		return $_SERVER ['REQUEST_METHOD'] === 'POST';
170
	}
171
172
	/**
173
	 * Returns true if the request is cross site
174
	 *
175
	 * @return boolean
176
	 */
177
	public static function isCrossSite() {
178
		return stripos ( $_SERVER ['HTTP_REFERER'], $_SERVER ['SERVER_NAME'] ) === FALSE;
179
	}
180
181
	/**
182
	 * Returns true if request contentType is set to json
183
	 *
184
	 * @return boolean
185
	 */
186 1
	public static function isJSON() {
187 1
		$contentType = self::getContentType ();
188 1
		return \stripos ( $contentType, "json" ) !== false;
189
	}
190
191
	/**
192
	 * Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist
193
	 *
194
	 * @param string $key
195
	 * @param string $default return value by default
196
	 * @return string
197
	 */
198
	public static function get($key, $default = NULL) {
199
		return isset ( $_GET [$key] ) ? $_GET [$key] : $default;
200
	}
201
202
	/**
203
	 * Returns a boolean at the key position in request
204
	 *
205
	 * @param string $key the key to add or set
206
	 * @return boolean
207
	 */
208 1
	public static function getBoolean($key) {
209 1
		$ret = false;
210 1
		if (isset ( $_REQUEST [$key] )) {
211 1
			$ret = UString::isBooleanTrue ( $_REQUEST [$key] );
212
		}
213 1
		return $ret;
214
	}
215
216
	/**
217
	 * Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist
218
	 *
219
	 * @param string $key
220
	 * @param string $default return value by default
221
	 * @return string
222
	 */
223 7
	public static function post($key, $default = NULL) {
224 7
		return isset ( $_POST [$key] ) ? $_POST [$key] : $default;
225
	}
226
227 21
	public static function getUrl($url) {
228 21
		$config = Startup::getConfig ();
229 21
		$siteUrl = \rtrim ( $config ["siteUrl"], '/' );
230 21
		if (UString::startswith ( $url, "/" ) === false) {
231 20
			$url = "/" . $url;
232
		}
233 21
		return $siteUrl . $url;
234
	}
235
236
	public static function getUrlParts() {
237
		return \explode ( "/", $_GET ["c"] );
238
	}
239
240
	/**
241
	 * Returns the http method
242
	 *
243
	 * @return string
244
	 */
245 12
	public static function getMethod() {
246 12
		return \strtolower ( $_SERVER ['REQUEST_METHOD'] );
247
	}
248
249
	/**
250
	 * Returns the request origin
251
	 *
252
	 * @return string
253
	 */
254 19
	public static function getOrigin() {
255 19
		$headers = Startup::getHttpInstance ()->getAllHeaders ();
256 19
		if (isset ( $headers ['Origin'] )) {
257 2
			return $headers ['Origin'];
258
		}
259 18
		if (isset ( $_SERVER ['HTTP_ORIGIN'] )) {
260
			return $_SERVER ['HTTP_ORIGIN'];
261 18
		} else if (isset ( $_SERVER ['HTTP_REFERER'] )) {
262 4
			return $_SERVER ['HTTP_REFERER'];
263
		} else {
264 15
			return $_SERVER ['REMOTE_ADDR'];
265
		}
266
	}
267
268
	public static function cleanUrl($url) {
269
		$url = \str_replace ( "\\", "/", $url );
270
		return \str_replace ( "//", "/", $url );
271
	}
272
273
	/**
274
	 * Fix up PHP's messing up input containing dots, etc.
275
	 * `$source` can be either 'post' or 'get'
276
	 *
277
	 * @param string $source
278
	 * @return string[]
279
	 * @see https://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays#68667
280
	 */
281 1
	public static function getRealInput($source = 'post') {
282 1
		$pairs = explode ( "&", strtolower ( $source ) === 'get' ? $_SERVER ['QUERY_STRING'] : file_get_contents ( "php://input" ) );
283 1
		$vars = array ();
284 1
		foreach ( $pairs as $pair ) {
285 1
			$nv = explode ( "=", $pair );
286 1
			$name = urldecode ( $nv [0] );
287 1
			$value = urldecode ( $nv [1] ?? '');
288 1
			$vars [$name] = $value;
289
		}
290 1
		return $vars;
291
	}
292
293
	public static function getRealGET() {
294
		return self::getRealInput ( 'get' );
295
	}
296
297
	public static function getRealPOST() {
298
		return self::getRealInput ( 'post' );
299
	}
300
}
301