-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
182 lines (171 loc) · 3.89 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
<?php
header('Access-Control-Allow-Origin: *');
require_once "config/config.php"; // config db, etc
require_once "lib/lib.php"; // config db, etc
require_once "lib/error.php"; // error
require_once "lib/structure.php"; // structure layout
require_once "lib/get_town.php"; // get town
require_once "lib/town_exists.php"; // get town
require_once "lib/display_categories.php"; // display_categories
require_once "lib/data.php"; // data
// mysql
$db = new mysqli($config['dbhost'], $config['dbuser'], $config['dbpassword'], $config['dbname']);
if ($db->connect_error)
{
// log error
if($config['log'] > 0)
{
append_file("log/api.txt","\n".date(DATE_RFC822)." \t error \t db connect_error \t main()");
}
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error();
}
else
{
$format = "";
$action = "";
$type = "";
$town = "";
$categories = array();
// format
if(isset($_GET['format']))
{
if($_GET['format'] == "json")
{
$format = "json";
}
else
{
// illegal format
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_format($_GET['format']);
return;
}
}
else
{
// no format -> json
$format = "json";
}
// action
if(isset($_GET['action']))
{
if($_GET['action'] == "query")
{
$action = "query";
}
else
{
// illegal action
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_undefined_action($_GET['action']);
return;
}
}
else
{
// no action
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_no_action();
return;
}
// type
if(isset($_GET['type']))
{
if($_GET['type'] == "structure")
{
$type = "structure";
echo return_structure_categories($db);
}
else if($_GET['type'] == "data")
{
$type = "data";
if((isset($_GET['wiki']))&&($_GET['wiki']==1))
{
$wiki = true;
}
else
{
$wiki = false;
}
if(isset($_GET['categories']))
{
$categories = explode("|",$_GET['categories']);
$categories = get_display_categories($db, $categories);
}
if(isset($_GET['town']))
{
$town = $db->real_escape_string($_GET['town']);
if(town_exists($db, $town))
{
echo return_town_info($db,$town,$wiki,$categories);
}
else
{
// town dosn't exist
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_town($_GET['town']);
return;
}
}
else if((isset($_GET['longitude'])) && (isset($_GET['latitude'])))
{
$town = get_town($db, $_GET['longitude'],$_GET['latitude']);
echo return_town_info($db,$town,$wiki,$categories);
}
else if(isset($_GET['wikidata']))
{
if($_GET['wikidata'] == "Q19842286"){$_GET['wikidata'] = "Q659891";} // fix mapbox bug
$town = get_town_wikidata($db, $_GET['wikidata']);
if(town_exists($db, $town))
{
echo return_town_info($db,$town,$wiki,$categories);
}
else
{
// town dosn't exist
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_wikidata($_GET['wikidata']);
return;
}
}
else if(isset($_GET['gemeindekennzahl']))
{
$town = get_town_kennzahl($db, $_GET['gemeindekennzahl']);
if(town_exists($db, $town))
{
echo return_town_info($db,$town,$wiki,$categories);
}
else
{
// town dosn't exist
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_gemeindekennzahl($_GET['gemeindekennzahl']);
return;
}
}
else
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_town($_GET['town']);
return;
}
}
else
{
// illegal type
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_type($_GET['type']);
return;
}
}
else
{
// no type
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo return_error_unknown_type("");
return;
}
$db->close();
} // $db
?>