]> git.mxchange.org Git - friendica-addons.git/blob - statusnet/statusnet.php
added more regex to SN addon to deal with tag links and linked images
[friendica-addons.git] / statusnet / statusnet.php
1 <?php
2 /**
3  * Name: StatusNet Connector
4  * Version: 1.0.3
5  * Author: Tobias Diekershoff <https://diekershoff.homeunix.net/friendika/profile/tobias>
6  */
7  
8 /*   StatusNet Plugin for Friendica
9  *
10  *   Author: Tobias Diekershoff
11  *           tobias.diekershoff@gmx.net
12  *
13  *   License:3-clause BSD license
14  *
15  *   Configuration:
16  *     To activate the plugin itself add it to the $a->config['system']['addon']
17  *     setting. After this, your user can configure their Twitter account settings
18  *     from "Settings -> Plugin Settings".
19  *
20  *     Requirements: PHP5, curl [Slinky library]
21  *
22  *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/StatusNet_Plugin
23  */
24
25 /***
26  * We have to alter the TwitterOAuth class a little bit to work with any StatusNet
27  * installation abroad. Basically it's only make the API path variable and be happy.
28  *
29  * Thank you guys for the Twitter compatible API!
30  */
31
32 require_once('library/twitteroauth.php');
33
34 class StatusNetOAuth extends TwitterOAuth {
35     function get_maxlength() {
36         $config = $this->get($this->host . 'statusnet/config.json');
37         return $config->site->textlimit;
38     }
39     function accessTokenURL()  { return $this->host.'oauth/access_token'; }
40     function authenticateURL() { return $this->host.'oauth/authenticate'; } 
41     function authorizeURL() { return $this->host.'oauth/authorize'; }
42     function requestTokenURL() { return $this->host.'oauth/request_token'; }
43     function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
44         parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
45         $this->host = $apipath;
46     }
47   /**
48    * Make an HTTP request
49    *
50    * @return API results
51    *
52    * Copied here from the twitteroauth library and complemented by applying the proxy settings of friendica
53    */
54   function http($url, $method, $postfields = NULL) {
55     $this->http_info = array();
56     $ci = curl_init();
57     /* Curl settings */
58     $prx = get_config('system','proxy');
59     if(strlen($prx)) {
60         curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL, 1);
61         curl_setopt($ci, CURLOPT_PROXY, $prx);
62         $prxusr = get_config('system','proxyuser');
63         if(strlen($prxusr))
64             curl_setopt($ci, CURLOPT_PROXYUSERPWD, $prxusr);
65     }
66     curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
67     curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
68     curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
69     curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
70     curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
71     curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
72     curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
73     curl_setopt($ci, CURLOPT_HEADER, FALSE);
74
75     switch ($method) {
76       case 'POST':
77         curl_setopt($ci, CURLOPT_POST, TRUE);
78         if (!empty($postfields)) {
79           curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
80         }
81         break;
82       case 'DELETE':
83         curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
84         if (!empty($postfields)) {
85           $url = "{$url}?{$postfields}";
86         }
87     }
88
89     curl_setopt($ci, CURLOPT_URL, $url);
90     $response = curl_exec($ci);
91     $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
92     $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
93     $this->url = $url;
94     curl_close ($ci);
95     return $response;
96   }
97 }
98
99 function statusnet_install() {
100         //  we need some hooks, for the configuration and for sending tweets
101         register_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
102         register_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
103         register_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
104         register_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
105         register_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
106         logger("installed statusnet");
107 }
108
109
110 function statusnet_uninstall() {
111         unregister_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
112         unregister_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
113         unregister_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
114         unregister_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
115         unregister_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
116
117         // old setting - remove only
118         unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
119         unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
120         unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
121
122 }
123
124 function statusnet_jot_nets(&$a,&$b) {
125         if(! local_user())
126                 return;
127
128         $statusnet_post = get_pconfig(local_user(),'statusnet','post');
129         if(intval($statusnet_post) == 1) {
130                 $statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default');
131                 $selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : '');
132                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="statusnet_enable"' . $selected . ' value="1" /> ' 
133                         . t('Post to StatusNet') . '</div>';    
134         }
135 }
136
137
138
139
140 function statusnet_settings_post ($a,$post) {
141         if(! local_user())
142             return;
143         // don't check statusnet settings if statusnet submit button is not clicked
144         if (!x($_POST,'statusnet-submit')) return;
145         
146         if (isset($_POST['statusnet-disconnect'])) {
147             /***
148              * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
149              */
150             del_pconfig( local_user(), 'statusnet', 'consumerkey'  );
151             del_pconfig( local_user(), 'statusnet', 'consumersecret' );
152             del_pconfig( local_user(), 'statusnet', 'post' );
153             del_pconfig( local_user(), 'statusnet', 'post_by_default' );
154             del_pconfig( local_user(), 'statusnet', 'oauthtoken' );
155             del_pconfig( local_user(), 'statusnet', 'oauthsecret' );
156             del_pconfig( local_user(), 'statusnet', 'baseapi' );
157         } else {
158             if (isset($_POST['statusnet-preconf-apiurl'])) {
159                 /***
160                  * If the user used one of the preconfigured StatusNet server credentials
161                  * use them. All the data are available in the global config.
162                  * Check the API Url never the less and blame the admin if it's not working ^^
163                  */
164                 $globalsn = get_config('statusnet', 'sites');
165                 foreach ( $globalsn as $asn) {
166                     if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl'] ) {
167                         $apibase = $asn['apiurl'];
168                         $c = fetch_url( $apibase . 'statusnet/version.xml' );
169                         if (strlen($c) > 0) {
170                             set_pconfig(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey'] );
171                             set_pconfig(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret'] );
172                             set_pconfig(local_user(), 'statusnet', 'baseapi', $asn['apiurl'] );
173                         } else {
174                             notice( t('Please contact your site administrator.<br />The provided API URL is not valid.').EOL.$asn['apiurl'].EOL );
175                         }
176                     }
177                 }
178                 goaway($a->get_baseurl().'/settings/connectors');
179             } else {
180             if (isset($_POST['statusnet-consumersecret'])) {
181                 //  check if we can reach the API of the StatusNet server
182                 //  we'll check the API Version for that, if we don't get one we'll try to fix the path but will
183                 //  resign quickly after this one try to fix the path ;-)
184                 $apibase = $_POST['statusnet-baseapi'];
185                 $c = fetch_url( $apibase . 'statusnet/version.xml' );
186                 if (strlen($c) > 0) {
187                     //  ok the API path is correct, let's save the settings
188                     set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
189                     set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
190                     set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
191                 } else {
192                     //  the API path is not correct, maybe missing trailing / ?
193                     $apibase = $apibase . '/';
194                     $c = fetch_url( $apibase . 'statusnet/version.xml' );
195                     if (strlen($c) > 0) {
196                         //  ok the API path is now correct, let's save the settings
197                         set_pconfig(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
198                         set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
199                         set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
200                     } else {
201                         //  still not the correct API base, let's do noting
202                         notice( t('We could not contact the StatusNet API with the Path you entered.').EOL );
203                     }
204                 }
205                 goaway($a->get_baseurl().'/settings/connectors');
206             } else {
207                 if (isset($_POST['statusnet-pin'])) {
208                         //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
209                     $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
210                                         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
211                                         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
212                                         //  the token and secret for which the PIN was generated were hidden in the settings
213                                         //  form as token and token2, we need a new connection to Twitter using these token
214                                         //  and secret to request a Access Token with the PIN
215                                         $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
216                                         $token   = $connection->getAccessToken( $_POST['statusnet-pin'] );
217                                         //  ok, now that we have the Access Token, save them in the user config
218                                         set_pconfig(local_user(),'statusnet', 'oauthtoken',  $token['oauth_token']);
219                                         set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
220                     set_pconfig(local_user(),'statusnet', 'post', 1);
221                     //  reload the Addon Settings page, if we don't do it see Bug #42
222                     goaway($a->get_baseurl().'/settings/connectors');
223                                 } else {
224                                         //  if no PIN is supplied in the POST variables, the user has changed the setting
225                                         //  to post a tweet for every new __public__ posting to the wall
226                                         set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
227                                         set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
228                                         info( t('StatusNet settings updated.') . EOL);
229                 }}}}
230 }
231 function statusnet_settings(&$a,&$s) {
232         if(! local_user())
233                 return;
234         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
235         /***
236          * 1) Check that we have a base api url and a consumer key & secret
237          * 2) If no OAuthtoken & stuff is present, generate button to get some
238          *    allow the user to cancel the connection process at this step
239          * 3) Checkbox for "Send public notices (respect size limitation)
240          */
241         $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
242         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
243         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
244         $otoken  = get_pconfig(local_user(), 'statusnet', 'oauthtoken'  );
245         $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
246         $enabled = get_pconfig(local_user(), 'statusnet', 'post');
247         $checked = (($enabled) ? ' checked="checked" ' : '');
248         $defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
249         $defchecked = (($defenabled) ? ' checked="checked" ' : '');
250         $s .= '<div class="settings-block">';
251         $s .= '<h3>'. t('StatusNet Posting Settings').'</h3>';
252
253         if ( (!$ckey) && (!$csecret) ) {
254                 /***
255                  * no consumer keys
256                  */
257             $globalsn = get_config('statusnet', 'sites');
258             /***
259              * lets check if we have one or more globally configured StatusNet
260              * server OAuth credentials in the configuration. If so offer them
261              * with a little explanation to the user as choice - otherwise
262              * ignore this option entirely.
263              */
264             if (! $globalsn == null) {
265                 $s .= '<h4>' . t('Globally Available StatusNet OAuthKeys') . '</h4>';
266                 $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>';
267                 $s .= '<div id="statusnet-preconf-wrapper">';
268                 foreach ($globalsn as $asn) {
269                     $s .= '<input type="radio" name="statusnet-preconf-apiurl" value="'. $asn['apiurl'] .'">'. $asn['sitename'] .'<br />';
270                 }
271                 $s .= '<p></p><div class="clear"></div></div>';
272                 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
273             }
274             $s .= '<h4>' . t('Provide your own OAuth Credentials') . '</h4>';
275             $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>';
276             $s .= '<div id="statusnet-consumer-wrapper">';
277             $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
278             $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" /><br />';
279             $s .= '<div class="clear"></div>';
280             $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
281             $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" /><br />';
282             $s .= '<div class="clear"></div>';
283             $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
284             $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" /><br />';
285             $s .= '<p></p><div class="clear"></div></div>';
286             $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
287         } else {
288                 /***
289                  * ok we have a consumer key pair now look into the OAuth stuff
290                  */
291                 if ( (!$otoken) && (!$osecret) ) {
292                         /***
293                          * the user has not yet connected the account to statusnet
294                          * get a temporary OAuth key/secret pair and display a button with
295                          * which the user can request a PIN to connect the account to a
296                          * account at statusnet
297                          */
298                         $connection = new StatusNetOAuth($api, $ckey, $csecret);
299                         $request_token = $connection->getRequestToken('oob');
300                         $token = $request_token['oauth_token'];
301                         /***
302                          *  make some nice form
303                          */
304                         $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>';
305                         $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
306                         $s .= '<div id="statusnet-pin-wrapper">';
307                         $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
308                         $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
309                         $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
310                         $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
311                         $s .= '</div><div class="clear"></div>';
312                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
313                         $s .= '<h4>'.t('Cancel Connection Process').'</h4>';
314                         $s .= '<div id="statusnet-cancel-wrapper">';
315                         $s .= '<p>'.t('Current StatusNet API is').': '.$api.'</p>';
316                         $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">'. t('Cancel StatusNet Connection') . '</label>';
317                         $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
318                         $s .= '</div><div class="clear"></div>';
319                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
320                 } else {
321                         /***
322                          *  we have an OAuth key / secret pair for the user
323                          *  so let's give a chance to disable the postings to statusnet
324                          */
325                         $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
326                         $details = $connection->get('account/verify_credentials');
327                         $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>';
328                         $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>';
329                         $s .= '<div id="statusnet-enable-wrapper">';
330                         $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Allow posting to StatusNet') .'</label>';
331                         $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
332                         $s .= '<div class="clear"></div>';
333                         $s .= '<label id="statusnet-default-label" for="statusnet-default">'. t('Send public postings to StatusNet by default') .'</label>';
334                         $s .= '<input id="statusnet-default" type="checkbox" name="statusnet-default" value="1" ' . $defchecked . '/>';
335                         $s .= '</div><div class="clear"></div>';
336
337                         $s .= '<div id="statusnet-disconnect-wrapper">';
338                         $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
339                         $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
340                         $s .= '</div><div class="clear"></div>';
341                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
342                 }
343         }
344         $s .= '</div><div class="clear"></div></div>';
345 }
346
347
348 function statusnet_post_local(&$a,&$b) {
349         if($b['edit'])
350                 return;
351
352         if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) {
353
354                 $statusnet_post = get_pconfig(local_user(),'statusnet','post');
355                 $statusnet_enable = (($statusnet_post && x($_REQUEST,'statusnet_enable')) ? intval($_REQUEST['statusnet_enable']) : 0);
356
357                 // if API is used, default to the chosen settings
358                 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'statusnet','post_by_default')))
359                         $statusnet_enable = 1;
360
361        if(! $statusnet_enable)
362             return;
363
364        if(strlen($b['postopts']))
365            $b['postopts'] .= ',';
366        $b['postopts'] .= 'statusnet';
367     }
368 }
369
370 function statusnet_post_hook(&$a,&$b) {
371
372         /**
373          * Post to statusnet
374          */
375
376         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
377                 return;
378
379         if(! strstr($b['postopts'],'statusnet'))
380                 return;
381
382         load_pconfig($b['uid'], 'statusnet');
383             
384         $api     = get_pconfig($b['uid'], 'statusnet', 'baseapi');
385         $ckey    = get_pconfig($b['uid'], 'statusnet', 'consumerkey'  );
386         $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret' );
387         $otoken  = get_pconfig($b['uid'], 'statusnet', 'oauthtoken'  );
388         $osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret' );
389
390         if($ckey && $csecret && $otoken && $osecret) {
391
392                 require_once('include/bbcode.php');     
393                 $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
394                 $max_char = $dent->get_maxlength(); // max. length for a dent
395                 // if [url=bla][img]blub.png[/img][/url] get blub.png
396                 $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $tmp);
397                 // preserve links to images, videos and audios
398                 $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']);
399                 $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp);
400                 $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp);
401                 $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp);
402                 $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp);
403                 // if a #tag is linked, don't send the [url] over to SN
404                 $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp);
405                 // preserve links to webpages
406                 $tmp = preg_replace( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp);
407                 // TODO apply the shortener to the URLs in the releyed dent
408                 logger($tmp);
409                 $msg = strip_tags(bbcode($tmp));
410                 // quotes not working - let's try this
411                 $msg = html_entity_decode($msg);
412                 if (( strlen($msg) > $max_char) && $max_char > 0) {
413                         $shortlink = "";
414                         require_once('library/slinky.php');
415                         $slinky = new Slinky( $b['plink'] );
416                         $yourls_url = get_config('yourls','url1');
417                         if ($yourls_url) {
418                                 $yourls_username = get_config('yourls','username1');
419                                 $yourls_password = get_config('yourls', 'password1');
420                                 $yourls_ssl = get_config('yourls', 'ssl1');
421                                 $yourls = new Slinky_YourLS();
422                                 $yourls->set( 'username', $yourls_username );
423                                 $yourls->set( 'password', $yourls_password );
424                                 $yourls->set( 'ssl', $yourls_ssl );
425                                 $yourls->set( 'yourls-url', $yourls_url );
426                                 $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
427                         }
428                         else {
429                                 // setup a cascade of shortening services
430                                 // try to get a short link from these services
431                                 // in the order ur1.ca, trim, id.gd, tinyurl
432                                 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
433                         }
434                         $shortlink = $slinky->short();
435                         // the new message will be shortened such that "... $shortlink"
436                         // will fit into the character limit
437                         $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
438                         $msg .= '... ' . $shortlink;
439                 }
440                 // and now tweet it :-)
441                 if(strlen($msg))
442                         $dent->post('statuses/update', array('status' => $msg));
443         }
444 }
445
446 function statusnet_plugin_admin_post(&$a){
447         
448         $sites = array();
449         
450         foreach($_POST['sitename'] as $id=>$sitename){
451                 $sitename=trim($sitename);
452                 $apiurl=trim($_POST['apiurl'][$id]);
453                 $secret=trim($_POST['secret'][$id]);
454                 $key=trim($_POST['key'][$id]);
455                 if ($sitename!="" &&
456                         $apiurl!="" &&
457                         $secret!="" &&
458                         $key!="" &&
459                         !x($_POST['delete'][$id])){
460                                 
461                                 $sites[] = Array(
462                                         'sitename' => $sitename,
463                                         'apiurl' => $apiurl,
464                                         'consumersecret' => $secret,
465                                         'consumerkey' => $key
466                                 );
467                 }
468         }
469         
470         $sites = set_config('statusnet','sites', $sites);
471         
472 }
473
474 function statusnet_plugin_admin(&$a, &$o){
475
476         $sites = get_config('statusnet','sites');
477         $sitesform=array();
478         if (is_array($sites)){
479                 foreach($sites as $id=>$s){
480                         $sitesform[] = Array(
481                                 'sitename' => Array("sitename[$id]", "Site name", $s['sitename'], ""),
482                                 'apiurl' => Array("apiurl[$id]", "Api url", $s['apiurl'], ""),
483                                 'secret' => Array("secret[$id]", "Secret", $s['consumersecret'], ""),
484                                 'key' => Array("key[$id]", "Key", $s['consumerkey'], ""),
485                                 'delete' => Array("delete[$id]", "Delete", False , "Check to delete this preset"),
486                         );
487                 }
488         }
489         /* empty form to add new site */
490         $id++;
491         $sitesform[] = Array(
492                 'sitename' => Array("sitename[$id]", t("Site name"), "", ""),
493                 'apiurl' => Array("apiurl[$id]", t("API URL"), "", ""),
494                 'secret' => Array("secret[$id]", t("Consumer Secret"), "", ""),
495                 'key' => Array("key[$id]", t("Consumer Key"), "", ""),
496         );
497
498         
499         $t = file_get_contents( dirname(__file__). "/admin.tpl" );
500         $o = replace_macros($t, array(
501                 '$submit' => t('Submit'),
502                                                         
503                 '$sites' => $sitesform,
504                 
505         ));
506         
507         
508 }