-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmisc.php
104 lines (85 loc) · 1.92 KB
/
misc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Return an array containing the input or transformed from the input, to ensure that foreach can be run on the input
*
* @author Dotan Cohen
* @version 2016-07-28
*
* @param $input
* @return array
*/
function ensureArray($input)
{
if ( is_array($input) ) {
return $input;
}
if ($input instanceof \stdClass) {
return json_decode(json_encode($input), TRUE);
}
return array($input);
}
/**
* Return a desired HTTP header
*
* Accepts pseudo-headers: Protocol and RespCode
* Known Issue: If multiple headers share a key, returns only the first
*
* @author Dotan Cohen
* @version 2017-03-20
*
* @param $headers array Array of HTTP headers
* @param $header string Name of desired HTTP header, or pseudo-header Protocol or RespCode
*
* @return null|string
*/
function getHeader(array $headers, $header)
{
// TODO: Support pseudo-headers ProtocolType ProtocolVersion RespDescription Charset
$header = trim(strtolower($header));
foreach ( $headers as $item ) {
$parts = explode(':', $item, 2);
// Support pseudo-headers
if ( count($parts) < 2 ) {
if ( substr($parts[0],0,4)=='HTTP' ) {
$parts = explode(' ', $item);
if ( 2<=count($parts) ) {
switch ($header) {
case 'protocol':
return $parts[0];
break;
case 'respcode':
return $parts[1];
break;
}
}
}
continue;
}
$key = trim(strtolower($parts[0]));
$val = trim($parts[1]);
if ( $key==$header ) {
return $val;
}
}
return NULL;
}
/**
* Return the HTTP request parameters normalized
*
* @author Dotan Cohen
* @version 2017-07-10
*
* @return array
*/
function getRequestNormalized()
{
static $request;
if ( is_null($request) ) {
$request = array();
// TODO: Separate _GET _POST _COOKIE and maybe _FILE
foreach ( $_REQUEST as $k => $v ) {
$request[strtolower($k)] = mb_convert_encoding($v, 'UTF-8', 'UTF-8');
}
}
return $request;
}