]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/bookmark/LinkUtils.php
Automatically retrieve description for new bookmarks
[github/shaarli/Shaarli.git] / application / bookmark / LinkUtils.php
CommitLineData
1557cefb
A
1<?php
2
f24896b2
V
3use Shaarli\Bookmark\LinkDB;
4
1557cefb 5/**
d65342e3 6 * Get cURL callback function for CURLOPT_WRITEFUNCTION
1557cefb 7 *
d65342e3
A
8 * @param string $charset to extract from the downloaded page (reference)
9 * @param string $title to extract from the downloaded page (reference)
6a487252
A
10 * @param string $description to extract from the downloaded page (reference)
11 * @param string $keywords to extract from the downloaded page (reference)
12 * @param bool $retrieveDescription Automatically tries to retrieve description and keywords from HTML content
fe3713d2 13 * @param string $curlGetInfo Optionally overrides curl_getinfo function
1557cefb 14 *
d65342e3 15 * @return Closure
1557cefb 16 */
6a487252
A
17function get_curl_download_callback(
18 &$charset,
19 &$title,
20 &$description,
21 &$keywords,
22 $retrieveDescription,
23 $curlGetInfo = 'curl_getinfo'
24) {
a1b727ef 25 $isRedirected = false;
6a487252
A
26 $currentChunk = 0;
27 $foundChunk = null;
28
d65342e3
A
29 /**
30 * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download).
31 *
32 * While downloading the remote page, we check that the HTTP code is 200 and content type is 'html/text'
33 * Then we extract the title and the charset and stop the download when it's done.
34 *
35 * @param resource $ch cURL resource
36 * @param string $data chunk of data being downloaded
37 *
38 * @return int|bool length of $data or false if we need to stop the download
39 */
6a487252
A
40 return function (&$ch, $data) use (
41 $retrieveDescription,
42 $curlGetInfo,
43 &$charset,
44 &$title,
45 &$description,
46 &$keywords,
47 &$isRedirected,
48 &$currentChunk,
49 &$foundChunk
50 ) {
51 $currentChunk++;
d65342e3 52 $responseCode = $curlGetInfo($ch, CURLINFO_RESPONSE_CODE);
a1b727ef
A
53 if (!empty($responseCode) && in_array($responseCode, [301, 302])) {
54 $isRedirected = true;
55 return strlen($data);
56 }
57 if (!empty($responseCode) && $responseCode !== 200) {
d65342e3
A
58 return false;
59 }
a1b727ef
A
60 // After a redirection, the content type will keep the previous request value
61 // until it finds the next content-type header.
62 if (! $isRedirected || strpos(strtolower($data), 'content-type') !== false) {
63 $contentType = $curlGetInfo($ch, CURLINFO_CONTENT_TYPE);
64 }
d65342e3
A
65 if (!empty($contentType) && strpos($contentType, 'text/html') === false) {
66 return false;
67 }
a1b727ef 68 if (!empty($contentType) && empty($charset)) {
d65342e3
A
69 $charset = header_extract_charset($contentType);
70 }
71 if (empty($charset)) {
72 $charset = html_extract_charset($data);
73 }
74 if (empty($title)) {
75 $title = html_extract_title($data);
6a487252
A
76 $foundChunk = ! empty($title) ? $currentChunk : $foundChunk;
77 }
78 if ($retrieveDescription && empty($description)) {
79 $description = html_extract_tag('description', $data);
80 $foundChunk = ! empty($description) ? $currentChunk : $foundChunk;
d65342e3 81 }
6a487252
A
82 if ($retrieveDescription && empty($keywords)) {
83 $keywords = html_extract_tag('keywords', $data);
84 if (! empty($keywords)) {
85 $foundChunk = $currentChunk;
86 // Keywords use the format tag1, tag2 multiple words, tag
87 // So we format them to match Shaarli's separator and glue multiple words with '-'
88 $keywords = implode(' ', array_map(function($keyword) {
89 return implode('-', preg_split('/\s+/', trim($keyword)));
90 }, explode(',', $keywords)));
91 }
92 }
93
d65342e3 94 // We got everything we want, stop the download.
6a487252
A
95 // If we already found either the title, description or keywords,
96 // it's highly unlikely that we'll found the other metas further than
97 // in the same chunk of data or the next one. So we also stop the download after that.
98 if ((!empty($responseCode) && !empty($contentType) && !empty($charset)) && $foundChunk !== null
99 && (! $retrieveDescription
100 || $foundChunk < $currentChunk
101 || (!empty($title) && !empty($description) && !empty($keywords))
102 )
103 ) {
d65342e3
A
104 return false;
105 }
106
107 return strlen($data);
108 };
1557cefb
A
109}
110
111/**
d65342e3 112 * Extract title from an HTML document.
1557cefb 113 *
d65342e3 114 * @param string $html HTML content where to look for a title.
1557cefb 115 *
d65342e3 116 * @return bool|string Extracted title if found, false otherwise.
1557cefb 117 */
d65342e3 118function html_extract_title($html)
1557cefb 119{
d65342e3
A
120 if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) {
121 return trim(str_replace("\n", '', $matches[1]));
1557cefb 122 }
d65342e3 123 return false;
1557cefb
A
124}
125
126/**
d65342e3 127 * Extract charset from HTTP header if it's defined.
1557cefb 128 *
d65342e3 129 * @param string $header HTTP header Content-Type line.
1557cefb
A
130 *
131 * @return bool|string Charset string if found (lowercase), false otherwise.
132 */
d65342e3 133function header_extract_charset($header)
1557cefb 134{
d65342e3
A
135 preg_match('/charset="?([^; ]+)/i', $header, $match);
136 if (! empty($match[1])) {
137 return strtolower(trim($match[1]));
1557cefb
A
138 }
139
140 return false;
141}
142
143/**
144 * Extract charset HTML content (tag <meta charset>).
145 *
146 * @param string $html HTML content where to look for charset.
147 *
148 * @return bool|string Charset string if found, false otherwise.
149 */
150function html_extract_charset($html)
151{
152 // Get encoding specified in HTML header.
ce7b0b64 153 preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc);
1557cefb
A
154 if (!empty($enc[1])) {
155 return strtolower($enc[1]);
156 }
157
158 return false;
159}
141a86c5 160
6a487252
A
161/**
162 * Extract meta tag from HTML content in either:
163 * - OpenGraph: <meta property="og:[tag]" ...>
164 * - Meta tag: <meta name="[tag]" ...>
165 *
166 * @param string $tag Name of the tag to retrieve.
167 * @param string $html HTML content where to look for charset.
168 *
169 * @return bool|string Charset string if found, false otherwise.
170 */
171function html_extract_tag($tag, $html)
172{
173 $propertiesKey = ['property', 'name', 'itemprop'];
174 $properties = implode('|', $propertiesKey);
175 // Try to retrieve OpenGraph image.
176 $ogRegex = '#<meta[^>]+(?:'. $properties .')=["\']?(?:og:)?'. $tag .'["\'\s][^>]*content=["\']?(.*?)["\'/>]#';
177 // If the attributes are not in the order property => content (e.g. Github)
178 // New regex to keep this readable... more or less.
179 $ogRegexReverse = '#<meta[^>]+content=["\']([^"\']+)[^>]+(?:'. $properties .')=["\']?(?:og)?:'. $tag .'["\'\s/>]#';
180
181 if (preg_match($ogRegex, $html, $matches) > 0
182 || preg_match($ogRegexReverse, $html, $matches) > 0
183 ) {
184 return $matches[1];
185 }
186
187 return false;
188}
189
141a86c5
A
190/**
191 * Count private links in given linklist.
192 *
7af9a418 193 * @param array|Countable $links Linklist.
141a86c5
A
194 *
195 * @return int Number of private links.
196 */
197function count_private($links)
198{
199 $cpt = 0;
200 foreach ($links as $link) {
ee6f4b64
V
201 if ($link['private']) {
202 $cpt += 1;
203 }
141a86c5 204 }
9ccca401 205
141a86c5
A
206 return $cpt;
207}
9ccca401
A
208
209/**
210 * In a string, converts URLs to clickable links.
211 *
212 * @param string $text input string.
9ccca401
A
213 *
214 * @return string returns $text with all links converted to HTML links.
215 *
216 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
217 */
520d2957 218function text2clickable($text)
9ccca401 219{
601faf97 220 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
520d2957 221 return preg_replace($regex, '<a href="$1">$1</a>', $text);
9ccca401
A
222}
223
224/**
225 * Auto-link hashtags.
226 *
227 * @param string $description Given description.
228 * @param string $indexUrl Root URL.
229 *
230 * @return string Description with auto-linked hashtags.
231 */
232function hashtag_autolink($description, $indexUrl = '')
233{
234 /*
235 * To support unicode: http://stackoverflow.com/a/35498078/1484919
236 * \p{Pc} - to match underscore
237 * \p{N} - numeric character in any script
238 * \p{L} - letter from any language
239 * \p{Mn} - any non marking space (accents, umlauts, etc)
240 */
241 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
242 $replacement = '$1<a href="'. $indexUrl .'?addtag=$2" title="Hashtag $2">#$2</a>';
243 return preg_replace($regex, $replacement, $description);
244}
245
246/**
247 * This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
248 * even in the absence of <pre> (This is used in description to keep text formatting).
249 *
250 * @param string $text input text.
251 *
252 * @return string formatted text.
253 */
254function space2nbsp($text)
255{
256 return preg_replace('/(^| ) /m', '$1&nbsp;', $text);
257}
258
259/**
260 * Format Shaarli's description
261 *
262 * @param string $description shaare's description.
7af9a418 263 * @param string $indexUrl URL to Shaarli's index.
fd08b50a 264
9ccca401
A
265 * @return string formatted description.
266 */
520d2957 267function format_description($description, $indexUrl = '')
f211e417 268{
520d2957 269 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl)));
9ccca401 270}
d592daea
A
271
272/**
273 * Generate a small hash for a link.
274 *
275 * @param DateTime $date Link creation date.
276 * @param int $id Link ID.
277 *
278 * @return string the small hash generated from link data.
279 */
280function link_small_hash($date, $id)
281{
282 return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id);
283}
a8e7da01
A
284
285/**
286 * Returns whether or not the link is an internal note.
287 * Its URL starts by `?` because it's actually a permalink.
288 *
289 * @param string $linkUrl
290 *
291 * @return bool true if internal note, false otherwise.
292 */
293function is_note($linkUrl)
294{
295 return isset($linkUrl[0]) && $linkUrl[0] === '?';
296}