|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Helper functions. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Helpers |
|
12
|
|
|
{ |
|
13
|
|
|
private static $hash_equals; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* helper function to fix paths '//' or '/./' or '/foo/../' in a path. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $path Path to resolve |
|
19
|
|
|
* |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function fixPath($path) |
|
23
|
|
|
{ |
|
24
|
|
|
$path = str_replace('\\', '/', $path); //windows paths |
|
25
|
|
|
$replace = ['#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#']; |
|
26
|
|
|
|
|
27
|
|
|
do { |
|
28
|
|
|
$path = preg_replace($replace, '/', $path, -1, $n); |
|
29
|
|
|
} while ($n > 0); |
|
30
|
|
|
|
|
31
|
|
|
return $path; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Join several pieces into a path. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string |
|
38
|
|
|
* ... |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function joinPath() |
|
43
|
|
|
{ |
|
44
|
|
|
return self::fixPath(implode('/', func_get_args())); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check whether a request is or not ajax. |
|
49
|
|
|
* |
|
50
|
|
|
* @param RequestInterface $request |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function isAjax(RequestInterface $request) |
|
55
|
|
|
{ |
|
56
|
|
|
return strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check if a request is post or any similar method. |
|
61
|
|
|
* |
|
62
|
|
|
* @param RequestInterface $request |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function isPost(RequestInterface $request) |
|
67
|
|
|
{ |
|
68
|
|
|
switch (strtoupper($request->getMethod())) { |
|
69
|
|
|
case 'GET': |
|
70
|
|
|
case 'HEAD': |
|
71
|
|
|
case 'CONNECT': |
|
72
|
|
|
case 'TRACE': |
|
73
|
|
|
case 'OPTIONS': |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Check whether a response is a redirection. |
|
82
|
|
|
* |
|
83
|
|
|
* @param ResponseInterface $response |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function isRedirect(ResponseInterface $response) |
|
88
|
|
|
{ |
|
89
|
|
|
return in_array($response->getStatusCode(), [302, 301]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the output buffer. |
|
94
|
|
|
* |
|
95
|
|
|
* @param int $level |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function getOutput($level) |
|
100
|
|
|
{ |
|
101
|
|
|
$output = ''; |
|
102
|
|
|
|
|
103
|
|
|
while (ob_get_level() >= $level) { |
|
104
|
|
|
$output .= ob_get_clean(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $output; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return the output buffer. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $level |
|
|
|
|
|
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function getMimeType(ResponseInterface $response) |
|
118
|
|
|
{ |
|
119
|
|
|
$mime = strtolower($response->getHeaderLine('Content-Type')); |
|
120
|
|
|
$mime = explode(';', $mime, 2); |
|
121
|
|
|
|
|
122
|
|
|
return trim($mime[0]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Very short timing attack safe string comparison for PHP < 5.6 |
|
127
|
|
|
* http://php.net/manual/en/function.hash-equals.php#118384. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $a |
|
130
|
|
|
* @param string $b |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public static function hashEquals($a, $b) |
|
135
|
|
|
{ |
|
136
|
|
|
if (self::$hash_equals === null) { |
|
137
|
|
|
self::$hash_equals = function_exists('hash_equals'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (self::$hash_equals) { |
|
141
|
|
|
return hash_equals($a, $b); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return substr_count($a ^ $b, "\0") * 2 === strlen($a.$b); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.