Skip to content

Commit f26b614

Browse files
committed
Http Component added
1 parent 093d010 commit f26b614

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed

Http.php

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
<?php
2+
3+
namespace Flextype\Component\Http;
4+
5+
use Flextype\Component\Arr\Arr;
6+
7+
class Http
8+
{
9+
/**
10+
* HTTP status codes and messages
11+
*
12+
* @var array
13+
*/
14+
public static $http_status_messages = [
15+
100 => 'Continue',
16+
101 => 'Switching Protocols',
17+
102 => 'Processing', // RFC2518
18+
200 => 'OK',
19+
201 => 'Created',
20+
202 => 'Accepted',
21+
203 => 'Non-Authoritative Information',
22+
204 => 'No Content',
23+
205 => 'Reset Content',
24+
206 => 'Partial Content',
25+
207 => 'Multi-Status', // RFC4918
26+
208 => 'Already Reported', // RFC5842
27+
226 => 'IM Used', // RFC3229
28+
300 => 'Multiple Choices',
29+
301 => 'Moved Permanently',
30+
302 => 'Found',
31+
303 => 'See Other',
32+
304 => 'Not Modified',
33+
305 => 'Use Proxy',
34+
306 => 'Reserved',
35+
307 => 'Temporary Redirect',
36+
308 => 'Permanent Redirect', // RFC-reschke-http-status-308-07
37+
400 => 'Bad Request',
38+
401 => 'Unauthorized',
39+
402 => 'Payment Required',
40+
403 => 'Forbidden',
41+
404 => 'Not Found',
42+
405 => 'Method Not Allowed',
43+
406 => 'Not Acceptable',
44+
407 => 'Proxy Authentication Required',
45+
408 => 'Request Timeout',
46+
409 => 'Conflict',
47+
410 => 'Gone',
48+
411 => 'Length Required',
49+
412 => 'Precondition Failed',
50+
413 => 'Request Entity Too Large',
51+
414 => 'Request-URI Too Long',
52+
415 => 'Unsupported Media Type',
53+
416 => 'Requested Range Not Satisfiable',
54+
417 => 'Expectation Failed',
55+
418 => 'I\'m a teapot', // RFC2324
56+
422 => 'Unprocessable Entity', // RFC4918
57+
423 => 'Locked', // RFC4918
58+
424 => 'Failed Dependency', // RFC4918
59+
425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817
60+
426 => 'Upgrade Required', // RFC2817
61+
428 => 'Precondition Required', // RFC6585
62+
429 => 'Too Many Requests', // RFC6585
63+
431 => 'Request Header Fields Too Large', // RFC6585
64+
500 => 'Internal Server Error',
65+
501 => 'Not Implemented',
66+
502 => 'Bad Gateway',
67+
503 => 'Service Unavailable',
68+
504 => 'Gateway Timeout',
69+
505 => 'HTTP Version Not Supported',
70+
506 => 'Variant Also Negotiates (Experimental)', // RFC2295
71+
507 => 'Insufficient Storage', // RFC4918
72+
508 => 'Loop Detected', // RFC5842
73+
510 => 'Not Extended', // RFC2774
74+
511 => 'Network Authentication Required', // RFC6585
75+
];
76+
77+
/**
78+
* Set response header status
79+
*
80+
* Http::setResponseStatus(404);
81+
*
82+
* @param integer $status Status code
83+
* @return void
84+
*/
85+
public static function setResponseStatus(int $status) : void
86+
{
87+
if (array_key_exists($status, Http::$http_status_messages)) {
88+
header('HTTP/1.1 ' . $status . ' ' . Http::$http_status_messages[$status]);
89+
}
90+
}
91+
92+
/**
93+
* Redirects the browser to a page specified by the $url argument.
94+
*
95+
* Http::redirect('test');
96+
*
97+
* @param string $url The URL
98+
* @param integer $status Status
99+
* @param integer $delay Delay
100+
*/
101+
public static function redirect(string $url, int $status = 302, int $delay = null)
102+
{
103+
// Status codes
104+
$messages = [];
105+
$messages[301] = '301 Moved Permanently';
106+
$messages[302] = '302 Found';
107+
108+
// Is Headers sent ?
109+
if (headers_sent()) {
110+
echo "<script>document.location.href='" . $url . "';</script>\n";
111+
} else {
112+
113+
// Redirect headers
114+
Http::setRequestHeaders('HTTP/1.1 ' . $status . ' ' . Arr::get($messages, $status, 302));
115+
116+
// Delay execution
117+
if ($delay !== null) {
118+
sleep((int) $delay);
119+
}
120+
121+
// Redirect
122+
Http::setRequestHeaders("Location: $url");
123+
124+
// Shutdown request
125+
Http::requestShutdown();
126+
}
127+
}
128+
129+
/**
130+
* Set one or multiple headers.
131+
*
132+
* Http::setRequestHeaders('Location: http://site.com/');
133+
*
134+
* @param mixed $headers String or array with headers to send.
135+
*/
136+
public static function setRequestHeaders($headers)
137+
{
138+
// Loop elements
139+
foreach ((array) $headers as $header) {
140+
141+
// Set header
142+
header((string) $header);
143+
}
144+
}
145+
146+
/**
147+
* Get
148+
*
149+
* $action = Http::get('action');
150+
*
151+
* @param string $key Key
152+
* @param mixed
153+
*/
154+
public static function get(string $key)
155+
{
156+
return Arr::get($_GET, $key);
157+
}
158+
159+
/**
160+
* Post
161+
*
162+
* $login = Http::post('login');
163+
*
164+
* @param string $key Key
165+
* @param mixed
166+
*/
167+
public static function post(string $key)
168+
{
169+
return Arr::get($_POST, $key);
170+
}
171+
172+
/**
173+
* Gets the base URL
174+
*
175+
* echo Http::getBaseUrl();
176+
*
177+
* @return string
178+
*/
179+
public static function getBaseUrl() : string
180+
{
181+
$https = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://';
182+
183+
return $https . rtrim(rtrim($_SERVER['HTTP_HOST'], '\\/') . dirname($_SERVER['PHP_SELF']), '\\/');
184+
}
185+
186+
/**
187+
* Gets current URL
188+
*
189+
* echo Http::getCurrentUrl();
190+
*
191+
* @return string
192+
*/
193+
public static function getCurrentUrl() : string
194+
{
195+
return (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
196+
}
197+
198+
/**
199+
* Get Uri String
200+
*
201+
* $uri_string = Http::getUriString();
202+
*
203+
* @access public
204+
* @return string
205+
*/
206+
public static function getUriString() : string
207+
{
208+
// Get request url and script url
209+
$url = '';
210+
$request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
211+
$script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';
212+
213+
// Get our url path and trim the / of the left and the right
214+
if ($request_url != $script_url) {
215+
$url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
216+
}
217+
218+
$url = preg_replace('/\?.*/', '', $url); // Strip query string
219+
220+
return $url;
221+
}
222+
223+
/**
224+
* Get Uri Segments
225+
*
226+
* $uri_segments = Http::getUriSegments();
227+
*
228+
* @access public
229+
* @return array
230+
*/
231+
public static function getUriSegments() : array
232+
{
233+
return explode('/', self::getUriString());
234+
}
235+
236+
/**
237+
* Get Uri Segment
238+
*
239+
* $uri_segment = Http::getUriSegment(1);
240+
*
241+
* @access public
242+
* @param int $segment segment
243+
* @return string
244+
*/
245+
public static function getUriSegment(int $segment) : string
246+
{
247+
$segments = self::getUriSegments();
248+
return isset($segments[$segment]) ? $segments[$segment] : null;
249+
}
250+
251+
/**
252+
* Returns whether this is an ajax request or not
253+
*
254+
* if (Http::isAjax()) {
255+
* // do something...
256+
* }
257+
*
258+
* @return boolean
259+
*/
260+
public static function isAjax() : bool
261+
{
262+
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
263+
}
264+
265+
/**
266+
* Terminate request
267+
*
268+
* Http::requestShutdown();
269+
*
270+
*/
271+
public static function requestShutdown()
272+
{
273+
exit(0);
274+
}
275+
276+
}

0 commit comments

Comments
 (0)