| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Get a URL suitable for matching in the database |
| 5 |
*/ |
| 6 |
class Red_Url_Match { |
| 7 |
/** |
| 8 |
* URL |
| 9 |
* |
| 10 |
* @var String |
| 11 |
*/ |
| 12 |
private $url; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
* |
| 17 |
* @param String $url The URL to match. |
| 18 |
*/ |
| 19 |
public function __construct( $url ) { |
| 20 |
$this->url = $url; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get the plain 'matched' URL: |
| 25 |
* |
| 26 |
* - Lowercase |
| 27 |
* - No trailing slashes |
| 28 |
* |
| 29 |
* @return string URL |
| 30 |
*/ |
| 31 |
public function get_url() { |
| 32 |
// Remove query params, and decode any encoded characters |
| 33 |
$url = new Red_Url_Path( $this->url ); |
| 34 |
$path = $url->get_without_trailing_slash(); |
| 35 |
|
| 36 |
// URL encode |
| 37 |
$decode = [ |
| 38 |
'/', |
| 39 |
':', |
| 40 |
'[', |
| 41 |
']', |
| 42 |
'@', |
| 43 |
'~', |
| 44 |
',', |
| 45 |
'(', |
| 46 |
')', |
| 47 |
';', |
| 48 |
]; |
| 49 |
|
| 50 |
// URL encode everything - this converts any i10n to the proper encoding |
| 51 |
$path = rawurlencode( $path ); |
| 52 |
|
| 53 |
// We also converted things we dont want encoding, such as a /. Change these back |
| 54 |
foreach ( $decode as $char ) { |
| 55 |
$path = str_replace( rawurlencode( $char ), $char, $path ); |
| 56 |
} |
| 57 |
|
| 58 |
// Lowercase everything |
| 59 |
$path = Red_Url_Path::to_lower( $path ); |
| 60 |
|
| 61 |
return $path ? $path : '/'; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the URL with parameters re-ordered into alphabetical order |
| 66 |
* |
| 67 |
* @return String |
| 68 |
*/ |
| 69 |
public function get_url_with_params() { |
| 70 |
$query = new Red_Url_Query( $this->url, new Red_Source_Flags( [ Red_Source_Flags::FLAG_CASE => true ] ) ); |
| 71 |
|
| 72 |
return $query->get_url_with_query( $this->get_url() ); |
| 73 |
} |
| 74 |
} |
| 75 |
|