This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.php
113 lines (91 loc) · 3.54 KB
/
endpoint.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
<?php require 'common.php'; ?>
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
//$postdata = $_POST['request'];
if (isset($postdata)){
/*post in JSON format:
{
"email":"[email protected]",
"password":"aaaaaa",
"a_function":"blank",
"parameter":"whatever"
}
*/
$request = json_decode($postdata, true);
include_once('connecttag.php');
if ($request['a_function'] == "add_tag"){
if (connectTag($request['parameter'])){
die ('addtag - success');
} else {
die('addtag - failure');
}
}
//if registering
else if ($request['a_function'] == "register"){
include_once('back_register.php');
if(register(true, $request['email'], $request['password'])){
die ("reg_success");
} else {
die ("reg_fail");
}
} else { //validate credentials
include_once("back_login.php");
if ($request['a_function'] != "login"){
$user_info = login(true, true, $request['email'], $request['password']);
} else {
$user_info = login(true, false, $request['email'], $request['password']);
}
if (!$user_info){
die ("bad_credentials");
} else {
if ($request['a_function'] == "login"){ //get user information
die ("{\"password\": \"" . $user_info['password'] . "\",
\"name\": \"" . $user_info['name'] . "\"}");
} else if ($request['a_function'] == "get_default"){//get panel and tag data
include_once('fetchpaneldata.php');
include_once('fetchtagdata.php');
$panel_data = fetchPanelData(true, $user_info['id']);
$tag_data = fetchTagData(true, $user_info['id']);
die ("{\"panel\":" . json_encode($panel_data) . ", \"tag\":" . json_encode($tag_data) . "}");
} else if ($request['a_function'] == "get_panels"){//get panel data
include_once('fetchpaneldata.php');
$panel_data = fetchPanelData(true, $user_info['id']);
die (json_encode($panel_data));
} else if ($request['a_function'] == "get_tags_user"){//get tag data
include_once('fetchtagdata.php');
$tag_data = fetchTagData(true, $user_info['id']);
die (json_encode($tag_data));
} else if ($request['a_function'] == "reg_panel"){
include_once('back_connectpanel.php');
if(connectPanel(true, $request['email'], $request['parameter'])){
die ("success");
} else {
die ("fail");
}
} else if ($request['a_function'] == "edit_tag"){
include_once('back_edittag.php');
if(editTag(true, $user_info['id'], $request['parameter'])){
die ("success");
} else {
die ("fail");
}
} else if ($request['a_function'] == "get_tags_cp"){
//TODO send data to Arduino
}
}
}
}
?>