-
Notifications
You must be signed in to change notification settings - Fork 23
/
api.php
86 lines (73 loc) · 1.9 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
<?php
include "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 = $_POST['item'];
$itemCount = $_POST['count'];
$jsonData = $_POST['jsonArray'];
$function = $_POST['function'];
$auth = $_POST['auth'];
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':
$dbConnector = "sqlite_connector.php";
$dbConfig = $SQLiteConfig;
break;
case 'MySQL':
$dbConnector = "mysql_connector.php";
$dbConfig = $MySQLConfig;
break;
default:
$dbConnector = "";
$dbConfig = "";
die (json_encode(array('type' => API_ERROR_NO_DATABASE, 'content' => 'no database type specified')));
}
include $dbConnector;
if (!hash_equals($authKey, crypt($auth, $authKey))){
die (json_encode(array('type' => API_ERROR_403, 'content' => 'Authentication failed.')));
}
$db = NEW DataBase($dbConfig);
switch ($function){
case 'listall':
echo $db->listall();
break;
case 'save':
if($db->exists($itemName)){
echo $db->update($itemName, $itemCount);
} else {
echo $db->save($itemName, $itemCount);
}
break;
case 'saveMultiple':
echo $db->saveMultiple($jsonData);
break;
case 'deleteMultiple':
echo $db->deleteMultiple($jsonData);
break;
case 'update':
echo $db->update($itemName, $itemCount);
break;
case 'delete':
echo $db->delete($itemName);
break;
case 'clear':
echo $db->clear();
break;
default:
die (json_encode(array('type' => API_ERROR_FUNCTION_NOT_SPECIFIED, 'content' => 'function not specified')));
}
?>