Ubiquity  2.0.2
php rapid development framework
URequest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
7 
13 class URequest {
14 
21  public static function setValuesToObject($object, $values=null) {
22  if (!isset($values))
23  $values=$_POST;
24  foreach ( $values as $key => $value ) {
25  $accessor="set" . ucfirst($key);
26  if (method_exists($object, $accessor)) {
27  $object->$accessor($value);
28  $object->_rest[$key]=$value;
29  }
30  }
31  }
32 
38  public static function setGetValuesToObject($object) {
39  self::setValuesToObject($object,$_GET);
40  }
41 
47  public static function setPostValuesToObject($object) {
48  self::setValuesToObject($object,$_POST);
49  }
50 
56  public static function getPost($function="htmlentities") {
57  return array_map($function, $_POST);
58  }
59 
63  public static function getInput() {
64  $put=array ();
65  \parse_str(\file_get_contents('php://input'), $put);
66  return $put;
67  }
68 
73  public static function getDatas() {
74  $method=\strtolower($_SERVER['REQUEST_METHOD']);
75  switch($method) {
76  case 'post':
77  return $_POST;
78  case 'get':
79  return $_GET;
80  default:
81  return self::getInput();
82  }
83  }
84 
89  public static function getContentType() {
90  $headers=getallheaders();
91  if (isset($headers["content-type"])) {
92  return $headers["content-type"];
93  }
94  return null;
95  }
96 
101  public static function isAjax() {
102  return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
103  }
104 
109  public static function isPost() {
110  return $_SERVER['REQUEST_METHOD'] === 'POST';
111  }
112 
117  public static function isCrossSite() {
118  return stripos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) === FALSE;
119  }
120 
125  public static function isJSON() {
126  $contentType=self::getContentType();
127  return \stripos($contentType, "json") !== false;
128  }
129 
136  public static function get($key, $default=NULL) {
137  return isset($_GET[$key]) ? $_GET[$key] : $default;
138  }
139 
146  public static function post($key, $default=NULL) {
147  return isset($_POST[$key]) ? $_POST[$key] : $default;
148  }
149 
150  public static function getUrl($url) {
151  $config=Startup::getConfig();
152  $siteUrl=\rtrim($config["siteUrl"], '/');
153  if (UString::startswith($url, "/") === false) {
154  $url="/" . $url;
155  }
156  return $siteUrl . $url;
157  }
158 
159  public static function getUrlParts() {
160  return \explode("/", $_GET["c"]);
161  }
162 
167  public static function getMethod() {
168  return \strtolower($_SERVER['REQUEST_METHOD']);
169  }
170 
171  public static function cleanUrl($url){
172  $url=\str_replace("\\", "/", $url);
173  return \str_replace("//", "/", $url);
174  }
175 }
static isPost()
Returns true if the request is sent by the POST method.
Definition: URequest.php:109
static getMethod()
Returns the http method.
Definition: URequest.php:167
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:38
static getDatas()
Returns the query data, regardless of the method.
Definition: URequest.php:73
static getContentType()
Returns the request content-type header.
Definition: URequest.php:89
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:21
static getPost($function="htmlentities")
Call a cleaning function on the post.
Definition: URequest.php:56
Http Request utilities.
Definition: URequest.php:13
static post($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:146
static isAjax()
Returns true if the request is an Ajax request.
Definition: URequest.php:101
static setPostValuesToObject($object)
Affects member to member the values of $_POST to the members of the object $object $object must have ...
Definition: URequest.php:47
static isJSON()
Returns true if request contentType is set to json.
Definition: URequest.php:125
static getInput()
Returns the query data, for PUT, DELETE PATCH methods.
Definition: URequest.php:63
static isCrossSite()
Returns true if the request is cross site.
Definition: URequest.php:117
static startswith($hay, $needle)
Definition: UString.php:12