Ubiquity  2.0.0
php rapid development framework
RequestUtils.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils;
4 
6 
12 class RequestUtils {
13 
20  public static function setValuesToObject($object, $values=null) {
21  if (!isset($values))
22  $values=$_POST;
23  foreach ( $values as $key => $value ) {
24  $accessor="set" . ucfirst($key);
25  if (method_exists($object, $accessor)) {
26  $object->$accessor($value);
27  $object->_rest[$key]=$value;
28  }
29  }
30  }
31 
37  public static function getPost($function="htmlentities") {
38  return array_map($function, $_POST);
39  }
40 
44  public static function getInput(){
45  $put = array();
46  \parse_str(\file_get_contents('php://input'), $put);
47  return $put;
48  }
49 
54  public static function getDatas(){
55  $method=\strtolower($_SERVER['REQUEST_METHOD']);
56  switch ($method) {
57  case 'post':
58  return $_POST;
59  case 'get':
60  return $_GET;
61  default:
62  return self::getInput();
63  }
64  }
65 
70  public static function getContentType(){
71  $headers=getallheaders();
72  if(isset($headers["content-type"])){
73  return $headers["content-type"];
74  }
75  return null;
76  }
77 
82  public static function isAjax() {
83  return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
84  }
85 
90  public static function isPost() {
91  return $_SERVER['REQUEST_METHOD'] === 'POST';
92  }
93 
98  public static function isCrossSite(){
99  return stripos($_SERVER['HTTP_REFERER'],$_SERVER['SERVER_NAME'])===FALSE;
100  }
101 
106  public static function isJSON(){
107  $contentType=self::getContentType();
108  return \stripos($contentType, "json")!==false;
109  }
110 
117  public static function get($key, $default=NULL) {
118  return isset($_GET[$key]) ? $_GET[$key] : $default;
119  }
120 
127  public static function post($key, $default=NULL) {
128  return isset($_POST[$key]) ? $_POST[$key] : $default;
129  }
130 
131  public static function getUrl($url) {
132  $config=Startup::getConfig();
133  if (UString::startswith($url, "/") === false) {
134  $url="/" . $url;
135  }
136  return $config["siteUrl"] . $url;
137  }
138 
139  public static function getUrlParts(){
140  return \explode("/", $_GET["c"]);
141  }
142 
147  public static function getMethod() {
148  return \strtolower($_SERVER['REQUEST_METHOD']);
149  }
150 }
static isJSON()
Returns true if request contentType is set to json.
static isPost()
Returns true if the request is sent by the POST method.
static getContentType()
Returns the request content-type header.
static getPost($function="htmlentities")
Call a cleaning function on the post.
static isCrossSite()
Returns true if the request is cross site.
static post($key, $default=NULL)
Returns the value of the $key variable passed by the post method or $default if the $key variable doe...
static setValuesToObject($object, $values=null)
Affects member to member the values of the associative array $values to the members of the object $ob...
static isAjax()
Returns true if the request is an Ajax request.
static getDatas()
Returns the query data, regardless of the method.
static startswith($hay, $needle)
Definition: UString.php:12
Request utilities.
static getMethod()
Returns the http method.
static getInput()
Returns the query data, for PUT, DELETE PATCH methods.