| @@ -14,14 +14,14 @@ | ||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function startsWith(string $haystack, string $needle): bool |
| 17 | 17 | { |
| 18 | - return str_starts_with($haystack, $needle); | |
| 18 | + return $needle === '' || str_starts_with($haystack, $needle); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function endsWith(string $haystack, string $needle): bool |
| 22 | 22 | { |
| 23 | - return str_ends_with($haystack, $needle); | |
| 23 | + return $needle === '' || str_ends_with($haystack, $needle); | |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * @throws Exception |
| @@ -29,21 +29,28 @@ | ||
| 29 | 29 | public function getRandomPassword(int $length = 32): string |
| 30 | 30 | { |
| 31 | 31 | $bytes = $this->php->opensslRandomPseudoBytes($length + 1, $strong); |
| 32 | 32 | |
| 33 | - if ($bytes === false || $strong !== true) { | |
| 34 | - throw new Exception('Unable to generate secure token from OpenSSL.'); | |
| 33 | + if ($bytes !== false && $strong === true) { | |
| 34 | + return substr(preg_replace('/[^a-zA-Z0-9]/', '', base64_encode($bytes)), 0, $length); | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - return substr(preg_replace('/[^a-zA-Z0-9]/', '', base64_encode($bytes)), 0, $length); | |
| 37 | + throw new Exception('Unable to generate secure token from OpenSSL.'); | |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | public function getCurrentUrl(): string |
| 41 | 41 | { |
| 42 | - $requestUri = $_SERVER['REQUEST_URI'] ?? $_SERVER['PHP_SELF']; | |
| 43 | - $secure = (($_SERVER['HTTPS'] ?? '') === 'on') ? 's' : ''; | |
| 44 | - $scheme = explode('/', strtolower($_SERVER['SERVER_PROTOCOL']))[0] . $secure; | |
| 45 | - $port = ((int) $_SERVER['SERVER_PORT'] === 80) ? '' : ':' . $_SERVER['SERVER_PORT']; | |
| 42 | + if (isset($_SERVER['REQUEST_URI']) === false) { | |
| 43 | + $serverRequestUri = $_SERVER['PHP_SELF']; | |
| 44 | + } else { | |
| 45 | + $serverRequestUri = $_SERVER['REQUEST_URI']; | |
| 46 | + } | |
| 46 | 47 | |
| 47 | - return $scheme . '://' . $_SERVER['SERVER_NAME'] . $port . $requestUri; | |
| 48 | + $https = $_SERVER['HTTPS'] ?? ''; | |
| 49 | + $secure = $https === 'on' ? 's' : ''; | |
| 50 | + $protocols = explode('/', strtolower($_SERVER['SERVER_PROTOCOL'])); | |
| 51 | + $protocol = $protocols[0] . $secure; | |
| 52 | + $port = ((int) $_SERVER['SERVER_PORT'] === 80) ? '' : (':' . $_SERVER['SERVER_PORT']); | |
| 53 | + | |
| 54 | + return $protocol . '://' . $_SERVER['SERVER_NAME'] . $port . $serverRequestUri; | |
| 48 | 55 | } |
| 49 | 56 | } |