3 * @file src/Content/Text/BBCode.php
5 namespace Friendica\Content\Text;
8 use Friendica\Content\Text\Plaintext;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Object\Image;
14 use Friendica\Util\ParseUrl;
15 use Friendica\Util\Network;
17 require_once "include/bbcode.php";
18 require_once "include/html2plain.php";
23 * @brief Fetches attachment data that were generated the old way
25 * @param string $body Message body
27 * 'type' -> Message type ("link", "video", "photo")
28 * 'text' -> Text before the shared message
29 * 'after' -> Text after the shared message
30 * 'image' -> Preview image of the message
31 * 'url' -> Url to the attached message
32 * 'title' -> Title of the attachment
33 * 'description' -> Description of the attachment
35 private static function getOldAttachmentData($body)
39 // Simplify image codes
40 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
42 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism", $body, $attached, PREG_SET_ORDER)) {
43 foreach ($attached as $data) {
44 if (!in_array($data[1], ["type-link", "type-video", "type-photo"])) {
48 $post["type"] = substr($data[1], 5);
50 $pos = strpos($body, $data[0]);
52 $post["text"] = trim(substr($body, 0, $pos));
53 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
55 $post["text"] = trim(str_replace($data[0], "", $body));
58 $attacheddata = $data[2];
60 $URLSearchString = "^\[\]";
62 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
64 $picturedata = Image::getInfoFromURL($matches[1]);
66 if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
67 $post["image"] = $matches[1];
69 $post["preview"] = $matches[1];
73 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
74 $post["url"] = $matches[1];
75 $post["title"] = $matches[2];
77 if (($post["url"] == "") && (in_array($post["type"], ["link", "video"]))
78 && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
79 $post["url"] = $matches[1];
82 // Search for description
83 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches)) {
84 $post["description"] = $matches[1];
92 * @brief Fetches attachment data that were generated with the "attachment" element
94 * @param string $body Message body
96 * 'type' -> Message type ("link", "video", "photo")
97 * 'text' -> Text before the shared message
98 * 'after' -> Text after the shared message
99 * 'image' -> Preview image of the message
100 * 'url' -> Url to the attached message
101 * 'title' -> Title of the attachment
102 * 'description' -> Description of the attachment
104 public static function getAttachmentData($body)
108 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
109 return self::getOldAttachmentData($body);
112 $attributes = $match[2];
114 $data["text"] = trim($match[1]);
117 preg_match("/type='(.*?)'/ism", $attributes, $matches);
118 if (x($matches, 1)) {
119 $type = strtolower($matches[1]);
122 preg_match('/type="(.*?)"/ism', $attributes, $matches);
123 if (x($matches, 1)) {
124 $type = strtolower($matches[1]);
131 if (!in_array($type, ["link", "audio", "photo", "video"])) {
136 $data["type"] = $type;
140 preg_match("/url='(.*?)'/ism", $attributes, $matches);
141 if (x($matches, 1)) {
145 preg_match('/url="(.*?)"/ism', $attributes, $matches);
146 if (x($matches, 1)) {
151 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
155 preg_match("/title='(.*?)'/ism", $attributes, $matches);
156 if (x($matches, 1)) {
157 $title = $matches[1];
160 preg_match('/title="(.*?)"/ism', $attributes, $matches);
161 if (x($matches, 1)) {
162 $title = $matches[1];
166 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
167 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
168 $title = str_replace(["[", "]"], ["[", "]"], $title);
169 $data["title"] = $title;
173 preg_match("/image='(.*?)'/ism", $attributes, $matches);
174 if (x($matches, 1)) {
175 $image = $matches[1];
178 preg_match('/image="(.*?)"/ism', $attributes, $matches);
179 if (x($matches, 1)) {
180 $image = $matches[1];
184 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
188 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
189 if (x($matches, 1)) {
190 $preview = $matches[1];
193 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
194 if (x($matches, 1)) {
195 $preview = $matches[1];
198 if ($preview != "") {
199 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
202 $data["description"] = trim($match[3]);
204 $data["after"] = trim($match[4]);
209 public static function getAttachedData($body, $item = [])
213 - type: link, video, photo
221 $has_title = !empty($item['title']);
222 $plink = (!empty($item['plink']) ? $item['plink'] : '');
223 $post = self::getAttachmentData($body);
225 // if nothing is found, it maybe having an image.
226 if (!isset($post["type"])) {
227 // Simplify image codes
228 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
230 $URLSearchString = "^\[\]";
231 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
232 if ((count($pictures) == 1) && !$has_title) {
233 // Checking, if the link goes to a picture
234 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
237 // Sometimes photo posts to the own album are not detected at the start.
238 // So we seem to cannot use the cache for these cases. That's strange.
239 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/")) {
240 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
243 if ($data["type"] == "photo") {
244 $post["type"] = "photo";
245 if (isset($data["images"][0])) {
246 $post["image"] = $data["images"][0]["src"];
247 $post["url"] = $data["url"];
249 $post["image"] = $data["url"];
252 $post["preview"] = $pictures[0][2];
253 $post["text"] = str_replace($pictures[0][0], "", $body);
255 $imgdata = Image::getInfoFromURL($pictures[0][1]);
256 if (substr($imgdata["mime"], 0, 6) == "image/") {
257 $post["type"] = "photo";
258 $post["image"] = $pictures[0][1];
259 $post["preview"] = $pictures[0][2];
260 $post["text"] = str_replace($pictures[0][0], "", $body);
263 } elseif (count($pictures) > 0) {
264 $post["type"] = "link";
265 $post["url"] = $plink;
266 $post["image"] = $pictures[0][2];
267 $post["text"] = $body;
269 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
270 if ((count($pictures) == 1) && !$has_title) {
271 $post["type"] = "photo";
272 $post["image"] = $pictures[0][1];
273 $post["text"] = str_replace($pictures[0][0], "", $body);
274 } elseif (count($pictures) > 0) {
275 $post["type"] = "link";
276 $post["url"] = $plink;
277 $post["image"] = $pictures[0][1];
278 $post["text"] = $body;
282 // Test for the external links
283 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
284 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
286 $links = array_merge($links1, $links2);
288 // If there is only a single one, then use it.
289 // This should cover link posts via API.
290 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
291 $post["type"] = "link";
292 $post["text"] = trim($body);
293 $post["url"] = $links[0][1];
296 // Now count the number of external media links
297 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
298 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
299 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
300 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
302 // Add them to the other external links
303 $links = array_merge($links, $links1, $links2, $links3, $links4);
305 // Are there more than one?
306 if (count($links) > 1) {
307 // The post will be the type "text", which means a blog post
308 unset($post["type"]);
309 $post["url"] = $plink;
312 if (!isset($post["type"])) {
313 $post["type"] = "text";
314 $post["text"] = trim($body);
316 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
317 $data = ParseUrl::getSiteinfoCached($post["url"], true);
319 if (isset($data["images"][0])) {
320 $post["image"] = $data["images"][0]["src"];
328 * @brief Convert a message into plaintext for connectors to other networks
330 * @param array $b The message array that is about to be posted
331 * @param int $limit The maximum number of characters when posting to that network
332 * @param bool $includedlinks Has an attached link to be included into the message?
333 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
334 * @param string $target_network Name of the network where the post should go to.
336 * @return string The converted message
338 public static function toPlaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "")
340 // Remove the hash tags
341 $URLSearchString = "^\[\]";
342 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
344 // Add an URL element if the text contains a raw link
345 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
347 // Remove the abstract
348 $body = remove_abstract($body);
350 // At first look at data that is attached via "type-..." stuff
351 // This will hopefully replaced with a dedicated bbcode later
352 //$post = self::getAttachedData($b["body"]);
353 $post = self::getAttachedData($body, $b);
355 if (($b["title"] != "") && ($post["text"] != "")) {
356 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
357 } elseif ($b["title"] != "") {
358 $post["text"] = trim($b["title"]);
363 // Fetch the abstract from the given target network
364 if ($target_network != "") {
365 $default_abstract = fetch_abstract($b["body"]);
366 $abstract = fetch_abstract($b["body"], $target_network);
368 // If we post to a network with no limit we only fetch
369 // an abstract exactly for this network
370 if (($limit == 0) && ($abstract == $default_abstract)) {
373 } else {// Try to guess the correct target network
376 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
379 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
382 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
384 default: // We don't know the exact target.
385 // We fetch an abstract since there is a posting limit.
387 $abstract = fetch_abstract($b["body"]);
392 if ($abstract != "") {
393 $post["text"] = $abstract;
395 if ($post["type"] == "text") {
396 $post["type"] = "link";
397 $post["url"] = $b["plink"];
401 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
402 $msg = html2plain($html, 0, true);
403 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
406 if ($includedlinks) {
407 if ($post["type"] == "link") {
408 $link = $post["url"];
409 } elseif ($post["type"] == "text") {
410 $link = $post["url"];
411 } elseif ($post["type"] == "video") {
412 $link = $post["url"];
413 } elseif ($post["type"] == "photo") {
414 $link = $post["image"];
417 if (($msg == "") && isset($post["title"])) {
418 $msg = trim($post["title"]);
421 if (($msg == "") && isset($post["description"])) {
422 $msg = trim($post["description"]);
425 // If the link is already contained in the post, then it neeedn't to be added again
426 // But: if the link is beyond the limit, then it has to be added.
427 if (($link != "") && strstr($msg, $link)) {
428 $pos = strpos($msg, $link);
430 // Will the text be shortened in the link?
431 // Or is the link the last item in the post?
432 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
433 $msg = trim(str_replace($link, "", $msg));
434 } elseif (($limit == 0) || ($pos < $limit)) {
435 // The limit has to be increased since it will be shortened - but not now
436 // Only do it with Twitter (htmlmode = 8)
437 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
438 $limit = $limit - 23 + strlen($link);
443 if ($post["type"] == "text") {
451 // Reduce multiple spaces
452 // When posted to a network with limited space, we try to gain space where possible
453 while (strpos($msg, " ") !== false) {
454 $msg = str_replace(" ", " ", $msg);
457 // Twitter is using its own limiter, so we always assume that shortened links will have this length
458 if (iconv_strlen($link, "UTF-8") > 0) {
459 $limit = $limit - 23;
462 if (iconv_strlen($msg, "UTF-8") > $limit) {
463 if (($post["type"] == "text") && isset($post["url"])) {
464 $post["url"] = $b["plink"];
465 } elseif (!isset($post["url"])) {
466 $limit = $limit - 23;
467 $post["url"] = $b["plink"];
468 // Which purpose has this line? It is now uncommented, but left as a reminder
469 //} elseif (strpos($b["body"], "[share") !== false) {
470 // $post["url"] = $b["plink"];
471 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
472 $post["url"] = $b["plink"];
474 $msg = Plaintext::shorten($msg, $limit);
478 $post["text"] = trim($msg);
483 public static function scaleExternalImages($srctext, $include_link = true, $scale_replace = false)
485 // Suppress "view full size"
486 if (intval(Config::get('system', 'no_view_full_size'))) {
487 $include_link = false;
490 // Picture addresses can contain special characters
491 $s = htmlspecialchars_decode($srctext);
494 $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
496 foreach ($matches as $mtch) {
497 logger('scale_external_image: ' . $mtch[1]);
499 $hostname = str_replace('www.', '', substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3));
500 if (stristr($mtch[1], $hostname)) {
504 // $scale_replace, if passed, is an array of two elements. The
505 // first is the name of the full-size image. The second is the
506 // name of a remote, scaled-down version of the full size image.
507 // This allows Friendica to display the smaller remote image if
508 // one exists, while still linking to the full-size image
509 if ($scale_replace) {
510 $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
514 $i = Network::fetchUrl($scaled);
519 // guess mimetype from headers or filename
520 $type = Image::guessType($mtch[1], true);
523 $Image = new Image($i, $type);
524 if ($Image->isValid()) {
525 $orig_width = $Image->getWidth();
526 $orig_height = $Image->getHeight();
528 if ($orig_width > 640 || $orig_height > 640) {
529 $Image->scaleDown(640);
530 $new_width = $Image->getWidth();
531 $new_height = $Image->getHeight();
532 logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
535 '[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
536 . "\n" . (($include_link)
537 ? '[url=' . $mtch[1] . ']' . L10n::t('view full size') . '[/url]' . "\n"
541 logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
548 // replace the special char encoding
549 $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
554 * The purpose of this function is to apply system message length limits to
555 * imported messages without including any embedded photos in the length
557 * @brief Truncates imported message body string length to max_import_size
558 * @param string $body
561 public static function limitBodySize($body)
563 $maxlen = get_max_import_size();
565 // If the length of the body, including the embedded images, is smaller
566 // than the maximum, then don't waste time looking for the images
567 if ($maxlen && (strlen($body) > $maxlen)) {
569 logger('the total body length exceeds the limit', LOGGER_DEBUG);
575 $img_start = strpos($orig_body, '[img');
576 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
577 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
578 while (($img_st_close !== false) && ($img_end !== false)) {
580 $img_st_close++; // make it point to AFTER the closing bracket
581 $img_end += $img_start;
582 $img_end += strlen('[/img]');
584 if (!strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
585 // This is an embedded image
587 if (($textlen + $img_start) > $maxlen) {
588 if ($textlen < $maxlen) {
589 logger('the limit happens before an embedded image', LOGGER_DEBUG);
590 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
594 $new_body = $new_body . substr($orig_body, 0, $img_start);
595 $textlen += $img_start;
598 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
601 if (($textlen + $img_end) > $maxlen) {
602 if ($textlen < $maxlen) {
603 logger('the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
604 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
608 $new_body = $new_body . substr($orig_body, 0, $img_end);
609 $textlen += $img_end;
612 $orig_body = substr($orig_body, $img_end);
614 if ($orig_body === false) {
615 // in case the body ends on a closing image tag
619 $img_start = strpos($orig_body, '[img');
620 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
621 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
624 if (($textlen + strlen($orig_body)) > $maxlen) {
625 if ($textlen < $maxlen) {
626 logger('the limit happens after the end of the last image', LOGGER_DEBUG);
627 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
630 logger('the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
631 $new_body = $new_body . $orig_body;