3 use Shaarli\Bookmark\Bookmark
;
4 use Shaarli\Formatter\BookmarkDefaultFormatter
;
7 * Extract title from an HTML document.
9 * @param string $html HTML content where to look for a title.
11 * @return bool|string Extracted title if found, false otherwise.
13 function html_extract_title($html)
15 if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) {
16 return trim(str_replace("\n", '', $matches[1]));
22 * Extract charset from HTTP header if it's defined.
24 * @param string $header HTTP header Content-Type line.
26 * @return bool|string Charset string if found (lowercase), false otherwise.
28 function header_extract_charset($header)
30 preg_match('/charset=["\']?([^; "\']+)/i', $header, $match);
31 if (! empty($match[1])) {
32 return strtolower(trim($match[1]));
39 * Extract charset HTML content (tag <meta charset>).
41 * @param string $html HTML content where to look for charset.
43 * @return bool|string Charset string if found, false otherwise.
45 function html_extract_charset($html)
47 // Get encoding specified in HTML header.
48 preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc);
49 if (!empty($enc[1])) {
50 return strtolower($enc[1]);
57 * Extract meta tag from HTML content in either:
58 * - OpenGraph: <meta property="og:[tag]" ...>
59 * - Meta tag: <meta name="[tag]" ...>
61 * @param string $tag Name of the tag to retrieve.
62 * @param string $html HTML content where to look for charset.
64 * @return bool|string Charset string if found, false otherwise.
66 function html_extract_tag($tag, $html)
68 $propertiesKey = ['property', 'name', 'itemprop'];
69 $properties = implode('|', $propertiesKey);
70 // We need a OR here to accept either 'property=og:noquote' or 'property="og:unrelated og:my-tag"'
71 $orCondition = '["\']?(?:og:)?' . $tag . '["\']?|["\'][^\'"]*?(?:og:)?' . $tag . '[^\'"]*?[\'"]';
72 // Support quotes in double quoted content, and the other way around
73 $content = 'content=(["\'])((?:(?!\1).)*)\1';
74 // Try to retrieve OpenGraph tag.
75 $ogRegex = '#<meta[^>]+(?:' . $properties . ')=(?:' . $orCondition . ')[^>]*' . $content . '.*?>#';
76 // If the attributes are not in the order property => content (e.g. Github)
77 // New regex to keep this readable... more or less.
78 $ogRegexReverse = '#<meta[^>]+' . $content . '[^>]+(?:' . $properties . ')=(?:' . $orCondition . ').*?>#';
81 preg_match($ogRegex, $html, $matches) > 0
82 || preg_match($ogRegexReverse, $html, $matches) > 0
91 * In a string, converts URLs to clickable bookmarks.
93 * @param string $text input string.
95 * @return string returns $text with all bookmarks converted to HTML bookmarks.
97 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
99 function text2clickable($text)
101 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
102 $format = function (array $match): string {
105 BookmarkDefaultFormatter
::SEARCH_HIGHLIGHT_OPEN
,
107 str_replace(BookmarkDefaultFormatter
::SEARCH_HIGHLIGHT_CLOSE
, '', $match[1])
109 '">' . $match[1] . '</a>'
113 return preg_replace_callback($regex, $format, $text);
117 * Auto-link hashtags.
119 * @param string $description Given description.
120 * @param string $indexUrl Root URL.
122 * @return string Description with auto-linked hashtags.
124 function hashtag_autolink($description, $indexUrl = '')
126 $tokens = '(?:' . BookmarkDefaultFormatter
::SEARCH_HIGHLIGHT_OPEN
. ')' .
127 '(?:' . BookmarkDefaultFormatter
::SEARCH_HIGHLIGHT_CLOSE
. ')'
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)
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
,
141 str_replace(BookmarkDefaultFormatter
::SEARCH_HIGHLIGHT_CLOSE
, '', $match[2])
143 return $match[1] . '<a href="' . $indexUrl . './add-tag/' . $cleanMatch . '"' .
144 ' title="Hashtag ' . $cleanMatch . '">' .
149 return preg_replace_callback($regex, $format, $description);
153 * This function inserts 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).
156 * @param string $text input text.
158 * @return string formatted text.
160 function space2nbsp($text)
162 return preg_replace('/(^| ) /m', '$1 ', $text);
166 * Format Shaarli's description
168 * @param string $description shaare's description.
169 * @param string $indexUrl URL to Shaarli's index.
170 * @param bool $autolink Turn on/off automatic linkifications of URLs and hashtags
172 * @return string formatted description.
174 function format_description($description, $indexUrl = '', $autolink = true)
177 $description = hashtag_autolink(text2clickable($description), $indexUrl);
180 return nl2br(space2nbsp($description));
184 * Generate a small hash for a link.
186 * @param DateTime $date Link creation date.
187 * @param int $id Link ID.
189 * @return string the small hash generated from link data.
191 function link_small_hash($date, $id)
193 return smallHash($date->format(Bookmark
::LINK_DATE_FORMAT
) . $id);
197 * Returns whether or not the link is an internal note.
198 * Its URL starts by `?` because it's actually a permalink.
200 * @param string $linkUrl
202 * @return bool true if internal note, false otherwise.
204 function is_note($linkUrl)
206 return isset($linkUrl[0]) && $linkUrl[0] === '?';
210 * Extract an array of tags from a given tag string, with provided separator.
212 * @param string|null $tags String containing a list of tags separated by $separator.
213 * @param string $separator Shaarli's default: ' ' (whitespace)
215 * @return array List of tags
217 function tags_str2array(?string $tags, string $separator): array
219 // For whitespaces, we use the special \s regex character
220 $separator = $separator === ' ' ? '\s' : $separator;
222 return preg_split('/\s*' . $separator . '+\s*/', trim($tags) ?? '', -1, PREG_SPLIT_NO_EMPTY
);
226 * Return a tag string with provided separator from a list of tags.
227 * Note that given array is clean up by tags_filter().
229 * @param array|null $tags List of tags
230 * @param string $separator
234 function tags_array2str(?array $tags, string $separator): string
236 return implode($separator, tags_filter($tags, $separator));
240 * Clean an array of tags: trim + remove empty entries
242 * @param array|null $tags List of tags
243 * @param string $separator
247 function tags_filter(?array $tags, string $separator): array
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);