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