3 /* StatusNet Plugin for Friendika
5 * Author: Tobias Diekershoff
6 * tobias.diekershoff@gmx.net
8 * License:3-clause BSD license (same as Friendika)
11 * To activate the plugin itself add it to the $a->config['system']['addon']
12 * setting. After this, your user can configure their Twitter account settings
13 * from "Settings -> Plugin Settings".
15 * Requirements: PHP5, curl [Slinky library]
17 * Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/StatusNet_Plugin
22 * - what about multimedia content?
23 * so far we just strip HTML tags from the message
28 * We have to alter the TwitterOAuth class a little bit to work with any StatusNet
29 * installation abroad. Basically it's only make the API path variable and be happy.
31 * Thank you guys for the Twitter compatible API!
34 require_once('library/twitteroauth.php');
36 class StatusNetOAuth extends TwitterOAuth {
37 function get_maxlength() {
38 $config = $this->get($this->host . 'statusnet/config.json');
39 return $config->site->textlimit;
41 function accessTokenURL() { return $this->host.'oauth/access_token'; }
42 function authenticateURL() { return $this->host.'oauth/authenticate'; }
43 function authorizeURL() { return $this->host.'oauth/authorize'; }
44 function requestTokenURL() { return $this->host.'oauth/request_token'; }
45 function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
46 parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
47 $this->host = $apipath;
51 function statusnet_install() {
52 // we need some hooks, for the configuration and for sending tweets
53 register_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
54 register_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
55 register_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
56 logger("installed statusnet");
60 function statusnet_uninstall() {
61 unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
62 unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
63 unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
66 function statusnet_settings_post ($a,$post) {
69 if (isset($_POST['statusnet-disconnect'])) {
71 * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
72 * TODO can we revoke the access tokens at Twitter and do we need to do so?
74 del_pconfig( local_user(), 'statusnet', 'consumerkey' );
75 del_pconfig( local_user(), 'statusnet', 'consumersecret' );
76 del_pconfig( local_user(), 'statusnet', 'post' );
77 del_pconfig( local_user(), 'statusnet', 'oauthtoken' );
78 del_pconfig( local_user(), 'statusnet', 'oauthsecret' );
79 del_pconfig( local_user(), 'statusnet', 'baseapi' );
81 if (isset($_POST['statusnet-consumersecret'])) {
82 set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
83 set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
84 set_pconfig(local_user(), 'statusnet', 'baseapi', $_POST['statusnet-baseapi']);
85 header('Location: '.$a->get_baseurl().'/settings/addon');
87 if (isset($_POST['statusnet-pin'])) {
88 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
89 logger('got a StatusNet security code');
90 $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
91 $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
92 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
93 // the token and secret for which the PIN was generated were hidden in the settings
94 // form as token and token2, we need a new connection to Twitter using these token
95 // and secret to request a Access Token with the PIN
96 $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
97 $token = $connection->getAccessToken( $_POST['statusnet-pin'] );
98 // ok, now that we have the Access Token, save them in the user config
99 set_pconfig(local_user(),'statusnet', 'oauthtoken', $token['oauth_token']);
100 set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
101 set_pconfig(local_user(),'statusnet', 'post', 1);
102 // reload the Addon Settings page, if we don't do it see Bug #42
103 header('Location: '.$a->get_baseurl().'/settings/addon');
105 // if no PIN is supplied in the POST variables, the user has changed the setting
106 // to post a tweet for every new __public__ posting to the wall
107 set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
110 function statusnet_settings(&$a,&$s) {
113 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
115 * 1) Check that we have a base api url and a consumer key & secret
116 * 2) If no OAuthtoken & stuff is present, generate button to get some
117 * 3) Checkbox for "Send public notices (respect size limitation)
119 $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
120 $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
121 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
122 $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' );
123 $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
124 $enabled = get_pconfig(local_user(), 'statusnet', 'post');
125 $checked = (($enabled) ? ' checked="checked" ' : '');
126 $s .= '<h3>'.t('StatusNet Posting Settings').'</h3>';
128 if ( (!$ckey) && (!$csecret) ) {
132 $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>';
133 $s .= '<div id="statusnet-consumer-wrapper">';
134 $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
135 $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" /><br />';
136 $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
137 $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" /><br />';
138 $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
139 $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" /><br />';
140 $s .= '</div><div class="clear"></div>';
141 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
144 * ok we have a consumer key pair now look into the OAuth stuff
146 if ( (!$otoken) && (!$osecret) ) {
148 * the user has not yet connected the account to statusnet
149 * get a temporary OAuth key/secret pair and display a button with
150 * which the user can request a PIN to connect the account to a
151 * account at statusnet
153 $connection = new StatusNetOAuth($api, $ckey, $csecret);
154 $request_token = $connection->getRequestToken('oob');
155 $token = $request_token['oauth_token'];
157 * make some nice form
159 $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>';
160 $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
161 $s .= '<div id="statusnet-pin-wrapper">';
162 $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
163 $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
164 $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
165 $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
166 $s .= '</div><div class="clear"></div>';
167 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
170 * we have an OAuth key / secret pair for the user
171 * so let's give a chance to disable the postings to statusnet
173 $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
174 $details = $connection->get('account/verify_credentials');
175 $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>';
176 $s .= '<p>'. t('If enabled all your <strong>public</strong> postings will be posted to the associated StatusNet account as well.') .'</p>';
177 $s .= '<div id="statusnet-enable-wrapper">';
178 $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Send public postings to StatusNet') .'</label>';
179 $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
180 $s .= '</div><div class="clear"></div>';
181 $s .= '<div id="statusnet-disconnect-wrapper">';
182 $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
183 $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
184 $s .= '</div><div class="clear"></div>';
185 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
188 $s .= '</div><div class="clear"></div>';
192 function statusnet_post_hook(&$a,&$b) {
198 logger('StatusNet post invoked');
200 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (!$b['parent']) ) {
202 load_pconfig(local_user(), 'statusnet');
204 $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
205 $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
206 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
207 $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' );
208 $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
210 if($ckey && $csecret && $otoken && $osecret) {
212 $statusnet_post = get_pconfig(local_user(),'statusnet','post');
214 if($statusnet_post) {
215 require_once('include/bbcode.php');
216 $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
217 $max_char = $dent->get_maxlength(); // max. length for a dent
218 $msg = strip_tags(bbcode($b['body']));
219 if ( strlen($msg) > $max_char) {
221 require_once('library/slinky.php');
222 // post url = base url + /display/ + owner + post id
223 // we construct this from the Owner link and replace
224 // profile by display - this will cause an error when
225 // /profile/ is in the owner url twice but I don't
226 // think this will be very common...
227 $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id'];
228 $slinky = new Slinky( $posturl );
229 // setup a cascade of shortening services
230 // try to get a short link from these services
231 // in the order ur1.ca, trim, id.gd, tinyurl
232 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
233 $shortlink = $slinky->short();
234 // the new message will be shortened such that "... $shortlink"
235 // will fit into the character limit
236 $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
237 $msg .= '... ' . $shortlink;
239 // and now tweet it :-)
241 $dent->post('statuses/update', array('status' => $msg));