Moved some stuff around. More comments and phpcs compliance.

This commit is contained in:
Zach Copley 2009-08-10 06:05:43 +00:00
parent df12206421
commit fa8433308f
6 changed files with 263 additions and 45 deletions

View File

@ -48,7 +48,6 @@ if (!defined('LACONICA')) {
*/
class TwitterauthorizationAction extends Action
{
/**
* Initialize class members. Looks for 'oauth_token' parameter.
*
@ -114,12 +113,13 @@ class TwitterauthorizationAction extends Action
// Get a new request token and authorize it
$client = new TwitterOAuthClient();
$req_tok = $client->getRequestToken();
$req_tok =
$client->getRequestToken(TwitterOAuthClient::$requestTokenURL);
// 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_secret'] = $req_tok->secret;
$auth_link = $client->getAuthorizeLink($req_tok);
@ -155,12 +155,12 @@ class TwitterauthorizationAction extends Action
// Exchange the request token for an access token
$atok = $client->getAccessToken();
$atok = $client->getAccessToken(TwitterOAuthClient::$accessTokenURL);
// Test the access token and get the user's Twitter info
$client = new TwitterOAuthClient($atok->key, $atok->secret);
$twitter_user = $client->verify_credentials();
$twitter_user = $client->verifyCredentials();
} catch (OAuthClientException $e) {
$msg = sprintf('OAuth client cURL error - code: %1$s, msg: %2$s',

View File

@ -1,54 +1,154 @@
<?php
/**
* Laconica, the distributed open-source microblogging tool
*
* Base class for doing OAuth calls as a consumer
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*
* @category Action
* @package Laconica
* @author Zach Copley <zach@controlyourself.ca>
* @copyright 2008 Control Yourself, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*/
require_once('OAuth.php');
if (!defined('LACONICA')) {
exit(1);
}
class OAuthClientCurlException extends Exception { }
require_once 'OAuth.php';
/**
* Exception wrapper for cURL errors
*
* @category Integration
* @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 OAuthClientCurlException extends Exception
{
}
/**
* Base class for doing OAuth calls as a consumer
*
* @category Integration
* @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 OAuthClient
{
var $consumer;
var $token;
/**
* Constructor
*
* Can be initialized with just consumer key and secret for requesting new
* tokens or with additional request token or access token
*
* @param string $consumer_key consumer key
* @param string $consumer_secret consumer secret
* @param string $oauth_token user's token
* @param string $oauth_token_secret user's secret
*
* @return nothing
*/
function __construct($consumer_key, $consumer_secret,
$oauth_token = null, $oauth_token_secret = null)
{
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$this->token = null;
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$this->token = null;
if (isset($oauth_token) && isset($oauth_token_secret)) {
$this->token = new OAuthToken($oauth_token, $oauth_token_secret);
}
}
function getRequestToken()
/**
* Gets a request token from the given url
*
* @param string $url OAuth endpoint for grabbing request tokens
*
* @return OAuthToken $token the request token
*/
function getRequestToken($url)
{
$response = $this->oAuthGet(TwitterOAuthClient::$requestTokenURL);
$response = $this->oAuthGet($url);
parse_str($response);
$token = new OAuthToken($oauth_token, $oauth_token_secret);
return $token;
}
function getAuthorizeLink($request_token, $oauth_callback = null)
/**
* Builds a link that can be redirected to in order to
* authorize a request token.
*
* @param string $url endpoint for authorizing request tokens
* @param OAuthToken $request_token the request token to be authorized
* @param string $oauth_callback optional callback url
*
* @return string $authorize_url the url to redirect to
*/
function getAuthorizeLink($url, $request_token, $oauth_callback = null)
{
$url = TwitterOAuthClient::$authorizeURL . '?oauth_token=' .
$authorize_url = $url . '?oauth_token=' .
$request_token->key;
if (isset($oauth_callback)) {
$url .= '&oauth_callback=' . urlencode($oauth_callback);
$authorize_url .= '&oauth_callback=' . urlencode($oauth_callback);
}
return $url;
common_debug("$authorize_url");
return $authorize_url;
}
function getAccessToken()
/**
* Fetches an access token
*
* @param string $url OAuth endpoint for exchanging authorized request tokens
* for access tokens
*
* @return OAuthToken $token the access token
*/
function getAccessToken($url)
{
$response = $this->oAuthPost(TwitterOAuthClient::$accessTokenURL);
$response = $this->oAuthPost($url);
parse_str($response);
$token = new OAuthToken($oauth_token, $oauth_token_secret);
return $token;
}
/**
* Use HTTP GET to make a signed OAuth request
*
* @param string $url OAuth endpoint
*
* @return mixed the request
*/
function oAuthGet($url)
{
$request = OAuthRequest::from_consumer_and_token($this->consumer,
@ -59,6 +159,14 @@ class OAuthClient
return $this->httpRequest($request->to_url());
}
/**
* Use HTTP POST to make a signed OAuth request
*
* @param string $url OAuth endpoint
* @param array $params additional post parameters
*
* @return mixed the request
*/
function oAuthPost($url, $params = null)
{
$request = OAuthRequest::from_consumer_and_token($this->consumer,
@ -70,6 +178,14 @@ class OAuthClient
$request->to_postdata());
}
/**
* Make a HTTP request using cURL.
*
* @param string $url Where to make the
* @param array $params post parameters
*
* @return mixed the request
*/
function httpRequest($url, $params = null)
{
$options = array(
@ -89,7 +205,7 @@ class OAuthClient
);
if (isset($params)) {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $params;
}

View File

@ -165,7 +165,7 @@ function broadcast_twitter($notice)
$status = null;
try {
$status = $client->statuses_update($statustxt);
$status = $client->statusesUpdate($statustxt);
} catch (OAuthClientCurlException $e) {
if ($e->getMessage() == 'The requested URL returned error: 401') {

View File

@ -1,11 +1,60 @@
<?php
/**
* Laconica, the distributed open-source microblogging tool
*
* Class for doing OAuth calls against Twitter
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*
* @category Integration
* @package Laconica
* @author Zach Copley <zach@controlyourself.ca>
* @copyright 2008 Control Yourself, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*/
if (!defined('LACONICA')) {
exit(1);
}
/**
* Class for talking to the Twitter API with OAuth.
*
* @category Integration
* @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 TwitterOAuthClient extends OAuthClient
{
public static $requestTokenURL = 'https://twitter.com/oauth/request_token';
public static $authorizeURL = 'https://twitter.com/oauth/authorize';
public static $accessTokenURL = 'https://twitter.com/oauth/access_token';
/**
* Constructor
*
* @param string $oauth_token the user's token
* @param string $oauth_token_secret the user's token secret
*
* @return nothing
*/
function __construct($oauth_token = null, $oauth_token_secret = null)
{
$consumer_key = common_config('twitter', 'consumer_key');
@ -15,39 +64,72 @@ class TwitterOAuthClient extends OAuthClient
$oauth_token, $oauth_token_secret);
}
function getAuthorizeLink($request_token) {
return parent::getAuthorizeLink($request_token,
/**
* Builds a link to Twitter's endpoint for authorizing a request token
*
* @param OAuthToken $request_token token to authorize
*
* @return the link
*/
function getAuthorizeLink($request_token)
{
return parent::getAuthorizeLink(self::$authorizeURL,
$request_token,
common_local_url('twitterauthorization'));
}
function verify_credentials()
/**
* Calls Twitter's /account/verify_credentials API method
*
* @return mixed the Twitter user
*/
function verifyCredentials()
{
$url = 'https://twitter.com/account/verify_credentials.json';
$response = $this->oAuthGet($url);
$url = 'https://twitter.com/account/verify_credentials.json';
$response = $this->oAuthGet($url);
$twitter_user = json_decode($response);
return $twitter_user;
}
function statuses_update($status, $in_reply_to_status_id = null)
/**
* Calls Twitter's /stutuses/update API method
*
* @param string $status text of the status
* @param int $in_reply_to_status_id optional id of the status it's
* a reply to
*
* @return mixed the status
*/
function statusesUpdate($status, $in_reply_to_status_id = null)
{
$url = 'https://twitter.com/statuses/update.json';
$params = array('status' => $status,
$url = 'https://twitter.com/statuses/update.json';
$params = array('status' => $status,
'in_reply_to_status_id' => $in_reply_to_status_id);
$response = $this->oAuthPost($url, $params);
$status = json_decode($response);
$status = json_decode($response);
return $status;
}
function statuses_friends_timeline($since_id = null, $max_id = null,
$cnt = null, $page = null) {
/**
* Calls Twitter's /stutuses/friends_timeline API method
*
* @param int $since_id show statuses after this id
* @param int $max_id show statuses before this id
* @param int $cnt number of statuses to show
* @param int $page page number
*
* @return mixed an array of statuses
*/
function statusesFriendsTimeline($since_id = null, $max_id = null,
$cnt = null, $page = null)
{
$url = 'https://twitter.com/statuses/friends_timeline.json';
$url = 'https://twitter.com/statuses/friends_timeline.json';
$params = array('since_id' => $since_id,
'max_id' => $max_id,
'count' => $cnt,
'page' => $page);
$qry = http_build_query($params);
$qry = http_build_query($params);
if (!empty($qry)) {
$url .= "?$qry";
@ -58,8 +140,18 @@ class TwitterOAuthClient extends OAuthClient
return $statuses;
}
function statuses_friends($id = null, $user_id = null, $screen_name = null,
$page = null)
/**
* Calls Twitter's /stutuses/friends API method
*
* @param int $id id of the user whom you wish to see friends of
* @param int $user_id numerical user id
* @param int $screen_name screen name
* @param int $page page number
*
* @return mixed an array of twitter users and their latest status
*/
function statusesFriends($id = null, $user_id = null, $screen_name = null,
$page = null)
{
$url = "https://twitter.com/statuses/friends.json";
@ -67,18 +159,28 @@ class TwitterOAuthClient extends OAuthClient
'user_id' => $user_id,
'screen_name' => $screen_name,
'page' => $page);
$qry = http_build_query($params);
$qry = http_build_query($params);
if (!empty($qry)) {
$url .= "?$qry";
}
$response = $this->oAuthGet($url);
$ids = json_decode($response);
return $ids;
$friends = json_decode($response);
return $friends;
}
function friends_ids($id = null, $user_id = null, $screen_name = null,
/**
* Calls Twitter's /stutuses/friends/ids API method
*
* @param int $id id of the user whom you wish to see friends of
* @param int $user_id numerical user id
* @param int $screen_name screen name
* @param int $page page number
*
* @return mixed a list of ids, 100 per page
*/
function friendsIds($id = null, $user_id = null, $screen_name = null,
$page = null)
{
$url = "https://twitter.com/friends/ids.json";
@ -87,14 +189,14 @@ class TwitterOAuthClient extends OAuthClient
'user_id' => $user_id,
'screen_name' => $screen_name,
'page' => $page);
$qry = http_build_query($params);
$qry = http_build_query($params);
if (!empty($qry)) {
$url .= "?$qry";
}
$response = $this->oAuthGet($url);
$ids = json_decode($response);
$ids = json_decode($response);
return $ids;
}

View File

@ -145,7 +145,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
$client = new TwitterOAuthClient($flink->token, $flink->credentials);
try {
$friends_ids = $client->friends_ids();
$friends_ids = $client->friendsIds();
} catch (OAuthCurlException $e) {
common_log(LOG_WARNING, $this->name() .
' - cURL error getting friend ids ' .
@ -174,7 +174,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
for ($i = 1; $i <= $pages; $i++) {
try {
$more_friends = $client->statuses_friends(null, null, null, $i);
$more_friends = $client->statusesFriends(null, null, null, $i);
} catch (OAuthCurlException $e) {
common_log(LOG_WARNING, $this->name() .
' - cURL error getting Twitter statuses/friends ' .

View File

@ -166,7 +166,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
$timeline = null;
try {
$timeline = $client->statuses_friends_timeline();
$timeline = $client->statusesFriendsTimeline();
} catch (OAuthClientCurlException $e) {
common_log(LOG_WARNING, $this->name() .
' - OAuth client unable to get friends timeline for user ' .
@ -175,7 +175,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
}
if (empty($timeline)) {
common_log(LOG_WARNING, $this->name . " - Empty timeline.");
common_log(LOG_WARNING, $this->name() . " - Empty timeline.");
return;
}