]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Add Trending Tags widget + template
[friendica.git] / src / Content / Text / BBCode.php
index da09e13dd9b8dfc5a243a9480d4b3de5b9207186..12497d66516ebbf44b6f127906b7c97959a6c97c 100644 (file)
@@ -21,6 +21,7 @@ use Friendica\Core\Renderer;
 use Friendica\Core\System;
 use Friendica\Model\Contact;
 use Friendica\Model\Event;
+use Friendica\Model\Photo;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
 use Friendica\Util\Map;
@@ -238,12 +239,32 @@ class BBCode extends BaseObject
                $plink = defaults($item, 'plink', '');
                $post = self::getAttachmentData($body);
 
+               // Get all linked images with alternative image description
+               if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
+                       foreach ($pictures as $picture) {
+                               if (Photo::isLocal($picture[1])) {
+                                       $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2]];
+                               }
+                       }
+                       if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
+                               $post['image_description'] = $post['images'][0]['description'];
+                       }
+               }
+
+               if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
+                       foreach ($pictures as $picture) {
+                               if (Photo::isLocal($picture[1])) {
+                                       $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => ''];
+                               }
+                       }
+               }
+
                // if nothing is found, it maybe having an image.
                if (!isset($post["type"])) {
                        // Simplify image codes
                        $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
-
                        $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
+                       $post["text"] = $body;
 
                        if (preg_match_all("(\[url=(.*?)\]\s*\[img\](.*?)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
                                if ((count($pictures) == 1) && !$has_title) {
@@ -288,6 +309,10 @@ class BBCode extends BaseObject
                                        $post["url"] = $plink;
                                        $post["image"] = $pictures[0][2];
                                        $post["text"] = $body;
+
+                                       foreach ($pictures as $picture) {
+                                               $post["text"] = trim(str_replace($picture[0], "", $post["text"]));
+                                       }
                                }
                        } elseif (preg_match_all("(\[img\](.*?)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
                                if ((count($pictures) == 1) && !$has_title) {
@@ -299,12 +324,16 @@ class BBCode extends BaseObject
                                        $post["url"] = $plink;
                                        $post["image"] = $pictures[0][1];
                                        $post["text"] = $body;
+
+                                       foreach ($pictures as $picture) {
+                                               $post["text"] = trim(str_replace($picture[0], "", $post["text"]));
+                                       }
                                }
                        }
 
                        // Test for the external links
-                       preg_match_all("(\[url\](.*?)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
-                       preg_match_all("(\[url\=(.*?)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
+                       preg_match_all("(\[url\](.*?)\[\/url\])ism", $post["text"], $links1, PREG_SET_ORDER);
+                       preg_match_all("(\[url\=(.*?)\].*?\[\/url\])ism", $post["text"], $links2, PREG_SET_ORDER);
 
                        $links = array_merge($links1, $links2);
 
@@ -312,15 +341,14 @@ class BBCode extends BaseObject
                        // This should cover link posts via API.
                        if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
                                $post["type"] = "link";
-                               $post["text"] = trim($body);
                                $post["url"] = $links[0][1];
                        }
 
                        // Now count the number of external media links
-                       preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
-                       preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
-                       preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
-                       preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
+                       preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $post["text"], $links1, PREG_SET_ORDER);
+                       preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $post["text"], $links2, PREG_SET_ORDER);
+                       preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $post["text"], $links3, PREG_SET_ORDER);
+                       preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $post["text"], $links4, PREG_SET_ORDER);
 
                        // Add them to the other external links
                        $links = array_merge($links, $links1, $links2, $links3, $links4);
@@ -357,10 +385,7 @@ class BBCode extends BaseObject
         */
        public static function toPlaintext($text, $keep_urls = true)
        {
-               $naked_text = preg_replace('/\[.+?\]/','', $text);
-               if (!$keep_urls) {
-                       $naked_text = preg_replace('#https?\://[^\s<]+[^\s\.\)]#i', '', $naked_text);
-               }
+               $naked_text = HTML::toPlaintext(BBCode::convert($text, false, 0, true), 0, !$keep_urls);
 
                return $naked_text;
        }
@@ -905,7 +930,17 @@ class BBCode extends BaseObject
                                // We only call this so that a previously unknown contact can be added.
                                // This is important for the function "Model\Contact::getDetailsByURL()".
                                // This function then can fetch an entry from the contact table.
-                               Contact::getIdForURL($attributes['profile'], 0, true);
+                               $default['url'] = $attributes['profile'];
+
+                               if (!empty($attributes['author'])) {
+                                       $default['name'] = $attributes['author'];
+                               }
+
+                               if (!empty($attributes['avatar'])) {
+                                       $default['photo'] = $attributes['avatar'];
+                               }
+
+                               Contact::getIdForURL($attributes['profile'], 0, true, $default);
 
                                $author_contact = Contact::getDetailsByURL($attributes['profile']);
                                $author_contact['addr'] = defaults($author_contact, 'addr' , Protocol::getAddrFromProfileUrl($attributes['profile']));
@@ -1092,7 +1127,11 @@ class BBCode extends BaseObject
                        if (substr($curl_info["content_type"], 0, 6) == "image/") {
                                $text = "[img]" . $match[1] . "[/img]";
                        } else {
-                               $text = "[img]" . $match[2] . "[/img]";
+                               if (!empty($match[3])) {
+                                       $text = "[img=" . $match[2] . "]" . $match[3] . "[/img]";
+                               } else {
+                                       $text = "[img]" . $match[2] . "[/img]";
+                               }
 
                                // if its not a picture then look if its a page that contains a picture link
                                $body = Network::fetchUrl($match[1]);
@@ -1110,7 +1149,11 @@ class BBCode extends BaseObject
                                        }
 
                                        if (strtolower($attr["name"]) == "twitter:image") {
-                                               $text = "[img]" . $attr["content"] . "[/img]";
+                                               if (!empty($match[3])) {
+                                                       $text = "[img=" . $attr["content"] . "]" . $match[3] . "[/img]";
+                                               } else {
+                                                       $text = "[img]" . $attr["content"] . "[/img]";
+                                               }
                                        }
                                }
                        }
@@ -1122,7 +1165,8 @@ class BBCode extends BaseObject
 
        public static function cleanPictureLinks($text)
        {
-               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::cleanPictureLinksCallback', $text);
+               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img=(.*)\](.*)\[\/img\]\[\/url\]&Usi", 'self::cleanPictureLinksCallback', $text);
+               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::cleanPictureLinksCallback', $return);
                return $return;
        }