-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
68 lines (53 loc) · 1.87 KB
/
Copy pathfunctions.php
File metadata and controls
68 lines (53 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
if(session_status() == PHP_SESSION_NONE) {
session_start();
}
function getBooks($sql) {
return $sql->query("SELECT books.*, users.name AS user_name
FROM books
JOIN users ON books.user_id = users.id
ORDER BY books.id DESC")->fetchAll();
}
function getBook($sql, $id) {
$stmt = $sql->prepare("SELECT books.*, users.name AS user_name
FROM books
JOIN users ON books.user_id = users.id
WHERE books.id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
function addBook($sql, $title, $author, $user_id) {
$stmt = $sql->prepare("INSERT INTO books (title, author, user_id) VALUES (?, ?, ?)");
$stmt->execute([$title, $author, $user_id]);
}
function editBook($sql, $id, $title, $author) {
$stmt = $sql->prepare("UPDATE books SET title = ?, author = ? WHERE id = ?");
$stmt->execute([$title, $author, $id]);
}
function deleteBook($sql, $id) {
$stmt = $sql->prepare("DELETE FROM books WHERE id = ?");
$stmt->execute([$id]);
}
function registerUser($sql, $name, $email, $password) {
$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $sql->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->execute([$name, $email, $hash]);
}
function loginUser($sql, $email, $password) {
$stmt = $sql->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if($user && password_verify($password, $user["password"])) {
$_SESSION["user_id"] = $user["id"];
$_SESSION["name"] = $user["name"];
return true;
}
return false;
}
function isLoggedIn() {
return isset($_SESSION["user_id"]);
}
function isOwner($book) {
return isLoggedIn() && $_SESSION["user_id"] == $book["user_id"];
}
?>