]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
Merge pull request #115 from tobiasd/snt_newstyle_share
[friendica-addons.git] / twitter / twitter.php
index aeacb5b297d54486267fe97a712f409e46d1643f..c07d3b58cb3986169b39c4b606f71292e3df2465 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,6 +389,10 @@ 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 the link exists in the original message)
+       if ((strlen(trim($origmsg)) <= $max_char) AND (strpos($origmsg, $msglink) OR ($msglink == "")))
+               return(trim($origmsg));
+
        if (strlen($msglink) > 20)
                $msglink = short_link($msglink);
 
@@ -505,7 +512,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 +550,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 +563,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'))
        ));
 }
 
@@ -604,16 +614,26 @@ function twitter_fetchtimeline($a, $uid) {
 
        $parameters = array("exclude_replies" => true, "trim_user" => true, "contributor_details" => false, "include_rts" => false);
 
+       $first_time = ($lastid == "");
+
        if ($lastid <> "")
                $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;
 
+               if ($first_time)
+                       continue;
+
                if (!strpos($post->source, $application_name)) {
                        $_SESSION["authenticated"] = true;
                        $_SESSION["uid"] = $uid;
@@ -644,7 +664,8 @@ function twitter_fetchtimeline($a, $uid) {
                        require_once('mod/item.php');
                        item_post($a);
 
-               }
+                }
+            }
        }
        set_pconfig($uid, 'twitter', 'lastid', $lastid);
 }