View source
<?php
module_load_include('php', 'oauth_common', 'lib/OAuth');
require_once libraries_get_path('twitter') . '/twitter.lib.php';
require 'twitter.lib.php';
function twitter_connect($account) {
$auth = $account
->get_auth();
if ($auth['oauth_token'] && $auth['oauth_token_secret']) {
return new Twitter6(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''), $auth['oauth_token'], $auth['oauth_token_secret']);
}
else {
return FALSE;
}
}
function twitter_account_save($twitter_user, $save_auth = FALSE, $account = NULL) {
$values = (array) $twitter_user;
$values['twitter_uid'] = $values['id'];
if ($save_auth) {
$values += $twitter_user
->get_auth();
if (empty($account)) {
global $user;
$account = $user;
}
$values['uid'] = $account->uid;
}
if (db_result(db_query("SELECT 1 FROM {twitter_account} WHERE twitter_uid = %d", $values['id']))) {
drupal_write_record('twitter_account', $values, array(
'twitter_uid',
));
}
else {
drupal_write_record('twitter_account', $values);
}
}
function twitter_account_load($id) {
$values = db_fetch_array(db_query("SELECT * FROM {twitter_account} WHERE twitter_uid = %d", $id));
$values['id'] = $values['twitter_uid'];
$account = new TwitterUser($values);
$account
->set_auth($values);
$account->uid = $values['uid'];
$account->import = $values['import'];
$account->include_retweets = $values['include_retweets'];
$account->is_global = $values['is_global'];
return $account;
}
function twitter_status_save($status) {
$status->twitter_id = $status->id;
$status->screen_name = $status->user->screen_name;
$status->created_time = strtotime($status->created_at);
if (db_result(db_query("SELECT 1 FROM {twitter} WHERE twitter_id = %s", $status->id))) {
drupal_write_record('twitter', $status, array(
'twitter_id',
));
}
else {
drupal_write_record('twitter', $status);
}
module_invoke_all('twitter_status_save', $status);
}
function twitter_set_status($twitter_account, $status) {
$twitter = twitter_connect($twitter_account);
$twitter
->statuses_update($status);
}
function twitter_fetch_user_timeline($id) {
$account = twitter_account_load($id);
$since = db_result(db_query("SELECT MAX(twitter_id) FROM {twitter} WHERE screen_name = '%s'", $account->screen_name));
$twitter = twitter_connect($account);
$params = array();
if ($since) {
$params['since_id'] = $since;
}
$statuses = $twitter
->user_timeline($account->id, $params);
foreach ($statuses as $status) {
twitter_status_save($status);
}
if (count($statuses) > 0) {
twitter_account_save($statuses[0]->user);
}
db_query("UPDATE {twitter_account} SET last_refresh = %d WHERE twitter_uid=%d", time(), $account->id);
}
function twitter_user_delete($twitter_uid) {
$account = twitter_account_load($twitter_uid);
$sql = "DELETE FROM {twitter_account} WHERE twitter_uid = %d";
db_query($sql, $twitter_uid);
$sql = "DELETE FROM {twitter} WHERE screen_name = '%s'";
db_query($sql, $account->screen_name);
$sql = "DELETE FROM {authmap} WHERE authname = %d AND module = 'twitter'";
db_query($sql, $twitter_uid);
}