PluginProbe
User Access Manager / 2.1.0
User Access Manager v2.1.0
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.1.0, at src/Util/Util.php

108 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 namespace UserAccessManager\Util;
16
17 use UserAccessManager\Wrapper\Php;
18
19 /**
20 * Class Util
21 *
22 * @package UserAccessManager\Util
23 */
24 class Util
25 {
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)
37 {
38 $this->php = $php;
39 }
40
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)
50 {
51 return $needle === '' || strpos($haystack, $needle) === 0;
52 }
53
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)
63 {
64 return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
65 }
66
67 /**
68 * Generates and returns a random password.
69 *
70 * @param int $length
71 *
72 * @return string
73 *
74 * @throws \Exception
75 */
76 public function getRandomPassword($length = 32)
77 {
78 $bytes = $this->php->opensslRandomPseudoBytes($length + 1, $strong);
79
80 if ($bytes !== false && $strong === true) {
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.');
84 }
85 }
86
87 /**
88 * Returns the current url.
89 *
90 * @return string
91 */
92 public function getCurrentUrl()
93 {
94 if (isset($_SERVER['REQUEST_URI']) === false) {
95 $serverRequestUri = $_SERVER['PHP_SELF'];
96 } else {
97 $serverRequestUri = $_SERVER['REQUEST_URI'];
98 }
99
100 $secure = empty($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] === 'on') ? 's' : '';
101 $protocols = explode('/', strtolower($_SERVER['SERVER_PROTOCOL']));
102 $protocol = $protocols[0].$secure;
103 $port = ((int)$_SERVER['SERVER_PORT'] === 80) ? '' : (':'.$_SERVER['SERVER_PORT']);
104
105 return $protocol.'://'.$_SERVER['SERVER_NAME'].$port.$serverRequestUri;
106 }
107 }
108