| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Util.php | |
| 4 | + * | |
| 5 | + * The Util class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Util; |
| @@ -6,44 +19,85 @@ | ||
| 6 | 19 | |
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Wrapper\Php; |
| 9 | 22 | |
| 23 | +/** | |
| 24 | + * Class Util | |
| 25 | + * | |
| 26 | + * @package UserAccessManager\Util | |
| 27 | + */ | |
| 10 | 28 | class Util |
| 11 | 29 | { |
| 12 | - public function __construct(private Php $php) | |
| 30 | + /** | |
| 31 | + * @var Php | |
| 32 | + */ | |
| 33 | + private $php; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Util constructor. | |
| 37 | + * @param Php $php | |
| 38 | + */ | |
| 39 | + public function __construct(Php $php) | |
| 13 | 40 | { |
| 41 | + $this->php = $php; | |
| 14 | 42 | } |
| 15 | 43 | |
| 44 | + /** | |
| 45 | + * Checks if a string starts with the given needle. | |
| 46 | + * @param string $haystack The haystack. | |
| 47 | + * @param string $needle The needle. | |
| 48 | + * @return bool | |
| 49 | + */ | |
| 16 | 50 | public function startsWith(string $haystack, string $needle): bool |
| 17 | 51 | { |
| 18 | - return str_starts_with($haystack, $needle); | |
| 52 | + return $needle === '' || strpos($haystack, $needle) === 0; | |
| 19 | 53 | } |
| 20 | 54 | |
| 55 | + /** | |
| 56 | + * Checks if a string ends with the given needle. | |
| 57 | + * @param string $haystack | |
| 58 | + * @param string $needle | |
| 59 | + * @return bool | |
| 60 | + */ | |
| 21 | 61 | public function endsWith(string $haystack, string $needle): bool |
| 22 | 62 | { |
| 23 | - return str_ends_with($haystack, $needle); | |
| 63 | + return $needle === '' || substr($haystack, -strlen($needle)) === $needle; | |
| 24 | 64 | } |
| 25 | 65 | |
| 26 | 66 | /** |
| 67 | + * Generates and returns a random password. | |
| 68 | + * @param int $length | |
| 69 | + * @return string | |
| 27 | 70 | * @throws Exception |
| 28 | 71 | */ |
| 29 | - public function getRandomPassword(int $length = 32): string | |
| 72 | + public function getRandomPassword($length = 32): string | |
| 30 | 73 | { |
| 31 | 74 | $bytes = $this->php->opensslRandomPseudoBytes($length + 1, $strong); |
| 32 | 75 | |
| 33 | - if ($bytes === false || $strong !== true) { | |
| 76 | + if ($bytes !== false && $strong === true) { | |
| 77 | + return substr(preg_replace('/[^a-zA-Z0-9]/', '', base64_encode($bytes)), 0, $length); | |
| 78 | + } else { | |
| 34 | 79 | throw new Exception('Unable to generate secure token from OpenSSL.'); |
| 35 | 80 | } |
| 36 | - | |
| 37 | - return substr(preg_replace('/[^a-zA-Z0-9]/', '', base64_encode($bytes)), 0, $length); | |
| 38 | 81 | } |
| 39 | 82 | |
| 83 | + /** | |
| 84 | + * Returns the current url. | |
| 85 | + * @return string | |
| 86 | + */ | |
| 40 | 87 | public function getCurrentUrl(): string |
| 41 | 88 | { |
| 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']; | |
| 89 | + if (isset($_SERVER['REQUEST_URI']) === false) { | |
| 90 | + $serverRequestUri = $_SERVER['PHP_SELF']; | |
| 91 | + } else { | |
| 92 | + $serverRequestUri = $_SERVER['REQUEST_URI']; | |
| 93 | + } | |
| 46 | 94 | |
| 47 | - return $scheme . '://' . $_SERVER['SERVER_NAME'] . $port . $requestUri; | |
| 95 | + $https = $_SERVER['HTTPS'] ?? ''; | |
| 96 | + $secure = $https === 'on' ? 's' : ''; | |
| 97 | + $protocols = explode('/', strtolower($_SERVER['SERVER_PROTOCOL'])); | |
| 98 | + $protocol = $protocols[0] . $secure; | |
| 99 | + $port = ((int) $_SERVER['SERVER_PORT'] === 80) ? '' : (':' . $_SERVER['SERVER_PORT']); | |
| 100 | + | |
| 101 | + return $protocol . '://' . $_SERVER['SERVER_NAME'] . $port . $serverRequestUri; | |
| 48 | 102 | } |
| 49 | 103 | } |