Skip to content

Commit

Permalink
Merge branch 'develop' into phoenix
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Aug 23, 2019
2 parents 4e66c3e + 420091e commit ff48ce3
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 259 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ext-mcrypt": "*",
"ext-memcached": "*",
"ext-pdo": "*",
"php-64bit": ">=5.5.0"
"php-64bit": ">=5.5.0",
"phpmailer/phpmailer": "^6.0"
}
}
68 changes: 67 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions etc/config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
}
},
"email": {
"recipient_from": "",
"recipient_reply_to": "",
"smtp_host": "smtp.example.com",
"smtp_password": "",
"smtp_port": 587,
"smtp_tls": true,
"smtp_user": ""
},
"memcache": {
Expand Down
72 changes: 72 additions & 0 deletions src/controllers/User/Activate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace BNETDocs\Controllers\User;

use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Controller;
use \CarlBennett\MVC\Libraries\Router;
use \CarlBennett\MVC\Libraries\View;

use \BNETDocs\Libraries\EventTypes;
use \BNETDocs\Libraries\Exceptions\UserNotFoundException;
use \BNETDocs\Libraries\Logger;
use \BNETDocs\Libraries\User;

use \BNETDocs\Models\User\Activate as UserActivateModel;

use \InvalidArgumentException;

class Activate extends Controller {

public function &run( Router &$router, View &$view, array &$args ) {

$data = $router->getRequestQueryArray();

$model = new UserActivateModel();

$model->error = 'INVALID_TOKEN';
$model->token = isset( $data[ 't' ]) ? $data[ 't' ] : null;
$model->user_id = isset( $data[ 'u' ]) ? $data[ 'u' ] : null;

if ( !is_null( $model->user_id )) {
$model->user_id = (int) $model->user_id;
}

try {
$model->user = new User( $model->user_id );
} catch ( UserNotFoundException $ex ) {
$model->user = null;
} catch ( InvalidArgumentException $ex ) {
$model->user = null;
}

if ( $model->user ) {
$model->user->invalidateVerificationToken();
$user_token = $model->user->getVerificationToken();

if ( $user_token === $model->token ) {
if (!$model->user->setVerified()) {
$model->error = 'INTERNAL_ERROR';
} else {
$model->error = false;
Logger::logEvent(
EventTypes::USER_VERIFIED,
$model->user_id,
getenv( 'REMOTE_ADDR' ),
json_encode([ 'error' => $model->error ])
);
}
}
}

$view->render( $model );

$model->_responseCode = 200;
$model->_responseHeaders[ 'Content-Type' ] = $view->getMimeType();
$model->_responseTTL = 0;

return $model;

}

}
86 changes: 84 additions & 2 deletions src/controllers/User/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Controller;
use \CarlBennett\MVC\Libraries\Router;
use \CarlBennett\MVC\Libraries\Template;
use \CarlBennett\MVC\Libraries\View;

use \PHPMailer\PHPMailer\Exception;
use \PHPMailer\PHPMailer\PHPMailer;

use \StdClass;

class Register extends Controller {

public function &run(Router &$router, View &$view, array &$args) {
Expand Down Expand Up @@ -136,20 +142,28 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
return;
}
} catch (UserNotFoundException $e) {}

try {
if (User::findIdByUsername($username)) {
$model->error = "USERNAME_TAKEN";
return;
}
} catch (UserNotFoundException $e) {}

$user = null;
$user_id = null;

try {

$success = User::create(
$email, $username, null, $pw1, User::DEFAULT_OPTION
);

if ($success) {
$user_id = User::findIdByUsername($username);
$user = new User( $user_id );
}

} catch (QueryException $e) {

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

}

if ($success) {
$state = new StdClass();

$mail = new PHPMailer( true ); // true enables exceptions
$mail_config = Common::$config->email;

$state->mail &= $mail;
$state->token = ( $user ? $user->getVerificationToken() : null );
$state->user_id = $user_id;

try {
//Server settings
$mail->Timeout = 10; // default is 300 per RFC2821 $ 4.5.3.2
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = $mail_config->smtp_host;
$mail->SMTPAuth = !empty($mail_config->smtp_user);
$mail->Username = $mail_config->smtp_user;
$mail->Password = $mail_config->smtp_password;
$mail->SMTPSecure = $mail_config->smtp_tls ? 'tls' : '';
$mail->Port = $mail_config->smtp_port;

//Recipients
if (!empty($mail_config->recipient_from)) {
$mail->setFrom($mail_config->recipient_from, 'BNETDocs');
}

$mail->addAddress($email, $username);

if (!empty($mail_config->recipient_reply_to)) {
$mail->addReplyTo($mail_config->recipient_reply_to);
}

// Content
$mail->isHTML(true);
$mail->Subject = 'Account Activation';
$mail->CharSet = PHPMailer::CHARSET_UTF8;

ob_start();
(new Template($state, 'Email/User/Register.rich'))->render();
$mail->Body = ob_get_clean();

ob_start();
(new Template($state, 'Email/User/Register.plain'))->render();
$mail->AltBody = ob_get_clean();

$mail->send();

Logger::logEvent(
EventTypes::EMAIL_SENT,
$user_id,
getenv('REMOTE_ADDR'),
json_encode([
'from' => $mail->From,
'to' => $mail->getToAddresses(),
'reply_to' => $mail->getReplyToAddresses(),
'subject' => $mail->Subject,
'content_type' => $mail->ContentType,
'body' => $mail->Body,
'alt_body' => $mail->AltBody,
])
);

} catch (\Exception $e) {
$model->error = "EMAIL_FAILURE";
}
}

if (!$success) {
$model->error = "INTERNAL_ERROR";
} else {
$model->error = false;
}
Logger::logEvent(
EventTypes::USER_CREATED,
null,
$user_id,
getenv("REMOTE_ADDR"),
json_encode([
"error" => $model->error,
Expand Down
34 changes: 0 additions & 34 deletions src/libraries/Email/Debug.php

This file was deleted.

45 changes: 0 additions & 45 deletions src/libraries/Email/User/Register.php

This file was deleted.

Loading

0 comments on commit ff48ce3

Please sign in to comment.