]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/bookmark/LinkUtils.php
Fix warning if the encoding retrieved from external headers is invalid
[github/shaarli/Shaarli.git] / application / bookmark / LinkUtils.php
CommitLineData
1557cefb
A
1<?php
2
336a28fa 3use Shaarli\Bookmark\Bookmark;
f24896b2 4
1557cefb 5/**
d65342e3 6 * Extract title from an HTML document.
1557cefb 7 *
d65342e3 8 * @param string $html HTML content where to look for a title.
1557cefb 9 *
d65342e3 10 * @return bool|string Extracted title if found, false otherwise.
1557cefb 11 */
d65342e3 12function html_extract_title($html)
1557cefb 13{
d65342e3
A
14 if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) {
15 return trim(str_replace("\n", '', $matches[1]));
1557cefb 16 }
d65342e3 17 return false;
1557cefb
A
18}
19
20/**
d65342e3 21 * Extract charset from HTTP header if it's defined.
1557cefb 22 *
d65342e3 23 * @param string $header HTTP header Content-Type line.
1557cefb
A
24 *
25 * @return bool|string Charset string if found (lowercase), false otherwise.
26 */
d65342e3 27function header_extract_charset($header)
1557cefb 28{
1ea09a1b 29 preg_match('/charset=["\']?([^; "\']+)/i', $header, $match);
d65342e3
A
30 if (! empty($match[1])) {
31 return strtolower(trim($match[1]));
1557cefb
A
32 }
33
34 return false;
35}
36
37/**
38 * Extract charset HTML content (tag <meta charset>).
39 *
40 * @param string $html HTML content where to look for charset.
41 *
42 * @return bool|string Charset string if found, false otherwise.
43 */
44function html_extract_charset($html)
45{
46 // Get encoding specified in HTML header.
ce7b0b64 47 preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc);
1557cefb
A
48 if (!empty($enc[1])) {
49 return strtolower($enc[1]);
50 }
51
52 return false;
53}
141a86c5 54
6a487252
A
55/**
56 * Extract meta tag from HTML content in either:
57 * - OpenGraph: <meta property="og:[tag]" ...>
58 * - Meta tag: <meta name="[tag]" ...>
59 *
60 * @param string $tag Name of the tag to retrieve.
61 * @param string $html HTML content where to look for charset.
62 *
63 * @return bool|string Charset string if found, false otherwise.
64 */
65function html_extract_tag($tag, $html)
66{
67 $propertiesKey = ['property', 'name', 'itemprop'];
68 $properties = implode('|', $propertiesKey);
69 // Try to retrieve OpenGraph image.
70 $ogRegex = '#<meta[^>]+(?:'. $properties .')=["\']?(?:og:)?'. $tag .'["\'\s][^>]*content=["\']?(.*?)["\'/>]#';
71 // If the attributes are not in the order property => content (e.g. Github)
72 // New regex to keep this readable... more or less.
73 $ogRegexReverse = '#<meta[^>]+content=["\']([^"\']+)[^>]+(?:'. $properties .')=["\']?(?:og)?:'. $tag .'["\'\s/>]#';
74
75 if (preg_match($ogRegex, $html, $matches) > 0
76 || preg_match($ogRegexReverse, $html, $matches) > 0
77 ) {
78 return $matches[1];
79 }
80
81 return false;
82}
83
141a86c5 84/**
336a28fa 85 * In a string, converts URLs to clickable bookmarks.
9ccca401
A
86 *
87 * @param string $text input string.
9ccca401 88 *
336a28fa 89 * @return string returns $text with all bookmarks converted to HTML bookmarks.
9ccca401
A
90 *
91 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
92 */
520d2957 93function text2clickable($text)
9ccca401 94{
601faf97 95 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
520d2957 96 return preg_replace($regex, '<a href="$1">$1</a>', $text);
9ccca401
A
97}
98
99/**
100 * Auto-link hashtags.
101 *
102 * @param string $description Given description.
103 * @param string $indexUrl Root URL.
104 *
105 * @return string Description with auto-linked hashtags.
106 */
107function hashtag_autolink($description, $indexUrl = '')
108{
109 /*
110 * To support unicode: http://stackoverflow.com/a/35498078/1484919
111 * \p{Pc} - to match underscore
112 * \p{N} - numeric character in any script
113 * \p{L} - letter from any language
114 * \p{Mn} - any non marking space (accents, umlauts, etc)
115 */
116 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
03340c18 117 $replacement = '$1<a href="'. $indexUrl .'./add-tag/$2" title="Hashtag $2">#$2</a>';
9ccca401
A
118 return preg_replace($regex, $replacement, $description);
119}
120
121/**
122 * This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
123 * even in the absence of <pre> (This is used in description to keep text formatting).
124 *
125 * @param string $text input text.
126 *
127 * @return string formatted text.
128 */
129function space2nbsp($text)
130{
131 return preg_replace('/(^| ) /m', '$1&nbsp;', $text);
132}
133
134/**
135 * Format Shaarli's description
136 *
137 * @param string $description shaare's description.
7af9a418 138 * @param string $indexUrl URL to Shaarli's index.
fd08b50a 139
9ccca401
A
140 * @return string formatted description.
141 */
520d2957 142function format_description($description, $indexUrl = '')
f211e417 143{
520d2957 144 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl)));
9ccca401 145}
d592daea
A
146
147/**
148 * Generate a small hash for a link.
149 *
150 * @param DateTime $date Link creation date.
151 * @param int $id Link ID.
152 *
153 * @return string the small hash generated from link data.
154 */
155function link_small_hash($date, $id)
156{
336a28fa 157 return smallHash($date->format(Bookmark::LINK_DATE_FORMAT) . $id);
d592daea 158}
a8e7da01
A
159
160/**
161 * Returns whether or not the link is an internal note.
162 * Its URL starts by `?` because it's actually a permalink.
163 *
164 * @param string $linkUrl
165 *
166 * @return bool true if internal note, false otherwise.
167 */
168function is_note($linkUrl)
169{
170 return isset($linkUrl[0]) && $linkUrl[0] === '?';
171}