PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
2.1.0 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 All 42 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.91.6, at vendor/wpfluent/framework/src/WPFluent/Http/URL.php

283 lines 5.7 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\Util;
7 use FluentBoards\Framework\Foundation\App;
8 use FluentBoards\Framework\Support\DateTime;
9 use FluentBoards\Framework\Support\UriTemplate;
10
11 class URL
12 {
13 protected $encrypter = null;
14
15 public function __construct($encrypter = null)
16 {
17 $this->encrypter = $encrypter ?: App::make('encrypter');
18 }
19
20 /**
21 * Get the current URL
22 *
23 * @return string
24 */
25 public function current()
26 {
27 return get_site_url() . rtrim($_SERVER['REQUEST_URI'], '/');
28 }
29
30 /**
31 * Parse a url from uncompiled route
32 *
33 * @param string $url
34 * @param array $params
35 * @return string
36 */
37 public function parse(string $url, array $params)
38 {
39 return UriTemplate::expand($url, $params);
40 }
41
42 /**
43 * Sign the current url.
44 *
45 * @param array $params
46 * @return string
47 */
48 public function signCurrentUrl($params = [])
49 {
50 return $this->sign($this->current(), $params);
51 }
52
53 /**
54 * Sign a URL
55 *
56 * @param string $url
57 * @param array $params
58 * @return string
59 */
60 public function sign($url, $params = [])
61 {
62 if (!preg_match('#^(http|https)://#', $url)) {
63 $config = App::config();
64 $slug = $config->get('app.slug');
65 $version = $config->get('app.rest_version');
66 $url = sprintf('%s%s/%s/%s',rest_url(), $slug, $version, $url);
67 }
68
69 $parts = parse_url($url);
70
71 $url = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
72
73 parse_str($parts['query'] ?? '', $query);
74
75 $params = $this->validateExpiryTime($params + $query);
76
77 $params = $this->encrypter->encrypt(http_build_query($params));
78
79 $signature = hash_hmac('sha256', $params, $this->encrypter->getKey());
80
81 return $url . '?_data=' . $params . '&_signature=' . $signature;
82 }
83
84 /**
85 * Normalize the expiry time.
86 *
87 * @param array $params
88 * @return array
89 */
90 public function validateExpiryTime($params)
91 {
92 if (isset($params['expires_at'])) {
93 if (is_string($params['expires_at'])) {
94 $params['expires_at'] = strtotime($params['expires_at']);
95 } elseif ($params['expires_at'] instanceof DateTime) {
96 $params['expires_at'] = $params['expires_at']->getTimestamp();
97 } elseif (is_numeric($params['expires_at'])) {
98 if ($params['expires_at'] <= time()) {
99 $params['expires_at'] = time() + (int) $params['expires_at'];
100 }
101 } else {
102 throw new Exception('The expiry time is invalid or has passed.');
103 }
104 }
105
106 return $params;
107 }
108
109 /**
110 * Validate a URL
111 *
112 * @param string $url
113 * @return mixed (false or array)
114 */
115 public function validate($url)
116 {
117 if (!$query = $this->parseUrlAndGetQuery($url)) {
118 return false;
119 }
120
121 if (!isset($query['_data']) || !isset($query['_signature'])) {
122 return false;
123 }
124
125 return $this->verifySignature($query['_data'], $query['_signature']);
126 }
127
128 /**
129 * Parse query string from the url.
130 *
131 * @param string $url
132 * @return mixed (bool or array)
133 */
134 public function parseUrlAndGetQuery($url)
135 {
136 $parts = parse_url($url);
137
138 if (!isset($parts['query'])) {
139 return false;
140 }
141
142 parse_str($parts['query'], $query);
143
144 return $query;
145 }
146
147 /**
148 * Verify the signature.
149 *
150 * @param array $data
151 * @param string $signature
152 * @return mixed (bool or array)
153 */
154 public function verifySignature($data, $signature)
155 {
156 $expected = hash_hmac('sha256', $data, $this->encrypter->getKey());
157
158 if (!hash_equals($expected, $signature)) return false;
159
160 parse_str($this->encrypter->decrypt($data), $params);
161
162 if (isset($params['expires_at']) && time() > $params['expires_at']) {
163 return false;
164 }
165
166 return empty($params) ? true : $params;
167 }
168
169 /**
170 * Helper method for home_url.
171 *
172 * @return string
173 */
174 public function wp($path = '')
175 {
176 $wp = get_option('siteUrl');
177
178 if ($path) {
179 $wp .= '/' . trim($path, '/');
180 }
181
182 return $wp;
183 }
184
185 /**
186 * Retrieve the wp-content URL.
187 *
188 * Note: The wp-content directory is renamable by devs so it's not
189 * guaranteed to be wp-content, so use this method for safety.
190 *
191 * @param string $path
192 * @return string
193 */
194 public function content($path = '')
195 {
196 return content_url($path);
197 }
198
199 /**
200 * Retrieve the wp-content/plugins URL.
201 *
202 * @param string $path
203 * @return string
204 */
205 public function plugins($path = '')
206 {
207 return $this->content('/plugins/' . ltrim($path, '/'));
208 }
209
210 /**
211 * Retrieve the wp-content/plugins URL.
212 *
213 * @param string $path
214 * @return string
215 */
216 public function plugin($path = '')
217 {
218 $pluginUrl = rtrim(plugin_dir_url(
219 App::make('__pluginfile__')
220 ), '/');
221
222 if ($path) {
223 $pluginUrl .= '/' . trim($path, '/');
224 }
225
226 return $pluginUrl;
227 }
228
229 /**
230 * Retrieve the wp-content/themes URL.
231 *
232 * @param string $path
233 * @return string
234 */
235 public function themes($path = '')
236 {
237 return $this->content('/themes/' . ltrim($path, '/'));
238 }
239
240 /**
241 * Retrieve the wp-content/uploads URL.
242 *
243 * @param string $path
244 * @return string
245 */
246 public function uploads($path = '')
247 {
248 return $this->content('/uploads/' . ltrim($path, '/'));
249 }
250
251 /**
252 * Retrieve the site/home URL.
253 *
254 * @param string $path
255 * @return string
256 */
257 public function home($path = '', $scheme = null)
258 {
259 return site_url($path, $scheme);
260 }
261
262 /**
263 * Generate a URL from a file path.
264 *
265 * @param string $path
266 * @return string
267 */
268 public function fromFile($path)
269 {
270 return Util::pathToUrl($path);
271 }
272
273 /**
274 * Returns the string representation of the URL object
275 *
276 * @return string Current URL.
277 */
278 public function __toString()
279 {
280 return $this->current();
281 }
282 }
283