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