]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/bookmark/LinkUtils.php
Support search highlights when matching URL content
[github/shaarli/Shaarli.git] / application / bookmark / LinkUtils.php
CommitLineData
1557cefb
A
1<?php
2
336a28fa 3use Shaarli\Bookmark\Bookmark;
9ef8555a 4use Shaarli\Formatter\BookmarkDefaultFormatter;
f24896b2 5
1557cefb 6/**
d65342e3 7 * Extract title from an HTML document.
1557cefb 8 *
d65342e3 9 * @param string $html HTML content where to look for a title.
1557cefb 10 *
d65342e3 11 * @return bool|string Extracted title if found, false otherwise.
1557cefb 12 */
d65342e3 13function html_extract_title($html)
1557cefb 14{
d65342e3
A
15 if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) {
16 return trim(str_replace("\n", '', $matches[1]));
1557cefb 17 }
d65342e3 18 return false;
1557cefb
A
19}
20
21/**
d65342e3 22 * Extract charset from HTTP header if it's defined.
1557cefb 23 *
d65342e3 24 * @param string $header HTTP header Content-Type line.
1557cefb
A
25 *
26 * @return bool|string Charset string if found (lowercase), false otherwise.
27 */
d65342e3 28function header_extract_charset($header)
1557cefb 29{
1ea09a1b 30 preg_match('/charset=["\']?([^; "\']+)/i', $header, $match);
d65342e3
A
31 if (! empty($match[1])) {
32 return strtolower(trim($match[1]));
1557cefb
A
33 }
34
35 return false;
36}
37
38/**
39 * Extract charset HTML content (tag <meta charset>).
40 *
41 * @param string $html HTML content where to look for charset.
42 *
43 * @return bool|string Charset string if found, false otherwise.
44 */
45function html_extract_charset($html)
46{
47 // Get encoding specified in HTML header.
ce7b0b64 48 preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc);
1557cefb
A
49 if (!empty($enc[1])) {
50 return strtolower($enc[1]);
51 }
52
53 return false;
54}
141a86c5 55
6a487252
A
56/**
57 * Extract meta tag from HTML content in either:
58 * - OpenGraph: <meta property="og:[tag]" ...>
59 * - Meta tag: <meta name="[tag]" ...>
60 *
61 * @param string $tag Name of the tag to retrieve.
62 * @param string $html HTML content where to look for charset.
63 *
64 * @return bool|string Charset string if found, false otherwise.
65 */
66function html_extract_tag($tag, $html)
67{
68 $propertiesKey = ['property', 'name', 'itemprop'];
69 $properties = implode('|', $propertiesKey);
2cd0509b 70 // We need a OR here to accept either 'property=og:noquote' or 'property="og:unrelated og:my-tag"'
53054b2b 71 $orCondition = '["\']?(?:og:)?' . $tag . '["\']?|["\'][^\'"]*?(?:og:)?' . $tag . '[^\'"]*?[\'"]';
88a8e284
A
72 // Support quotes in double quoted content, and the other way around
73 $content = 'content=(["\'])((?:(?!\1).)*)\1';
00d3dd91 74 // Try to retrieve OpenGraph tag.
88a8e284 75 $ogRegex = '#<meta[^>]+(?:' . $properties . ')=(?:' . $orCondition . ')[^>]*' . $content . '.*?>#';
6a487252
A
76 // If the attributes are not in the order property => content (e.g. Github)
77 // New regex to keep this readable... more or less.
88a8e284 78 $ogRegexReverse = '#<meta[^>]+' . $content . '[^>]+(?:' . $properties . ')=(?:' . $orCondition . ').*?>#';
6a487252 79
53054b2b
A
80 if (
81 preg_match($ogRegex, $html, $matches) > 0
6a487252
A
82 || preg_match($ogRegexReverse, $html, $matches) > 0
83 ) {
00d3dd91 84 return $matches[2];
6a487252
A
85 }
86
87 return false;
88}
89
141a86c5 90/**
336a28fa 91 * In a string, converts URLs to clickable bookmarks.
9ccca401
A
92 *
93 * @param string $text input string.
9ccca401 94 *
336a28fa 95 * @return string returns $text with all bookmarks converted to HTML bookmarks.
9ccca401
A
96 *
97 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
98 */
520d2957 99function text2clickable($text)
9ccca401 100{
601faf97 101 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
9ef8555a
A
102 $format = function (array $match): string {
103 return '<a href="' .
104 str_replace(
105 BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN,
106 '',
107 str_replace(BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE, '', $match[1])
108 ) .
109 '">' . $match[1] . '</a>'
110 ;
111 };
112
113 return preg_replace_callback($regex, $format, $text);
9ccca401
A
114}
115
116/**
117 * Auto-link hashtags.
118 *
119 * @param string $description Given description.
120 * @param string $indexUrl Root URL.
121 *
122 * @return string Description with auto-linked hashtags.
123 */
124function hashtag_autolink($description, $indexUrl = '')
125{
9ef8555a
A
126 $tokens = '(?:' . BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN . ')' .
127 '(?:' . BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE . ')'
128 ;
9ccca401
A
129 /*
130 * To support unicode: http://stackoverflow.com/a/35498078/1484919
131 * \p{Pc} - to match underscore
132 * \p{N} - numeric character in any script
133 * \p{L} - letter from any language
134 * \p{Mn} - any non marking space (accents, umlauts, etc)
135 */
9ef8555a
A
136 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}' . $tokens . ']+)/mui';
137 $format = function (array $match) use ($indexUrl): string {
138 $cleanMatch = str_replace(
139 BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_OPEN,
140 '',
141 str_replace(BookmarkDefaultFormatter::SEARCH_HIGHLIGHT_CLOSE, '', $match[2])
142 );
143 return $match[1] . '<a href="' . $indexUrl . './add-tag/' . $cleanMatch . '"' .
144 ' title="Hashtag ' . $cleanMatch . '">' .
145 '#' . $match[2] .
146 '</a>';
147 };
148
149 return preg_replace_callback($regex, $format, $description);
9ccca401
A
150}
151
152/**
153 * This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
154 * even in the absence of <pre> (This is used in description to keep text formatting).
155 *
156 * @param string $text input text.
157 *
158 * @return string formatted text.
159 */
160function space2nbsp($text)
161{
162 return preg_replace('/(^| ) /m', '$1&nbsp;', $text);
163}
164
165/**
166 * Format Shaarli's description
167 *
168 * @param string $description shaare's description.
7af9a418 169 * @param string $indexUrl URL to Shaarli's index.
740b32b5
A
170 * @param bool $autolink Turn on/off automatic linkifications of URLs and hashtags
171 *
9ccca401
A
172 * @return string formatted description.
173 */
740b32b5 174function format_description($description, $indexUrl = '', $autolink = true)
f211e417 175{
740b32b5
A
176 if ($autolink) {
177 $description = hashtag_autolink(text2clickable($description), $indexUrl);
178 }
179
180 return nl2br(space2nbsp($description));
9ccca401 181}
d592daea
A
182
183/**
184 * Generate a small hash for a link.
185 *
186 * @param DateTime $date Link creation date.
187 * @param int $id Link ID.
188 *
189 * @return string the small hash generated from link data.
190 */
191function link_small_hash($date, $id)
192{
336a28fa 193 return smallHash($date->format(Bookmark::LINK_DATE_FORMAT) . $id);
d592daea 194}
a8e7da01
A
195
196/**
197 * Returns whether or not the link is an internal note.
198 * Its URL starts by `?` because it's actually a permalink.
199 *
200 * @param string $linkUrl
201 *
202 * @return bool true if internal note, false otherwise.
203 */
204function is_note($linkUrl)
205{
206 return isset($linkUrl[0]) && $linkUrl[0] === '?';
207}
b3bd8c3e
A
208
209/**
210 * Extract an array of tags from a given tag string, with provided separator.
211 *
212 * @param string|null $tags String containing a list of tags separated by $separator.
213 * @param string $separator Shaarli's default: ' ' (whitespace)
214 *
215 * @return array List of tags
216 */
217function tags_str2array(?string $tags, string $separator): array
218{
219 // For whitespaces, we use the special \s regex character
220 $separator = $separator === ' ' ? '\s' : $separator;
221
222 return preg_split('/\s*' . $separator . '+\s*/', trim($tags) ?? '', -1, PREG_SPLIT_NO_EMPTY);
223}
224
225/**
226 * Return a tag string with provided separator from a list of tags.
227 * Note that given array is clean up by tags_filter().
228 *
229 * @param array|null $tags List of tags
230 * @param string $separator
231 *
232 * @return string
233 */
234function tags_array2str(?array $tags, string $separator): string
235{
236 return implode($separator, tags_filter($tags, $separator));
237}
238
239/**
240 * Clean an array of tags: trim + remove empty entries
241 *
242 * @param array|null $tags List of tags
243 * @param string $separator
244 *
245 * @return array
246 */
247function tags_filter(?array $tags, string $separator): array
248{
249 $trimDefault = " \t\n\r\0\x0B";
250 return array_values(array_filter(array_map(function (string $entry) use ($separator, $trimDefault): string {
251 return trim($entry, $trimDefault . $separator);
252 }, $tags ?? [])));
253}