/**
* Registers a user, returning false if the username already exists
*
* @param string $username The username of the new user
* @param string $password The password
* @param string $name The user's display name
* @param string $email The user's email address
* @param bool $allow_multiple_emails Allow the same email address to be
* registered multiple times?
*
* @return int|false The new user's GUID; false on failure
* @throws \RegistrationException
*/
function register($username, $password, $name, $email, $allow_multiple_emails = false)
{
// no need to trim password.
$username = trim($username);
$name = trim(strip_tags($name));
$email = trim($email);
// A little sanity checking
if (empty($username) || empty($password) || empty($name) || empty($email)) {
return false;
}
// Make sure a user with conflicting details hasn't registered and been disabled
$access_status = access_get_show_hidden_status();
access_show_hidden_entities(true);
if (!validate_email_address($email)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:emailnotvalid'));
}
if (!validate_password($password)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:passwordnotvalid'));
}
if (!validate_username($username)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:usernamenotvalid'));
}
if ($user = get_user_by_username($username)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:userexists'));
}
if (!$allow_multiple_emails && get_user_by_email($email)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:dupeemail'));
}
access_show_hidden_entities($access_status);
// Create user
$user = new \ElggUser();
$user->username = $username;
$user->email = $email;
$user->name = $name;
$user->access_id = ACCESS_PUBLIC;
$user->setPassword($password);
$user->owner_guid = 0;
// Users aren't owned by anyone, even if they are admin created.
$user->container_guid = 0;
// Users aren't contained by anyone, even if they are admin created.
$user->language = _elgg_services()->translator->getCurrentLanguage();
if ($user->save() === false) {
return false;
}
// Turn on email notifications by default
set_user_notification_setting($user->getGUID(), 'email', true);
return $user->getGUID();
}
foreach ($_POST as $key => $value) {
$_POST[$key] = remove_email_injection(trim($value));
}
// Loop into required fields and make sure they match our needs
foreach ($required_fields as $field) {
// the field has been submitted?
if (!array_key_exists($field, $_POST)) {
array_push($validation, $field);
}
// check there is information in the field?
if ($_POST[$field] == '') {
array_push($validation, $field);
}
// validate the email address supplied
if ($field == 'email') {
if (!validate_email_address($_POST[$field])) {
array_push($validation, $field);
}
}
}
// basic validation result
if (count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach ($_POST as $key => $value) {
if ($key != 'submit') {
$email_content .= $key . ': ' . $value . "\n";
}
}
// if validation passed ok then send the email
/**
* Registers a user, returning false if the username already exists
*
* @param string $username The username of the new user
* @param string $password The password
* @param string $name The user's display name
* @param string $email Their email address
* @param bool $allow_multiple_emails Allow the same email address to be registered multiple times?
* @param int $friend_guid Optionally, GUID of a user this user will friend once fully registered
* @return int|false The new user's GUID; false on failure
*/
function register_user($username, $password, $name, $email, $allow_multiple_emails = false, $friend_guid = 0, $invitecode = '')
{
// Load the configuration
global $CONFIG;
$username = trim($username);
$password = trim($password);
$name = trim($name);
$email = trim($email);
// A little sanity checking
if (empty($username) || empty($password) || empty($name) || empty($email)) {
return false;
}
// See if it exists and is disabled
$access_status = access_get_show_hidden_status();
access_show_hidden_entities(true);
// Validate email address
if (!validate_email_address($email)) {
throw new RegistrationException(elgg_echo('registration:emailnotvalid'));
}
// Validate password
if (!validate_password($password)) {
throw new RegistrationException(elgg_echo('registration:passwordnotvalid'));
}
// Validate the username
if (!validate_username($username)) {
throw new RegistrationException(elgg_echo('registration:usernamenotvalid'));
}
// Check to see if $username exists already
if ($user = get_user_by_username($username)) {
//return false;
throw new RegistrationException(elgg_echo('registration:userexists'));
}
// If we're not allowed multiple emails then see if this address has been used before
if (!$allow_multiple_emails && get_user_by_email($email)) {
throw new RegistrationException(elgg_echo('registration:dupeemail'));
}
access_show_hidden_entities($access_status);
// Check to see if we've registered the first admin yet.
// If not, this is the first admin user!
$admin = datalist_get('admin_registered');
// Otherwise ...
$user = new ElggUser();
$user->username = $username;
$user->email = $email;
$user->name = $name;
$user->access_id = ACCESS_PUBLIC;
$user->salt = generate_random_cleartext_password();
// Note salt generated before password!
$user->password = generate_user_password($user, $password);
$user->owner_guid = 0;
// Users aren't owned by anyone, even if they are admin created.
$user->container_guid = 0;
// Users aren't contained by anyone, even if they are admin created.
$user->save();
// If $friend_guid has been set, make mutual friends
if ($friend_guid) {
if ($friend_user = get_user($friend_guid)) {
if ($invitecode == generate_invite_code($friend_user->username)) {
$user->addFriend($friend_guid);
$friend_user->addFriend($user->guid);
}
}
}
global $registering_admin;
if (!$admin) {
$user->admin = true;
datalist_set('admin_registered', 1);
$registering_admin = true;
} else {
$registering_admin = false;
}
// Turn on email notifications by default
set_user_notification_setting($user->getGUID(), 'email', true);
return $user->getGUID();
}
/**
* Registers a user, returning false if the username already exists
*
* @param string $username The username of the new user
* @param string $password The password
* @param string $name The user's display name
* @param string $email Their email address
* @param bool $allow_multiple_emails Allow the same email address to be
* registered multiple times?
* @param int $friend_guid GUID of a user to friend once fully registered
* @param string $invitecode An invite code from a friend
*
* @return int|false The new user's GUID; false on failure
*/
function register_user($username, $password, $name, $email, $allow_multiple_emails = false, $friend_guid = 0, $invitecode = '')
{
// Load the configuration
global $CONFIG;
// no need to trim password.
$username = trim($username);
$name = trim(strip_tags($name));
$email = trim($email);
// A little sanity checking
if (empty($username) || empty($password) || empty($name) || empty($email)) {
return false;
}
// Make sure a user with conflicting details hasn't registered and been disabled
$access_status = access_get_show_hidden_status();
access_show_hidden_entities(true);
if (!validate_email_address($email)) {
throw new RegistrationException(elgg_echo('registration:emailnotvalid'));
}
if (!validate_password($password)) {
throw new RegistrationException(elgg_echo('registration:passwordnotvalid'));
}
if (!validate_username($username)) {
throw new RegistrationException(elgg_echo('registration:usernamenotvalid'));
}
if ($user = get_user_by_username($username)) {
throw new RegistrationException(elgg_echo('registration:userexists'));
}
if (!$allow_multiple_emails && get_user_by_email($email)) {
throw new RegistrationException(elgg_echo('registration:dupeemail'));
}
access_show_hidden_entities($access_status);
// Create user
$user = new ElggUser();
$user->username = $username;
$user->email = $email;
$user->name = $name;
$user->access_id = ACCESS_PUBLIC;
$user->salt = generate_random_cleartext_password();
// Note salt generated before password!
$user->password = generate_user_password($user, $password);
$user->owner_guid = 0;
// Users aren't owned by anyone, even if they are admin created.
$user->container_guid = 0;
// Users aren't contained by anyone, even if they are admin created.
$user->language = get_current_language();
$user->save();
// If $friend_guid has been set, make mutual friends
if ($friend_guid) {
if ($friend_user = get_user($friend_guid)) {
if ($invitecode == generate_invite_code($friend_user->username)) {
$user->addFriend($friend_guid);
$friend_user->addFriend($user->guid);
// @todo Should this be in addFriend?
add_to_river('river/relationship/friend/create', 'friend', $user->getGUID(), $friend_guid);
add_to_river('river/relationship/friend/create', 'friend', $friend_guid, $user->getGUID());
}
}
}
// Turn on email notifications by default
set_user_notification_setting($user->getGUID(), 'email', true);
return $user->getGUID();
}
开发者ID:riggo,项目名称:Elgg,代码行数:76,代码来源:users.php
示例11: user_get_user_by_email
/**
* Web service to get all users registered with an email ID
*
* @param string $email Email ID to check for
* @return string $foundusers Array of usernames registered with this email ID
* @throws InvalidParameterException
* @throws RegistrationException
*/
function user_get_user_by_email($email)
{
if (!validate_email_address($email)) {
throw new RegistrationException(elgg_echo('registration:notemail'));
}
$user = get_user_by_email($email);
if (!$user) {
throw new InvalidParameterException('registration:emailnotvalid');
}
foreach ($user as $key => $singleuser) {
$foundusers[$key] = $singleuser->username;
}
return $foundusers;
}
/**
* Generate a unique username based on a provided email address.
*
* The first part (before @) of the email address will be used as a base.
* Numbers will be added to the end to make it unique.
*
* @param string $email the email address to use
*
* @see simplesaml_generate_unique_username()
*
* @return bool|string a unique username, false on failure
*/
function simplesaml_generate_username_from_email($email)
{
$result = false;
if (!empty($email) && validate_email_address($email)) {
list($username) = explode("@", $email);
// make sure the username is unique
$result = simplesaml_generate_unique_username($username);
}
return $result;
}
/**
* Generate a unique username based on a provided email address.
*
* The first part (before @) of the email address will be used as a base.
* Numbers will be added to the end to make it unique.
*
* @param string $email the email address to use
*
* @see simplesaml_generate_unique_username()
*
* @return false|string
*/
function simplesaml_generate_username_from_email($email)
{
if (empty($email) || !validate_email_address($email)) {
return false;
}
list($username) = explode('@', $email);
// make sure the username is unique
return simplesaml_generate_unique_username($username);
}
请发表评论