1
|
|
|
<?php |
2
|
|
|
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
3
|
|
|
|
4
|
|
|
class Invitation extends Base { |
5
|
|
|
var $table = 'invitations'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Fetch invitations for one account |
9
|
|
|
* @param account_id int Account ID |
10
|
|
|
* @return mixed Array on success, bool on failure |
11
|
|
|
**/ |
12
|
|
|
public function getInvitations($account_id) { |
13
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
14
|
|
|
$stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE account_id = ?"); |
15
|
|
|
if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) |
16
|
|
|
return $result->fetch_all(MYSQLI_ASSOC); |
17
|
|
|
$this->sqlError('E0021'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Count invitations sent by an account_id |
22
|
|
|
* @param account_id integer Account ID |
23
|
|
|
* @return mixes Integer on success, boolean on failure |
24
|
|
|
**/ |
25
|
|
|
public function getCountInvitations($account_id) { |
26
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
27
|
|
|
$stmt = $this->mysqli->prepare("SELECT count(id) AS total FROM $this->table WHERE account_id = ?"); |
28
|
|
|
if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $stmt->bind_result($total) && $stmt->fetch()) |
29
|
|
|
return $total; |
30
|
|
|
$this->sqlError('E0021'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get a specific invitation by email address |
35
|
|
|
* Used to ensure no invitation was already sent |
36
|
|
|
* @param strEmail string Email address to check for |
37
|
|
|
* @return bool boolean true of ralse |
38
|
|
|
**/ |
39
|
|
|
public function getByEmail($strEmail) { |
40
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
41
|
|
|
return $this->getSingle($strEmail, 'id', 'email', 's'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get a specific token by token ID |
46
|
|
|
* Used to match an invitation against a token |
47
|
|
|
* @param token_id integer Token ID stored in invitation |
48
|
|
|
* @return data mixed Invitation ID on success, false on error |
49
|
|
|
**/ |
50
|
|
|
public function getByTokenId($token_id) { |
51
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
52
|
|
|
return $this->getSingle($token_id, 'id', 'token_id'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set an invitation as activated by the invitee |
57
|
|
|
* @param token_id integer Token to activate |
58
|
|
|
* @return bool boolean true or false |
59
|
|
|
**/ |
60
|
|
|
public function setActivated($token_id) { |
61
|
|
|
if (!$iInvitationId = $this->getByTokenId($token_id)) { |
62
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0030')); |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
$field = array('name' => 'is_activated', 'type' => 'i', 'value' => 1); |
66
|
|
|
return $this->updateSingle($iInvitationId, $field); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Insert a new invitation to the database |
71
|
|
|
* @param account_id integer Account ID to bind the invitation to |
72
|
|
|
* @param email string Email address the invite was sent to |
73
|
|
|
* @param token_id integer Token ID used during invitation |
74
|
|
|
* @return bool boolean True of false |
75
|
|
|
**/ |
76
|
|
|
public function createInvitation($account_id, $email, $token_id) { |
77
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
78
|
|
|
$stmt = $this->mysqli->prepare("INSERT INTO $this->table ( account_id, email, token_id ) VALUES ( ?, ?, ?)"); |
79
|
|
|
if ($stmt && $stmt->bind_param('isi', $account_id, $email, $token_id) && $stmt->execute()) |
80
|
|
|
return true; |
81
|
|
|
$this->sqlError('E0022'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Send an invitation out to a user |
86
|
|
|
* Uses the mail class to send mails |
87
|
|
|
* @param account_id integer Sending account ID |
88
|
|
|
* @param aData array Data array including mail information |
89
|
|
|
* @return bool boolean True or false |
90
|
|
|
**/ |
91
|
|
|
public function sendInvitation($account_id, $aData) { |
92
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
93
|
|
|
// Check data input |
94
|
|
|
if (empty($aData['email']) || !filter_var($aData['email'], FILTER_VALIDATE_EMAIL)) { |
95
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0023')); |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
if (preg_match('/[^a-z_\.\!\?\-0-9 ]/i', $aData['message'])) { |
99
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0024')); |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
// Ensure this invitation does not exist yet nor do we have an account with that email |
103
|
|
|
if ($this->user->getEmail($aData['email'])) { |
104
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0025')); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
if ($this->getByEmail($aData['email'])) { |
108
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0026')); |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
View Code Duplication |
if (!$aData['token'] = $this->token->createToken('invitation', $account_id)) { |
112
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0027', $this->token->getError())); |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
$aData['username'] = $this->user->getUserName($account_id); |
116
|
|
|
$aData['subject'] = 'Pending Invitation'; |
117
|
|
|
$this->log->log("info", $this->user->getUserName($account_id)." sent an invitation"); |
118
|
|
|
if ($this->mail->sendMail('invitations/body', $aData)) { |
119
|
|
|
$aToken = $this->token->getToken($aData['token'], 'invitation'); |
120
|
|
|
if (!$this->createInvitation($account_id, $aData['email'], $aToken['id'])) |
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
return true; |
123
|
|
|
} else { |
124
|
|
|
$this->log->log("warn", $this->user->getUserName($account_id)." sent an invitation but failed to send e-mail"); |
125
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0028')); |
126
|
|
|
} |
127
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0029')); |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Instantiate class |
133
|
|
|
$invitation = new invitation(); |
134
|
|
|
$invitation->setDebug($debug); |
135
|
|
|
$invitation->setLog($log); |
136
|
|
|
$invitation->setMysql($mysqli); |
137
|
|
|
$invitation->setMail($mail); |
138
|
|
|
$invitation->setUser($user); |
139
|
|
|
$invitation->setToken($oToken); |
140
|
|
|
$invitation->setConfig($config); |
141
|
|
|
$invitation->setErrorCodes($aErrorCodes); |
142
|
|
|
|
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.