3 * @file src/Content/Text/BBCode.php
5 namespace Friendica\Content\Text;
8 use Friendica\Content\Text\Plaintext;
9 use Friendica\Core\PConfig;
10 use Friendica\Object\Image;
11 use Friendica\Util\ParseUrl;
13 require_once "include/bbcode.php";
14 require_once "include/html2plain.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"];
324 * @brief Convert a message into plaintext for connectors to other networks
326 * @param array $b The message array that is about to be posted
327 * @param int $limit The maximum number of characters when posting to that network
328 * @param bool $includedlinks Has an attached link to be included into the message?
329 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
330 * @param string $target_network Name of the network where the post should go to.
332 * @return string The converted message
334 public static function toPlaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "")
336 // Remove the hash tags
337 $URLSearchString = "^\[\]";
338 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
340 // Add an URL element if the text contains a raw link
341 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
343 // Remove the abstract
344 $body = remove_abstract($body);
346 // At first look at data that is attached via "type-..." stuff
347 // This will hopefully replaced with a dedicated bbcode later
348 //$post = self::getAttachedData($b["body"]);
349 $post = self::getAttachedData($body, $b);
351 if (($b["title"] != "") && ($post["text"] != "")) {
352 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
353 } elseif ($b["title"] != "") {
354 $post["text"] = trim($b["title"]);
359 // Fetch the abstract from the given target network
360 if ($target_network != "") {
361 $default_abstract = fetch_abstract($b["body"]);
362 $abstract = fetch_abstract($b["body"], $target_network);
364 // If we post to a network with no limit we only fetch
365 // an abstract exactly for this network
366 if (($limit == 0) && ($abstract == $default_abstract)) {
369 } else {// Try to guess the correct target network
372 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
375 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
378 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
380 default: // We don't know the exact target.
381 // We fetch an abstract since there is a posting limit.
383 $abstract = fetch_abstract($b["body"]);
388 if ($abstract != "") {
389 $post["text"] = $abstract;
391 if ($post["type"] == "text") {
392 $post["type"] = "link";
393 $post["url"] = $b["plink"];
397 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
398 $msg = html2plain($html, 0, true);
399 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
402 if ($includedlinks) {
403 if ($post["type"] == "link") {
404 $link = $post["url"];
405 } elseif ($post["type"] == "text") {
406 $link = $post["url"];
407 } elseif ($post["type"] == "video") {
408 $link = $post["url"];
409 } elseif ($post["type"] == "photo") {
410 $link = $post["image"];
413 if (($msg == "") && isset($post["title"])) {
414 $msg = trim($post["title"]);
417 if (($msg == "") && isset($post["description"])) {
418 $msg = trim($post["description"]);
421 // If the link is already contained in the post, then it neeedn't to be added again
422 // But: if the link is beyond the limit, then it has to be added.
423 if (($link != "") && strstr($msg, $link)) {
424 $pos = strpos($msg, $link);
426 // Will the text be shortened in the link?
427 // Or is the link the last item in the post?
428 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
429 $msg = trim(str_replace($link, "", $msg));
430 } elseif (($limit == 0) || ($pos < $limit)) {
431 // The limit has to be increased since it will be shortened - but not now
432 // Only do it with Twitter (htmlmode = 8)
433 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
434 $limit = $limit - 23 + strlen($link);
439 if ($post["type"] == "text") {
447 // Reduce multiple spaces
448 // When posted to a network with limited space, we try to gain space where possible
449 while (strpos($msg, " ") !== false) {
450 $msg = str_replace(" ", " ", $msg);
453 // Twitter is using its own limiter, so we always assume that shortened links will have this length
454 if (iconv_strlen($link, "UTF-8") > 0) {
455 $limit = $limit - 23;
458 if (iconv_strlen($msg, "UTF-8") > $limit) {
459 if (($post["type"] == "text") && isset($post["url"])) {
460 $post["url"] = $b["plink"];
461 } elseif (!isset($post["url"])) {
462 $limit = $limit - 23;
463 $post["url"] = $b["plink"];
464 // Which purpose has this line? It is now uncommented, but left as a reminder
465 //} elseif (strpos($b["body"], "[share") !== false) {
466 // $post["url"] = $b["plink"];
467 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
468 $post["url"] = $b["plink"];
470 $msg = Plaintext::shorten($msg, $limit);
474 $post["text"] = trim($msg);