| 1 |
<?php
|
| 2 |
// $Id: twitter.pages.inc,v 1.4.2.2 2009/06/11 02:50:07 walkah Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
*
|
| 6 |
*/
|
| 7 |
function twitter_admin_form() {
|
| 8 |
$form = array();
|
| 9 |
|
| 10 |
$form['oauth'] = array(
|
| 11 |
'#type' => 'fieldset',
|
| 12 |
'#title' => t('OAuth Settings'),
|
| 13 |
'#description' => t(''),
|
| 14 |
'#access' => module_exists('oauth'),
|
| 15 |
);
|
| 16 |
|
| 17 |
$form['oauth']['twitter_consumer_key'] = array(
|
| 18 |
'#type' => 'textfield',
|
| 19 |
'#title' => t('OAuth Consumer key'),
|
| 20 |
'#default_value' => variable_get('twitter_consumer_key', NULL),
|
| 21 |
);
|
| 22 |
|
| 23 |
$form['oauth']['twitter_consumer_secret'] = array(
|
| 24 |
'#type' => 'textfield',
|
| 25 |
'#title' => t('OAuth Consumer secret'),
|
| 26 |
'#default_value' => variable_get('twitter_consumer_secret', NULL),
|
| 27 |
);
|
| 28 |
|
| 29 |
$form['global_account'] = array(
|
| 30 |
'#type' => 'fieldset',
|
| 31 |
'#title' => t('Global account'),
|
| 32 |
'#description' => t('A site-wide Twitter account to use as the default when tweets are posted. This is useful for single-user blogs or sites where many users post to a single shared Twitter account.'),
|
| 33 |
);
|
| 34 |
|
| 35 |
$form['global_account']['twitter_global_name'] = array(
|
| 36 |
'#type' => 'textfield',
|
| 37 |
'#title' => t('Twitter user name'),
|
| 38 |
'#default_value' => variable_get('twitter_global_name', NULL),
|
| 39 |
);
|
| 40 |
|
| 41 |
$form['global_account']['twitter_global_password'] = array(
|
| 42 |
'#type' => 'password',
|
| 43 |
'#title' => t('Password'),
|
| 44 |
'#description' => t("If your Twitter account is protected, or you wish to post to Twitter from Drupal, you must enter the Twitter account's password."),
|
| 45 |
'#default_value' => variable_get('twitter_global_password', NULL),
|
| 46 |
);
|
| 47 |
|
| 48 |
$form['import'] = array(
|
| 49 |
'#type' => 'fieldset',
|
| 50 |
'#title' => t('Twitter import'),
|
| 51 |
'#description' => t('Import and display the Twitter statuses of site users who have entered their Twitter account information.'),
|
| 52 |
);
|
| 53 |
|
| 54 |
$form['import']['twitter_import'] = array(
|
| 55 |
'#type' => 'checkbox',
|
| 56 |
'#title' => t('Import Twitter statuses'),
|
| 57 |
'#default_value' => variable_get('twitter_import', TRUE),
|
| 58 |
);
|
| 59 |
|
| 60 |
$periods = array(0 => t('Never'));
|
| 61 |
$periods += drupal_map_assoc(array(604800, 2419200, 7257600, 31449600), 'format_interval');
|
| 62 |
$form['import']['twitter_expire'] = array(
|
| 63 |
'#type' => 'select',
|
| 64 |
'#title' => t('Delete old statuses'),
|
| 65 |
'#default_value' => variable_get('amazon_refresh_schedule', 0),
|
| 66 |
'#options' => $periods
|
| 67 |
);
|
| 68 |
|
| 69 |
return system_settings_form($form);
|
| 70 |
}
|
| 71 |
|
| 72 |
function twitter_user_settings($account) {
|
| 73 |
module_load_include('inc', 'twitter');
|
| 74 |
|
| 75 |
$output = '';
|
| 76 |
// This is directly calling a hook implementation, which is bad and naughty,
|
| 77 |
// but oh well. We'll fix this in the next reshuffling when user account
|
| 78 |
// management gets an overhaul.
|
| 79 |
$twitter_accounts = twitter_twitter_accounts($account);
|
| 80 |
if (!empty($twitter_accounts)) {
|
| 81 |
$output .= drupal_get_form('twitter_account_list_form', $twitter_accounts);
|
| 82 |
}
|
| 83 |
$output .= drupal_get_form('twitter_account_form', $account);
|
| 84 |
|
| 85 |
return $output;
|
| 86 |
}
|
| 87 |
|
| 88 |
function twitter_account_list_form($form_state, $twitter_accounts = array()) {
|
| 89 |
$form['#tree'] = TRUE;
|
| 90 |
$form['accounts'] = array();
|
| 91 |
|
| 92 |
foreach ($twitter_accounts as $twitter_account) {
|
| 93 |
$form['accounts'][] = _twitter_account_list_row($twitter_account);
|
| 94 |
}
|
| 95 |
|
| 96 |
if (!empty($twitter_accounts)) {
|
| 97 |
$form['buttons']['submit'] = array(
|
| 98 |
'#type' => 'submit',
|
| 99 |
'#value' => t('Save changes'),
|
| 100 |
);
|
| 101 |
}
|
| 102 |
|
| 103 |
return $form;
|
| 104 |
}
|
| 105 |
|
| 106 |
function _twitter_account_list_row($account) {
|
| 107 |
$form['#account'] = $account;
|
| 108 |
|
| 109 |
$form['uid'] = array(
|
| 110 |
'#type' => 'value',
|
| 111 |
'#value' => $account['uid'],
|
| 112 |
);
|
| 113 |
|
| 114 |
$form['screen_name'] = array(
|
| 115 |
'#type' => 'value',
|
| 116 |
'#value' => $account['screen_name'],
|
| 117 |
);
|
| 118 |
|
| 119 |
$form['image'] = array(
|
| 120 |
'#type' => 'markup',
|
| 121 |
'#value' => theme('image', $account['profile_image_url'], '', '', array(), FALSE),
|
| 122 |
);
|
| 123 |
|
| 124 |
$form['visible_name'] = array(
|
| 125 |
'#type' => 'markup',
|
| 126 |
'#value' => l($account['screen_name'], 'http://www.twitter.com/' . $account['screen_name']),
|
| 127 |
);
|
| 128 |
|
| 129 |
$form['description'] = array(
|
| 130 |
'#type' => 'markup',
|
| 131 |
'#value' => filter_xss($account['description']),
|
| 132 |
);
|
| 133 |
|
| 134 |
$form['protected'] = array(
|
| 135 |
'#type' => 'markup',
|
| 136 |
'#value' => empty($account['protected']) ? t('No') : t('Yes'),
|
| 137 |
);
|
| 138 |
|
| 139 |
$form['import'] = array(
|
| 140 |
'#type' => 'checkbox',
|
| 141 |
'#default_value' => $account['import'],
|
| 142 |
);
|
| 143 |
|
| 144 |
$form['delete'] = array(
|
| 145 |
'#type' => 'checkbox',
|
| 146 |
);
|
| 147 |
return $form;
|
| 148 |
}
|
| 149 |
|
| 150 |
function theme_twitter_account_list_form($form) {
|
| 151 |
$header = array('', t('Name'), t('Description'), t('Private'), t('Import'), t('Delete'));
|
| 152 |
$rows = array();
|
| 153 |
|
| 154 |
foreach (element_children($form['accounts']) as $key) {
|
| 155 |
$element = &$form['accounts'][$key];
|
| 156 |
$rows[] = array(
|
| 157 |
drupal_render($element['image']),
|
| 158 |
drupal_render($element['uid']) . drupal_render($element['screen_name']) . drupal_render($element['visible_name']),
|
| 159 |
drupal_render($element['description']),
|
| 160 |
drupal_render($element['protected']),
|
| 161 |
drupal_render($element['import']),
|
| 162 |
drupal_render($element['delete']),
|
| 163 |
);
|
| 164 |
}
|
| 165 |
|
| 166 |
$output = theme('table', $header, $rows);
|
| 167 |
$output .= drupal_render($form);
|
| 168 |
return $output;
|
| 169 |
}
|
| 170 |
|
| 171 |
function twitter_account_list_form_submit($form, &$form_state) {
|
| 172 |
$accounts = $form_state['values']['accounts'];
|
| 173 |
foreach($accounts as $account) {
|
| 174 |
if (empty($account['delete'])) {
|
| 175 |
twitter_account_save($account);
|
| 176 |
}
|
| 177 |
else {
|
| 178 |
twitter_user_delete($account['uid'], $account['screen_name']);
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
function twitter_account_form($form_state, $account = NULL) {
|
| 184 |
if (empty($account)) {
|
| 185 |
global $user;
|
| 186 |
$account = $user;
|
| 187 |
}
|
| 188 |
|
| 189 |
$form['uid'] = array(
|
| 190 |
'#type' => 'value',
|
| 191 |
'#value' => $account->uid,
|
| 192 |
);
|
| 193 |
|
| 194 |
if (_twitter_use_oauth()) {
|
| 195 |
$form['#validate'] = array('twitter_account_oauth_validate');
|
| 196 |
}
|
| 197 |
else {
|
| 198 |
$form['screen_name'] = array(
|
| 199 |
'#type' => 'textfield',
|
| 200 |
'#required' => TRUE,
|
| 201 |
'#title' => t('Twitter user name'),
|
| 202 |
);
|
| 203 |
$form['password'] = array(
|
| 204 |
'#type' => 'password',
|
| 205 |
'#title' => t('Password'),
|
| 206 |
'#description' => t("If your Twitter account is protected, or you wish to post to Twitter from Drupal, you must enter the Twitter account's password.")
|
| 207 |
);
|
| 208 |
|
| 209 |
$form['import'] = array(
|
| 210 |
'#type' => 'checkbox',
|
| 211 |
'#title' => t('Import statuses from this account'),
|
| 212 |
'#default_value' => TRUE,
|
| 213 |
'#access' => FALSE,
|
| 214 |
);
|
| 215 |
|
| 216 |
}
|
| 217 |
|
| 218 |
$form['submit'] = array(
|
| 219 |
'#type' => 'submit',
|
| 220 |
'#value' => t('Add account'),
|
| 221 |
);
|
| 222 |
|
| 223 |
return $form;
|
| 224 |
}
|
| 225 |
|
| 226 |
function twitter_account_form_validate($form, &$form_state) {
|
| 227 |
module_load_include('lib.php', 'twitter');
|
| 228 |
module_load_include('inc', 'twitter');
|
| 229 |
|
| 230 |
$verify = FALSE;
|
| 231 |
|
| 232 |
$pass = $form_state['values']['password'];
|
| 233 |
$name = $form_state['values']['screen_name'];
|
| 234 |
|
| 235 |
if (!empty($pass)) {
|
| 236 |
$verify = TRUE;
|
| 237 |
}
|
| 238 |
|
| 239 |
if ($verify) {
|
| 240 |
$twitter = new Twitter($name, $pass);
|
| 241 |
$account = $twitter->verify_credentials();
|
| 242 |
if (!$account) {
|
| 243 |
form_set_error("password", t('Twitter authentication failed. Please check your account name and try again.'));
|
| 244 |
}
|
| 245 |
$form_state['twitter_account'] = $account;
|
| 246 |
}
|
| 247 |
}
|
| 248 |
|
| 249 |
function twitter_account_form_submit($form, &$form_state) {
|
| 250 |
module_load_include('inc', 'twitter');
|
| 251 |
|
| 252 |
if (!empty($form_state['values']['screen_name'])) {
|
| 253 |
$account = $form_state['twitter_account'];
|
| 254 |
$account->set_auth($form_state['values']);
|
| 255 |
twitter_account_save($account, TRUE);
|
| 256 |
}
|
| 257 |
}
|
| 258 |
|
| 259 |
function twitter_account_oauth_validate($form, &$form_state) {
|
| 260 |
module_load_include('lib.php', 'oauth');
|
| 261 |
module_load_include('lib.php', 'twitter');
|
| 262 |
|
| 263 |
$key = variable_get('twitter_consumer_key', '');
|
| 264 |
$secret = variable_get('twitter_consumer_secret', '');
|
| 265 |
$twitter = new TwitterOAuth($key, $secret);
|
| 266 |
$token = $twitter->get_request_token();
|
| 267 |
|
| 268 |
$_SESSION['twitter_oauth']['token'] = $token;
|
| 269 |
$_SESSION['twitter_oauth']['destination'] = $_GET['q'];
|
| 270 |
drupal_goto($twitter->get_authorize_url($token));
|
| 271 |
}
|
| 272 |
|
| 273 |
|
| 274 |
function twitter_oauth_callback(&$form_state) {
|
| 275 |
$form['#post']['oauth_token'] = $_GET['oauth_token'];
|
| 276 |
|
| 277 |
$form['oauth_token'] = array(
|
| 278 |
'#type' => 'hidden',
|
| 279 |
'#default_value' => $_GET['oauth_token'],
|
| 280 |
);
|
| 281 |
|
| 282 |
return $form;
|
| 283 |
}
|
| 284 |
|
| 285 |
function twitter_oauth_callback_validate($form, &$form_state) {
|
| 286 |
$key = variable_get('twitter_consumer_key', '');
|
| 287 |
$secret = variable_get('twitter_consumer_secret', '');
|
| 288 |
|
| 289 |
$form_state['twitter_oauth'] = $_SESSION['twitter_oauth'];
|
| 290 |
unset($_SESSION['twitter_oauth']);
|
| 291 |
|
| 292 |
$token = $form_state['twitter_oauth']['token'];
|
| 293 |
if (!is_array($token) || !$key || !$secret) {
|
| 294 |
form_set_error('oauth_token', t('Invalid Twitter OAuth request'));
|
| 295 |
}
|
| 296 |
|
| 297 |
if ($token['oauth_token'] != $form_state['values']['oauth_token']) {
|
| 298 |
form_set_error('oauth_token', t('Invalid OAuth token.'));
|
| 299 |
}
|
| 300 |
|
| 301 |
module_load_include('lib.php', 'oauth');
|
| 302 |
module_load_include('lib.php', 'twitter');
|
| 303 |
module_load_include('inc', 'twitter');
|
| 304 |
|
| 305 |
$twitter = new TwitterOAuth($key, $secret, $token['oauth_token'], $token['oauth_token_secret']);
|
| 306 |
$response = $twitter->get_access_token();
|
| 307 |
$form_state['twitter_oauth']['response'] = $response;
|
| 308 |
}
|
| 309 |
|
| 310 |
function twitter_oauth_callback_submit(&$form, &$form_state) {
|
| 311 |
$key = variable_get('twitter_consumer_key', '');
|
| 312 |
$secret = variable_get('twitter_consumer_secret', '');
|
| 313 |
$response = $form_state['twitter_oauth']['response'];
|
| 314 |
|
| 315 |
$twitter = new TwitterOAuth($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
|
| 316 |
$account = $twitter->users_show($response['screen_name']);
|
| 317 |
$account->set_auth($response);
|
| 318 |
twitter_account_save($account, TRUE);
|
| 319 |
$form_state['redirect'] = $form_state['twitter_oauth']['destination'];
|
| 320 |
$form['#programmed'] = FALSE;
|
| 321 |
}
|