Ubiquity  2.0.0
php rapid development framework
Request.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
7 
13 class Request {
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 getPost($function="htmlentities") {
39  return array_map($function, $_POST);
40  }
41 
45  public static function getInput() {
46  $put=array ();
47  \parse_str(\file_get_contents('php://input'), $put);
48  return $put;
49  }
50 
55  public static function getDatas() {
56  $method=\strtolower($_SERVER['REQUEST_METHOD']);
57  switch($method) {
58  case 'post':
59  return $_POST;
60  case 'get':
61  return $_GET;
62  default:
63  return self::getInput();
64  }
65  }
66 
71  public static function getContentType() {
72  $headers=getallheaders();
73  if (isset($headers["content-type"])) {
74  return $headers["content-type"];
75  }
76  return null;
77  }
78 
83  public static function isAjax() {
84  return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
85  }
86 
91  public static function isPost() {
92  return $_SERVER['REQUEST_METHOD'] === 'POST';
93  }
94 
99  public static function isCrossSite() {
100  return stripos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) === FALSE;
101  }
102 
107  public static function isJSON() {
108  $contentType=self::getContentType();
109  return \stripos($contentType, "json") !== false;
110  }
111 
118  public static function get($key, $default=NULL) {
119  return isset($_GET[$key]) ? $_GET[$key] : $default;
120  }
121 
128  public static function post($key, $default=NULL) {
129  return isset($_POST[$key]) ? $_POST[$key] : $default;
130  }
131 
132  public static function getUrl($url) {
133  $config=Startup::getConfig();
134  $siteUrl=\rtrim($config["siteUrl"], '/');
135  if (UString::startswith($url, "/") === false) {
136  $url="/" . $url;
137  }
138  return $siteUrl . $url;
139  }
140 
141  public static function getUrlParts() {
142  return \explode("/", $_GET["c"]);
143  }
144 
149  public static function getMethod() {
150  return \strtolower($_SERVER['REQUEST_METHOD']);
151  }
152 }
static setValuesToObject($object, $values=null)
Affects member to member the values of the associative array $values to the members of the object $ob...
Definition: Request.php:21
static getDatas()
Returns the query data, regardless of the method.
Definition: Request.php:55
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: Request.php:128
Request utilities.
Definition: Request.php:13
static isCrossSite()
Returns true if the request is cross site.
Definition: Request.php:99
static getMethod()
Returns the http method.
Definition: Request.php:149
static isPost()
Returns true if the request is sent by the POST method.
Definition: Request.php:91
static isJSON()
Returns true if request contentType is set to json.
Definition: Request.php:107
static getPost($function="htmlentities")
Call a cleaning function on the post.
Definition: Request.php:38
static getContentType()
Returns the request content-type header.
Definition: Request.php:71
static isAjax()
Returns true if the request is an Ajax request.
Definition: Request.php:83
static startswith($hay, $needle)
Definition: UString.php:12
static getInput()
Returns the query data, for PUT, DELETE PATCH methods.
Definition: Request.php:45