This repository was archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.php
More file actions
80 lines (77 loc) · 3.62 KB
/
Copy pathrest.php
File metadata and controls
80 lines (77 loc) · 3.62 KB
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
<?php
header("Content-Type: application/json; charset=UTF-8");
#load config
$ini = parse_ini_file('./config/api.ini');
$db = $ini["db_file_location"];
#check if file exists
if (file_exists($db)) {
$pdo = new PDO('sqlite:' . $db);
if($pdo){
#only run if pdo connected
if(isset($_GET['countries'])){
#return all countries
$stmt = $pdo->prepare("SELECT * FROM countries ORDER BY name");
$stmt->execute();
$data = array();
while ($row = $stmt->fetch()) {
$data[$row["key"]] = $row["name"];
}
echo json_encode($data);
} elseif (isset($_GET['id'])){
#get country information and default information
$stmt = $pdo->prepare("SELECT format.key, format.value, format.id FROM format
INNER JOIN countries ON countries.id=format.country_id
WHERE countries.key = :isoid AND format.parent_id IS NULL
UNION
SELECT key, value, 0 FROM defaults
WHERE NOT EXISTS(
SELECT format.key FROM format
INNER JOIN countries
ON countries.id=format.country_id
WHERE countries.key = :isoid AND format.parent_id IS NULL AND defaults.key = format.key
);");
$stmt->execute(array("isoid" => $_GET['id']));
$data = array();
#get all info about the subregion
while ($row = $stmt->fetch()) {
if($row["key"] != "sub_keys"){
$data[$row["key"]] = $row["value"];
} else {
$states = array();
$stmt2 = $pdo->prepare("SELECT format.key, format.value, format.id FROM format WHERE parent_id=:parent_id");
$stmt2->execute(array("parent_id" => $row["id"]));
while ($sub = $stmt2->fetch()) {
$states[$sub["key"]] = array();
$stmt3 = $pdo->prepare("SELECT format.key, format.value FROM format WHERE parent_id=:parent_id");
$stmt3->execute(array("parent_id" => $sub["id"]));
while ($state = $stmt3->fetch()) {
$states[$sub["key"]][$state["key"]] = $state["value"];
}
}
$data["administrative_areas"] = $states;
}
}
echo json_encode($data);
} else if (isset($_GET['template'])) {
#returns a list of all available templates or the specified template
if(empty($_GET['template'])){
#return list of templates
$templates = glob($ini["template_folder"]."*.tmpl");
for($i = 0; $i < count($templates); $i++){
$templates[$i] = basename($templates[$i], ".tmpl");
}
echo json_encode($templates);
} else {
#return template
echo file_get_contents($ini["template_folder"].pathinfo($_GET['template'])['filename'].".tmpl");
}
} else {
echo json_encode(array("error"=>"unsupported get parameter"));
}
} else {
echo json_encode(array("error"=>"no pdo connection"));
}
} else {
echo json_encode(array("error"=>"file not found"));
}
?>