/
api.php
245 lines (202 loc) · 5.13 KB
/
api.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
require_once 'config.php';
require_once 'core/system.php';
error_reporting(E_ERROR);
$json = file_get_contents('php://input');
$obj = json_decode($json, 1);
$result = array();
// Error JSON
if (!$obj || empty($obj)) {
$result['status'] = 'Error';
$result['info'] = 'JSON Error';
die(json_encode($result));
}
// Access denied
if ($obj['token'] !== API_TOKEN) {
$result['status'] = 'Error';
$result['info'] = 'Forbidden';
die(json_encode($result));
}
/*
externalID
categoryTop
categoryProduct
vendor
name
url
pictureURL
price
currency
description
isActive
*/
$db = new Database(DB_NAME, DB_USER, DB_PASSWORD, DB_HOST);
if (!$db) {
$result['status'] = 'Error';
$result['info'] = '[1] DB Error';
die(json_encode($result));
}
if (isset($obj['description'])) {
$obj['description'] = addslashes($obj['description']);
}
if (isset($obj['name'])) {
$obj['name'] = addslashes($obj['name']);
}
if (strtolower($obj['action']) == 'insert') {
// Check category and insert if not exists
if (!empty($obj['categoryProduct'])) {
$sql = "INSERT IGNORE INTO categories (name) VALUES('{$obj['categoryProduct']}')";
if (!$db->query($sql)) {
$result['status'] = 'Error';
$result['info'] = '[3] DB Error';
die(json_encode($result));
}
}
if (!empty($obj['categoryTop'])) {
$sql = "INSERT IGNORE INTO categories (name) VALUES('{$obj['categoryTop']}')";
if (!$db->query($sql)) {
$result['status'] = 'Error';
$result['info'] = '[2] DB Error';
die(json_encode($result));
}
$sql = "SELECT ID FROM categories WHERE name='{$obj['categoryTop']}'";
$datarow = $db->query($sql)->result();
$catID = $datarow[0]->ID;
if ($catID > 0) {
$sql = "UPDATE categories SET parentID = '{$catID}' WHERE name='{$obj['categoryProduct']}'";
$db->query($sql);
}
}
// Insert Goods
// Get Category ID
$sql = "SELECT ID FROM categories WHERE name = '{$obj['categoryProduct']}'";
$datarow = $db->query($sql)->result();
$catID = $datarow[0]->ID;
if ($catID <= 0) {
$result['status'] = 'Error';
$result['info'] = $obj['categoryProduct'] . ' not Found!';
die(json_encode($result));
}
$sql = <<<H1
INSERT INTO goods (
externalID,
categoryID,
vendor,
name,
url,
pictureURL,
price,
currency,
description,
isActive
) VALUES (
'{$obj['externalID']}',
'{$catID}',
'{$obj['vendor']}',
'{$obj['name']}',
'{$obj['url']}',
'{$obj['pictureURL']}',
'{$obj['price']}',
'{$obj['currency']}',
'{$obj['description']}',
'{$obj['isActive']}'
)
ON DUPLICATE KEY UPDATE price = '{$obj['price']}', isActive = '{$obj['isActive']}';
H1;
if (!$db->query($sql)) {
$result['status'] = 'Error';
$result['info'] = '[5] DB Error';
die(json_encode($result));
}
} else if (strtolower($obj['action']) == 'delete') {
$eID = $obj['externalID'];
if (!empty($eID)) {
$sql = "DELETE FROM goods WHERE externalID = '{$eID}'";
$db->query($sql);
} else {
$result['status'] = 'Error';
$result['info'] = 'Error: externalID';
die(json_encode($result));
}
} else if (strtolower($obj['action']) == 'update') {
// Check category and insert if not exists
if (!empty($obj['categoryProduct'])) {
$sql = "INSERT IGNORE INTO categories (name) VALUES('{$obj['categoryProduct']}')";
if (!$db->query($sql)) {
$result['status'] = 'Error';
$result['info'] = '[3] DB Error';
die(json_encode($result));
}
}
if (!empty($obj['categoryTop'])) {
$sql = "INSERT IGNORE INTO categories (name) VALUES('{$obj['categoryTop']}')";
if (!$db->query($sql)) {
$result['status'] = 'Error';
$result['info'] = '[2] DB Error';
die(json_encode($result));
}
$sql = "SELECT ID FROM categories WHERE name='{$obj['categoryTop']}'";
$datarow = $db->query($sql)->result();
$catID = $datarow[0]->ID;
if ($catID > 0) {
$sql = "UPDATE categories SET parentID = '{$catID}' WHERE name='{$obj['categoryProduct']}'";
$db->query($sql);
}
}
// Insert Goods
// Get Category ID
$sql = "SELECT ID FROM categories WHERE name = '{$obj['categoryProduct']}'";
$datarow = $db->query($sql)->result();
$catID = $datarow[0]->ID;
if ($catID <= 0) {
$result['status'] = 'Error';
$result['info'] = $obj['categoryProduct'] . ' not Found!';
die(json_encode($result));
}
$sql = <<<H1
INSERT INTO goods (
externalID,
categoryID,
vendor,
name,
url,
pictureURL,
price,
currency,
description,
isActive
) VALUES (
'{$obj['externalID']}',
'{$catID}',
'{$obj['vendor']}',
'{$obj['name']}',
'{$obj['url']}',
'{$obj['pictureURL']}',
'{$obj['price']}',
'{$obj['currency']}',
'{$obj['description']}',
'{$obj['isActive']}'
)
ON DUPLICATE KEY UPDATE
price = '{$obj['price']}',
categoryID = '{$catID}',
vendor = '{$obj['vendor']}',
name = '{$obj['name']}',
url = '{$obj['url']}',
pictureURL = '{$obj['pictureURL']}',
price = '{$obj['price']}',
currency = '{$obj['currency']}',
description = '{$obj['description']}',
isActive = '{$obj['isActive']}';
H1;
if (!$db->query($sql)) {
$result['status'] = 'Error';
$result['info'] = 'Update Error';
die(json_encode($result));
}
}
// All OK
$result['status'] = 'Ok';
$result['info'] = 'Ok';
die(json_encode($result));
?>