X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FTwitterBridge%2Ftwitter.php;h=e133ce6f745c1a8c5e9e30ef67d367e91b6f1bbc;hb=d6719760d61b204ab7cf5ce013f2933ef4024f1a;hp=280cdb0a33d93d6fcfcdef2e8669f9cee55e383d;hpb=5efe588174c71979fc1970353c9a556ea441f138;p=quix0rs-gnu-social.git diff --git a/plugins/TwitterBridge/twitter.php b/plugins/TwitterBridge/twitter.php index 280cdb0a33..20cdf42b82 100644 --- a/plugins/TwitterBridge/twitter.php +++ b/plugins/TwitterBridge/twitter.php @@ -1,7 +1,7 @@ . */ -if (!defined('LACONICA')) { +if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1 -function update_twitter_user($twitter_id, $screen_name) -{ - $uri = 'http://twitter.com/' . $screen_name; - $fuser = new Foreign_user(); - - $fuser->query('BEGIN'); - - // Dropping down to SQL because regular DB_DataObject udpate stuff doesn't seem - // to work so good with tables that have multiple column primary keys - - // Any time we update the uri for a forein user we have to make sure there - // are no dupe entries first -- unique constraint on the uri column - - $qry = 'UPDATE foreign_user set uri = \'\' WHERE uri = '; - $qry .= '\'' . $uri . '\'' . ' AND service = ' . TWITTER_SERVICE; - - $fuser->query($qry); - - // Update the user - - $qry = 'UPDATE foreign_user SET nickname = '; - $qry .= '\'' . $screen_name . '\'' . ', uri = \'' . $uri . '\' '; - $qry .= 'WHERE id = ' . $twitter_id . ' AND service = ' . TWITTER_SERVICE; - - $fuser->query('COMMIT'); - - $fuser->free(); - unset($fuser); - - return true; -} +require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php'; +require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php'; function add_twitter_user($twitter_id, $screen_name) { - - $new_uri = 'http://twitter.com/' . $screen_name; - // Clear out any bad old foreign_users with the new user's legit URL // This can happen when users move around or fakester accounts get // repoed, and things like that. - $luser = new Foreign_user(); - $luser->uri = $new_uri; - $luser->service = TWITTER_SERVICE; - $result = $luser->delete(); + $luser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE); - if (empty($result)) { - common_log(LOG_WARNING, - "Twitter bridge - removed invalid Twitter user squatting on uri: $new_uri"); + if (!empty($luser)) { + $result = $luser->delete(); + if ($result != false) { + common_log( + LOG_INFO, + "Twitter bridge - removed old Twitter user: $screen_name ($twitter_id)." + ); + } } - $luser->free(); - unset($luser); - - // Otherwise, create a new Twitter user - $fuser = new Foreign_user(); $fuser->nickname = $screen_name; @@ -93,7 +58,8 @@ function add_twitter_user($twitter_id, $screen_name) "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name."); common_log_db_error($fuser, 'INSERT', __FILE__); } else { - common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id)."); + common_log(LOG_INFO, + "Twitter bridge - Added new Twitter user: $screen_name ($twitter_id)."); } return $result; @@ -102,7 +68,6 @@ function add_twitter_user($twitter_id, $screen_name) // Creates or Updates a Twitter user function save_twitter_user($twitter_id, $screen_name) { - // Check to see whether the Twitter user is already in the system, // and update its screen name and uri if so. @@ -110,27 +75,39 @@ function save_twitter_user($twitter_id, $screen_name) if (!empty($fuser)) { - $result = true; - - // Only update if Twitter screen name has changed + // Delete old record if Twitter user changed screen name if ($fuser->nickname != $screen_name) { - $result = update_twitter_user($twitter_id, $screen_name); - - common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' . - "$fuser->id to $screen_name, was $fuser->nickname"); + $oldname = $fuser->nickname; + $fuser->delete(); + common_log(LOG_INFO, sprintf('Twitter bridge - Updated nickname (and URI) ' . + 'for Twitter user %1$d - %2$s, was %3$s.', + $fuser->id, + $screen_name, + $oldname)); } - return $result; - } else { - return add_twitter_user($twitter_id, $screen_name); - } - $fuser->free(); - unset($fuser); + // Kill any old, invalid records for this screen name + + $fuser = Foreign_user::getByNickname($screen_name, TWITTER_SERVICE); + + if (!empty($fuser)) { + $fuser->delete(); + common_log( + LOG_INFO, + sprintf( + 'Twitter bridge - deteted old record for Twitter ' . + 'screen name "%s" belonging to Twitter ID %d.', + $screen_name, + $fuser->id + ) + ); + } + } - return true; + return add_twitter_user($twitter_id, $screen_name); } function is_twitter_bound($notice, $flink) { @@ -138,9 +115,12 @@ function is_twitter_bound($notice, $flink) { // Check to see if notice should go to Twitter if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) { - // If it's not a Twitter-style reply, or if the user WANTS to send replies. + // If it's not a Twitter-style reply, or if the user WANTS to send replies, + // or if it's in reply to a twitter notice + if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) || - ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) { + ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) || + is_twitter_notice($notice->reply_to)) { return true; } } @@ -148,88 +128,231 @@ function is_twitter_bound($notice, $flink) { return false; } +function is_twitter_notice($id) +{ + $notice = Notice::staticGet('id', $id); + + if (empty($notice)) { + // it's not any kind of notice, so it's definitely not a Twitter notice. + return false; + } + + return ($notice->source == 'twitter'); +} + function broadcast_twitter($notice) { $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); - if (is_twitter_bound($notice, $flink)) { + // Don't bother with basic auth, since it's no longer allowed - $user = $flink->getUser(); + if (!empty($flink) && TwitterOAuthClient::isPackedToken($flink->credentials)) { + if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) { + return retweet_notice($flink, Notice::staticGet('id', $notice->repeat_of)); + } else if (is_twitter_bound($notice, $flink)) { + return broadcast_oauth($notice, $flink); + } + } - // XXX: Hack to get around PHP cURL's use of @ being a a meta character - $statustxt = preg_replace('/^@/', ' @', $notice->content); + return true; +} - $token = TwitterOAuthClient::unpackToken($flink->credentials); +function retweet_notice($flink, $notice) +{ + $token = TwitterOAuthClient::unpackToken($flink->credentials); + $client = new TwitterOAuthClient($token->key, $token->secret); - $client = new TwitterOAuthClient($token->key, $token->secret); + $id = twitter_status_id($notice); - $status = null; + try { + $status = $client->statusesRetweet($id); + } catch (OAuthClientException $e) { + return process_error($e, $flink, $notice); + } +} - try { - $status = $client->statusesUpdate($statustxt); - } catch (OAuthClientCurlException $e) { +function twitter_status_id($notice) +{ + if ($notice->source == 'twitter' && + preg_match('#^http://twitter.com/[\w_.]+/status/(\d+)$#', $notice->uri, $match)) { + return $match[1]; + } - if ($e->getMessage() == 'The requested URL returned error: 401') { + return 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); +/** + * Pull any extra information from a notice that we should transfer over + * to Twitter beyond the notice text itself. + * + * @param Notice $notice + * @return array of key-value pairs for Twitter update submission + * @access private + */ +function twitter_update_params($notice) +{ + $params = array(); + if ($notice->lat || $notice->lon) { + $params['lat'] = $notice->lat; + $params['long'] = $notice->lon; + } + return $params; +} + +function broadcast_oauth($notice, $flink) { + $user = $flink->getUser(); + $statustxt = format_status($notice); + $params = twitter_update_params($notice); - // Bad auth token! We need to delete the foreign_link - // to Twitter and inform the user. + $token = TwitterOAuthClient::unpackToken($flink->credentials); + $client = new TwitterOAuthClient($token->key, $token->secret); + $status = null; - remove_twitter_link($flink); - return true; + try { + $status = $client->statusesUpdate($statustxt, $params); + } catch (OAuthClientException $e) { + return process_error($e, $flink, $notice); + } - } else { + if (empty($status)) { - // Some other error happened, so we should probably - // try to send again later. + // This could represent a failure posting, + // or the Twitter API might just be behaving flakey. - $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); + $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . + 'trying to post notice %d for User %s (user id %d).', + $notice->id, + $user->nickname, + $user->id); - return false; - } - } + 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 %d to Twitter using ' . + 'OAuth for User %s (user id %d).', + $notice->id, + $user->nickname, + $user->id); - return false; - } + common_log(LOG_INFO, $msg); + + return true; +} + +function broadcast_basicauth($notice, $flink) +{ + $user = $flink->getUser(); + + $statustxt = format_status($notice); + $params = twitter_update_params($notice); + + $client = new TwitterBasicAuthClient($flink); + $status = null; + + try { + $status = $client->statusesUpdate($statustxt, $params); + } catch (BasicAuthException $e) { + return process_error($e, $flink, $notice); + } - // 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('Twitter bridge - No data returned by Twitter API when ' . + 'trying to post notice %d for %s (user id %d).', + $notice->id, + $user->nickname, + $user->id); + + common_log(LOG_WARNING, $errmsg); + + $errmsg = sprintf('No data returned by Twitter API when ' . + 'trying to post notice %d for %s (user id %d).', + $notice->id, + $user->nickname, + $user->id); + common_log(LOG_WARNING, $errmsg); + return false; } + $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' . + 'HTTP basic auth for User %s (user id %d).', + $notice->id, + $user->nickname, + $user->id); + + common_log(LOG_INFO, $msg); + return true; } +function process_error($e, $flink, $notice) +{ + $user = $flink->getUser(); + $code = $e->getCode(); + + $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' . + 'User %s (user id: %d): %s.', + $code, + $notice->id, + $user->nickname, + $user->id, + $e->getMessage()); + + common_log(LOG_WARNING, $logmsg); + + switch($code) { + case 401: + // Probably a revoked or otherwise bad access token - nuke! + remove_twitter_link($flink); + return true; + break; + case 403: + // User has exceeder her rate limit -- toss the notice + return true; + break; + default: + + // For every other case, it's probably some flakiness so try + // sending the notice again later (requeue). + + return false; + break; + } +} + +function format_status($notice) +{ + // XXX: Hack to get around PHP cURL's use of @ being a a meta character + $statustxt = preg_replace('/^@/', ' @', $notice->content); + + // Convert !groups to #hashes + + // XXX: Make this an optional setting? + + $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt); + + if (mb_strlen($statustxt) > 140) { + $noticeUrl = common_shorten_url($notice->uri); + $urlLen = mb_strlen($noticeUrl); + $statustxt = mb_substr($statustxt, 0, 140 - ($urlLen + 3)) . ' … ' . $noticeUrl; + } + + return $statustxt; +} + 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(); + $result = $flink->safeDelete(); if (empty($result)) { common_log(LOG_ERR, 'Could not remove Twitter bridge ' . @@ -256,3 +379,39 @@ function remove_twitter_link($flink) } +/** + * Send a mail message to notify a user that her Twitter bridge link + * has stopped working, and therefore has been removed. This can + * happen when the user changes her Twitter password, or otherwise + * revokes access. + * + * @param User $user user whose Twitter bridge link has been removed + * + * @return boolean success flag + */ + +function mail_twitter_bridge_removed($user) +{ + $profile = $user->getProfile(); + + common_switch_locale($user->language); + + $subject = sprintf(_m('Your Twitter bridge has been disabled.')); + + $site_name = common_config('site', 'name'); + + $body = sprintf(_m('Hi, %1$s. We\'re sorry to inform you that your ' . + 'link to Twitter has been disabled. We no longer seem to have ' . + 'permission to update your Twitter status. (Did you revoke ' . + '%3$s\'s access?)' . "\n\n" . + 'You can re-enable your Twitter bridge by visiting your ' . + "Twitter settings page:\n\n\t%2\$s\n\n" . + "Regards,\n%3\$s\n"), + $profile->getBestName(), + common_local_url('twittersettings'), + common_config('site', 'name')); + + common_switch_locale(); + return mail_to_user($user, $subject, $body); +} +