1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebThumbnailer\Utils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class UrlUtils |
7
|
|
|
* |
8
|
|
|
* Util class for operations on URL strings. |
9
|
|
|
* |
10
|
|
|
* @package WebThumbnailer\Utils |
11
|
|
|
*/ |
12
|
|
|
class UrlUtils |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Extract the domains from an URL. |
16
|
|
|
* |
17
|
|
|
* @param string $url Given URL. |
18
|
|
|
* |
19
|
|
|
* @return string Extracted domains, lowercase. |
20
|
|
|
*/ |
21
|
|
|
public static function getDomain($url) |
22
|
|
|
{ |
23
|
|
|
if (! parse_url($url, PHP_URL_SCHEME)) { |
24
|
|
|
$url = 'http://' . $url; |
25
|
|
|
} |
26
|
|
|
return strtolower(parse_url($url, PHP_URL_HOST)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Generate a relative URL from absolute local path. |
31
|
|
|
* Example: |
32
|
|
|
* - /home/website/resources/file.txt |
33
|
|
|
* ====> |
34
|
|
|
* - resources/file.txt |
35
|
|
|
* |
36
|
|
|
* @param array $server $_SERVER array. |
37
|
|
|
* @param string $path Absolute path to transform. |
38
|
|
|
* |
39
|
|
|
* @return string Relative path. |
40
|
|
|
*/ |
41
|
|
|
public static function generateRelativeUrlFromPath($server, $path) |
42
|
|
|
{ |
43
|
|
|
if (isset($server['DOCUMENT_ROOT'])) { |
44
|
|
|
$root = ! empty($server['DOCUMENT_ROOT']) ? rtrim($server['DOCUMENT_ROOT'], '/') .'/' : ''; |
45
|
|
|
$path = substr($path, strlen($root)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (isset($server['SCRIPT_NAME']) && preg_match('#(.*/)\w+\.php$#', $server['SCRIPT_NAME'], $matches) > 0) { |
49
|
|
|
$path = substr($path, strlen($matches[1])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $path; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the file extension from a URL. |
57
|
|
|
* |
58
|
|
|
* @param string $url given URL. |
59
|
|
|
* |
60
|
|
|
* @return string|bool File extension or false if not found. |
61
|
|
|
*/ |
62
|
|
|
public static function getUrlFileExtension($url) |
63
|
|
|
{ |
64
|
|
|
$path = parse_url($url, PHP_URL_PATH); |
65
|
|
|
if (preg_match('/\.(\w+)$/i', $path, $match) > 0) { |
66
|
|
|
return strtolower($match[1]); |
67
|
|
|
} |
68
|
|
|
return ''; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|