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