<?php
class
DbHandler {
private
$conn
;
function
__construct() {
require_once
dirname(
__FILE__
) .
'./DbConnect.php'
;
$db
=
new
DbConnect();
$this
->conn =
$db
->connect();
}
public
function
createUser(
$name
,
$email
,
$password
) {
require_once
'PassHash.php'
;
$response
=
array
();
if
(!
$this
->isUserExists(
$email
)) {
$password_hash
= PassHash::hash(
$password
);
$api_key
=
$this
->generateApiKey();
$stmt
=
$this
->conn->prepare(
"INSERT INTO users(name, email, password_hash, api_key, status) values(?, ?, ?, ?, 1)"
);
$stmt
->bind_param(
"ssss"
,
$name
,
$email
,
$password_hash
,
$api_key
);
$result
=
$stmt
->execute();
$stmt
->close();
if
(
$result
) {
return
USER_CREATED_SUCCESSFULLY;
}
else
{
return
USER_CREATE_FAILED;
}
}
else
{
return
USER_ALREADY_EXISTED;
}
return
$response
;
}
public
function
checkLogin(
$email
,
$password
) {
$stmt
=
$this
->conn->prepare(
"SELECT password_hash FROM users WHERE email = ?"
);
$stmt
->bind_param(
"s"
,
$email
);
$stmt
->execute();
$stmt
->bind_result(
$password_hash
);
$stmt
->store_result();
if
(
$stmt
->num_rows > 0) {
$stmt
->fetch();
$stmt
->close();
if
(PassHash::check_password(
$password_hash
,
$password
)) {
return
TRUE;
}
else
{
return
FALSE;
}
}
else
{
$stmt
->close();
return
FALSE;
}
}
private
function
isUserExists(
$email
) {
$stmt
=
$this
->conn->prepare(
"SELECT id from users WHERE email = ?"
);
$stmt
->bind_param(
"s"
,
$email
);
$stmt
->execute();
$stmt
->store_result();
$num_rows
=
$stmt
->num_rows;
$stmt
->close();
return
$num_rows
> 0;
}
public
function
getUserByEmail(
$email
) {
$stmt
=
$this
->conn->prepare(
"SELECT name, email, api_key, status, created_at FROM users WHERE email = ?"
);
$stmt
->bind_param(
"s"
,
$email
);
if
(
$stmt
->execute()) {
$user
=
$stmt
->get_result()->fetch_assoc();
$stmt
->close();
return
$user
;
}
else
{
return
NULL;
}
}
public
function
getApiKeyById(
$user_id
) {
$stmt
=
$this
->conn->prepare(
"SELECT api_key FROM users WHERE id = ?"
);
$stmt
->bind_param(
"i"
,
$user_id
);
if
(
$stmt
->execute()) {
$api_key
=
$stmt
->get_result()->fetch_assoc();
$stmt
->close();
return
$api_key
;
}
else
{
return
NULL;
}
}
public
function
getUserId(
$api_key
) {
$stmt
=
$this
->conn->prepare(
"SELECT id FROM users WHERE api_key = ?"
);
$stmt
->bind_param(
"s"
,
$api_key
);
if
(
$stmt
->execute()) {
$user_id
=
$stmt
->get_result()->fetch_assoc();
$stmt
->close();
return
$user_id
;
}
else
{
return
NULL;
}
}
public
function
isValidApiKey(
$api_key
) {
$stmt
=
$this
->conn->prepare(
"SELECT id from users WHERE api_key = ?"
);
$stmt
->bind_param(
"s"
,
$api_key
);
$stmt
->execute();
$stmt
->store_result();
$num_rows
=
$stmt
->num_rows;
$stmt
->close();
return
$num_rows
> 0;
}
private
function
generateApiKey() {
return
md5(uniqid(rand(), true));
}
public
function
createTask(
$user_id
,
$task
) {
$stmt
=
$this
->conn->prepare(
"INSERT INTO tasks(task) VALUES(?)"
);
$stmt
->bind_param(
"s"
,
$task
);
$result
=
$stmt
->execute();
$stmt
->close();
if
(
$result
) {
$new_task_id
=
$this
->conn->insert_id;
$res
=
$this
->createUserTask(
$user_id
,
$new_task_id
);
if
(
$res
) {
return
$new_task_id
;
}
else
{
return
NULL;
}
}
else
{
return
NULL;
}
}
public
function
getTask(
$task_id
,
$user_id
) {
$stmt
=
$this
->conn->prepare(
"SELECT t.id, t.task, t.status, t.created_at from tasks t, user_tasks ut WHERE t.id = ? AND ut.task_id = t.id AND ut.user_id = ?"
);
$stmt
->bind_param(
"ii"
,
$task_id
,
$user_id
);
if
(
$stmt
->execute()) {
$task
=
$stmt
->get_result()->fetch_assoc();
$stmt
->close();
return
$task
;
}
else
{
return
NULL;
}
}
public
function
getAllUserTasks(
$user_id
) {
$stmt
=
$this
->conn->prepare(
"SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?"
);
$stmt
->bind_param(
"i"
,
$user_id
);
$stmt
->execute();
$tasks
=
$stmt
->get_result();
$stmt
->close();
return
$tasks
;
}
public
function
updateTask(
$user_id
,
$task_id
,
$task
,
$status
) {
$stmt
=
$this
->conn->prepare(
"UPDATE tasks t, user_tasks ut set t.task = ?, t.status = ? WHERE t.id = ? AND t.id = ut.task_id AND ut.user_id = ?"
);
$stmt
->bind_param(
"siii"
,
$task
,
$status
,
$task_id
,
$user_id
);
$stmt
->execute();
$num_affected_rows
=
$stmt
->affected_rows;
$stmt
->close();
return
$num_affected_rows
> 0;
}
public
function
deleteTask(
$user_id
,
$task_id
) {
$stmt
=
$this
->conn->prepare(
"DELETE t FROM tasks t, user_tasks ut WHERE t.id = ? AND ut.task_id = t.id AND ut.user_id = ?"
);
$stmt
->bind_param(
"ii"
,
$task_id
,
$user_id
);
$stmt
->execute();
$num_affected_rows
=
$stmt
->affected_rows;
$stmt
->close();
return
$num_affected_rows
> 0;
}
public
function
createUserTask(
$user_id
,
$task_id
) {
$stmt
=
$this
->conn->prepare(
"INSERT INTO user_tasks(user_id, task_id) values(?, ?)"
);
$stmt
->bind_param(
"ii"
,
$user_id
,
$task_id
);
$result
=
$stmt
->execute();
$stmt
->close();
return
$result
;
}
}
?>