* 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>
*/
* 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
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("♲ ", ENT_QUOTES, 'UTF-8');
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
+ $origmsg = $msg;
+
// Removing URLs
$msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
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);
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');
// 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);
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'))
));
}
$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;
require_once('mod/item.php');
item_post($a);
- }
+ }
+ }
}
set_pconfig($uid, 'twitter', 'lastid', $lastid);
}