-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
149 lines (120 loc) · 5.1 KB
/
index.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
<?php
header("Access-Control-Allow-Origin: *");
require_once "class/EG_Julien/Server.php";
$DB = new PDO('mysql:dbname=iserver;host=localhost', "root", "root");
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$Server = new Server("root", "Com", "115200");
$Server->getServer('ttyACM0', 115200);
/**
* @init var
* Methods / Protocols / API Version
**/
$Methods = ['post', 'get'];
$Protocols = ['json', 'data', 'start', 'auth'];
$Datas = ['all', 'degrees', 'light', 'door', 'tension', 'a_status', 'b_status', 'blue', 'red', 'green', 'white'];
$version = '1.2.4';
/**
* @init URL params :
*
* Methods get => /get/json/data
*
* Methods post => /post/data/token/data
*
* Errors verification :
**/
if (!isset($_GET['p'])) {
$page = 'null';
} else {
$page = $_GET['p'];
}
if ($page != 'null') {
$request = explode("/", $page);
$request_lenght = count($request) - 1;
$method = $request[1];
$protocol = $request[2];
if (in_array($method, $Methods) == FALSE) {
$errors = ['methods' => 'Unregonized method !'];
} elseif (in_array($protocol, $Protocols) == FALSE) {
$errors = ['protocols' => 'Unregonized protocol !'];
} elseif ($method == 'get' && ($request_lenght > 3 || $request_lenght < 3)) {
$errors = ['request_lenght' => 'Not enought or too few arguments in your request !'];
} elseif ($method == 'post' && ($request_lenght > 4 || $request_lenght < 4)) {
$errors = ['request_lenght' => 'Not enought or too few arguments in your request !'];
}
if ($method == 'post' && $request_lenght == 4 && $protocol == 'data' && strlen($request[3]) == 40 && isset($request[4])) {
$user = $request[3];
$data = $request[4];
$q = $DB->prepare("SELECT login FROM users WHERE password = ?");
$q->execute([$user]);
$result = $q->fetch();
if (isset($result) && $result != FALSE) {
$json = [
'api' => 'iServer API',
'version' => $version,
'timestamp' => time(),
'status' => 'success',
'user' => $result,
'response' => ['data_posted' => $data]
];
$data = str_replace("$", "/", $data);
$Server->setSerial($data);
echo json_encode($json);
} else {
$errors = ['user' => 'User not found in the DB'];
}
} elseif ($method == 'get' && $request_lenght == 3 && $protocol == 'json' && isset($request[3])) {
$what = $request[3];
if (in_array($what, $Datas) == TRUE) {
$data = $Server->getSerial();
$data = explode("\n", $data);
for ($i=0; $i < count($Datas); $i++) {
if ($what == $Datas[$i]) {
if ($i == 0) {
$return = [
$Datas[1] => $data[0],
$Datas[2] => $data[1],
$Datas[3] => $data[2],
$Datas[4] => $data[3],
$Datas[5] => $data[4],
$Datas[6] => $data[5],
$Datas[7] => $data[6],
$Datas[8] => $data[7],
$Datas[9] => $data[8],
$Datas[10] => $data[9]
];
} else {
$return = [$what => $data[$i - 1]];
}
$json = [
'api' => 'iServer API',
'version' => $version,
'timestamp' => time(),
'status' => 'success',
'response' => $return
];
}
}
echo json_encode($json);
} else {
$errors = ['data_get' => 'An error has been detected in your API syntax !'];
}
} elseif ($method == 'get' && $protocol == 'auth' && $request_lenght == 3 && isset($request[3])) {
$q = $DB->prepare('SELECT * FROM users WHERE password = ?');
$q->execute([$request[3]]);
$auth = $q->fetch();
if (!empty($auth)) {
echo json_encode($auth);
} else {
$errors = ['get_auth' => 'Invalid token given in parameter !'];
}
} else {
$errors = ['post_request' => 'An error has been detected in your API syntax !'];
}
if (isset($errors)) {
echo json_encode($errors);
}
} else {
$errors = ['request_error' => 'You need ask something to have a response !'];
echo json_encode($errors);
}