Skip to content

Commit 420091e

Browse files
committed
Merge branch 'feature/email' into develop
2 parents 0c381ca + 4912314 commit 420091e

21 files changed

+453
-257
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"ext-mcrypt": "*",
5050
"ext-memcached": "*",
5151
"ext-pdo": "*",
52-
"php-64bit": ">=5.5.0"
52+
"php-64bit": ">=5.5.0",
53+
"phpmailer/phpmailer": "^6.0"
5354
}
5455
}

composer.lock

+67-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/config.sample.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
}
4242
},
4343
"email": {
44+
"recipient_from": "",
45+
"recipient_reply_to": "",
4446
"smtp_host": "smtp.example.com",
4547
"smtp_password": "",
4648
"smtp_port": 587,
49+
"smtp_tls": true,
4750
"smtp_user": ""
4851
},
4952
"memcache": {

src/controllers/User/Activate.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers\User;
4+
5+
use \CarlBennett\MVC\Libraries\Common;
6+
use \CarlBennett\MVC\Libraries\Controller;
7+
use \CarlBennett\MVC\Libraries\Router;
8+
use \CarlBennett\MVC\Libraries\View;
9+
10+
use \BNETDocs\Libraries\EventTypes;
11+
use \BNETDocs\Libraries\Exceptions\UserNotFoundException;
12+
use \BNETDocs\Libraries\Logger;
13+
use \BNETDocs\Libraries\User;
14+
15+
use \BNETDocs\Models\User\Activate as UserActivateModel;
16+
17+
use \InvalidArgumentException;
18+
19+
class Activate extends Controller {
20+
21+
public function &run( Router &$router, View &$view, array &$args ) {
22+
23+
$data = $router->getRequestQueryArray();
24+
25+
$model = new UserActivateModel();
26+
27+
$model->error = 'INVALID_TOKEN';
28+
$model->token = isset( $data[ 't' ]) ? $data[ 't' ] : null;
29+
$model->user_id = isset( $data[ 'u' ]) ? $data[ 'u' ] : null;
30+
31+
if ( !is_null( $model->user_id )) {
32+
$model->user_id = (int) $model->user_id;
33+
}
34+
35+
try {
36+
$model->user = new User( $model->user_id );
37+
} catch ( UserNotFoundException $ex ) {
38+
$model->user = null;
39+
} catch ( InvalidArgumentException $ex ) {
40+
$model->user = null;
41+
}
42+
43+
if ( $model->user ) {
44+
$model->user->invalidateVerificationToken();
45+
$user_token = $model->user->getVerificationToken();
46+
47+
if ( $user_token === $model->token ) {
48+
if (!$model->user->setVerified()) {
49+
$model->error = 'INTERNAL_ERROR';
50+
} else {
51+
$model->error = false;
52+
Logger::logEvent(
53+
EventTypes::USER_VERIFIED,
54+
$model->user_id,
55+
getenv( 'REMOTE_ADDR' ),
56+
json_encode([ 'error' => $model->error ])
57+
);
58+
}
59+
}
60+
}
61+
62+
$view->render( $model );
63+
64+
$model->_responseCode = 200;
65+
$model->_responseHeaders[ 'Content-Type' ] = $view->getMimeType();
66+
$model->_responseTTL = 0;
67+
68+
return $model;
69+
70+
}
71+
72+
}

src/controllers/User/Register.php

+84-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
use \CarlBennett\MVC\Libraries\Common;
1717
use \CarlBennett\MVC\Libraries\Controller;
1818
use \CarlBennett\MVC\Libraries\Router;
19+
use \CarlBennett\MVC\Libraries\Template;
1920
use \CarlBennett\MVC\Libraries\View;
2021

22+
use \PHPMailer\PHPMailer\Exception;
23+
use \PHPMailer\PHPMailer\PHPMailer;
24+
25+
use \StdClass;
26+
2127
class Register extends Controller {
2228

2329
public function &run(Router &$router, View &$view, array &$args) {
@@ -136,20 +142,28 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
136142
return;
137143
}
138144
} catch (UserNotFoundException $e) {}
139-
145+
140146
try {
141147
if (User::findIdByUsername($username)) {
142148
$model->error = "USERNAME_TAKEN";
143149
return;
144150
}
145151
} catch (UserNotFoundException $e) {}
146152

153+
$user = null;
154+
$user_id = null;
155+
147156
try {
148157

149158
$success = User::create(
150159
$email, $username, null, $pw1, User::DEFAULT_OPTION
151160
);
152161

162+
if ($success) {
163+
$user_id = User::findIdByUsername($username);
164+
$user = new User( $user_id );
165+
}
166+
153167
} catch (QueryException $e) {
154168

155169
// SQL error occurred. We can show a friendly message to the user while
@@ -158,14 +172,82 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
158172

159173
}
160174

175+
if ($success) {
176+
$state = new StdClass();
177+
178+
$mail = new PHPMailer( true ); // true enables exceptions
179+
$mail_config = Common::$config->email;
180+
181+
$state->mail &= $mail;
182+
$state->token = ( $user ? $user->getVerificationToken() : null );
183+
$state->user_id = $user_id;
184+
185+
try {
186+
//Server settings
187+
$mail->Timeout = 10; // default is 300 per RFC2821 $ 4.5.3.2
188+
$mail->SMTPDebug = 0;
189+
$mail->isSMTP();
190+
$mail->Host = $mail_config->smtp_host;
191+
$mail->SMTPAuth = !empty($mail_config->smtp_user);
192+
$mail->Username = $mail_config->smtp_user;
193+
$mail->Password = $mail_config->smtp_password;
194+
$mail->SMTPSecure = $mail_config->smtp_tls ? 'tls' : '';
195+
$mail->Port = $mail_config->smtp_port;
196+
197+
//Recipients
198+
if (!empty($mail_config->recipient_from)) {
199+
$mail->setFrom($mail_config->recipient_from, 'BNETDocs');
200+
}
201+
202+
$mail->addAddress($email, $username);
203+
204+
if (!empty($mail_config->recipient_reply_to)) {
205+
$mail->addReplyTo($mail_config->recipient_reply_to);
206+
}
207+
208+
// Content
209+
$mail->isHTML(true);
210+
$mail->Subject = 'Account Activation';
211+
$mail->CharSet = PHPMailer::CHARSET_UTF8;
212+
213+
ob_start();
214+
(new Template($state, 'Email/User/Register.rich'))->render();
215+
$mail->Body = ob_get_clean();
216+
217+
ob_start();
218+
(new Template($state, 'Email/User/Register.plain'))->render();
219+
$mail->AltBody = ob_get_clean();
220+
221+
$mail->send();
222+
223+
Logger::logEvent(
224+
EventTypes::EMAIL_SENT,
225+
$user_id,
226+
getenv('REMOTE_ADDR'),
227+
json_encode([
228+
'from' => $mail->From,
229+
'to' => $mail->getToAddresses(),
230+
'reply_to' => $mail->getReplyToAddresses(),
231+
'subject' => $mail->Subject,
232+
'content_type' => $mail->ContentType,
233+
'body' => $mail->Body,
234+
'alt_body' => $mail->AltBody,
235+
])
236+
);
237+
238+
} catch (\Exception $e) {
239+
$model->error = "EMAIL_FAILURE";
240+
}
241+
}
242+
161243
if (!$success) {
162244
$model->error = "INTERNAL_ERROR";
163245
} else {
164246
$model->error = false;
165247
}
166248
Logger::logEvent(
167249
EventTypes::USER_CREATED,
168-
null,
250+
$user_id,
169251
getenv("REMOTE_ADDR"),
170252
json_encode([
171253
"error" => $model->error,

src/libraries/Email/Debug.php

-34
This file was deleted.

src/libraries/Email/User/Register.php

-45
This file was deleted.

0 commit comments

Comments
 (0)