]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | /** | |
4 | * Get cURL callback function for CURLOPT_WRITEFUNCTION | |
5 | * | |
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 | |
9 | * | |
10 | * @return Closure | |
11 | */ | |
12 | function get_curl_download_callback(&$charset, &$title, $curlGetInfo = 'curl_getinfo') | |
13 | { | |
14 | /** | |
15 | * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download). | |
16 | * | |
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. | |
19 | * | |
20 | * @param resource $ch cURL resource | |
21 | * @param string $data chunk of data being downloaded | |
22 | * | |
23 | * @return int|bool length of $data or false if we need to stop the download | |
24 | */ | |
25 | return function(&$ch, $data) use ($curlGetInfo, &$charset, &$title) { | |
26 | $responseCode = $curlGetInfo($ch, CURLINFO_RESPONSE_CODE); | |
27 | if (!empty($responseCode) && $responseCode != 200) { | |
28 | return false; | |
29 | } | |
30 | $contentType = $curlGetInfo($ch, CURLINFO_CONTENT_TYPE); | |
31 | if (!empty($contentType) && strpos($contentType, 'text/html') === false) { | |
32 | return false; | |
33 | } | |
34 | if (empty($charset)) { | |
35 | $charset = header_extract_charset($contentType); | |
36 | } | |
37 | if (empty($charset)) { | |
38 | $charset = html_extract_charset($data); | |
39 | } | |
40 | if (empty($title)) { | |
41 | $title = html_extract_title($data); | |
42 | } | |
43 | // We got everything we want, stop the download. | |
44 | if (!empty($responseCode) && !empty($contentType) && !empty($charset) && !empty($title)) { | |
45 | return false; | |
46 | } | |
47 | ||
48 | return strlen($data); | |
49 | }; | |
50 | } | |
51 | ||
52 | /** | |
53 | * Extract title from an HTML document. | |
54 | * | |
55 | * @param string $html HTML content where to look for a title. | |
56 | * | |
57 | * @return bool|string Extracted title if found, false otherwise. | |
58 | */ | |
59 | function html_extract_title($html) | |
60 | { | |
61 | if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) { | |
62 | return trim(str_replace("\n", '', $matches[1])); | |
63 | } | |
64 | return false; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Extract charset from HTTP header if it's defined. | |
69 | * | |
70 | * @param string $header HTTP header Content-Type line. | |
71 | * | |
72 | * @return bool|string Charset string if found (lowercase), false otherwise. | |
73 | */ | |
74 | function header_extract_charset($header) | |
75 | { | |
76 | preg_match('/charset="?([^; ]+)/i', $header, $match); | |
77 | if (! empty($match[1])) { | |
78 | return strtolower(trim($match[1])); | |
79 | } | |
80 | ||
81 | return false; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Extract charset HTML content (tag <meta charset>). | |
86 | * | |
87 | * @param string $html HTML content where to look for charset. | |
88 | * | |
89 | * @return bool|string Charset string if found, false otherwise. | |
90 | */ | |
91 | function html_extract_charset($html) | |
92 | { | |
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]); | |
97 | } | |
98 | ||
99 | return false; | |
100 | } | |
101 | ||
102 | /** | |
103 | * Count private links in given linklist. | |
104 | * | |
105 | * @param array|Countable $links Linklist. | |
106 | * | |
107 | * @return int Number of private links. | |
108 | */ | |
109 | function count_private($links) | |
110 | { | |
111 | $cpt = 0; | |
112 | foreach ($links as $link) { | |
113 | if ($link['private']) { | |
114 | $cpt += 1; | |
115 | } | |
116 | } | |
117 | ||
118 | return $cpt; | |
119 | } | |
120 | ||
121 | /** | |
122 | * In a string, converts URLs to clickable links. | |
123 | * | |
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. | |
127 | * | |
128 | * @return string returns $text with all links converted to HTML links. | |
129 | * | |
130 | * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 | |
131 | */ | |
132 | function text2clickable($text, $redirector = '', $urlEncode = true) | |
133 | { | |
134 | $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si'; | |
135 | ||
136 | if (empty($redirector)) { | |
137 | return preg_replace($regex, '<a href="$1">$1</a>', $text); | |
138 | } | |
139 | // Redirector is set, urlencode the final URL. | |
140 | return preg_replace_callback( | |
141 | $regex, | |
142 | function ($matches) use ($redirector, $urlEncode) { | |
143 | $url = $urlEncode ? urlencode($matches[1]) : $matches[1]; | |
144 | return '<a href="' . $redirector . $url .'">'. $matches[1] .'</a>'; | |
145 | }, | |
146 | $text | |
147 | ); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Auto-link hashtags. | |
152 | * | |
153 | * @param string $description Given description. | |
154 | * @param string $indexUrl Root URL. | |
155 | * | |
156 | * @return string Description with auto-linked hashtags. | |
157 | */ | |
158 | function hashtag_autolink($description, $indexUrl = '') | |
159 | { | |
160 | /* | |
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) | |
166 | */ | |
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); | |
170 | } | |
171 | ||
172 | /** | |
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). | |
175 | * | |
176 | * @param string $text input text. | |
177 | * | |
178 | * @return string formatted text. | |
179 | */ | |
180 | function space2nbsp($text) | |
181 | { | |
182 | return preg_replace('/(^| ) /m', '$1 ', $text); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Format Shaarli's description | |
187 | * | |
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. | |
192 | ||
193 | * @return string formatted description. | |
194 | */ | |
195 | function format_description($description, $redirector = '', $urlEncode = true, $indexUrl = '') { | |
196 | return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector, $urlEncode), $indexUrl))); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Generate a small hash for a link. | |
201 | * | |
202 | * @param DateTime $date Link creation date. | |
203 | * @param int $id Link ID. | |
204 | * | |
205 | * @return string the small hash generated from link data. | |
206 | */ | |
207 | function link_small_hash($date, $id) | |
208 | { | |
209 | return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id); | |
210 | } |