From bdc07b42139ed1f4c5f7c0f7afb29b7abdbb0bcd Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Mon, 5 Aug 2019 16:27:45 +0000
Subject: [PATCH 1/2] Added support for image descriptions and multiple image
 posts to external services

---
 src/Content/Text/BBCode.php | 22 +++++++++++++++++++++-
 src/Model/Photo.php         | 18 ++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php
index b012e79fb2..668d6e6236 100644
--- a/src/Content/Text/BBCode.php
+++ b/src/Content/Text/BBCode.php
@@ -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,11 +239,30 @@ 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'][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);
 
 			if (preg_match_all("(\[url=(.*?)\]\s*\[img\](.*?)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
diff --git a/src/Model/Photo.php b/src/Model/Photo.php
index c983fdc058..e722444b18 100644
--- a/src/Model/Photo.php
+++ b/src/Model/Photo.php
@@ -709,4 +709,22 @@ class Photo extends BaseObject
 		$guid = substr($guid, 0, -2);
 		return $guid;
 	}
+
+	/**
+	 * Tests if the picture link points to a locally stored picture
+	 *
+	 * @param string $name Picture link
+	 * @return boolean
+	 * @throws \Exception
+	 */
+	public static function isLocal($name)
+	{
+		$guid = self::getGUID($name);
+
+		if (empty($guid)) {
+			return false;
+		}
+
+		return DBA::exists('photo', ['resource-id' => $guid]);
+	}
 }

From cbee91a61b7b066b7c0706ecd51c2893d626ea19 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Mon, 5 Aug 2019 16:48:58 +0000
Subject: [PATCH 2/2] Avoid a notice

---
 src/Content/Text/BBCode.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php
index 668d6e6236..a5cb78727c 100644
--- a/src/Content/Text/BBCode.php
+++ b/src/Content/Text/BBCode.php
@@ -246,7 +246,7 @@ class BBCode extends BaseObject
 					$post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2]];
 				}
 			}
-			if (!empty($post['images'][0]['description'])) {
+			if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
 				$post['image_description'] = $post['images'][0]['description'];
 			}
 		}