From 6790681b1ba0a2a61ec5e1da923604685fa05c61 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 24 Jul 2013 08:10:20 +0200 Subject: [PATCH] twitter: If a post contains an image and a link then both will be posted. --- twitter/twitter.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index 997f30a96..f84ab96da 100755 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -392,6 +392,21 @@ function twitter_shortenmsg($b) { // If there is no bookmark element then take the first link if ($link == '') { $links = collecturls($html); + + foreach($links AS $singlelink) { + $img_str = fetch_url($singlelink); + + $tempfile = tempnam(get_config("system","temppath"), "cache"); + file_put_contents($tempfile, $img_str); + $mime = image_type_to_mime_type(exif_imagetype($tempfile)); + unlink($tempfile); + + if (substr($mime, 0, 6) == "image/") { + $image = $singlelink; + unset($links[$singlelink]); + } + } + if (sizeof($links) > 0) { reset($links); $link = current($links); @@ -416,6 +431,10 @@ function twitter_shortenmsg($b) { if ((strlen(trim($origmsg)) <= $max_char) AND ($msglink == "")) return(array("msg"=>trim($origmsg), "image"=>"")); + // If the message is short enough and contains a picture then post the picture as well + if ((strlen(trim($origmsg)) <= ($max_char - 40)) AND strpos($origmsg, $msglink)) + return(array("msg"=>trim($origmsg), "image"=>$image)); + // If the message is short enough and the link exists in the original message don't modify it as well // -3 because of the bad shortener of twitter if ((strlen(trim($origmsg)) <= ($max_char - 3)) AND strpos($origmsg, $msglink)) @@ -444,6 +463,20 @@ function twitter_shortenmsg($b) { $msg = substr($msg, 0, $pos); else if ($lastchar != "\n") $msg = substr($msg, 0, -3)."..."; + + // if the post contains a picture and a link then the system tries to cut the post earlier. + // So the link and the picture can be posted. + if (($image != "") AND ($orig_link != $image)) { + $msg2 = substr($msg, 0, ($max_char - 40) - (strlen($msglink))); + $lastchar = substr($msg2, -1); + $msg2 = substr($msg2, 0, -1); + $pos = strrpos($msg2, "\n"); + if ($pos > 0) + $msg = substr($msg2, 0, $pos); + else if ($lastchar == "\n") + $msg = trim($msg2); + } + } //$msg = str_replace("\n", " ", $msg); @@ -451,6 +484,10 @@ function twitter_shortenmsg($b) { while (strpos($msg, " ") !== false) $msg = str_replace(" ", " ", $msg); + // Removing multiple newlines + //while (strpos($msg, "\n\n") !== false) + // $msg = str_replace("\n\n", "\n", $msg); + // Looking if the link points to an image $img_str = fetch_url($orig_link); @@ -461,6 +498,8 @@ function twitter_shortenmsg($b) { if (($image == $orig_link) OR (substr($mime, 0, 6) == "image/")) return(array("msg"=>trim($msg), "image"=>$orig_link)); + else if (($image != $orig_link) AND ($image != "") AND (strlen($msg."\n".$orig_link) <= 100)) + return(array("msg"=>trim($msg."\n".$orig_link), "image"=>$image)); else return(array("msg"=>trim($msg."\n".$orig_link), "image"=>"")); }