PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.32
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.32
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Http / URL.php

URL.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.32, at vendor/wpfluent/framework/src/WPFluent/Http/URL.php

175 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Http;
4
5 use Exception;
6 use FluentBoards\Framework\Support\DateTime;
7
8 class URL
9 {
10 protected $encrypter = null;
11
12 public function __construct($encrypter)
13 {
14 $this->encrypter = $encrypter;
15 }
16
17 /**
18 * Get the current URL
19 *
20 * @return string
21 */
22 public function current()
23 {
24 return get_site_url() . rtrim($_SERVER['REQUEST_URI'], '/');
25 }
26
27 /**
28 * Parse a url from uncompiled route
29 *
30 * @param string $url
31 * @param array $params
32 * @return string
33 */
34 public function parse(string $url, array $params)
35 {
36 $pattern = '#\{[a-zA-Z0-9-_]+\}#';
37
38 return preg_replace_callback($pattern, function($matches) use ($params) {
39 foreach ($matches as $match) {
40 $match = str_replace(['{', '}'], ['', ''], $match);
41 if (isset($params[$match])) {
42 return $params[$match];
43 }
44 }
45 }, $url);
46 }
47
48 /**
49 * Sign the current url.
50 *
51 * @param array $params
52 * @return string
53 */
54 public function signCurrentUrl($params = [])
55 {
56 return $this->sign($this->current(), $params);
57 }
58
59 /**
60 * Sign a URL
61 *
62 * @param string $url
63 * @return string
64 */
65 public function sign($url, $params = [])
66 {
67 $parts = parse_url($url);
68
69 $url = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
70
71 parse_str($parts['query'] ?? '', $query);
72
73 $params = $this->validateExpiryTime($params + $query);
74
75 $params = $this->encrypter->encrypt(http_build_query($params));
76
77 $signature = hash_hmac('sha256', $params, $this->encrypter->getKey());
78
79 return $url . '?_data=' . $params . '&_signature=' . $signature;
80 }
81
82 /**
83 * Normalize the expiry time.
84 *
85 * @param array $params
86 * @return array
87 */
88 public function validateExpiryTime($params)
89 {
90 if (isset($params['expires_at'])) {
91 if (is_string($params['expires_at'])) {
92 $params['expires_at'] = strtotime($params['expires_at']);
93 } elseif ($params['expires_at'] instanceof DateTime) {
94 $params['expires_at'] = $params['expires_at']->getTimestamp();
95 }
96
97 if (!is_numeric($params['expires_at']) || $params['expires_at'] < time()) {
98 throw new Exception('The expiry time has passed or invalid.');
99 }
100 }
101
102 return $params;
103 }
104
105 /**
106 * Validate a URL
107 *
108 * @param string $url
109 * @return mixed (false or array)
110 */
111 public function validate($url)
112 {
113 if (!$query = $this->parseUrlAndGetQuery($url)) {
114 return false;
115 }
116
117 if (!$query['_data'] || !$query['_signature']) {
118 return false;
119 }
120
121 return $this->verifySignature($query['_data'], $query['_signature']);
122 }
123
124 /**
125 * Parse query string from the url.
126 *
127 * @param string $url
128 * @return mixed (bool or array)
129 */
130 public function parseUrlAndGetQuery($url)
131 {
132 $parts = parse_url($url);
133
134 if (!isset($parts['query'])) {
135 return false;
136 }
137
138 parse_str($parts['query'], $query);
139
140 return $query;
141 }
142
143 /**
144 * Verify the signature.
145 *
146 * @param array $data
147 * @param string $signature
148 * @return mixed (false or array)
149 */
150 public function verifySignature($data, $signature)
151 {
152 $expected = hash_hmac('sha256', $data, $this->encrypter->getKey());
153
154 if (!hash_equals($expected, $signature)) return false;
155
156 parse_str($this->encrypter->decrypt(urldecode($data)), $params);
157
158 if (isset($params['expires_at']) && time() > $params['expires_at']) {
159 return false;
160 }
161
162 return $params;
163 }
164
165 /**
166 * Returns the string representation of the URL object
167 *
168 * @return string Current URL.
169 */
170 public function __toString()
171 {
172 return $this->current();
173 }
174 }
175