*/
-/* Twitter Plugin for Friendika
+/* Twitter Plugin for Friendica
*
* Author: Tobias Diekershoff
* tobias.diekershoff@gmx.net
* To use this plugin you need a OAuth Consumer key pair (key & secret)
* you can get it from Twitter at https://twitter.com/apps
*
- * Register your Friendika site as "Client" application with "Read & Write" access
+ * Register your Friendica site as "Client" application with "Read & Write" access
* we do not need "Twitter as login". When you've registered the app you get the
* OAuth Consumer key and secret pair for your application/site.
*
- * Add this key pair to your global .htconfig.php
+ * Add this key pair to your global .htconfig.php or use the admin panel.
*
* $a->config['twitter']['consumerkey'] = 'your consumer_key here';
* $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
// we need some hooks, for the configuration and for sending tweets
register_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
register_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
- register_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
+ register_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
+ register_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
logger("installed twitter");
}
function twitter_uninstall() {
unregister_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
unregister_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
+ unregister_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
+ unregister_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
+ unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
+
+ // old setting - remove only
+ unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings');
unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
- unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
- unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
+
}
function twitter_jot_nets(&$a,&$b) {
/***
* if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
* from the user configuration
- * TODO can we revoke the access tokens at Twitter and do we need to do so?
*/
del_pconfig( local_user(), 'twitter', 'consumerkey' );
del_pconfig( local_user(), 'twitter', 'consumersecret' );
+ del_pconfig( local_user(), 'twitter', 'oauthtoken' );
+ del_pconfig( local_user(), 'twitter', 'oauthsecret' );
del_pconfig( local_user(), 'twitter', 'post' );
del_pconfig( local_user(), 'twitter', 'post_by_default' );
} else {
/***
* make some nice form
*/
- $s .= '<p>'. t('At this Friendika instance the Twitter plugin was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to Twitter.') .'</p>';
+ $s .= '<p>'. t('At this Friendica instance the Twitter plugin was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to Twitter.') .'</p>';
$s .= '<a href="'.$connection->getAuthorizeURL($token).'" target="_twitter"><img src="addon/twitter/lighter.png" alt="'.t('Log in with Twitter').'"></a>';
$s .= '<div id="twitter-pin-wrapper">';
$s .= '<label id="twitter-pin-label" for="twitter-pin">'. t('Copy the PIN from Twitter here') .'</label>';
}
+function twitter_post_local(&$a,&$b) {
+
+ if($b['edit'])
+ return;
+
+ if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
+
+ $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
+ $twitter_enable = (($twitter_post && x($_REQUEST,'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
+
+ // if API is used, default to the chosen settings
+ if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'twitter','post_by_default')))
+ $twitter_enable = 1;
+
+ if(! $twitter_enable)
+ return;
+
+ if(strlen($b['postopts']))
+ $b['postopts'] .= ',';
+ $b['postopts'] .= 'twitter';
+ }
+}
+
+
function twitter_post_hook(&$a,&$b) {
/**
* Post to Twitter
*/
- logger('twitter post invoked');
+ if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
+ return;
- if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
+ if(! strstr($b['postopts'],'twitter'))
+ return;
- // Twitter is not considered a private network
- if($b['prvnets'])
- return;
+ if($b['parent'] != $b['id'])
+ return;
+ logger('twitter post invoked');
- load_pconfig(local_user(), 'twitter');
- $ckey = get_config('twitter', 'consumerkey' );
- $csecret = get_config('twitter', 'consumersecret' );
- $otoken = get_pconfig(local_user(), 'twitter', 'oauthtoken' );
- $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
-
- if($ckey && $csecret && $otoken && $osecret) {
-
- $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
- $twitter_enable = (($twitter_post && x($_POST,'twitter_enable')) ? intval($_POST['twitter_enable']) : 0);
-
- // if API is used, default to the chosen settings
- if($_POST['api_source'] && intval(get_pconfig(local_user(),'twitter','post_by_default')))
- $twitter_enable = 1;
-
- if($twitter_post && $twitter_enable) {
- logger('Posting to Twitter', LOGGER_DEBUG);
- require_once('library/twitteroauth.php');
- require_once('include/bbcode.php');
- $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
- $max_char = 140; // max. length for a tweet
- $msg = strip_tags(bbcode($b['body']));
- if ( strlen($msg) > $max_char) {
- $shortlink = "";
- require_once('library/slinky.php');
- // post url = base url + /display/ + owner + post id
- // we construct this from the Owner link and replace
- // profile by display - this will cause an error when
- // /profile/ is in the owner url twice but I don't
- // think this will be very common...
- $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id'];
- $slinky = new Slinky( $posturl );
- // setup a cascade of shortening services
- // try to get a short link from these services
- // in the order ur1.ca, trim, id.gd, tinyurl
- $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
- $shortlink = $slinky->short();
- // the new message will be shortened such that "... $shortlink"
- // will fit into the character limit
- $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
- $msg .= '... ' . $shortlink;
- }
- // and now tweet it :-)
- if(strlen($msg)) {
- $result = $tweet->post('statuses/update', array('status' => $msg));
- logger('twitter_post returns: ' . $result);
- }
-
- }
+ load_pconfig($b['uid'], 'twitter');
+
+ $ckey = get_config('twitter', 'consumerkey' );
+ $csecret = get_config('twitter', 'consumersecret' );
+ $otoken = get_pconfig($b['uid'], 'twitter', 'oauthtoken' );
+ $osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret' );
+
+ if($ckey && $csecret && $otoken && $osecret) {
+ logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
+
+ require_once('library/twitteroauth.php');
+ require_once('include/bbcode.php');
+ $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
+ $max_char = 138; // max. length for a tweet
+ $msg = strip_tags(bbcode($b['body']));
+ if ( strlen($msg) > $max_char) {
+ logger('Twitter: have to shorten the message to fit 140 chars', LOGGER_DEBUG);
+ $shortlink = "";
+ require_once('library/slinky.php');
+ $slinky = new Slinky( $b['plink'] );
+ // setup a cascade of shortening services
+ // try to get a short link from these services
+ // in the order ur1.ca, trim, id.gd, tinyurl
+ $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
+ $shortlink = $slinky->short();
+ // the new message will be shortened such that "... $shortlink"
+ // will fit into the character limit
+ $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
+ $msg .= '... ' . $shortlink;
+ }
+ // and now tweet it :-)
+ if(strlen($msg)) {
+ $result = $tweet->post('statuses/update', array('status' => $msg));
+ logger('twitter_post send', LOGGER_DEBUG);
}
}
}