/
api.php
49 lines (40 loc) · 1.64 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
<?php
// Parameters (GET or POST):
// - apikey (required): API restriction key (must match specified in settings)
// - item_ids (required): list of item IDs or tag names separated by "|"
// - data_names: list of data names separated by "|" (may use LIKE-syntax)
// - periods: list of periods to return (day, week, month, year, total)
// - to: maximum date to return the data
// - back: how much columns to return
// Result is JSON-encoded.
// In case of any error its text is returned plainly.
define("NO_AUTH", 1);
require_once "overall.php";
$params = $_POST + $_GET;
try {
$apikey = @$params['apikey'];
$allowedKeys = preg_split('/\s+/s', trim(getSetting("apikeys", "")));
if (!$apikey || !in_array($apikey, $allowedKeys)) {
throw new Exception('Invalid "apikey" parameter');
}
$itemIds = trim(@$params['item_ids']);
if (!$itemIds) {
throw new Exception('Parameter "item_ids" must be specified and not empty');
}
$dataNames = trim(@$params['data_names']);
$dataNames = $dataNames? explode(TAGS_SEP, $dataNames) : array();
$onlyPeriods = trim(@$params['periods']);
$onlyPeriods = $onlyPeriods? explode(TAGS_SEP, $onlyPeriods) : array();
list ($to, $back) = parseToBackPeriod($params);
$result = array();
foreach (getPeriods() as $period => $periodName) {
if ($onlyPeriods && !in_array($period, $onlyPeriods)) continue;
$result[$period] = generateTableData($to, $back, $period, $itemIds, $dataNames);
}
// print_r($result);
header("Content-Type: application/json");
echo json_encode($result);
} catch (Exception $e) {
header("Content-Type: text/plain");
echo $e->getMessage();
}