]> git.mxchange.org Git - friendica-addons.git/blob - statusnet/statusnet.php
rename 'friendika'
[friendica-addons.git] / statusnet / statusnet.php
1 <?php
2 /**
3  * Name: StatusNet Connector
4  * Version: 1.0.2
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     logger('Proxy SN: '.$prx);
67     if(strlen($prx)) {
68         curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL, 1);
69         curl_setopt($ci, CURLOPT_PROXY, $prx);
70         $prxusr = get_config('system','proxyuser');
71         if(strlen($prxusr))
72             curl_setopt($ci, CURLOPT_PROXYUSERPWD, $prxusr);
73     }
74     curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
75     curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
76     curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
77     curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
78     curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
79     curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
80     curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
81     curl_setopt($ci, CURLOPT_HEADER, FALSE);
82
83     switch ($method) {
84       case 'POST':
85         curl_setopt($ci, CURLOPT_POST, TRUE);
86         if (!empty($postfields)) {
87           curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
88         }
89         break;
90       case 'DELETE':
91         curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
92         if (!empty($postfields)) {
93           $url = "{$url}?{$postfields}";
94         }
95     }
96
97     curl_setopt($ci, CURLOPT_URL, $url);
98     $response = curl_exec($ci);
99     $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
100     $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
101     $this->url = $url;
102     curl_close ($ci);
103     return $response;
104   }
105 }
106
107 function statusnet_install() {
108         //  we need some hooks, for the configuration and for sending tweets
109         register_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
110         register_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
111         register_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
112         register_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
113         register_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
114         logger("installed statusnet");
115 }
116
117
118 function statusnet_uninstall() {
119         unregister_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
120         unregister_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
121         unregister_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
122         unregister_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
123         unregister_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
124
125         // old setting - remove only
126         unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
127         unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); 
128         unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
129
130 }
131
132 function statusnet_jot_nets(&$a,&$b) {
133         if(! local_user())
134                 return;
135
136         $statusnet_post = get_pconfig(local_user(),'statusnet','post');
137         if(intval($statusnet_post) == 1) {
138                 $statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default');
139                 $selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : '');
140                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="statusnet_enable"' . $selected . ' value="1" /> ' 
141                         . t('Post to StatusNet') . '</div>';    
142         }
143 }
144
145
146
147
148 function statusnet_settings_post ($a,$post) {
149         if(! local_user())
150             return;
151         // don't check statusnet settings if statusnet submit button is not clicked
152         if (!x($_POST,'statusnet-submit')) return;
153         
154         if (isset($_POST['statusnet-disconnect'])) {
155             /***
156              * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
157              * TODO can we revoke the access tokens at Twitter and do we need to do so?
158              */
159             del_pconfig( local_user(), 'statusnet', 'consumerkey'  );
160             del_pconfig( local_user(), 'statusnet', 'consumersecret' );
161             del_pconfig( local_user(), 'statusnet', 'post' );
162             del_pconfig( local_user(), 'statusnet', 'post_by_default' );
163             del_pconfig( local_user(), 'statusnet', 'oauthtoken' );
164             del_pconfig( local_user(), 'statusnet', 'oauthsecret' );
165             del_pconfig( local_user(), 'statusnet', 'baseapi' );
166         } else {
167             if (isset($_POST['statusnet-preconf-apiurl'])) {
168                 /***
169                  * If the user used one of the preconfigured StatusNet server credentials
170                  * use them. All the data are available in the global config.
171                  * Check the API Url never the less and blame the admin if it's not working ^^
172                  */
173                 $globalsn = get_config('statusnet', 'sites');
174                 foreach ( $globalsn as $asn) {
175                     if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl'] ) {
176                         $apibase = $asn['apiurl'];
177                         $c = fetch_url( $apibase . 'statusnet/version.xml' );
178                         if (strlen($c) > 0) {
179                             set_pconfig(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey'] );
180                             set_pconfig(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret'] );
181                             set_pconfig(local_user(), 'statusnet', 'baseapi', $asn['apiurl'] );
182                         } else {
183                             notice( t('Please contact your site administrator.<br />The provided API URL is not valid.').EOL.$asn['apiurl'].EOL );
184                         }
185                     }
186                 }
187                 goaway($a->get_baseurl().'/settings/connectors');
188             } else {
189             if (isset($_POST['statusnet-consumersecret'])) {
190                 //  check if we can reach the API of the StatusNet server
191                 //  we'll check the API Version for that, if we don't get one we'll try to fix the path but will
192                 //  resign quickly after this one try to fix the path ;-)
193                 $apibase = $_POST['statusnet-baseapi'];
194                 $c = fetch_url( $apibase . 'statusnet/version.xml' );
195                 if (strlen($c) > 0) {
196                     //  ok the API path is 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                     //  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 Twitter, let the magic of OAuth happen
218                         logger('got a StatusNet security code');
219                     $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
220                                         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
221                                         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
222                                         //  the token and secret for which the PIN was generated were hidden in the settings
223                                         //  form as token and token2, we need a new connection to Twitter using these token
224                                         //  and secret to request a Access Token with the PIN
225                                         $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
226                                         $token   = $connection->getAccessToken( $_POST['statusnet-pin'] );
227                                         //  ok, now that we have the Access Token, save them in the user config
228                                         set_pconfig(local_user(),'statusnet', 'oauthtoken',  $token['oauth_token']);
229                                         set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
230                     set_pconfig(local_user(),'statusnet', 'post', 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 tweet 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                                         info( t('StatusNet settings updated.') . EOL);
239                 }}}}
240 }
241 function statusnet_settings(&$a,&$s) {
242         if(! local_user())
243                 return;
244         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
245         /***
246          * 1) Check that we have a base api url and a consumer key & secret
247          * 2) If no OAuthtoken & stuff is present, generate button to get some
248          *    allow the user to cancel the connection process at this step
249          * 3) Checkbox for "Send public notices (respect size limitation)
250          */
251         $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
252         $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey' );
253         $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' );
254         $otoken  = get_pconfig(local_user(), 'statusnet', 'oauthtoken'  );
255         $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' );
256         $enabled = get_pconfig(local_user(), 'statusnet', 'post');
257         $checked = (($enabled) ? ' checked="checked" ' : '');
258         $defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
259         $defchecked = (($defenabled) ? ' checked="checked" ' : '');
260         $s .= '<div class="settings-block">';
261         $s .= '<h3>'. t('StatusNet Posting Settings').'</h3>';
262
263         if ( (!$ckey) && (!$csecret) ) {
264                 /***
265                  * no consumer keys
266                  */
267             $globalsn = get_config('statusnet', 'sites');
268             /***
269              * lets check if we have one or more globally configured StatusNet
270              * server OAuth credentials in the configuration. If so offer them
271              * with a little explanation to the user as choice - otherwise
272              * ignore this option entirely.
273              */
274             if (! $globalsn == null) {
275                 $s .= '<h4>' . t('Globally Available StatusNet OAuthKeys') . '</h4>';
276                 $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>';
277                 $s .= '<div id="statusnet-preconf-wrapper">';
278                 foreach ($globalsn as $asn) {
279                     $s .= '<input type="radio" name="statusnet-preconf-apiurl" value="'. $asn['apiurl'] .'">'. $asn['sitename'] .'<br />';
280                 }
281                 $s .= '<p></p><div class="clear"></div></div>';
282                 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
283             }
284             $s .= '<h4>' . t('Provide your own OAuth Credentials') . '</h4>';
285             $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>';
286             $s .= '<div id="statusnet-consumer-wrapper">';
287             $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">'. t('OAuth Consumer Key') .'</label>';
288             $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" /><br />';
289             $s .= '<div class="clear"></div>';
290             $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">'. t('OAuth Consumer Secret') .'</label>';
291             $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" /><br />';
292             $s .= '<div class="clear"></div>';
293             $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</label>';
294             $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" /><br />';
295             $s .= '<p></p><div class="clear"></div></div>';
296             $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
297         } else {
298                 /***
299                  * ok we have a consumer key pair now look into the OAuth stuff
300                  */
301                 if ( (!$otoken) && (!$osecret) ) {
302                         /***
303                          * the user has not yet connected the account to statusnet
304                          * get a temporary OAuth key/secret pair and display a button with
305                          * which the user can request a PIN to connect the account to a
306                          * account at statusnet
307                          */
308                         $connection = new StatusNetOAuth($api, $ckey, $csecret);
309                         $request_token = $connection->getRequestToken('oob');
310                         $token = $request_token['oauth_token'];
311                         /***
312                          *  make some nice form
313                          */
314                         $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>';
315                         $s .= '<a href="'.$connection->getAuthorizeURL($token,False).'" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="'. t('Log in with StatusNet') .'"></a>';
316                         $s .= '<div id="statusnet-pin-wrapper">';
317                         $s .= '<label id="statusnet-pin-label" for="statusnet-pin">'. t('Copy the security code from StatusNet here') .'</label>';
318                         $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
319                         $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="'.$token.'" />';
320                         $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="'.$request_token['oauth_token_secret'].'" />';
321                         $s .= '</div><div class="clear"></div>';
322                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
323                         $s .= '<h4>'.t('Cancel Connection Process').'</h4>';
324                         $s .= '<div id="statusnet-cancel-wrapper">';
325                         $s .= '<p>'.t('Current StatusNet API is').': '.$api.'</p>';
326                         $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">'. t('Cancel StatusNet Connection') . '</label>';
327                         $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
328                         $s .= '</div><div class="clear"></div>';
329                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
330                 } else {
331                         /***
332                          *  we have an OAuth key / secret pair for the user
333                          *  so let's give a chance to disable the postings to statusnet
334                          */
335                         $connection = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
336                         $details = $connection->get('account/verify_credentials');
337                         $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>';
338                         $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>';
339                         $s .= '<div id="statusnet-enable-wrapper">';
340                         $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">'. t('Allow posting to StatusNet') .'</label>';
341                         $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
342                         $s .= '<div class="clear"></div>';
343                         $s .= '<label id="statusnet-default-label" for="statusnet-default">'. t('Send public postings to StatusNet by default') .'</label>';
344                         $s .= '<input id="statusnet-default" type="checkbox" name="statusnet-default" value="1" ' . $defchecked . '/>';
345                         $s .= '</div><div class="clear"></div>';
346
347                         $s .= '<div id="statusnet-disconnect-wrapper">';
348                         $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
349                         $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
350                         $s .= '</div><div class="clear"></div>';
351                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
352                 }
353         }
354         $s .= '</div><div class="clear"></div></div>';
355 }
356
357
358 function statusnet_post_local(&$a,&$b) {
359         if($b['edit'])
360                 return;
361
362         if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) {
363
364                 $statusnet_post = get_pconfig(local_user(),'statusnet','post');
365                 $statusnet_enable = (($statusnet_post && x($_REQUEST,'statusnet_enable')) ? intval($_REQUEST['statusnet_enable']) : 0);
366
367                 // if API is used, default to the chosen settings
368                 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'statusnet','post_by_default')))
369                         $statusnet_enable = 1;
370
371        if(! $statusnet_enable)
372             return;
373
374        if(strlen($b['postopts']))
375            $b['postopts'] .= ',';
376        $b['postopts'] .= 'statusnet';
377     }
378 }
379
380 function statusnet_post_hook(&$a,&$b) {
381
382         /**
383          * Post to statusnet
384          */
385
386         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
387                 return;
388
389         if(! strstr($b['postopts'],'statusnet'))
390                 return;
391
392         logger('StatusNet post invoked');
393
394
395         load_pconfig($b['uid'], 'statusnet');
396             
397         $api     = get_pconfig($b['uid'], 'statusnet', 'baseapi');
398         $ckey    = get_pconfig($b['uid'], 'statusnet', 'consumerkey'  );
399         $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret' );
400         $otoken  = get_pconfig($b['uid'], 'statusnet', 'oauthtoken'  );
401         $osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret' );
402
403         if($ckey && $csecret && $otoken && $osecret) {
404
405                 require_once('include/bbcode.php');     
406                 $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
407                 $max_char = $dent->get_maxlength(); // max. length for a dent
408                 $msg = strip_tags(bbcode($b['body']));
409                 // quotes not working - let's try this
410                 $msg = html_entity_decode($msg);
411                 if ( strlen($msg) > $max_char) {
412                         $shortlink = "";
413                         require_once('library/slinky.php');
414                         $slinky = new Slinky( $b['plink'] );
415                         // setup a cascade of shortening services
416                         // try to get a short link from these services
417                         // in the order ur1.ca, trim, id.gd, tinyurl
418                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
419                         $shortlink = $slinky->short();
420                         // the new message will be shortened such that "... $shortlink"
421                         // will fit into the character limit
422                         $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);
423             $msg .= '... ' . $shortlink;
424                 }
425         // and now tweet it :-)
426                 if(strlen($msg))
427                         $dent->post('statuses/update', array('status' => $msg));
428         }
429 }
430
431 function statusnet_plugin_admin_post(&$a){
432         
433         $sites = array();
434         
435         foreach($_POST['sitename'] as $id=>$sitename){
436                 $sitename=trim($sitename);
437                 $apiurl=trim($_POST['apiurl'][$id]);
438                 $secret=trim($_POST['secret'][$id]);
439                 $key=trim($_POST['key'][$id]);
440                 if ($sitename!="" &&
441                         $apiurl!="" &&
442                         $secret!="" &&
443                         $key!="" &&
444                         !x($_POST['delete'][$id])){
445                                 
446                                 $sites[] = Array(
447                                         'sitename' => $sitename,
448                                         'apiurl' => $apiurl,
449                                         'consumersecret' => $secret,
450                                         'consumerkey' => $key
451                                 );
452                 }
453         }
454         
455         $sites = set_config('statusnet','sites', $sites);
456         
457 }
458
459 function statusnet_plugin_admin(&$a, &$o){
460
461         $sites = get_config('statusnet','sites');
462         $sitesform=array();
463         if (is_array($sites)){
464                 foreach($sites as $id=>$s){
465                         $sitesform[] = Array(
466                                 'sitename' => Array("sitename[$id]", "Site name", $s['sitename'], ""),
467                                 'apiurl' => Array("apiurl[$id]", "Api url", $s['apiurl'], ""),
468                                 'secret' => Array("secret[$id]", "Secret", $s['consumersecret'], ""),
469                                 'key' => Array("key[$id]", "Key", $s['consumerkey'], ""),
470                                 'delete' => Array("delete[$id]", "Delete", False , "Check to delete this preset"),
471                         );
472                 }
473         }
474         /* empty form to add new site */
475         $id++;
476         $sitesform[] = Array(
477                 'sitename' => Array("sitename[$id]", t("Site name"), "", ""),
478                 'apiurl' => Array("apiurl[$id]", t("API URL"), "", ""),
479                 'secret' => Array("secret[$id]", t("Consumer Secret"), "", ""),
480                 'key' => Array("key[$id]", t("Consumer Key"), "", ""),
481         );
482
483         
484         $t = file_get_contents( dirname(__file__). "/admin.tpl" );
485         $o = replace_macros($t, array(
486                 '$submit' => t('Submit'),
487                                                         
488                 '$sites' => $sitesform,
489                 
490         ));
491         
492         
493 }