View source
<?php
module_load_include('php', 'oauth_common', 'lib/OAuth');
require_once 'twitter.lib.php';
function twitter_connect($twitter_account = NULL, $access_global = FALSE, $force = FALSE) {
if (empty($twitter_account)) {
$accounts = twitter_load_authenticated_accounts(NULL, $access_global, $force);
if (empty($accounts)) {
watchdog('twitter', 'There are no authenticated Twitter accounts to use for API connections.');
}
else {
$twitter_account = reset($accounts);
}
}
if (!empty($twitter_account)) {
$auth = $twitter_account
->get_auth();
if (isset($auth['oauth_token']) && isset($auth['oauth_token_secret'])) {
return new Twitter(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''), $auth['oauth_token'], $auth['oauth_token_secret']);
}
}
return FALSE;
}
function twitter_account_save($twitter_user, $save_auth = FALSE) {
$values = (array) $twitter_user;
$values['twitter_uid'] = $values['id'];
foreach (array(
'protected',
'verified',
'profile_background_tile',
) as $k) {
if (isset($values[$k])) {
$values[$k] = (int) $values[$k];
}
}
if ($save_auth) {
$values += $twitter_user
->get_auth();
}
$schema = drupal_get_schema('twitter_account');
foreach ($values as $k => $v) {
if (!isset($schema['fields'][$k])) {
unset($values[$k]);
}
}
if (db_result(db_query("SELECT 1 FROM {twitter_account} WHERE twitter_uid = %d", $values['twitter_uid']))) {
drupal_write_record('twitter_account', $values, array(
'twitter_uid',
));
}
else {
drupal_write_record('twitter_account', $values);
}
module_invoke_all('twitter_account_save', $values);
}
function twitter_account_load($id, $is_uid = TRUE) {
if ($is_uid) {
$query = "SELECT * FROM {twitter_account} WHERE twitter_uid = %d";
}
else {
$query = "SELECT * FROM {twitter_account} WHERE screen_name = '%s'";
}
$values = db_fetch_array(db_query($query, $id));
if (!empty($values)) {
$values['id'] = $values['twitter_uid'];
$account = new TwitterUser($values);
$account
->set_auth($values);
$account->import = $values['import'];
$account->mentions = $values['mentions'];
$account->is_global = $values['is_global'];
return $account;
}
return NULL;
}
function twitter_load_accounts() {
$accounts = array();
$result = db_query('SELECT twitter_uid
FROM {twitter_account}
WHERE uid <> 0
ORDER BY screen_name');
while ($row = db_fetch_array($result)) {
$accounts[] = twitter_account_load($row['twitter_uid']);
}
return $accounts;
}
function twitter_load_authenticated_accounts($uid = NULL, $access_global = TRUE, $force = FALSE) {
$auth_accounts = array();
foreach (twitter_load_accounts() as $index => $twitter_account) {
if ($twitter_account
->is_auth()) {
if ($force || $access_global && $twitter_account->is_global || isset($uid) && $uid == $twitter_account->uid) {
$auth_accounts[] = $twitter_account;
}
}
}
return $auth_accounts;
}
function twitter_status_save($status) {
if (!empty($status->retweeted_status)) {
$text = 'RT @' . $status->retweeted_status->user->screen_name . ': ' . $status->retweeted_status->text;
}
else {
$text = $status->text;
}
$row = array(
'twitter_id' => $status->id,
'screen_name' => $status->user->screen_name,
'created_time' => strtotime($status->created_at),
'text' => $text,
'source' => $status->source,
'in_reply_to_status_id' => $status->in_reply_to_status_id > 0 ? (string) $status->in_reply_to_status_id : NULL,
'in_reply_to_user_id' => $status->in_reply_to_user_id,
'in_reply_to_screen_name' => $status->in_reply_to_screen_name,
'truncated' => (int) $status->truncated,
);
if (db_result(db_query("SELECT 1 FROM {twitter} WHERE twitter_id = %s", $row['twitter_id']))) {
drupal_write_record('twitter', $row, array(
'twitter_id',
));
}
else {
drupal_write_record('twitter', $row);
}
module_invoke_all('twitter_status_save', $status);
}
function twitter_set_status($twitter_account, $status) {
$twitter = twitter_connect($twitter_account);
if (empty($twitter)) {
return FALSE;
}
return $twitter
->statuses_update($status);
}
function twitter_statuses_oembed($tweet_id) {
$twitter = twitter_connect(NULL, TRUE, TRUE);
if (empty($twitter)) {
watchdog('twitter', 'Unable to load an authenticated Twitter account to embed a tweet.');
return FALSE;
}
else {
$params = array(
'hide_media' => !variable_get('twitter_media', FALSE),
'hide_thread' => !variable_get('twitter_conversation', FALSE),
'align' => variable_get('twitter_align', 'none'),
);
return $twitter
->statuses_oembed($tweet_id, $params);
}
}
function twitter_fetch_user_timeline($id) {
$twitter_account = twitter_account_load($id);
$since = db_result(db_query("SELECT MAX(twitter_id) FROM {twitter} WHERE screen_name = '%s'", $twitter_account->screen_name));
if ($twitter_account
->is_auth()) {
$twitter = twitter_connect($twitter_account);
}
else {
$twitter = twitter_connect(NULL, TRUE, TRUE);
}
if (empty($twitter)) {
watchdog('twitter', 'Unable to authenticate to download tweets for the %name account.', array(
'%name' => $twitter_account->screen_name,
));
return FALSE;
}
$params = array();
if (!empty($since)) {
$params['since_id'] = $since;
}
$hook = 'twitter_prefetch_timeline';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
if (function_exists($function)) {
$function($twitter_account, $params);
}
}
$statuses = $twitter
->user_timeline($id, $params);
drupal_alter('twitter_statuses', $statuses, $twitter_account);
if (count($statuses) > 0) {
watchdog('twitter', 'Downloaded %count tweets for the %name account.', array(
'%count' => count($statuses),
'%name' => $twitter_account->screen_name,
));
foreach ($statuses as $status) {
twitter_status_save($status);
}
}
module_invoke_all('twitter_insert_statuses', $statuses, $twitter_account);
if (count($statuses) > 0) {
twitter_account_save($statuses[0]->user);
}
}
function twitter_retweet($twitter_account, $tweet_id) {
module_load_include('lib.php', 'twitter');
$twitter = twitter_connect($twitter_account);
$twitter
->retweet($tweet_id, $twitter_account);
}
function twitter_fetch_mentions_timeline($id) {
$twitter_account = twitter_account_load($id);
$twitter = twitter_connect($twitter_account);
if (empty($twitter)) {
watchdog('twitter', 'Unable to authenticate to download mentions for the %name account.', array(
'%name' => $account->screen_name,
));
return FALSE;
}
$params = array();
$statuses = $twitter
->mentions_timeline($params);
foreach ($statuses as $status) {
if (!twitter_account_load($status->user->id)) {
twitter_account_save($status->user);
}
twitter_status_save($status);
}
}
function twitter_tweets($screen_name = NULL) {
if (empty($screen_name)) {
$result = db_query('SELECT * FROM {twitter}');
}
else {
$result = db_query("SELECT * FROM {twitter} WHERE screen_name = '%s'", $screen_name);
}
$tweets = array();
while ($row = db_fetch_object($result)) {
$tweets[] = $row;
}
return $tweets;
}
function twitter_account_delete($twitter_uid) {
$account = twitter_account_load($twitter_uid);
db_query('DELETE FROM {twitter_account} WHERE twitter_uid = %d', $twitter_uid);
db_query("DELETE FROM {twitter} WHERE screen_name = '%s'", $account->screen_name);
db_query("DELETE FROM {authmap} WHERE authname = %d AND module = 'twitter'", $twitter_uid);
}