1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Phile Utility class |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Core; |
6
|
|
|
|
7
|
|
|
use Phile\Core\Container; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Utility class |
11
|
|
|
* |
12
|
|
|
* @author PhileCMS |
13
|
|
|
* @link https://philecms.github.io |
14
|
|
|
* @license http://opensource.org/licenses/MIT |
15
|
|
|
* @package Phile |
16
|
|
|
*/ |
17
|
|
|
class Utility |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* method to get the current http protocol |
22
|
|
|
* |
23
|
|
|
* @return string the current protocol |
24
|
|
|
* @deprecated since 1.5 will be removed |
25
|
|
|
*/ |
26
|
|
|
public static function getProtocol() |
27
|
|
|
{ |
28
|
|
|
return Container::getInstance()->get('Phile_Router')->getProtocol(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* detect base url |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
* @deprecated since 1.5 will be removed |
36
|
|
|
*/ |
37
|
|
|
public static function getBaseUrl() |
38
|
|
|
{ |
39
|
|
|
$container = Container::getInstance(); |
40
|
|
|
if ($container->has('Phile_Router')) { |
41
|
|
|
$router = $container->get('Phile_Router'); |
42
|
|
|
} else { |
43
|
|
|
// BC: some old 1.x plugins may call this before the core is initialized |
44
|
|
|
$router = new Router; |
45
|
|
|
} |
46
|
|
|
return $router->getBaseUrl(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* detect install path |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @deprecated since 1.5 will be removed |
54
|
|
|
*/ |
55
|
|
|
public static function getInstallPath() |
56
|
|
|
{ |
57
|
|
|
$path = self::getBaseUrl(); |
|
|
|
|
58
|
|
|
$path = substr($path, strpos($path, '://') + 3); |
59
|
|
|
$path = substr($path, strpos($path, '/') + 1); |
60
|
|
|
|
61
|
|
|
return $path; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* resolve a file path by replace the mod: prefix |
66
|
|
|
* |
67
|
|
|
* @param string $path |
68
|
|
|
* |
69
|
|
|
* @return string|null the full filepath or null if file does not exists |
70
|
|
|
*/ |
71
|
|
|
public static function resolveFilePath($path) |
72
|
|
|
{ |
73
|
|
|
// resolve MOD: prefix |
74
|
|
|
if (strtoupper(substr($path, 0, 3)) === 'MOD') { |
75
|
|
|
$path = str_ireplace('mod:', PLUGINS_DIR, $path); |
76
|
|
|
} |
77
|
|
|
// check if file exists |
78
|
|
|
if (file_exists($path)) { |
|
|
|
|
79
|
|
|
return $path; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* load files e.g. config files |
87
|
|
|
* |
88
|
|
|
* @param string $file |
89
|
|
|
* |
90
|
|
|
* @return mixed|null |
91
|
|
|
*/ |
92
|
40 |
|
public static function load($file) |
93
|
|
|
{ |
94
|
40 |
|
if (file_exists($file)) { |
95
|
40 |
|
return include $file; |
96
|
|
|
} |
97
|
|
|
|
98
|
34 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* check if a plugin is loaded |
103
|
|
|
* |
104
|
|
|
* @param string $plugin |
105
|
|
|
* @return bool |
106
|
|
|
* @deprecated since 1.5 will be removed. Use 'plugins_loaded' event! |
107
|
|
|
*/ |
108
|
|
|
public static function isPluginLoaded($plugin) |
109
|
|
|
{ |
110
|
|
|
$config = Container::getInstance()->get('Phile_Config'); |
111
|
|
|
if ($config->get('plugins')) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
$plugins = $config->get('plugins'); |
115
|
|
|
return (isset($plugins[$plugin]['active']) && $plugins[$plugin]['active'] === true); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* static method to get files by directory and file filter |
120
|
|
|
* |
121
|
|
|
* @param string $directory |
122
|
|
|
* @param string $filter |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
10 |
|
public static function getFiles($directory, $filter = '\Phile\FilterIterator\GeneralFileFilterIterator') |
127
|
|
|
{ |
128
|
10 |
|
$files = new $filter( |
129
|
10 |
|
new \RecursiveIteratorIterator( |
130
|
10 |
|
new \RecursiveDirectoryIterator( |
131
|
|
|
$directory, |
132
|
|
|
\RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
133
|
|
|
) |
134
|
|
|
) |
135
|
|
|
); |
136
|
10 |
|
$result = array(); |
137
|
10 |
|
foreach ($files as $file) { |
138
|
|
|
/** |
139
|
|
|
* @var \SplFileInfo $file |
140
|
|
|
*/ |
141
|
10 |
|
$result[] = $file->getPathname(); |
142
|
|
|
} |
143
|
|
|
|
144
|
10 |
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* redirect to an url |
149
|
|
|
* |
150
|
|
|
* @param string $url the url to redirect to |
151
|
|
|
* @param int $statusCode the http status code |
152
|
|
|
* @deprecated since 1.5 will be removed |
153
|
|
|
*/ |
154
|
|
|
public static function redirect($url, $statusCode = 302) |
155
|
|
|
{ |
156
|
|
|
(new Response)->redirect($url, $statusCode); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* generate secure md5 hash |
161
|
|
|
* |
162
|
|
|
* @param string $value |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public static function getSecureMD5Hash($value) |
167
|
|
|
{ |
168
|
|
|
$config = Container::getInstance()->get('Phile_Config'); |
169
|
|
|
|
170
|
|
|
return md5($config->get('encryptionKey') . $value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* method to generate a secure token |
175
|
|
|
* code from http://stackoverflow.com/a/13733588/1372085 |
176
|
|
|
* modified by Frank Nägler |
177
|
|
|
* |
178
|
|
|
* @param int $length |
179
|
|
|
* @param bool $widthSpecialChars |
180
|
|
|
* @param null|string $additionalChars |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
1 |
|
public static function generateSecureToken($length = 32, $widthSpecialChars = true, $additionalChars = null) |
185
|
|
|
{ |
186
|
1 |
|
$token = ""; |
187
|
1 |
|
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
188
|
1 |
|
$codeAlphabet .= "abcdefghijklmnopqrstuvwxyz"; |
189
|
1 |
|
$codeAlphabet .= "0123456789"; |
190
|
1 |
|
if ($widthSpecialChars) { |
191
|
1 |
|
$codeAlphabet .= "!/()=?[]|{}"; |
192
|
|
|
} |
193
|
1 |
|
if ($additionalChars !== null) { |
194
|
|
|
$codeAlphabet .= $additionalChars; |
195
|
|
|
} |
196
|
1 |
|
for ($i = 0; $i < $length; $i++) { |
197
|
1 |
|
$token .= $codeAlphabet[Utility::crypto_rand_secure(0, strlen($codeAlphabet))]; |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
return $token; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* method to get a more secure random value |
205
|
|
|
* code from http://stackoverflow.com/a/13733588/1372085 |
206
|
|
|
* |
207
|
|
|
* @param int $min |
208
|
|
|
* @param int $max |
209
|
|
|
* |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
|
|
// @codingStandardsIgnoreStart |
213
|
1 |
|
public static function crypto_rand_secure(int $min, int $max) |
214
|
|
|
{ |
215
|
|
|
// @codingStandardsIgnoreEnd |
216
|
1 |
|
$range = $max - $min; |
217
|
1 |
|
if ($range < 0) { |
218
|
|
|
return $min; |
219
|
|
|
} // not so random... |
220
|
1 |
|
$log = log($range, 2); |
221
|
1 |
|
$bytes = (int)($log / 8) + 1; // length in bytes |
222
|
1 |
|
$bits = (int)$log + 1; // length in bits |
223
|
1 |
|
$filter = (int)(1 << $bits) - 1; // set all lower bits to 1 |
224
|
|
|
do { |
225
|
1 |
|
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
226
|
1 |
|
$rnd = $rnd & $filter; // discard irrelevant bits |
227
|
1 |
|
} while ($rnd >= $range); |
228
|
|
|
|
229
|
1 |
|
return $min + $rnd; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.