diff options
author | Sem <sembrestels@riseup.net> | 2013-11-09 16:25:34 +0100 |
---|---|---|
committer | Sem <sembrestels@riseup.net> | 2013-11-09 16:25:34 +0100 |
commit | 2161e2335c9c650b8e44a56f6c9b0bd37546ae1f (patch) | |
tree | 91bef55b773a7a80b5f6a065411574bdbe9fe522 /mod/openid_client/lib | |
parent | a9ac4c861335e60373c1e99b61372e6e0d6ac9f4 (diff) | |
parent | 11ea6ae4734a0e722c6ecaaee90e9ab772e8d2cc (diff) | |
download | elgg-2161e2335c9c650b8e44a56f6c9b0bd37546ae1f.tar.gz elgg-2161e2335c9c650b8e44a56f6c9b0bd37546ae1f.tar.bz2 |
Add 'mod/openid_client/' from commit '11ea6ae4734a0e722c6ecaaee90e9ab772e8d2cc'
git-subtree-dir: mod/openid_client
git-subtree-mainline: a9ac4c861335e60373c1e99b61372e6e0d6ac9f4
git-subtree-split: 11ea6ae4734a0e722c6ecaaee90e9ab772e8d2cc
Diffstat (limited to 'mod/openid_client/lib')
-rw-r--r-- | mod/openid_client/lib/helpers.php | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/mod/openid_client/lib/helpers.php b/mod/openid_client/lib/helpers.php new file mode 100644 index 000000000..3fc101c42 --- /dev/null +++ b/mod/openid_client/lib/helpers.php @@ -0,0 +1,114 @@ +<?php +/** + * Helper functions for the OpenID client plugin + */ + +/** + * Serves a page to the new user to determine account values + * + * This should only be called after validating the OpenID response. + * + * @param array $data Key value pairs extracted from the response + * @return bool + */ +function openid_client_registration_page_handler(array $data) { + + if (!is_array($data)) { + return false; + } + + $title = elgg_echo('openid_client:create'); + + $vars = openid_client_prepare_registration_vars($data); + $content = elgg_view('openid_client/register', $vars); + + $params = array( + 'title' => $title, + 'content' => $content, + ); + $body = elgg_view_layout('one_column', $params); + echo elgg_view_page($title, $body); + + return true; +} + +/** + * Create the form vars for registration + * + * @param array $data + * @return array + */ +function openid_client_prepare_registration_vars(array $data) { + $vars = array(); + + $vars['openid_identifier'] = $data['openid_identifier']; + + // username + if (isset($data['username'])) { + $vars['username'] = $data['username']; + } else if (isset($data['email'])) { + $vars['username'] = array_shift(explode('@', $data['email'])); + } else { + $vars['username'] = null; + } + + // is the username available + if ($vars['username']) { + $vars['is_username_available'] = openid_client_is_username_available($vars['username']); + } + + // is the username valid + try { + $vars['is_username_valid'] = validate_username($vars['username']); + } catch (RegistrationException $e) { + $vars['is_username_valid'] = false; + } + + // the rest + $vars['email'] = elgg_extract('email', $data); + $vars['name'] = elgg_extract('name', $data); + + if ($vars['email']) { + $vars['is_email_available'] = openid_client_is_email_available($vars['email']); + } + + return $vars; +} + +/** + * Is this username available? + * + * @param string $username The username + * @return bool + */ +function openid_client_is_username_available($username) { + $db_prefix = elgg_get_config('dbprefix'); + $username = sanitize_string($username); + + $query = "SELECT count(*) AS total FROM {$db_prefix}users_entity WHERE username = '$username'"; + $result = get_data_row($query); + if ($result->total == 0) { + return true; + } else { + return false; + } +} + +/** + * Is this email address available? + * + * @param string $email Email address + * @return bool + */ +function openid_client_is_email_available($email) { + $db_prefix = elgg_get_config('dbprefix'); + $email = sanitize_string($email); + + $query = "SELECT count(*) AS total FROM {$db_prefix}users_entity WHERE email = '$email'"; + $result = get_data_row($query); + if ($result->total == 0) { + return true; + } else { + return false; + } +} |