]> 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 2be686b30fed2b7956d83c21a5b51e470a824ce1..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>
  */
 
 
@@ -306,6 +307,20 @@ 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);
@@ -337,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);
 
@@ -372,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);
 
@@ -491,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);
 
@@ -529,23 +550,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'))
        ));
 }
 
@@ -595,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;
@@ -635,7 +664,8 @@ function twitter_fetchtimeline($a, $uid) {
                        require_once('mod/item.php');
                        item_post($a);
 
-               }
+                }
+            }
        }
        set_pconfig($uid, 'twitter', 'lastid', $lastid);
 }