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