1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* HTTP functions declaration. |
5
|
|
|
* |
6
|
|
|
* @category Asymptix PHP Framework |
7
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
8
|
|
|
* @copyright (c) 2009-2015, Dmytro Zarezenko |
9
|
|
|
* |
10
|
|
|
* @git https://github.com/Asymptix/Framework |
11
|
|
|
* @license http://opensource.org/licenses/MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if (!function_exists("http_redirect")) { |
15
|
|
|
/** |
16
|
|
|
* Redirect to the given url. |
17
|
|
|
* |
18
|
|
|
* @param string $url The URL to redirect to. |
19
|
|
|
* @param array<mixed> $params Associative array of query parameters. |
20
|
|
|
* @param boolean $session Whether to append session information. |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
function http_redirect($url, $params = array(), $session = false) { |
|
|
|
|
23
|
|
|
$paramsString = ""; |
24
|
|
|
foreach ($params as $key => $value) { |
25
|
|
|
$paramsString .= "&" . $key . "=" . $value; |
26
|
|
|
} |
27
|
|
|
if ($session) { |
28
|
|
|
$paramsString .= "&" . session_name() . "=" . session_id(); |
29
|
|
|
} |
30
|
|
|
$paramsString = substr($paramsString, 1); |
31
|
|
|
if ($paramsString) { |
32
|
|
|
$paramsString = "?" . $paramsString; |
33
|
|
|
} |
34
|
|
|
header("Location: " . $url . $paramsString); |
35
|
|
|
exit(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!function_exists('http_response_code')) { |
41
|
|
|
/** |
42
|
|
|
* Get or Set the HTTP response code. |
43
|
|
|
* |
44
|
|
|
* @param integer $code The optional response_code will set the response code. |
45
|
|
|
* @return integer The current response code. By default the return value is int(200). |
46
|
|
|
*/ |
47
|
|
|
function http_response_code($code = NULL) { |
|
|
|
|
48
|
|
|
if ($code !== NULL) { |
49
|
|
|
switch ($code) { |
50
|
|
|
case 100: $text = 'Continue'; |
|
|
|
|
51
|
|
|
break; |
52
|
|
|
case 101: $text = 'Switching Protocols'; |
|
|
|
|
53
|
|
|
break; |
54
|
|
|
case 200: $text = 'OK'; |
|
|
|
|
55
|
|
|
break; |
56
|
|
|
case 201: $text = 'Created'; |
|
|
|
|
57
|
|
|
break; |
58
|
|
|
case 202: $text = 'Accepted'; |
|
|
|
|
59
|
|
|
break; |
60
|
|
|
case 203: $text = 'Non-Authoritative Information'; |
|
|
|
|
61
|
|
|
break; |
62
|
|
|
case 204: $text = 'No Content'; |
|
|
|
|
63
|
|
|
break; |
64
|
|
|
case 205: $text = 'Reset Content'; |
|
|
|
|
65
|
|
|
break; |
66
|
|
|
case 206: $text = 'Partial Content'; |
|
|
|
|
67
|
|
|
break; |
68
|
|
|
case 300: $text = 'Multiple Choices'; |
|
|
|
|
69
|
|
|
break; |
70
|
|
|
case 301: $text = 'Moved Permanently'; |
|
|
|
|
71
|
|
|
break; |
72
|
|
|
case 302: $text = 'Moved Temporarily'; |
|
|
|
|
73
|
|
|
break; |
74
|
|
|
case 303: $text = 'See Other'; |
|
|
|
|
75
|
|
|
break; |
76
|
|
|
case 304: $text = 'Not Modified'; |
|
|
|
|
77
|
|
|
break; |
78
|
|
|
case 305: $text = 'Use Proxy'; |
|
|
|
|
79
|
|
|
break; |
80
|
|
|
case 400: $text = 'Bad Request'; |
|
|
|
|
81
|
|
|
break; |
82
|
|
|
case 401: $text = 'Unauthorized'; |
|
|
|
|
83
|
|
|
break; |
84
|
|
|
case 402: $text = 'Payment Required'; |
|
|
|
|
85
|
|
|
break; |
86
|
|
|
case 403: $text = 'Forbidden'; |
|
|
|
|
87
|
|
|
break; |
88
|
|
|
case 404: $text = 'Not Found'; |
|
|
|
|
89
|
|
|
break; |
90
|
|
|
case 405: $text = 'Method Not Allowed'; |
|
|
|
|
91
|
|
|
break; |
92
|
|
|
case 406: $text = 'Not Acceptable'; |
|
|
|
|
93
|
|
|
break; |
94
|
|
|
case 407: $text = 'Proxy Authentication Required'; |
|
|
|
|
95
|
|
|
break; |
96
|
|
|
case 408: $text = 'Request Time-out'; |
|
|
|
|
97
|
|
|
break; |
98
|
|
|
case 409: $text = 'Conflict'; |
|
|
|
|
99
|
|
|
break; |
100
|
|
|
case 410: $text = 'Gone'; |
|
|
|
|
101
|
|
|
break; |
102
|
|
|
case 411: $text = 'Length Required'; |
|
|
|
|
103
|
|
|
break; |
104
|
|
|
case 412: $text = 'Precondition Failed'; |
|
|
|
|
105
|
|
|
break; |
106
|
|
|
case 413: $text = 'Request Entity Too Large'; |
|
|
|
|
107
|
|
|
break; |
108
|
|
|
case 414: $text = 'Request-URI Too Large'; |
|
|
|
|
109
|
|
|
break; |
110
|
|
|
case 415: $text = 'Unsupported Media Type'; |
|
|
|
|
111
|
|
|
break; |
112
|
|
|
case 500: $text = 'Internal Server Error'; |
|
|
|
|
113
|
|
|
break; |
114
|
|
|
case 501: $text = 'Not Implemented'; |
|
|
|
|
115
|
|
|
break; |
116
|
|
|
case 502: $text = 'Bad Gateway'; |
|
|
|
|
117
|
|
|
break; |
118
|
|
|
case 503: $text = 'Service Unavailable'; |
|
|
|
|
119
|
|
|
break; |
120
|
|
|
case 504: $text = 'Gateway Time-out'; |
|
|
|
|
121
|
|
|
break; |
122
|
|
|
case 505: $text = 'HTTP Version not supported'; |
|
|
|
|
123
|
|
|
break; |
124
|
|
|
default: |
125
|
|
|
exit('Unknown http status code "' . htmlentities($code) . '"'); |
|
|
|
|
126
|
|
|
break; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); |
130
|
|
|
|
131
|
|
|
header($protocol . ' ' . $code . ' ' . $text); |
132
|
|
|
|
133
|
|
|
$GLOBALS['http_response_code'] = $code; |
134
|
|
|
} else { |
135
|
|
|
$code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $code; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (!function_exists('http_chunked_decode')) { |
143
|
|
|
/** |
144
|
|
|
* Dechunk an http 'transfer-encoding: chunked' message. |
145
|
|
|
* |
146
|
|
|
* @param string $chunk the encoded message |
147
|
|
|
* @return string the decoded message. |
148
|
|
|
* If $chunk wasn't encoded properly it will be returned unmodified. |
149
|
|
|
*/ |
150
|
|
|
function http_chunked_decode($chunk) { |
151
|
|
|
$pos = 0; |
152
|
|
|
$len = strlen($chunk); |
153
|
|
|
$dechunk = null; |
154
|
|
|
|
155
|
|
|
while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) { |
156
|
|
|
if (!is_hex($chunkLenHex)) { |
157
|
|
|
trigger_error('Value is not properly chunk encoded', E_USER_WARNING); |
158
|
|
|
return $chunk; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$pos = $newlineAt + 1; |
162
|
|
|
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n")); |
163
|
|
|
$dechunk .= substr($chunk, $pos, $chunkLen); |
164
|
|
|
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1; |
165
|
|
|
} |
166
|
|
|
return $dechunk; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Determine if a string can represent a number in hexadecimal. |
171
|
|
|
* |
172
|
|
|
* @param string $hex |
173
|
|
|
* @return boolean true if the string is a hex, otherwise false |
174
|
|
|
*/ |
175
|
|
|
function is_hex($hex) { |
176
|
|
|
$hex = strtolower(trim(ltrim($hex, "0"))); |
177
|
|
|
if (empty($hex)) { |
178
|
|
|
$hex = 0; |
179
|
|
|
}; |
180
|
|
|
$dec = hexdec($hex); |
181
|
|
|
return ($hex == dechex($dec)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.