]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkUtils.php
4 * Get cURL callback function for CURLOPT_WRITEFUNCTION
6 * @param string $charset to extract from the downloaded page (reference)
7 * @param string $title to extract from the downloaded page (reference)
8 * @param string $curlGetInfo Optionnaly overrides curl_getinfo function
12 function get_curl_download_callback(&$charset, &$title, $curlGetInfo = 'curl_getinfo')
15 * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download).
17 * While downloading the remote page, we check that the HTTP code is 200 and content type is 'html/text'
18 * Then we extract the title and the charset and stop the download when it's done.
20 * @param resource $ch cURL resource
21 * @param string $data chunk of data being downloaded
23 * @return int|bool length of $data or false if we need to stop the download
25 return function(&$ch, $data) use ($curlGetInfo, &$charset, &$title) {
26 $responseCode = $curlGetInfo($ch, CURLINFO_RESPONSE_CODE
);
27 if (!empty($responseCode) && $responseCode != 200) {
30 $contentType = $curlGetInfo($ch, CURLINFO_CONTENT_TYPE
);
31 if (!empty($contentType) && strpos($contentType, 'text/html') === false) {
34 if (empty($charset)) {
35 $charset = header_extract_charset($contentType);
37 if (empty($charset)) {
38 $charset = html_extract_charset($data);
41 $title = html_extract_title($data);
43 // We got everything we want, stop the download.
44 if (!empty($responseCode) && !empty($contentType) && !empty($charset) && !empty($title)) {
53 * Extract title from an HTML document.
55 * @param string $html HTML content where to look for a title.
57 * @return bool|string Extracted title if found, false otherwise.
59 function html_extract_title($html)
61 if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) {
62 return trim(str_replace("\n", '', $matches[1]));
68 * Extract charset from HTTP header if it's defined.
70 * @param string $header HTTP header Content-Type line.
72 * @return bool|string Charset string if found (lowercase), false otherwise.
74 function header_extract_charset($header)
76 preg_match('/charset="?([^; ]+)/i', $header, $match);
77 if (! empty($match[1])) {
78 return strtolower(trim($match[1]));
85 * Extract charset HTML content (tag <meta charset>).
87 * @param string $html HTML content where to look for charset.
89 * @return bool|string Charset string if found, false otherwise.
91 function html_extract_charset($html)
93 // Get encoding specified in HTML header.
94 preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc);
95 if (!empty($enc[1])) {
96 return strtolower($enc[1]);
103 * Count private links in given linklist.
105 * @param array|Countable $links Linklist.
107 * @return int Number of private links.
109 function count_private($links)
112 foreach ($links as $link) {
113 if ($link['private']) {
122 * In a string, converts URLs to clickable links.
124 * @param string $text input string.
125 * @param string $redirector if a redirector is set, use it to gerenate links.
126 * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
128 * @return string returns $text with all links converted to HTML links.
130 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
132 function text2clickable($text, $redirector = '', $urlEncode = true)
134 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
136 if (empty($redirector)) {
137 return preg_replace($regex, '<a href="$1">$1</a>', $text);
139 // Redirector is set, urlencode the final URL.
140 return preg_replace_callback(
142 function ($matches) use ($redirector, $urlEncode) {
143 $url = $urlEncode ? urlencode($matches[1]) : $matches[1];
144 return '<a href="' . $redirector . $url .'">'. $matches[1] .'</a>';
151 * Auto-link hashtags.
153 * @param string $description Given description.
154 * @param string $indexUrl Root URL.
156 * @return string Description with auto-linked hashtags.
158 function hashtag_autolink($description, $indexUrl = '')
161 * To support unicode: http://stackoverflow.com/a/35498078/1484919
162 * \p{Pc} - to match underscore
163 * \p{N} - numeric character in any script
164 * \p{L} - letter from any language
165 * \p{Mn} - any non marking space (accents, umlauts, etc)
167 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
168 $replacement = '$1<a href="'. $indexUrl .'?addtag=$2" title="Hashtag $2">#$2</a>';
169 return preg_replace($regex, $replacement, $description);
173 * This function inserts where relevant so that multiple spaces are properly displayed in HTML
174 * even in the absence of <pre> (This is used in description to keep text formatting).
176 * @param string $text input text.
178 * @return string formatted text.
180 function space2nbsp($text)
182 return preg_replace('/(^| ) /m', '$1 ', $text);
186 * Format Shaarli's description
188 * @param string $description shaare's description.
189 * @param string $redirector if a redirector is set, use it to gerenate links.
190 * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
191 * @param string $indexUrl URL to Shaarli's index.
193 * @return string formatted description.
195 function format_description($description, $redirector = '', $urlEncode = true, $indexUrl = '') {
196 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector, $urlEncode), $indexUrl)));
200 * Generate a small hash for a link.
202 * @param DateTime $date Link creation date.
203 * @param int $id Link ID.
205 * @return string the small hash generated from link data.
207 function link_small_hash($date, $id)
209 return smallHash($date->format(LinkDB
::LINK_DATE_FORMAT
) . $id);