janrain_capture.pages.inc in Janrain Registration 6
Same filename and directory in other branches
User page callbacks for the janrain_capture module.
File
janrain_capture.pages.incView source
<?php
/**
* @file
* User page callbacks for the janrain_capture module.
*/
/**
* Callback for the janrain_capture/oauth menu item. This serves as the
* redirect_uri Capture redirects the user to and performs the authentication.
*/
function janrain_capture_oauth() {
global $user;
$token = isset($_REQUEST['code']) ? $_REQUEST['code'] : '';
$origin = isset($_REQUEST['origin']) ? $_REQUEST['origin'] : '';
$janrain_capture_fields = variable_get('janrain_capture_fields', array());
$janrain_capture_main = variable_get('janrain_capture_main', array());
$janrain_capture_optional = variable_get('janrain_capture_optional', array());
if ($token) {
// Cannot use the Drupal url query option because drupal_http_build_query
// decodes slashes causing incompatibility.
$redirect_uri = url('janrain_capture/oauth', array(
'absolute' => TRUE,
));
if ($origin) {
$redirect_uri .= (strpos($redirect_uri, '?') !== FALSE ? '&' : '?') . 'origin=' . urlencode($origin);
}
$api = new JanrainCaptureApi();
if ($api
->newAccessToken($token, $redirect_uri) == FALSE) {
$profile = NULL;
}
else {
$profile = $api
->loadUserEntity();
}
if (!$profile || $profile['stat'] != 'ok') {
drupal_set_message(t('We were unable to complete your request.'), 'error');
watchdog('janrain_capture', 'Failed to obtain a Capture record', array(), WATCHDOG_ERROR);
}
else {
$_SESSION['janrain_capture_email'] = $profile['result']['email'];
$user_info = array(
'name' => janrain_capture_profile_field($profile),
'mail' => $profile['result']['email'],
'status' => 1,
);
$user_info = janrain_capture_fields_array($user_info, $profile['result']);
$account = user_external_load($profile['result']['uuid']);
$new_user = FALSE;
// No user was found with our Capture uuid.
if (!$account->uid) {
// Look for a local user with the same email address.
if ($local_user = user_load(array(
'mail' => $profile['result']['email'],
))) {
// Are we configured to match users based on email?
if (isset($janrain_capture_fields['capture_match_email']) && $janrain_capture_fields['capture_match_email']) {
// Check to see if this user is already mapped to a Capture uuid.
$result = db_query("SELECT aid FROM {authmap} WHERE module = 'janrain_capture' AND uid = %d", $local_user->uid);
if (db_fetch_array($result)) {
$mapped_hook = module_invoke_all('janrain_capture_user_already_mapped');
if (empty($mapped_hook) || !in_array(FALSE, $mapped_hook)) {
drupal_set_message(t('A user with this email address is already mapped.'), 'error');
}
}
else {
$user_info['auth_janrain_capture'] = $profile['result']['uuid'];
$account = user_save($local_user, $user_info);
if (!$account->uid) {
$mapping_failed_hook = module_invoke_all('janrain_capture_mapping_failed');
if (empty($mapping_failed_hook) || !in_array(FALSE, $mapping_failed_hook)) {
drupal_set_message(t('Failed to map to existing user.'), 'error');
}
}
}
}
else {
$user_exists_hook = module_invoke_all('janrain_capture_user_exists');
if (empty($user_exists_hook) || !in_array(FALSE, $user_exists_hook)) {
drupal_set_message(t('A user with this email address already exists.'), 'error');
}
}
}
else {
$user_info['pass'] = user_password();
$user_info['auth_janrain_capture'] = $profile['result']['uuid'];
$account = user_save(NULL, $user_info);
$new_user = TRUE;
if (!$account->uid) {
$failed_create = module_invoke_all('janrain_capture_failed_create');
if (empty($failed_create) || !in_array(FALSE, $failed_create)) {
drupal_set_message(t('Failed to create new user.'), 'error');
}
}
}
}
else {
$account = user_save($account, $user_info);
}
$signin = TRUE;
if (!$account->uid) {
$signin = FALSE;
}
if (isset($_SESSION['janrain_capture_action']) && ($_SESSION['janrain_capture_action'] == 'finish_third_party' || $_SESSION['janrain_capture_action'] == 'legacy_register') && isset($janrain_capture_fields['capture_enforce_verification']) && $janrain_capture_fields['capture_enforce_verification'] && $profile['result']['emailVerified'] == NULL) {
$signin = FALSE;
if (isset($_SESSION['janrain_capture_email'])) {
drupal_set_message(t('A verification link has been sent to @email. Please check your email.', array(
'@email' => $_SESSION['janrain_capture_email'],
)), 'status');
}
else {
drupal_set_message(t('A verification link has been sent. Please check your email.'), 'status');
}
}
elseif (isset($janrain_capture_fields['capture_enforce_verification']) && $janrain_capture_fields['capture_enforce_verification'] && $profile['result']['emailVerified'] == NULL) {
$signin = FALSE;
$args = array(
'action' => 'resend_verification_email',
'access_token' => $_SESSION['janrain_capture_access_token'],
'redirect_uri' => url('janrain_capture/resend_verification_email', array(
'absolute' => TRUE,
)),
);
$resend_link = janrain_capture_url($args);
$email_unverified = module_invoke_all('janrain_capture_email_unverified', $resend_link);
if (empty($email_unverified) || !in_array(FALSE, $email_unverified)) {
drupal_set_message(t('Your email address has not yet been verified. Please check your email and try again. <a href="@resend-link">Click here</a> to have this email resent.', array(
'@resend-link' => $resend_link,
)), 'error');
}
}
if ($signin) {
user_external_login($account);
module_invoke_all('janrain_capture_user_authenticated', $profile['result'], $account, $new_user);
}
}
}
else {
$no_oauth = module_invoke_all('janrain_capture_no_oauth');
if (empty($no_oauth) || !in_array(FALSE, $no_oauth)) {
drupal_set_message(t('No Oauth token found!'), 'error');
}
}
$front_page = url('<front>', array(
'absolute' => TRUE,
));
$output = <<<OAUTH_END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<p>Please wait...</p>
<script type="text/javascript">
if (window.location.href != window.parent.location.href) {
if (window.parent.location.href.indexOf("logout") > 1) {
window.parent.location.href = "{<span class="php-variable">$front_page</span>}";
} else {
window.parent.location.reload();
}
} else {
window.location.href = "{<span class="php-variable">$front_page</span>}";
}
</script>
</body>
</html>
OAUTH_END;
print $output;
return NULL;
}
/**
* Callback for the janrain_capture/profile menu item. Ensures the access_token
* is valid before redirecting the user to the Capture profile screen.
*/
function janrain_capture_profile() {
$method = isset($_GET['method']) ? $_GET['method'] : '';
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'CAPTURE.closeProfileEditor';
$janrain_capture_main = variable_get('janrain_capture_main', array());
$janrain_capture_optional = variable_get('janrain_capture_optional', array());
$redirect_uri = url('janrain_capture/oauth', array(
'absolute' => TRUE,
));
if (REQUEST_TIME >= $_SESSION['janrain_capture_expires_in']) {
$api = new JanrainCaptureApi();
$api
->refreshAccessToken();
}
$args = array(
'action' => 'profile' . $method,
'access_token' => $_SESSION['janrain_capture_access_token'],
'callback' => $callback,
);
$url = janrain_capture_url($args);
drupal_goto($url);
}
/**
* Callback for the janrain_capture/profile_sync menu item. Retrieves
* the most recent data from Capture and stores values locally.
*/
function janrain_capture_profile_sync() {
global $user;
$api = new JanrainCaptureApi();
$profile = $api
->loadUserEntity();
if (!$profile) {
drupal_set_message(t('We were unable to complete your request.'), 'error');
watchdog('janrain_capture', 'Failed to obtain a Capture record', array(), WATCHDOG_ERROR);
return;
}
$user_data = array(
'name' => janrain_capture_profile_field($profile),
'mail' => $profile['result']['email'],
);
$merged_data = janrain_capture_fields_array($user_data, $profile['result']);
if ($account = user_save($user, $merged_data)) {
$profile_updated_hook = module_invoke_all('janrain_capture_user_profile_updated', $profile['result'], $account);
if (empty($profile_updated_hook) || !in_array(FALSE, $profile_updated_hook)) {
drupal_goto();
}
}
else {
drupal_set_message(t('We were unable to complete your request.'), 'error');
watchdog('janrain_capture', 'Failed to save Capture data to user', array(), WATCHDOG_ERROR);
}
}
/**
* Function to retrive the value of a dot-delimited field name from the
* returned Capture user profile.
*/
function janrain_capture_profile_field($profile) {
$janrain_capture_fields = variable_get('janrain_capture_fields', array());
$name = !empty($janrain_capture_fields['capture_name_field']) ? $janrain_capture_fields['capture_name_field'] : 'email';
if (strpos($name, '.')) {
$names = explode('.', $name);
$value = $profile['result'];
foreach ($names as $n) {
$value = $value[$n];
}
return $value;
}
else {
return $profile['result'][$name];
}
}
/**
* Function to create the array of user data to save.
*/
function janrain_capture_fields_array($user_data, $profile) {
$fields = module_invoke_all('janrain_capture_fields_array', $profile);
if (is_array($fields)) {
foreach ($fields as &$f) {
if (is_array($f)) {
$f = $f[0];
}
}
}
if (!empty($fields) && is_array($fields)) {
$user_data = array_merge($user_data, $fields);
}
return $user_data;
}
/**
* Callback for the janrain_capture/resend_verification_email menu item.
* Displays a confirmation message that a verification email was resent.
*/
function janrain_capture_resend_verification_email() {
$hook = module_invoke_all('janrain_capture_verification_resent');
if (empty($hook) || !in_array(FALSE, $hook)) {
if (isset($_SESSION['janrain_capture_email'])) {
drupal_set_message(t('A verification link has been sent to @email. Please check your email.', array(
'@email' => $_SESSION['janrain_capture_email'],
)), 'status');
}
else {
drupal_set_message(t('A verification link has been sent. Please check your email.'), 'status');
}
drupal_goto();
}
}
/**
* Callback the janrain_capture/token_expired menu item. Logs out a user
* due to an expired session.
*/
function janrain_capture_token_expired($token) {
global $user;
$tmp = NULL;
session_destroy();
user_module_invoke('logout', $tmp, $user);
$user = drupal_anonymous_user();
drupal_set_message(t('Your session has expired. Please sign in again.'), 'error');
drupal_goto();
}
Functions
Name | Description |
---|---|
janrain_capture_fields_array | Function to create the array of user data to save. |
janrain_capture_oauth | Callback for the janrain_capture/oauth menu item. This serves as the redirect_uri Capture redirects the user to and performs the authentication. |
janrain_capture_profile | Callback for the janrain_capture/profile menu item. Ensures the access_token is valid before redirecting the user to the Capture profile screen. |
janrain_capture_profile_field | Function to retrive the value of a dot-delimited field name from the returned Capture user profile. |
janrain_capture_profile_sync | Callback for the janrain_capture/profile_sync menu item. Retrieves the most recent data from Capture and stores values locally. |
janrain_capture_resend_verification_email | Callback for the janrain_capture/resend_verification_email menu item. Displays a confirmation message that a verification email was resent. |
janrain_capture_token_expired | Callback the janrain_capture/token_expired menu item. Logs out a user due to an expired session. |