/
index.php
349 lines (292 loc) · 8.92 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* Email authentication module (UNFINISHED)
*
* This module is used for authenticating users using just an email address.
* It is useful for signing up people for future newsletter notifications of for an early beta program.
*
* @todo Finish the implementation ;)
*
* @package StartupAPI
* @subpackage Authentication\Email
*/
class EmailAuthenticationModule extends AuthenticationModule {
public function getID() {
return "email";
}
public function getLegendColor() {
return "f7dc67";
}
public static function getModulesTitle() {
return "Email";
}
public static function getModulesDescription() {
return "<p>Email authentication module (UNFINISHED)</p>
<p>This module is used for authenticating users using just an email address.</p>
<p>It is useful for signing up people for future newsletter
notifications of for an early beta program.</p>";
}
public function getDescription() {
return self::getModulesDescription();
}
/**
* Returns user's credentials (email address)
*
* @param User $user User object
*
* @return EmailUserCredentials|null
*
* @throws DBException
*/
public function getUserCredentials($user) {
$db = UserConfig::getDB();
$userid = $user->getID();
if ($stmt = $db->prepare('SELECT email FROM u_users WHERE id = ?')) {
if (!$stmt->bind_param('i', $userid)) {
throw new DBBindParamException($db, $stmt);
}
if (!$stmt->execute()) {
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($email)) {
throw new DBBindResultException($db, $stmt);
}
$stmt->fetch();
$stmt->close();
if (!is_null($email)) {
return new EmailUserCredentials($email);
}
} else {
throw new DBPrepareStmtException($db);
}
return null;
}
/**
* Returns a number of users with who provided email address
*
* @return int Number of users with email address
*
* @throws DBException
*/
public function getTotalConnectedUsers() {
$db = UserConfig::getDB();
$conns = 0;
if ($stmt = $db->prepare('SELECT count(*) AS conns FROM u_users WHERE email IS NOT NULL')) {
if (!$stmt->execute()) {
throw new DBExecuteStmtException($db, $stmt);
}
if (!$stmt->bind_result($conns)) {
throw new DBBindResultException($db, $stmt);
}
$stmt->fetch();
$stmt->close();
} else {
throw new DBPrepareStmtException($db);
}
return $conns;
}
public function renderLoginForm($template_info, $action) {
$template_info['slug'] = $this->getID();
$template_info['action'] = $action;
return StartupAPI::$template->render("@startupapi/modules/email/login_form.html.twig", $template_info);
}
public function renderRegistrationForm($template_info, $full = false, $action = null, $errors = null, $data = null) {
$slug = $this->getID();
$template_info['slug'] = $slug;
$template_info['action'] = $action;
$template_info['full'] = $full ? TRUE : FALSE;
$template_info['errors'] = $errors;
$template_info['data'] = $data;
return StartupAPI::$template->render("@startupapi/modules/email/registration_form.html.twig", $template_info);
}
/**
* Renders user editing form
*
* @param array[] $template_info Array of base information for Twig template
* @param string $action Form action to post back to
* @param array[] $errors Error messages to display
* @param User $user User object for current user that is being edited
* @param mixed[] $data Data submitted to the form
*
* @return string Rendered user ediging form for this module
*/
public function renderEditUserForm($template_info, $action, $errors, $user, $data) {
$template_info['slug'] = $this->getID();
$template_info['action'] = $action;
$template_info['errors'] = $errors;
$template_info['data'] = $data;
return StartupAPI::$template->render("@startupapi/modules/email/edit_user_form.html.twig", $template_info);
}
/**
* Instead of logging user in, sends a link to their email address for clicking back
*
* @param array $data Form array
* @param boolesn $remember Remember user or not
*
* @return null Never returns a user object
*
* @todo implement actual email sending
*/
public function processLogin($data, &$remember) {
$user = User::getUsersByEmailOrUsername($data['email']);
// $user->sendConfirmationEmail();
header('Location: ' . UserConfig::$USERSROOTURL . '/modules/email/login.php?module=email&message=linksent');
exit;
return null; // kind-of pointless after redirect, but indicates that you can' just login using email
}
/**
* Processes login link and logs user in
*
* @param string $email email address
* @param type $code
*
* @return User User object if successfully logged in
*
* @throws InputValidationException
*
* @todo Finish implementation
*/
public function processLoginLink($email, $code) {
$errors = array();
if ($email) {
$email = trim(mb_convert_encoding($email, 'UTF-8'));
if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
$errors['email'][] = 'Invalid email address';
}
} else {
$errors['email'][] = 'No email specified';
}
if (count($errors) > 0) {
throw new InputValidationException('Validation failed', 0, $errors);
}
// TODO check if code is good and log the user in
return null;
}
/**
* Registers a user, but instead of logging them in, sends a link to their email address for clicking back
*
* @param array $data Form data
* @param boolean $remember Remember user or not
*
* @return null Never returns a user object
*
* @throws InputValidationException
* @throws ExistingUserException
*/
public function processRegistration($data, &$remember) {
$errors = array();
if (array_key_exists('name', $data)) {
$name = trim(mb_convert_encoding($data['name'], 'UTF-8'));
if ($name == '') {
$errors['name'][] = "Name can't be empty";
}
} else {
$errors['name'][] = 'No name specified';
}
if (array_key_exists('email', $data)) {
$email = trim(mb_convert_encoding($data['email'], 'UTF-8'));
if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
$errors['email'][] = 'Invalid email address';
}
} else {
$errors['email'][] = 'No email specified';
}
if (count($errors) > 0) {
throw new InputValidationException('Validation failed', 0, $errors);
}
$existing_users = User::getUsersByEmailOrUsername($email);
if (count($existing_users) > 0) {
$errors['email'][] = "This email is already used by another user, please enter a different email address.";
}
if (count($errors) > 0) {
throw new ExistingUserException($existing_users[0], 'User already exists', 0, $errors);
}
// ok, let's create a user
$user = User::createNewWithoutCredentials($this, $name, $email);
$user->recordActivity(USERBASE_ACTIVITY_REGISTER_EMAIL);
// $user->sendConfirmationEmail();
header('Location: ' . UserConfig::$USERSROOTURL . '/modules/email/login.php?module=email&message=linksent&email=' . rawurlencode($email));
exit;
return null;
}
/**
* Sends actual email with confirmation code
*
* @todo Implement this method
*/
public function sendConfirmationEmail() {
// TODO: Implement email sending
}
/**
* Edits user information
*
* @throws InputValidationException
*
* @todo Actually implement checking if email is real
*/
public function processEditUser($user, $data) {
$errors = array();
if (array_key_exists('name', $data)) {
$name = trim(mb_convert_encoding($data['name'], 'UTF-8'));
if ($name == '') {
$errors['name'][] = "Name can't be empty";
}
} else {
$errors['name'][] = 'No name specified';
}
if (array_key_exists('email', $data)) {
$email = trim(mb_convert_encoding($data['email'], 'UTF-8'));
if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
$errors['email'][] = 'Invalid email address';
}
} else {
$errors['email'][] = 'No email specified';
}
$existing_users = User::getUsersByEmailOrUsername($email);
if (!array_key_exists('email', $errors) &&
(count($existing_users) > 0 && !$existing_users[0]->isTheSameAs($user))
) {
$errors['email'][] = "This email is already used by another user, please enter a different email address.";
}
if (count($errors) > 0) {
throw new InputValidationException('Validation failed', 0, $errors);
}
$user->setName($name);
$user->setEmail($email);
// $user->sendConfirmationEmail();
$user->save();
$user->recordActivity(USERBASE_ACTIVITY_UPDATEUSERINFO);
return true;
}
}
/**
* Email credentials for the user
*
* @package StartupAPI
* @subpackage Authentication\Email
*/
class EmailUserCredentials extends UserCredentials {
/**
* @var string User's email address
*/
private $email;
/**
* Creates user credentials object
*
* @param string $email User's email address
*/
public function __construct($email) {
$this->email = $email;
}
/**
* Returns user's email address
*
* @return string User's email address
*/
public function getEmail() {
return $this->email;
}
public function getHTML() {
return $this->email;
}
}