| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\UrlException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Decodes a base64 encoded data. |
| 9 |
* |
| 10 |
* @param string $data The encoded data. |
| 11 |
* @param bool $strict If the strict parameter is set to TRUE |
| 12 |
* then the base64_decode function will return |
| 13 |
* FALSE if the input contains character from outside the base64 |
| 14 |
* alphabet. Otherwise invalid characters will be silently discarded. |
| 15 |
* @return string Returns the decoded data. The returned data may be |
| 16 |
* binary. |
| 17 |
* @throws UrlException |
| 18 |
* |
| 19 |
*/ |
| 20 |
function base64_decode(string $data, bool $strict = false): string |
| 21 |
{ |
| 22 |
error_clear_last(); |
| 23 |
$result = \base64_decode($data, $strict); |
| 24 |
if ($result === false) { |
| 25 |
throw UrlException::createFromPhpError(); |
| 26 |
} |
| 27 |
return $result; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* get_headers returns an array with the headers sent |
| 33 |
* by the server in response to a HTTP request. |
| 34 |
* |
| 35 |
* @param string $url The target URL. |
| 36 |
* @param int $format If the optional format parameter is set to non-zero, |
| 37 |
* get_headers parses the response and sets the |
| 38 |
* array's keys. |
| 39 |
* @param resource $context A valid context resource created with |
| 40 |
* stream_context_create. |
| 41 |
* @return array Returns an indexed or associative array with the headers. |
| 42 |
* @throws UrlException |
| 43 |
* |
| 44 |
*/ |
| 45 |
function get_headers(string $url, int $format = 0, $context = null): array |
| 46 |
{ |
| 47 |
error_clear_last(); |
| 48 |
if ($context !== null) { |
| 49 |
$result = \get_headers($url, $format, $context); |
| 50 |
} else { |
| 51 |
$result = \get_headers($url, $format); |
| 52 |
} |
| 53 |
if ($result === false) { |
| 54 |
throw UrlException::createFromPhpError(); |
| 55 |
} |
| 56 |
return $result; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* This function parses a URL and returns an associative array containing any |
| 62 |
* of the various components of the URL that are present. |
| 63 |
* The values of the array elements are not URL decoded. |
| 64 |
* |
| 65 |
* This function is not meant to validate |
| 66 |
* the given URL, it only breaks it up into the above listed parts. Partial |
| 67 |
* URLs are also accepted, parse_url tries its best to |
| 68 |
* parse them correctly. |
| 69 |
* |
| 70 |
* @param string $url The URL to parse. Invalid characters are replaced by |
| 71 |
* _. |
| 72 |
* @param int $component Specify one of PHP_URL_SCHEME, |
| 73 |
* PHP_URL_HOST, PHP_URL_PORT, |
| 74 |
* PHP_URL_USER, PHP_URL_PASS, |
| 75 |
* PHP_URL_PATH, PHP_URL_QUERY |
| 76 |
* or PHP_URL_FRAGMENT to retrieve just a specific |
| 77 |
* URL component as a string (except when |
| 78 |
* PHP_URL_PORT is given, in which case the return |
| 79 |
* value will be an integer). |
| 80 |
* @return mixed On seriously malformed URLs, parse_url. |
| 81 |
* |
| 82 |
* If the component parameter is omitted, an |
| 83 |
* associative array is returned. At least one element will be |
| 84 |
* present within the array. Potential keys within this array are: |
| 85 |
* |
| 86 |
* |
| 87 |
* |
| 88 |
* scheme - e.g. http |
| 89 |
* |
| 90 |
* |
| 91 |
* |
| 92 |
* |
| 93 |
* host |
| 94 |
* |
| 95 |
* |
| 96 |
* |
| 97 |
* |
| 98 |
* port |
| 99 |
* |
| 100 |
* |
| 101 |
* |
| 102 |
* |
| 103 |
* user |
| 104 |
* |
| 105 |
* |
| 106 |
* |
| 107 |
* |
| 108 |
* pass |
| 109 |
* |
| 110 |
* |
| 111 |
* |
| 112 |
* |
| 113 |
* path |
| 114 |
* |
| 115 |
* |
| 116 |
* |
| 117 |
* |
| 118 |
* query - after the question mark ? |
| 119 |
* |
| 120 |
* |
| 121 |
* |
| 122 |
* |
| 123 |
* fragment - after the hashmark # |
| 124 |
* |
| 125 |
* |
| 126 |
* |
| 127 |
* |
| 128 |
* If the component parameter is specified, |
| 129 |
* parse_url returns a string (or an |
| 130 |
* integer, in the case of PHP_URL_PORT) |
| 131 |
* instead of an array. If the requested component doesn't exist |
| 132 |
* within the given URL, NULL will be returned. |
| 133 |
* @throws UrlException |
| 134 |
* |
| 135 |
*/ |
| 136 |
function parse_url(string $url, int $component = -1) |
| 137 |
{ |
| 138 |
error_clear_last(); |
| 139 |
$result = \parse_url($url, $component); |
| 140 |
if ($result === false) { |
| 141 |
throw UrlException::createFromPhpError(); |
| 142 |
} |
| 143 |
return $result; |
| 144 |
} |
| 145 |
|