-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
173 lines (147 loc) · 5.73 KB
/
functions.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
session_start();
include("./config/db.php");
// RegExp for validatation of email and phone number
$emailPattern = '/^\w{2,}@\w{2,}\.\w{2,4}$/';
$mobilePattern = "/^[0][0-9]{10}$/";
function escape($string)
{
global $conn;
return mysqli_real_escape_string($conn, trim($string));
}
function isUserExists($email)
{
global $conn;
$sanitized_email = escape($email);
$query = "SELECT * FROM clients WHERE email = '{$sanitized_email}'";
$select_query = mysqli_query($conn, $query);
if (!$select_query) {
die("Query failed: " . mysqli_error($conn));
}
if (mysqli_num_rows($select_query) > 0) {
return true;
} else {
return false;
}
}
function checkOtherclients($email, $user_id = null)
{
global $conn;
$sanitized_email = escape($email);
$query = "SELECT * FROM clients WHERE email = '{$sanitized_email}'";
if ($user_id !== null) {
$sanitized_user_id = escape($user_id);
$query .= " AND client_id != '{$sanitized_user_id}'";
}
$select_query = mysqli_query($conn, $query);
if (!$select_query) {
die("Query failed: " . mysqli_error($conn));
}
return mysqli_num_rows($select_query) > 0;
}
function confirmQuery($result)
{
global $conn;
if (!$result) {
die("QUERY FAILED" . mysqli_error($conn));
}
return true;
}
// Register Logic
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['register'])) {
if (!empty($_POST["firstName"]) && !empty($_POST["lastName"]) && !empty($_POST["email"]) && !empty($_POST["phone"]) && !empty($_POST["password"])) {
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$password = $_POST["password"];
$firstName = escape($firstName);
$lastName = escape($lastName);
$email = escape($email);
$phone = escape($phone);
$password = escape($password);
if (isUserExists($email)) {
header("Location: sign-up.php?userExists");
exit();
} else {
if (preg_match($emailPattern, $email) && preg_match($mobilePattern, $phone)) {
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$query = "INSERT INTO clients (firstName, lastName, email, phone, password, role) VALUES ('$firstName', '$lastName', '$email', '$phone', '$hashedPassword', 'client')";
$reg_user = mysqli_query($conn, $query);
if (!$reg_user) {
die("QUERY FAILED" . mysqli_error($conn));
}
header("Location: sign-up.php?success");
exit();
}
}
}
}
}
// Login Logic
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['login'])) {
if (!empty($_POST['email']) && !empty($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$email = escape($email);
$password = escape($password);
if (preg_match($emailPattern, $email)) {
$query = "SELECT * FROM clients WHERE email = '{$email}'";
$result = mysqli_query($conn, $query);
if ($result) {
$row = mysqli_fetch_assoc($result);
if ($row) {
if (password_verify($password, $row['password'])) {
$_SESSION['user_id'] = $row["client_id"];
$_SESSION['user_firstname'] = $row['firstName'];
$_SESSION['user_lastname'] = $row['lastName'];
$_SESSION['user_phone'] = $row['phone'];
$_SESSION['user_email'] = $row['email'];
$_SESSION['user_role'] = $row['role'];
session_regenerate_id(true);
header("Location: index.php?loginSuccess");
exit();
} else {
header("Location: sign-in.php?loginFailed");
exit();
}
} else {
header("Location: sign-in.php?loginFailed");
exit();
}
} else {
header("Location: sign-in.php?loginFailed");
exit();
}
}
}
}
}
// Create A Reservation
if (isset($_POST['reservation'])) {
$client_id = $_SESSION['user_id'];
$table_id = $_POST['table'];
$reservation_date = $_POST['reservation_date'];
$reservation_time = $_POST['reservation_time'];
$num_guests = $_POST['num_guests'];
$message = $_POST['message'];
$status = "pending";
$client_id = escape($client_id);
$table_id = escape($table_id);
$reservation_date = escape($reservation_date);
$reservation_time = escape($reservation_time);
$num_guests = escape($num_guests);
$message = escape($message);
$query = "INSERT INTO reservations (client_id, table_id, num_guests, date, time, status, message) VALUES ('$client_id', '$table_id', '$num_guests', '$reservation_date', '$reservation_time', '$status', '$message')";
$reg_reservation = mysqli_query($conn, $query);
if (confirmQuery($reg_reservation)) {
$table_query = "UPDATE tables SET status = 'full' WHERE table_id = {$table_id}";
$updated_query = mysqli_query($conn, $table_query);
if (confirmQuery($updated_query)) {
header("Location: reservation.php?reservationSuccess");
exit();
}
}
}