';
+
+ if ($avatar != "")
+ $headline .= '';
+
+ $headline .= sprintf(t('%s wrote the following post'.$reldate.':'), $profile, $author, $link);
+
+ $headline .= "
";
+
+ $text = $headline.'
'.trim($match[2])."
";
+
+ return($text);
+}
+
+function bb_ShareAttributesSimple($match) {
+
+ $attributes = $match[1];
+
+ $author = "";
+ preg_match("/author='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
+
+ preg_match('/author="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $author = $matches[1];
+
+ $profile = "";
+ preg_match("/profile='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $profile = $matches[1];
+
+ preg_match('/profile="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $profile = $matches[1];
+
+ $text = " ".html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$author.": »".$match[2]."«";
+
+ return($text);
+}
+
// BBcode 2 HTML was written by WAY2WEB.net
// extended to work with Mistpark/Friendica - Mike Macgirvin
-function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
+function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = false) {
+
+ $stamp1 = microtime(true);
$a = get_app();
- // Hide all [noparse] contained bbtags spacefying them
+ // Hide all [noparse] contained bbtags by spacefying them
+ // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
$Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
$Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
$Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
- // Extract a single private image which uses data url's since preg has issues with
- // large data sizes. Stash it away while we do bbcode conversion, and then put it back
+ // Move all spaces out of the tags
+ $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
+ $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
+
+ // Extract the private images which use data url's since preg has issues with
+ // large data sizes. Stash them away while we do bbcode conversion, and then put them back
// in after we've done all the regex matching. We cannot use any preg functions to do this.
- $saved_image = '';
- $img_start = strpos($Text,'[img]data:');
- $img_end = strpos($Text,'[/img]');
-
- if($img_start !== false && $img_end !== false && $img_end > $img_start) {
- $start_fragment = substr($Text,0,$img_start);
- $img_start += strlen('[img]');
- $saved_image = substr($Text,$img_start,$img_end - $img_start);
- $end_fragment = substr($Text,$img_end + strlen('[/img]'));
-// logger('saved_image: ' . $saved_image,LOGGER_DEBUG);
- $Text = $start_fragment . '[$#saved_image#$]' . $end_fragment;
- }
+ $extracted = bb_extract_images($Text);
+ $Text = $extracted['body'];
+ $saved_image = $extracted['images'];
// If we find any event code, turn it into an event.
// After we're finished processing the bbcode we'll
@@ -91,6 +347,17 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$Text = str_replace("<", "<", $Text);
$Text = str_replace(">", ">", $Text);
+ // remove some newlines before the general conversion
+ $Text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","[share$1]$2[/share]",$Text);
+ $Text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism","[quote$1]$2[/quote]",$Text);
+
+ $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
+ $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $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);
+
// Convert new line chars to html tags
// nlbr seems to be hopelessly messed up
@@ -98,7 +365,19 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// We'll emulate it.
+ $Text = trim($Text);
$Text = str_replace("\r\n","\n", $Text);
+
+ // removing multiplicated newlines
+ if (get_config("system", "remove_multiplicated_lines")) {
+ $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]");
+ $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]");
+ do {
+ $oldtext = $Text;
+ $Text = str_replace($search, $replace, $Text);
+ } while ($oldtext != $Text);
+ }
+
$Text = str_replace(array("\r","\n"), array(' ',' '), $Text);
if($preserve_nl)
@@ -114,7 +393,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// Perform URL Search
- $Text = preg_replace("/([^\]\=]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text);
+ $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text);
if ($tryoembed)
$Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
@@ -128,6 +407,10 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $Text);
//$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text);
+ // Red compatibility, though the link can't be authenticated on Friendica
+ $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '$2', $Text);
+
+
// we may need to restrict this further if it picks up too many strays
// link acct:user@host to a webfinger profile redirector
@@ -167,6 +450,12 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// Check for list text
$Text = str_replace("[*]", "
", $Text);
+
// If we found an event earlier, strip out all the event code and replace with a reformatted version.
// Replace the event-start section with the entire formatted event. The other bbcode is stripped.
// Summary (e.g. title) is required, earlier revisions only required description (in addition to
@@ -330,14 +644,45 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$Text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/','&$1;',$Text);
+ $Text = preg_replace('/\&\#039\;/','\'',$Text);
+ $Text = preg_replace('/\"\;/','"',$Text);
// fix any escaped ampersands that may have been converted into links
- $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
- if(strlen($saved_image))
- $Text = str_replace('[$#saved_image#$]','',$Text);
+ $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
+ $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|cid)(.*?)\>/ism",'<$1$2="">',$Text);
+
+ if($saved_image)
+ $Text = bb_replace_images($Text, $saved_image);
+
+ // Clean up the HTML by loading and saving the HTML with the DOM
+ // Only do it when it has to be done - for performance reasons
+ // Update: Now it is done every time - since bad structured html can break a whole page
+ //if (!$tryoembed) {
+// $doc = new DOMDocument();
+// $doc->preserveWhiteSpace = false;
+
+// $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
+
+// $doctype = '';
+// @$doc->loadHTML($doctype."".$Text."");
+
+// $Text = $doc->saveHTML();
+// $Text = str_replace(array("", "", $doctype), array("", "", ""), $Text);
+
+// $Text = str_replace('