Commit bde13c35 by Munteanu Petrisor

Add email send and verification with token and account activation via API

parent 0d6758a9
<?php <?php
function appConfig() { function appConfig()
return [ {
'displayErrorDetails' => true, return [
'logger' => [ 'host' => 'http://localhost/',
'name' => 'slim-app', 'displayErrorDetails' => true,
'path' => __DIR__ . '/../logs/app.log', 'logger' => [
], 'name' => 'slim-app',
'upload' => [ 'path' => __DIR__ . '/../logs/app.log',
'path' => __DIR__ . '/uploads', ],
], 'upload' => [
'db' => [ 'path' => __DIR__ . '/uploads',
'host' => '', ],
'port' => '', 'db' => [
'dbname' => '', 'host' => '',
'user' => '', 'port' => '',
'password' => '', 'dbname' => '',
], 'user' => '',
'api' => [ 'password' => '',
'address' => 'address', ],
'port' => 'port', 'api' => [
'email' => 'username', 'address' => 'address',
'password' => 'password', 'port' => 'port',
'cookieFile' => 'cookie file location', 'email' => 'username',
'certificate' => '' 'password' => 'password',
], 'cookieFile' => 'cookie file location',
'payments' => [ 'certificate' => ''
'name' => 'Romania Libera', ],
'email' => 'email', 'payments' => [
'terminal' => 'terminal id', 'name' => 'Romania Libera',
'merchant' => 'merchant id', 'email' => 'email',
'url' => 'url', 'terminal' => 'terminal id',
'backref' => 'callback endpoint', 'merchant' => 'merchant id',
'desc' => 'Anunt Romania Libera', 'url' => 'url',
'currency' => 'RON' 'backref' => 'callback endpoint',
], 'desc' => 'Anunt Romania Libera',
'admin' => [ 'currency' => 'RON'
'email' => '' ],
], 'admin' => [
'smtp' => [ 'email' => ''
"host" => "", ],
"username" => "", 'smtp' => [
"password" => "", "host" => "",
"secure" => "", "username" => "",
"port" => "", "password" => "",
] "secure" => "",
]; "port" => "",
]
];
} }
...@@ -22,4 +22,15 @@ CREATE TABLE IF NOT EXISTS `quotes` ...@@ -22,4 +22,15 @@ CREATE TABLE IF NOT EXISTS `quotes`
KEY `status_index` (`status`) KEY `status_index` (`status`)
) ENGINE = InnoDB ) ENGINE = InnoDB
DEFAULT CHARSET = latin1 DEFAULT CHARSET = latin1
AUTO_INCREMENT = 100; AUTO_INCREMENT = 100;
\ No newline at end of file
CREATE TABLE IF NOT EXISTS `register_requests`
(
`user_id` int(11) NOT NULL,
`email` varchar(255) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`),
KEY `token_index` (`token`),
KEY `email_index` (`email`)
) ENGINE = InnoDB
DEFAULT CHARSET = latin1;
\ No newline at end of file
...@@ -46,6 +46,7 @@ class API ...@@ -46,6 +46,7 @@ class API
$this->db = $c->get('db'); $this->db = $c->get('db');
$this->query_factory = new QueryFactory('mysql'); $this->query_factory = new QueryFactory('mysql');
$this->session = $c->get('session'); $this->session = $c->get('session');
$this->router = $c->router;
} }
// cookie file for client // cookie file for client
$this->cookieFileClient = "/tmp/cookieFileClient.txt"; $this->cookieFileClient = "/tmp/cookieFileClient.txt";
...@@ -167,8 +168,8 @@ class API ...@@ -167,8 +168,8 @@ class API
} }
} }
// var_dump($response); // var_dump($response, $info);
// die(); // die();
if ($info['http_code'] != 200) { if ($info['http_code'] != 200) {
throw new Exception('Internal API error'); throw new Exception('Internal API error');
...@@ -505,10 +506,10 @@ class API ...@@ -505,10 +506,10 @@ class API
$mail->setFrom($from); $mail->setFrom($from);
$mail->addAddress($to); // Add a recipient $mail->addAddress($to); // Add a recipient
//Content //Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject; $mail->Subject = $subject;
$mail->Body = $body; $mail->Body = $body;
$mail->AltBody = $altBody; $mail->AltBody = $altBody;
$mail->isHTML(true); // Set email format to HTML
$mail->send(); $mail->send();
} catch (PException $e) { } catch (PException $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
...@@ -878,7 +879,7 @@ class API ...@@ -878,7 +879,7 @@ class API
{ {
// TODO validation on backend // TODO validation on backend
$data = [ $data = [
"active" => true, "active" => false,
"admin" => false, "admin" => false,
"password" => $params["password"], "password" => $params["password"],
"email" => $params["email"], "email" => $params["email"],
...@@ -897,7 +898,70 @@ class API ...@@ -897,7 +898,70 @@ class API
] ]
]; ];
return $this->Request('POST', '/users', $data); $resp = $this->Request('POST', '/users', $data);
if ($resp['info']['http_code'] == 200) {
$user = $resp['data'];
$token = bin2hex(openssl_random_pseudo_bytes(16));
$insert = $this->query_factory->newInsert();
$insert->into('register_requests')->cols([
'token' => $token,
'email' => $user['email'],
'user_id' => $user['id']
]);
$sth = $this->db->prepare($insert->getStatement());
$sth->execute($insert->getBindValues());
// TODO find a way to pass HOST from config
$route = $this->router->pathFor('verificare-cont');
// TODO find a way to add a file as an email template
$emailBody = "Trebuie sa verificati contul: <a href='" . $route . '?token=' . $token . "' >Verificare cont</a>";
$this::SendMail($this->admin["email"], $user['email'], "Verificare inregistrare", $emailBody);
}
return $resp;
}
function VerifyAccount($params)
{
if (!empty($params) && !empty($params['token'])) {
$select = $this->query_factory->newSelect();
$select->from('register_requests')->cols([
'*'
])
->where("token = :token")
->bindValues([
'token' => $params['token']
]);
$result = $this->db->fetchOne($select->getStatement(), $select->getBindValues());
if ($result) {
$resp = $this->Request('PUT', "/users/{$result['user_id']}", [
'active' => true,
"groupID" => 32,
]);
if ($resp['info']['http_code'] == 200) {
$delete = $this->query_factory->newDelete();
$delete->from('register_requests')->where('user_id = :user_id')
->bindValues([
"user_id" => $result['user_id'],
]);
$stmt = $this->db->prepare($delete->getStatement());
$stmt->execute($delete->getBindValues());
return true;
}
}
}
return false;
} }
} }
......
...@@ -197,6 +197,28 @@ $app->get('/inregistrare', function ($request, $response, $args) { ...@@ -197,6 +197,28 @@ $app->get('/inregistrare', function ($request, $response, $args) {
})->setName('inregistrare'); })->setName('inregistrare');
$app->get('/verificare-cont/', function ($request, $response, $args) {
if (!$this->session->exists('user_id')) {
$params = $request->getQueryParams();
if (!empty($params)) {
$api = new API($this);
$resp = $api->VerifyAccount($params);
// TODO check why the messages are not working on auth page
if ($resp) {
$this->flash->addMessage("success", "Verificarea contului a fost efectuata.");
} else {
$this->flash->addMessage("error", "Verificarea contului a esuat.");
}
}
return $response->withRedirect($this->router->pathFor('autentificare'), 303);
}
return $response->withRedirect($this->router->pathFor('home'), 303);
})->setName('verificare-cont');
$app->post('/inregistrare', function ($request, $response, $args) { $app->post('/inregistrare', function ($request, $response, $args) {
if (!$this->session->exists('user_id')) { if (!$this->session->exists('user_id')) {
$api = new API($this); $api = new API($this);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment