| 1 |
<?php
|
| 2 |
// $Id: twitter_signin.module,v 1.1.2.1 2009/06/11 02:57:55 walkah Exp $
|
| 3 |
|
| 4 |
function twitter_signin_menu() {
|
| 5 |
$items = array();
|
| 6 |
|
| 7 |
$items['admin/settings/twitter/signin'] = array(
|
| 8 |
'title' => 'Twitter Signin Settings',
|
| 9 |
'page callback' => 'drupal_get_form',
|
| 10 |
'page arguments' => array('twitter_signin_admin_settings'),
|
| 11 |
'file' => 'twitter_signin.pages.inc',
|
| 12 |
'type' => MENU_LOCAL_TASK,
|
| 13 |
);
|
| 14 |
|
| 15 |
return $items;
|
| 16 |
}
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_block().
|
| 20 |
*/
|
| 21 |
function twitter_signin_block($op = 'list', $delta = 0, $edit = array()) {
|
| 22 |
switch ($op) {
|
| 23 |
case 'list':
|
| 24 |
$block[0]['info'] = t('Sign in with Twitter');
|
| 25 |
return $block;
|
| 26 |
case 'view':
|
| 27 |
$block['subject'] = t('Sign in with Twitter');
|
| 28 |
$block['content'] = drupal_get_form('twitter_signin_form');
|
| 29 |
return $block;
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Main form for twitter signin
|
| 35 |
*/
|
| 36 |
function twitter_signin_form(&$form_state) {
|
| 37 |
$form = array();
|
| 38 |
|
| 39 |
$form['signin'] = array(
|
| 40 |
'#type' => 'image_button',
|
| 41 |
'#src' => drupal_get_path('module', 'twitter_signin') .'/images/Sign-in-with-Twitter-lighter-small.png',
|
| 42 |
|
| 43 |
);
|
| 44 |
|
| 45 |
return $form;
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Submit handler for twitter signin
|
| 50 |
*/
|
| 51 |
function twitter_signin_form_validate(&$form, &$form_state) {
|
| 52 |
module_load_include('lib.php', 'oauth');
|
| 53 |
module_load_include('lib.php', 'twitter');
|
| 54 |
module_load_include('inc', 'twitter');
|
| 55 |
|
| 56 |
$token = $_SESSION['twitter_oauth'];
|
| 57 |
$key = variable_get('twitter_consumer_key', '');
|
| 58 |
$secret = variable_get('twitter_consumer_secret', '');
|
| 59 |
$twitter = new TwitterOAuth($key, $secret);
|
| 60 |
$token = $twitter->get_request_token();
|
| 61 |
|
| 62 |
$_SESSION['twitter_oauth']['token'] = $token;
|
| 63 |
$_SESSION['twitter_oauth']['destination'] = $_GET['q'];
|
| 64 |
$_SESSION['twitter_oauth']['signin'] = TRUE;
|
| 65 |
drupal_goto($twitter->get_authenticate_url($token));
|
| 66 |
}
|
| 67 |
|
| 68 |
function twitter_signin_form_alter(&$form, $form_state, $form_id) {
|
| 69 |
if ($form_id == 'twitter_oauth_callback' && $_SESSION['twitter_oauth']['signin']) {
|
| 70 |
$submit = $form['#submit'];
|
| 71 |
$form['#submit'] = array('twitter_signin_oauth_callback_submit');
|
| 72 |
$form['#submit'] += $submit;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
function twitter_signin_oauth_callback_submit(&$form, &$form_state) {
|
| 77 |
global $user;
|
| 78 |
|
| 79 |
$key = variable_get('twitter_consumer_key', '');
|
| 80 |
$secret = variable_get('twitter_consumer_secret', '');
|
| 81 |
$response = $form_state['twitter_oauth']['response'];
|
| 82 |
|
| 83 |
$account = user_external_load($response['user_id']);
|
| 84 |
if (isset($account->uid)) {
|
| 85 |
user_external_login($account, $response);
|
| 86 |
}
|
| 87 |
elseif ($uid = db_result(db_query("SELECT uid FROM {twitter_account} WHERE twitter_uid= %d", $response['user_id']))) {
|
| 88 |
// We have an existing twitter account - set it up for login
|
| 89 |
$account = user_load($uid);
|
| 90 |
$edit["authname_twitter"] = $response['user_id'];
|
| 91 |
user_save($account, $edit);
|
| 92 |
$user = $account;
|
| 93 |
}
|
| 94 |
else {
|
| 95 |
$edit = array(
|
| 96 |
'name' => $response['screen_name'],
|
| 97 |
'pass' => user_password(),
|
| 98 |
'init' => $response['screen_name'],
|
| 99 |
'status' => 1,
|
| 100 |
"authname_twitter" => $response['user_id'],
|
| 101 |
'access' => time(),
|
| 102 |
);
|
| 103 |
$account = user_save('', $edit);
|
| 104 |
$user = $account;
|
| 105 |
}
|
| 106 |
|
| 107 |
}
|