-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
152 lines (131 loc) · 4.39 KB
/
server.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
<?php
require_once 'backend/DBHandler.php';
session_start();
function post($name) {
return htmlspecialchars($_POST[$name]);
}
$action = post('action');
$response = [];
switch ($action) {
case 'create-task':
$task = createTask(post('userid'), post('name'), post('status'), 0);
if ($task != null) {
$response['success'] = 'Task added.';
ob_start();
require __DIR__ . '/frontend/card.php';
$response['html'] = ob_get_clean();
} else {
$response['error'] = 'Could not create task.';
}
break;
case 'delete-task':
$return = deleteTask(post('taskid'));
if ($return == false) {
$response['error'] = 'Could not delete task.';
} else {
$response['success'] = 'Task deleted.';
}
break;
case 'update-task-minutes':
$return = updateTaskMinutes(post('taskid'), post('userid'), post('totalminutes'));
if ($return === false) {
$response['error'] = 'Could not update task information. Refresh your site.';
} else {
$response['success'] = 'Task information has been updated.';
}
break;
case 'update-task-name':
$return = updateTaskName(post('taskid'), post('name'));
if ($return === false) {
$response['error'] = 'Could not save the name.';
} else {
$response['success'] = 'Task name has been updated.';
}
break;
case 'update-task-status':
$return = updateTaskStatus(post('taskid'), post('status'));
if ($return === false) {
$response['error'] = 'Could not save the status.';
} else {
$response['success'] = 'Task status has been updated.';
}
break;
case 'get-active-tasks':
$tasks = [];
$tasks = getAllUserTasks(post('userid'), 0);
ob_start();
foreach ($tasks as $task) {
require __DIR__ . '/frontend/card.php';
}
$response['html'] = ob_get_clean();
break;
case 'get-completed-tasks':
$tasks = [];
$tasks = getAllUserTasks(post('userid'), 1);
ob_start();
foreach ($tasks as $task) {
require __DIR__ . '/frontend/card.php';
}
$response['html'] = ob_get_clean();
break;
case 'signup':
$user = getUserByName(post('username'));
if ($user == null) {
$userid = createUser(post('username'), post('password'));
if ($userid != -1) {
// if there were any localtasks before sign up - create them
if (isset($_POST['tasks'])) {
foreach ($_POST['tasks'] as $item) {
createTask($userid, htmlspecialchars($item['name']), htmlspecialchars($item['status']), htmlspecialchars($item['minutes']));
}
}
$response['success'] = "User has been registered.";
$_SESSION['userid'] = $userid;
$_SESSION['eggtimer_start'] = 25;
} else {
$response['error'] = "An unknown error occured. Try again later.";
}
} else {
$response['error'] = "This email address is already registered.";
}
break;
case 'login':
$user = getUserByName(post('username'));
if ($user != null) {
$user = authorizeUser(post('username'), post('password'));
if ($user != null) {
// if there were any localtasks before login - create them
if (isset($_POST['tasks'])) {
foreach ($_POST['tasks'] as $item) {
createTask($user->id, htmlspecialchars($item['name']), htmlspecialchars($item['status']), htmlspecialchars($item['minutes']));
}
}
$response['success'] = "User has logged in.";
$_SESSION['userid'] = $user->id;
$_SESSION['eggtimer_start'] = $user->eggtimer_start;
} else {
$response['error'] = "Wrong password.";
}
} else {
$response['error'] = "This email address is not registered.";
}
break;
case 'update-eggtimer-minute-setting':
$return = updateUserEggtimerStart(post('userid'), post('eggtimer_start'));
if ($return === false) {
$response['error'] = 'Could not update eggtimer start value.';
} else {
$_SESSION['eggtimer_start'] = post('eggtimer_start');
$response['success'] = 'Eggtimer start value has been updated.';
}
break;
case 'update-password':
$return = updateUserPassword(post('userid'), post('password'));
if ($return === false) {
$response['error'] = 'Could not update password.';
} else {
$response['success'] = 'Password has been updated.';
}
break;
}
echo json_encode($response);