| 1 |
<?php
|
| 2 |
// $Id: twitter.module,v 1.10.2.2 2009/06/11 02:50:07 walkah Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_meu()
|
| 6 |
*/
|
| 7 |
function twitter_menu() {
|
| 8 |
$items = array();
|
| 9 |
|
| 10 |
$items['twitter/oauth'] = array(
|
| 11 |
'title' => 'Twitter',
|
| 12 |
'access callback' => TRUE,
|
| 13 |
'page callback' => 'drupal_get_form',
|
| 14 |
'page arguments' => array('twitter_oauth_callback'),
|
| 15 |
'type' => MENU_CALLBACK,
|
| 16 |
'file' => 'twitter.pages.inc',
|
| 17 |
);
|
| 18 |
|
| 19 |
$items['admin/settings/twitter'] = array(
|
| 20 |
'title' => 'Twitter setup',
|
| 21 |
'description' => 'Twitter module settings',
|
| 22 |
'page callback' => 'drupal_get_form',
|
| 23 |
'page arguments' => array('twitter_admin_form'),
|
| 24 |
'access arguments' => array('administer site configuration'),
|
| 25 |
'file' => 'twitter.pages.inc'
|
| 26 |
);
|
| 27 |
|
| 28 |
$items['user/%user_category/edit/twitter'] = array(
|
| 29 |
'title' => 'Twitter accounts',
|
| 30 |
'page callback' => 'twitter_user_settings',
|
| 31 |
'page arguments' => array(1),
|
| 32 |
'access arguments' => array('add twitter accounts'),
|
| 33 |
'load arguments' => array('%map', '%index'),
|
| 34 |
'weight' => 10,
|
| 35 |
'file' => 'twitter.pages.inc',
|
| 36 |
'type' => MENU_LOCAL_TASK,
|
| 37 |
);
|
| 38 |
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_perm()
|
| 44 |
*/
|
| 45 |
function twitter_perm() {
|
| 46 |
return array('add twitter accounts', 'use global twitter account');
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Implementation of hook_user().
|
| 51 |
*/
|
| 52 |
function twitter_user($op, &$edit, &$account, $category = NULL) {
|
| 53 |
switch ($op) {
|
| 54 |
case 'categories':
|
| 55 |
return array(
|
| 56 |
array(
|
| 57 |
'name' => 'twitter',
|
| 58 |
'title' => 'Twitter accounts',
|
| 59 |
'weight' => 3,
|
| 60 |
),
|
| 61 |
);
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
function twitter_theme() {
|
| 66 |
return array(
|
| 67 |
'twitter_account_list_form' => array(
|
| 68 |
'arguments' => array('form' => NULL),
|
| 69 |
)
|
| 70 |
);
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Very lightweight helper function to generate a TinyURL for a given post.
|
| 75 |
*/
|
| 76 |
function twitter_shorten_url($url) {
|
| 77 |
if (function_exists('custom_twitter_shorten_url')) {
|
| 78 |
return custom_twitter_shorten_url($url);
|
| 79 |
}
|
| 80 |
else {
|
| 81 |
$response = drupal_http_request("http://tinyurl.com/api-create.php?url=" . $url);
|
| 82 |
if ($response->code == 200) {
|
| 83 |
return $response->data;
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
return $url;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Implementation of hook_cron()
|
| 93 |
*
|
| 94 |
* Imports new Twitter statuses for site users, and deletes expired tweets.
|
| 95 |
*/
|
| 96 |
function twitter_cron() {
|
| 97 |
if (!variable_get('twitter_import', TRUE)) {
|
| 98 |
return;
|
| 99 |
}
|
| 100 |
|
| 101 |
module_load_include('inc', 'twitter');
|
| 102 |
|
| 103 |
// Pull up a list of Twitter accounts that are flagged for updating,
|
| 104 |
// sorted by how long it's been since we last updated them. This ensures
|
| 105 |
// that the most out-of-date accounts get updated first.
|
| 106 |
|
| 107 |
$sql = "SELECT twitter_uid FROM {twitter_account} WHERE import = 1 ORDER BY last_refresh ASC";
|
| 108 |
|
| 109 |
$results = db_query_range($sql, 0, 20);
|
| 110 |
while ($account = db_fetch_object($results)) {
|
| 111 |
twitter_fetch_user_timeline($account->twitter_uid);
|
| 112 |
}
|
| 113 |
|
| 114 |
// Nuke old statuses.
|
| 115 |
if ($age = variable_get('twitter_expire', 0)) {
|
| 116 |
db_query('DELETE FROM {twitter} WHERE created_time < %d', time() - $age);
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
| 120 |
|
| 121 |
/**
|
| 122 |
* Implementation of hook_filter().
|
| 123 |
* - Twitter @username converter:
|
| 124 |
* .Converts Twitter-style @usernames into links to Twitter account pages.
|
| 125 |
* - Twitter #hashtag converter:
|
| 126 |
* .Converts Twitter-style #hashtags into links to hashtags.org.
|
| 127 |
*/
|
| 128 |
function twitter_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 129 |
switch ($op) {
|
| 130 |
case 'list':
|
| 131 |
return array(0 => t('Twitter @username converter'), 1 => t('Twitter #hashtag converter'));
|
| 132 |
|
| 133 |
case 'description':
|
| 134 |
switch ($delta) {
|
| 135 |
case 0:
|
| 136 |
return t('Converts Twitter-style @usernames into links to Twitter account pages.');
|
| 137 |
case 1:
|
| 138 |
return t('Converts Twitter-style #hashtags into links to hashtags.org.');
|
| 139 |
default:
|
| 140 |
return;
|
| 141 |
}
|
| 142 |
|
| 143 |
case 'process':
|
| 144 |
switch ($delta) {
|
| 145 |
case 0:
|
| 146 |
return twitter_link_filter($text);
|
| 147 |
case 1:
|
| 148 |
return twitter_link_filter($text, '#', 'http://search.twitter.com/search?q=%23');
|
| 149 |
default:
|
| 150 |
return $text;
|
| 151 |
}
|
| 152 |
|
| 153 |
default:
|
| 154 |
return $text;
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Implementation of hook_filter_tips().
|
| 160 |
*/
|
| 161 |
function twitter_filter_tips($delta, $format, $long = FALSE) {
|
| 162 |
global $base_url;
|
| 163 |
switch ($delta) {
|
| 164 |
case 0:
|
| 165 |
return t('Twitter-style @usersnames are linked to their Twitter account pages.');
|
| 166 |
|
| 167 |
case 1:
|
| 168 |
return t('Twitter-style #hashtags are linked to !url.', array('!url' => '<a href="http://search.twitter.com/">search.twitter.com</a>'));
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* This helper function converts Twitter-style @usernames and #hashtags into
|
| 174 |
* actual links.
|
| 175 |
*/
|
| 176 |
function twitter_link_filter($text, $prefix = '@', $destination = 'http://twitter.com/') {
|
| 177 |
$matches = array(
|
| 178 |
'/\>' . $prefix . '([a-z0-9_]{0,15})/i',
|
| 179 |
'/^' . $prefix . '([a-z0-9_]{0,15})/i',
|
| 180 |
'/(\s+)' . $prefix . '([a-z0-9_]{0,15})/i',
|
| 181 |
);
|
| 182 |
$replacements = array(
|
| 183 |
'><a href="' . $destination . '${1}">' . $prefix . '${1}</a>',
|
| 184 |
'<a href="' . $destination . '${1}">' . $prefix . '${1}</a>',
|
| 185 |
'${1}<a href="' . $destination . '${2}">' . $prefix . '${2}</a>',
|
| 186 |
);
|
| 187 |
return preg_replace($matches, $replacements, $text);
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
* An implementation of hook_twitter_accounts. We want to move this into a
|
| 192 |
* separate module eventually, but sticking the code here and using a hook
|
| 193 |
* lets other modules solve the 'what accounts can a user post with' problem
|
| 194 |
* in cleaner ways.
|
| 195 |
*/
|
| 196 |
function twitter_twitter_accounts($drupal_user, $full_access = FALSE) {
|
| 197 |
$accounts = array();
|
| 198 |
if (user_access('use global twitter account') &&
|
| 199 |
($name = variable_get('twitter_global_name', NULL)) &&
|
| 200 |
($pass = variable_get('twitter_global_password', NULL))) {
|
| 201 |
|
| 202 |
$accounts[$name] = array(
|
| 203 |
'screen_name' => $name,
|
| 204 |
'password' => $pass,
|
| 205 |
);
|
| 206 |
}
|
| 207 |
|
| 208 |
$sql = "SELECT * FROM {twitter_account} WHERE uid = %d";
|
| 209 |
if ($full_access) {
|
| 210 |
$sql .= " AND password IS NOT NULL";
|
| 211 |
}
|
| 212 |
$args = array($drupal_user->uid);
|
| 213 |
$results = db_query($sql, $args);
|
| 214 |
|
| 215 |
while ($account = db_fetch_array($results)) {
|
| 216 |
$accounts[$account['screen_name']] = $account;
|
| 217 |
}
|
| 218 |
return $accounts;
|
| 219 |
}
|
| 220 |
|
| 221 |
function _twitter_use_oauth() {
|
| 222 |
if (!module_exists('oauth')) {
|
| 223 |
return FALSE;
|
| 224 |
}
|
| 225 |
|
| 226 |
return (variable_get('twitter_consumer_key', '') && variable_get('twitter_consumer_secret', ''));
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Implementation of hook_views_api.
|
| 231 |
* Notifies the Views module that we're compatible with a particular API revision.
|
| 232 |
*/
|
| 233 |
function twitter_views_api() {
|
| 234 |
return array('api' => 2);
|
| 235 |
}
|