Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

261 lines
12 KiB

  1. <?php
  2. /**
  3. * Name: Twitter Connector
  4. * Version: 1.0
  5. * Author: Tobias Diekershoff <https://diekershoff.homeunix.net/friendika/profile/tobias>
  6. */
  7. /* Twitter Plugin for Friendika
  8. *
  9. * Author: Tobias Diekershoff
  10. * tobias.diekershoff@gmx.net
  11. *
  12. * License:3-clause BSD license (same as Friendika)
  13. *
  14. * Configuration:
  15. * To use this plugin you need a OAuth Consumer key pair (key & secret)
  16. * you can get it from Twitter at https://twitter.com/apps
  17. *
  18. * Register your Friendika site as "Client" application with "Read & Write" access
  19. * we do not need "Twitter as login". When you've registered the app you get the
  20. * OAuth Consumer key and secret pair for your application/site.
  21. *
  22. * Add this key pair to your global .htconfig.php
  23. *
  24. * $a->config['twitter']['consumerkey'] = 'your consumer_key here';
  25. * $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
  26. *
  27. * To activate the plugin itself add it to the $a->config['system']['addon']
  28. * setting. After this, your user can configure their Twitter account settings
  29. * from "Settings -> Plugin Settings".
  30. *
  31. * Requirements: PHP5, curl [Slinky library]
  32. *
  33. * Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/Twitter_Plugin
  34. */
  35. /* __TODO__
  36. *
  37. * - what about multimedia content?
  38. * so far we just strip HTML tags from the message
  39. */
  40. function twitter_install() {
  41. // we need some hooks, for the configuration and for sending tweets
  42. register_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings');
  43. register_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
  44. register_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
  45. register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
  46. register_hook('plugin_admin', 'addon/twitter/twitter.php', 'plugin_admin');
  47. logger("installed twitter");
  48. }
  49. function twitter_uninstall() {
  50. unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings');
  51. unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
  52. unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
  53. unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
  54. unregister_hook('plugin_admin', 'addon/twitter/twitter.php', 'plugin_admin');
  55. }
  56. function twitter_jot_nets(&$a,&$b) {
  57. if(! local_user())
  58. return;
  59. $tw_post = get_pconfig(local_user(),'twitter','post');
  60. if(intval($tw_post) == 1) {
  61. $tw_defpost = get_pconfig(local_user(),'twitter','post_by_default');
  62. $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
  63. $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . 'value="1" /> '
  64. . t('Post to Twitter') . '</div>';
  65. }
  66. }
  67. function twitter_settings_post ($a,$post) {
  68. if(! local_user())
  69. return;
  70. if (isset($_POST['twitter-disconnect'])) {
  71. /***
  72. * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
  73. * from the user configuration
  74. * TODO can we revoke the access tokens at Twitter and do we need to do so?
  75. */
  76. del_pconfig( local_user(), 'twitter', 'consumerkey' );
  77. del_pconfig( local_user(), 'twitter', 'consumersecret' );
  78. del_pconfig( local_user(), 'twitter', 'post' );
  79. } else {
  80. if (isset($_POST['twitter-pin'])) {
  81. // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
  82. logger('got a Twitter PIN');
  83. require_once('library/twitteroauth.php');
  84. $ckey = get_config('twitter', 'consumerkey' );
  85. $csecret = get_config('twitter', 'consumersecret' );
  86. // the token and secret for which the PIN was generated were hidden in the settings
  87. // form as token and token2, we need a new connection to Twitter using these token
  88. // and secret to request a Access Token with the PIN
  89. $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
  90. $token = $connection->getAccessToken( $_POST['twitter-pin'] );
  91. // ok, now that we have the Access Token, save them in the user config
  92. set_pconfig(local_user(),'twitter', 'oauthtoken', $token['oauth_token']);
  93. set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
  94. set_pconfig(local_user(),'twitter', 'post', 1);
  95. // reload the Addon Settings page, if we don't do it see Bug #42
  96. header('Location: '.$a->get_baseurl().'/settings/addon');
  97. } else {
  98. // if no PIN is supplied in the POST variables, the user has changed the setting
  99. // to post a tweet for every new __public__ posting to the wall
  100. set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
  101. }}
  102. }
  103. function twitter_settings(&$a,&$s) {
  104. if(! local_user())
  105. return;
  106. $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
  107. /***
  108. * 1) Check that we have global consumer key & secret
  109. * 2) If no OAuthtoken & stuff is present, generate button to get some
  110. * 3) Checkbox for "Send public notices (140 chars only)
  111. */
  112. $ckey = get_config('twitter', 'consumerkey' );
  113. $csecret = get_config('twitter', 'consumersecret' );
  114. $otoken = get_pconfig(local_user(), 'twitter', 'oauthtoken' );
  115. $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
  116. $enabled = get_pconfig(local_user(), 'twitter', 'post');
  117. $checked = (($enabled) ? ' checked="checked" ' : '');
  118. $s .= '<div class="settings-block">';
  119. $s .= '<h3>'. t('Twitter Posting Settings') .'</h3>';
  120. if ( (!$ckey) && (!$csecret) ) {
  121. /***
  122. * no global consumer keys
  123. * display warning and skip personal config
  124. */
  125. $s .= '<p>'. t('No consumer key pair for Twitter found. Please contact your site administrator.') .'</p>';
  126. } else {
  127. /***
  128. * ok we have a consumer key pair now look into the OAuth stuff
  129. */
  130. if ( (!$otoken) && (!$osecret) ) {
  131. /***
  132. * the user has not yet connected the account to twitter...
  133. * get a temporary OAuth key/secret pair and display a button with
  134. * which the user can request a PIN to connect the account to a
  135. * account at Twitter.
  136. */
  137. require_once('library/twitteroauth.php');
  138. $connection = new TwitterOAuth($ckey, $csecret);
  139. $request_token = $connection->getRequestToken();
  140. $token = $request_token['oauth_token'];
  141. /***
  142. * make some nice form
  143. */
  144. $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>';
  145. $s .= '<a href="'.$connection->getAuthorizeURL($token).'" target="_twitter"><img src="addon/twitter/lighter.png" alt="'.t('Log in with Twitter').'"></a>';
  146. $s .= '<div id="twitter-pin-wrapper">';
  147. $s .= '<label id="twitter-pin-label" for="twitter-pin">'. t('Copy the PIN from Twitter here') .'</label>';
  148. $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
  149. $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="'.$token.'" />';
  150. $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="'.$request_token['oauth_token_secret'].'" />';
  151. $s .= '</div><div class="clear"></div>';
  152. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  153. } else {
  154. /***
  155. * we have an OAuth key / secret pair for the user
  156. * so let's give a chance to disable the postings to Twitter
  157. */
  158. require_once('library/twitteroauth.php');
  159. $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
  160. $details = $connection->get('account/verify_credentials');
  161. $s .= '<div id="twitter-info" ><img id="twitter-avatar" src="'.$details->profile_image_url.'" /><p id="twitter-info-block">'. t('Currently connected to: ') .'<a href="https://twitter.com/'.$details->screen_name.'" target="_twitter">'.$details->screen_name.'</a><br /><em>'.$details->description.'</em></p></div>';
  162. $s .= '<p>'. t('If enabled all your <strong>public</strong> postings will be posted to the associated Twitter account as well.') .'</p>';
  163. $s .= '<div id="twitter-enable-wrapper">';
  164. $s .= '<label id="twitter-enable-label" for="twitter-checkbox">'. t('Send public postings to Twitter'). '</label>';
  165. $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
  166. $s .= '</div><div class="clear"></div>';
  167. $s .= '<div id="twitter-disconnect-wrapper">';
  168. $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">'. t('Clear OAuth configuration') .'</label>';
  169. $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
  170. $s .= '</div><div class="clear"></div>';
  171. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  172. }
  173. }
  174. $s .= '</div><div class="clear"></div></div>';
  175. }
  176. function twitter_post_hook(&$a,&$b) {
  177. /**
  178. * Post to Twitter
  179. */
  180. logger('twitter post invoked');
  181. if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
  182. load_pconfig(local_user(), 'twitter');
  183. $ckey = get_config('twitter', 'consumerkey' );
  184. $csecret = get_config('twitter', 'consumersecret' );
  185. $otoken = get_pconfig(local_user(), 'twitter', 'oauthtoken' );
  186. $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
  187. if($ckey && $csecret && $otoken && $osecret) {
  188. $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
  189. $twitter_enable = (($twitter_post && x($_POST,'twitter_enable')) ? intval($_POST['twitter_enable']) : 0);
  190. if($twitter_post && $twitter_enable) {
  191. require_once('library/twitteroauth.php');
  192. require_once('include/bbcode.php');
  193. $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
  194. $max_char = 140; // max. length for a tweet
  195. $msg = strip_tags(bbcode($b['body']));
  196. if ( strlen($msg) > $max_char) {
  197. $shortlink = "";
  198. require_once('library/slinky.php');
  199. // post url = base url + /display/ + owner + post id
  200. // we construct this from the Owner link and replace
  201. // profile by display - this will cause an error when
  202. // /profile/ is in the owner url twice but I don't
  203. // think this will be very common...
  204. $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id'];
  205. $slinky = new Slinky( $posturl );
  206. // setup a cascade of shortening services
  207. // try to get a short link from these services
  208. // in the order ur1.ca, trim, id.gd, tinyurl
  209. $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
  210. $shortlink = $slinky->short();
  211. // the new message will be shortened such that "... $shortlink"
  212. // will fit into the character limit
  213. $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
  214. $msg .= '... ' . $shortlink;
  215. }
  216. // and now tweet it :-)
  217. if(strlen($msg))
  218. $tweet->post('statuses/update', array('status' => $msg));
  219. }
  220. }
  221. }
  222. }
  223. function plugin_admin_post(&$a){
  224. $consumerkey = ((x($_POST,'consumerkey')) ? notags(trim($_POST['consumerkey'])) : '');
  225. $consumersecret = ((x($_POST,'consumersecret')) ? notags(trim($_POST['consumersecret'])): '');
  226. set_config('twitter','consumerkey',$consumerkey);
  227. set_config('twitter','consumersecret',$consumersecret);
  228. info( t('Settings updated.'). EOL );
  229. }
  230. function plugin_admin(&$a, &$o){
  231. $t = file_get_contents( dirname(__file__). "/admin.tpl" );
  232. $o = replace_macros($t, array(
  233. '$submit' => t('Submit'),
  234. // name, label, value, help, [extra values]
  235. '$consumerkey' => array('consumerkey', t('Consumer key'), get_config('twitter', 'consumerkey' ), ''),
  236. '$consumersecret' => array('consumersecret', t('Consumer secret'), get_config('twitter', 'consumersecret' ), '')
  237. ));
  238. }