aboutsummaryrefslogtreecommitdiff
path: root/mod/uservalidationbyemail/start.php
blob: 2479d3b98979280e3acb2d262ce982d193d92b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
	/**
	 * Email user validation plugin.
	 * Non-admin or admin created accounts are invalid until their email address is confirmed. 
	 * 
	 * @package ElggUserValidationByEmail
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.com/
	 */

	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');
	}

	/**
	 * 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');
?>