Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
app-monitorul-oficial
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
romania-libera
app-monitorul-oficial
Commits
bde13c35
Commit
bde13c35
authored
Mar 16, 2019
by
Munteanu Petrisor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add email send and verification with token and account activation via API
parent
0d6758a9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
53 deletions
+152
-53
config_sample.php
config_sample.php
+48
-46
init.sql
private/init.sql
+13
-2
api.php
src/api.php
+69
-5
routes.php
src/routes.php
+22
-0
No files found.
config_sample.php
View file @
bde13c35
<?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"
=>
""
,
]
];
}
private/init.sql
View file @
bde13c35
...
...
@@ -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
src/api.php
View file @
bde13c35
...
...
@@ -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"
=>
tru
e
,
"active"
=>
fals
e
,
"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
;
}
}
...
...
src/routes.php
View file @
bde13c35
...
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment