From 51adf00bd80253322b473ab199e5b97dd4951d5c Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 28 Aug 2009 04:36:47 +0000 Subject: Renable basic auth posting to Twitter for users who already have a bridge setup --- lib/twitter.php | 176 +++++++++++++++++++++++++++++++++------------ lib/twitteroauthclient.php | 9 +++ 2 files changed, 141 insertions(+), 44 deletions(-) (limited to 'lib') 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 * -- cgit v1.2.3-54-g00ecf From 36b6ef8d05bf6e4290894f9677cc9618a9bfd486 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 28 Aug 2009 06:00:30 +0000 Subject: Abstract the Twitter basic auth stuff into its own client class --- lib/twitter.php | 77 +++++--------- lib/twitterbasicauthclient.php | 236 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+), 51 deletions(-) create mode 100644 lib/twitterbasicauthclient.php (limited to 'lib') diff --git a/lib/twitter.php b/lib/twitter.php index d63384fc9..b734d22d8 100644 --- a/lib/twitter.php +++ b/lib/twitter.php @@ -218,7 +218,7 @@ function broadcast_oauth($notice, $flink) { // Notice crossed the great divide - $msg = sprintf('Twitter bridge posted notice %s to Twitter.', + $msg = sprintf('Twitter bridge posted notice %s to Twitter using OAuth.', $notice->id); common_log(LOG_INFO, $msg); @@ -228,70 +228,44 @@ function broadcast_oauth($notice, $flink) { 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); + $client = new TwitterBasicAuthClient($flink); + $status = null; - // Bad credentials. We need to delete the foreign_link - // to Twitter and inform the user. + try { + $status = $client->statusesUpdate($statustxt); + } catch (BasicAuthCurlException $e) { - remove_twitter_link($flink); - return true; + if ($e->getMessage() == 'The requested URL returned error: 401') { - } elseif (!empty($errmsg)) { + $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); - $code = curl_errno($ch); + remove_twitter_link($flink); + return true; - $msg = "cURL error: $code, '$errmsg' - trying to send notice $notice->id " . - "to Twitter using basic auth."; + } else { - common_log(LOG_WARNING, $msg); + $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; + } } - 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); + 'trying to send update for %1$s (user id %2$s).', + $user->nickname, $user->id); common_log(LOG_WARNING, $errmsg); return false; @@ -302,6 +276,7 @@ function broadcast_basicauth($notice, $flink) common_log(LOG_INFO, $msg); return true; + } function format_status($notice) diff --git a/lib/twitterbasicauthclient.php b/lib/twitterbasicauthclient.php new file mode 100644 index 000000000..82359d93d --- /dev/null +++ b/lib/twitterbasicauthclient.php @@ -0,0 +1,236 @@ +. + * + * @category Integration + * @package StatusNet + * @author Zach Copley + * @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 + * @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 + * @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, true); + $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, null, true); + $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 = false) + { + $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; + } + +} -- cgit v1.2.3-54-g00ecf From 36c104fb34d7128a0372dd3f77504c0b2f76ba80 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 28 Aug 2009 07:02:27 +0000 Subject: Make SyncTwitterFriends and TwitterStatusFetcher daemons use both HTTP Basic Auth as well as OAuth --- lib/oauthclient.php | 2 +- lib/twitterbasicauthclient.php | 6 +++--- lib/twitteroauthclient.php | 2 +- scripts/synctwitterfriends.php | 17 +++++++++++++---- scripts/twitterqueuehandler.php | 2 ++ scripts/twitterstatusfetcher.php | 21 +++++++++++++++------ 6 files changed, 35 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/oauthclient.php b/lib/oauthclient.php index cc10cea8f..f1827726e 100644 --- a/lib/oauthclient.php +++ b/lib/oauthclient.php @@ -22,7 +22,7 @@ * @category Action * @package StatusNet * @author Zach Copley - * @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/ */ diff --git a/lib/twitterbasicauthclient.php b/lib/twitterbasicauthclient.php index 82359d93d..66bb01e53 100644 --- a/lib/twitterbasicauthclient.php +++ b/lib/twitterbasicauthclient.php @@ -88,7 +88,7 @@ class TwitterBasicAuthClient $params = array('status' => $status, 'source' => common_config('integration', 'source'), 'in_reply_to_status_id' => $in_reply_to_status_id); - $response = $this->httpRequest($url, $params, true); + $response = $this->httpRequest($url, $params); $status = json_decode($response); return $status; } @@ -117,7 +117,7 @@ class TwitterBasicAuthClient $url .= "?$qry"; } - $response = $this->httpRequest($url, null, true); + $response = $this->httpRequest($url); $statuses = json_decode($response); return $statuses; } @@ -190,7 +190,7 @@ class TwitterBasicAuthClient * * @return mixed the request */ - function httpRequest($url, $params = null, $auth = false) + function httpRequest($url, $params = null, $auth = true) { $options = array( CURLOPT_RETURNTRANSFER => true, diff --git a/lib/twitteroauthclient.php b/lib/twitteroauthclient.php index 9821a491e..e37fa05f0 100644 --- a/lib/twitteroauthclient.php +++ b/lib/twitteroauthclient.php @@ -22,7 +22,7 @@ * @category Integration * @package StatusNet * @author Zach Copley - * @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/ */ diff --git a/scripts/synctwitterfriends.php b/scripts/synctwitterfriends.php index 545cb23b3..2cb7525ea 100755 --- a/scripts/synctwitterfriends.php +++ b/scripts/synctwitterfriends.php @@ -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() . ' - ' . diff --git a/scripts/twitterqueuehandler.php b/scripts/twitterqueuehandler.php index ce4d824d0..992141f9d 100755 --- a/scripts/twitterqueuehandler.php +++ b/scripts/twitterqueuehandler.php @@ -19,6 +19,8 @@ */ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); +define('STATUSNET', true); +define('LACONICA', true); // compatibility $shortoptions = 'i::'; $longoptions = array('id::'); diff --git a/scripts/twitterstatusfetcher.php b/scripts/twitterstatusfetcher.php index 68f7e9bf7..6dca6f75b 100755 --- a/scripts/twitterstatusfetcher.php +++ b/scripts/twitterstatusfetcher.php @@ -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()); } -- cgit v1.2.3-54-g00ecf