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 register_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
58 logger("installed statusnet");
62 function statusnet_uninstall() {
63 unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
64 unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
65 unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
66 unregister_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
69 function statusnet_jot_nets(&$a,&$b) {
73 $statusnet_post = get_pconfig(local_user(),'statusnet','post');
74 if(intval($statusnet_post) == 1) {
75 $statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default');
76 $selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : '');
77 $b .= '<div class="profile-jot-net"><input type="checkbox" name="statusnet_enable"' . $selected . 'value="1" /> '
78 . t('Post to StatusNet') . '</div>';
85 function statusnet_settings_post ($a,$post) {
88 if (isset($_POST['statusnet-disconnect'])) {
90 * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
91 * TODO can we revoke the access tokens at Twitter and do we need to do so?
93 del_pconfig( local_user(), 'statusnet', 'consumerkey' );
94 del_pconfig( local_user(), 'statusnet', 'consumersecret' );
95 del_pconfig( local_user(), 'statusnet', 'post' );
96 del_pconfig( local_user(), 'statusnet', 'post_by_default' );
97 del_pconfig( local_user(), 'statusnet', 'oauthtoken' );
98 del_pconfig( local_user(), 'statusnet', 'oauthsecret' );
99 del_pconfig( local_user(), 'statusnet', 'baseapi' );
101 if (isset($_POST['statusnet-consumersecret'])) {
102 // check if we can reach the API of the StatusNet server
103 // we'll check the API Version for that, if we don't get one we'll try to fix the path but will
104 // resign quickly after this one try to fix the path ;-)
105 $apibase = $_POST['statusnet-baseapi'];
106 $c = fetch_url( $apibase . 'statusnet/version.xml' );
107 if (strlen($c) > 0) {
108 // ok the API path is correct, let's save the settings
109 set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
110 set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
111 set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
113 // the API path is not correct, maybe missing trailing / ?
114 $apibase = $apibase . '/';
115 $c = fetch_url( $apibase . 'statusnet/version.xml' );
116 if (strlen($c) > 0) {
117 // ok the API path is now correct, let's save the settings
118 set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
119 set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
120 set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
122 // still not the correct API base, let's do noting
123 notice( t('We could not contact the StatusNet API with the Path you entered.').EOL );
126 goaway($a->get_baseurl().'/settings/addon');
128 if (isset($_POST['statusnet-pin'])) {
129 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
130 logger('got a StatusNet security code');
131 $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
132 $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
133 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
134 // the token and secret for which the PIN was generated were hidden in the settings
135 // form as token and token2, we need a new connection to Twitter using these token
136 // and secret to request a Access Token with the PIN
137 $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
138 $token = $connection->getAccessToken( $_POST['statusnet-pin'] );
139 // ok, now that we have the Access Token, save them in the user config
140 set_pconfig(local_user(),'statusnet', 'oauthtoken', $token['oauth_token']);
141 set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
142 set_pconfig(local_user(),'statusnet', 'post', 1);
143 // reload the Addon Settings page, if we don't do it see Bug #42
144 goaway($a->get_baseurl().'/settings/addon');
146 // if no PIN is supplied in the POST variables, the user has changed the setting
147 // to post a tweet for every new __public__ posting to the wall
148 set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
149 set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
150 notice( t('StatusNet settings updated.') . EOL);
153 function statusnet_settings(&$a,&$s) {
156 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
158 * 1) Check that we have a base api url and a consumer key & secret
159 * 2) If no OAuthtoken & stuff is present, generate button to get some
160 * allow the user to cancel the connection process at this step
161 * 3) Checkbox for "Send public notices (respect size limitation)
163 $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
164 $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
165 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
166 $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' );
167 $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
168 $enabled = get_pconfig(local_user(), 'statusnet', 'post');
169 $checked = (($enabled) ? ' checked="checked" ' : '');
170 $defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
171 $defchecked = (($defenabled) ? ' checked="checked" ' : '');
172 $s .= '<div class="settings-block">';
173 $s .= '<h3>'. t('StatusNet Posting Settings').'</h3>';
175 if ( (!$ckey) && (!$csecret) ) {
179 $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>';
180 $s .= '<div id="statusnet-consumer-wrapper">';
181 $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
182 $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" />';
183 $s .= '<div class="clear"></div>';
184 $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
185 $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" />';
186 $s .= '<div class="clear"></div>';
187 $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
188 $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" />';
189 $s .= '<div class="clear"></div></div>';
190 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
193 * ok we have a consumer key pair now look into the OAuth stuff
195 if ( (!$otoken) && (!$osecret) ) {
197 * the user has not yet connected the account to statusnet
198 * get a temporary OAuth key/secret pair and display a button with
199 * which the user can request a PIN to connect the account to a
200 * account at statusnet
202 $connection = new StatusNetOAuth($api, $ckey, $csecret);
203 $request_token = $connection->getRequestToken('oob');
204 $token = $request_token['oauth_token'];
206 * make some nice form
208 $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>';
209 $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
210 $s .= '<div id="statusnet-pin-wrapper">';
211 $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
212 $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
213 $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
214 $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
215 $s .= '</div><div class="clear"></div>';
216 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
217 $s .= '<h4>'.t('Cancel Connection Process').'</h4>';
218 $s .= '<div id="statusnet-cancel-wrapper">';
219 $s .= '<p>'.t('Current StatusNet API is').': '.$api.'</p>';
220 $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">'. t('Cancel StatusNet Connection') . '</label>';
221 $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
222 $s .= '</div><div class="clear"></div>';
223 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
226 * we have an OAuth key / secret pair for the user
227 * so let's give a chance to disable the postings to statusnet
229 $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
230 $details = $connection->get('account/verify_credentials');
231 $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>';
232 $s .= '<p>'. t('If enabled all your <strong>public</strong> postings will be posted to the associated StatusNet account.') .'</p>';
233 $s .= '<div id="statusnet-enable-wrapper">';
234 $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Allow posting to StatusNet') .'</label>';
235 $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
236 $s .= '<div class="clear"></div>';
237 $s .= '<label id="statusnet-default-label" for="statusnet-default">'. t('Send public postings to StatusNet by default') .'</label>';
238 $s .= '<input id="statusnet-default" type="checkbox" name="statusnet-default" value="1" ' . $defchecked . '/>';
239 $s .= '</div><div class="clear"></div>';
241 $s .= '<div id="statusnet-disconnect-wrapper">';
242 $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
243 $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
244 $s .= '</div><div class="clear"></div>';
245 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
248 $s .= '</div><div class="clear"></div></div>';
252 function statusnet_post_hook(&$a,&$b) {
258 logger('StatusNet post invoked');
260 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (!$b['parent']) ) {
262 load_pconfig(local_user(), 'statusnet');
264 $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
265 $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
266 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
267 $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' );
268 $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
270 if($ckey && $csecret && $otoken && $osecret) {
272 $statusnet_post = get_pconfig(local_user(),'statusnet','post');
273 $statusnet_enable = (($statusnet_post && x($_POST,'statusnet_enable')) ? intval($_POST['statusnet_enable']) : 0);
275 if($statusnet_enable && $statusnet_post) {
276 require_once('include/bbcode.php');
277 $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
278 $max_char = $dent->get_maxlength(); // max. length for a dent
279 $msg = strip_tags(bbcode($b['body']));
280 if ( strlen($msg) > $max_char) {
282 require_once('library/slinky.php');
283 // post url = base url + /display/ + owner + post id
284 // we construct this from the Owner link and replace
285 // profile by display - this will cause an error when
286 // /profile/ is in the owner url twice but I don't
287 // think this will be very common...
288 $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id'];
289 $slinky = new Slinky( $posturl );
290 // setup a cascade of shortening services
291 // try to get a short link from these services
292 // in the order ur1.ca, trim, id.gd, tinyurl
293 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
294 $shortlink = $slinky->short();
295 // the new message will be shortened such that "... $shortlink"
296 // will fit into the character limit
297 $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
298 $msg .= '... ' . $shortlink;
300 // and now tweet it :-)
302 $dent->post('statuses/update', array('status' => $msg));