3 * @file include/plaintext.php
6 use Friendica\Core\PConfig;
7 use Friendica\Object\Image;
8 use Friendica\Util\ParseUrl;
10 require_once "include/bbcode.php";
11 require_once "include/html2plain.php";
12 require_once "include/network.php";
15 * @brief Fetches attachment data that were generated the old way
17 * @param string $body Message body
19 * 'type' -> Message type ("link", "video", "photo")
20 * 'text' -> Text before the shared message
21 * 'after' -> Text after the shared message
22 * 'image' -> Preview image of the message
23 * 'url' -> Url to the attached message
24 * 'title' -> Title of the attachment
25 * 'description' -> Description of the attachment
27 function get_old_attachment_data($body) {
31 // Simplify image codes
32 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
34 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
35 foreach ($attached AS $data) {
36 if (!in_array($data[1], ["type-link", "type-video", "type-photo"]))
39 $post["type"] = substr($data[1], 5);
41 $pos = strpos($body, $data[0]);
43 $post["text"] = trim(substr($body, 0, $pos));
44 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
46 $post["text"] = trim(str_replace($data[0], "", $body));
48 $attacheddata = $data[2];
50 $URLSearchString = "^\[\]";
52 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
54 $picturedata = Image::getInfoFromURL($matches[1]);
56 if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1]))
57 $post["image"] = $matches[1];
59 $post["preview"] = $matches[1];
62 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
63 $post["url"] = $matches[1];
64 $post["title"] = $matches[2];
66 if (($post["url"] == "") && (in_array($post["type"], ["link", "video"]))
67 && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
68 $post["url"] = $matches[1];
71 // Search for description
72 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
73 $post["description"] = $matches[1];
81 * @brief Fetches attachment data that were generated with the "attachment" element
83 * @param string $body Message body
85 * 'type' -> Message type ("link", "video", "photo")
86 * 'text' -> Text before the shared message
87 * 'after' -> Text after the shared message
88 * 'image' -> Preview image of the message
89 * 'url' -> Url to the attached message
90 * 'title' -> Title of the attachment
91 * 'description' -> Description of the attachment
93 function get_attachment_data($body)
97 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
98 return get_old_attachment_data($body);
101 $attributes = $match[2];
103 $data["text"] = trim($match[1]);
106 preg_match("/type='(.*?)'/ism", $attributes, $matches);
107 if (x($matches, 1)) {
108 $type = strtolower($matches[1]);
111 preg_match('/type="(.*?)"/ism', $attributes, $matches);
112 if (x($matches, 1)) {
113 $type = strtolower($matches[1]);
120 if (!in_array($type, ["link", "audio", "photo", "video"])) {
125 $data["type"] = $type;
129 preg_match("/url='(.*?)'/ism", $attributes, $matches);
130 if (x($matches, 1)) {
134 preg_match('/url="(.*?)"/ism', $attributes, $matches);
135 if (x($matches, 1)) {
140 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
144 preg_match("/title='(.*?)'/ism", $attributes, $matches);
145 if (x($matches, 1)) {
146 $title = $matches[1];
149 preg_match('/title="(.*?)"/ism', $attributes, $matches);
150 if (x($matches, 1)) {
151 $title = $matches[1];
155 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
156 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
157 $title = str_replace(["[", "]"], ["[", "]"], $title);
158 $data["title"] = $title;
162 preg_match("/image='(.*?)'/ism", $attributes, $matches);
163 if (x($matches, 1)) {
164 $image = $matches[1];
167 preg_match('/image="(.*?)"/ism', $attributes, $matches);
168 if (x($matches, 1)) {
169 $image = $matches[1];
173 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
177 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
178 if (x($matches, 1)) {
179 $preview = $matches[1];
182 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
183 if (x($matches, 1)) {
184 $preview = $matches[1];
187 if ($preview != "") {
188 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
191 $data["description"] = trim($match[3]);
193 $data["after"] = trim($match[4]);
198 function get_attached_data($body, $item = []) {
201 - type: link, video, photo
209 $has_title = !empty($item['title']);
210 $plink = (!empty($item['plink']) ? $item['plink'] : '');
211 $post = get_attachment_data($body);
213 // if nothing is found, it maybe having an image.
214 if (!isset($post["type"])) {
215 // Simplify image codes
216 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
218 $URLSearchString = "^\[\]";
219 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
220 if ((count($pictures) == 1) && !$has_title) {
221 // Checking, if the link goes to a picture
222 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
225 // Sometimes photo posts to the own album are not detected at the start.
226 // So we seem to cannot use the cache for these cases. That's strange.
227 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/"))
228 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
230 if ($data["type"] == "photo") {
231 $post["type"] = "photo";
232 if (isset($data["images"][0])) {
233 $post["image"] = $data["images"][0]["src"];
234 $post["url"] = $data["url"];
236 $post["image"] = $data["url"];
238 $post["preview"] = $pictures[0][2];
239 $post["text"] = str_replace($pictures[0][0], "", $body);
241 $imgdata = Image::getInfoFromURL($pictures[0][1]);
242 if (substr($imgdata["mime"], 0, 6) == "image/") {
243 $post["type"] = "photo";
244 $post["image"] = $pictures[0][1];
245 $post["preview"] = $pictures[0][2];
246 $post["text"] = str_replace($pictures[0][0], "", $body);
249 } elseif (count($pictures) > 0) {
250 $post["type"] = "link";
251 $post["url"] = $plink;
252 $post["image"] = $pictures[0][2];
253 $post["text"] = $body;
255 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
256 if ((count($pictures) == 1) && !$has_title) {
257 $post["type"] = "photo";
258 $post["image"] = $pictures[0][1];
259 $post["text"] = str_replace($pictures[0][0], "", $body);
260 } elseif (count($pictures) > 0) {
261 $post["type"] = "link";
262 $post["url"] = $plink;
263 $post["image"] = $pictures[0][1];
264 $post["text"] = $body;
268 // Test for the external links
269 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
270 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
272 $links = array_merge($links1, $links2);
274 // If there is only a single one, then use it.
275 // This should cover link posts via API.
276 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
277 $post["type"] = "link";
278 $post["text"] = trim($body);
279 $post["url"] = $links[0][1];
282 // Now count the number of external media links
283 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
284 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
285 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
286 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
288 // Add them to the other external links
289 $links = array_merge($links, $links1, $links2, $links3, $links4);
291 // Are there more than one?
292 if (count($links) > 1) {
293 // The post will be the type "text", which means a blog post
294 unset($post["type"]);
295 $post["url"] = $plink;
298 if (!isset($post["type"])) {
299 $post["type"] = "text";
300 $post["text"] = trim($body);
302 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
303 $data = ParseUrl::getSiteinfoCached($post["url"], true);
305 if (isset($data["images"][0]))
306 $post["image"] = $data["images"][0]["src"];
319 * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
321 function shortenmsg($msg, $limit)
323 $lines = explode("\n", $msg);
325 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
326 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
327 foreach ($lines AS $row => $line) {
328 if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
329 $msg = trim($msg . "\n" . $line);
330 } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
331 // Is the new message empty by now or is it a reshared message?
332 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
342 * @brief Convert a message into plaintext for connectors to other networks
344 * @param array $b The message array that is about to be posted
345 * @param int $limit The maximum number of characters when posting to that network
346 * @param bool $includedlinks Has an attached link to be included into the message?
347 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
348 * @param string $target_network Name of the network where the post should go to.
350 * @return string The converted message
352 function plaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
354 // Remove the hash tags
355 $URLSearchString = "^\[\]";
356 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
358 // Add an URL element if the text contains a raw link
359 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
361 // Remove the abstract
362 $body = remove_abstract($body);
364 // At first look at data that is attached via "type-..." stuff
365 // This will hopefully replaced with a dedicated bbcode later
366 //$post = get_attached_data($b["body"]);
367 $post = get_attached_data($body, $b);
369 if (($b["title"] != "") && ($post["text"] != ""))
370 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
371 elseif ($b["title"] != "")
372 $post["text"] = trim($b["title"]);
376 // Fetch the abstract from the given target network
377 if ($target_network != "") {
378 $default_abstract = fetch_abstract($b["body"]);
379 $abstract = fetch_abstract($b["body"], $target_network);
381 // If we post to a network with no limit we only fetch
382 // an abstract exactly for this network
383 if (($limit == 0) && ($abstract == $default_abstract))
386 } else // Try to guess the correct target network
389 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
392 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
395 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
397 default: // We don't know the exact target.
398 // We fetch an abstract since there is a posting limit.
400 $abstract = fetch_abstract($b["body"]);
403 if ($abstract != "") {
404 $post["text"] = $abstract;
406 if ($post["type"] == "text") {
407 $post["type"] = "link";
408 $post["url"] = $b["plink"];
412 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
413 $msg = html2plain($html, 0, true);
414 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
417 if ($includedlinks) {
418 if ($post["type"] == "link")
419 $link = $post["url"];
420 elseif ($post["type"] == "text")
421 $link = $post["url"];
422 elseif ($post["type"] == "video")
423 $link = $post["url"];
424 elseif ($post["type"] == "photo")
425 $link = $post["image"];
427 if (($msg == "") && isset($post["title"]))
428 $msg = trim($post["title"]);
430 if (($msg == "") && isset($post["description"]))
431 $msg = trim($post["description"]);
433 // If the link is already contained in the post, then it neeedn't to be added again
434 // But: if the link is beyond the limit, then it has to be added.
435 if (($link != "") && strstr($msg, $link)) {
436 $pos = strpos($msg, $link);
438 // Will the text be shortened in the link?
439 // Or is the link the last item in the post?
440 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg))))
441 $msg = trim(str_replace($link, "", $msg));
442 elseif (($limit == 0) || ($pos < $limit)) {
443 // The limit has to be increased since it will be shortened - but not now
444 // Only do it with Twitter (htmlmode = 8)
445 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8))
446 $limit = $limit - 23 + strlen($link);
450 if ($post["type"] == "text")
457 // Reduce multiple spaces
458 // When posted to a network with limited space, we try to gain space where possible
459 while (strpos($msg, " ") !== false)
460 $msg = str_replace(" ", " ", $msg);
462 // Twitter is using its own limiter, so we always assume that shortened links will have this length
463 if (iconv_strlen($link, "UTF-8") > 0)
464 $limit = $limit - 23;
466 if (iconv_strlen($msg, "UTF-8") > $limit) {
468 if (($post["type"] == "text") && isset($post["url"])) {
469 $post["url"] = $b["plink"];
470 } elseif (!isset($post["url"])) {
471 $limit = $limit - 23;
472 $post["url"] = $b["plink"];
473 // Which purpose has this line? It is now uncommented, but left as a reminder
474 //} elseif (strpos($b["body"], "[share") !== false) {
475 // $post["url"] = $b["plink"];
476 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
477 $post["url"] = $b["plink"];
479 $msg = shortenmsg($msg, $limit);
483 $post["text"] = trim($msg);