|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Application\Users; |
|
10
|
|
|
|
|
11
|
|
|
use Application\Exception; |
|
12
|
|
|
use Application\UsersActions; |
|
13
|
|
|
use Bluz\Application\Application; |
|
14
|
|
|
use Bluz\Proxy\Logger; |
|
15
|
|
|
use Bluz\Proxy\Mailer; |
|
16
|
|
|
use Bluz\Proxy\Router; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Mail |
|
20
|
|
|
* |
|
21
|
|
|
* @package Application\Users |
|
22
|
|
|
* @author Anton Shevchuk |
|
23
|
|
|
*/ |
|
24
|
|
|
class Mail |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Activation |
|
28
|
|
|
* |
|
29
|
|
|
* @param Row $user |
|
30
|
|
|
* @param string $password |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function activation($user, $password) |
|
36
|
|
|
{ |
|
37
|
|
|
// email subject |
|
38
|
|
|
$subject = __('Activation'); |
|
39
|
|
|
|
|
40
|
|
|
// create activation token |
|
41
|
|
|
// valid for 5 days |
|
42
|
|
|
$actionRow = UsersActions\Table::getInstance()->generate( |
|
43
|
|
|
$user->id, |
|
44
|
|
|
UsersActions\Table::ACTION_ACTIVATION |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
// send activation email |
|
48
|
|
|
// generate activation URL |
|
49
|
|
|
$activationUrl = Router::getFullUrl( |
|
50
|
|
|
'users', |
|
51
|
|
|
'activation', |
|
52
|
|
|
['code' => $actionRow->code, 'id' => $user->id] |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
// generate mail template by controller |
|
56
|
|
|
$body = Application::getInstance()->dispatch( |
|
57
|
|
|
'users', |
|
58
|
|
|
'mail/template', |
|
59
|
|
|
[ |
|
60
|
|
|
'template' => 'registration', |
|
61
|
|
|
'vars' => ['user' => $user, 'activationUrl' => $activationUrl, 'password' => $password] |
|
62
|
|
|
] |
|
63
|
|
|
)->render(); |
|
64
|
|
|
|
|
65
|
|
|
// try to send email |
|
66
|
|
|
try { |
|
67
|
|
|
$mail = Mailer::create(); |
|
68
|
|
|
$mail->Subject = $subject; |
|
69
|
|
|
$mail->msgHTML(nl2br($body)); |
|
70
|
|
|
$mail->addAddress($user->email); |
|
71
|
|
|
|
|
72
|
|
|
return Mailer::send($mail); |
|
73
|
|
|
} catch (\Exception $e) { |
|
74
|
|
|
Logger::log( |
|
75
|
|
|
'error', |
|
76
|
|
|
$e->getMessage(), |
|
77
|
|
|
['module' => 'users', 'controller' => 'change-email', 'userId' => $user->id] |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Password Recovery |
|
86
|
|
|
* |
|
87
|
|
|
* @param Row $user |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function recovery($user) |
|
92
|
|
|
{ |
|
93
|
|
|
// email subject |
|
94
|
|
|
$subject = __('Password Recovery'); |
|
95
|
|
|
|
|
96
|
|
|
// create activation token |
|
97
|
|
|
// valid for 5 days |
|
98
|
|
|
$actionRow = UsersActions\Table::getInstance()->generate( |
|
99
|
|
|
$user->id, |
|
100
|
|
|
UsersActions\Table::ACTION_RECOVERY |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
// send activation email |
|
104
|
|
|
// generate restore URL |
|
105
|
|
|
$resetUrl = Router::getFullUrl( |
|
106
|
|
|
'users', |
|
107
|
|
|
'recovery-reset', |
|
108
|
|
|
['code' => $actionRow->code, 'id' => $user->id] |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$body = Application::getInstance()->dispatch( |
|
113
|
|
|
'users', |
|
114
|
|
|
'mail/template', |
|
115
|
|
|
[ |
|
116
|
|
|
'template' => 'recovery', |
|
117
|
|
|
'vars' => ['user' => $user, 'resetUrl' => $resetUrl] |
|
118
|
|
|
] |
|
119
|
|
|
)->render(); |
|
120
|
|
|
|
|
121
|
|
|
try { |
|
122
|
|
|
$mail = Mailer::create(); |
|
123
|
|
|
$mail->Subject = $subject; |
|
124
|
|
|
$mail->msgHTML(nl2br($body)); |
|
125
|
|
|
$mail->addAddress($user->email); |
|
126
|
|
|
|
|
127
|
|
|
return Mailer::send($mail); |
|
128
|
|
|
} catch (\Exception $e) { |
|
129
|
|
|
// log it |
|
130
|
|
|
Logger::log( |
|
131
|
|
|
'error', |
|
132
|
|
|
$e->getMessage(), |
|
133
|
|
|
['module' => 'users', 'controller' => 'recovery', 'email' => $user->email] |
|
134
|
|
|
); |
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Change email |
|
141
|
|
|
* |
|
142
|
|
|
* @param Row $user |
|
143
|
|
|
* @param string $email |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
* @throws Exception |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function changeEmail($user, $email) |
|
149
|
|
|
{ |
|
150
|
|
|
// email subject |
|
151
|
|
|
$subject = __('Change email'); |
|
152
|
|
|
|
|
153
|
|
|
// generate change mail token and get full url |
|
154
|
|
|
$actionRow = UsersActions\Table::getInstance()->generate( |
|
155
|
|
|
$user->id, |
|
156
|
|
|
UsersActions\Table::ACTION_CHANGE_EMAIL, |
|
157
|
|
|
5, // ttl in days |
|
158
|
|
|
['email' => $email] |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
$changeUrl = Router::getFullUrl( |
|
162
|
|
|
'users', |
|
163
|
|
|
'change-email', |
|
164
|
|
|
['token' => $actionRow->code] |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$body = Application::getInstance()->dispatch( |
|
168
|
|
|
'users', |
|
169
|
|
|
'mail/template', |
|
170
|
|
|
[ |
|
171
|
|
|
'template' => 'change-email', |
|
172
|
|
|
'vars' => [ |
|
173
|
|
|
'user' => $user, |
|
174
|
|
|
'email' => $email, |
|
175
|
|
|
'changeUrl' => $changeUrl, |
|
176
|
|
|
'profileUrl' => Router::getFullUrl('users', 'profile') |
|
177
|
|
|
] |
|
178
|
|
|
] |
|
179
|
|
|
)->render(); |
|
180
|
|
|
|
|
181
|
|
|
try { |
|
182
|
|
|
$mail = Mailer::create(); |
|
183
|
|
|
$mail->Subject = $subject; |
|
184
|
|
|
$mail->msgHTML(nl2br($body)); |
|
185
|
|
|
$mail->addAddress($email); |
|
186
|
|
|
|
|
187
|
|
|
return Mailer::send($mail); |
|
188
|
|
|
} catch (\Exception $e) { |
|
189
|
|
|
Logger::log( |
|
190
|
|
|
'error', |
|
191
|
|
|
$e->getMessage(), |
|
192
|
|
|
['module' => 'users', 'controller' => 'change-email', 'userId' => $user->id] |
|
193
|
|
|
); |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|