Rev 56 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 56 | Rev 63 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * PHP Utilities - Misc functions |
4 | * PHP Utilities - Misc functions |
5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
6 | * Revision: 2022-12-27 |
6 | * Revision: 2023-02-27 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
Line 139... | Line 139... | ||
139 | function isInternetExplorer() { |
139 | function isInternetExplorer() { |
140 | // see also includes/oidplus_base.js |
140 | // see also includes/oidplus_base.js |
141 | $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
141 | $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
142 | return ((strpos($ua,'MSIE ') !== false) || (strpos($ua,'Trident/') !== false)); |
142 | return ((strpos($ua,'MSIE ') !== false) || (strpos($ua,'Trident/') !== false)); |
143 | } |
143 | } |
- | 144 | ||
- | 145 | if (!function_exists('str_ends_with')) { |
|
- | 146 | // PHP 7.x compatibility |
|
- | 147 | function str_ends_with($haystack, $needle) { |
|
- | 148 | $length = strlen($needle); |
|
- | 149 | return $length > 0 ? substr($haystack, -$length) === $needle : true; |
|
- | 150 | } |
|
- | 151 | } |
|
- | 152 | ||
- | 153 | if (!function_exists('str_starts_with')) { |
|
- | 154 | // PHP 7.x compatibility |
|
- | 155 | function str_starts_with($haystack, $needle) { |
|
- | 156 | return strpos($haystack, $needle) === 0; |
|
- | 157 | } |
|
- | 158 | } |
|
- | 159 | ||
- | 160 | function random_bytes_ex($len, $raw=true, $force_cryptographically_secure=true) { |
|
- | 161 | if ($len === 0) return ''; |
|
- | 162 | assert($len > 0); |
|
- | 163 | ||
- | 164 | if (function_exists('random_bytes')) { |
|
- | 165 | try { |
|
- | 166 | $a = random_bytes($len); |
|
- | 167 | } catch (Exception $e) { $a = null; } |
|
- | 168 | if ($a) return $raw ? $a : bin2hex($a); |
|
- | 169 | } |
|
- | 170 | ||
- | 171 | if (function_exists('openssl_random_pseudo_bytes')) { |
|
- | 172 | try { |
|
- | 173 | $a = openssl_random_pseudo_bytes($len); |
|
- | 174 | } catch (Exception $e) { $a = null; } |
|
- | 175 | if ($a) return $raw ? $a : bin2hex($a); |
|
- | 176 | } |
|
- | 177 | ||
- | 178 | if (function_exists('mcrypt_create_iv') && defined('MCRYPT_DEV_RANDOM')) { |
|
- | 179 | try { |
|
- | 180 | $a = bin2hex(mcrypt_create_iv($len, MCRYPT_DEV_RANDOM)); |
|
- | 181 | } catch (Exception $e) { $a = null; } |
|
- | 182 | if ($a) return $raw ? $a : bin2hex($a); |
|
- | 183 | } |
|
- | 184 | ||
- | 185 | if ($force_cryptographically_secure) { |
|
- | 186 | $msg = 'Cannot find a fitting Cryptographically Secure Random Number Generator (CSRNG).'; |
|
- | 187 | if (version_compare(PHP_VERSION, '8.2.0') >= 0) { |
|
- | 188 | throw new \Random\RandomException($msg); |
|
- | 189 | } else { |
|
- | 190 | throw new \Exception($msg); |
|
- | 191 | } |
|
- | 192 | } |
|
- | 193 | ||
- | 194 | if (function_exists('mcrypt_create_iv') && defined('MCRYPT_DEV_URANDOM')) { |
|
- | 195 | // /dev/urandom uses the same entropy pool than /dev/random, but if there is not enough data |
|
- | 196 | // then the security is lowered. |
|
- | 197 | try { |
|
- | 198 | $a = bin2hex(mcrypt_create_iv($len, MCRYPT_DEV_URANDOM)); |
|
- | 199 | } catch (Exception $e) { $a = null; } |
|
- | 200 | if ($a) return $raw ? $a : bin2hex($a); |
|
- | 201 | } |
|
- | 202 | ||
- | 203 | if (function_exists('mcrypt_create_iv') && defined('MCRYPT_RAND')) { |
|
- | 204 | try { |
|
- | 205 | $a = bin2hex(mcrypt_create_iv($len, MCRYPT_RAND)); |
|
- | 206 | } catch (Exception $e) { $a = null; } |
|
- | 207 | if ($a) return $raw ? $a : bin2hex($a); |
|
- | 208 | } |
|
- | 209 | ||
- | 210 | // Fallback to non-secure RNG |
|
- | 211 | $a = ''; |
|
- | 212 | while (strlen($a) < $len*2) { |
|
- | 213 | $a .= sha1(uniqid((string)mt_rand(), true)); |
|
- | 214 | } |
|
- | 215 | $a = substr($a, 0, $len*2); |
|
- | 216 | return $raw ? hex2bin($a) : $a; |
|
- | 217 | } |