3 * @file src/Content/Text/Plaintext.php
5 namespace Friendica\Content\Text;
8 use Friendica\Core\PConfig;
9 use Friendica\Object\Image;
10 use Friendica\Util\ParseUrl;
12 require_once "include/bbcode.php";
13 require_once "include/html2plain.php";
14 require_once "include/network.php";
19 * @brief Fetches attachment data that were generated the old way
21 * @param string $body Message body
23 * 'type' -> Message type ("link", "video", "photo")
24 * 'text' -> Text before the shared message
25 * 'after' -> Text after the shared message
26 * 'image' -> Preview image of the message
27 * 'url' -> Url to the attached message
28 * 'title' -> Title of the attachment
29 * 'description' -> Description of the attachment
31 private static function getOldAttachmentData($body)
35 // Simplify image codes
36 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
38 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism", $body, $attached, PREG_SET_ORDER)) {
39 foreach ($attached as $data) {
40 if (!in_array($data[1], ["type-link", "type-video", "type-photo"])) {
44 $post["type"] = substr($data[1], 5);
46 $pos = strpos($body, $data[0]);
48 $post["text"] = trim(substr($body, 0, $pos));
49 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
51 $post["text"] = trim(str_replace($data[0], "", $body));
54 $attacheddata = $data[2];
56 $URLSearchString = "^\[\]";
58 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
60 $picturedata = Image::getInfoFromURL($matches[1]);
62 if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
63 $post["image"] = $matches[1];
65 $post["preview"] = $matches[1];
69 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
70 $post["url"] = $matches[1];
71 $post["title"] = $matches[2];
73 if (($post["url"] == "") && (in_array($post["type"], ["link", "video"]))
74 && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
75 $post["url"] = $matches[1];
78 // Search for description
79 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches)) {
80 $post["description"] = $matches[1];
88 * @brief Fetches attachment data that were generated with the "attachment" element
90 * @param string $body Message body
92 * 'type' -> Message type ("link", "video", "photo")
93 * 'text' -> Text before the shared message
94 * 'after' -> Text after the shared message
95 * 'image' -> Preview image of the message
96 * 'url' -> Url to the attached message
97 * 'title' -> Title of the attachment
98 * 'description' -> Description of the attachment
100 public static function getAttachmentData($body)
104 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
105 return self::getOldAttachmentData($body);
108 $attributes = $match[2];
110 $data["text"] = trim($match[1]);
113 preg_match("/type='(.*?)'/ism", $attributes, $matches);
114 if (x($matches, 1)) {
115 $type = strtolower($matches[1]);
118 preg_match('/type="(.*?)"/ism', $attributes, $matches);
119 if (x($matches, 1)) {
120 $type = strtolower($matches[1]);
127 if (!in_array($type, ["link", "audio", "photo", "video"])) {
132 $data["type"] = $type;
136 preg_match("/url='(.*?)'/ism", $attributes, $matches);
137 if (x($matches, 1)) {
141 preg_match('/url="(.*?)"/ism', $attributes, $matches);
142 if (x($matches, 1)) {
147 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
151 preg_match("/title='(.*?)'/ism", $attributes, $matches);
152 if (x($matches, 1)) {
153 $title = $matches[1];
156 preg_match('/title="(.*?)"/ism', $attributes, $matches);
157 if (x($matches, 1)) {
158 $title = $matches[1];
162 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
163 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
164 $title = str_replace(["[", "]"], ["[", "]"], $title);
165 $data["title"] = $title;
169 preg_match("/image='(.*?)'/ism", $attributes, $matches);
170 if (x($matches, 1)) {
171 $image = $matches[1];
174 preg_match('/image="(.*?)"/ism', $attributes, $matches);
175 if (x($matches, 1)) {
176 $image = $matches[1];
180 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
184 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
185 if (x($matches, 1)) {
186 $preview = $matches[1];
189 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
190 if (x($matches, 1)) {
191 $preview = $matches[1];
194 if ($preview != "") {
195 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
198 $data["description"] = trim($match[3]);
200 $data["after"] = trim($match[4]);
205 public static function getAttachedData($body, $item = [])
209 - type: link, video, photo
217 $has_title = !empty($item['title']);
218 $plink = (!empty($item['plink']) ? $item['plink'] : '');
219 $post = self::getAttachmentData($body);
221 // if nothing is found, it maybe having an image.
222 if (!isset($post["type"])) {
223 // Simplify image codes
224 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
226 $URLSearchString = "^\[\]";
227 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
228 if ((count($pictures) == 1) && !$has_title) {
229 // Checking, if the link goes to a picture
230 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
233 // Sometimes photo posts to the own album are not detected at the start.
234 // So we seem to cannot use the cache for these cases. That's strange.
235 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/")) {
236 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
239 if ($data["type"] == "photo") {
240 $post["type"] = "photo";
241 if (isset($data["images"][0])) {
242 $post["image"] = $data["images"][0]["src"];
243 $post["url"] = $data["url"];
245 $post["image"] = $data["url"];
248 $post["preview"] = $pictures[0][2];
249 $post["text"] = str_replace($pictures[0][0], "", $body);
251 $imgdata = Image::getInfoFromURL($pictures[0][1]);
252 if (substr($imgdata["mime"], 0, 6) == "image/") {
253 $post["type"] = "photo";
254 $post["image"] = $pictures[0][1];
255 $post["preview"] = $pictures[0][2];
256 $post["text"] = str_replace($pictures[0][0], "", $body);
259 } elseif (count($pictures) > 0) {
260 $post["type"] = "link";
261 $post["url"] = $plink;
262 $post["image"] = $pictures[0][2];
263 $post["text"] = $body;
265 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
266 if ((count($pictures) == 1) && !$has_title) {
267 $post["type"] = "photo";
268 $post["image"] = $pictures[0][1];
269 $post["text"] = str_replace($pictures[0][0], "", $body);
270 } elseif (count($pictures) > 0) {
271 $post["type"] = "link";
272 $post["url"] = $plink;
273 $post["image"] = $pictures[0][1];
274 $post["text"] = $body;
278 // Test for the external links
279 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
280 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
282 $links = array_merge($links1, $links2);
284 // If there is only a single one, then use it.
285 // This should cover link posts via API.
286 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
287 $post["type"] = "link";
288 $post["text"] = trim($body);
289 $post["url"] = $links[0][1];
292 // Now count the number of external media links
293 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
294 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
295 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
296 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
298 // Add them to the other external links
299 $links = array_merge($links, $links1, $links2, $links3, $links4);
301 // Are there more than one?
302 if (count($links) > 1) {
303 // The post will be the type "text", which means a blog post
304 unset($post["type"]);
305 $post["url"] = $plink;
308 if (!isset($post["type"])) {
309 $post["type"] = "text";
310 $post["text"] = trim($body);
312 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
313 $data = ParseUrl::getSiteinfoCached($post["url"], true);
315 if (isset($data["images"][0])) {
316 $post["image"] = $data["images"][0]["src"];
330 * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
332 public static function shortenMsg($msg, $limit)
334 $lines = explode("\n", $msg);
336 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
337 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
338 foreach ($lines as $row => $line) {
339 if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
340 $msg = trim($msg . "\n" . $line);
341 } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
342 // Is the new message empty by now or is it a reshared message?
343 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
353 * @brief Convert a message into plaintext for connectors to other networks
355 * @param array $b The message array that is about to be posted
356 * @param int $limit The maximum number of characters when posting to that network
357 * @param bool $includedlinks Has an attached link to be included into the message?
358 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
359 * @param string $target_network Name of the network where the post should go to.
361 * @return string The converted message
363 public static function toPlaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "")
365 // Remove the hash tags
366 $URLSearchString = "^\[\]";
367 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
369 // Add an URL element if the text contains a raw link
370 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
372 // Remove the abstract
373 $body = remove_abstract($body);
375 // At first look at data that is attached via "type-..." stuff
376 // This will hopefully replaced with a dedicated bbcode later
377 //$post = self::getAttachedData($b["body"]);
378 $post = self::getAttachedData($body, $b);
380 if (($b["title"] != "") && ($post["text"] != "")) {
381 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
382 } elseif ($b["title"] != "") {
383 $post["text"] = trim($b["title"]);
388 // Fetch the abstract from the given target network
389 if ($target_network != "") {
390 $default_abstract = fetch_abstract($b["body"]);
391 $abstract = fetch_abstract($b["body"], $target_network);
393 // If we post to a network with no limit we only fetch
394 // an abstract exactly for this network
395 if (($limit == 0) && ($abstract == $default_abstract)) {
398 } else {// Try to guess the correct target network
401 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
404 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
407 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
409 default: // We don't know the exact target.
410 // We fetch an abstract since there is a posting limit.
412 $abstract = fetch_abstract($b["body"]);
417 if ($abstract != "") {
418 $post["text"] = $abstract;
420 if ($post["type"] == "text") {
421 $post["type"] = "link";
422 $post["url"] = $b["plink"];
426 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
427 $msg = html2plain($html, 0, true);
428 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
431 if ($includedlinks) {
432 if ($post["type"] == "link") {
433 $link = $post["url"];
434 } elseif ($post["type"] == "text") {
435 $link = $post["url"];
436 } elseif ($post["type"] == "video") {
437 $link = $post["url"];
438 } elseif ($post["type"] == "photo") {
439 $link = $post["image"];
442 if (($msg == "") && isset($post["title"])) {
443 $msg = trim($post["title"]);
446 if (($msg == "") && isset($post["description"])) {
447 $msg = trim($post["description"]);
450 // If the link is already contained in the post, then it neeedn't to be added again
451 // But: if the link is beyond the limit, then it has to be added.
452 if (($link != "") && strstr($msg, $link)) {
453 $pos = strpos($msg, $link);
455 // Will the text be shortened in the link?
456 // Or is the link the last item in the post?
457 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
458 $msg = trim(str_replace($link, "", $msg));
459 } elseif (($limit == 0) || ($pos < $limit)) {
460 // The limit has to be increased since it will be shortened - but not now
461 // Only do it with Twitter (htmlmode = 8)
462 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
463 $limit = $limit - 23 + strlen($link);
468 if ($post["type"] == "text") {
476 // Reduce multiple spaces
477 // When posted to a network with limited space, we try to gain space where possible
478 while (strpos($msg, " ") !== false) {
479 $msg = str_replace(" ", " ", $msg);
482 // Twitter is using its own limiter, so we always assume that shortened links will have this length
483 if (iconv_strlen($link, "UTF-8") > 0) {
484 $limit = $limit - 23;
487 if (iconv_strlen($msg, "UTF-8") > $limit) {
488 if (($post["type"] == "text") && isset($post["url"])) {
489 $post["url"] = $b["plink"];
490 } elseif (!isset($post["url"])) {
491 $limit = $limit - 23;
492 $post["url"] = $b["plink"];
493 // Which purpose has this line? It is now uncommented, but left as a reminder
494 //} elseif (strpos($b["body"], "[share") !== false) {
495 // $post["url"] = $b["plink"];
496 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
497 $post["url"] = $b["plink"];
499 $msg = self::shortenMsg($msg, $limit);
503 $post["text"] = trim($msg);