diff options
author | Zach Copley <zach@controlyourself.ca> | 2009-08-28 04:36:47 +0000 |
---|---|---|
committer | Zach Copley <zach@controlyourself.ca> | 2009-08-28 04:36:47 +0000 |
commit | 51adf00bd80253322b473ab199e5b97dd4951d5c (patch) | |
tree | ff1563c623bc10e705e4f6bacd1ce88c2c6c21fd | |
parent | bbb830e14c718c687f0636710a1827c90b11f4cc (diff) |
Renable basic auth posting to Twitter for users who already have a bridge setup
-rw-r--r-- | lib/twitter.php | 176 | ||||
-rw-r--r-- | lib/twitteroauthclient.php | 9 |
2 files changed, 141 insertions, 44 deletions
diff --git a/lib/twitter.php b/lib/twitter.php index 7546ffa98..d63384fc9 100644 --- a/lib/twitter.php +++ b/lib/twitter.php @@ -154,80 +154,168 @@ 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); + } + } +} - $user = $flink->getUser(); +function broadcast_oauth($notice, $flink) { - // XXX: Hack to get around PHP cURL's use of @ being a a meta character - $statustxt = preg_replace('/^@/', ' @', $notice->content); + $user = $flink->getUser(); + $statustxt = format_status($notice); + $token = TwitterOAuthClient::unpackToken($flink->credentials); + $client = new TwitterOAuthClient($token->key, $token->secret); + $status = null; - $token = TwitterOAuthClient::unpackToken($flink->credentials); + try { + $status = $client->statusesUpdate($statustxt); + } catch (OAuthClientCurlException $e) { - $client = new TwitterOAuthClient($token->key, $token->secret); + if ($e->getMessage() == 'The requested URL returned error: 401') { - $status = null; + $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); - try { - $status = $client->statusesUpdate($statustxt); - } catch (OAuthClientCurlException $e) { + // Bad auth token! We need to delete the foreign_link + // to Twitter and inform the user. - if ($e->getMessage() == 'The requested URL returned error: 401') { + remove_twitter_link($flink); + return true; - $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); + } else { - // Bad auth token! We need to delete the foreign_link - // to Twitter and inform the user. + // Some other error happened, so we should probably + // try to send again later. - remove_twitter_link($flink); - return true; + $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); - } else { + return false; + } + } - // Some other error happened, so we should probably - // try to send again later. + if (empty($status)) { - $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); + // This could represent a failure posting, + // or the Twitter API might just be behaving flakey. - return false; - } - } + $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); - if (empty($status)) { + return false; + } - // This could represent a failure posting, - // or the Twitter API might just be behaving flakey. + // Notice crossed the great divide - $errmsg = sprint('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); + $msg = sprintf('Twitter bridge posted notice %s to Twitter.', + $notice->id); + common_log(LOG_INFO, $msg); - return false; - } + 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; + } - // Notice crossed the great divide + curl_close($ch); - $msg = sprintf('Twitter bridge posted notice %s to Twitter.', - $notice->id); - common_log(LOG_INFO, $msg); + $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(); 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(); diff --git a/lib/twitteroauthclient.php b/lib/twitteroauthclient.php index 3da522fc5..9821a491e 100644 --- a/lib/twitteroauthclient.php +++ b/lib/twitteroauthclient.php @@ -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 * |