forked from idno/known
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Invitation.php
123 lines (103 loc) · 3.98 KB
/
Invitation.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Site invitation representation
*
* @package idno
* @subpackage core
*/
namespace Idno\Entities {
use Idno\Core\Email;
class Invitation extends \Idno\Common\Entity
{
function __construct()
{
$this->generateCode();
parent::__construct();
}
/**
* Generates the code associated with this invitation
*/
function generateCode()
{
if (\Idno\Core\site()->session()->isLoggedOn()) {
$email = \Idno\Core\site()->session()->currentUser()->email;
} else {
$email = base64_encode(time() . rand(0, 99999));
}
$this->code = md5(time() . rand(0, 9999) . $email);
}
/**
* Associates this invitation with a particular email address; returns false if the address is invalid
* @param $email
* @return bool
*/
function associateWithEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->email = $email;
return true;
}
return false;
}
/**
* Saves this invitation and sends it to the appropriate email address
* @param $email
* @return bool|int
*/
function sendToEmail($email)
{
if ($this->associateWithEmail($email)) {
$this->save();
$message = new Email();
$message->addTo($email);
$message->setSubject(\Idno\Core\site()->session()->currentUser()->getTitle() . " has invited you to join " . \Idno\Core\site()->config()->title . '!');
$message->setHTMLBodyFromTemplate('account/invite', array('email' => $email, 'code' => $this->code, 'inviter' => \Idno\Core\site()->session()->currentUser()->getTitle()));
$message->setTextBodyFromTemplate('account/invite', array('email' => $email, 'code' => $this->code, 'inviter' => \Idno\Core\site()->session()->currentUser()->getTitle()));
return $message->send();
}
return false;
}
/**
* Retrieves an invitation associated with a particular email address
* @param $email
* @return bool
*/
static function getByEmail($email)
{
if ($result = \Idno\Core\site()->db()->getObjects(get_called_class(), array('email' => $email), null, 1)) {
foreach ($result as $row) {
return $row;
}
}
return false;
}
/**
* Retrieves an invitation associated with a particular email address and code.
* @param $email
* @param $code
* @return bool
*/
static function getByEmailAndCode($email, $code)
{
if ($result = \Idno\Core\site()->db()->getObjects(get_called_class(), array('email' => $email, 'code' => $code), null, 1)) {
foreach ($result as $row) {
return $row;
}
}
return false;
}
/**
* Validates an email address / invitation code combination (or returns false if no such invitation exists).
* @param $email
* @param $code
* @return \Idno\Entities\Invitation|false
*/
static function validate($email, $code)
{
if ($invitation = self::getByEmailAndCode($email, $code)) {
return $invitation;
}
return false;
}
}
}