Merge branch 'twitter-basic-auth' into 0.8.x
This commit is contained in:
commit
ca30cc2d59
|
@ -22,7 +22,7 @@
|
|||
* @category Action
|
||||
* @package StatusNet
|
||||
* @author Zach Copley <zach@status.net>
|
||||
* @copyright 2008 StatusNet, Inc.
|
||||
* @copyright 2009 StatusNet, Inc.
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*/
|
||||
|
|
175
lib/twitter.php
175
lib/twitter.php
|
@ -154,80 +154,143 @@ function broadcast_twitter($notice)
|
|||
TWITTER_SERVICE);
|
||||
|
||||
if (is_twitter_bound($notice, $flink)) {
|
||||
|
||||
$user = $flink->getUser();
|
||||
|
||||
// XXX: Hack to get around PHP cURL's use of @ being a a meta character
|
||||
$statustxt = preg_replace('/^@/', ' @', $notice->content);
|
||||
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
|
||||
$status = null;
|
||||
|
||||
try {
|
||||
$status = $client->statusesUpdate($statustxt);
|
||||
} catch (OAuthClientCurlException $e) {
|
||||
|
||||
if ($e->getMessage() == 'The requested URL returned error: 401') {
|
||||
|
||||
$errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
|
||||
'Twitter OAuth access token.',
|
||||
$user->nickname, $user->id);
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
// Bad auth token! We need to delete the foreign_link
|
||||
// to Twitter and inform the user.
|
||||
|
||||
remove_twitter_link($flink);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
// Some other error happened, so we should probably
|
||||
// try to send again later.
|
||||
|
||||
$errmsg = sprintf('cURL error trying to send notice to Twitter ' .
|
||||
'for user %1$s (user id: %2$s) - ' .
|
||||
'code: %3$s message: $4$s.',
|
||||
$user->nickname, $user->id,
|
||||
$e->getCode(), $e->getMessage());
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
return false;
|
||||
}
|
||||
if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
|
||||
return broadcast_oauth($notice, $flink);
|
||||
} else {
|
||||
return broadcast_basicauth($notice, $flink);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($status)) {
|
||||
function broadcast_oauth($notice, $flink) {
|
||||
|
||||
// This could represent a failure posting,
|
||||
// or the Twitter API might just be behaving flakey.
|
||||
$user = $flink->getUser();
|
||||
$statustxt = format_status($notice);
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
$status = null;
|
||||
|
||||
$errmsg = sprint('No data returned by Twitter API when ' .
|
||||
'trying to send update for %1$s (user id %2$s).',
|
||||
$user->nickname, $user->id);
|
||||
try {
|
||||
$status = $client->statusesUpdate($statustxt);
|
||||
} catch (OAuthClientCurlException $e) {
|
||||
|
||||
if ($e->getMessage() == 'The requested URL returned error: 401') {
|
||||
|
||||
$errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
|
||||
'Twitter OAuth access token.',
|
||||
$user->nickname, $user->id);
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
// Bad auth token! We need to delete the foreign_link
|
||||
// to Twitter and inform the user.
|
||||
|
||||
remove_twitter_link($flink);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
// Some other error happened, so we should probably
|
||||
// try to send again later.
|
||||
|
||||
$errmsg = sprintf('cURL error trying to send notice to Twitter ' .
|
||||
'for user %1$s (user id: %2$s) - ' .
|
||||
'code: %3$s message: $4$s.',
|
||||
$user->nickname, $user->id,
|
||||
$e->getCode(), $e->getMessage());
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Notice crossed the great divide
|
||||
|
||||
$msg = sprintf('Twitter bridge posted notice %s to Twitter.',
|
||||
$notice->id);
|
||||
common_log(LOG_INFO, $msg);
|
||||
}
|
||||
|
||||
if (empty($status)) {
|
||||
|
||||
// This could represent a failure posting,
|
||||
// or the Twitter API might just be behaving flakey.
|
||||
|
||||
$errmsg = sprintf('No data returned by Twitter API when ' .
|
||||
'trying to send update for %1$s (user id %2$s).',
|
||||
$user->nickname, $user->id);
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Notice crossed the great divide
|
||||
|
||||
$msg = sprintf('Twitter bridge posted notice %s to Twitter using OAuth.',
|
||||
$notice->id);
|
||||
common_log(LOG_INFO, $msg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function broadcast_basicauth($notice, $flink)
|
||||
{
|
||||
$user = $flink->getUser();
|
||||
|
||||
$statustxt = format_status($notice);
|
||||
|
||||
$client = new TwitterBasicAuthClient($flink);
|
||||
$status = null;
|
||||
|
||||
try {
|
||||
$status = $client->statusesUpdate($statustxt);
|
||||
} catch (BasicAuthCurlException $e) {
|
||||
|
||||
if ($e->getMessage() == 'The requested URL returned error: 401') {
|
||||
|
||||
$errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
|
||||
'Twitter screen_name/password combo.',
|
||||
$user->nickname, $user->id);
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
remove_twitter_link($flink);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
$errmsg = sprintf('cURL error trying to send notice to Twitter ' .
|
||||
'for user %1$s (user id: %2$s) - ' .
|
||||
'code: %3$s message: $4$s.',
|
||||
$user->nickname, $user->id,
|
||||
$e->getCode(), $e->getMessage());
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($status)) {
|
||||
|
||||
$errmsg = sprintf('No data returned by Twitter API when ' .
|
||||
'trying to send update for %1$s (user id %2$s).',
|
||||
$user->nickname, $user->id);
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$msg = sprintf('Twitter bridge posted notice %s to Twitter using basic auth.',
|
||||
$notice->id);
|
||||
common_log(LOG_INFO, $msg);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
function format_status($notice)
|
||||
{
|
||||
// XXX: Hack to get around PHP cURL's use of @ being a a meta character
|
||||
return preg_replace('/^@/', ' @', $notice->content);
|
||||
}
|
||||
|
||||
function remove_twitter_link($flink)
|
||||
{
|
||||
$user = $flink->getUser();
|
||||
|
||||
common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
|
||||
"user $user->nickname (user id: $user->id).");
|
||||
"user $user->nickname (user id: $user->id).");
|
||||
|
||||
$result = $flink->delete();
|
||||
|
||||
|
|
236
lib/twitterbasicauthclient.php
Normal file
236
lib/twitterbasicauthclient.php
Normal file
|
@ -0,0 +1,236 @@
|
|||
<?php
|
||||
/**
|
||||
* StatusNet, 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 StatusNet
|
||||
* @author Zach Copley <zach@status.net>
|
||||
* @copyright 2009 StatusNet, Inc.
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*/
|
||||
|
||||
if (!defined('STATUSNET') && !defined('LACONICA')) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception wrapper for cURL errors
|
||||
*
|
||||
* @category Integration
|
||||
* @package StatusNet
|
||||
* @author Zach Copley <zach@status.net>
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*
|
||||
*/
|
||||
class BasicAuthCurlException extends Exception
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for talking to the Twitter API with HTTP Basic Auth.
|
||||
*
|
||||
* @category Integration
|
||||
* @package StatusNet
|
||||
* @author Zach Copley <zach@status.net>
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*
|
||||
*/
|
||||
class TwitterBasicAuthClient
|
||||
{
|
||||
var $screen_name = null;
|
||||
var $password = null;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param Foreign_link $flink a Foreign_link storing the
|
||||
* Twitter user's password, etc.
|
||||
*/
|
||||
function __construct($flink)
|
||||
{
|
||||
$fuser = $flink->getForeignUser();
|
||||
$this->screen_name = $fuser->nickname;
|
||||
$this->password = $flink->credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
'source' => common_config('integration', 'source'),
|
||||
'in_reply_to_status_id' => $in_reply_to_status_id);
|
||||
$response = $this->httpRequest($url, $params);
|
||||
$status = json_decode($response);
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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';
|
||||
$params = array('since_id' => $since_id,
|
||||
'max_id' => $max_id,
|
||||
'count' => $cnt,
|
||||
'page' => $page);
|
||||
$qry = http_build_query($params);
|
||||
|
||||
if (!empty($qry)) {
|
||||
$url .= "?$qry";
|
||||
}
|
||||
|
||||
$response = $this->httpRequest($url);
|
||||
$statuses = json_decode($response);
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
$params = array('id' => $id,
|
||||
'user_id' => $user_id,
|
||||
'screen_name' => $screen_name,
|
||||
'page' => $page);
|
||||
$qry = http_build_query($params);
|
||||
|
||||
if (!empty($qry)) {
|
||||
$url .= "?$qry";
|
||||
}
|
||||
|
||||
$response = $this->httpRequest($url);
|
||||
$friends = json_decode($response);
|
||||
return $friends;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
$params = array('id' => $id,
|
||||
'user_id' => $user_id,
|
||||
'screen_name' => $screen_name,
|
||||
'page' => $page);
|
||||
$qry = http_build_query($params);
|
||||
|
||||
if (!empty($qry)) {
|
||||
$url .= "?$qry";
|
||||
}
|
||||
|
||||
$response = $this->httpRequest($url);
|
||||
$ids = json_decode($response);
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a HTTP request using cURL.
|
||||
*
|
||||
* @param string $url Where to make the request
|
||||
* @param array $params post parameters
|
||||
*
|
||||
* @return mixed the request
|
||||
*/
|
||||
function httpRequest($url, $params = null, $auth = true)
|
||||
{
|
||||
$options = array(
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FAILONERROR => true,
|
||||
CURLOPT_HEADER => false,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_USERAGENT => 'StatusNet',
|
||||
CURLOPT_CONNECTTIMEOUT => 120,
|
||||
CURLOPT_TIMEOUT => 120,
|
||||
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
|
||||
// Twitter is strict about accepting invalid "Expect" headers
|
||||
|
||||
CURLOPT_HTTPHEADER => array('Expect:')
|
||||
);
|
||||
|
||||
if (isset($params)) {
|
||||
$options[CURLOPT_POST] = true;
|
||||
$options[CURLOPT_POSTFIELDS] = $params;
|
||||
}
|
||||
|
||||
if ($auth) {
|
||||
$options[CURLOPT_USERPWD] = $this->screen_name .
|
||||
':' . $this->password;
|
||||
}
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, $options);
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if ($response === false) {
|
||||
$msg = curl_error($ch);
|
||||
$code = curl_errno($ch);
|
||||
throw new BasicAuthCurlException($msg, $code);
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
|
@ -22,7 +22,7 @@
|
|||
* @category Integration
|
||||
* @package StatusNet
|
||||
* @author Zach Copley <zach@status.net>
|
||||
* @copyright 2008 StatusNet, Inc.
|
||||
* @copyright 2009 StatusNet, Inc.
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*/
|
||||
|
@ -81,6 +81,15 @@ class TwitterOAuthClient extends OAuthClient
|
|||
return new OAuthToken($vals[0], $vals[1]);
|
||||
}
|
||||
|
||||
static function isPackedToken($str)
|
||||
{
|
||||
if (strpos($str, chr(0)) === false) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a link to Twitter's endpoint for authorizing a request token
|
||||
*
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
*/
|
||||
|
||||
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
|
||||
define('STATUSNET', true);
|
||||
define('LACONICA', true); // compatibility
|
||||
|
||||
$shortoptions = 'di::';
|
||||
$longoptions = array('id::', 'debug');
|
||||
|
@ -142,13 +144,20 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
|
|||
{
|
||||
$friends = array();
|
||||
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
$client = null;
|
||||
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
common_debug($this->name() . '- Grabbing friends IDs with OAuth.');
|
||||
} else {
|
||||
$client = new TwitterBasicAuthClient($flink);
|
||||
common_debug($this->name() . '- Grabbing friends IDs with basic auth.');
|
||||
}
|
||||
|
||||
try {
|
||||
$friends_ids = $client->friendsIds();
|
||||
} catch (OAuthCurlException $e) {
|
||||
} catch (Exception $e) {
|
||||
common_log(LOG_WARNING, $this->name() .
|
||||
' - cURL error getting friend ids ' .
|
||||
$e->getCode() . ' - ' . $e->getMessage());
|
||||
|
@ -177,7 +186,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
|
|||
|
||||
try {
|
||||
$more_friends = $client->statusesFriends(null, null, null, $i);
|
||||
} catch (OAuthCurlException $e) {
|
||||
} catch (Exception $e) {
|
||||
common_log(LOG_WARNING, $this->name() .
|
||||
' - cURL error getting Twitter statuses/friends ' .
|
||||
"page $i - " . $e->getCode() . ' - ' .
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
*/
|
||||
|
||||
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
|
||||
define('STATUSNET', true);
|
||||
define('LACONICA', true); // compatibility
|
||||
|
||||
$shortoptions = 'i::';
|
||||
$longoptions = array('id::');
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
*/
|
||||
|
||||
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
|
||||
define('STATUSNET', true);
|
||||
define('LACONICA', true); // compatibility
|
||||
|
||||
// Tune number of processes and how often to poll Twitter
|
||||
// XXX: Should these things be in config.php?
|
||||
|
@ -148,9 +150,9 @@ class TwitterStatusFetcher extends ParallelizingDaemon
|
|||
|
||||
function getTimeline($flink)
|
||||
{
|
||||
if (empty($flink)) {
|
||||
if (empty($flink)) {
|
||||
common_log(LOG_WARNING, $this->name() .
|
||||
" - Can't retrieve Foreign_link for foreign ID $fid");
|
||||
" - Can't retrieve Foreign_link for foreign ID $fid");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -161,17 +163,24 @@ class TwitterStatusFetcher extends ParallelizingDaemon
|
|||
// to start importing? How many statuses? Right now I'm going
|
||||
// with the default last 20.
|
||||
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
$client = null;
|
||||
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
common_debug($this->name() . ' - Grabbing friends timeline with OAuth.');
|
||||
} else {
|
||||
$client = new TwitterBasicAuthClient($flink);
|
||||
common_debug($this->name() . ' - Grabbing friends timeline with basic auth.');
|
||||
}
|
||||
|
||||
$timeline = null;
|
||||
|
||||
try {
|
||||
$timeline = $client->statusesFriendsTimeline();
|
||||
} catch (OAuthClientCurlException $e) {
|
||||
} catch (Exception $e) {
|
||||
common_log(LOG_WARNING, $this->name() .
|
||||
' - OAuth client unable to get friends timeline for user ' .
|
||||
' - Twitter client unable to get friends timeline for user ' .
|
||||
$flink->user_id . ' - code: ' .
|
||||
$e->getCode() . 'msg: ' . $e->getMessage());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user