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_load($screen_name) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'twitter_account')
->propertyCondition('screen_name', $screen_name);
$result = $query
->execute();
$ids = !empty($result['twitter_account']) ? array_keys($result['twitter_account']) : array();
if (count($ids)) {
return entity_load_single('twitter_account', array_pop($ids));
}
else {
return FALSE;
}
}
function twitter_load_accounts() {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'twitter_account')
->propertyCondition('uid', 0, '<>')
->propertyOrderBy('screen_name');
$result = $query
->execute();
$ids = !empty($result['twitter_account']) ? array_keys($result['twitter_account']) : array();
if (count($ids)) {
return entity_load('twitter_account', $ids);
}
else {
return array();
}
}
function twitter_load_account_names() {
$accounts = array();
foreach (twitter_load_accounts() as $account) {
if (isset($account->twitter_uid) && isset($account->name)) {
$accounts[$account->twitter_uid] = check_plain($account->name);
}
}
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)) {
if (is_array($status->retweeted_status)) {
$text = 'RT @' . $status->retweeted_status['user']['screen_name'];
if (empty($status->retweeted_status['full_text'])) {
$text .= ': ' . $status->retweeted_status['text'];
}
else {
$text .= ': ' . $status->retweeted_status['full_text'];
}
}
else {
$text = 'RT @' . $status->retweeted_status->user->screen_name;
if (empty($status->retweeted_status->full_text)) {
$text .= ': ' . $status->retweeted_status->text;
}
else {
$text .= ': ' . $status->retweeted_status->full_text;
}
}
}
else {
if (empty($status->full_text)) {
$text = $status->text;
}
else {
$text = $status->full_text;
}
}
$twitter_status = new TwitterStatus();
$twitter_status->twitter_id = $status->id;
$twitter_status->screen_name = $status->user->screen_name;
$twitter_status->created_time = strtotime($status->created_at);
$twitter_status->text = $text;
$twitter_status->source = $status->source;
$twitter_status->in_reply_to_status_id = $status->in_reply_to_status_id > 0 ? (string) $status->in_reply_to_status_id : NULL;
$twitter_status->in_reply_to_user_id = $status->in_reply_to_user_id;
$twitter_status->in_reply_to_screen_name = $status->in_reply_to_screen_name;
$twitter_status->truncated = (int) $status->truncated;
$twitter_status->retweet_count = $status->retweet_count;
$twitter_status->is_new = entity_load_single('twitter_status', $twitter_status->twitter_id) ? FALSE : TRUE;
entity_save('twitter_status', $twitter_status);
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;
}
$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 = entity_load_single('twitter_account', $id);
$since = db_query("SELECT MAX(twitter_id)\n FROM {twitter}\n WHERE screen_name = :screen_name", array(
':screen_name' => $twitter_account->screen_name,
))
->fetchField();
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 = entity_load_single('twitter_account', $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 $twitter_status) {
twitter_account_save($twitter_status->user);
twitter_status_save($twitter_status);
}
}
function twitter_tweets($screen_name = NULL) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'twitter_status');
if (isset($screen_name)) {
$query
->entityCondition('screen_name', $screen_name);
}
$result = $query
->execute();
$ids = !empty($result['twitter_status']) ? array_keys($result['twitter_status']) : array();
$twitter_statuses = entity_load('twitter_status', $ids);
return $twitter_statuses;
}
function twitter_account_save($twitter_account) {
$twitter_account_db = entity_load_single('twitter_account', $twitter_account->twitter_uid);
$twitter_account->is_new = $twitter_account_db ? FALSE : TRUE;
if ($twitter_account_db) {
$twitter_account = (object) array_merge((array) $twitter_account_db, (array) $twitter_account);
}
entity_save('twitter_account', $twitter_account);
}
function twitter_account_delete($twitter_uid) {
$account = entity_load_single('twitter_account', $twitter_uid);
entity_delete('twitter_account', $twitter_uid);
$twitter_statuses = twitter_tweets($account->screen_name);
entity_delete_multiple('twitter_status', array_keys($twitter_statuses));
$query = db_delete('authmap');
$query
->condition('authname', $twitter_uid);
$query
->condition('module', 'twitter');
$query
->execute();
}
function twitter_screen_name_from_uid($twitter_uid) {
return db_select('twitter_account', 'ta')
->fields('ta', array(
'screen_name',
))
->condition('twitter_uid', $twitter_uid)
->range(0, 1)
->execute()
->fetchField();
}