PluginProbe
User Access Manager / 2.2.12
User Access Manager v2.2.12
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Util / Util.php

Util.php in User Access Manager 2.2.12, at src/Util/Util.php

104 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Util;
19
20 use Exception;
21 use UserAccessManager\Wrapper\Php;
22
23 /**
24 * Class Util
25 *
26 * @package UserAccessManager\Util
27 */
28 class Util
29 {
30 /**
31 * @var Php
32 */
33 private $php;
34
35 /**
36 * Util constructor.
37 * @param Php $php
38 */
39 public function __construct(Php $php)
40 {
41 $this->php = $php;
42 }
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 */
50 public function startsWith(string $haystack, string $needle): bool
51 {
52 return $needle === '' || strpos($haystack, $needle) === 0;
53 }
54
55 /**
56 * Checks if a string ends with the given needle.
57 * @param string $haystack
58 * @param string $needle
59 * @return bool
60 */
61 public function endsWith(string $haystack, string $needle): bool
62 {
63 return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
64 }
65
66 /**
67 * Generates and returns a random password.
68 * @param int $length
69 * @return string
70 * @throws Exception
71 */
72 public function getRandomPassword($length = 32): string
73 {
74 $bytes = $this->php->opensslRandomPseudoBytes($length + 1, $strong);
75
76 if ($bytes !== false && $strong === true) {
77 return substr(preg_replace('/[^a-zA-Z0-9]/', '', base64_encode($bytes)), 0, $length);
78 } else {
79 throw new Exception('Unable to generate secure token from OpenSSL.');
80 }
81 }
82
83 /**
84 * Returns the current url.
85 * @return string
86 */
87 public function getCurrentUrl(): string
88 {
89 if (isset($_SERVER['REQUEST_URI']) === false) {
90 $serverRequestUri = $_SERVER['PHP_SELF'];
91 } else {
92 $serverRequestUri = $_SERVER['REQUEST_URI'];
93 }
94
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;
102 }
103 }
104