Commit bde13c35 by Munteanu Petrisor

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

parent 0d6758a9
<?php
function appConfig() {
return [
'displayErrorDetails' => true,
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/../logs/app.log',
],
'upload' => [
'path' => __DIR__ . '/uploads',
],
'db' => [
'host' => '',
'port' => '',
'dbname' => '',
'user' => '',
'password' => '',
],
'api' => [
'address' => 'address',
'port' => 'port',
'email' => 'username',
'password' => 'password',
'cookieFile' => 'cookie file location',
'certificate' => ''
],
'payments' => [
'name' => 'Romania Libera',
'email' => 'email',
'terminal' => 'terminal id',
'merchant' => 'merchant id',
'url' => 'url',
'backref' => 'callback endpoint',
'desc' => 'Anunt Romania Libera',
'currency' => 'RON'
],
'admin' => [
'email' => ''
],
'smtp' => [
"host" => "",
"username" => "",
"password" => "",
"secure" => "",
"port" => "",
]
];
function appConfig()
{
return [
'host' => 'http://localhost/',
'displayErrorDetails' => true,
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/../logs/app.log',
],
'upload' => [
'path' => __DIR__ . '/uploads',
],
'db' => [
'host' => '',
'port' => '',
'dbname' => '',
'user' => '',
'password' => '',
],
'api' => [
'address' => 'address',
'port' => 'port',
'email' => 'username',
'password' => 'password',
'cookieFile' => 'cookie file location',
'certificate' => ''
],
'payments' => [
'name' => 'Romania Libera',
'email' => 'email',
'terminal' => 'terminal id',
'merchant' => 'merchant id',
'url' => 'url',
'backref' => 'callback endpoint',
'desc' => 'Anunt Romania Libera',
'currency' => 'RON'
],
'admin' => [
'email' => ''
],
'smtp' => [
"host" => "",
"username" => "",
"password" => "",
"secure" => "",
"port" => "",
]
];
}
......@@ -22,4 +22,15 @@ CREATE TABLE IF NOT EXISTS `quotes`
KEY `status_index` (`status`)
) ENGINE = InnoDB
DEFAULT CHARSET = latin1
AUTO_INCREMENT = 100;
\ No newline at end of file
AUTO_INCREMENT = 100;
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
$this->db = $c->get('db');
$this->query_factory = new QueryFactory('mysql');
$this->session = $c->get('session');
$this->router = $c->router;
}
// cookie file for client
$this->cookieFileClient = "/tmp/cookieFileClient.txt";
......@@ -167,8 +168,8 @@ class API
}
}
// var_dump($response);
// die();
// var_dump($response, $info);
// die();
if ($info['http_code'] != 200) {
throw new Exception('Internal API error');
......@@ -505,10 +506,10 @@ class API
$mail->setFrom($from);
$mail->addAddress($to); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $altBody;
$mail->isHTML(true); // Set email format to HTML
$mail->send();
} catch (PException $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
......@@ -878,7 +879,7 @@ class API
{
// TODO validation on backend
$data = [
"active" => true,
"active" => false,
"admin" => false,
"password" => $params["password"],
"email" => $params["email"],
......@@ -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) {
})->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) {
if (!$this->session->exists('user_id')) {
$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