From 14ea4b972ecddbf66f9661a62983e007a452b700 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sun, 2 Nov 2014 17:35:49 +0100
Subject: [PATCH 1/2] BBCode: Newlines under attachments are now removed

---
 include/bbcode.php | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/include/bbcode.php b/include/bbcode.php
index 8efeabb8bf..ee607cf6a9 100644
--- a/include/bbcode.php
+++ b/include/bbcode.php
@@ -825,9 +825,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
 	// Rearrange shares to attachments
 	$Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism", "bb_rearrange_share",$Text);
 
-	// Handle attached links or videos
-	$Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
-
 	// Rearrange shared links
 //	if (get_config("system", "rearrange_shared_links") AND (!$simplehtml OR $tryoembed))
 //		$Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_rearrange_link",$Text);
@@ -848,8 +845,8 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
 
 	// removing multiplicated newlines
 	if (get_config("system", "remove_multiplicated_lines")) {
-		$search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ");
-		$replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ");
+		$search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n");
+		$replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ", "[/attachment]");
 		do {
 			$oldtext = $Text;
 			$Text = str_replace($search, $replace, $Text);
@@ -861,7 +858,8 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
 	if($preserve_nl)
 		$Text = str_replace(array("\n","\r"), array('',''),$Text);
 
-
+	// Handle attached links or videos
+	$Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
 
 	// Set up the parameters for a URL search string
 	$URLSearchString = "^\[\]";

From 3eed5bbd3e6891738332649f73602329d7973bb0 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sun, 2 Nov 2014 18:39:23 +0100
Subject: [PATCH 2/2] Further improvement for newlines after attachments

---
 include/bbcode.php | 44 ++++++--------------------------------------
 1 file changed, 6 insertions(+), 38 deletions(-)

diff --git a/include/bbcode.php b/include/bbcode.php
index ee607cf6a9..5395548f51 100644
--- a/include/bbcode.php
+++ b/include/bbcode.php
@@ -66,7 +66,7 @@ function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
 			}
 
 			if ($plaintext)
-				$text = sprintf('<a href="%s" target="_blank">%s</a>', $url, $title);
+				$text = sprintf('<a href="%s" target="_blank">%s</a><br>', $url, $title);
 			else {
 				$text = sprintf('<span class="type-%s">', $type);
 
@@ -92,35 +92,6 @@ function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
 	return($Text);
 }
 
-/* function bb_rearrange_link($shared) {
-	if ($shared[1] != "type-link")
-		return($shared[0]);
-
-	$newshare = trim($shared[2]);
-	$newshare = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $newshare);
-
-	if (!strpos($shared[0], "[bookmark"))
-		$newshare = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $newshare, 1);
-
-	preg_match("/\[img\](.*?)\[\/img\]/ism", $newshare, $matches);
-
-	if ($matches) {
-		$newshare = str_replace($matches[0], '', $newshare);
-		$newshare = "[img]".$matches[1]."[/img]\n".$newshare;
-	}
-
-	$search = array("\n\n", "\n ", " \n");
-	$replace = array("\n", "\n", "\n");
-	do {
-		$oldtext = $newshare;
-		$newshare = str_replace($search, $replace, $newshare);
-	} while ($oldtext != $newshare);
-
-	$newshare = "[class=type-link]".$newshare."[/class]";
-
-	return($newshare);
-} */
-
 function bb_rearrange_share($shared) {
 	if (!in_array(strtolower($shared[2]), array("type-link", "type-audio", "type-video")))
 		return($shared[0]);
@@ -211,7 +182,8 @@ function bb_cleanup_share($shared, $plaintext, $nolink) {
 	if (isset($bookmark[1][0]))
 		$link = $bookmark[1][0];
 
-	if (($title != "") AND (strpos($shared[1],$title) !== false))
+	if (($title != "") AND ((strpos($shared[1],$title) !== false) OR
+		(similar_text($shared[1],$title) / strlen($title)) > 0.9))
 		$title = "";
 
 //        if (strpos($shared[1],$link) !== false)
@@ -825,10 +797,6 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
 	// Rearrange shares to attachments
 	$Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism", "bb_rearrange_share",$Text);
 
-	// Rearrange shared links
-//	if (get_config("system", "rearrange_shared_links") AND (!$simplehtml OR $tryoembed))
-//		$Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_rearrange_link",$Text);
-
 	// when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
 	if (!$tryoembed)
 		$Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
@@ -853,14 +821,14 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
 		} while ($oldtext != $Text);
 	}
 
+	// Handle attached links or videos
+	$Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
+
 	$Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
 
 	if($preserve_nl)
 		$Text = str_replace(array("\n","\r"), array('',''),$Text);
 
-	// Handle attached links or videos
-	$Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
-
 	// Set up the parameters for a URL search string
 	$URLSearchString = "^\[\]";
 	// Set up the parameters for a MAIL search string