diff options
author | Zach Copley <zach@controlyourself.ca> | 2009-08-28 07:03:32 +0000 |
---|---|---|
committer | Zach Copley <zach@controlyourself.ca> | 2009-08-28 07:03:32 +0000 |
commit | ca30cc2d594acabefeafa61edb61342b32752bc1 (patch) | |
tree | 8e843c7ecbaeb7c160495e18f7407acade4aa117 /lib/twitter.php | |
parent | c0d03fc2799c7b0c57d05166b404a3d13427f497 (diff) | |
parent | 36c104fb34d7128a0372dd3f77504c0b2f76ba80 (diff) |
Merge branch 'twitter-basic-auth' into 0.8.x
Diffstat (limited to 'lib/twitter.php')
-rw-r--r-- | lib/twitter.php | 145 |
1 files changed, 104 insertions, 41 deletions
diff --git a/lib/twitter.php b/lib/twitter.php index 7546ffa98..b734d22d8 100644 --- a/lib/twitter.php +++ b/lib/twitter.php @@ -154,72 +154,135 @@ 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; + + } else { - $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); + // 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); - // Bad auth token! We need to delete the foreign_link - // to Twitter and inform the user. + return false; + } + } - remove_twitter_link($flink); - return true; + if (empty($status)) { - } else { + // This could represent a failure posting, + // or the Twitter API might just be behaving flakey. - // Some other error happened, so we should probably - // try to send again later. + $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); - $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; + } - return false; - } - } + // Notice crossed the great divide - if (empty($status)) { + $msg = sprintf('Twitter bridge posted notice %s to Twitter using OAuth.', + $notice->id); + common_log(LOG_INFO, $msg); - // This could represent a failure posting, - // or the Twitter API might just be behaving flakey. + return true; +} - $errmsg = sprint('No data returned by Twitter API when ' . - 'trying to send update for %1$s (user id %2$s).', - $user->nickname, $user->id); +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; } + } - // Notice crossed the great divide + if (empty($status)) { - $msg = sprintf('Twitter bridge posted notice %s to Twitter.', - $notice->id); - common_log(LOG_INFO, $msg); + $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) @@ -227,7 +290,7 @@ 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(); |