|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FFCMS\Controllers\User; |
|
4
|
|
|
|
|
5
|
|
|
use FFMVC\Helpers; |
|
6
|
|
|
use FFCMS\{Controllers, Models, Mappers, Traits, Enums}; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* User Website Controller Class. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Vijay Mahrra <[email protected]> |
|
12
|
|
|
* @copyright 2016 Vijay Mahrra |
|
13
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
|
14
|
|
|
*/ |
|
15
|
|
|
class User extends Base |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Add default scripts for displaying templates |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
* @see app/config/default.ini |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function addScripts() |
|
25
|
|
|
{ |
|
26
|
|
|
// no scripts to add, override me and set css and js |
|
27
|
|
|
$this->setScripts([], ['showdown']); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* user homepage |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Base $f3 |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function index(\Base $f3) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->redirectLoggedOutUser(); |
|
40
|
|
|
|
|
41
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
42
|
|
|
echo \View::instance()->render('user/index.phtml'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* login form submitted |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Base $f3 |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function loginPost(\Base $f3) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->redirectLoggedInUser(); |
|
55
|
|
|
$this->csrf('@user'); |
|
56
|
|
|
|
|
57
|
|
|
// url if login failed |
|
58
|
|
|
$view = 'user/login.phtml'; |
|
59
|
|
|
|
|
60
|
|
|
// filter input vars of request |
|
61
|
|
|
$usersModel = Models\Users::instance(); |
|
62
|
|
|
$usersMapper = $usersModel->getMapper(); |
|
63
|
|
|
$usersMapper->copyfrom($f3->get('REQUEST')); |
|
64
|
|
|
$data = $usersMapper->filter(); |
|
65
|
|
|
$request = $f3->get('REQUEST'); |
|
66
|
|
|
foreach ($data as $k => $v) { |
|
67
|
|
|
if (array_key_exists($k, $request)) { |
|
68
|
|
|
$f3->set('REQUEST.' . $k, $v); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// find user by email address |
|
73
|
|
|
$usersMapper = $usersModel->getUserByEmail($f3->get('REQUEST.email')); |
|
74
|
|
View Code Duplication |
if (null == $usersMapper->id) { |
|
75
|
|
|
$this->notify(_('No user found with that email!'), 'error'); |
|
76
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
77
|
|
|
echo \View::instance()->render($view); |
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// check the password is set |
|
82
|
|
|
$password = $f3->get('REQUEST.password'); |
|
83
|
|
|
if (empty($password)) { |
|
84
|
|
|
$this->notify(_('You must enter a password!'), 'warning'); |
|
85
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
86
|
|
|
echo \View::instance()->render($view); |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// verify password |
|
91
|
|
|
if (!Helpers\Str::passwordVerify($usersMapper->password, $password)) { |
|
92
|
|
|
$this->notify(_('Incorrect password!'), 'warning'); |
|
93
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
94
|
|
|
echo \View::instance()->render($view); |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!$usersModel->login()) { |
|
99
|
|
|
$this->notify(_('Unable to login!'), 'warning'); |
|
100
|
|
|
} else { |
|
101
|
|
|
$f3->set('SESSION.uuid', $usersMapper->uuid); |
|
102
|
|
|
$f3->set('uuid', $usersMapper->uuid); |
|
103
|
|
|
$this->notify(_('You are now logged in!'), 'success'); |
|
104
|
|
|
$uri = $f3->get('REQUEST.redirect_uri'); |
|
105
|
|
|
return $f3->reroute(empty($uri) || !is_string($uri) ? '@user' : urldecode($uri)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* my account |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Base $f3 |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function account(\Base $f3) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->redirectLoggedOutUser(); |
|
119
|
|
|
$this->csrf(); |
|
120
|
|
|
|
|
121
|
|
|
$f3->set('breadcrumbs', [ |
|
122
|
|
|
_('My Account') => 'user', |
|
123
|
|
|
_('My Details') => 'account', |
|
124
|
|
|
]); |
|
125
|
|
|
|
|
126
|
|
|
$f3->set('form', $f3->get('user')); |
|
127
|
|
|
echo \View::instance()->render('user/account.phtml'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* my account posted |
|
133
|
|
|
* |
|
134
|
|
|
* @param \Base $f3 |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function accountPost(\Base $f3) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->redirectLoggedOutUser(); |
|
140
|
|
|
$this->csrf(); |
|
141
|
|
|
|
|
142
|
|
|
$view = 'user/account.phtml'; |
|
143
|
|
|
$f3->set('breadcrumbs', [ |
|
144
|
|
|
_('My Account') => 'user', |
|
145
|
|
|
_('My Details') => 'account', |
|
146
|
|
|
]); |
|
147
|
|
|
|
|
148
|
|
|
// get current user details |
|
149
|
|
|
$usersModel = Models\Users::instance(); |
|
150
|
|
|
$usersMapper = $usersModel->getUserByUUID($f3->get('uuid')); |
|
151
|
|
View Code Duplication |
if (null == $usersMapper->id) { |
|
152
|
|
|
$this->notify(_('Your account no longer exists!'), 'error'); |
|
153
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
154
|
|
|
echo \View::instance()->render('user/account.phtml'); |
|
155
|
|
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// check password is correct |
|
159
|
|
|
$str = Helpers\Str::instance(); |
|
160
|
|
|
$old_password = $f3->get('REQUEST.old_password'); |
|
161
|
|
View Code Duplication |
if (empty($old_password) || !$str->passwordVerify($usersMapper->password, $old_password)) { |
|
162
|
|
|
$this->notify(_('You entered your current password incorrectly!'), 'warning'); |
|
163
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
164
|
|
|
echo \View::instance()->render($view); |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// only allow updating of these fields |
|
169
|
|
|
$data = $f3->get('REQUEST'); |
|
170
|
|
|
$fields = [ |
|
171
|
|
|
'email', |
|
172
|
|
|
'password', |
|
173
|
|
|
'firstname', |
|
174
|
|
|
'lastname', |
|
175
|
|
|
'password_question', |
|
176
|
|
|
'password_answer', |
|
177
|
|
|
]; |
|
178
|
|
|
// check input data has values set for the above fields |
|
179
|
|
|
foreach ($fields as $k => $field) { |
|
180
|
|
|
if (!array_key_exists($field, $data)) { |
|
181
|
|
|
$data[$field] = null; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
// then remove any input data fields that aren't in the above fields |
|
185
|
|
|
foreach ($data as $field => $v) { |
|
186
|
|
|
if (!in_array($field, $fields)) { |
|
187
|
|
|
unset($data[$field]); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// is this a password change? if so, check they match |
|
192
|
|
|
$password = $f3->get('REQUEST.password'); |
|
193
|
|
|
$confirm_password = $f3->get('REQUEST.confirm_password'); |
|
194
|
|
View Code Duplication |
if (!empty($password) || !empty($confirm_password)) { |
|
195
|
|
|
if ($password !== $confirm_password) { |
|
196
|
|
|
$this->notify(_('That password and confirm password must match!'), 'warning'); |
|
197
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
198
|
|
|
echo \View::instance()->render($view); |
|
199
|
|
|
return; |
|
200
|
|
|
} elseif ($str->passwordVerify($usersMapper->password, $password)) { |
|
201
|
|
|
$this->notify(_('The new password and old password are the same!'), 'warning'); |
|
202
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
203
|
|
|
echo \View::instance()->render($view); |
|
204
|
|
|
return; |
|
205
|
|
|
} else { |
|
206
|
|
|
// set new hashed password |
|
207
|
|
|
$data['password'] = $str->password($password); |
|
208
|
|
|
} |
|
209
|
|
|
} else { |
|
210
|
|
|
// same password |
|
211
|
|
|
$data['password'] = $usersMapper->password; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// check if email address change that email isn't taken |
|
215
|
|
|
$email = $f3->get('REQUEST.email'); |
|
216
|
|
View Code Duplication |
if ($usersMapper->email !== $email) { |
|
217
|
|
|
$usersMapper->load(['email = ?', $email]); |
|
218
|
|
|
if ($usersMapper->email == $email) { |
|
219
|
|
|
$this->notify(sprintf(_('The email address %s is already in use!'), $email), 'warning'); |
|
220
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
221
|
|
|
echo \View::instance()->render($view); |
|
222
|
|
|
return; |
|
223
|
|
|
} else { |
|
224
|
|
|
// new email |
|
225
|
|
|
$data['email'] = $email; |
|
226
|
|
|
} |
|
227
|
|
|
} else { |
|
228
|
|
|
// no change |
|
229
|
|
|
unset($data['email']); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
// update required fields to check from ones which changed |
|
233
|
|
|
// validate the entered data |
|
234
|
|
|
$data['uuid'] = $f3->get('uuid'); |
|
235
|
|
|
$usersMapper->copyfrom($data); |
|
236
|
|
|
$usersMapper->validationRequired($fields); |
|
237
|
|
|
$errors = $usersMapper->validate(false); |
|
238
|
|
|
if (is_array($errors)) { |
|
239
|
|
|
$this->notify(['warning' => $usersMapper->validationErrors($errors)]); |
|
240
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
241
|
|
|
echo \View::instance()->render($view); |
|
242
|
|
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// no change, do nothing |
|
246
|
|
|
if (!$usersMapper->changed()) { |
|
247
|
|
|
$this->notify(_('There was nothing to change!'), 'info'); |
|
248
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
249
|
|
|
echo \View::instance()->render($view); |
|
250
|
|
|
return; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
// reset usermapper and copy in valid data |
|
254
|
|
|
$usersMapper->load(['uuid = ?', $data['uuid']]); |
|
255
|
|
|
$usersMapper->copyfrom($data); |
|
256
|
|
View Code Duplication |
if ($usersMapper->save()) { |
|
257
|
|
|
$this->notify(_('Your account was updated!'), 'success'); |
|
258
|
|
|
} else { |
|
259
|
|
|
$this->notify(_('Unable to update your account!'), 'error'); |
|
260
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
261
|
|
|
echo \View::instance()->render($view); |
|
262
|
|
|
return; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// send verification email if email change - non-fatal |
|
266
|
|
|
if ($usersMapper->changed()) { |
|
267
|
|
|
// if email address changed, send confirmation enail |
|
268
|
|
|
if (!$usersModel->saveKey([ |
|
269
|
|
|
'users_uuid' => $usersMapper->uuid, |
|
270
|
|
|
'key' => 'email_confirmed', |
|
271
|
|
|
'value' => 0 |
|
272
|
|
|
])) { |
|
273
|
|
|
$this->notify(_('Setting confirmation email failed.'), 'warning'); |
|
274
|
|
|
} |
|
275
|
|
|
$this->sendConfirmationEmail(); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
$f3->reroute('@user'); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* registration page |
|
284
|
|
|
* |
|
285
|
|
|
* @param \Base $f3 |
|
286
|
|
|
* @return void |
|
287
|
|
|
*/ |
|
288
|
|
View Code Duplication |
public function register(\Base $f3) |
|
289
|
|
|
{ |
|
290
|
|
|
$this->redirectLoggedInUser(); |
|
291
|
|
|
$this->csrf('@user'); |
|
292
|
|
|
|
|
293
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
294
|
|
|
echo \View::instance()->render('user/register.phtml'); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* registration posted |
|
300
|
|
|
* |
|
301
|
|
|
* @param \Base $f3 |
|
302
|
|
|
* @return void |
|
303
|
|
|
*/ |
|
304
|
|
|
public function registerPost(\Base $f3) |
|
305
|
|
|
{ |
|
306
|
|
|
$this->redirectLoggedInUser(); |
|
307
|
|
|
$this->csrf('@register'); |
|
308
|
|
|
|
|
309
|
|
|
// filter input vars of request |
|
310
|
|
|
$usersModel = Models\Users::instance(); |
|
311
|
|
|
$usersMapper = $usersModel->getMapper(); |
|
312
|
|
|
$usersMapper->copyfrom($f3->get('REQUEST')); |
|
313
|
|
|
$data = $usersMapper->filter(); |
|
314
|
|
|
$request = $f3->get('REQUEST'); |
|
315
|
|
|
foreach ($data as $k => $v) { |
|
316
|
|
|
if (array_key_exists($k, $request)) { |
|
317
|
|
|
$f3->set('REQUEST.' . $k, $v); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$view = 'user/register.phtml'; |
|
322
|
|
|
|
|
323
|
|
|
$email = $f3->get('REQUEST.email'); |
|
324
|
|
|
if (empty($email)) { |
|
325
|
|
|
$this->notify(_('You need to enter an email address!'), 'warning'); |
|
326
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
327
|
|
|
echo \View::instance()->render($view); |
|
328
|
|
|
return; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
// find user by email address |
|
332
|
|
|
$usersModel = Models\Users::instance(); |
|
333
|
|
|
$usersMapper = $usersModel->getUserByEmail($email); |
|
334
|
|
View Code Duplication |
if (null !== $usersMapper->id) { |
|
335
|
|
|
$this->notify(_('That user already exists!'), 'error'); |
|
336
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
337
|
|
|
echo \View::instance()->render($view); |
|
338
|
|
|
return; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
// bad password |
|
342
|
|
|
$password = $f3->get('REQUEST.password'); |
|
343
|
|
|
$confirm_password = $f3->get('REQUEST.confirm_password'); |
|
344
|
|
View Code Duplication |
if (empty($password) || empty($confirm_password) || ($password !== $confirm_password)) { |
|
345
|
|
|
$this->notify(_('That password and confirm password must match!'), 'warning'); |
|
346
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
347
|
|
|
echo \View::instance()->render($view); |
|
348
|
|
|
return; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
// use the model to validate the data input |
|
352
|
|
|
$usersMapper->copyfrom($f3->get('REQUEST')); |
|
353
|
|
|
$usersMapper->validationRequired([ |
|
354
|
|
|
'email', |
|
355
|
|
|
'password', |
|
356
|
|
|
'firstname', |
|
357
|
|
|
'lastname', |
|
358
|
|
|
'password_question', |
|
359
|
|
|
'password_answer', |
|
360
|
|
|
]); |
|
361
|
|
|
|
|
362
|
|
|
// set defaults |
|
363
|
|
|
$usersMapper->setUUID(); |
|
364
|
|
|
$usersMapper->scopes = 'user'; |
|
365
|
|
|
$usersMapper->status = 'registered'; |
|
366
|
|
|
$usersMapper->created = Helpers\Time::database(); |
|
367
|
|
|
|
|
368
|
|
|
$errors = $usersMapper->validate(false); |
|
369
|
|
|
|
|
370
|
|
|
if (is_array($errors)) { |
|
371
|
|
|
$this->notify(['info' => $usersMapper->validationErrors($errors)]); |
|
372
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
373
|
|
|
echo \View::instance()->render($view); |
|
374
|
|
|
return; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
if (!$usersModel->register()) { |
|
378
|
|
|
$this->notify(_('Registration failed!'), 'error'); |
|
379
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
380
|
|
|
echo \View::instance()->render($view); |
|
381
|
|
|
return; |
|
382
|
|
|
} |
|
383
|
|
|
$usersModel->login(); |
|
384
|
|
|
$f3->set('SESSION.uuid', $usersMapper->uuid); |
|
385
|
|
|
$f3->set('uuid', $usersMapper->uuid); |
|
386
|
|
|
$this->notify(_('You successfully registered!'), 'success'); |
|
387
|
|
|
$uri = $f3->get('REQUEST.redirect_uri'); |
|
388
|
|
|
|
|
389
|
|
|
// send confirmation email |
|
390
|
|
|
$this->sendConfirmationEmail(); |
|
391
|
|
|
|
|
392
|
|
|
return $f3->reroute(empty($uri) ? '@user' : urldecode($uri)); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Confirm email address link handler from email |
|
398
|
|
|
* |
|
399
|
|
|
* @param \Base $f3 |
|
400
|
|
|
* @return void |
|
401
|
|
|
*/ |
|
402
|
|
|
public function confirmEmail(\Base $f3) |
|
403
|
|
|
{ |
|
404
|
|
|
$db = \Registry::get('db'); |
|
405
|
|
|
$usersModel = Models\Users::instance(); |
|
406
|
|
|
$usersMapper = $usersModel->getMapper(); |
|
407
|
|
|
$usersDataMapper = $usersModel->getDataMapper(); |
|
408
|
|
|
|
|
409
|
|
|
// load in the forgot password reset code row |
|
410
|
|
|
$usersDataMapper->load([$db->quotekey('value')." = ? AND ".$db->quotekey('key')." = 'confirm_email_code'", $f3->get('REQUEST.code')]); |
|
411
|
|
View Code Duplication |
if (null == $usersDataMapper->uuid) { |
|
412
|
|
|
$this->notify(_('Unknown password reset code!'), 'error'); |
|
413
|
|
|
$f3->reroute('@index'); |
|
414
|
|
|
return; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
// check that the user exists for the reset code |
|
418
|
|
|
$usersMapper->load(['uuid = ?', $usersDataMapper->users_uuid]); |
|
419
|
|
View Code Duplication |
if (null == $usersDataMapper->uuid) { |
|
420
|
|
|
$this->notify(_('Unknown user for confirmation code!'), 'error'); |
|
421
|
|
|
$f3->reroute('@index'); |
|
422
|
|
|
return; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
// update account status to 'confirmed' |
|
426
|
|
|
$usersMapper->status = 'confirmed'; |
|
427
|
|
|
if (!$usersMapper->save()) { |
|
428
|
|
|
$this->notify(_('Unable to update account status!'), 'error'); |
|
429
|
|
|
$f3->reroute('@index'); |
|
430
|
|
|
return; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
//delete confirm_email_code and add email_confirmed |
|
434
|
|
|
$usersDataMapper->erase(); |
|
435
|
|
|
$usersModel->saveKey([ |
|
436
|
|
|
'users_uuid' => $usersMapper->uuid, |
|
437
|
|
|
'key' => 'email_confirmed', |
|
438
|
|
|
'value' => 1 |
|
439
|
|
|
]); |
|
440
|
|
|
|
|
441
|
|
|
$this->notify(_('Your email was successfully confirmed!'), 'success'); |
|
442
|
|
|
|
|
443
|
|
|
$f3->reroute('@index'); |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* Send an email confirmation to the given user |
|
449
|
|
|
* |
|
450
|
|
|
* @param null|string $email the email address of the user |
|
451
|
|
|
* @param boolean true/false |
|
452
|
|
|
*/ |
|
453
|
|
|
private function sendConfirmationEmail(string $email = null) |
|
454
|
|
|
{ |
|
455
|
|
|
$f3 = \Base::instance(); |
|
456
|
|
|
$usersModel = Models\Users::instance(); |
|
457
|
|
|
$usersMapper = empty($email) ? $usersModel->getMapper() : $usersModel->getUserByEmail($email); |
|
458
|
|
|
$usersDataMapper = $usersModel->getDataMapper(); |
|
459
|
|
|
if (empty($usersMapper->email)) { |
|
460
|
|
|
$this->notify(_('Could not send confirmation email.'), 'error'); |
|
461
|
|
|
return; |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
// generate a random code to email to the user for confirming the email |
|
465
|
|
|
$usersModel->saveKey([ |
|
466
|
|
|
'users_uuid' => $usersMapper->uuid, |
|
467
|
|
|
'key' => 'confirm_email_code', |
|
468
|
|
|
'value' => Helpers\Str::random(6) |
|
469
|
|
|
]); |
|
470
|
|
|
|
|
471
|
|
|
// set the email template variables |
|
472
|
|
|
$f3->set('templateData', |
|
473
|
|
|
array_merge($usersMapper->cast(), [ |
|
474
|
|
|
'code' => $usersDataMapper->value, |
|
475
|
|
|
'url' => Helpers\Url::internal('@confirm_email') |
|
476
|
|
|
])); |
|
477
|
|
|
|
|
478
|
|
|
// setup PHPMailer |
|
479
|
|
|
$mail = Helpers\Mail::getPHPMailer([ |
|
480
|
|
|
'To' => $usersMapper->email, |
|
481
|
|
|
'Subject' => "Confirm Email Address", |
|
482
|
|
|
'Body' => \Markdown::instance()->convert(\View::instance()->render('email/confirm_email.md')), |
|
483
|
|
|
'AltBody' => \View::instance()->render('email/forgot_password.md', 'text/plain') |
|
484
|
|
|
]); |
|
485
|
|
|
|
|
486
|
|
View Code Duplication |
if ($mail->send()) { |
|
487
|
|
|
$this->notify(_("A notification has been sent to confirm your email address."), "success"); |
|
488
|
|
|
} else { |
|
489
|
|
|
$this->notify(_("There was a problem sending you a registration email, please check your email and/or try again later."), "warning"); |
|
490
|
|
|
$this->notify($mail->ErrorInfo, "error"); |
|
491
|
|
|
} |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
|
|
495
|
|
|
/** |
|
496
|
|
|
* my profile |
|
497
|
|
|
* |
|
498
|
|
|
* @param \Base $f3 |
|
499
|
|
|
* @return void |
|
500
|
|
|
*/ |
|
501
|
|
View Code Duplication |
public function profile(\Base $f3) |
|
502
|
|
|
{ |
|
503
|
|
|
$this->redirectLoggedOutUser(); |
|
504
|
|
|
$this->csrf(); |
|
505
|
|
|
|
|
506
|
|
|
$f3->set('breadcrumbs', [ |
|
507
|
|
|
_('My Account') => 'user', |
|
508
|
|
|
_('My Profile') => 'profile', |
|
509
|
|
|
]); |
|
510
|
|
|
|
|
511
|
|
|
// fetch profile |
|
512
|
|
|
$usersModel = Models\Users::instance(); |
|
513
|
|
|
$profileData = $usersModel->getProfile($f3->get('uuid')); |
|
514
|
|
|
$f3->set('form', $profileData); |
|
515
|
|
|
|
|
516
|
|
|
echo \View::instance()->render('user/profile.phtml'); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* my profile posted |
|
522
|
|
|
* |
|
523
|
|
|
* @param \Base $f3 |
|
524
|
|
|
* @return void |
|
525
|
|
|
*/ |
|
526
|
|
|
public function profilePost(\Base $f3) |
|
527
|
|
|
{ |
|
528
|
|
|
$this->redirectLoggedOutUser(); |
|
529
|
|
|
$this->csrf(); |
|
530
|
|
|
|
|
531
|
|
|
$view = 'user/profile.phtml'; |
|
532
|
|
|
$f3->set('breadcrumbs', [ |
|
533
|
|
|
_('My Account') => 'user', |
|
534
|
|
|
_('My Profile') => 'profile', |
|
535
|
|
|
]); |
|
536
|
|
|
|
|
537
|
|
|
// get existing profile and merge with input |
|
538
|
|
|
$usersModel = Models\Users::instance(); |
|
539
|
|
|
$profileEnum = new Enums\ProfileKeys; |
|
540
|
|
|
|
|
541
|
|
|
// merge profile keys and filter input |
|
542
|
|
|
$profileData = $this->filter( |
|
543
|
|
|
array_intersect_key( |
|
544
|
|
|
array_merge( |
|
545
|
|
|
$usersModel->getProfile($f3->get('uuid')), |
|
546
|
|
|
$f3->get('REQUEST') |
|
547
|
|
|
), |
|
548
|
|
|
$profileEnum->values() |
|
549
|
|
|
), [ |
|
550
|
|
|
'nickname' => 'trim|sanitize_string', |
|
551
|
|
|
'bio' => 'trim|sanitize_string' |
|
552
|
|
|
]); |
|
553
|
|
|
|
|
554
|
|
|
$errors = $this->validate(false, $profileData, [ |
|
555
|
|
|
'nickname' => 'valid_name', |
|
556
|
|
|
]); |
|
557
|
|
|
if (is_array($errors)) { |
|
558
|
|
|
$this->notify(['warning' => $this->validationErrors($errors)]); |
|
559
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
|
560
|
|
|
echo \View::instance()->render($view); |
|
561
|
|
|
return; |
|
562
|
|
|
} |
|
563
|
|
|
|
|
564
|
|
|
// save profile |
|
565
|
|
|
$usersModel->saveData($f3->get('uuid'), $profileData); |
|
566
|
|
|
|
|
567
|
|
|
// set form data |
|
568
|
|
|
$f3->set('form', $profileData); |
|
569
|
|
|
|
|
570
|
|
|
echo \View::instance()->render('user/profile.phtml'); |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
|
|
|