]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
pumpio: Added README
[friendica-addons.git] / twitter / twitter.php
index 88cf38588e3532a707180639e83b31095cadd870..223f69bd73a27c5dcc62eee1112337f1078ac68d 100755 (executable)
@@ -4,6 +4,7 @@
  * Description: Relay public postings to a connected Twitter account
  * Version: 1.0.4
  * Author: Tobias Diekershoff <http://diekershoff.homeunix.net/friendika/profile/tobias>
+ * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  */
 
 
@@ -351,6 +352,8 @@ function twitter_shortenmsg($b) {
        while (strpos($msg, "  ") !== false)
                $msg = str_replace("  ", " ", $msg);
 
+       $origmsg = $msg;
+
        // Removing URLs
        $msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
 
@@ -386,11 +389,20 @@ function twitter_shortenmsg($b) {
        if (($msglink == "") and strlen($msg) > $max_char)
                $msglink = $b["plink"];
 
+       // If the message is short enough then don't modify it.
+       if ((strlen(trim($origmsg)) <= $max_char) AND ($msglink == ""))
+               return(trim($origmsg));
+
+       // 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))
+               return(trim($origmsg));
+
        if (strlen($msglink) > 20)
                $msglink = short_link($msglink);
 
-       if (strlen(trim($msg." ".$msglink)) > $max_char) {
-               $msg = substr($msg, 0, $max_char - (strlen($msglink)));
+       if (strlen(trim($msg." ".$msglink)) > ($max_char - 3)) {
+               $msg = substr($msg, 0, ($max_char - 3) - (strlen($msglink)));
                $lastchar = substr($msg, -1);
                $msg = substr($msg, 0, -1);
                $pos = strrpos($msg, "\n");
@@ -505,7 +517,7 @@ function twitter_post_hook(&$a,&$b) {
                        // ok, all the links we want to send out are save, now strip 
                        // away the remaining bbcode
                        //$msg = strip_tags(bbcode($tmp, false, false));
-                       $msg = bbcode($tmp, false, false);
+                       $msg = bbcode($tmp, false, false, true);
                        $msg = str_replace(array('<br>','<br />'),"\n",$msg);
                        $msg = strip_tags($msg);
 
@@ -543,8 +555,10 @@ function twitter_post_hook(&$a,&$b) {
 function twitter_plugin_admin_post(&$a){
        $consumerkey    =       ((x($_POST,'consumerkey'))              ? notags(trim($_POST['consumerkey']))   : '');
        $consumersecret =       ((x($_POST,'consumersecret'))   ? notags(trim($_POST['consumersecret'])): '');
+        $applicationname = ((x($_POST, 'applicationname')) ? notags(trim($_POST['applicationname'])):'');
        set_config('twitter','consumerkey',$consumerkey);
        set_config('twitter','consumersecret',$consumersecret);
+       set_config('twitter','application_name',$applicationname);
        info( t('Settings updated.'). EOL );
 }
 function twitter_plugin_admin(&$a, &$o){
@@ -554,7 +568,8 @@ function twitter_plugin_admin(&$a, &$o){
                '$submit' => t('Submit'),
                                                                // name, label, value, help, [extra values]
                '$consumerkey' => array('consumerkey', t('Consumer key'),  get_config('twitter', 'consumerkey' ), ''),
-               '$consumersecret' => array('consumersecret', t('Consumer secret'),  get_config('twitter', 'consumersecret' ), '')
+                '$consumersecret' => array('consumersecret', t('Consumer secret'),  get_config('twitter', 'consumersecret' ), ''),
+                '$applicationname' => array('applicationname', t('Name of the Twitter Application'), get_config('twitter','application_name'),t('set this to avoid mirroring postings from ~friendica back to ~friendica'))
        ));
 }
 
@@ -610,9 +625,14 @@ function twitter_fetchtimeline($a, $uid) {
                $parameters["since_id"] = $lastid;
 
        $items = $connection->get('statuses/user_timeline', $parameters);
+
+       if (!is_array($items))
+               return;
+
        $posts = array_reverse($items);
 
-       foreach ($posts as $post) {
+        if (count($posts)) {
+           foreach ($posts as $post) {
                if ($post->id_str > $lastid)
                        $lastid = $post->id_str;
 
@@ -649,7 +669,8 @@ function twitter_fetchtimeline($a, $uid) {
                        require_once('mod/item.php');
                        item_post($a);
 
-               }
+                }
+            }
        }
        set_pconfig($uid, 'twitter', 'lastid', $lastid);
 }