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