View source
<?php
function twitter_signin_menu() {
$items = array();
$items['twitter/redirect'] = array(
'title' => 'Twitter Redirect',
'page callback' => 'twitter_signin_redirect',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/settings/twitter/signin'] = array(
'title' => 'Sign-in',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'twitter_signin_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'twitter_signin.pages.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
return $items;
}
function twitter_signin_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Sign in with Twitter');
return $block;
case 'view':
global $user;
if (!$user->uid && _twitter_use_oauth()) {
$block['subject'] = t('Sign in with Twitter');
$block['content'] = twitter_signin_button();
return $block;
}
}
}
function twitter_signin_button() {
return theme('twitter_signin_button');
}
function twitter_signin_theme() {
return array(
'twitter_signin_button' => array(),
);
}
function theme_twitter_signin_button() {
$img_path = drupal_get_path('module', 'twitter_signin') . '/images/';
$img = $img_path . variable_get('twitter_signin_button', 'Sign-in-with-Twitter-lighter-small.png');
return l(theme('image', $img, t('Sign in with Twitter')), 'twitter/redirect', array(
'html' => TRUE,
));
}
function twitter_signin_redirect() {
module_load_include('php', 'oauth_common', 'lib/OAuth');
module_load_include('inc', 'twitter');
$key = variable_get('twitter_consumer_key', '');
$secret = variable_get('twitter_consumer_secret', '');
$twitter = new TwitterOAuth($key, $secret);
$token = $twitter
->get_request_token();
$_SESSION['twitter_oauth']['token'] = $token;
$_SESSION['twitter_oauth']['destination'] = referer_uri();
$_SESSION['twitter_oauth']['signin'] = TRUE;
drupal_goto($twitter
->get_authenticate_url($token));
}
function twitter_signin_form_alter(&$form, $form_state, $form_id) {
if (!_twitter_use_oauth()) {
return;
}
if ($form_id == 'twitter_oauth_callback' && isset($_SESSION['twitter_oauth']['signin'])) {
$form['#submit'] = array_merge(array(
'twitter_signin_oauth_callback_submit',
), $form['#submit']);
}
if ($form_id == 'user_login' || $form_id == 'user_login_block') {
$items = array();
$items[] = twitter_signin_button();
$form['twitter_signin'] = array(
'#value' => theme('item_list', $items),
);
}
elseif ($form_id == 'user_register' && isset($_SESSION['twitter']['values'])) {
$form['name']['#default_value'] = $_SESSION['twitter']['values']['screen_name'];
$form['auth_twitter'] = array(
'#type' => 'hidden',
'#value' => $_SESSION['twitter']['values']['user_id'],
);
}
}
function twitter_signin_oauth_callback_submit(&$form, &$form_state) {
global $user;
$success = FALSE;
$key = variable_get('twitter_consumer_key', '');
$secret = variable_get('twitter_consumer_secret', '');
$response = $form_state['twitter_oauth']['response'];
$account = user_external_load($response['user_id']);
if (isset($account->uid)) {
user_external_login($account, $response);
$form_state['twitter_oauth']['account'] = $account;
$success = TRUE;
}
elseif ($uid = db_result(db_query("SELECT uid FROM {twitter_account} WHERE twitter_uid= %d", $response['user_id']))) {
$account = user_load($uid);
$edit["authname_twitter"] = $response['user_id'];
user_save($account, $edit);
$user = $account;
$form_state['twitter_oauth']['account'] = $account;
$success = TRUE;
}
else {
if (variable_get('twitter_signin_register', 0)) {
$account = user_load(array(
'name' => $response['screen_name'],
));
if (!$account->uid) {
$edit = array(
'name' => $response['screen_name'],
'pass' => user_password(),
'init' => $response['screen_name'],
'status' => 1,
"authname_twitter" => $response['user_id'],
'access' => time(),
);
$account = user_save('', $edit);
$user = $account;
$success = TRUE;
}
else {
drupal_set_message(t('The nickname %name is already in use on this site, please register below with a new nick name, or @login to continue.', array(
'%name' => $response['screen_name'],
'@login' => url('user/login'),
)), 'warning');
}
}
else {
drupal_set_message(t('Please complete the following registration form to create your new account on %site', array(
'%site' => variable_get('site_name', ''),
)));
}
}
if (!$success) {
$_SESSION['twitter']['values'] = $response;
drupal_goto('user/register');
}
}
function twitter_signin_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
case 'login':
if (isset($_SESSION['twitter']['values'])) {
module_load_include('lib.php', 'oauth');
module_load_include('inc', 'twitter');
$key = variable_get('twitter_consumer_key', '');
$secret = variable_get('twitter_consumer_secret', '');
$response = $_SESSION['twitter']['values'];
$twitter = new TwitterOAuth($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
$twitter_account = $twitter
->users_show($response['screen_name']);
$twitter_account
->set_auth($response);
twitter_account_save($twitter_account, TRUE, $account);
unset($_SESSION['twitter']);
drupal_set_message(t('You have related a Twitter account with your user. Next time you can sign in with Twitter.'));
}
break;
}
}