-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
delete.php
196 lines (173 loc) · 5.83 KB
/
delete.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
// written by sqall
// twitter: https://twitter.com/sqall01
// blog: https://h4des.org/blog
// github: https://github.com/sqall01
//
// Licensed under the GNU Affero General Public License, version 3.
require_once(__DIR__ . "/config/config.php");
require_once(__DIR__ . "/lib/helper.php");
require_once(__DIR__ . "/lib/objects.php");
// Set global settings.
header("Content-type: application/json");
date_default_timezone_set("UTC");
// Start session.
$cookie_conf = session_get_cookie_params();
session_set_cookie_params($cookie_conf["lifetime"], // lifetime
$cookie_conf["path"], // path
$cookie_conf["domain"], // domain
TRUE, // secure
TRUE); // httponly
session_start();
// Check if needed data is given.
if(!isset($_GET["mode"])
|| !isset($_GET["device"])) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Mode or device not set.";
die(json_encode($result));
}
$mysqli = new mysqli(
$config_mysql_server,
$config_mysql_username,
$config_mysql_password,
$config_mysql_database,
$config_mysql_port);
if($mysqli->connect_errno) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->connect_error;
die(json_encode($result));
}
// Get user id.
$user_id = auth_user($mysqli);
if($user_id === -1 || $user_id === -4) {
chasr_session_destroy();
$result = array();
$result["code"] = ErrorCodes::AUTH_ERROR;
$result["msg"] = "Wrong user or password.";
die(json_encode($result));
}
else if($user_id === -2) {
chasr_session_destroy();
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Database error during authentication.";
die(json_encode($result));
}
else if($user_id === -3) {
chasr_session_destroy();
$result = array();
$result["code"] = ErrorCodes::SESSION_EXPIRED;
$result["msg"] = "Authenticated session expired.";
die(json_encode($result));
}
// Check if the mode is supported.
switch($_GET["mode"]) {
case "device":
case "position":
break;
default:
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Mode unknown.";
die(json_encode($result));
}
switch($_GET["mode"]) {
// Delete whole device and all its data.
case "device":
// Get id of device.
$device_id = get_device_id($mysqli, $user_id, $_GET["device"]);
if($device_id === -1) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Device does not exist.";
die(json_encode($result));
}
else if($device_id === -2) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Error while fetching device id.";
die(json_encode($result));
}
// Delete gps data.
$delete_gps = "DELETE FROM chasr_gps WHERE "
. "users_id="
. intval($user_id)
. " AND "
. "device_id="
. intval($device_id);
$result = $mysqli->query($delete_gps);
if(!$result) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->error;
die(json_encode($result));
}
// Delete device.
$delete_device = "DELETE FROM chasr_device WHERE "
. "users_id="
. intval($user_id)
. " AND "
. "id="
. intval($device_id);
$result = $mysqli->query($delete_device);
if(!$result) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->error;
die(json_encode($result));
}
break;
// Delete single position.
case "position":
// Check if the time is given.
if(!isset($_GET["utctime"])) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Time not set.";
die(json_encode($result));
}
// Get id of device.
$device_id = get_device_id($mysqli, $user_id, $_GET["device"]);
if($device_id === -1) {
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Device does not exist.";
die(json_encode($result));
}
else if($device_id === -2) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = "Error while fetching device id.";
die(json_encode($result));
}
// Delete gps data.
$delete_gps = "DELETE FROM chasr_gps WHERE "
. "users_id="
. intval($user_id)
. " AND "
. "device_id="
. intval($device_id)
. " AND "
. "utctime="
. intval($_GET["utctime"]);
$result = $mysqli->query($delete_gps);
if(!$result) {
$result = array();
$result["code"] = ErrorCodes::DATABASE_ERROR;
$result["msg"] = $mysqli->error;
die(json_encode($result));
}
break;
default:
$result = array();
$result["code"] = ErrorCodes::ILLEGAL_MSG_ERROR;
$result["msg"] = "Mode unknown.";
die(json_encode($result));
}
$result = array();
$result["code"] = ErrorCodes::NO_ERROR;
$result["msg"] = "Success.";
echo json_encode($result);
?>