forked from GroundApps/ShoppingList_Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
152 lines (131 loc) · 4.57 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
<?php
require_once('CONSTANTS.php');
header("ShoLiBackendVersion: ".BACKEND_VERSION);
if(!function_exists('hash_equals')) {
function hash_equals($a, $b) {
$ret = strlen($a) ^ strlen($b);
$ret |= array_sum(unpack("C*", $a^$b));
return !$ret;
}
}
$itemName = array_key_exists('item', $_POST) ? $_POST['item'] : null;
$itemCount = array_key_exists('count', $_POST) ? $_POST['count'] : null;
$itemChecked = array_key_exists('checked', $_POST) ? $_POST['checked'] : null;
$jsonData = array_key_exists('jsonArray', $_POST) ? $_POST['jsonArray'] : null;
$function = array_key_exists('function', $_POST) ? $_POST['function'] : null;
$auth = array_key_exists('auth', $_POST) ? $_POST['auth'] : null;
include('config.php');
if($authKey == ''){
if ($_SERVER['HTTP_USER_AGENT'] != "ShoLiApp"){
header("Location: INSTALL.php");
exit();
} else {
die (json_encode(array('type' => API_ERROR_NOT_CONFIGURED, 'content' => 'Backend has not been configured yet!')));
}
}
switch($dataBase){
case 'SQLite':
$dbConfig = $SQLiteConfig;
break;
case 'MySQL':
$dbConfig = $MySQLConfig;
break;
default:
$dbConnector = "";
$dbConfig = "";
die (json_encode(array('type' => API_ERROR_NO_DATABASE, 'content' => 'no database type specified')));
}
include('db_connector.php');
$db = NEW DataBase($dataBase, $dbConfig);
$db->init(); //TODO: put this to INSTALL.php
session_start();
if (isset($_SESSION['user_logged']) && $_SESSION['user_logged'] == 0 && $_SESSION['user_read'] == 1) {
echo $db->listall();
exit();
} else if (! isset($_SESSION['user_logged']) || $_SESSION['user_logged'] != 1) {
if (!hash_equals($authKey, crypt($auth, $authKey))){
die (json_encode(array('type' => API_ERROR_403, 'content' => 'Authentication failed.')));
}
}
switch ($function){
case 'listall':
echo $db->listall();
break;
case 'save':
if($db->exists($itemName)){
echo $db->update($itemName, $itemCount, $itemChecked);
} else {
echo $db->save($itemName, $itemCount, $itemChecked);
}
break;
case 'saveMultiple':
echo $db->saveMultiple($jsonData);
break;
case 'deleteMultiple':
echo $db->deleteMultiple($jsonData);
break;
case 'update':
echo $db->update($itemName, $itemCount, $itemChecked);
break;
case 'delete':
echo $db->delete($itemName);
break;
case 'clear':
echo $db->clear();
break;
case 'addQRcodeItem':
/* OUTPAN
$outpanApiKey='1a74a95c40a331e50d4b2c7fe311328c'; // taken from https://github.com/johncipponeri/outpan-api-java
$response = file_get_contents('https://api.outpan.com/v2/products/' . $itemName . '/?apikey=' . $outpanApiKey);
if($response!==false) {
$name = json_decode($response)->{'name'};
if ( $name != '' ) {
$itemName = $name;
$itemCount = 1;
$itemChecked = 'false';
if( $db->exists($itemName) ) {
echo $db->update($itemName, $itemCount, $itemChecked);
} else {
echo $db->save($itemName, $itemCount, $itemChecked);
}
} else {
die (json_encode(array('type' => API_ERROR_QRCODE, 'content' => 'Code not found: ' . $itemName)));
}
} else {
die (json_encode(array('type' => API_ERROR_QRCODE, 'content' => 'Error querying outpan.com')));
}
//*/
$opengtindbApiKey='400000000'; // taken from https://opengtindb.org/api.php
$response = file_get_contents('https://opengtindb.org/?ean=' . $itemName . '&cmd=query&queryid=' . $opengtindbApiKey);
if($response!==false) {
if(strpos($response,'error=0')!==false) {
$values = parse_ini_string(utf8_encode($response));
//file_put_contents('/tmp/sholi',print_r($values, true));
$name = trim($values['name']);
if ($name == '') $name = trim($values['detailname']);
if ($name == '') $name = trim($values['name_en']);
if ($name == '') $name = trim($values['detailname_en']);
if ($name == '') $name = trim($values['descr']);
if ( $name != '' ) {
$itemName = $name;
$itemCount = 1;
$itemChecked = 'false';
if( $db->exists($itemName) ) {
echo $db->update($itemName, $itemCount, $itemChecked);
} else {
echo $db->save($itemName, $itemCount, $itemChecked);
}
} else {
die (json_encode(array('type' => API_ERROR_QRCODE, 'content' => 'Code not found: ' . $itemName)));
}
} else {
die (json_encode(array('type' => API_ERROR_QRCODE, 'content' => 'Code not found: ' . $itemName . ', '.$response)));
}
} else {
die (json_encode(array('type' => API_ERROR_QRCODE, 'content' => 'Error querying opengtindb.org')));
}
break;
default:
die (json_encode(array('type' => API_ERROR_FUNCTION_NOT_SPECIFIED, 'content' => 'function not specified')));
}
?>