diff options
author | Evan Prodromou <evan@controlyourself.ca> | 2009-08-24 16:55:49 -0400 |
---|---|---|
committer | Evan Prodromou <evan@controlyourself.ca> | 2009-08-24 16:55:49 -0400 |
commit | ff87732053bae38879988ba7d002a294998ccb4e (patch) | |
tree | ccccfe7fc8976baf1f0cd4438a57bf817dc4052c /actions/twitterauthorization.php | |
parent | 27aeba01dd366f15f7847267b7518fb873987ddb (diff) | |
parent | f3cdc7f272e409d391979d3e6c58dd63573530c0 (diff) |
Merge branch '0.8.x' into testing
Conflicts:
actions/twitterauthorization.php
lib/oauthclient.php
lib/twitter.php
lib/twitterapi.php
lib/twitteroauthclient.php
scripts/twitterstatusfetcher.php
Diffstat (limited to 'actions/twitterauthorization.php')
-rw-r--r-- | actions/twitterauthorization.php | 153 |
1 files changed, 100 insertions, 53 deletions
diff --git a/actions/twitterauthorization.php b/actions/twitterauthorization.php index 2390034cd..866e3a1e7 100644 --- a/actions/twitterauthorization.php +++ b/actions/twitterauthorization.php @@ -43,6 +43,13 @@ class TwitterauthorizationAction extends Action return true; } + /** + * Handler method + * + * @param array $args is ignored since it's now passed in in prepare() + * + * @return nothing + */ function handle($args) { parent::handle($args); @@ -51,7 +58,7 @@ class TwitterauthorizationAction extends Action $this->clientError(_('Not logged in.'), 403); } - $user = common_current_user(); + $user = common_current_user(); $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE); // If there's already a foreign link record, it means we already @@ -66,88 +73,128 @@ class TwitterauthorizationAction extends Action // process if (empty($this->oauth_token)) { + $this->authorizeRequestToken(); + } else { + $this->saveAccessToken(); + } + } - try { + /** + * Asks Twitter for a request token, and then redirects to Twitter + * to authorize it. + * + * @return nothing + */ + function authorizeRequestToken() + { + try { - // Get a new request token and authorize it + // Get a new request token and authorize it - $client = new TwitterOAuthClient(); - $req_tok = $client->getRequestToken(); + $client = new TwitterOAuthClient(); + $req_tok = + $client->getRequestToken(TwitterOAuthClient::$requestTokenURL); - // Sock the request token away in the session temporarily + // Sock the request token away in the session temporarily - $_SESSION['twitter_request_token'] = $req_tok->key; - $_SESSION['twitter_request_token_secret'] = $req_tok->key; + $_SESSION['twitter_request_token'] = $req_tok->key; + $_SESSION['twitter_request_token_secret'] = $req_tok->secret; - $auth_link = $client->getAuthorizeLink($req_tok); - - } catch (TwitterOAuthClientException $e) { - $msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s', - $e->getCode(), $e->getMessage()); - $this->serverError(_('Couldn\'t link your Twitter account.')); - } + $auth_link = $client->getAuthorizeLink($req_tok); - common_redirect($auth_link); + } catch (TwitterOAuthClientException $e) { + $msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s', + $e->getCode(), $e->getMessage()); + $this->serverError(_('Couldn\'t link your Twitter account.')); + } - } else { + common_redirect($auth_link); + } - // Check to make sure Twitter returned the same request - // token we sent them + /** + * Called when Twitter returns an authorized request token. Exchanges + * it for an access token and stores it. + * + * @return nothing + */ + function saveAccessToken() + { - if ($_SESSION['twitter_request_token'] != $this->oauth_token) { - $this->serverError(_('Couldn\'t link your Twitter account.')); - } + // Check to make sure Twitter returned the same request + // token we sent them - try { + if ($_SESSION['twitter_request_token'] != $this->oauth_token) { + $this->serverError(_('Couldn\'t link your Twitter account.')); + } - $client = new TwitterOAuthClient($_SESSION['twitter_request_token'], - $_SESSION['twitter_request_token_secret']); + try { - // Exchange the request token for an access token + $client = new TwitterOAuthClient($_SESSION['twitter_request_token'], + $_SESSION['twitter_request_token_secret']); - $atok = $client->getAccessToken(); + // Exchange the request token for an access token - // Save the access token and Twitter user info + $atok = $client->getAccessToken(TwitterOAuthClient::$accessTokenURL); - $client = new TwitterOAuthClient($atok->key, $atok->secret); + // Test the access token and get the user's Twitter info - $twitter_user = $client->verify_credentials(); + $client = new TwitterOAuthClient($atok->key, $atok->secret); + $twitter_user = $client->verifyCredentials(); - } catch (OAuthClientException $e) { - $msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s', + } catch (OAuthClientException $e) { + $msg = sprintf('OAuth client cURL error - code: %1$s, msg: %2$s', $e->getCode(), $e->getMessage()); - $this->serverError(_('Couldn\'t link your Twitter account.')); - } + $this->serverError(_('Couldn\'t link your Twitter account.')); + } - $user = common_current_user(); + // Save the access token and Twitter user info - $flink = new Foreign_link(); + $this->saveForeignLink($atok, $twitter_user); - $flink->user_id = $user->id; - $flink->foreign_id = $twitter_user->id; - $flink->service = TWITTER_SERVICE; - $flink->token = $atok->key; - $flink->credentials = $atok->secret; - $flink->created = common_sql_now(); + // Clean up the the mess we made in the session - $flink->set_flags(true, false, false, false); + unset($_SESSION['twitter_request_token']); + unset($_SESSION['twitter_request_token_secret']); - $flink_id = $flink->insert(); + common_redirect(common_local_url('twittersettings')); + } - if (empty($flink_id)) { - common_log_db_error($flink, 'INSERT', __FILE__); - $this->serverError(_('Couldn\'t link your Twitter account.')); - } + /** + * Saves a Foreign_link between Twitter user and local user, + * which includes the access token and secret. + * + * @param OAuthToken $access_token the access token to save + * @param mixed $twitter_user twitter API user object + * + * @return nothing + */ + function saveForeignLink($access_token, $twitter_user) + { + $user = common_current_user(); - save_twitter_user($twitter_user->id, $twitter_user->screen_name); + $flink = new Foreign_link(); - // clean up the the mess we made in the session + $flink->user_id = $user->id; + $flink->foreign_id = $twitter_user->id; + $flink->service = TWITTER_SERVICE; - unset($_SESSION['twitter_request_token']); - unset($_SESSION['twitter_request_token_secret']); + $creds = TwitterOAuthClient::packToken($access_token); - common_redirect(common_local_url('twittersettings')); + $flink->credentials = $creds; + $flink->created = common_sql_now(); + + // Defaults: noticesync on, everything else off + + $flink->set_flags(true, false, false, false); + + $flink_id = $flink->insert(); + + if (empty($flink_id)) { + common_log_db_error($flink, 'INSERT', __FILE__); + $this->serverError(_('Couldn\'t link your Twitter account.')); } + + save_twitter_user($twitter_user->id, $twitter_user->screen_name); } } |