|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JeroenDesloovere\VCard\Util; |
|
4
|
|
|
|
|
5
|
|
|
use JeroenDesloovere\VCard\Exception\InvalidUrlException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class GeneralUtil |
|
9
|
|
|
* |
|
10
|
|
|
* @package JeroenDesloovere\VCard\Util |
|
11
|
|
|
*/ |
|
12
|
|
|
class GeneralUtil |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param array $types |
|
16
|
|
|
* @param string $default |
|
17
|
|
|
* |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function parseKey(array $types, string $default = 'default'): string |
|
21
|
|
|
{ |
|
22
|
|
|
return !empty($types) ? implode(';', $types) : $default; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Escape newline characters according to RFC2425 section 5.8.4. |
|
27
|
|
|
* |
|
28
|
|
|
* @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $text |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function escape(string $text): string |
|
35
|
|
|
{ |
|
36
|
|
|
$text = str_replace(array("\r\n", "\n"), "\\n", $text); |
|
37
|
|
|
|
|
38
|
|
|
return $text; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Unescape newline characters according to RFC2425 section 5.8.4. |
|
43
|
|
|
* This function will replace escaped line breaks with PHP_EOL. |
|
44
|
|
|
* |
|
45
|
|
|
* @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
|
46
|
|
|
* @param string $text |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function unescape($text): string |
|
50
|
|
|
{ |
|
51
|
|
|
return str_replace("\\n", PHP_EOL, $text); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Fold a line according to RFC2425 section 5.8.1. |
|
56
|
|
|
* |
|
57
|
|
|
* @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $text |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool|string |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function fold($text) |
|
64
|
|
|
{ |
|
65
|
|
|
if (\strlen($text) <= 75) { |
|
66
|
|
|
return $text; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// split, wrap and trim trailing separator |
|
70
|
|
|
return substr(chunk_split($text, 73, "\r\n "), 0, -3); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $url |
|
75
|
|
|
* |
|
76
|
|
|
* @return null|string |
|
77
|
|
|
* @throws InvalidUrlException |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function getMimeType(string $url): ?string |
|
80
|
|
|
{ |
|
81
|
|
|
$mimeType = null; |
|
82
|
|
|
|
|
83
|
|
|
$url = trim($url); |
|
84
|
|
|
|
|
85
|
|
|
if ($url === '') { |
|
86
|
|
|
throw new InvalidUrlException(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
//Is this URL for a remote resource? |
|
90
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL) !== false) { |
|
91
|
|
|
$headers = get_headers($url, 1); |
|
92
|
|
|
|
|
93
|
|
|
if (array_key_exists('Content-Type', $headers)) { |
|
94
|
|
|
$mimeType = $headers['Content-Type']; |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
|
|
//Local file, so inspect it directly |
|
98
|
|
|
$mimeType = mime_content_type($url); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (strpos($mimeType, ';') !== false) { |
|
102
|
|
|
$mimeType = strstr($mimeType, ';', true); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $mimeType; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|