diff options
-rw-r--r-- | actions/register.php | 5 | ||||
-rw-r--r-- | engine/lib/users.php | 2 | ||||
-rw-r--r-- | mod/uservalidationbyemail/actions/email/confirm.php | 46 | ||||
-rw-r--r-- | mod/uservalidationbyemail/languages/en.php | 13 | ||||
-rw-r--r-- | mod/uservalidationbyemail/start.php | 75 |
5 files changed, 136 insertions, 5 deletions
diff --git a/actions/register.php b/actions/register.php index eb5f24c98..7a8a01c5d 100644 --- a/actions/register.php +++ b/actions/register.php @@ -47,12 +47,11 @@ $new_user->admin = 'yes'; } - if (!$new_user->admin) - $new_user->disable('new_user'); // Now disable if not an admin - // Send user validation request on register only request_user_validation($guid); + if (!$new_user->admin) + $new_user->disable('new_user'); // Now disable if not an admin system_message(sprintf(elgg_echo("registerok"),$CONFIG->sitename)); diff --git a/engine/lib/users.php b/engine/lib/users.php index 613993c0c..a0b3aad26 100644 --- a/engine/lib/users.php +++ b/engine/lib/users.php @@ -922,7 +922,7 @@ function request_user_validation($user_guid) { $user = get_entity($user_guid); - + if (($user) && ($user instanceof ElggUser)) { // invalidate any existing validations diff --git a/mod/uservalidationbyemail/actions/email/confirm.php b/mod/uservalidationbyemail/actions/email/confirm.php new file mode 100644 index 000000000..6f96a076f --- /dev/null +++ b/mod/uservalidationbyemail/actions/email/confirm.php @@ -0,0 +1,46 @@ +<?php + /** + * Action which confirms an email when it is registered or changed, based on a code. + * + * @package Elgg + * @subpackage Core + * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2 + * @author + * @copyright Curverider Ltd 2008 + * @link http://elgg.org/ + */ + + global $CONFIG; + + // Get user id + $access_status = access_get_show_hidden_status(); + access_show_hidden_entities(true); + + $user_guid = (int)get_input('u'); + $user = get_entity($user_guid); + + // And the code + $code = sanitise_string(get_input('c')); + + if ( ($code) && ($user) ) + { + if (uservalidationbyemail_validate_email($user_guid, $code)) { + system_message(elgg_echo('email:confirm:success')); + + $user = get_entity($user_guid); + $user->enable(); + + notify_user($user_guid, $CONFIG->site->guid, sprintf(elgg_echo('email:validate:success:subject'), $user->username), sprintf(elgg_echo('email:validate:success:body'), $user->name), NULL, 'email'); + + } else + register_error(elgg_echo('email:confirm:fail')); + } + else + register_error(elgg_echo('email:confirm:fail')); + + access_show_hidden_entities($access_status); + + forward($_SERVER['HTTP_REFERER']); + exit; + +?>
\ No newline at end of file diff --git a/mod/uservalidationbyemail/languages/en.php b/mod/uservalidationbyemail/languages/en.php index 03dba01a8..b2fe69218 100644 --- a/mod/uservalidationbyemail/languages/en.php +++ b/mod/uservalidationbyemail/languages/en.php @@ -10,6 +10,19 @@ */ $english = array( + + 'email:validate:subject' => "%s please confirm your email address!", + 'email:validate:body' => "Hi %s, + +Please confirm your email address by clicking on the link below: + +%s +", + 'email:validate:success:subject' => "Email validated %s!", + 'email:validate:success:body' => "Hi %s, + +Congratulations, you have successfully validated your email address.", + ); add_translation("en",$english); diff --git a/mod/uservalidationbyemail/start.php b/mod/uservalidationbyemail/start.php index f02b2cf68..2479d3b98 100644 --- a/mod/uservalidationbyemail/start.php +++ b/mod/uservalidationbyemail/start.php @@ -12,12 +12,85 @@ function uservalidationbyemail_init() { + global $CONFIG; + // Register actions + register_action("email/confirm",true, $CONFIG->pluginspath . "uservalidationbyemail/actions/email/confirm.php"); // Register hook listening to new users. + register_elgg_event_handler('user', 'validate', 'uservalidationbyemail_email_validation'); } - // create - if not admin & if not admin logged in then request email validation + /** + * Request email validation. + */ + function uservalidationbyemail_email_validation($event, $object_type, $object) + { + error_log('EMAIL : '.$event." $object_type"); + if (($object) && ($object instanceof ElggUser)) + { + uservalidationbyemail_request_validation($object->guid); + } + } + + /** + * Generate an email activation code. + * + * @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; + + return md5($user_guid . $email_address . $CONFIG->site->url); // Note I bind to site URL, this is important on multisite! + } + + /** + * Request user validation email. + * Send email out to the address and request a confirmation. + * + * @param int $user_guid The user + * @return mixed + */ + function uservalidationbyemail_request_validation($user_guid) + { + global $CONFIG; + + $user_guid = (int)$user_guid; + $user = get_entity($user_guid); + + if (($user) && ($user instanceof ElggUser)) + { + // Work out validate link + $link = $CONFIG->site->url . "action/email/confirm?u=$user_guid&c=" . uservalidationbyemail_generate_code($user_guid, $user->email); + + // Send validation email + return notify_user($user->guid, $CONFIG->site->guid, sprintf(elgg_echo('email:validate:subject'), $user->username), sprintf(elgg_echo('email:validate:body'), $user->name, $link), NULL, 'email'); + + } + + return false; + } + + /** + * Validate a user + * + * @param unknown_type $user_guid + * @param unknown_type $code + * @return unknown + */ + function uservalidationbyemail_validate_email($user_guid, $code) + { + $user = get_entity($user_guid); + + $valid = ($code == uservalidationbyemail_generate_code($user_guid, $user->email)); + if ($valid) + set_user_validation_status($user_guid, true, 'email'); + + return $valid; + } // Initialise register_elgg_event_handler('init','system','uservalidationbyemail_init'); |