Renable basic auth posting to Twitter for users who already have a bridge setup
This commit is contained in:
parent
bbb830e14c
commit
51adf00bd8
104
lib/twitter.php
104
lib/twitter.php
@ -154,16 +154,20 @@ function broadcast_twitter($notice)
|
||||
TWITTER_SERVICE);
|
||||
|
||||
if (is_twitter_bound($notice, $flink)) {
|
||||
if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
|
||||
return broadcast_oauth($notice, $flink);
|
||||
} else {
|
||||
return broadcast_basicauth($notice, $flink);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function broadcast_oauth($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);
|
||||
|
||||
$statustxt = format_status($notice);
|
||||
$token = TwitterOAuthClient::unpackToken($flink->credentials);
|
||||
|
||||
$client = new TwitterOAuthClient($token->key, $token->secret);
|
||||
|
||||
$status = null;
|
||||
|
||||
try {
|
||||
@ -204,7 +208,7 @@ function broadcast_twitter($notice)
|
||||
// This could represent a failure posting,
|
||||
// or the Twitter API might just be behaving flakey.
|
||||
|
||||
$errmsg = sprint('No data returned by Twitter API when ' .
|
||||
$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);
|
||||
@ -217,11 +221,95 @@ function broadcast_twitter($notice)
|
||||
$msg = sprintf('Twitter bridge posted notice %s to Twitter.',
|
||||
$notice->id);
|
||||
common_log(LOG_INFO, $msg);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function broadcast_basicauth($notice, $flink)
|
||||
{
|
||||
$user = $flink->getUser();
|
||||
$fuser = $flink->getForeignUser();
|
||||
$twitter_user = $fuser->nickname;
|
||||
$twitter_password = $flink->credentials;
|
||||
$uri = 'http://www.twitter.com/statuses/update.json';
|
||||
$statustxt = format_status($notice);
|
||||
|
||||
$options = array(CURLOPT_USERPWD => "$twitter_user:$twitter_password",
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS =>
|
||||
array(
|
||||
'status' => $statustxt,
|
||||
'source' => common_config('integration', 'source')
|
||||
),
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FAILONERROR => true,
|
||||
CURLOPT_HEADER => false,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_USERAGENT => "StatusNet",
|
||||
CURLOPT_CONNECTTIMEOUT => 120,
|
||||
CURLOPT_TIMEOUT => 120,
|
||||
|
||||
# Twitter is strict about accepting invalid "Expect" headers
|
||||
CURLOPT_HTTPHEADER => array('Expect:'));
|
||||
|
||||
$ch = curl_init($uri);
|
||||
curl_setopt_array($ch, $options);
|
||||
$data = curl_exec($ch);
|
||||
$errmsg = curl_error($ch);
|
||||
|
||||
if ($errmsg == 'The requested URL returned error: 401') {
|
||||
|
||||
$errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
|
||||
'Twitter basic auth username/password.',
|
||||
$user->nickname, $user->id);
|
||||
common_log(LOG_WARNING, $errmsg);
|
||||
|
||||
// Bad credentials. We need to delete the foreign_link
|
||||
// to Twitter and inform the user.
|
||||
|
||||
remove_twitter_link($flink);
|
||||
return true;
|
||||
|
||||
} elseif (!empty($errmsg)) {
|
||||
|
||||
$code = curl_errno($ch);
|
||||
|
||||
$msg = "cURL error: $code, '$errmsg' - trying to send notice $notice->id " .
|
||||
"to Twitter using basic auth.";
|
||||
|
||||
common_log(LOG_WARNING, $msg);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$status = json_decode($data);
|
||||
|
||||
if (empty($status)) {
|
||||
|
||||
$errmsg = sprintf('No data returned by Twitter API when ' .
|
||||
'trying to send update for %1$s (user id %2$s) ' .
|
||||
'using basic auth.',
|
||||
$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();
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user