Rev 1180 | Rev 1182 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1103 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | use ViaThinkSoft\OIDplus\OIDplus; |
1149 | daniel-mar | 21 | use ViaThinkSoft\OIDplus\OIDplusException; |
1050 | daniel-mar | 22 | |
1130 | daniel-mar | 23 | /** |
24 | * @param string $privKey |
||
25 | * @return bool |
||
26 | */ |
||
27 | function is_privatekey_encrypted(string $privKey): bool { |
||
830 | daniel-mar | 28 | return strpos($privKey,'BEGIN ENCRYPTED PRIVATE KEY') !== false; |
29 | } |
||
30 | |||
1130 | daniel-mar | 31 | /** |
32 | * @param string $privKey |
||
33 | * @param string $pubKey |
||
34 | * @return bool |
||
35 | */ |
||
36 | function verify_private_public_key(string $privKey, string $pubKey): bool { |
||
721 | daniel-mar | 37 | if (!function_exists('openssl_public_encrypt')) return false; |
74 | daniel-mar | 38 | try { |
39 | if (empty($privKey)) return false; |
||
40 | if (empty($pubKey)) return false; |
||
453 | daniel-mar | 41 | $data = generateRandomString(25); |
386 | daniel-mar | 42 | $encrypted = ''; |
43 | $decrypted = ''; |
||
74 | daniel-mar | 44 | if (!@openssl_public_encrypt($data, $encrypted, $pubKey)) return false; |
45 | if (!@openssl_private_decrypt($encrypted, $decrypted, $privKey)) return false; |
||
46 | return $decrypted == $data; |
||
1050 | daniel-mar | 47 | } catch (\Exception $e) { |
74 | daniel-mar | 48 | return false; |
49 | } |
||
50 | } |
||
51 | |||
1130 | daniel-mar | 52 | /** |
53 | * @param string $privKeyOld |
||
54 | * @param string|null $passphrase_old |
||
55 | * @param string|null $passphrase_new |
||
56 | * @return false|string |
||
57 | */ |
||
58 | function change_private_key_passphrase(string $privKeyOld, string $passphrase_old=null, string $passphrase_new=null) { |
||
830 | daniel-mar | 59 | $pkey_config = array( |
60 | //"digest_alg" => "sha512", |
||
61 | //"private_key_bits" => 2048, |
||
62 | //"private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
1158 | daniel-mar | 63 | "config" => class_exists(OIDplus::class) ? OIDplus::getOpenSslCnf() : @getenv('OPENSSL_CONF') |
830 | daniel-mar | 64 | ); |
65 | $privKeyNew = @openssl_pkey_get_private($privKeyOld, $passphrase_old); |
||
66 | if ($privKeyNew === false) return false; |
||
1116 | daniel-mar | 67 | if (!@openssl_pkey_export($privKeyNew, $privKeyNewExport, $passphrase_new, $pkey_config)) return false; |
68 | if ($privKeyNewExport === "") return false; |
||
69 | return "$privKeyNewExport"; |
||
830 | daniel-mar | 70 | } |
71 | |||
1130 | daniel-mar | 72 | /** |
73 | * @param string $privKey |
||
74 | * @param string $passphrase |
||
75 | * @return false|string |
||
76 | */ |
||
77 | function decrypt_private_key(string $privKey, string $passphrase) { |
||
830 | daniel-mar | 78 | return change_private_key_passphrase($privKey, $passphrase, null); |
79 | } |
||
80 | |||
1130 | daniel-mar | 81 | /** |
82 | * @param string $privKey |
||
83 | * @param string $passphrase |
||
84 | * @return false|string |
||
85 | */ |
||
86 | function encrypt_private_key(string $privKey, string $passphrase) { |
||
830 | daniel-mar | 87 | return change_private_key_passphrase($privKey, null, $passphrase); |
88 | } |
||
89 | |||
1130 | daniel-mar | 90 | /** |
91 | * @param string $data |
||
92 | * @return int |
||
93 | */ |
||
94 | function smallhash(string $data): int { // get 31 bits from SHA1. Values 0..2147483647 |
||
250 | daniel-mar | 95 | return (hexdec(substr(sha1($data),-4*2)) & 0x7FFFFFFF); |
74 | daniel-mar | 96 | } |
180 | daniel-mar | 97 | |
1130 | daniel-mar | 98 | /** |
99 | * @param string $name |
||
100 | * @return array |
||
101 | */ |
||
102 | function split_firstname_lastname(string $name): array { |
||
182 | daniel-mar | 103 | $ary = explode(' ', $name); |
104 | $last_name = array_pop($ary); |
||
105 | $first_name = implode(' ', $ary); |
||
106 | return array($first_name, $last_name); |
||
107 | } |
||
108 | |||
1130 | daniel-mar | 109 | /** |
110 | * @return void |
||
111 | */ |
||
180 | daniel-mar | 112 | function originHeaders() { |
113 | // CORS |
||
114 | // Author: Till Wehowski |
||
426 | daniel-mar | 115 | // TODO: add to class OIDplus |
182 | daniel-mar | 116 | |
180 | daniel-mar | 117 | header("Access-Control-Allow-Credentials: true"); |
118 | header("Access-Control-Allow-Origin: ".strip_tags(((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*"))); |
||
119 | |||
120 | header("Access-Control-Allow-Headers: If-None-Match, X-Requested-With, Origin, X-Frdlweb-Bugs, Etag, X-Forgery-Protection-Token, X-CSRF-Token"); |
||
121 | |||
122 | if (isset($_SERVER['HTTP_ORIGIN'])) { |
||
123 | header('X-Frame-Options: ALLOW-FROM '.$_SERVER['HTTP_ORIGIN']); |
||
124 | } else { |
||
125 | header_remove("X-Frame-Options"); |
||
126 | } |
||
127 | |||
128 | $expose = array('Etag', 'X-CSRF-Token'); |
||
129 | foreach (headers_list() as $num => $header) { |
||
130 | $h = explode(':', $header); |
||
131 | $expose[] = trim($h[0]); |
||
132 | } |
||
133 | header("Access-Control-Expose-Headers: ".implode(',',$expose)); |
||
134 | |||
135 | header("Vary: Origin"); |
||
136 | } |
||
236 | daniel-mar | 137 | |
346 | daniel-mar | 138 | if (!function_exists('mb_wordwrap')) { |
1130 | daniel-mar | 139 | /** |
140 | * @param string $str |
||
141 | * @param int $width |
||
142 | * @param string $break |
||
143 | * @param bool $cut |
||
144 | * @return string |
||
145 | */ |
||
146 | function mb_wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false): string { |
||
346 | daniel-mar | 147 | // https://stackoverflow.com/a/4988494/488539 |
148 | $lines = explode($break, $str); |
||
149 | foreach ($lines as &$line) { |
||
150 | $line = rtrim($line); |
||
151 | if (mb_strlen($line) <= $width) { |
||
152 | continue; |
||
153 | } |
||
154 | $words = explode(' ', $line); |
||
155 | $line = ''; |
||
156 | $actual = ''; |
||
157 | foreach ($words as $word) { |
||
158 | if (mb_strlen($actual.$word) <= $width) { |
||
159 | $actual .= $word.' '; |
||
160 | } else { |
||
161 | if ($actual != '') { |
||
162 | $line .= rtrim($actual).$break; |
||
163 | } |
||
164 | $actual = $word; |
||
165 | if ($cut) { |
||
166 | while (mb_strlen($actual) > $width) { |
||
167 | $line .= mb_substr($actual, 0, $width).$break; |
||
168 | $actual = mb_substr($actual, $width); |
||
169 | } |
||
170 | } |
||
171 | $actual .= ' '; |
||
172 | } |
||
173 | } |
||
174 | $line .= trim($actual); |
||
175 | } |
||
176 | return implode($break, $lines); |
||
177 | } |
||
178 | } |
||
355 | daniel-mar | 179 | |
1130 | daniel-mar | 180 | /** |
181 | * @param string $out |
||
182 | * @param string $contentType |
||
183 | * @param string $filename |
||
184 | * @return void |
||
185 | */ |
||
186 | function httpOutWithETag(string $out, string $contentType, string $filename='') { |
||
379 | daniel-mar | 187 | $etag = md5($out); |
188 | header("Etag: $etag"); |
||
189 | header("Content-MD5: $etag"); // RFC 2616 clause 14.15 |
||
190 | if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) { |
||
1068 | daniel-mar | 191 | if (PHP_SAPI != 'cli') @http_response_code(304); // 304 Not Modified |
379 | daniel-mar | 192 | } else { |
193 | header("Content-Type: $contentType"); |
||
194 | if (!empty($filename)) { |
||
195 | header('Content-Disposition:inline; filename="'.$filename.'"'); |
||
196 | } |
||
197 | echo $out; |
||
198 | } |
||
199 | die(); |
||
200 | } |
||
201 | |||
1130 | daniel-mar | 202 | /** |
203 | * @param string $str |
||
204 | * @param array $args |
||
205 | * @return string |
||
206 | */ |
||
207 | function my_vsprintf(string $str, array $args): string { |
||
208 | $n = 1; |
||
209 | foreach ($args as $val) { |
||
210 | $str = str_replace("%$n", $val, $str); |
||
211 | $n++; |
||
212 | } |
||
213 | return str_replace("%%", "%", $str); |
||
360 | daniel-mar | 214 | } |
215 | |||
1130 | daniel-mar | 216 | /** |
217 | * @param string $str |
||
218 | * @param mixed ...$sprintfArgs |
||
219 | * @return string |
||
220 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
||
221 | * @throws \ViaThinkSoft\OIDplus\OIDplusException |
||
222 | */ |
||
223 | function _L(string $str, ...$sprintfArgs): string { |
||
401 | daniel-mar | 224 | static $translation_array = array(); |
225 | static $translation_loaded = null; |
||
226 | |||
506 | daniel-mar | 227 | $str = trim($str); |
228 | |||
1050 | daniel-mar | 229 | if (!class_exists(OIDplus::class)) { |
438 | daniel-mar | 230 | return my_vsprintf($str, $sprintfArgs); |
231 | } |
||
232 | |||
360 | daniel-mar | 233 | $lang = OIDplus::getCurrentLang(); |
468 | daniel-mar | 234 | $ta = OIDplus::getTranslationArray($lang); |
1162 | daniel-mar | 235 | $res = $ta[$lang][$str] ?? $str; |
360 | daniel-mar | 236 | |
237 | $res = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $res); |
||
238 | |||
1130 | daniel-mar | 239 | return my_vsprintf($res, $sprintfArgs); |
370 | daniel-mar | 240 | } |
386 | daniel-mar | 241 | |
1130 | daniel-mar | 242 | /** |
243 | * @param array $params |
||
244 | * @param string $key |
||
245 | * @return void |
||
246 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
||
247 | * @throws \ViaThinkSoft\OIDplus\OIDplusException |
||
248 | */ |
||
249 | function _CheckParamExists(array $params, string $key) { |
||
1050 | daniel-mar | 250 | if (class_exists(OIDplusException::class)) { |
698 | daniel-mar | 251 | if (!isset($params[$key])) throw new OIDplusException(_L('Parameter %1 is missing', $key)); |
252 | } else { |
||
253 | if (!isset($params[$key])) throw new Exception(_L('Parameter %1 is missing', $key)); |
||
254 | } |
||
552 | daniel-mar | 255 | } |
256 | |||
1130 | daniel-mar | 257 | /** |
258 | * @param string $cont |
||
259 | * @return array |
||
260 | */ |
||
261 | function extractHtmlContents(string $cont): array { |
||
721 | daniel-mar | 262 | // make sure the program works even if the user provided HTML is not UTF-8 |
263 | $cont = convert_to_utf8_no_bom($cont); |
||
264 | |||
386 | daniel-mar | 265 | $out_js = ''; |
266 | $m = array(); |
||
267 | preg_match_all('@<script[^>]*>(.+)</script>@ismU', $cont, $m); |
||
268 | foreach ($m[1] as $x) { |
||
269 | $out_js = $x . "\n\n"; |
||
270 | } |
||
271 | |||
272 | $out_css = ''; |
||
273 | $m = array(); |
||
274 | preg_match_all('@<style[^>]*>(.+)</style>@ismU', $cont, $m); |
||
275 | foreach ($m[1] as $x) { |
||
276 | $out_css = $x . "\n\n"; |
||
277 | } |
||
278 | |||
279 | $out_html = $cont; |
||
280 | $out_html = preg_replace('@^(.+)<body[^>]*>@isU', '', $out_html); |
||
281 | $out_html = preg_replace('@</body>.+$@isU', '', $out_html); |
||
282 | $out_html = preg_replace('@<title>.+</title>@isU', '', $out_html); |
||
283 | $out_html = preg_replace('@<h1>.+</h1>@isU', '', $out_html, 1); |
||
284 | $out_html = preg_replace('@<script[^>]*>(.+)</script>@ismU', '', $out_html); |
||
285 | $out_html = preg_replace('@<style[^>]*>(.+)</style>@ismU', '', $out_html); |
||
286 | |||
287 | return array($out_html, $out_js, $out_css); |
||
392 | daniel-mar | 288 | } |
289 | |||
1130 | daniel-mar | 290 | /** |
291 | * @param string $password |
||
292 | * @param bool $raw_output |
||
293 | * @return string |
||
294 | * @throws Exception |
||
295 | */ |
||
296 | function sha3_512(string $password, bool $raw_output=false): string { |
||
1103 | daniel-mar | 297 | if (hash_supported_natively('sha3-512')) { |
400 | daniel-mar | 298 | return hash('sha3-512', $password, $raw_output); |
392 | daniel-mar | 299 | } else { |
1021 | daniel-mar | 300 | return \bb\Sha3\Sha3::hash($password, 512, $raw_output); |
392 | daniel-mar | 301 | } |
302 | } |
||
486 | daniel-mar | 303 | |
1130 | daniel-mar | 304 | /** |
305 | * @param string $message |
||
306 | * @param string $key |
||
307 | * @param bool $raw_output |
||
308 | * @return string |
||
309 | */ |
||
310 | function sha3_512_hmac(string $message, string $key, bool $raw_output=false): string { |
||
710 | daniel-mar | 311 | // RFC 2104 HMAC |
1103 | daniel-mar | 312 | if (hash_hmac_supported_natively('sha3-512')) { |
847 | daniel-mar | 313 | return hash_hmac('sha3-512', $message, $key, $raw_output); |
314 | } else { |
||
1021 | daniel-mar | 315 | return \bb\Sha3\Sha3::hash_hmac($message, $key, 512, $raw_output); |
847 | daniel-mar | 316 | } |
710 | daniel-mar | 317 | } |
318 | |||
1130 | daniel-mar | 319 | /** |
320 | * @param string $password |
||
321 | * @param string $salt |
||
322 | * @param int $iterations |
||
323 | * @param int $length |
||
324 | * @param bool $binary |
||
325 | * @return string |
||
326 | */ |
||
327 | function sha3_512_pbkdf2(string $password, string $salt, int $iterations, int $length=0, bool $binary=false): string { |
||
1103 | daniel-mar | 328 | if (hash_pbkdf2_supported_natively('sha3-512')) { |
329 | return hash_pbkdf2('sha3-512', $password, $salt, $iterations, $length, $binary); |
||
330 | } else { |
||
331 | return \bb\Sha3\Sha3::hash_pbkdf2($password, $salt, $iterations, 512, $length, $binary); |
||
332 | } |
||
333 | } |
||
334 | |||
1130 | daniel-mar | 335 | /** |
1181 | daniel-mar | 336 | * @param bool $require_ssl |
337 | * @param string|null $reason |
||
1149 | daniel-mar | 338 | * @return bool |
1181 | daniel-mar | 339 | * @throws OIDplusException |
340 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
||
1149 | daniel-mar | 341 | */ |
1181 | daniel-mar | 342 | function url_post_contents_available(bool $require_ssl=true, string &$reason=null): bool { |
343 | if (function_exists('curl_init')) { |
||
344 | return true; |
||
345 | } else { |
||
346 | $reason = _L('Please install the PHP extension %1, so that OIDplus can connect to the Internet.', '<code>php_curl</code>'); |
||
347 | return false; |
||
348 | } |
||
1149 | daniel-mar | 349 | } |
350 | |||
351 | /** |
||
1130 | daniel-mar | 352 | * @param string $url |
1149 | daniel-mar | 353 | * @param array $params |
354 | * @param array $extraHeaders |
||
1130 | daniel-mar | 355 | * @param string $userAgent |
356 | * @return string|false |
||
1149 | daniel-mar | 357 | * @throws \ViaThinkSoft\OIDplus\OIDplusException |
1130 | daniel-mar | 358 | */ |
1149 | daniel-mar | 359 | function url_post_contents(string $url, array $params=array(), array $extraHeaders=array(), string $userAgent='ViaThinkSoft-OIDplus/2.0') { |
360 | $postFields = http_build_query($params); |
||
361 | |||
362 | $headers = array( |
||
363 | "User-Agent: $userAgent", |
||
364 | "Content-Length: ".strlen($postFields) |
||
365 | ); |
||
366 | |||
367 | foreach ($extraHeaders as $name => $val) { |
||
368 | $headers[] = "$name: $val"; |
||
369 | } |
||
370 | |||
653 | daniel-mar | 371 | if (function_exists('curl_init')) { |
372 | $ch = curl_init(); |
||
1050 | daniel-mar | 373 | if (class_exists(OIDplus::class)) { |
698 | daniel-mar | 374 | if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem'); |
375 | } |
||
653 | daniel-mar | 376 | curl_setopt($ch, CURLOPT_URL, $url); |
963 | daniel-mar | 377 | curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); |
1149 | daniel-mar | 378 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
379 | curl_setopt($ch, CURLOPT_POST, true); |
||
380 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
||
653 | daniel-mar | 381 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
382 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
383 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); |
||
1006 | daniel-mar | 384 | $res = @curl_exec($ch); |
963 | daniel-mar | 385 | $error_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
1006 | daniel-mar | 386 | @curl_close($ch); |
963 | daniel-mar | 387 | if ($error_code >= 400) return false; |
1006 | daniel-mar | 388 | if ($res === false) return false; |
653 | daniel-mar | 389 | } else { |
1149 | daniel-mar | 390 | throw new OIDplusException(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl')); |
391 | } |
||
392 | return $res; |
||
393 | } |
||
394 | |||
395 | /** |
||
1181 | daniel-mar | 396 | * @param bool $require_ssl |
397 | * @param string|null $reason |
||
398 | * @return bool |
||
399 | * @throws OIDplusException |
||
400 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
||
401 | */ |
||
402 | function url_get_contents_available(bool $require_ssl=true, string &$reason=null): bool { |
||
403 | if (function_exists('curl_init')) { |
||
404 | // Via cURL |
||
405 | return true; |
||
406 | } else { |
||
407 | // Via file_get_contents() |
||
408 | if (!ini_get('allow_url_fopen')) { |
||
409 | $reason = _L('Please install the PHP extension %1 and/or enable %2 in your PHP configuration, so that OIDplus can connect to the Internet.', '<code>php_curl</code>', '<code>allow_url_fopen</code>'); |
||
410 | return false; |
||
411 | } |
||
412 | // Use extension_loaded() instead of function_exists(), because our supplement does not help... |
||
413 | if ($require_ssl && !extension_loaded('openssl')) { |
||
414 | $reason = _L('Please install the PHP extension %1 and/or %2, so that OIDplus can connect to the Internet.', '<code>php_curl</code>', '<code>php_openssl</code>'); |
||
415 | return false; |
||
416 | } |
||
417 | return true; |
||
418 | } |
||
419 | } |
||
420 | |||
421 | /** |
||
1149 | daniel-mar | 422 | * @param string $url |
423 | * @param array $extraHeaders |
||
424 | * @param string $userAgent |
||
425 | * @return string|false |
||
426 | */ |
||
427 | function url_get_contents(string $url, array $extraHeaders=array(), string $userAgent='ViaThinkSoft-OIDplus/2.0') { |
||
428 | $headers = array("User-Agent: $userAgent"); |
||
429 | foreach ($extraHeaders as $name => $val) { |
||
430 | $headers[] = "$name: $val"; |
||
431 | } |
||
432 | if (function_exists('curl_init')) { |
||
433 | $ch = curl_init(); |
||
434 | if (class_exists(OIDplus::class)) { |
||
435 | if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem'); |
||
436 | } |
||
437 | curl_setopt($ch, CURLOPT_URL, $url); |
||
438 | curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); |
||
439 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
440 | curl_setopt($ch, CURLOPT_POST, false); |
||
441 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
442 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
443 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); |
||
444 | $res = @curl_exec($ch); |
||
445 | $error_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
446 | @curl_close($ch); |
||
447 | if ($error_code >= 400) return false; |
||
448 | if ($res === false) return false; |
||
449 | } else { |
||
963 | daniel-mar | 450 | // Attention: HTTPS only works if OpenSSL extension is enabled. |
451 | // Our supplement does not help... |
||
716 | daniel-mar | 452 | $opts = [ |
453 | "http" => [ |
||
454 | "method" => "GET", |
||
1149 | daniel-mar | 455 | "header" => implode("\r\n",$headers)."\r\n" |
716 | daniel-mar | 456 | ] |
457 | ]; |
||
458 | $context = stream_context_create($opts); |
||
459 | $res = @file_get_contents($url, false, $context); |
||
653 | daniel-mar | 460 | if ($res === false) return false; |
461 | } |
||
462 | return $res; |
||
660 | daniel-mar | 463 | } |
778 | daniel-mar | 464 | |
1130 | daniel-mar | 465 | /** |
466 | * @return string |
||
467 | */ |
||
468 | function getSortedQuery(): string { |
||
778 | daniel-mar | 469 | // https://stackoverflow.com/a/51777249/488539 |
470 | $url = []; |
||
471 | parse_str($_SERVER['QUERY_STRING'], $url); |
||
472 | ksort($url); |
||
473 | return http_build_query($url); |
||
474 | } |
||
1148 | daniel-mar | 475 | |
476 | /** |
||
477 | * @param array &$rows |
||
478 | * @param string $fieldName |
||
479 | * @return void |
||
480 | */ |
||
1158 | daniel-mar | 481 | function natsort_field(array &$rows, string $fieldName) { |
1148 | daniel-mar | 482 | usort($rows, function($a,$b) use($fieldName) { |
483 | if ($a[$fieldName] == $b[$fieldName]) return 0; // equal |
||
484 | $ary = array( |
||
485 | -1 => $a[$fieldName], |
||
486 | 1 => $b[$fieldName] |
||
487 | ); |
||
1150 | daniel-mar | 488 | natsort($ary); |
1148 | daniel-mar | 489 | $keys = array_keys($ary); |
490 | return $keys[0]; |
||
491 | }); |
||
492 | } |
||
1156 | daniel-mar | 493 | |
494 | /** |
||
495 | * @param array $ary |
||
496 | * @return \stdClass |
||
497 | */ |
||
498 | function array_to_stdobj(array $ary): \stdClass { |
||
499 | $obj = new \stdClass; |
||
500 | foreach ($ary as $name => $val) { |
||
501 | $obj->$name = $val; |
||
502 | } |
||
503 | return $obj; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @param \stdClass $obj |
||
508 | * @return array |
||
509 | */ |
||
510 | function stdobj_to_array(\stdClass $obj): array { |
||
511 | $ary = array(); |
||
512 | foreach ($obj as $name => $val) { |
||
513 | $ary[$name] = $val; |
||
514 | } |
||
515 | return $ary; |
||
516 | } |
||
1180 | daniel-mar | 517 | |
518 | /** |
||
519 | * @return string|false |
||
520 | */ |
||
521 | function get_own_username() { |
||
522 | $current_user = exec('whoami'); |
||
523 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
524 | try { |
||
525 | if (function_exists('mb_convert_encoding')) { |
||
526 | $current_user = @mb_convert_encoding($current_user, "UTF-8", "cp850"); |
||
527 | } else if (function_exists('iconv')) { |
||
528 | $current_user = @iconv("cp850", "UTF-8", $current_user); |
||
529 | } |
||
530 | } catch (\Exception $e) {} |
||
531 | if (function_exists('mb_strtoupper')) { |
||
532 | $current_user = mb_strtoupper($current_user); // just cosmetics |
||
533 | } |
||
534 | } |
||
535 | if (!$current_user) { |
||
536 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
537 | // Windows on an IIS server: |
||
538 | // getenv('USERNAME') MARSCHALL$ (That is the "machine account", see https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities#accessing-the-network ) |
||
539 | // get_current_user() DefaultAppPool |
||
540 | // exec('whoami') iis apppool\defaultapppool |
||
541 | // Windows with XAMPP: |
||
542 | // getenv('USERNAME') dmarschall |
||
543 | // get_current_user() dmarschall (even if script has a different NTFS owner!) |
||
544 | // exec('whoami') hickelsoft\dmarschall |
||
545 | $current_user = get_current_user(); |
||
546 | if (!$current_user) { |
||
547 | $current_user = getenv('USERNAME'); |
||
548 | $current_user = mb_strtoupper($current_user); // just cosmetics |
||
549 | } |
||
550 | } else { |
||
551 | // On Linux: |
||
552 | $current_user = exec('id -un'); |
||
553 | if (!$current_user) { |
||
554 | // PHP'S get_current_user() will get the owner of the PHP script, not the process owner! |
||
555 | // We want the process owner, so we use posix_geteuid() preferably. |
||
556 | if (function_exists('posix_geteuid')) { |
||
557 | $uid = posix_geteuid(); |
||
558 | } else { |
||
559 | $temp_file = tempnam(sys_get_temp_dir(), 'TMP'); |
||
560 | if ($temp_file !== false) { |
||
561 | $uid = fileowner($temp_file); |
||
562 | if ($uid === false) $uid = -1; |
||
563 | @unlink($temp_file); |
||
564 | } else { |
||
565 | $uid = -1; |
||
566 | } |
||
567 | } |
||
568 | if ($uid >= 0) { |
||
569 | $current_user = '#' . $uid; |
||
570 | if (function_exists('posix_getpwuid')) { |
||
571 | $userinfo = posix_getpwuid($uid); // receive username from the UID (requires read access to /etc/passwd) |
||
572 | if ($userinfo !== false) $current_user = $userinfo['name']; |
||
573 | } |
||
574 | } else { |
||
575 | $current_user = get_current_user(); |
||
576 | } |
||
577 | } |
||
578 | } |
||
579 | } |
||
580 | return $current_user ?: false; |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * @param string $path |
||
585 | * @return bool |
||
586 | */ |
||
587 | function isFileOrPathWritable(string $path): bool { |
||
588 | if ($writable_file = (file_exists($path) && is_writable($path))) return true; |
||
589 | if ($writable_directory = (!file_exists($path) && is_writable(dirname($path)))) return true; |
||
590 | return false; |
||
591 | } |