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.

347 lines
20 KiB

  1. <?php
  2. /* StatusNet Plugin for Friendika
  3. *
  4. * Author: Tobias Diekershoff
  5. * tobias.diekershoff@gmx.net
  6. *
  7. * License:3-clause BSD license (same as Friendika)
  8. *
  9. * Configuration:
  10. * To activate the plugin itself add it to the $a->config['system']['addon']
  11. * setting. After this, your user can configure their Twitter account settings
  12. * from "Settings -> Plugin Settings".
  13. *
  14. * Requirements: PHP5, curl [Slinky library]
  15. *
  16. * Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/StatusNet_Plugin
  17. */
  18. /* __TODO__
  19. *
  20. * - what about multimedia content?
  21. * so far we just strip HTML tags from the message
  22. */
  23. /***
  24. * We have to alter the TwitterOAuth class a little bit to work with any StatusNet
  25. * installation abroad. Basically it's only make the API path variable and be happy.
  26. *
  27. * Thank you guys for the Twitter compatible API!
  28. */
  29. require_once('library/twitteroauth.php');
  30. class StatusNetOAuth extends TwitterOAuth {
  31. function get_maxlength() {
  32. $config = $this->get($this->host . 'statusnet/config.json');
  33. return $config->site->textlimit;
  34. }
  35. function accessTokenURL() { return $this->host.'oauth/access_token'; }
  36. function authenticateURL() { return $this->host.'oauth/authenticate'; }
  37. function authorizeURL() { return $this->host.'oauth/authorize'; }
  38. function requestTokenURL() { return $this->host.'oauth/request_token'; }
  39. function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  40. parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
  41. $this->host = $apipath;
  42. }
  43. }
  44. function statusnet_install() {
  45. // we need some hooks, for the configuration and for sending tweets
  46. register_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
  47. register_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
  48. register_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
  49. register_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
  50. logger("installed statusnet");
  51. }
  52. function statusnet_uninstall() {
  53. unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
  54. unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
  55. unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
  56. unregister_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
  57. }
  58. function statusnet_jot_nets(&$a,&$b) {
  59. if(! local_user())
  60. return;
  61. $statusnet_post = get_pconfig(local_user(),'statusnet','post');
  62. if(intval($statusnet_post) == 1) {
  63. $statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default');
  64. $selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : '');
  65. $b .= '<div class="profile-jot-net"><input type="checkbox" name="statusnet_enable"' . $selected . 'value="1" /> '
  66. . t('Post to StatusNet') . '</div>';
  67. }
  68. }
  69. function statusnet_settings_post ($a,$post) {
  70. if(! local_user())
  71. return;
  72. if (isset($_POST['statusnet-disconnect'])) {
  73. /***
  74. * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
  75. * TODO can we revoke the access tokens at Twitter and do we need to do so?
  76. */
  77. del_pconfig( local_user(), 'statusnet', 'consumerkey' );
  78. del_pconfig( local_user(), 'statusnet', 'consumersecret' );
  79. del_pconfig( local_user(), 'statusnet', 'post' );
  80. del_pconfig( local_user(), 'statusnet', 'post_by_default' );
  81. del_pconfig( local_user(), 'statusnet', 'oauthtoken' );
  82. del_pconfig( local_user(), 'statusnet', 'oauthsecret' );
  83. del_pconfig( local_user(), 'statusnet', 'baseapi' );
  84. } else {
  85. if (isset($_POST['statusnet-preconf-apiurl'])) {
  86. /***
  87. * If the user used one of the preconfigured StatusNet server credentials
  88. * use them. All the data are available in the global config.
  89. * Check the API Url never the less and blame the admin if it's not working ^^
  90. */
  91. $globalsn = get_config('statusnet', 'sites');
  92. foreach ( $globalsn as $asn) {
  93. if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl'] ) {
  94. $apibase = $asn['apiurl'];
  95. $c = fetch_url( $apibase . 'statusnet/version.xml' );
  96. if (strlen($c) > 0) {
  97. set_pconfig(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey'] );
  98. set_pconfig(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret'] );
  99. set_pconfig(local_user(), 'statusnet', 'baseapi', $asn['apiurl'] );
  100. } else {
  101. notice( t('Please contact your site administrator.<br />The provided API URL is not valid.').EOL.$asn['apiurl'].EOL );
  102. }
  103. }
  104. }
  105. goaway($a->get_baseurl().'/settings/addon');
  106. } else {
  107. if (isset($_POST['statusnet-consumersecret'])) {
  108. // check if we can reach the API of the StatusNet server
  109. // we'll check the API Version for that, if we don't get one we'll try to fix the path but will
  110. // resign quickly after this one try to fix the path ;-)
  111. $apibase = $_POST['statusnet-baseapi'];
  112. $c = fetch_url( $apibase . 'statusnet/version.xml' );
  113. if (strlen($c) > 0) {
  114. // ok the API path is correct, let's save the settings
  115. set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
  116. set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
  117. set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
  118. } else {
  119. // the API path is not correct, maybe missing trailing / ?
  120. $apibase = $apibase . '/';
  121. $c = fetch_url( $apibase . 'statusnet/version.xml' );
  122. if (strlen($c) > 0) {
  123. // ok the API path is now correct, let's save the settings
  124. set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
  125. set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
  126. set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
  127. } else {
  128. // still not the correct API base, let's do noting
  129. notice( t('We could not contact the StatusNet API with the Path you entered.').EOL );
  130. }
  131. }
  132. goaway($a->get_baseurl().'/settings/addon');
  133. } else {
  134. if (isset($_POST['statusnet-pin'])) {
  135. // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
  136. logger('got a StatusNet security code');
  137. $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
  138. $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
  139. $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
  140. // the token and secret for which the PIN was generated were hidden in the settings
  141. // form as token and token2, we need a new connection to Twitter using these token
  142. // and secret to request a Access Token with the PIN
  143. $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
  144. $token = $connection->getAccessToken( $_POST['statusnet-pin'] );
  145. // ok, now that we have the Access Token, save them in the user config
  146. set_pconfig(local_user(),'statusnet', 'oauthtoken', $token['oauth_token']);
  147. set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
  148. set_pconfig(local_user(),'statusnet', 'post', 1);
  149. // reload the Addon Settings page, if we don't do it see Bug #42
  150. goaway($a->get_baseurl().'/settings/addon');
  151. } else {
  152. // if no PIN is supplied in the POST variables, the user has changed the setting
  153. // to post a tweet for every new __public__ posting to the wall
  154. set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
  155. set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
  156. info( t('StatusNet settings updated.') . EOL);
  157. }}}}
  158. }
  159. function statusnet_settings(&$a,&$s) {
  160. if(! local_user())
  161. return;
  162. $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
  163. /***
  164. * 1) Check that we have a base api url and a consumer key & secret
  165. * 2) If no OAuthtoken & stuff is present, generate button to get some
  166. * allow the user to cancel the connection process at this step
  167. * 3) Checkbox for "Send public notices (respect size limitation)
  168. */
  169. $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
  170. $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
  171. $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
  172. $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' );
  173. $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
  174. $enabled = get_pconfig(local_user(), 'statusnet', 'post');
  175. $checked = (($enabled) ? ' checked="checked" ' : '');
  176. $defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
  177. $defchecked = (($defenabled) ? ' checked="checked" ' : '');
  178. $s .= '<div class="settings-block">';
  179. $s .= '<h3>'. t('StatusNet Posting Settings').'</h3>';
  180. if ( (!$ckey) && (!$csecret) ) {
  181. /***
  182. * no consumer keys
  183. */
  184. $globalsn = get_config('statusnet', 'sites');
  185. /***
  186. * lets check if we have one or more globally configured StatusNet
  187. * server OAuth credentials in the configuration. If so offer them
  188. * with a little explanation to the user as choice - otherwise
  189. * ignore this option entirely.
  190. */
  191. if (! $globalsn == null) {
  192. $s .= '<h4>' . t('Globally Available StatusNet OAuthKeys') . '</h4>';
  193. $s .= '<p>'. t("There are preconfigured OAuth key pairs for some StatusNet servers available. If you are useing one of them, please use these credentials. If not feel free to connect to any other StatusNet instance \x28see below\x29.") .'</p>';
  194. $s .= '<div id="statusnet-preconf-wrapper">';
  195. foreach ($globalsn as $asn) {
  196. $s .= '<input type="radio" name="statusnet-preconf-apiurl" value="'. $asn['apiurl'] .'">'. $asn['sitename'] .'<br />';
  197. }
  198. $s .= '<p></p><div class="clear"></div></div>';
  199. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  200. }
  201. $s .= '<h4>' . t('Provide your own OAuth Credentials') . '</h4>';
  202. $s .= '<p>'. t('No consumer key pair for StatusNet found. Register your Friendika Account as an desktop client on your StatusNet account, copy the consumer key pair here and enter the API base root.<br />Before you register your own OAuth key pair ask the administrator if there is already a key pair for this Friendika installation at your favorited StatusNet installation.') .'</p>';
  203. $s .= '<div id="statusnet-consumer-wrapper">';
  204. $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
  205. $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" /><br />';
  206. $s .= '<div class="clear"></div>';
  207. $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
  208. $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" /><br />';
  209. $s .= '<div class="clear"></div>';
  210. $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
  211. $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" /><br />';
  212. $s .= '<p></p><div class="clear"></div></div>';
  213. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  214. } else {
  215. /***
  216. * ok we have a consumer key pair now look into the OAuth stuff
  217. */
  218. if ( (!$otoken) && (!$osecret) ) {
  219. /***
  220. * the user has not yet connected the account to statusnet
  221. * get a temporary OAuth key/secret pair and display a button with
  222. * which the user can request a PIN to connect the account to a
  223. * account at statusnet
  224. */
  225. $connection = new StatusNetOAuth($api, $ckey, $csecret);
  226. $request_token = $connection->getRequestToken('oob');
  227. $token = $request_token['oauth_token'];
  228. /***
  229. * make some nice form
  230. */
  231. $s .= '<p>'. t('To connect to your StatusNet account click the button below to get a security code from StatusNet which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to StatusNet.') .'</p>';
  232. $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
  233. $s .= '<div id="statusnet-pin-wrapper">';
  234. $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
  235. $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
  236. $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
  237. $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
  238. $s .= '</div><div class="clear"></div>';
  239. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  240. $s .= '<h4>'.t('Cancel Connection Process').'</h4>';
  241. $s .= '<div id="statusnet-cancel-wrapper">';
  242. $s .= '<p>'.t('Current StatusNet API is').': '.$api.'</p>';
  243. $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">'. t('Cancel StatusNet Connection') . '</label>';
  244. $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
  245. $s .= '</div><div class="clear"></div>';
  246. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  247. } else {
  248. /***
  249. * we have an OAuth key / secret pair for the user
  250. * so let's give a chance to disable the postings to statusnet
  251. */
  252. $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
  253. $details = $connection->get('account/verify_credentials');
  254. $s .= '<div id="statusnet-info" ><img id="statusnet-avatar" src="'.$details->profile_image_url.'" /><p id="statusnet-info-block">'. t('Currently connected to: ') .'<a href="'.$details->statusnet_profile_url.'" target="_statusnet">'.$details->screen_name.'</a><br /><em>'.$details->description.'</em></p></div>';
  255. $s .= '<p>'. t('If enabled all your <strong>public</strong> postings will be posted to the associated StatusNet account.') .'</p>';
  256. $s .= '<div id="statusnet-enable-wrapper">';
  257. $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Allow posting to StatusNet') .'</label>';
  258. $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
  259. $s .= '<div class="clear"></div>';
  260. $s .= '<label id="statusnet-default-label" for="statusnet-default">'. t('Send public postings to StatusNet by default') .'</label>';
  261. $s .= '<input id="statusnet-default" type="checkbox" name="statusnet-default" value="1" ' . $defchecked . '/>';
  262. $s .= '</div><div class="clear"></div>';
  263. $s .= '<div id="statusnet-disconnect-wrapper">';
  264. $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
  265. $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
  266. $s .= '</div><div class="clear"></div>';
  267. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  268. }
  269. }
  270. $s .= '</div><div class="clear"></div></div>';
  271. }
  272. function statusnet_post_hook(&$a,&$b) {
  273. /**
  274. * Post to statusnet
  275. */
  276. logger('StatusNet post invoked');
  277. if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (!$b['parent']) ) {
  278. load_pconfig(local_user(), 'statusnet');
  279. $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
  280. $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
  281. $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
  282. $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' );
  283. $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
  284. if($ckey && $csecret && $otoken && $osecret) {
  285. $statusnet_post = get_pconfig(local_user(),'statusnet','post');
  286. $statusnet_enable = (($statusnet_post && x($_POST,'statusnet_enable')) ? intval($_POST['statusnet_enable']) : 0);
  287. if($statusnet_enable && $statusnet_post) {
  288. require_once('include/bbcode.php');
  289. $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
  290. $max_char = $dent->get_maxlength(); // max. length for a dent
  291. $msg = strip_tags(bbcode($b['body']));
  292. if ( strlen($msg) > $max_char) {
  293. $shortlink = "";
  294. require_once('library/slinky.php');
  295. // post url = base url + /display/ + owner + post id
  296. // we construct this from the Owner link and replace
  297. // profile by display - this will cause an error when
  298. // /profile/ is in the owner url twice but I don't
  299. // think this will be very common...
  300. $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id'];
  301. $slinky = new Slinky( $posturl );
  302. // setup a cascade of shortening services
  303. // try to get a short link from these services
  304. // in the order ur1.ca, trim, id.gd, tinyurl
  305. $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
  306. $shortlink = $slinky->short();
  307. // the new message will be shortened such that "... $shortlink"
  308. // will fit into the character limit
  309. $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
  310. $msg .= '... ' . $shortlink;
  311. }
  312. // and now tweet it :-)
  313. if(strlen($msg))
  314. $dent->post('statuses/update', array('status' => $msg));
  315. }
  316. }
  317. }
  318. }