Broke some stuff out into functions. Ran it through phpcs.

This commit is contained in:
Zach Copley 2009-08-08 18:13:19 +00:00
parent e3bb64cd6c
commit df12206421

View File

@ -19,7 +19,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category TwitterauthorizationAction * @category Twitter
* @package Laconica * @package Laconica
* @author Zach Copely <zach@controlyourself.ca> * @author Zach Copely <zach@controlyourself.ca>
* @copyright 2009 Control Yourself, Inc. * @copyright 2009 Control Yourself, Inc.
@ -31,9 +31,31 @@ if (!defined('LACONICA')) {
exit(1); exit(1);
} }
/**
* Class for doing OAuth authentication against Twitter
*
* Peforms the OAuth "dance" between Laconica and Twitter -- requests a token,
* authorizes it, and exchanges it for an access token. It also creates a link
* (Foreign_link) between the Laconica user and Twitter user and stores the
* access token and secret in the link.
*
* @category Twitter
* @package Laconica
* @author Zach Copley <zach@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*
*/
class TwitterauthorizationAction extends Action class TwitterauthorizationAction extends Action
{ {
/**
* Initialize class members. Looks for 'oauth_token' parameter.
*
* @param array $args misc. arguments
*
* @return boolean true
*/
function prepare($args) function prepare($args)
{ {
parent::prepare($args); parent::prepare($args);
@ -43,6 +65,13 @@ class TwitterauthorizationAction extends Action
return true; return true;
} }
/**
* Handler method
*
* @param array $args is ignored since it's now passed in in prepare()
*
* @return nothing
*/
function handle($args) function handle($args)
{ {
parent::handle($args); parent::handle($args);
@ -66,7 +95,20 @@ class TwitterauthorizationAction extends Action
// process // process
if (empty($this->oauth_token)) { if (empty($this->oauth_token)) {
$this->authorizeRequestToken();
} else {
$this->saveAccessToken();
}
}
/**
* Asks Twitter for a request token, and then redirects to Twitter
* to authorize it.
*
* @return nothing
*/
function authorizeRequestToken()
{
try { try {
// Get a new request token and authorize it // Get a new request token and authorize it
@ -88,8 +130,16 @@ class TwitterauthorizationAction extends Action
} }
common_redirect($auth_link); common_redirect($auth_link);
}
} else { /**
* Called when Twitter returns an authorized request token. Exchanges
* it for an access token and stores it.
*
* @return nothing
*/
function saveAccessToken()
{
// Check to make sure Twitter returned the same request // Check to make sure Twitter returned the same request
// token we sent them // token we sent them
@ -107,18 +157,40 @@ class TwitterauthorizationAction extends Action
$atok = $client->getAccessToken(); $atok = $client->getAccessToken();
// Save the access token and Twitter user info // Test the access token and get the user's Twitter info
$client = new TwitterOAuthClient($atok->key, $atok->secret); $client = new TwitterOAuthClient($atok->key, $atok->secret);
$twitter_user = $client->verify_credentials(); $twitter_user = $client->verify_credentials();
} catch (OAuthClientException $e) { } catch (OAuthClientException $e) {
$msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s', $msg = sprintf('OAuth client cURL error - code: %1$s, msg: %2$s',
$e->getCode(), $e->getMessage()); $e->getCode(), $e->getMessage());
$this->serverError(_('Couldn\'t link your Twitter account.')); $this->serverError(_('Couldn\'t link your Twitter account.'));
} }
// Save the access token and Twitter user info
$this->saveForeignLink($atok, $twitter_user);
// Clean up the the mess we made in the session
unset($_SESSION['twitter_request_token']);
unset($_SESSION['twitter_request_token_secret']);
common_redirect(common_local_url('twittersettings'));
}
/**
* 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(); $user = common_current_user();
$flink = new Foreign_link(); $flink = new Foreign_link();
@ -126,10 +198,12 @@ class TwitterauthorizationAction extends Action
$flink->user_id = $user->id; $flink->user_id = $user->id;
$flink->foreign_id = $twitter_user->id; $flink->foreign_id = $twitter_user->id;
$flink->service = TWITTER_SERVICE; $flink->service = TWITTER_SERVICE;
$flink->token = $atok->key; $flink->token = $access_token->key;
$flink->credentials = $atok->secret; $flink->credentials = $access_token->secret;
$flink->created = common_sql_now(); $flink->created = common_sql_now();
// Defaults: noticesync on, everything else off
$flink->set_flags(true, false, false, false); $flink->set_flags(true, false, false, false);
$flink_id = $flink->insert(); $flink_id = $flink->insert();
@ -140,14 +214,6 @@ class TwitterauthorizationAction extends Action
} }
save_twitter_user($twitter_user->id, $twitter_user->screen_name); save_twitter_user($twitter_user->id, $twitter_user->screen_name);
// clean up the the mess we made in the session
unset($_SESSION['twitter_request_token']);
unset($_SESSION['twitter_request_token_secret']);
common_redirect(common_local_url('twittersettings'));
}
} }
} }