-function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
- $Text = preg_replace_callback("/(.*?)\[attachment(.*?)\](.*?)\[\/attachment\]/ism",
- function ($match) use ($plaintext){
-
- $attributes = $match[2];
-
- $type = "";
- preg_match("/type='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $type = strtolower($matches[1]);
-
- preg_match('/type="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $type = strtolower($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];
-
- //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
- $title = bbcode(html_entity_decode($title), false, false, true);
- $title = str_replace(array("[", "]"), array("[", "]"), $title);
-
- $image = "";
- if ($type != "video") {
- preg_match("/image='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $image = $matches[1];
-
- preg_match('/image="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $image = $matches[1];
- }
-
- $preview = "";
- if ($type != "video") {
- preg_match("/preview='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $preview = $matches[1];
-
- preg_match('/preview="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $preview = $matches[1];
- }
-
- if (((strpos($match[1], "[img=") !== false) OR (strpos($match[1], "[img]") !== false)) AND ($image != "")) {
- $preview = $image;
- $image = "";
- }
-
- if ($plaintext)
- $text = sprintf('<a href="%s" target="_blank">%s</a><br>', $url, $title);
- else {
- $text = sprintf('<span class="type-%s">', $type);
-
- $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $url, $title), $url, $title);
- if ($tryoembed)
- $oembed = tryoembed($bookmark);
- else
- $oembed = $bookmark[0];
-
- if (($image != "") AND !strstr(strtolower($oembed), "<img "))
- $text .= sprintf('<a href="%s" target="_blank"><img src="%s" alt="" title="%s" class="attachment-image" /></a><br />', $url, $image, $title);
- elseif (($preview != "") AND !strstr(strtolower($oembed), "<img "))
- $text .= sprintf('<a href="%s" target="_blank"><img src="%s" alt="" title="%s" class="attachment-preview" /></a><br />', $url, $preview, $title);
-
- $text .= $oembed;
-
- $text .= sprintf('<blockquote>%s</blockquote></span>', trim($match[3]));
- }