| 1 |
<?php |
<?php |
| 2 |
// $Id: openid.module,v 1.13 2007/05/23 22:36:28 walkah Exp $ |
// $Id: openid.module,v 1.2.2.3 2007/05/24 15:01:21 walkah Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 51 |
} |
} |
| 52 |
|
|
| 53 |
/** |
/** |
| 54 |
|
* Implementation of hook_help(). |
| 55 |
|
*/ |
| 56 |
|
function openid_help($section) { |
| 57 |
|
switch ($section) { |
| 58 |
|
case 'user/'. arg(1) .'/openid': |
| 59 |
|
return t('You may login to this site using an OpenID. You may add your OpenId URLs below, and also see a list of any OpenIDs which have already been added.'); |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
/** |
| 64 |
|
* Implementation of hook_user(). |
| 65 |
|
*/ |
| 66 |
|
function openid_user($op, &$edit, &$account, $category = NULL) { |
| 67 |
|
if ($op == 'insert' && isset($_SESSION['openid'])) { |
| 68 |
|
// The user has registered after trying to login via OpenID. |
| 69 |
|
if (variable_get('user_email_verification', TRUE)) { |
| 70 |
|
drupal_set_message(t('Once you have verified your email address, you may log in via OpenID.')); |
| 71 |
|
} |
| 72 |
|
unset($_SESSION['openid']); |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
/** |
| 77 |
* Implementation of hook_form_alter : adds OpenID login to the login forms. |
* Implementation of hook_form_alter : adds OpenID login to the login forms. |
| 78 |
*/ |
*/ |
| 79 |
function openid_form_alter($form_id, &$form) { |
function openid_form_alter($form_id, &$form) { |
| 84 |
if (!empty($form['#post']['openid_url'])) { |
if (!empty($form['#post']['openid_url'])) { |
| 85 |
$form['name']['#required'] = FALSE; |
$form['name']['#required'] = FALSE; |
| 86 |
$form['pass']['#required'] = FALSE; |
$form['pass']['#required'] = FALSE; |
| 87 |
unset($form['#validate']); |
unset($form['#submit']); |
| 88 |
$form['#submit'] = array('openid_login_submit' => array()); |
$form['#validate'] = array('openid_login_validate' => array()); |
| 89 |
} |
} |
| 90 |
|
|
| 91 |
$form['openid_link'] = array( |
$form['openid_link'] = array('#value' => l(t('Log in using OpenID'), '#', array('class' => 'openid-link'))); |
| 92 |
'#value' => l(t('Log in using OpenID'), '#', array('class' => 'openid-link')) |
$form['user_link'] = array('#value' => l(t('Cancel OpenID login'), '#', array('class' => 'user-link'))); |
|
); |
|
| 93 |
|
|
| 94 |
$form['openid_url'] = array( |
$form['openid_url'] = array( |
| 95 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 113 |
// form with the values we have. |
// form with the values we have. |
| 114 |
$form['name']['#default_value'] = $_SESSION['openid']['name']; |
$form['name']['#default_value'] = $_SESSION['openid']['name']; |
| 115 |
$form['mail']['#default_value'] = $_SESSION['openid']['mail']; |
$form['mail']['#default_value'] = $_SESSION['openid']['mail']; |
| 116 |
|
// If user_email_verification is off, hide the password field and just fill |
| 117 |
|
// with random password to avoid confusion. |
| 118 |
|
if (!variable_get('user_email_verification', TRUE)) { |
| 119 |
|
$form['pass']['#type'] = 'hidden'; |
| 120 |
|
$form['pass']['#value'] = user_password(); |
| 121 |
|
} |
| 122 |
$form['auth_openid'] = array('#type' => 'hidden', '#value' => $_SESSION['openid']['auth_openid']); |
$form['auth_openid'] = array('#type' => 'hidden', '#value' => $_SESSION['openid']['auth_openid']); |
| 123 |
} |
} |
| 124 |
return $form; |
return $form; |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
/** |
/** |
| 128 |
* Login form _submit hook |
* Login form _validate hook. |
| 129 |
*/ |
*/ |
| 130 |
function openid_login_submit($form_id, $form_values) { |
function openid_login_validate($form_id, $form_values) { |
| 131 |
$return_to = $form_values['openid.return_to']; |
$return_to = $form_values['openid.return_to']; |
| 132 |
if (empty($return_to)) { |
if (empty($return_to)) { |
| 133 |
$return_to = url('', NULL, NULL, TRUE); |
$return_to = url('', NULL, NULL, TRUE); |
| 141 |
case 'success': |
case 'success': |
| 142 |
return openid_authentication($result); |
return openid_authentication($result); |
| 143 |
case 'failed': |
case 'failed': |
| 144 |
drupal_set_message(t('OpenID login failed.')); |
drupal_set_message(t('OpenID login failed.'), 'error'); |
| 145 |
break; |
break; |
| 146 |
case 'cancel': |
case 'cancel': |
| 147 |
drupal_set_message(t('OpenID login cancelled.')); |
drupal_set_message(t('OpenID login cancelled.')); |
| 160 |
drupal_set_message(t('Successfully added %identity', array('%identity' => $result['openid.identity']))); |
drupal_set_message(t('Successfully added %identity', array('%identity' => $result['openid.identity']))); |
| 161 |
} |
} |
| 162 |
|
|
| 163 |
$header = array(t('OpenID'), ''); |
$header = array(t('OpenID'), t('Operations')); |
| 164 |
$rows = array(); |
$rows = array(); |
| 165 |
|
|
| 166 |
$result = db_query("SELECT * FROM {authmap} WHERE module='openid' AND uid=%d", $account->uid); |
$result = db_query("SELECT * FROM {authmap} WHERE module='openid' AND uid=%d", $account->uid); |
| 182 |
return $form; |
return $form; |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
function openid_user_add_submit($form_id, $form_values) { |
function openid_user_add_validate($form_id, $form_values) { |
| 186 |
$return_to = url('user/'. arg(1) .'/openid', NULL, NULL, TRUE); |
// Check for existing entries. |
| 187 |
return openid_begin($form_values['openid_url'], $return_to); |
$claimed_id = _openid_normalize($form_values['openid_url']); |
| 188 |
|
if (db_result(db_query("SELECT authname FROM {authmap} WHERE authname='%s'", $claimed_id))) { |
| 189 |
|
form_set_error('openid_url', t('That OpenID is already in use on this site.')); |
| 190 |
|
} |
| 191 |
|
else { |
| 192 |
|
$return_to = url('user/'. arg(1) .'/openid', NULL, NULL, TRUE); |
| 193 |
|
openid_begin($form_values['openid_url'], $return_to); |
| 194 |
|
} |
| 195 |
} |
} |
| 196 |
|
|
| 197 |
function openid_user_delete($account, $aid = 0) { |
function openid_user_delete($account, $aid = 0) { |
| 341 |
$version = 2; |
$version = 2; |
| 342 |
|
|
| 343 |
// 1.0 links |
// 1.0 links |
| 344 |
$uri = _openid_link_href('openid.server', $result->data); |
if (empty($uri)) { |
| 345 |
$delegate = _openid_link_href('openid.delegate', $result->data); |
$uri = _openid_link_href('openid.server', $result->data); |
| 346 |
$version = 1; |
$delegate = _openid_link_href('openid.delegate', $result->data); |
| 347 |
|
$version = 1; |
| 348 |
|
} |
| 349 |
if (!empty($uri)) { |
if (!empty($uri)) { |
| 350 |
$services[] = array('uri' => $uri, 'delegate' => $delegate, 'version' => $version); |
$services[] = array('uri' => $uri, 'delegate' => $delegate, 'version' => $version); |
| 351 |
} |
} |
| 418 |
|
|
| 419 |
$account = user_external_load($identity); |
$account = user_external_load($identity); |
| 420 |
if (isset($account->uid)) { |
if (isset($account->uid)) { |
| 421 |
global $user; |
if (!variable_get('user_email_verification', TRUE) || $account->login) { |
| 422 |
$user = $account; |
global $user; |
| 423 |
|
$user = $account; |
| 424 |
|
user_login_submit('user_login', array()); |
| 425 |
|
} |
| 426 |
|
else { |
| 427 |
|
drupal_set_message(t('You must validate your email address for this account before logging in via OpenID')); |
| 428 |
|
} |
| 429 |
} |
} |
| 430 |
else { |
else { |
| 431 |
// Register new user |
// Register new user |
| 433 |
$edit['name'] = (empty($response['openid.sreg.nickname'])) ? $identity : $response['openid.sreg.nickname']; |
$edit['name'] = (empty($response['openid.sreg.nickname'])) ? $identity : $response['openid.sreg.nickname']; |
| 434 |
$edit['mail'] = (empty($response['openid.sreg.email'])) ? '' : $response['openid.sreg.email']; |
$edit['mail'] = (empty($response['openid.sreg.email'])) ? '' : $response['openid.sreg.email']; |
| 435 |
$edit['pass'] = user_password(); |
$edit['pass'] = user_password(); |
| 436 |
$edit['status'] = 1; |
$edit['status'] = variable_get('user_register', 1) == 1; |
| 437 |
|
$edit['response'] = $response; |
| 438 |
$edit['auth_openid'] = $identity; |
$edit['auth_openid'] = $identity; |
| 439 |
$form['#post'] = $edit; |
$form['#post'] = $edit; |
| 440 |
drupal_prepare_form('user_register', $form); |
drupal_prepare_form('user_register', $form); |
| 442 |
if (form_get_errors()) { |
if (form_get_errors()) { |
| 443 |
// We were unable to register a valid new user, redirect to standard |
// We were unable to register a valid new user, redirect to standard |
| 444 |
// user/register and prefill with the values we received. |
// user/register and prefill with the values we received. |
| 445 |
$_SESSION['openid'] = $edit; |
drupal_set_message(t('OpenID registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and add your OpenID under "My Account"', array('@login' => url('user/login'))), 'error'); |
| 446 |
drupal_goto('user/register'); |
$_SESSION['openid'] = $edit; |
| 447 |
|
// We'll want to redirect back to the same place. |
| 448 |
|
$destination = drupal_get_destination(); |
| 449 |
|
unset($_REQUEST['destination']); |
| 450 |
|
drupal_goto('user/register', $destination); |
| 451 |
} |
} |
| 452 |
else { |
else { |
| 453 |
user_save('', $edit); |
unset($edit['response']); |
| 454 |
|
$account = user_save('', $edit); |
| 455 |
|
global $user; |
| 456 |
|
$user = $account; |
| 457 |
|
user_login_submit('user_login', array()); |
| 458 |
} |
} |
| 459 |
} |
} |
| 460 |
drupal_goto(); |
drupal_goto(); |
| 487 |
$realm = $base_url; |
$realm = $base_url; |
| 488 |
} |
} |
| 489 |
|
|
| 490 |
$ns = ($version == 2) ? OPENID_NS_2_0 : OPENID_NS_1_1; |
$ns = ($version == 2) ? OPENID_NS_2_0 : OPENID_NS_1_0; |
| 491 |
$request = array( |
$request = array( |
| 492 |
'openid.ns' => $ns, |
'openid.ns' => $ns, |
| 493 |
'openid.mode' => 'checkid_setup', |
'openid.mode' => 'checkid_setup', |