-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·163 lines (141 loc) · 5.07 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
//import all the settings and all the commonly used functions
include_once('settings.php');
include_once('functions.php');
//Break up the requested URL into parameters
$parameters = array_filter(explode("/",urldecode($_SERVER['REQUEST_URI'])), 'strlen');
array_shift($parameters);
//Check that the endpoint that the client has requested is valid
$requested = "/" . implode("/", array_slice($parameters, 0, 2));
if(in_array($requested, $endpoints) == false){
echo $GLOBALS['endpoint_error'];
die();
}
//Execute the requested function
$parameters[0]($parameters);
//Function for products
function products($parameters){
if($parameters[1] == "list"){
$params = array(
"verb" => "SELECT",
"columns" => "*",
"table" => "products"
);
} else if($parameters[1] == "add"){
if(sizeof($parameters) < 4){
echo $GLOBALS['too_few_params'];
die();
}
$params = array(
"verb" => "INSERT",
"table" => "products",
"values" => array(
$parameters[2],
0,
$parameters[3],
),
);
} else if($parameters[1] == "delete"){
if(sizeof($parameters) != 3){
echo $GLOBALS['too_few_params'];
die();
}
$params = array(
"verb" => "DELETE",
"table" => "products",
"conditions" => array(
"id=" . $parameters[2],
),
"conjunctives" => array(
"",
),
);
} else if($parameters[1] == "update"){
$params = array(
"verb" => "UPDATE",
"table" => "products",
"values" => array(
$parameters[2] => $parameters[3],
),
"conditions" => array(
$parameters[4] . "=" . $parameters[5],
),
"conjunctives" => array(),
);
} else {
echo $GLOBALS['endpoint_error'];
}
display(query($params));
}
function orders($parameters){
if($parameters[1] == "place"){
if(sizeof($parameters) != 4){
echo $GLOBALS['too_few_params'];
die();
}
$params = array(
"verb" => "INSERT",
"table" => "orders",
"values" => array(
json_encode(explode(",", $parameters[3])),
$parameters[2],
0,
),
);
display(query($params));
} else if($parameters[1] == "complete"){
if(sizeof($parameters) != 3){
echo $GLOBALS['too_few_params'];
die();
}
$params = array(
"verb" => "UPDATE",
"table" => "orders",
"values" => array(
"completed" => 1,
),
"conditions" => array(
"id=" . $parameters[2],
),
"conjunctives" => array(),
);
display(query($params));
} else if($parameters[1] == "list"){
$params = array(
"verb" => "SELECT",
"table" => "orders",
"columns" => "*",
"conditions" => array(
"completed=0",
),
"conjunctives" => array(),
);
$orders = query($params);
$order_index = 0;
foreach($orders['results'] as $order){
$list = "(";
foreach(json_decode($order['product_list']) as $product){
$list.= $product . ',';
}
$list = substr($list, 0, -1) . ")";
$params = array(
"verb" => "SELECT",
"table" => "products",
"columns" => "product_name",
"conditions" => array(
"id IN " . $list,
),
"conjunctives" => array(),
);
$order['product_list'] = json_encode(query($params)['results']);
$orders['results'][$order_index] = $order;
$order_index++;
}
display($orders);
} else {
echo $GLOBALS['endpoint_error'];
}
}
function display($output){
echo json_encode($output);
}