|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tartana; |
|
3
|
|
|
use Pdp\Parser; |
|
4
|
|
|
use Pdp\PublicSuffixListManager; |
|
5
|
|
|
|
|
6
|
|
|
final class Util |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Returns if there is a process with the given id running. |
|
11
|
|
|
* |
|
12
|
|
|
* @param integer $pid |
|
13
|
7 |
|
* @return boolean |
|
14
|
|
|
*/ |
|
15
|
7 |
|
public static function isPidRunning ($pid) |
|
16
|
|
|
{ |
|
17
|
|
|
return posix_getpgid((int) $pid) > 0; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Convertes the given size to a human readable format. |
|
22
|
|
|
* |
|
23
|
|
|
* @param integer $size |
|
24
|
|
|
* @param array $strings |
|
25
|
8 |
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
8 |
|
public static function readableSize ($size, $strings = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']) |
|
28
|
|
|
{ |
|
29
|
|
|
for ($i = 0; ($size / 1024) > 0.9; $i ++, $size /= 1024) |
|
|
|
|
|
|
30
|
8 |
|
{ |
|
31
|
|
|
} |
|
32
|
|
|
return round($size, 2) . (isset($strings[$i]) ? ' ' . $strings[$i] : ''); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the real path for the given path. |
|
37
|
|
|
* If the path is relative and the file exists, the real fill path is |
|
38
|
|
|
* returned. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $path |
|
41
|
54 |
|
* @return NULL|string |
|
42
|
|
|
*/ |
|
43
|
54 |
|
public static function realPath ($path) |
|
44
|
|
|
{ |
|
45
|
11 |
|
if (! $path) |
|
46
|
|
|
{ |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
48 |
|
|
|
50
|
|
|
// If it is a relative path add the root path to it |
|
51
|
11 |
|
if ($path[0] !== DIRECTORY_SEPARATOR && preg_match('~\A[A-Z]:(?![^/\\\\])~i', $path) < 1) |
|
52
|
|
|
{ |
|
53
|
|
|
$path = TARTANA_PATH_ROOT . '/' . $path; |
|
54
|
48 |
|
} |
|
55
|
|
|
|
|
56
|
10 |
|
if (! file_exists($path)) |
|
57
|
|
|
{ |
|
58
|
|
|
return null; |
|
59
|
42 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $path; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns a deep cloned array of the given objects. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $objects |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
91 |
|
public static function cloneObjects (array $objects) |
|
71
|
85 |
|
{ |
|
72
|
91 |
|
return array_map(function ($obj) { |
|
73
|
|
|
return clone $obj; |
|
74
|
|
|
}, $objects); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if the haystack starts with the needle. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $haystack |
|
81
|
|
|
* @param string $needle |
|
82
|
12 |
|
* @return boolean |
|
83
|
|
|
*/ |
|
84
|
12 |
|
public static function startsWith ($haystack, $needle) |
|
85
|
|
|
{ |
|
86
|
|
|
return (substr($haystack, 0, strlen($needle)) === $needle); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Checks if the haystack ends with the needle. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $haystack |
|
93
|
|
|
* @param string $needle |
|
94
|
37 |
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
37 |
|
public static function endsWith ($haystack, $needle) |
|
97
|
37 |
|
{ |
|
98
|
|
|
$length = strlen($needle); |
|
99
|
1 |
|
if ($length == 0) |
|
100
|
|
|
{ |
|
101
|
|
|
return true; |
|
102
|
37 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
return (substr($haystack, - $length) === $needle); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Shortens the given string by replacing the middle part with three dots. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $string |
|
111
|
|
|
* @param integer $length |
|
112
|
|
|
* |
|
113
|
5 |
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
5 |
|
public static function shorten ($string, $length = 20) |
|
116
|
|
|
{ |
|
117
|
1 |
|
if (strlen($string) > $length) |
|
118
|
1 |
|
{ |
|
119
|
|
|
$characters = floor($length / 2); |
|
120
|
5 |
|
return substr($string, 0, $characters) . '...' . substr($string, - 1 * $characters); |
|
121
|
|
|
} |
|
122
|
|
|
return $string; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Parses the given url and returns an array with the following properties |
|
127
|
|
|
* for that url: |
|
128
|
|
|
* http://user:[email protected]:8000/link/test.html?hello=foo#bar |
|
129
|
|
|
* |
|
130
|
|
|
* [scheme] => http |
|
131
|
|
|
* [user] => user |
|
132
|
|
|
* [pass] => pass |
|
133
|
|
|
* [host] => mirrors.kernel.org |
|
134
|
|
|
* [subdomain] => mirrors |
|
135
|
|
|
* [registerableDomain] => kernel.org |
|
136
|
|
|
* [publicSuffix] => org |
|
137
|
|
|
* [port] => 8000 |
|
138
|
|
|
* [path] => /link/test.html |
|
139
|
|
|
* [query] => hello=foo |
|
140
|
|
|
* [fragment] => bar |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $url |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function parseUrl ($url) |
|
146
|
|
|
{ |
|
147
|
|
|
try |
|
148
|
|
|
{ |
|
149
|
|
|
$pslManager = new PublicSuffixListManager(); |
|
150
|
|
|
$parser = new Parser($pslManager->getList()); |
|
151
|
|
|
|
|
152
|
|
|
return $parser->parseUrl($url)->toArray(); |
|
153
|
|
|
} |
|
154
|
|
|
catch (\Exception $e) |
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
|
|
'scheme' => '', |
|
158
|
|
|
'user' => '', |
|
159
|
|
|
'pass' => '', |
|
160
|
|
|
'host' => '', |
|
161
|
|
|
'subdomain' => '', |
|
162
|
|
|
'registerableDomain' => '', |
|
163
|
|
|
'publicSuffix' => '', |
|
164
|
|
|
'port' => '', |
|
165
|
|
|
'path' => '', |
|
166
|
|
|
'query' => '', |
|
167
|
|
|
'fragment' => '' |
|
168
|
|
|
]; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Cleans special characters from the uri which can be used as identifier in |
|
174
|
|
|
* YAMl files. |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $uri |
|
177
|
|
|
* @return string |
|
178
|
|
|
* @see Util::parseUrl() |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function cleanHostName ($uri) |
|
181
|
|
|
{ |
|
182
|
|
|
if (is_string($uri)) |
|
183
|
|
|
{ |
|
184
|
|
|
$uri = [ |
|
185
|
|
|
'host' => $uri |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
if (! isset($uri['registerableDomain'])) |
|
189
|
|
|
{ |
|
190
|
|
|
$uri['registerableDomain'] = ''; |
|
191
|
|
|
} |
|
192
|
|
|
if (! isset($uri['host'])) |
|
193
|
|
|
{ |
|
194
|
|
|
$uri['host'] = ''; |
|
195
|
|
|
} |
|
196
|
|
|
$hostName = $uri['registerableDomain'] ? $uri['registerableDomain'] : $uri['host']; |
|
197
|
|
|
$hostName = preg_replace("/[^A-Za-z0-9 ]/", '', $hostName); |
|
198
|
|
|
|
|
199
|
|
|
return $hostName; |
|
200
|
|
|
} |
|
201
|
|
|
} |
This check looks for
forloops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.