View source
<?php
module_load_include('lib.php', 'twitter');
define('TWITTER_HOST', 'http://twitter.com');
define('TWITTER_API', 'https://api.twitter.com');
define('TWITTER_SEARCH', 'http://search.twitter.com');
define('TWITTER_TINYURL', 'http://tinyurl.com');
function twitter_menu() {
$items = array();
$items['twitter/oauth'] = array(
'title' => 'Twitter',
'access callback' => TRUE,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'twitter_oauth_callback',
),
'type' => MENU_CALLBACK,
'file' => 'twitter.pages.inc',
);
$items['admin/settings/twitter'] = array(
'title' => 'Twitter setup',
'description' => 'Twitter module settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'twitter_admin_form',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'twitter.pages.inc',
);
$items['admin/settings/twitter/default'] = array(
'title' => 'Twitter',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['user/%user_category/edit/twitter'] = array(
'title' => 'Twitter accounts',
'page callback' => 'twitter_user_settings',
'page arguments' => array(
1,
),
'access callback' => 'twitter_edit_access',
'access arguments' => array(
1,
),
'load arguments' => array(
'%map',
'%index',
),
'weight' => 10,
'file' => 'twitter.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['user/%user/edit/twitter/global/%'] = array(
'title' => 'Twitter accounts',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'twitter_user_make_global',
1,
5,
),
'access arguments' => array(
'make twitter accounts global',
),
'file' => 'twitter.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function twitter_edit_access($account) {
return user_edit_access($account) && user_access('add twitter accounts');
}
function twitter_perm() {
return array(
'add twitter accounts',
'use global twitter account',
'make twitter accounts global',
'import own tweets',
);
}
function twitter_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'categories':
return array(
array(
'name' => 'twitter',
'title' => t('Twitter accounts'),
'weight' => 3,
),
);
}
}
function twitter_theme() {
return array(
'twitter_account_list_form' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
function twitter_shorten_url($url) {
if (module_exists('shorten')) {
return shorten_url($url);
}
else {
$response = drupal_http_request(variable_get('twitter_tinyurl', TWITTER_TINYURL) . "/api-create.php?url=" . $url);
if ($response->code == 200) {
return $response->data;
}
else {
return $url;
}
}
}
function twitter_cron() {
if (!variable_get('twitter_import', TRUE)) {
return;
}
module_load_include('inc', 'twitter');
$sql = "SELECT twitter_uid FROM {twitter_account} WHERE import = 1 ORDER BY last_refresh ASC";
$results = db_query_range($sql, 0, 20);
while ($account = db_fetch_object($results)) {
twitter_fetch_user_timeline($account->twitter_uid);
}
if ($age = variable_get('twitter_expire', 0)) {
db_query('DELETE FROM {twitter} WHERE created_time < %d', time() - $age);
}
}
function twitter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Twitter @username converter'),
1 => t('Twitter #hashtag converter'),
);
case 'description':
switch ($delta) {
case 0:
return t('Converts Twitter-style @usernames into links to Twitter account pages.');
case 1:
return t('Converts Twitter-style #hashtags into links to hashtags.org.');
default:
return;
}
case 'process':
switch ($delta) {
case 0:
return twitter_link_filter($text);
case 1:
$destination = variable_get('twitter_search', TWITTER_SEARCH) . '/search?q=%23';
return twitter_link_filter($text, '#', $destination);
default:
return $text;
}
default:
return $text;
}
}
function twitter_filter_tips($delta, $format, $long = FALSE) {
global $base_url;
switch ($delta) {
case 0:
return t('Twitter-style @usersnames are linked to their Twitter account pages.');
case 1:
return t('Twitter-style #hashtags are linked to !url.', array(
'!url' => '<a href="http://search.twitter.com/">search.twitter.com</a>',
));
}
}
function twitter_link_filter($text, $prefix = '@', $destination = '') {
if ($destination == '') {
$destination = variable_get('twitter_host', TWITTER_HOST) . '/';
}
$matches = array(
'/\\>' . $prefix . '(\\w+)/ui',
'/^' . $prefix . '(\\w+)/ui',
'/(\\s+)' . $prefix . '(\\w+)/ui',
);
$replacements = array(
'><a href="' . $destination . '${1}" target="_blank">' . $prefix . '${1}</a>',
'<a href="' . $destination . '${1}" target="_blank">' . $prefix . '${1}</a>',
'${1}<a href="' . $destination . '${2}" target="_blank">' . $prefix . '${2}</a>',
);
return preg_replace($matches, $replacements, $text);
}
function twitter_get_user_accounts($uid) {
$drupal_user = user_load($uid);
return module_invoke_all('twitter_accounts', $drupal_user);
}
function twitter_twitter_accounts($account) {
module_load_include('inc', 'twitter');
$twitter_accounts = array();
$sql = "SELECT twitter_uid FROM {twitter_account} WHERE uid = %d";
if (user_access('use global twitter account', $account)) {
$sql .= " OR is_global=1";
}
$results = db_query($sql, $account->uid);
while ($row = db_fetch_array($results)) {
$key = $row['twitter_uid'];
$twitter_accounts[] = twitter_account_load($key);
}
return $twitter_accounts;
}
function _twitter_use_oauth() {
if (!module_exists('oauth_common')) {
return FALSE;
}
return variable_get('twitter_consumer_key', '') && variable_get('twitter_consumer_secret', '');
}
function twitter_views_api() {
return array(
'api' => 2,
);
}
function twitter_profile_url($twitter_screen_name) {
$name = check_plain($twitter_screen_name);
return variable_get('twitter_host', TWITTER_HOST) . '/' . $name;
}