+function twitter_settings_post ($a,$post) {
+ if(! local_user())
+ return;
+ // don't check twitter settings if twitter submit button is not clicked
+ if (!x($_POST,'twitter-submit')) return;
+
+ if (isset($_POST['twitter-disconnect'])) {
+ /***
+ * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
+ * from the user configuration
+ * TODO can we revoke the access tokens at Twitter and do we need to do so?
+ */
+ del_pconfig( local_user(), 'twitter', 'consumerkey' );
+ del_pconfig( local_user(), 'twitter', 'consumersecret' );
+ del_pconfig( local_user(), 'twitter', 'post' );
+ } else {
+ if (isset($_POST['twitter-pin'])) {
+ // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
+ logger('got a Twitter PIN');
+ require_once('library/twitteroauth.php');
+ $ckey = get_config('twitter', 'consumerkey' );
+ $csecret = get_config('twitter', 'consumersecret' );
+ // the token and secret for which the PIN was generated were hidden in the settings
+ // form as token and token2, we need a new connection to Twitter using these token
+ // and secret to request a Access Token with the PIN
+ $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
+ $token = $connection->getAccessToken( $_POST['twitter-pin'] );
+ // ok, now that we have the Access Token, save them in the user config
+ set_pconfig(local_user(),'twitter', 'oauthtoken', $token['oauth_token']);
+ set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
+ set_pconfig(local_user(),'twitter', 'post', 1);
+ // reload the Addon Settings page, if we don't do it see Bug #42
+ header('Location: '.$a->get_baseurl().'/settings/addon');
+ } else {
+ // if no PIN is supplied in the POST variables, the user has changed the setting
+ // to post a tweet for every new __public__ posting to the wall
+ set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
+ }}
+}
+function twitter_settings(&$a,&$s) {
+ if(! local_user())
+ return;
+ $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
+ /***
+ * 1) Check that we have global consumer key & secret
+ * 2) If no OAuthtoken & stuff is present, generate button to get some
+ * 3) Checkbox for "Send public notices (140 chars only)
+ */
+ $ckey = get_config('twitter', 'consumerkey' );
+ $csecret = get_config('twitter', 'consumersecret' );
+ $otoken = get_pconfig(local_user(), 'twitter', 'oauthtoken' );
+ $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
+ $enabled = get_pconfig(local_user(), 'twitter', 'post');
+ $checked = (($enabled) ? ' checked="checked" ' : '');
+ $s .= '<div class="settings-block">';
+ $s .= '<h3>'. t('Twitter Posting Settings') .'</h3>';