]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
pumpio: Added README
[friendica-addons.git] / twitter / twitter.php
index ad6577992f46dcaf04ff8483b23d16a6059e7892..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>
  */
 
 
@@ -36,7 +37,7 @@
  *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/Twitter_Plugin
  */
 
-define('TWITTER_DEFAULT_POLL_INTERVAL', 30); // given in minutes
+define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
 
 function twitter_install() {
        //  we need some hooks, for the configuration and for sending tweets
@@ -74,7 +75,7 @@ function twitter_jot_nets(&$a,&$b) {
                $tw_defpost = get_pconfig(local_user(),'twitter','post_by_default');
                $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
                $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> ' 
-                       . t('Post to Twitter') . '</div>';      
+                       . t('Post to Twitter') . '</div>';
        }
 }
 
@@ -306,6 +307,24 @@ function twitter_shortenmsg($b) {
        if ($b["title"] != "")
                $body = $b["title"]."\n\n".$body;
 
+       if (strpos($body, "[bookmark") !== false) {
+               // splitting the text in two parts:
+               // before and after the bookmark
+               $pos = strpos($body, "[bookmark");
+               $body1 = substr($body, 0, $pos);
+               $body2 = substr($body, $pos);
+
+               // Removing all quotes after the bookmark
+               // they are mostly only the content after the bookmark.
+               $body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
+               $body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
+               $body = $body1.$body2;
+       }
+
+       // Add some newlines so that the message could be cut better
+       $body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
+                       array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
+
        // remove the recycle signs and the names since they aren't helpful on twitter
        // recycle 1
        $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
@@ -333,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);
 
@@ -368,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");
@@ -405,8 +435,8 @@ function twitter_post_hook(&$a,&$b) {
        if($b['parent'] != $b['id'])
                return;
 
-       // if posts comes from twitter don't send it back
-       if($b['app'] == "twitter.com")
+       // if post comes from twitter don't send it back
+       if($b['app'] == "Twitter")
                return;
 
        logger('twitter post invoked');
@@ -487,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);
 
@@ -525,23 +555,21 @@ 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){
        $t = get_markup_template( "admin.tpl", "addon/twitter/" );
 
-       $includes = array(
-               '$field_input' => 'field_input.tpl',
-       );
-       //$includes = set_template_includes($a->theme['template_engine'], $includes);
-
-       $o = replace_macros($t, $includes + array(
+       $o = replace_macros($t, array(
                '$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'))
        ));
 }
 
@@ -581,29 +609,44 @@ function twitter_fetchtimeline($a, $uid) {
        $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
        $lastid  = get_pconfig($uid, 'twitter', 'lastid');
 
+       $application_name  = get_config('twitter', 'application_name');
+
+       if ($application_name == "")
+               $application_name = $a->get_hostname();
+
        require_once('library/twitteroauth.php');
        $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
 
        $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 (!strpos($post->source, $a->get_hostname())) {
+               if ($first_time)
+                       continue;
+
+               if (!strpos($post->source, $application_name)) {
                        $_SESSION["authenticated"] = true;
                        $_SESSION["uid"] = $uid;
 
                        $_REQUEST["type"] = "wall";
                        $_REQUEST["api_source"] = true;
                        $_REQUEST["profile_uid"] = $uid;
-                       $_REQUEST["source"] = "twitter.com";
+                       $_REQUEST["source"] = "Twitter";
 
                        //$_REQUEST["date"] = $post->created_at;
 
@@ -626,7 +669,8 @@ function twitter_fetchtimeline($a, $uid) {
                        require_once('mod/item.php');
                        item_post($a);
 
-               }
+                }
+            }
        }
        set_pconfig($uid, 'twitter', 'lastid', $lastid);
 }