diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2013-12-29 20:45:58 -0200 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2013-12-29 20:45:58 -0200 |
commit | 97e689213ff4e829f251af526ed4e796a3cc2b71 (patch) | |
tree | b04d03ec56305041216b72328fc9b5afde27bc76 /mod/openid_client/lib | |
parent | 0ab6351abb7a602d96c62b0ad35413c88113a6cf (diff) | |
parent | 69e2d8c5d8732042c9319aef1fdea45a82b63e42 (diff) | |
download | elgg-97e689213ff4e829f251af526ed4e796a3cc2b71.tar.gz elgg-97e689213ff4e829f251af526ed4e796a3cc2b71.tar.bz2 |
Merge branch 'master' into saravea
Conflicts:
.gitmodules
mod/admins
mod/assemblies
mod/audio_html5
mod/beechat
mod/crud
mod/elgg-activitystreams
mod/elggman
mod/elggpg
mod/favorites
mod/federated-objects
mod/friendly_time
mod/group_alias
mod/group_operators
mod/languages
mod/lightpics
mod/openid_client
mod/spotlight
mod/suicide
mod/tasks
mod/videolist
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; + } +} |