View source
<?php
define('HYBRIDAUTH_MINIMUM_PHP', '5.2.0');
define('HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_WRAPPER_TITLE_DEFAULT', 'Or log in with...');
define('HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_STYLE_DEFAULT', 'list');
define('HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_ICON_SIZE_DEFAULT', '64');
define('HYBRIDAUTH_WIDGET_USE_OVERLAY_DEFAULT', TRUE);
define('HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_LINK_TITLE_DEFAULT', 'Social networking account');
define('HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_LINK_ALT_DEFAULT', '');
function hybridauth_permission() {
$permissions = array(
'administer hybridauth' => array(
'title' => t('Administer HybridAuth settings'),
'restrict access' => TRUE,
),
'manage own identities' => array(
'title' => t('Manage own 3rd party identities'),
'description' => t('Add and delete own 3rd party identities on the HybridAuth tab in the user profile.'),
),
);
return $permissions;
}
function hybridauth_menu() {
$items = array();
$items['admin/config/people/hybridauth'] = array(
'title' => 'HybridAuth settings',
'description' => 'Provides HybridAuth social sign-on',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hybridauth_admin_settings',
),
'file' => 'hybridauth.admin.inc',
'access arguments' => array(
'administer hybridauth',
),
);
$items['admin/config/people/hybridauth/providers'] = array(
'title' => 'Providers',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['admin/config/people/hybridauth/profile'] = array(
'title' => 'Profile',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hybridauth_admin_profile_settings',
),
'file' => 'hybridauth.admin.inc',
'access arguments' => array(
'administer hybridauth',
),
);
$items['admin/config/people/hybridauth/widget'] = array(
'title' => 'Widget',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hybridauth_admin_widget_settings',
),
'file' => 'hybridauth.admin.inc',
'access arguments' => array(
'administer hybridauth',
),
);
$items['admin/config/people/hybridauth/provider/%'] = array(
'title callback' => 'hybridauth_admin_format_provider_title',
'title arguments' => array(
5,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hybridauth_admin_provider_settings',
5,
),
'file' => 'hybridauth.admin.inc',
'access arguments' => array(
'administer hybridauth',
),
);
$items['hybridauth/endpoint'] = array(
'page callback' => 'hybridauth_endpoint',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'hybridauth.pages.inc',
);
$items['hybridauth/popup'] = array(
'page callback' => 'hybridauth_popup',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'hybridauth.pages.inc',
);
$items['hybridauth/popup/openid'] = array(
'page callback' => 'hybridauth_popup_openid',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'hybridauth.pages.inc',
);
$items['hybridauth/confirm/%/%/%'] = array(
'title' => 'Confirm email',
'page callback' => 'hybridauth_email_confirm',
'page arguments' => array(
2,
3,
4,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'hybridauth.pages.inc',
);
$items['user/%user/hybridauth'] = array(
'title' => 'Linked Accounts',
'page callback' => 'hybridauth_user_identities',
'page arguments' => array(
1,
),
'access callback' => '_hybridauth_own_identities_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'file' => 'hybridauth.pages.inc',
);
$items['user/%user/hybridauth/delete'] = array(
'title' => 'Delete Linked Account',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hybridauth_user_delete_form',
1,
),
'access callback' => '_hybridauth_own_identities_access',
'access arguments' => array(
1,
),
'type' => MENU_CALLBACK,
'file' => 'hybridauth.pages.inc',
);
return $items;
}
function hybridauth_admin_format_provider_title($provider_id) {
module_load_include('inc', 'hybridauth', 'hybridauth.auth');
return hybridauth_get_provider_name($provider_id);
}
function _hybridauth_own_identities_access($account) {
return user_access('manage own identities') && user_edit_access($account);
}
function hybridauth_form_user_login_block_alter(&$form, &$form_state) {
if (variable_get('hybridauth_widget_attach_loginform', FALSE)) {
_hybridauth_widget_user_login_form_alter($form, $form_state);
}
}
function hybridauth_form_user_login_alter(&$form, &$form_state) {
if (variable_get('hybridauth_widget_attach_loginform', FALSE)) {
_hybridauth_widget_user_login_form_alter($form, $form_state);
}
}
function _hybridauth_widget_user_login_form_alter(&$form, &$form_state) {
$style = variable_get('hybridauth_widget_attach_loginform_style', HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_STYLE_DEFAULT);
$form['hybridauth_widget_wrapper'] = array(
'#type' => 'fieldset',
'#title' => variable_get('hybridauth_widget_attach_loginform_wrapper_title', HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_WRAPPER_TITLE_DEFAULT),
'hybridauth_widget_wrapper' => array(
'#theme' => 'hybridauth_widget',
'#style' => $style,
'#size' => variable_get('hybridauth_widget_attach_loginform_icon_size', HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_ICON_SIZE_DEFAULT),
'#weight' => variable_get('hybridauth_widget_attach_loginform_weight', 150),
),
);
}
function hybridauth_form_user_register_form_alter(&$form, &$form_state) {
if (!isset($_SESSION) || !isset($_SESSION['hybridauth']) || $_SESSION['hybridauth']['stat'] != 'ok' || user_access('administer users')) {
return;
}
$profile = $_SESSION['hybridauth']['profile'];
$form['account']['name']['#default_value'] = $profile['displayName'];
$mail = '';
if (!empty($profile['email'])) {
$mail = $profile['email'];
}
$form['account']['mail']['#default_value'] = $mail;
if (!empty($profile['emailVerified']) || !variable_get('user_email_verification', TRUE) || variable_get('hybridauth_bypass_email_verification', FALSE)) {
$form['account']['pass']['#type'] = 'hidden';
$form['account']['pass']['#value'] = user_password();
}
$form['hybridauth_id'] = array(
'#type' => 'value',
'#default_value' => $profile['identifier'],
);
$form['id_display'] = array(
'#type' => 'item',
'#weight' => 10,
'#title' => t('Your account ID'),
'#description' => t('This @provider account will be linked to your site account after registration.', array(
'@provider' => $_SESSION['hybridauth_provider_info']['name'],
)),
'#markup' => check_plain(_hybridauth_encode_authname($_SESSION['hybridauth_provider_info']['id'], $profile['identifier'])),
);
$form['#submit'][array_search('user_register_submit', $form['#submit'])] = 'hybridauth_user_register_submit';
}
function _hybridauth_populate_user_record(&$account, $payload) {
$edit['data'] = array(
'hybridauth_data' => $payload,
);
return user_save($account, $edit);
}
function _hybridauth_save_profile_picture(&$account) {
if (!variable_get('hybridauth_profile_import_photo', FALSE) || !variable_get('user_pictures', 0) || !isset($account->data['hybridauth_data']['profile']['photoURL'])) {
return $account;
}
$photo_url = $account->data['hybridauth_data']['profile']['photoURL'];
$tmp_photo = drupal_tempnam('temporary://', 'drupal_hybridauth-');
$tmp_photo_realpath = drupal_realpath($tmp_photo);
copy($photo_url, $tmp_photo_realpath);
$info = image_get_info($tmp_photo_realpath);
$file = new stdClass();
$file->uid = $account->uid;
$file->status = 0;
$file->filename = basename($tmp_photo_realpath);
$file->uri = $tmp_photo;
$file->filemime = $info['mime_type'];
$file->filesize = $info['file_size'];
$errors = array();
$errors += file_validate_is_image($file);
$errors += file_validate_image_resolution($file, variable_get('user_picture_dimensions', '85x85'));
$errors += file_validate_size($file, variable_get('user_picture_file_size', '30') * 1024);
if (!in_array(strtolower($info['extension']), array(
'jpg',
'png',
'gif',
))) {
$errors[] = ' invalid image file extension.';
}
if (count($errors)) {
drupal_set_message(t('Profile Image Import:') . ' ' . $errors[0], 'warning');
$file->fid = 0;
file_delete($file);
}
else {
$file = file_save($file);
$fields['picture'] = $file;
$account = user_save($account, $fields);
}
return $account;
}
function hybridauth_user_register_submit($form, &$form_state) {
form_state_values_clean($form_state);
$pass = user_password();
$form_state['values']['pass'] = $pass;
$form_state['values']['init'] = $form_state['values']['mail'];
$account = $form['#user'];
$category = $form['#user_category'];
$account_unchanged = clone $account;
entity_form_submit_build_entity('user', $account, $form, $form_state);
$edit = array_intersect_key((array) $account, $form_state['values']);
$account = user_save($account_unchanged, $edit, $category);
$account = _hybridauth_populate_user_record($account, $_SESSION['hybridauth']);
if (!$account) {
drupal_set_message(t("Error saving user account."), 'error');
$form_state['redirect'] = '';
return;
}
$account = _hybridauth_save_profile_picture($account);
watchdog('hybridauth', 'New user: %name (%email).', array(
'%name' => $form_state['values']['name'],
'%email' => $form_state['values']['mail'],
), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));
$form_state['user'] = $account;
$form_state['values']['uid'] = $account->uid;
$account->password = $pass;
if ((!variable_get('user_email_verification', TRUE) || variable_get('hybridauth_bypass_email_verification', FALSE) || !empty($_SESSION['hybridauth']['profile']['verifiedEmail']) && strtolower($account->mail) == strtolower($_SESSION['hybridauth']['profile']['verifiedEmail'])) && $account->status) {
_user_mail_notify('register_no_approval_required', $account);
$form_state['uid'] = $account->uid;
user_login_submit(array(), $form_state);
drupal_set_message(t('Registration successful. You are now logged in.'));
}
elseif ($account->status) {
drupal_mail('hybridauth', 'hybridauth_confirm_email', $account->mail, user_preferred_language($account), array(
'account' => $account,
));
drupal_set_message(t('In order to confirm your email address, an email has been sent to you with confirmation instructions.'));
}
else {
_user_mail_notify('register_pending_approval', $account);
drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
}
$form_state['redirect'] = '';
}
function hybridauth_user_insert(&$edit, $account, $category) {
if (!isset($_SESSION['hybridauth'])) {
return;
}
$provider_id = $_SESSION['hybridauth_provider_info']['id'];
$provider_name = $_SESSION['hybridauth_provider_info']['name'];
$profile = $_SESSION['hybridauth']['profile'];
$txn = db_transaction();
try {
user_set_authmaps($account, array(
'authname_hybridauth' => _hybridauth_encode_authname($provider_id, $profile['identifier']),
));
$aid = db_query('SELECT aid FROM {authmap} WHERE authname = :id', array(
'id' => _hybridauth_encode_authname($provider_id, $profile['identifier']),
))
->fetchField();
db_insert('hybridauth_account')
->fields(array(
'aid' => $aid,
'provider_id' => $provider_id,
'created' => REQUEST_TIME,
))
->execute();
} catch (Exception $e) {
$txn
->rollback();
watchdog_exception('hybridauth', $e);
}
}
function _hybridauth_delete_user_data($uid) {
$aids = db_select('authmap')
->fields('authmap', array(
'aid',
))
->condition('uid', $uid);
db_delete('hybridauth_account')
->condition('aid', $aids, 'IN')
->execute();
}
function hybridauth_user_delete($account) {
_hybridauth_delete_user_data($account->uid);
}
function hybridauth_user_cancel($edit, $account, $method) {
switch ($method) {
case 'user_cancel_reassign':
_hybridauth_delete_user_data($account->uid);
break;
}
}
function hybridauth_mail($key, &$message, $params) {
$language = $message['language'];
$variables = array(
'user' => $params['account'],
);
switch ($key) {
case 'hybridauth_confirm_email':
$message['subject'] = _hybridauth_mail_text($key . '_subject', $language, $variables);
$message['body'][] = str_replace('user/reset', 'hybridauth/confirm', _hybridauth_mail_text($key . '_body', $language, $variables));
break;
}
}
function _hybridauth_mail_text($key, $language = NULL, $variables = array(), $replace = TRUE) {
$langcode = isset($language) ? $language->language : NULL;
if ($admin_setting = variable_get('hybridauth_mail_' . $key, FALSE)) {
$text = $admin_setting;
}
else {
switch ($key) {
case 'hybridauth_confirm_email_subject':
$text = t('Confirm your account at [site:name]', array(), array(
'langcode' => $langcode,
));
break;
case 'hybridauth_confirm_email_body':
$text = t("[user:name],\n\nThank you for registering at [site:name].\n\nTo confirm your email address, click on this link or copy and paste it in your browser:\n\n[user:one-time-login-url]\n\nAfter confirming your email address, you will be able to log in to [site:name] using your new account.\n\n-- [site:name] team", array(), array(
'langcode' => $langcode,
));
break;
}
}
if ($replace) {
return token_replace($text, $variables, array(
'language' => $language,
'callback' => 'user_mail_tokens',
));
}
return $text;
}
function _hybridauth_add_widget_css() {
$done =& drupal_static(__FUNCTION__, FALSE);
if (!$done) {
drupal_add_css(drupal_get_path('module', 'hybridauth') . '/hybridauth.widget.css');
}
}
function _hybridauth_add_icon_css() {
$done =& drupal_static(__FUNCTION__, FALSE);
if (!$done) {
drupal_add_css(drupal_get_path('module', 'hybridauth') . '/hybridauth.icons.css');
}
}
function hybridauth_theme($existing, $type, $theme, $path) {
return array(
'hybridauth_admin_settings' => array(
'render element' => 'form',
'file' => 'hybridauth.admin.inc',
),
'hybridauth_widget' => array(
'variables' => array(
'style' => HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_STYLE_DEFAULT,
'size' => HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_ICON_SIZE_DEFAULT,
),
'template' => 'hybridauth-widget',
'file' => 'hybridauth.widget.inc',
),
'hybridauth_popup_loading' => array(
'variables' => array(
'provider_id' => NULL,
'done' => FALSE,
'overlay' => FALSE,
),
'template' => 'hybridauth-popup-loading',
'file' => 'hybridauth.widget.inc',
),
'hybridauth_popup_list' => array(
'variables' => array(
'overlay' => FALSE,
),
'template' => 'hybridauth-popup-list',
'file' => 'hybridauth.widget.inc',
),
'hybridauth_popup_provider' => array(
'variables' => array(
'provider_id' => NULL,
'overlay' => FALSE,
),
'template' => 'hybridauth-popup-provider',
'file' => 'hybridauth.widget.inc',
),
'hybridauth_provider_icon' => array(
'variables' => array(
'size' => HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_ICON_SIZE_DEFAULT,
'provider_id' => NULL,
),
),
);
}
function theme_hybridauth_provider_icon($vars) {
module_load_include('inc', 'hybridauth', 'hybridauth.auth');
$size = $vars['size'];
$provider_id = $vars['provider_id'];
$provider_name = hybridauth_get_provider_name($provider_id);
$style = array(
drupal_html_class('hybridauth-icon'),
drupal_html_class('hybridauth-icon-' . $size),
drupal_html_class('hybridauth-' . $provider_id),
drupal_html_class('hybridauth-' . $provider_id . '-' . $size),
);
_hybridauth_add_icon_css();
return '<span' . drupal_attributes(array(
'class' => $style,
'id' => drupal_html_id('hybridauth-' . $provider_id),
'title' => $provider_name,
)) . '></span>';
}
function _hybridauth_encode_authname($provider_id, $identifier) {
switch ($provider_id) {
case 'Facebook':
if (is_numeric($identifier)) {
return 'http://www.facebook.com/profile.php?id=' . $identifier;
}
return $identifier;
case 'Twitter':
if (is_numeric($identifier)) {
return 'http://twitter.com/account/profile?user_id=' . $identifier;
}
return $identifier;
case 'Google':
if (is_numeric($identifier)) {
return 'https://www.google.com/profiles/' . $identifier;
}
return $identifier;
default:
return $identifier;
}
}
function _hybridauth_decode_authname($provider_id, $authname) {
switch ($provider_id) {
case 'Facebook':
return str_replace('http://www.facebook.com/profile.php?id=', '', $authname);
case 'Twitter':
return str_replace('http://twitter.com/account/profile?user_id=', '', $authname);
case 'Google':
return str_replace('https://www.google.com/profiles/', '', $authname);
default:
return $authname;
}
}