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