diff options
Diffstat (limited to 'mod/uservalidationbyemail/lib/functions.php')
| -rw-r--r-- | mod/uservalidationbyemail/lib/functions.php | 105 |
1 files changed, 32 insertions, 73 deletions
diff --git a/mod/uservalidationbyemail/lib/functions.php b/mod/uservalidationbyemail/lib/functions.php index 92b37c843..f3091f94d 100644 --- a/mod/uservalidationbyemail/lib/functions.php +++ b/mod/uservalidationbyemail/lib/functions.php @@ -9,26 +9,29 @@ /** * Generate an email activation code. * - * @param int $user_guid The guid of the user + * @param int $user_guid The guid of the user * @param string $email_address Email address * @return string */ function uservalidationbyemail_generate_code($user_guid, $email_address) { - global $CONFIG; + + $site_url = elgg_get_site_url(); // Note I bind to site URL, this is important on multisite! - return md5($user_guid . $email_address . $CONFIG->site->url . get_site_secret()); + return md5($user_guid . $email_address . $site_url . get_site_secret()); } /** * Request user validation email. * Send email out to the address and request a confirmation. * - * @param int $user_guid The user + * @param int $user_guid The user's GUID + * @param bool $admin_requested Was it requested by admin * @return mixed */ -function uservalidationbyemail_request_validation($user_guid) { - global $CONFIG; +function uservalidationbyemail_request_validation($user_guid, $admin_requested = FALSE) { + + $site = elgg_get_site_entity(); $user_guid = (int)$user_guid; $user = get_entity($user_guid); @@ -36,15 +39,15 @@ function uservalidationbyemail_request_validation($user_guid) { if (($user) && ($user instanceof ElggUser)) { // Work out validate link $code = uservalidationbyemail_generate_code($user_guid, $user->email); - $link = "{$CONFIG->site->url}pg/uservalidationbyemail/confirm?u=$user_guid&c=$code"; - $site = $CONFIG->site; + $link = "{$site->url}uservalidationbyemail/confirm?u=$user_guid&c=$code"; + // Send validation email - $subject = sprintf(elgg_echo('email:validate:subject'), $user->name, $site->name); - $body = sprintf(elgg_echo('email:validate:body'), $user->name, $site->name, $link, $site->name, $site->url); - $result = notify_user($user->guid, $CONFIG->site->guid, $subject, $body, NULL, 'email'); + $subject = elgg_echo('email:validate:subject', array($user->name, $site->name)); + $body = elgg_echo('email:validate:body', array($user->name, $site->name, $link, $site->name, $site->url)); + $result = notify_user($user->guid, $site->guid, $subject, $body, NULL, 'email'); - if ($result) { + if ($result && !$admin_requested) { system_message(elgg_echo('uservalidationbyemail:registerok')); } @@ -57,93 +60,49 @@ function uservalidationbyemail_request_validation($user_guid) { /** * Validate a user * - * @param unknown_type $user_guid - * @param unknown_type $code - * @return unknown + * @param int $user_guid + * @param string $code + * @return bool */ function uservalidationbyemail_validate_email($user_guid, $code) { $user = get_entity($user_guid); if ($code == uservalidationbyemail_generate_code($user_guid, $user->email)) { - return uservalidationbyemail_set_user_validation_status($user_guid, true, 'email'); + return elgg_set_user_validation_status($user_guid, true, 'email'); } return false; } /** - * Set the validation status for a user. - * - * @param bool $status Validated (true) or false - * @param string $method Optional method to say how a user was validated - * @return bool - */ -function uservalidationbyemail_set_user_validation_status($user_guid, $status, $method = '') { - if (!$status) { - $method = ''; - } - - if ($status) { - if ( - (create_metadata($user_guid, 'validated', $status,'', 0, ACCESS_PUBLIC)) && - (create_metadata($user_guid, 'validated_method', $method,'', 0, ACCESS_PUBLIC)) - ) { - return TRUE; - } - } else { - $validated = get_metadata_byname($user_guid, 'validated'); - $validated_method = get_metadata_byname($user_guid, 'validated_method'); - - if ( - ($validated) && - ($validated_method) && - (delete_metadata($validated->id)) && - (delete_metadata($validated_method->id)) - ) - return TRUE; - } - - return FALSE; -} - -/** - * Returns the validation status of a user. - * - * @param unknown_type $user_guid - * @return int|null - */ -function uservalidationbyemail_get_user_validation_status($user_guid) { - $md = get_metadata_byname($user_guid, 'validated'); - - if ($md && $md->value) { - return TRUE; - } - - return FALSE; -} - -/** - * Returns all users who haven't been validated. + * Return a where clause to get entities * * "Unvalidated" means metadata of validated is not set or not truthy. - * We can't use the elgg_get_entities_from_metadata() because you can't say + * We can't use elgg_get_entities_from_metadata() because you can't say * "where the entity has metadata set OR it's not equal to 1". * - * This doesn't include any security, so should be called ONLY be admin users! * @return array */ function uservalidationbyemail_get_unvalidated_users_sql_where() { global $CONFIG; $validated_id = get_metastring_id('validated'); - $one_id = get_metastring_id(1); + if ($validated_id === false) { + $validated_id = add_metastring('validated'); + } + $one_id = get_metastring_id('1'); + if ($one_id === false) { + $one_id = add_metastring('1'); + } // thanks to daveb@freenode for the SQL tips! - $where = "NOT EXISTS ( + $wheres = array(); + $wheres[] = "e.enabled='no'"; + $wheres[] = "NOT EXISTS ( SELECT 1 FROM {$CONFIG->dbprefix}metadata md WHERE md.entity_guid = e.guid AND md.name_id = $validated_id AND md.value_id = $one_id)"; - return $where; + return $wheres; }
\ No newline at end of file |
