-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.php
More file actions
329 lines (264 loc) · 12.9 KB
/
classes.php
File metadata and controls
329 lines (264 loc) · 12.9 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
require_once "./db.php";
class Cookie {
function setCookie($username, $id, $password) {
// Function to set cookies
// Set cookies
setcookie("belongs", hash('sha256', $username), time() + (3600 * 24 * 30));
setcookie("key", hash('sha256', $id) , time() + (3600 * 24 * 30));
}
function validateCookie($cookie) {
// Function to validate cookies
// Check if cookies are exists
if (isset($cookie["belongs"]) && isset($cookie["key"])) {
// Check if cookie values are valid
global $db_conn;
// Select rows in a table
$res = mysqli_query($db_conn, "SELECT * FROM user");
// Run loop for all rows in table
while ($row = mysqli_fetch_assoc($res)) {
// Check if username, id, and password match with row data
if (hash('sha256', $row["username"]) === $cookie["belongs"] &&
hash('sha256', $row["id"]) === $cookie["key"]) {
// Check if login session was made, if not create user login session
if (!isset($_SESSION["login"])) $_SESSION["login"] = $row["username"];
return true;
}
}
}
return false;
}
function updateCookie($cookie) {
// Function to update cookie
// Check if cookies are exists
if (isset($cookie["belongs"]) && isset($cookie["key"])) {
// Update cookies
setcookie("belongs", $cookie["belongs"], time() + (36000 * 24 * 30));
setcookie("key", $cookie["key"], time() + (36000 * 24 * 30));
}
}
function deleteCookie($cookie) {
// Function to delete cookies
// Check if cookies are exists
if (isset($cookie["belongs"]) && isset($cookie["key"])) {
setcookie("belongs", $cookie["belongs"], time());
setcookie("key", $cookie["key"], time());
}
}
}
class User {
function validateRegister($data) {
// Function to validate user registration
global $db_conn;
global $db_conn_task;
// Get name input
$name = strtolower($data["name"]);
// Check if name field is empty
if (empty(trim($name))) return 'Name field can\\\'t be empty';
// Formatting name
for ($idx = 0; $idx < strlen($name); $idx++) {
if (!$idx || $name[$idx - 1] === ' ') $name[$idx] = strtoupper($name[$idx]);
}
// Get username input
$username = ucfirst(strtolower($data["username"]));
// Check if name or username already exist in database
if (mysqli_num_rows(mysqli_query($db_conn, "SELECT * FROM user WHERE name = '$name'"))) {
return 'Name already regsitered';
}
else if (mysqli_num_rows(mysqli_query($db_conn, "SELECT * FROM user WHERE username = '$username'"))) {
return 'Username already exist. Plese use other username';
}
// Get password and confirmation password input
$password1 = mysqli_real_escape_string($db_conn, $data["password1"]);
$password2 = mysqli_real_escape_string($db_conn, $data["password2"]);
// Check if password and confirmation password filed is empty
if (empty(trim($password1))) return 'Password field can\\\'t be empty';
else if (empty(trim($password2))) return 'Confirmation password field can\\\'t be empty';
// Check password and confirmation password matchesness
if ($password1 !== $password2) return 'Password not match';
// Encrpyt password
$password1 = password_hash($password1, PASSWORD_DEFAULT);
// Append data into database table
mysqli_query($db_conn, "INSERT INTO user VALUES('', '$name', '$username', '$password1')");
// Check whenever the data appended
$isAppended = mysqli_affected_rows($db_conn);
// Make a new task table for user
mysqli_query($db_conn_task, "CREATE TABLE $username (
id INT AUTO_INCREMENT PRIMARY KEY,
task VARCHAR(255) NOT NULL,
deadline VARCHAR(255) NOT NULL,
isDone Int(1) NOT NULL
)");
// Return append data status message
return $isAppended ? 'Account registered successfully' : 'Failed register account';
}
function validateLogin($data) {
// Function to validate user login
global $db_conn;
// Get username and password input
$username = ucfirst(strtolower($data["username"]));
$password = mysqli_real_escape_string($db_conn, $data["password"]);
// Get account datas from database
$account = mysqli_fetch_assoc(mysqli_query($db_conn, "SELECT * FROM user WHERE username = '$username'"));
// Check if account is exist
if ($account === NULL) return 'Account doesn\\\'t exist';
// Check if password is match
if (!password_verify($password, $account["password"])) return 'Password wrong';
// Make session to pass session checkin in todolist page
$_SESSION["login"] = $username;
// Check if remember me check box is checked
if (isset($_POST["keep-me-logged-in"])) {
$cookie_maker = new Cookie();
// Set cookies
$cookie_maker->setCookie($username, $account["id"], $password);
}
// Return sucess message
return 'Login success';
}
}
class Task {
static function sortDate($date1, $date2) {
// Fucntion to pick which date is the newest
if (strtotime($date1["deadline"]) == strtotime($date2["deadline"])) return 0;
return strtotime($date1["deadline"]) > strtotime($date2["deadline"]) ? 1 : -1;
}
static function taskTemplate($id, $taskName, $deadline, $isDone) {
// Create element class, status, and mark status
$status = $isDone ? "is-done" : "is-not-done";
$actionStatus = $isDone ? "done-action" : "";
$titleContainer = ($isDone) ? "
<span>$taskName</span>
" : "
<input type='text' value='$taskName' name='task-title' disabled required>
";
return "
<form method='post' action=''>
<!-- Task contents -->
<div>
<!-- Check box button -->
<input type='checkbox' onClick='this.form.submit()' name='is-done-btn'>
<div class='content-wrapper'>
<div class='content'>
<div>
<!-- Task title section -->
<div>
$titleContainer
<input type='hidden' value='$id' name='task-id'>
<input type='hidden' value='$isDone' name='task-status'>
<button type='submit' name='rename-task-btn'>Rename</button>
</div>
<!-- Deadline section -->
<div class='$status'>
<box-icon name='calendar-event'></box-icon>
<label for='calendar-event'>$deadline</label>
</div>
</div>
<!-- Action icons -->
<div class='$actionStatus'>
<!-- Rename icon -->
<box-icon type='solid' name='edit'></box-icon>
<!-- Remove button -->
<button type='submit' name='delete-task-btn'>
<box-icon name='trash'></box-icon>
</button>
</div>
</div>
</div>
</div>
</form>
";
}
function displayTasks($status) {
// Function to display all user tasks from database
global $db_conn_task;
// Get username
$username = strtolower($_SESSION["login"]);
// Get tasks from username database table
$res = mysqli_query($db_conn_task, "SELECT * FROM $username");
$taskArr = []; // This array will hold user tasks
while ($row = mysqli_fetch_assoc($res)) {
// Check status to display
if ($status === "done" && $row['isDone'] ||
$status === "not-done" && !$row['isDone']) {
$taskArr[] = $row; // Push task into array
}
}
// Sort array using sortDate function
usort($taskArr, "Task::sortDate");
foreach ($taskArr as $task) {
// Get array values
$keys = array_values($task); // Get current array values
// Get id, task name, deadline, and status
$id = $keys[0];
$task = $keys[1];
$deadline = $keys[2];
$isDone = $keys[3];
echo Task::taskTemplate($id, $task, $deadline, $isDone);
}
// Print no task indicator as the task counter is 0
if (empty($taskArr)) {
if ($status === "not-done") echo "There's no task to do..";
else if ($status === "done") echo "There's no completed task..";
}
}
function validateTask($data) {
// Function to validate task
global $db_conn;
global $db_conn_task;
// Get task name and username
$task = ucfirst(strtolower($data["task-title"]));
// Select all username from user table
$res = mysqli_query($db_conn, "SELECT username FROM user");
// Run a loop to all username
while ($row = mysqli_fetch_assoc($res)) {
// Formating username to database title format
$username = strtolower($row["username"]);
// Search for same task name from other user task table
$res = mysqli_query($db_conn_task, "SELECT task FROM $username WHERE task = '$task'");
// Check if same task name exist in username task database
if (mysqli_num_rows($res)) return $username;
}
// Return false value if there's no same task name
return false;
}
function createTask($data) {
// Function to create task
global $db_conn_task;
// Get username, task name, and deadline
$username = strtolower($_SESSION["login"]);
$taskName = ucfirst(strtolower($data["task-title"]));
$deadline = $data["deadline"];
// Push task into user task database
mysqli_query($db_conn_task, "INSERT INTO $username VALUES('', '$taskName', '$deadline', 0)");
}
function markTask($data) {
// Function to mark or unmark a task
global $db_conn_task;
// Get username and atask id
$username = $_SESSION["login"];
$id = $data["task-id"];
$newStatus = (float)!$data["task-status"];
// Update task status in user task table database
mysqli_query($db_conn_task, "UPDATE $username SET isDone = $newStatus WHERE id = $id");
}
function renameTask($data) {
// Function to rename task
global $db_conn_task;
// Get username, id, and task name
$username = strtolower($_SESSION["login"]);
$id = (int)$data["task-id"];
$newName = ucfirst(strtolower($data["task-title"]));
// Update task name in user task table
mysqli_query($db_conn_task, "UPDATE $username SET task = '$newName' WHERE id = $id");
}
function deleteTask($data) {
// Function to delete task
global $db_conn_task;
// Get username and task id
$username = strtolower($_SESSION["login"]);
$taskId = (int)$data["task-id"];
// Delete task using it's id
mysqli_query($db_conn_task, "DELETE FROM $username WHERE id = $taskId");
}
}
?>