+function appnet_create_entities($a, $b, $postdata) {
+ require_once("include/bbcode.php");
+ require_once("include/plaintext.php");
+
+ $bbcode = $b["body"];
+ $bbcode = bb_remove_share_information($bbcode, false, true);
+
+ // Change pure links in text to bbcode uris
+ $bbcode = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2]$2[/url]', $bbcode);
+
+ $URLSearchString = "^\[\]";
+
+ $bbcode = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'#$2',$bbcode);
+ $bbcode = preg_replace("/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'@$2',$bbcode);
+ $bbcode = preg_replace("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$bbcode);
+ $bbcode = preg_replace("/\[video\](.*?)\[\/video\]/ism",'[url=$1]$1[/url]',$bbcode);
+ $bbcode = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
+ '[url=https://www.youtube.com/watch?v=$1]https://www.youtube.com/watch?v=$1[/url]', $bbcode);
+ $bbcode = preg_replace("/\[youtube\](.*?)\[\/youtube\]/ism",'[url=$1]$1[/url]',$bbcode);
+ $bbcode = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
+ '[url=https://vimeo.com/$1]https://vimeo.com/$1[/url]', $bbcode);
+ $bbcode = preg_replace("/\[vimeo\](.*?)\[\/vimeo\]/ism",'[url=$1]$1[/url]',$bbcode);
+
+ $bbcode = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $bbcode);
+
+
+ preg_match_all("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $bbcode, $urls, PREG_SET_ORDER);
+
+ $bbcode = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'$1',$bbcode);
+
+ $b["body"] = $bbcode;
+
+ // To-Do:
+ // Bilder
+ // https://alpha.app.net/heluecht/post/32424376
+ // https://alpha.app.net/heluecht/post/32424307
+
+ $plaintext = plaintext($a, $b, 0, false, 6);
+
+ $text = $plaintext["text"];
+
+ $start = 0;
+ $entities = array();
+
+ foreach ($urls AS $url) {
+ $lenurl = iconv_strlen($url[1], "UTF-8");
+ $len = iconv_strlen($url[2], "UTF-8");
+ $pos = iconv_strpos($text, $url[1], $start, "UTF-8");
+ $pre = iconv_substr($text, 0, $pos, "UTF-8");
+ $post = iconv_substr($text, $pos + $lenurl, 1000000, "UTF-8");
+
+ $mid = $url[2];
+ $html = bbcode($mid, false, false, 6);
+ $mid = html2plain($html, 0, true);
+ $mid = trim(html_entity_decode($mid,ENT_QUOTES,'UTF-8'));
+
+ $text = $pre.$mid.$post;
+
+ if ($mid != "")
+ $entities[] = array("pos" => $pos, "len" => $len, "url" => $url[1], "text" => $mid);
+
+ $start = $pos + 1;
+ }
+
+ if (isset($postdata["url"]) AND isset($postdata["title"])) {
+ $postdata["title"] = shortenmsg($postdata["title"], 90);
+ $text = shortenmsg($text, 256 - strlen($postdata["title"]));
+ $text .= "\n[".$postdata["title"]."](".$postdata["url"].")";
+ } elseif (isset($postdata["url"])) {
+ $postdata["url"] = short_link($postdata["url"]);
+ $text = shortenmsg($text, 240);
+ $text .= " [".$postdata["url"]."](".$postdata["url"].")";
+ } else
+ $text = shortenmsg($text, 256);
+
+ krsort($entities);
+ foreach ($entities AS $entity) {
+ if (iconv_strlen($text) >= $entity["pos"] + $entity["len"]) {
+ $pre = iconv_substr($text, 0, $entity["pos"], "UTF-8");
+ $post = iconv_substr($text, $entity["pos"] + $entity["len"], 1000000, "UTF-8");
+
+ $text = $pre."[".$entity["text"]."](".$entity["url"].")".$post;
+ }
+ }
+
+ return($text);
+}
+