Completed
Push — master ( 37883e...733fa6 )
by Jean-Christophe
01:30
created

URequest::getContentType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 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
10
 * @author jc
11
 * @version 1.0.0.2
12
 */
13
class URequest {
14
15
	/**
16
	 * Affects member to member the values of the associative array $values to the members of the object $object
17
	 * Used for example to retrieve the variables posted and assign them to the members of an object
18
	 * @param object $object
19
	 * @param associative array $values
20
	 */
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
33
	/**
34
	 * Call a cleaning function on the post
35
	 * @param string $function the cleaning function, default htmlentities
36
	 * @return array
37
	 */
38
	public static function getPost($function="htmlentities") {
39
		return array_map($function, $_POST);
40
	}
41
42
	/**
43
	 * Returns the query data, for PUT, DELETE PATCH methods
44
	 */
45
	public static function getInput() {
46
		$put=array ();
47
		\parse_str(\file_get_contents('php://input'), $put);
48
		return $put;
49
	}
50
51
	/**
52
	 * Returns the query data, regardless of the method
53
	 * @return array
54
	 */
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
67
	/**
68
	 * Returns the request content-type header
69
	 * @return string
70
	 */
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
79
	/**
80
	 * Returns true if the request is an Ajax request
81
	 * @return boolean
82
	 */
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
87
	/**
88
	 * Returns true if the request is sent by the POST method
89
	 * @return boolean
90
	 */
91
	public static function isPost() {
92
		return $_SERVER['REQUEST_METHOD'] === 'POST';
93
	}
94
95
	/**
96
	 * Returns true if the request is cross site
97
	 * @return boolean
98
	 */
99
	public static function isCrossSite() {
0 ignored issues
show
Coding Style introduced by
isCrossSite uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
		return stripos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) === FALSE;
101
	}
102
103
	/**
104
	 * Returns true if request contentType is set to json
105
	 * @return boolean
106
	 */
107
	public static function isJSON() {
108
		$contentType=self::getContentType();
109
		return \stripos($contentType, "json") !== false;
110
	}
111
112
	/**
113
	 * Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist
114
	 * @param string $key
115
	 * @param string $default return value by default
116
	 * @return string
117
	 */
118
	public static function get($key, $default=NULL) {
119
		return isset($_GET[$key]) ? $_GET[$key] : $default;
120
	}
121
122
	/**
123
	 * Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist
124
	 * @param string $key
125
	 * @param string $default return value by default
126
	 * @return string
127
	 */
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
145
	/**
146
	 * Returns the http method
147
	 * @return string
148
	 */
149
	public static function getMethod() {
150
		return \strtolower($_SERVER['REQUEST_METHOD']);
151
	}
152
153
	public static function cleanUrl($url){
154
		$url=\str_replace("\\", "/", $url);
155
		return \str_replace("//", "/", $url);
156
	}
157
}
158