]>
Commit | Line | Data |
---|---|---|
1557cefb A |
1 | <?php |
2 | ||
f24896b2 V |
3 | use 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) | |
fe3713d2 | 10 | * @param string $curlGetInfo Optionally overrides curl_getinfo function |
1557cefb | 11 | * |
d65342e3 | 12 | * @return Closure |
1557cefb | 13 | */ |
d65342e3 | 14 | function get_curl_download_callback(&$charset, &$title, $curlGetInfo = 'curl_getinfo') |
1557cefb | 15 | { |
a1b727ef | 16 | $isRedirected = false; |
d65342e3 A |
17 | /** |
18 | * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download). | |
19 | * | |
20 | * While downloading the remote page, we check that the HTTP code is 200 and content type is 'html/text' | |
21 | * Then we extract the title and the charset and stop the download when it's done. | |
22 | * | |
23 | * @param resource $ch cURL resource | |
24 | * @param string $data chunk of data being downloaded | |
25 | * | |
26 | * @return int|bool length of $data or false if we need to stop the download | |
27 | */ | |
f211e417 | 28 | return function (&$ch, $data) use ($curlGetInfo, &$charset, &$title, &$isRedirected) { |
d65342e3 | 29 | $responseCode = $curlGetInfo($ch, CURLINFO_RESPONSE_CODE); |
a1b727ef A |
30 | if (!empty($responseCode) && in_array($responseCode, [301, 302])) { |
31 | $isRedirected = true; | |
32 | return strlen($data); | |
33 | } | |
34 | if (!empty($responseCode) && $responseCode !== 200) { | |
d65342e3 A |
35 | return false; |
36 | } | |
a1b727ef A |
37 | // After a redirection, the content type will keep the previous request value |
38 | // until it finds the next content-type header. | |
39 | if (! $isRedirected || strpos(strtolower($data), 'content-type') !== false) { | |
40 | $contentType = $curlGetInfo($ch, CURLINFO_CONTENT_TYPE); | |
41 | } | |
d65342e3 A |
42 | if (!empty($contentType) && strpos($contentType, 'text/html') === false) { |
43 | return false; | |
44 | } | |
a1b727ef | 45 | if (!empty($contentType) && empty($charset)) { |
d65342e3 A |
46 | $charset = header_extract_charset($contentType); |
47 | } | |
48 | if (empty($charset)) { | |
49 | $charset = html_extract_charset($data); | |
50 | } | |
51 | if (empty($title)) { | |
52 | $title = html_extract_title($data); | |
53 | } | |
54 | // We got everything we want, stop the download. | |
55 | if (!empty($responseCode) && !empty($contentType) && !empty($charset) && !empty($title)) { | |
56 | return false; | |
57 | } | |
58 | ||
59 | return strlen($data); | |
60 | }; | |
1557cefb A |
61 | } |
62 | ||
63 | /** | |
d65342e3 | 64 | * Extract title from an HTML document. |
1557cefb | 65 | * |
d65342e3 | 66 | * @param string $html HTML content where to look for a title. |
1557cefb | 67 | * |
d65342e3 | 68 | * @return bool|string Extracted title if found, false otherwise. |
1557cefb | 69 | */ |
d65342e3 | 70 | function html_extract_title($html) |
1557cefb | 71 | { |
d65342e3 A |
72 | if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) { |
73 | return trim(str_replace("\n", '', $matches[1])); | |
1557cefb | 74 | } |
d65342e3 | 75 | return false; |
1557cefb A |
76 | } |
77 | ||
78 | /** | |
d65342e3 | 79 | * Extract charset from HTTP header if it's defined. |
1557cefb | 80 | * |
d65342e3 | 81 | * @param string $header HTTP header Content-Type line. |
1557cefb A |
82 | * |
83 | * @return bool|string Charset string if found (lowercase), false otherwise. | |
84 | */ | |
d65342e3 | 85 | function header_extract_charset($header) |
1557cefb | 86 | { |
d65342e3 A |
87 | preg_match('/charset="?([^; ]+)/i', $header, $match); |
88 | if (! empty($match[1])) { | |
89 | return strtolower(trim($match[1])); | |
1557cefb A |
90 | } |
91 | ||
92 | return false; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Extract charset HTML content (tag <meta charset>). | |
97 | * | |
98 | * @param string $html HTML content where to look for charset. | |
99 | * | |
100 | * @return bool|string Charset string if found, false otherwise. | |
101 | */ | |
102 | function html_extract_charset($html) | |
103 | { | |
104 | // Get encoding specified in HTML header. | |
ce7b0b64 | 105 | preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc); |
1557cefb A |
106 | if (!empty($enc[1])) { |
107 | return strtolower($enc[1]); | |
108 | } | |
109 | ||
110 | return false; | |
111 | } | |
141a86c5 A |
112 | |
113 | /** | |
114 | * Count private links in given linklist. | |
115 | * | |
7af9a418 | 116 | * @param array|Countable $links Linklist. |
141a86c5 A |
117 | * |
118 | * @return int Number of private links. | |
119 | */ | |
120 | function count_private($links) | |
121 | { | |
122 | $cpt = 0; | |
123 | foreach ($links as $link) { | |
ee6f4b64 V |
124 | if ($link['private']) { |
125 | $cpt += 1; | |
126 | } | |
141a86c5 | 127 | } |
9ccca401 | 128 | |
141a86c5 A |
129 | return $cpt; |
130 | } | |
9ccca401 A |
131 | |
132 | /** | |
133 | * In a string, converts URLs to clickable links. | |
134 | * | |
135 | * @param string $text input string. | |
9ccca401 A |
136 | * |
137 | * @return string returns $text with all links converted to HTML links. | |
138 | * | |
139 | * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 | |
140 | */ | |
520d2957 | 141 | function text2clickable($text) |
9ccca401 | 142 | { |
601faf97 | 143 | $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si'; |
520d2957 | 144 | return preg_replace($regex, '<a href="$1">$1</a>', $text); |
9ccca401 A |
145 | } |
146 | ||
147 | /** | |
148 | * Auto-link hashtags. | |
149 | * | |
150 | * @param string $description Given description. | |
151 | * @param string $indexUrl Root URL. | |
152 | * | |
153 | * @return string Description with auto-linked hashtags. | |
154 | */ | |
155 | function hashtag_autolink($description, $indexUrl = '') | |
156 | { | |
157 | /* | |
158 | * To support unicode: http://stackoverflow.com/a/35498078/1484919 | |
159 | * \p{Pc} - to match underscore | |
160 | * \p{N} - numeric character in any script | |
161 | * \p{L} - letter from any language | |
162 | * \p{Mn} - any non marking space (accents, umlauts, etc) | |
163 | */ | |
164 | $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; | |
165 | $replacement = '$1<a href="'. $indexUrl .'?addtag=$2" title="Hashtag $2">#$2</a>'; | |
166 | return preg_replace($regex, $replacement, $description); | |
167 | } | |
168 | ||
169 | /** | |
170 | * This function inserts where relevant so that multiple spaces are properly displayed in HTML | |
171 | * even in the absence of <pre> (This is used in description to keep text formatting). | |
172 | * | |
173 | * @param string $text input text. | |
174 | * | |
175 | * @return string formatted text. | |
176 | */ | |
177 | function space2nbsp($text) | |
178 | { | |
179 | return preg_replace('/(^| ) /m', '$1 ', $text); | |
180 | } | |
181 | ||
182 | /** | |
183 | * Format Shaarli's description | |
184 | * | |
185 | * @param string $description shaare's description. | |
7af9a418 | 186 | * @param string $indexUrl URL to Shaarli's index. |
fd08b50a | 187 | |
9ccca401 A |
188 | * @return string formatted description. |
189 | */ | |
520d2957 | 190 | function format_description($description, $indexUrl = '') |
f211e417 | 191 | { |
520d2957 | 192 | return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl))); |
9ccca401 | 193 | } |
d592daea A |
194 | |
195 | /** | |
196 | * Generate a small hash for a link. | |
197 | * | |
198 | * @param DateTime $date Link creation date. | |
199 | * @param int $id Link ID. | |
200 | * | |
201 | * @return string the small hash generated from link data. | |
202 | */ | |
203 | function link_small_hash($date, $id) | |
204 | { | |
205 | return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id); | |
206 | } |