]> git.mxchange.org Git - friendica.git/commitdiff
bbcode: new element "attachment" for attached links.
authorMichael Vogel <icarus@dabo.de>
Mon, 2 Jun 2014 21:45:32 +0000 (23:45 +0200)
committerMichael Vogel <icarus@dabo.de>
Mon, 2 Jun 2014 21:45:32 +0000 (23:45 +0200)
include/bbcode.php

index 4ccab8db5d9ffb276a17874a1ff3bd9edd680296..d6fc81983f0e0eb8846da9dbfe8ba8fd115b2ac8 100644 (file)
@@ -2,6 +2,79 @@
 require_once("include/oembed.php");
 require_once('include/event.php');
 
+function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
+       $Text = preg_replace_callback("/\[attachment(.*?)\](.*?)\[\/attachment\]/ism",
+               function ($match) use ($plaintext){
+
+                       $attributes = $match[1];
+
+                       $type = "";
+                       preg_match("/type='(.*?)'/ism", $attributes, $matches);
+                       if ($matches[1] != "")
+                               $type = $matches[1];
+
+                       preg_match('/type="(.*?)"/ism', $attributes, $matches);
+                       if ($matches[1] != "")
+                               $type = $matches[1];
+
+                       if ($type == "")
+                               return($match[0]);
+
+                       if (!in_array($type, array("link", "audio", "video")))
+                               return($match[0]);
+
+                       $url = "";
+                       preg_match("/url='(.*?)'/ism", $attributes, $matches);
+                       if ($matches[1] != "")
+                               $url = $matches[1];
+
+                       preg_match('/url="(.*?)"/ism', $attributes, $matches);
+                       if ($matches[1] != "")
+                               $url = $matches[1];
+
+                       $title = "";
+                       preg_match("/title='(.*?)'/ism", $attributes, $matches);
+                       if ($matches[1] != "")
+                               $title = $matches[1];
+
+                       preg_match('/title="(.*?)"/ism', $attributes, $matches);
+                       if ($matches[1] != "")
+                               $title = $matches[1];
+
+                       $image = "";
+                       preg_match("/image='(.*?)'/ism", $attributes, $matches);
+                       if ($matches[1] != "")
+                               $image = $matches[1];
+
+                       preg_match('/image="(.*?)"/ism', $attributes, $matches);
+                       if ($matches[1] != "")
+                               $image = $matches[1];
+
+                       if ($plaintext)
+                               $text = sprintf('<a href="%s" target="_blank">%s</a>', $url, $title);
+                       else {
+                               $text = sprintf('<span class="type-%s">', $type);
+
+                               $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $url, $title), $title, $url);
+                               if ($tryoembed)
+                                       $oembed = tryoembed($bookmark);
+                               else
+                                       $oembed = $bookmark[0];
+
+                               if (($image != "") AND !strstr(strtolower($oembed), "<img "))
+                                       $text .= sprintf('<img src="%s" alt="%s" />', $image, $title); // To-Do: Anführungszeichen in "alt"
+
+                               $text .= $oembed;
+
+                               $text .= sprintf('<blockquote>%s</blockquote></span>', trim($match[2]));
+                       }
+
+                       return($text);
+               },$Text);
+
+        return($Text);
+}
+
 function bb_rearrange_link($shared) {
        if ($shared[1] != "type-link")
                return($shared[0]);
@@ -706,6 +779,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
        $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
        $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $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);