]> git.mxchange.org Git - friendica.git/blob - addon/statusnet/statusnet.php
made the connection process to SN more robust
[friendica.git] / addon / statusnet / statusnet.php
1 <?php
2
3 /*   StatusNet Plugin for Friendika
4  *
5  *   Author: Tobias Diekershoff
6  *           tobias.diekershoff@gmx.net
7  *
8  *   License:3-clause BSD license (same as Friendika)
9  *
10  *   Configuration:
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".
14  *
15  *     Requirements: PHP5, curl [Slinky library]
16  *
17  *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/StatusNet_Plugin
18  */
19
20 /*   __TODO__
21  *
22  *   - what about multimedia content?
23  *     so far we just strip HTML tags from the message
24  */
25
26
27 /***
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.
30  *
31  * Thank you guys for the Twitter compatible API!
32  */
33
34 require_once('library/twitteroauth.php');
35
36 class StatusNetOAuth extends TwitterOAuth {
37     function get_maxlength() {
38         $config = $this->get($this->host . 'statusnet/config.json');
39         return $config->site->textlimit;
40     }
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;
48     }
49 }
50
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');
57
58         logger("installed statusnet");
59 }
60
61
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');
67 }
68
69 function statusnet_jot_nets(&$a,&$b) {
70         if(! local_user())
71                 return;
72
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>';    
79         }
80 }
81
82
83
84
85 function statusnet_settings_post ($a,$post) {
86         if(! local_user())
87             return;
88         if (isset($_POST['statusnet-disconnect'])) {
89             /***
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?
92              */
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', 'oauthtoken' );
97             del_pconfig( local_user(), 'statusnet', 'oauthsecret' );
98             del_pconfig( local_user(), 'statusnet', 'baseapi' );
99         } else {
100         if (isset($_POST['statusnet-consumersecret'])) {
101             //  check if we can reach the API of the StatusNet server
102             //  we'll check the API Version for that, if we don't get one we'll try to fix the path but will
103             //  resign
104             $apibase = $_POST['statusnet-baseapi'];
105             $f = fopen( $apibase . 'statusnet/version.xml', 'r');
106             $c = stream_get_contents($f);
107             fclose($f);
108             if (strlen($c) > 0) {
109                 //  ok the API path is correct, let's save the settings
110                 set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
111                 set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
112                 set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
113             } else {
114                 //  the API path is not correct, maybe missing trailing / ?
115                 $apibase = $apibase . '/';
116                 $f = fopen( $apibase . 'statusnet/version.xml', 'r');
117                 $c = stream_get_contents($f);
118                 fclose($f);
119                 if (strlen($c) > 0) {
120                     //  ok the API path is now correct, let's save the settings
121                     set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
122                     set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
123                     set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
124                 } else {
125                     //  still not the correct API base, let's do noting
126                     notice( t('We could not contact the StatusNet API with the Path you entered.').EOL );
127                 }
128             }
129             header('Location: '.$a->get_baseurl().'/settings/addon');
130         } else {
131         if (isset($_POST['statusnet-pin'])) {
132             //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
133             logger('got a StatusNet security code');
134             $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
135             $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
136             $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
137             //  the token and secret for which the PIN was generated were hidden in the settings
138             //  form as token and token2, we need a new connection to Twitter using these token
139             //  and secret to request a Access Token with the PIN
140             $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
141             $token   = $connection->getAccessToken( $_POST['statusnet-pin'] );
142             //  ok, now that we have the Access Token, save them in the user config
143             set_pconfig(local_user(),'statusnet', 'oauthtoken',  $token['oauth_token']);
144             set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
145             set_pconfig(local_user(),'statusnet', 'post', 1);
146             //  reload the Addon Settings page, if we don't do it see Bug #42
147             header('Location: '.$a->get_baseurl().'/settings/addon');
148         } else {
149             //  if no PIN is supplied in the POST variables, the user has changed the setting
150             //  to post a tweet for every new __public__ posting to the wall
151             set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
152         }}}
153 }
154 function statusnet_settings(&$a,&$s) {
155         if(! local_user())
156                 return;
157         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
158         /***
159          * 1) Check that we have a base api url and a consumer key & secret
160          * 2) If no OAuthtoken & stuff is present, generate button to get some
161          *    allow the user to cancel the connection process at this step
162          * 3) Checkbox for "Send public notices (respect size limitation)
163          */
164         $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
165         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
166         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
167         $otoken  = get_pconfig(local_user(), 'statusnet', 'oauthtoken'  );
168         $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
169         $enabled = get_pconfig(local_user(), 'statusnet', 'post');
170         $checked = (($enabled) ? ' checked="checked" ' : '');
171         $s .= '<div class="settings-block">';
172         $s .= '<h3>'. t('StatusNet Posting Settings').'</h3>';
173
174         if ( (!$ckey) && (!$csecret) ) {
175                 /***
176                  * no consumer keys
177                  */
178                 $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>';
179                 $s .= '<div id="statusnet-consumer-wrapper">';
180                 $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
181                                 $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" />';
182                                 $s .= '<div class="clear"></div>';
183                 $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
184                                 $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" />';
185                                 $s .= '<div class="clear"></div>';
186                 $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
187                 $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" />';
188                 $s .= '<div class="clear"></div></div>';
189                 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
190         } else {
191                 /***
192                  * ok we have a consumer key pair now look into the OAuth stuff
193                  */
194                 if ( (!$otoken) && (!$osecret) ) {
195                         /***
196                          * the user has not yet connected the account to statusnet
197                          * get a temporary OAuth key/secret pair and display a button with
198                          * which the user can request a PIN to connect the account to a
199                          * account at statusnet
200                          */
201                         $connection = new StatusNetOAuth($api, $ckey, $csecret);
202                         $request_token = $connection->getRequestToken('oob');
203                         $token = $request_token['oauth_token'];
204                         /***
205                          *  make some nice form
206                          */
207                         $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>';
208                         $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
209                         $s .= '<div id="statusnet-pin-wrapper">';
210                         $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
211                         $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
212                         $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
213                         $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
214                         $s .= '</div><div class="clear"></div>';
215                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
216                         $s .= '<h4>'.t('Cancel Connection Process').'</h4>';
217                         $s .= '<div id="statusnet-cancel-wrapper">';
218                         $s .= '<p>'.t('Current StatusNet API is').': '.$api.'</p>';
219                         $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">'. t('Cancel StatusNet Connection') . '</label>';
220                         $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
221                         $s .= '</div><div class="clear"></div>';
222                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
223                 } else {
224                         /***
225                          *  we have an OAuth key / secret pair for the user
226                          *  so let's give a chance to disable the postings to statusnet
227                          */
228                         $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
229                         $details = $connection->get('account/verify_credentials');
230                         $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>';
231                         $s .= '<p>'. t('If enabled all your <strong>public</strong> postings will be posted to the associated StatusNet account as well.') .'</p>';
232                         $s .= '<div id="statusnet-enable-wrapper">';
233                         $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Send public postings to StatusNet') .'</label>';
234                         $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
235                         $s .= '</div><div class="clear"></div>';
236                         $s .= '<div id="statusnet-disconnect-wrapper">';
237                         $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
238                         $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
239                         $s .= '</div><div class="clear"></div>';
240                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
241                 }
242         }
243         $s .= '</div><div class="clear"></div></div>';
244 }
245
246
247 function statusnet_post_hook(&$a,&$b) {
248
249         /**
250          * Post to statusnet
251          */
252
253         logger('StatusNet post invoked');
254
255         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (!$b['parent']) ) {
256
257                 load_pconfig(local_user(), 'statusnet');
258             
259                 $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
260                 $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
261                 $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
262                 $otoken  = get_pconfig(local_user(), 'statusnet', 'oauthtoken'  );
263                 $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
264
265                 if($ckey && $csecret && $otoken && $osecret) {
266
267                         $statusnet_post = get_pconfig(local_user(),'statusnet','post');
268                         $statusnet_enable = (($statusnet_post && x($_POST,'statusnet_enable')) ? intval($_POST['statusnet_enable']) : 0);
269
270                         if($statusnet_enable && $statusnet_post) {
271                                 require_once('include/bbcode.php');     
272                                 $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
273                                 $max_char = $dent->get_maxlength(); // max. length for a dent
274                                 $msg = strip_tags(bbcode($b['body']));
275                                 if ( strlen($msg) > $max_char) {
276                                         $shortlink = "";
277                                         require_once('library/slinky.php');
278                                         // post url = base url + /display/ + owner + post id
279                                         // we construct this from the Owner link and replace
280                                         // profile by display - this will cause an error when
281                                         // /profile/ is in the owner url twice but I don't
282                                         // think this will be very common...
283                                         $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id'];
284                                         $slinky = new Slinky( $posturl );
285                                         // setup a cascade of shortening services
286                                         // try to get a short link from these services
287                                         // in the order ur1.ca, trim, id.gd, tinyurl
288                                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
289                                         $shortlink = $slinky->short();
290                                         // the new message will be shortened such that "... $shortlink"
291                                         // will fit into the character limit
292                                         $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
293                                         $msg .= '... ' . $shortlink;
294                                 }
295                                 // and now tweet it :-)
296                                 if(strlen($msg))
297                                         $dent->post('statuses/update', array('status' => $msg));
298                         }
299                 }
300     }
301 }
302