Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
URequest.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\http;
4
8
19
23 private static $uriInfos;
31 public static function setValuesToObject($object, $values = null): void {
32 if (! isset ( $values )) {
33 $values = $_POST;
34 }
35 foreach ( $values as $key => $value ) {
36 $accessor = 'set' . \ucfirst ( $key );
37 if (\method_exists ( $object, $accessor )) {
38 $object->$accessor ( $value );
39 $object->_rest [$key] = $value;
40 }
41 }
42 }
43
50 public static function setGetValuesToObject($object): void {
51 self::setValuesToObject ( $object, $_GET );
52 }
53
60 public static function setPostValuesToObject($object): void {
61 self::setValuesToObject ( $object, $_POST );
62 }
63
70 public static function getPost($function = 'htmlentities'): array {
71 return \array_map ( $function, $_POST );
72 }
73
77 public static function getInput(): array {
78 return Startup::getHttpInstance ()->getInput ();
79 }
80
86 public static function getDatas(): array {
87 $method = \strtolower ( $_SERVER ['REQUEST_METHOD'] );
88 switch ($method) {
89 case 'post' :
90 if (self::getContentType () == 'application/x-www-form-urlencoded') {
91 return $_POST;
92 }
93 break;
94 case 'get' :
95 return $_GET;
96 default :
97 return self::getInput ();
98 }
99 return self::getInput ();
100 }
101
102
110 public static function set(string $key, $value = true) {
111 return $_REQUEST [$key] = $value;
112 }
113
120 public static function getDefaultLanguage(): string {
121 if (isset ( $_SERVER ['HTTP_ACCEPT_LANGUAGE'] )) {
122 return self::parseDefaultLanguage ( $_SERVER ['HTTP_ACCEPT_LANGUAGE'] );
123 }
124 return self::parseDefaultLanguage ( NULL );
125 }
126
127 private static function parseDefaultLanguage($http_accept, $deflang = 'en'): string {
128 if (isset ( $http_accept ) && \strlen ( $http_accept ) > 1) {
129 $x = \explode ( ",", $http_accept );
130 $lang = [ ];
131 foreach ( $x as $val ) {
132 if (\preg_match ( "/(.*);q=([0-1]{0,1}.\d{0,4})/i", $val, $matches )) {
133 $lang [$matches [1]] = ( float )$matches [2];
134 }else {
135 $lang [$val] = 1.0;
136 }
137 }
138
139 $qval = 0.0;
140 foreach ( $lang as $key => $value ) {
141 if ($value > $qval) {
142 $qval = ( float ) $value;
143 $deflang = $key;
144 }
145 }
146 }
147 return $deflang;
148 }
149
150 public static function setLocale(string $locale): void {
151 try {
152 if (\class_exists ( 'Locale', false )) {
153 \Locale::setDefault ( $locale );
154 }
155 } catch ( \Exception $e ) {
156 // Nothing to do
157 }
158 }
159
160
168 public static function get(string $key, $default = NULL): ?string {
169 return $_GET [$key] ?? $default;
170 }
171
178 public static function getBoolean(string $key): bool {
179 $ret = false;
180 if (isset ( $_REQUEST [$key] )) {
181 $ret = UString::isBooleanTrue ( $_REQUEST [$key] );
182 }
183 return $ret;
184 }
185
193 public static function post(string $key, $default = NULL) {
194 return $_POST [$key] ?? $default;
195 }
196
197 public static function getUrl($url): string {
198 $config = Startup::getConfig ();
199 $siteUrl = \rtrim ( $config ['siteUrl'], '/' );
200 if (UString::startswith ( $url, '/' ) === false) {
201 $url = '/' . $url;
202 }
203 return $siteUrl . $url;
204 }
205
206 public static function getUrlParts(): array {
207 return \explode ( '/', $_GET ['c'] );
208 }
209
215 public static function getMethod(): string {
216 return \strtolower ( $_SERVER ['REQUEST_METHOD'] );
217 }
218
224 public static function getOrigin(): string {
225 $headers = Startup::getHttpInstance ()->getAllHeaders ();
226 return $headers ['Origin']??$_SERVER ['HTTP_ORIGIN']??$_SERVER ['HTTP_REFERER']??$_SERVER ['REMOTE_ADDR']??'';
227 }
228
229 public static function cleanUrl($url): string {
230 $url = \str_replace ( "\\", "/", $url );
231 return \str_replace ( "//", "/", $url );
232 }
233
242 public static function getRealInput($source = 'post'): array {
243 $pairs = \explode ( '&', \strtolower ( $source ) === 'get' ? $_SERVER ['QUERY_STRING'] : \file_get_contents ( 'php://input' ) );
244 $vars =[];
245 foreach ( $pairs as $pair ) {
246 $nv = \explode ( "=", $pair );
247 $name = \urldecode ( $nv [0] );
248 $value = \urldecode ( $nv [1] ?? '');
249 $vars [$name] = $value;
250 }
251 return $vars;
252 }
253
254 public static function getRealGET(): array {
255 return self::getRealInput ( 'get' );
256 }
257
258 public static function getRealPOST(): array {
259 return self::getRealInput ( 'post' );
260 }
261
269 public static function password_hash(string $key, string $algo = PASSWORD_DEFAULT) {
270 if (isset ( $_POST [$key] )) {
271 return $_POST [$key] = \password_hash ( $_POST [$key], $algo );
272 }
273 return false;
274 }
275
276
285 public static function password_verify(string $passwordKey,string $hash):bool {
286 if (isset ( $_POST [$passwordKey] )) {
287 return \password_verify( $_POST [$passwordKey], $hash );
288 }
289 return false;
290 }
291
298 public static function parseURI(string $uri,string $basedir):array {
299 return self::$uriInfos[$uri]??=self::_parseURI($uri,$basedir);
300 }
301
310 public static function filter(string $key,int $type=\INPUT_POST,int $filter=\FILTER_DEFAULT,$default=null){
311 return \filter_input($type,$key,$filter)??$default;
312 }
313
321 public static function filterPost(string $key,int $filter=\FILTER_DEFAULT,$default=null){
322 return self::filter($key,INPUT_POST,$filter,$default);
323 }
324
332 public static function filterGet(string $key,int $filter=\FILTER_DEFAULT,$default=null){
333 return self::filter($key,INPUT_GET,$filter,$default);
334 }
335
336 private static function _parseURI(string $uri,string $basedir):array {
337 $uri = \ltrim(\urldecode(\parse_url($uri, PHP_URL_PATH)), '/');
338 $isAction = ($uri == null || ! ($fe = \file_exists($basedir . '/' . $uri))) && ($uri != 'favicon.ico');
339 return [
340 'uri' => $uri,
341 'isAction' => $isAction,
342 'file' => $fe??false
343 ];
344 }
345}
Starts the framework.
Definition Startup.php:19
String utilities.
Definition UString.php:15
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18
static _parseURI(string $uri, string $basedir)
Definition URequest.php:336
static getMethod()
Returns the http method.
Definition URequest.php:215
static getPost($function='htmlentities')
Call a cleaning function on the post.
Definition URequest.php:70
static getRealInput($source='post')
Fix up PHP's messing up input containing dots, etc.
Definition URequest.php:242
static setValuesToObject($object, $values=null)
Affects member to member the values of the associative array $values to the members of the object $ob...
Definition URequest.php:31
static parseDefaultLanguage($http_accept, $deflang='en')
Definition URequest.php:127
static post(string $key, $default=NULL)
Returns the value of the $key variable passed by the post method or $default if the $key variable doe...
Definition URequest.php:193
static getDefaultLanguage()
Copyright © 2008 Darrin Yeager https://www.dyeager.org/ Licensed under BSD license.
Definition URequest.php:120
static setPostValuesToObject($object)
Affects member to member the values of $_POST to the members of the object $object $object must have ...
Definition URequest.php:60
static filter(string $key, int $type=\INPUT_POST, int $filter=\FILTER_DEFAULT, $default=null)
Gets a specific external variable by name and optionally filters it.
Definition URequest.php:310
static filterGet(string $key, int $filter=\FILTER_DEFAULT, $default=null)
Gets a specific GET variable by name and optionally filters it.
Definition URequest.php:332
static password_hash(string $key, string $algo=PASSWORD_DEFAULT)
Creates a password hash for a posted value at $key position.
Definition URequest.php:269
static getDatas()
Returns the query data, regardless of the method.
Definition URequest.php:86
static parseURI(string $uri, string $basedir)
Internal use for async servers (Swoole and Workerman).
Definition URequest.php:298
static password_verify(string $passwordKey, string $hash)
Verifies that a posted password matches a hash at $passwordKey position.
Definition URequest.php:285
static getInput()
Returns the query data, for PUT, DELETE PATCH methods.
Definition URequest.php:77
static setGetValuesToObject($object)
Affects member to member the values of $_GET to the members of the object $object $object must have a...
Definition URequest.php:50
static getBoolean(string $key)
Returns a boolean at the key position in request.
Definition URequest.php:178
static setLocale(string $locale)
Definition URequest.php:150
static filterPost(string $key, int $filter=\FILTER_DEFAULT, $default=null)
Gets a specific POST variable by name and optionally filters it.
Definition URequest.php:321
static getOrigin()
Returns the request origin.
Definition URequest.php:224
Ubiquity\utils\http\traits$URequestTesterTrait This class is part of Ubiquity.