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.
 
 
 
 
 
 

415 lines
22 KiB

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