]> git.mxchange.org Git - friendica-addons.git/blob - statusnet/statusnet.php
app name now in form if user types in her own connection
[friendica-addons.git] / statusnet / statusnet.php
1 <?php
2 /**
3  * Name: StatusNet Connector
4  * Description: Relay public postings to a connected StatusNet account
5  * Version: 1.0.5
6  * Author: Tobias Diekershoff <http://diekershoff.homeunix.net/friendika/profile/tobias>
7  */
8  
9 /*   StatusNet Plugin for Friendica
10  *
11  *   Author: Tobias Diekershoff
12  *           tobias.diekershoff@gmx.net
13  *
14  *   License:3-clause BSD license
15  *
16  *   Configuration:
17  *     To activate the plugin itself add it to the $a->config['system']['addon']
18  *     setting. After this, your user can configure their Twitter account settings
19  *     from "Settings -> Plugin Settings".
20  *
21  *     Requirements: PHP5, curl [Slinky library]
22  *
23  *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/StatusNet_Plugin
24  */
25
26 /***
27  * We have to alter the TwitterOAuth class a little bit to work with any StatusNet
28  * installation abroad. Basically it's only make the API path variable and be happy.
29  *
30  * Thank you guys for the Twitter compatible API!
31  */
32
33 define('STATUSNET_DEFAULT_POLL_INTERVAL', 5); // given in minutes
34
35 require_once('library/twitteroauth.php');
36
37 class StatusNetOAuth extends TwitterOAuth {
38     function get_maxlength() {
39         $config = $this->get($this->host . 'statusnet/config.json');
40         return $config->site->textlimit;
41     }
42     function accessTokenURL()  { return $this->host.'oauth/access_token'; }
43     function authenticateURL() { return $this->host.'oauth/authenticate'; } 
44     function authorizeURL() { return $this->host.'oauth/authorize'; }
45     function requestTokenURL() { return $this->host.'oauth/request_token'; }
46     function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
47         parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
48         $this->host = $apipath;
49     }
50   /**
51    * Make an HTTP request
52    *
53    * @return API results
54    *
55    * Copied here from the twitteroauth library and complemented by applying the proxy settings of friendica
56    */
57   function http($url, $method, $postfields = NULL) {
58     $this->http_info = array();
59     $ci = curl_init();
60     /* Curl settings */
61     $prx = get_config('system','proxy');
62     if(strlen($prx)) {
63         curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL, 1);
64         curl_setopt($ci, CURLOPT_PROXY, $prx);
65         $prxusr = get_config('system','proxyuser');
66         if(strlen($prxusr))
67             curl_setopt($ci, CURLOPT_PROXYUSERPWD, $prxusr);
68     }
69     curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
70     curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
71     curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
72     curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
73     curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
74     curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
75     curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
76     curl_setopt($ci, CURLOPT_HEADER, FALSE);
77
78     switch ($method) {
79       case 'POST':
80         curl_setopt($ci, CURLOPT_POST, TRUE);
81         if (!empty($postfields)) {
82           curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
83         }
84         break;
85       case 'DELETE':
86         curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
87         if (!empty($postfields)) {
88           $url = "{$url}?{$postfields}";
89         }
90     }
91
92     curl_setopt($ci, CURLOPT_URL, $url);
93     $response = curl_exec($ci);
94     $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
95     $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
96     $this->url = $url;
97     curl_close ($ci);
98     return $response;
99   }
100 }
101
102 function statusnet_install() {
103         //  we need some hooks, for the configuration and for sending tweets
104         register_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
105         register_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
106         register_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
107         register_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
108         register_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
109         register_hook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
110         logger("installed statusnet");
111 }
112
113
114 function statusnet_uninstall() {
115         unregister_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
116         unregister_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
117         unregister_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
118         unregister_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
119         unregister_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
120         unregister_hook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
121
122         // old setting - remove only
123         unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
124         unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
125         unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
126
127 }
128
129 function statusnet_jot_nets(&$a,&$b) {
130         if(! local_user())
131                 return;
132
133         $statusnet_post = get_pconfig(local_user(),'statusnet','post');
134         if(intval($statusnet_post) == 1) {
135                 $statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default');
136                 $selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : '');
137                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="statusnet_enable"' . $selected . ' value="1" /> ' 
138                         . t('Post to StatusNet') . '</div>';
139         }
140 }
141
142 function statusnet_settings_post ($a,$post) {
143         if(! local_user())
144             return;
145         // don't check statusnet settings if statusnet submit button is not clicked
146         if (!x($_POST,'statusnet-submit')) return;
147         
148         if (isset($_POST['statusnet-disconnect'])) {
149             /***
150              * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
151              */
152             del_pconfig(local_user(), 'statusnet', 'consumerkey');
153             del_pconfig(local_user(), 'statusnet', 'consumersecret');
154             del_pconfig(local_user(), 'statusnet', 'post');
155             del_pconfig(local_user(), 'statusnet', 'post_by_default');
156             del_pconfig(local_user(), 'statusnet', 'oauthtoken');
157             del_pconfig(local_user(), 'statusnet', 'oauthsecret');
158             del_pconfig(local_user(), 'statusnet', 'baseapi');
159             del_pconfig(local_user(), 'statusnet', 'post_taglinks');
160             del_pconfig(local_user(), 'statusnet', 'lastid');
161             del_pconfig(local_user(), 'statusnet', 'mirror_posts');
162             del_pconfig(local_user(), 'statusnet', 'intelligent_shortening');
163         } else {
164             if (isset($_POST['statusnet-preconf-apiurl'])) {
165                 /***
166                  * If the user used one of the preconfigured StatusNet server credentials
167                  * use them. All the data are available in the global config.
168                  * Check the API Url never the less and blame the admin if it's not working ^^
169                  */
170                 $globalsn = get_config('statusnet', 'sites');
171                 foreach ( $globalsn as $asn) {
172                     if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl'] ) {
173                         $apibase = $asn['apiurl'];
174                         $c = fetch_url( $apibase . 'statusnet/version.xml' );
175                         if (strlen($c) > 0) {
176                             set_pconfig(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey'] );
177                             set_pconfig(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret'] );
178                             set_pconfig(local_user(), 'statusnet', 'baseapi', $asn['apiurl'] );
179                             set_pconfig(local_user(), 'statusnet', 'application_name', $asn['applicationname'] );
180                         } else {
181                             notice( t('Please contact your site administrator.<br />The provided API URL is not valid.').EOL.$asn['apiurl'].EOL );
182                         }
183                     }
184                 }
185                 goaway($a->get_baseurl().'/settings/connectors');
186             } else {
187             if (isset($_POST['statusnet-consumersecret'])) {
188                 //  check if we can reach the API of the StatusNet server
189                 //  we'll check the API Version for that, if we don't get one we'll try to fix the path but will
190                 //  resign quickly after this one try to fix the path ;-)
191                 $apibase = $_POST['statusnet-baseapi'];
192                 $c = fetch_url( $apibase . 'statusnet/version.xml' );
193                 if (strlen($c) > 0) {
194                     //  ok the API path is correct, let's save the settings
195                     set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
196                     set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
197                     set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
198                     set_pconfig(local_user(), 'statusnet', 'application_name', $_POST['statusnet-applicationname'] );
199                 } else {
200                     //  the API path is not correct, maybe missing trailing / ?
201                     $apibase = $apibase . '/';
202                     $c = fetch_url( $apibase . 'statusnet/version.xml' );
203                     if (strlen($c) > 0) {
204                         //  ok the API path is now correct, let's save the settings
205                         set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
206                         set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
207                         set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
208                     } else {
209                         //  still not the correct API base, let's do noting
210                         notice( t('We could not contact the StatusNet API with the Path you entered.').EOL );
211                     }
212                 }
213                 goaway($a->get_baseurl().'/settings/connectors');
214             } else {
215                 if (isset($_POST['statusnet-pin'])) {
216                         //  if the user supplied us with a PIN from StatusNet, let the magic of OAuth happen
217                     $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
218                                         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
219                                         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
220                                         //  the token and secret for which the PIN was generated were hidden in the settings
221                                         //  form as token and token2, we need a new connection to Twitter using these token
222                                         //  and secret to request a Access Token with the PIN
223                                         $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
224                                         $token   = $connection->getAccessToken( $_POST['statusnet-pin'] );
225                                         //  ok, now that we have the Access Token, save them in the user config
226                                         set_pconfig(local_user(),'statusnet', 'oauthtoken',  $token['oauth_token']);
227                                         set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
228                                         set_pconfig(local_user(),'statusnet', 'post', 1);
229                                         set_pconfig(local_user(),'statusnet', 'post_taglinks', 1);
230                     //  reload the Addon Settings page, if we don't do it see Bug #42
231                     goaway($a->get_baseurl().'/settings/connectors');
232                                 } else {
233                                         //  if no PIN is supplied in the POST variables, the user has changed the setting
234                                         //  to post a dent for every new __public__ posting to the wall
235                                         set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
236                                         set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
237                                         set_pconfig(local_user(),'statusnet','post_taglinks',intval($_POST['statusnet-sendtaglinks']));
238                                         set_pconfig(local_user(), 'statusnet', 'mirror_posts', intval($_POST['statusnet-mirror']));
239                                         set_pconfig(local_user(), 'statusnet', 'intelligent_shortening', intval($_POST['statusnet-shortening']));
240                                         info( t('StatusNet settings updated.') . EOL);
241                 }}}}
242 }
243 function statusnet_settings(&$a,&$s) {
244         if(! local_user())
245                 return;
246         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
247         /***
248          * 1) Check that we have a base api url and a consumer key & secret
249          * 2) If no OAuthtoken & stuff is present, generate button to get some
250          *    allow the user to cancel the connection process at this step
251          * 3) Checkbox for "Send public notices (respect size limitation)
252          */
253         $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
254         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
255         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
256         $otoken  = get_pconfig(local_user(), 'statusnet', 'oauthtoken'  );
257         $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
258         $enabled = get_pconfig(local_user(), 'statusnet', 'post');
259         $checked = (($enabled) ? ' checked="checked" ' : '');
260         $defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
261         $defchecked = (($defenabled) ? ' checked="checked" ' : '');
262         $linksenabled = get_pconfig(local_user(),'statusnet','post_taglinks');
263         $linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
264
265         $mirrorenabled = get_pconfig(local_user(),'statusnet','mirror_posts');
266         $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
267         $shorteningenabled = get_pconfig(local_user(),'statusnet','intelligent_shortening');
268         $shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : '');
269
270         $s .= '<div class="settings-block">';
271         $s .= '<h3>'. t('StatusNet Posting Settings').'</h3>';
272
273         if ( (!$ckey) && (!$csecret) ) {
274                 /***
275                  * no consumer keys
276                  */
277             $globalsn = get_config('statusnet', 'sites');
278             /***
279              * lets check if we have one or more globally configured StatusNet
280              * server OAuth credentials in the configuration. If so offer them
281              * with a little explanation to the user as choice - otherwise
282              * ignore this option entirely.
283              */
284             if (! $globalsn == null) {
285                 $s .= '<h4>' . t('Globally Available StatusNet OAuthKeys') . '</h4>';
286                 $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>';
287                 $s .= '<div id="statusnet-preconf-wrapper">';
288                 foreach ($globalsn as $asn) {
289                     $s .= '<input type="radio" name="statusnet-preconf-apiurl" value="'. $asn['apiurl'] .'">'. $asn['sitename'] .'<br />';
290                 }
291                 $s .= '<p></p><div class="clear"></div></div>';
292                 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
293             }
294             $s .= '<h4>' . t('Provide your own OAuth Credentials') . '</h4>';
295             $s .= '<p>'. t('No consumer key pair for StatusNet found. Register your Friendica 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 Friendica installation at your favorited StatusNet installation.') .'</p>';
296             $s .= '<div id="statusnet-consumer-wrapper">';
297             $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
298             $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" /><br />';
299             $s .= '<div class="clear"></div>';
300             $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
301             $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" /><br />';
302             $s .= '<div class="clear"></div>';
303             $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
304             $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" /><br />';
305             $s .= '<p></p><div class="clear"></div></div>';
306             $s .= '<label id="statusnet-applicationname-label" for="statusnet-applicationname">'.t('StatusNet application name').'</label>';
307             $s .= '<input id="statusnet-applicationname" type="text" name="statusnet-applicationname" size="35" /><br />';
308             $s .= '<p></p><div class="clear"></div></div>';
309             $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
310         } else {
311                 /***
312                  * ok we have a consumer key pair now look into the OAuth stuff
313                  */
314                 if ( (!$otoken) && (!$osecret) ) {
315                         /***
316                          * the user has not yet connected the account to statusnet
317                          * get a temporary OAuth key/secret pair and display a button with
318                          * which the user can request a PIN to connect the account to a
319                          * account at statusnet
320                          */
321                         $connection = new StatusNetOAuth($api, $ckey, $csecret);
322                         $request_token = $connection->getRequestToken('oob');
323                         $token = $request_token['oauth_token'];
324                         /***
325                          *  make some nice form
326                          */
327                         $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>';
328                         $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
329                         $s .= '<div id="statusnet-pin-wrapper">';
330                         $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
331                         $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
332                         $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
333                         $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
334                         $s .= '</div><div class="clear"></div>';
335                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
336                         $s .= '<h4>'.t('Cancel Connection Process').'</h4>';
337                         $s .= '<div id="statusnet-cancel-wrapper">';
338                         $s .= '<p>'.t('Current StatusNet API is').': '.$api.'</p>';
339                         $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">'. t('Cancel StatusNet Connection') . '</label>';
340                         $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
341                         $s .= '</div><div class="clear"></div>';
342                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
343                 } else {
344                         /***
345                          *  we have an OAuth key / secret pair for the user
346                          *  so let's give a chance to disable the postings to statusnet
347                          */
348                         $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
349                         $details = $connection->get('account/verify_credentials');
350                         $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>';
351                         $s .= '<p>'. t('If enabled all your <strong>public</strong> postings can be posted to the associated StatusNet account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') .'</p>';
352                         if ($a->user['hidewall']) {
353                             $s .= '<p>'. t('<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to StatusNet will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') .'</p>';
354                         }
355                         $s .= '<div id="statusnet-enable-wrapper">';
356                         $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Allow posting to StatusNet') .'</label>';
357                         $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
358                         $s .= '<div class="clear"></div>';
359                         $s .= '<label id="statusnet-default-label" for="statusnet-default">'. t('Send public postings to StatusNet by default') .'</label>';
360                         $s .= '<input id="statusnet-default" type="checkbox" name="statusnet-default" value="1" ' . $defchecked . '/>';
361                         $s .= '<div class="clear"></div>';
362
363                         $s .= '<label id="statusnet-mirror-label" for="statusnet-mirror">'.t('Mirror all posts from statusnet that are no replies or repeated messages').'</label>';
364                         $s .= '<input id="statusnet-mirror" type="checkbox" name="statusnet-mirror" value="1" '. $mirrorchecked . '/>';
365                         $s .= '<div class="clear"></div>';
366
367                         $s .= '<label id="statusnet-shortening-label" for="statusnet-shortening">'.t('Shortening method that optimizes the post').'</label>';
368                         $s .= '<input id="statusnet-shortening" type="checkbox" name="statusnet-shortening" value="1" '. $shorteningchecked . '/>';
369                         $s .= '<div class="clear"></div>';
370
371                         $s .= '<label id="statusnet-sendtaglinks-label" for="statusnet-sendtaglinks">'.t('Send linked #-tags and @-names to StatusNet').'</label>';
372                         $s .= '<input id="statusnet-sendtaglinks" type="checkbox" name="statusnet-sendtaglinks" value="1" '. $linkschecked . '/>';
373                         $s .= '</div><div class="clear"></div>';
374
375                         $s .= '<div id="statusnet-disconnect-wrapper">';
376                         $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
377                         $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
378                         $s .= '</div><div class="clear"></div>';
379                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
380                 }
381         }
382         $s .= '</div><div class="clear"></div></div>';
383 }
384
385
386 function statusnet_post_local(&$a,&$b) {
387         if($b['edit'])
388                 return;
389
390         if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) {
391
392                 $statusnet_post = get_pconfig(local_user(),'statusnet','post');
393                 $statusnet_enable = (($statusnet_post && x($_REQUEST,'statusnet_enable')) ? intval($_REQUEST['statusnet_enable']) : 0);
394
395                 // if API is used, default to the chosen settings
396                 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'statusnet','post_by_default')))
397                         $statusnet_enable = 1;
398
399        if(! $statusnet_enable)
400             return;
401
402        if(strlen($b['postopts']))
403            $b['postopts'] .= ',';
404        $b['postopts'] .= 'statusnet';
405     }
406 }
407
408 if (! function_exists( 'short_link' )) {
409 function short_link($url) {
410     require_once('library/slinky.php');
411     $slinky = new Slinky( $url );
412     $yourls_url = get_config('yourls','url1');
413     if ($yourls_url) {
414             $yourls_username = get_config('yourls','username1');
415             $yourls_password = get_config('yourls', 'password1');
416             $yourls_ssl = get_config('yourls', 'ssl1');
417             $yourls = new Slinky_YourLS();
418             $yourls->set( 'username', $yourls_username );
419             $yourls->set( 'password', $yourls_password );
420             $yourls->set( 'ssl', $yourls_ssl );
421             $yourls->set( 'yourls-url', $yourls_url );
422             $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
423     }
424     else {
425             // setup a cascade of shortening services
426             // try to get a short link from these services
427             // in the order ur1.ca, trim, id.gd, tinyurl
428             $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
429     }
430     return $slinky->short();
431 } };
432
433 function statusnet_shortenmsg($b, $max_char) {
434         require_once("include/bbcode.php");
435         require_once("include/html2plain.php");
436
437         // Looking for the first image
438         $image = '';
439         if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
440                 $image = $matches[3];
441
442         if ($image == '')
443                 if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
444                         $image = $matches[1];
445
446         $multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img"));
447
448         // When saved into the database the content is sent through htmlspecialchars
449         // That means that we have to decode all image-urls
450         $image = htmlspecialchars_decode($image);
451
452         $body = $b["body"];
453         if ($b["title"] != "")
454                 $body = $b["title"]."\n\n".$body;
455
456         if (strpos($body, "[bookmark") !== false) {
457                 // splitting the text in two parts:
458                 // before and after the bookmark
459                 $pos = strpos($body, "[bookmark");
460                 $body1 = substr($body, 0, $pos);
461                 $body2 = substr($body, $pos);
462
463                 // Removing all quotes after the bookmark
464                 // they are mostly only the content after the bookmark.
465                 $body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
466                 $body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
467                 $body = $body1.$body2;
468         }
469
470         // Add some newlines so that the message could be cut better
471         $body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
472                                 array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
473
474         // remove the recycle signs and the names since they aren't helpful on twitter
475         // recycle 1
476         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
477         $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
478         // recycle 2 (Test)
479         $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
480         $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
481
482         // remove the share element
483         $body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
484
485         // At first convert the text to html
486         $html = bbcode($body, false, false);
487
488         // Then convert it to plain text
489         //$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
490         $msg = trim(html2plain($html, 0, true));
491         $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
492
493         // Removing multiple newlines
494         while (strpos($msg, "\n\n\n") !== false)
495                 $msg = str_replace("\n\n\n", "\n\n", $msg);
496
497         // Removing multiple spaces
498         while (strpos($msg, "  ") !== false)
499                 $msg = str_replace("  ", " ", $msg);
500
501         // Removing URLs
502         $msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
503
504         $msg = trim($msg);
505
506         $link = '';
507         // look for bookmark-bbcode and handle it with priority
508         if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
509                 $link = $matches[1];
510
511         $multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
512
513         // If there is no bookmark element then take the first link
514         if ($link == '') {
515                 $links = collecturls($html);
516                 if (sizeof($links) > 0) {
517                         reset($links);
518                         $link = current($links);
519                 }
520                 $multiplelinks = (sizeof($links) > 1);
521         }
522
523         $msglink = "";
524         if ($multiplelinks)
525                 $msglink = $b["plink"];
526         else if ($link != "")
527                 $msglink = $link;
528         else if ($multipleimages)
529                 $msglink = $b["plink"];
530         else if ($image != "")
531                 $msglink = $image;
532
533         if (($msglink == "") and strlen($msg) > $max_char)
534                 $msglink = $b["plink"];
535
536         if (strlen($msglink) > 20)
537                 $msglink = short_link($msglink);
538
539         if (strlen(trim($msg." ".$msglink)) > $max_char) {
540                 $msg = substr($msg, 0, $max_char - (strlen($msglink)));
541                 $lastchar = substr($msg, -1);
542                 $msg = substr($msg, 0, -1);
543                 $pos = strrpos($msg, "\n");
544                 if ($pos > 0)
545                         $msg = substr($msg, 0, $pos);
546                 else if ($lastchar != "\n")
547                         $msg = substr($msg, 0, -3)."...";
548         }
549         $msg = str_replace("\n", " ", $msg);
550
551         // Removing multiple spaces - again
552         while (strpos($msg, "  ") !== false)
553                 $msg = str_replace("  ", " ", $msg);
554
555         return(array("msg"=>trim($msg." ".$msglink), "image"=>$image));
556 }
557
558 function statusnet_post_hook(&$a,&$b) {
559
560         /**
561          * Post to statusnet
562          */
563
564         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
565                 return;
566
567         if(! strstr($b['postopts'],'statusnet'))
568                 return;
569
570         // if posts comes from statusnet don't send it back
571         if($b['app'] == "StatusNet")
572                 return;
573
574         logger('statusnet post invoked');
575
576         load_pconfig($b['uid'], 'statusnet');
577
578         $api     = get_pconfig($b['uid'], 'statusnet', 'baseapi');
579         $ckey    = get_pconfig($b['uid'], 'statusnet', 'consumerkey');
580         $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret');
581         $otoken  = get_pconfig($b['uid'], 'statusnet', 'oauthtoken');
582         $osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret');
583         $intelligent_shortening = get_pconfig($b['uid'], 'statusnet', 'intelligent_shortening');
584
585         // Global setting overrides this
586         if (get_config('statusnet','intelligent_shortening'))
587                 $intelligent_shortening = get_config('statusnet','intelligent_shortening');
588
589         if($ckey && $csecret && $otoken && $osecret) {
590
591                 require_once('include/bbcode.php');
592                 $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
593                 $max_char = $dent->get_maxlength(); // max. length for a dent
594                 // we will only work with up to two times the length of the dent
595                 // we can later send to StatusNet. This way we can "gain" some
596                 // information during shortening of potential links but do not
597                 // shorten all the links in a 200000 character long essay.
598
599                 $tempfile = "";
600                 $intelligent_shortening = get_config('statusnet','intelligent_shortening');
601                 if (!$intelligent_shortening) {
602                         if (! $b['title']=='') {
603                                 $tmp = $b['title'].": \n".$b['body'];
604         //                    $tmp = substr($tmp, 0, 4*$max_char);
605                         } else {
606                             $tmp = $b['body']; // substr($b['body'], 0, 3*$max_char);
607                         }
608                         // if [url=bla][img]blub.png[/img][/url] get blub.png
609                         $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\]\[img\](\\w+.*?)\\[\\/img\]\\[\\/url\]/i', '$2', $tmp);
610                         // preserve links to images, videos and audios
611                         $tmp = preg_replace( '/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism', '$3', $tmp);
612                         $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $tmp);
613                         $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp);
614                         $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp);
615                         $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp);
616                         $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp);
617                         $linksenabled = get_pconfig($b['uid'],'statusnet','post_taglinks');
618                         // if a #tag is linked, don't send the [url] over to SN
619                         // that is, don't send if the option is not set in the 
620                         // connector settings
621                         if ($linksenabled=='0') {
622                                 // #-tags
623                                 $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp);
624                                 // @-mentions
625                                 $tmp = preg_replace( '/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $tmp);
626                                 // recycle 1
627                                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
628                                 $tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
629                                 // recycle 2 (test)
630                                 $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
631                                 $tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
632                         }
633                         // preserve links to webpages
634                         $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp);
635                         $tmp = preg_replace( '/\[bookmark\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/bookmark\]/i', '$2 $1', $tmp);
636                         // find all http or https links in the body of the entry and 
637                         // apply the shortener if the link is longer then 20 characters 
638                         if (( strlen($tmp)>$max_char ) && ( $max_char > 0 )) {
639                             preg_match_all ( '/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', $tmp, $allurls  );
640                             foreach ($allurls as $url) {
641                                 foreach ($url as $u) {
642                                     if (strlen($u)>20) {
643                                         $sl = short_link($u);
644                                         $tmp = str_replace( $u, $sl, $tmp );
645                                     }
646                                 }
647                             }
648                         }
649                         // ok, all the links we want to send out are save, now strip 
650                         // away the remaining bbcode
651                         //$msg = strip_tags(bbcode($tmp, false, false));
652                         $msg = bbcode($tmp, false, false);
653                         $msg = str_replace(array('<br>','<br />'),"\n",$msg);
654                         $msg = strip_tags($msg);
655
656                         // quotes not working - let's try this
657                         $msg = html_entity_decode($msg);
658
659                         if (( strlen($msg) > $max_char) && $max_char > 0) {
660                                 $shortlink = short_link( $b['plink'] );
661                                 // the new message will be shortened such that "... $shortlink"
662                                 // will fit into the character limit
663                                 $msg = nl2br(substr($msg, 0, $max_char-strlen($shortlink)-4));
664                                 $msg = str_replace(array('<br>','<br />'),' ',$msg);
665                                 $e = explode(' ', $msg);
666                                 //  remove the last word from the cut down message to 
667                                 //  avoid sending cut words to the MicroBlog
668                                 array_pop($e);
669                                 $msg = implode(' ', $e);
670                                 $msg .= '... ' . $shortlink;
671                         }
672
673                         $msg = trim($msg);
674                         $postdata = array('status' => $msg);
675                 } else {
676                         $msgarr = statusnet_shortenmsg($b, $max_char);
677                         $msg = $msgarr["msg"];
678                         $image = $msgarr["image"];
679                         if ($image != "") {
680                                 $imagedata = file_get_contents($image);
681                                 $tempfile = tempnam(get_config("system","temppath"), "upload");
682                                 file_put_contents($tempfile, $imagedata);
683                                 $postdata = array("status"=>$msg, "media"=>"@".$tempfile);
684                         } else
685                                 $postdata = array("status"=>$msg);
686                 }
687
688                 // and now dent it :-)
689                 if(strlen($msg)) {
690                     //$result = $dent->post('statuses/update', array('status' => $msg));
691                     $result = $dent->post('statuses/update', $postdata);
692                     logger('statusnet_post send, result: ' . print_r($result, true).
693                            "\nmessage: ".$msg, LOGGER_DEBUG."\nOriginal post: ".print_r($b, true)."\nPost Data: ".print_r($postdata, true));
694                     if ($result->error) {
695                         logger('Send to StatusNet failed: "' . $result->error . '"');
696                     }
697                 }
698                 if ($tempfile != "")
699                         unlink($tempfile);
700         }
701 }
702
703 function statusnet_plugin_admin_post(&$a){
704         
705         $sites = array();
706         
707         foreach($_POST['sitename'] as $id=>$sitename){
708                 $sitename=trim($sitename);
709                 $apiurl=trim($_POST['apiurl'][$id]);
710                 $secret=trim($_POST['secret'][$id]);
711                 $key=trim($_POST['key'][$id]);
712                 $applicationname = ((x($_POST, 'applicationname')) ? notags(trim($_POST['applicationname'][$id])):'');
713                 if ($sitename!="" &&
714                         $apiurl!="" &&
715                         $secret!="" &&
716                         $key!="" &&
717                         !x($_POST['delete'][$id])){
718                                 
719                                 $sites[] = Array(
720                                         'sitename' => $sitename,
721                                         'apiurl' => $apiurl,
722                                         'consumersecret' => $secret,
723                                         'consumerkey' => $key,
724                                         'applicationname' => $applicationname
725                                 );
726                 }
727         }
728         
729         $sites = set_config('statusnet','sites', $sites);
730         
731 }
732
733 function statusnet_plugin_admin(&$a, &$o){
734
735         $sites = get_config('statusnet','sites');
736         $sitesform=array();
737         if (is_array($sites)){
738                 foreach($sites as $id=>$s){
739                         $sitesform[] = Array(
740                                 'sitename' => Array("sitename[$id]", "Site name", $s['sitename'], ""),
741                                 'apiurl' => Array("apiurl[$id]", "Api url", $s['apiurl'], ""),
742                                 'secret' => Array("secret[$id]", "Secret", $s['consumersecret'], ""),
743                                 'key' => Array("key[$id]", "Key", $s['consumerkey'], ""),
744                                 'applicationname' => Array("applicationname[$id]", "Application name", $s['applicationname'], ""),
745                                 'delete' => Array("delete[$id]", "Delete", False , "Check to delete this preset"),
746                         );
747                 }
748         }
749         /* empty form to add new site */
750         $id++;
751         $sitesform[] = Array(
752                 'sitename' => Array("sitename[$id]", t("Site name"), "", ""),
753                 'apiurl' => Array("apiurl[$id]", t("API URL"), "", ""),
754                 'secret' => Array("secret[$id]", t("Consumer Secret"), "", ""),
755                 'key' => Array("key[$id]", t("Consumer Key"), "", ""),
756                 'applicationname' => Array("applicationname[$id]", t("Application name"), "", ""),
757         );
758
759         $t = get_markup_template( "admin.tpl", "addon/statusnet/" );
760         $o = replace_macros($t, array(
761                 '$submit' => t('Submit'),
762                 '$sites' => $sitesform,
763         ));
764 }
765
766 function statusnet_cron($a,$b) {
767         $last = get_config('statusnet','last_poll');
768
769         $poll_interval = intval(get_config('statusnet','poll_interval'));
770         if(! $poll_interval)
771                 $poll_interval = STATUSNET_DEFAULT_POLL_INTERVAL;
772
773         if($last) {
774                 $next = $last + ($poll_interval * 60);
775                 if($next > time()) {
776                         logger('statusnet: poll intervall not reached');
777                         return;
778                 }
779         }
780         logger('statusnet: cron_start');
781
782         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'statusnet' AND `k` = 'mirror_posts' AND `v` = '1' ORDER BY RAND() ");
783         if(count($r)) {
784                 foreach($r as $rr) {
785                         logger('statusnet: fetching for user '.$rr['uid']);
786                         statusnet_fetchtimeline($a, $rr['uid']);
787                 }
788         }
789
790         logger('statusnet: cron_end');
791
792         set_config('statusnet','last_poll', time());
793 }
794
795 function statusnet_fetchtimeline($a, $uid) {
796         $ckey    = get_pconfig($uid, 'statusnet', 'consumerkey');
797         $csecret = get_pconfig($uid, 'statusnet', 'consumersecret');
798         $api     = get_pconfig($uid, 'statusnet', 'baseapi');
799         $otoken  = get_pconfig($uid, 'statusnet', 'oauthtoken');
800         $osecret = get_pconfig($uid, 'statusnet', 'oauthsecret');
801         $lastid  = get_pconfig($uid, 'statusnet', 'lastid');
802
803         //  get the application name for the SN app
804         //  1st try personal config, then system config and fallback to the 
805         //  hostname of the node if neither one is set. 
806         $application_name  = get_pconfig( $uid, 'statusnet', 'application_name');
807         if ($application_name == "")
808                 $application_name  = get_config('statusnet', 'application_name');
809         if ($application_name == "")
810                 $application_name = $a->get_hostname();
811
812         $connection = new StatusNetOAuth($api, $ckey,$csecret,$otoken,$osecret);
813
814         $parameters = array("exclude_replies" => true, "trim_user" => true, "contributor_details" => false, "include_rts" => false);
815
816         $first_time = ($lastid == "");
817
818         if ($lastid <> "")
819                 $parameters["since_id"] = $lastid;
820
821         $items = $connection->get('statuses/user_timeline', $parameters);
822         $posts = array_reverse($items);
823
824         if (count($posts)) {
825             foreach ($posts as $post) {
826                 if ($post->id > $lastid)
827                         $lastid = $post->id;
828
829                 if ($first_time)
830                         continue;
831
832                 if (is_object($post->retweeted_status))
833                         continue;
834
835                 if ($post->in_reply_to_status_id != "")
836                         continue;
837
838                 if (!strpos($post->source, $application_name)) {
839                         $_SESSION["authenticated"] = true;
840                         $_SESSION["uid"] = $uid;
841
842                         $_REQUEST["type"] = "wall";
843                         $_REQUEST["api_source"] = true;
844                         $_REQUEST["profile_uid"] = $uid;
845                         $_REQUEST["source"] = "StatusNet";
846
847                         //$_REQUEST["date"] = $post->created_at;
848
849                         $_REQUEST["body"] = $post->text;
850                         if (is_string($post->place->name))
851                                 $_REQUEST["location"] = $post->place->name;
852
853                         if (is_string($post->place->full_name))
854                                 $_REQUEST["location"] = $post->place->full_name;
855
856                         if (is_array($post->geo->coordinates))
857                                 $_REQUEST["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
858
859                         if (is_array($post->coordinates->coordinates))
860                                 $_REQUEST["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
861
862                         //print_r($_REQUEST);
863                         if ($_REQUEST["body"] != "") {
864                                 logger('statusnet: posting for user '.$uid);
865
866                                 require_once('mod/item.php');
867                                 item_post($a);
868                         }
869                 }
870             }
871         }
872         set_pconfig($uid, 'statusnet', 'lastid', $lastid);
873 }
874