+ function api_convert_item($item) {
+
+ $body = $item['body'];
+ $attachments = api_get_attachments($body);
+
+ // Workaround for ostatus messages where the title is identically to the body
+ $html = bbcode(api_clean_plain_items($body), false, false, 2, true);
+ $statusbody = trim(html2plain($html, 0));
+
+ // handle data: images
+ $statusbody = api_format_items_embeded_images($item,$statusbody);
+
+ $statustitle = trim($item['title']);
+
+ if (($statustitle != '') and (strpos($statusbody, $statustitle) !== false))
+ $statustext = trim($statusbody);
+ else
+ $statustext = trim($statustitle."\n\n".$statusbody);
+
+ if (($item["network"] == NETWORK_FEED) and (strlen($statustext)> 1000))
+ $statustext = substr($statustext, 0, 1000)."... \n".$item["plink"];
+
+ $statushtml = trim(bbcode($body, false, false));
+
+ if ($item['title'] != "")
+ $statushtml = "<h4>".bbcode($item['title'])."</h4>\n".$statushtml;
+
+ $entities = api_get_entitities($statustext, $body);
+
+ return(array("text" => $statustext, "html" => $statushtml, "attachments" => $attachments, "entities" => $entities));
+ }
+
+ function api_get_attachments(&$body) {
+
+ $text = $body;
+ $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
+
+ $URLSearchString = "^\[\]";
+ $ret = preg_match_all("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $text, $images);
+
+ if (!$ret)
+ return false;
+
+ $attachments = array();
+
+ foreach ($images[1] AS $image) {
+ $imagedata = get_photo_info($image);
+
+ if ($imagedata)
+ $attachments[] = array("url" => $image, "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]);
+ }
+
+ if (strstr($_SERVER['HTTP_USER_AGENT'], "AndStatus"))
+ foreach ($images[0] AS $orig)
+ $body = str_replace($orig, "", $body);
+
+ return $attachments;
+ }
+
+ function api_get_entitities(&$text, $bbcode) {
+ /*
+ To-Do:
+ * Links at the first character of the post
+ */
+
+ $a = get_app();
+
+ $include_entities = strtolower(x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:"false");
+
+ if ($include_entities != "true") {
+ require_once("mod/proxy.php");
+
+ preg_match_all("/\[img](.*?)\[\/img\]/ism", $bbcode, $images);
+
+ foreach ($images[1] AS $image) {
+ $replace = proxy_url($image);
+ $text = str_replace($image, $replace, $text);
+ }
+ return array();
+ }
+
+ $bbcode = bb_CleanPictureLinks($bbcode);
+
+ // Change pure links in text to bbcode uris
+ $bbcode = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2]$2[/url]', $bbcode);
+
+ $entities = array();
+ $entities["hashtags"] = array();
+ $entities["symbols"] = array();
+ $entities["urls"] = array();
+ $entities["user_mentions"] = array();
+
+ $URLSearchString = "^\[\]";
+
+ $bbcode = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'#$2',$bbcode);
+
+ $bbcode = preg_replace("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$bbcode);
+ //$bbcode = preg_replace("/\[url\](.*?)\[\/url\]/ism",'[url=$1]$1[/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, $urls1);
+ preg_match_all("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $bbcode, $urls);
+
+ $ordered_urls = array();
+ foreach ($urls[1] AS $id=>$url) {
+ //$start = strpos($text, $url, $offset);
+ $start = iconv_strpos($text, $url, 0, "UTF-8");
+ if (!($start === false))
+ $ordered_urls[$start] = array("url" => $url, "title" => $urls[2][$id]);
+ }
+
+ ksort($ordered_urls);
+
+ $offset = 0;
+ //foreach ($urls[1] AS $id=>$url) {
+ foreach ($ordered_urls AS $url) {
+ if ((substr($url["title"], 0, 7) != "http://") AND (substr($url["title"], 0, 8) != "https://") AND
+ !strpos($url["title"], "http://") AND !strpos($url["title"], "https://"))
+ $display_url = $url["title"];
+ else {
+ $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $url["url"]);
+ $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
+
+ if (strlen($display_url) > 26)
+ $display_url = substr($display_url, 0, 25)."…";
+ }
+
+ //$start = strpos($text, $url, $offset);
+ $start = iconv_strpos($text, $url["url"], $offset, "UTF-8");
+ if (!($start === false)) {
+ $entities["urls"][] = array("url" => $url["url"],
+ "expanded_url" => $url["url"],
+ "display_url" => $display_url,
+ "indices" => array($start, $start+strlen($url["url"])));
+ $offset = $start + 1;
+ }
+ }
+
+ preg_match_all("/\[img](.*?)\[\/img\]/ism", $bbcode, $images);
+ $ordered_images = array();
+ foreach ($images[1] AS $image) {
+ //$start = strpos($text, $url, $offset);
+ $start = iconv_strpos($text, $image, 0, "UTF-8");
+ if (!($start === false))
+ $ordered_images[$start] = $image;
+ }
+ //$entities["media"] = array();
+ $offset = 0;
+
+ foreach ($ordered_images AS $url) {
+ $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $url);
+ $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
+
+ if (strlen($display_url) > 26)
+ $display_url = substr($display_url, 0, 25)."…";
+
+ $start = iconv_strpos($text, $url, $offset, "UTF-8");
+ if (!($start === false)) {
+ $image = get_photo_info($url);
+ if ($image) {
+ // If image cache is activated, then use the following sizes:
+ // thumb (150), small (340), medium (600) and large (1024)
+ if (!get_config("system", "proxy_disabled")) {
+ require_once("mod/proxy.php");
+ $media_url = proxy_url($url);
+
+ $sizes = array();
+ $scale = scale_image($image[0], $image[1], 150);
+ $sizes["thumb"] = array("w" => $scale["width"], "h" => $scale["height"], "resize" => "fit");
+
+ if (($image[0] > 150) OR ($image[1] > 150)) {
+ $scale = scale_image($image[0], $image[1], 340);
+ $sizes["small"] = array("w" => $scale["width"], "h" => $scale["height"], "resize" => "fit");
+ }
+
+ $scale = scale_image($image[0], $image[1], 600);
+ $sizes["medium"] = array("w" => $scale["width"], "h" => $scale["height"], "resize" => "fit");
+
+ if (($image[0] > 600) OR ($image[1] > 600)) {
+ $scale = scale_image($image[0], $image[1], 1024);
+ $sizes["large"] = array("w" => $scale["width"], "h" => $scale["height"], "resize" => "fit");
+ }
+ } else {
+ $media_url = $url;
+ $sizes["medium"] = array("w" => $image[0], "h" => $image[1], "resize" => "fit");
+ }
+
+ $entities["media"][] = array(
+ "id" => $start+1,
+ "id_str" => (string)$start+1,
+ "indices" => array($start, $start+strlen($url)),
+ "media_url" => normalise_link($media_url),
+ "media_url_https" => $media_url,
+ "url" => $url,
+ "display_url" => $display_url,
+ "expanded_url" => $url,
+ "type" => "photo",
+ "sizes" => $sizes);
+ }
+ $offset = $start + 1;
+ }
+ }
+
+ return($entities);
+ }
+ function api_format_items_embeded_images($item, $text){
+ $a = get_app();
+ $text = preg_replace_callback(
+ "|data:image/([^;]+)[^=]+=*|m",
+ function($match) use ($a, $item) {
+ return $a->get_baseurl()."/display/".$item['guid'];
+ },
+ $text);
+ return $text;
+ }
+