forked from OpenRailwayMap/OpenRailwayMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
218 lines (170 loc) · 5.26 KB
/
functions.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
OpenRailwayMap Copyright (C) 2012 Alexander Matheisen
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under certain conditions.
See https://wiki.openstreetmap.org/wiki/OpenRailwayMap for details.
*/
require_once("config.php");
// sets a language file for gettext, either in the given or the most matching language
function includeLocale($lang)
{
global $langs;
if ((!$lang) || (!array_key_exists($lang, $langs)))
$lang = getUserLang();
setlocale(LC_ALL, $langs[$lang][0]);
bind_textdomain_codeset("messages", "UTF-8");
bindtextdomain("messages", "locales");
textdomain("messages");
}
// returns the IP address of the current user; returns x-forwarded-for behind proxies
function getUserIP()
{
return ($_SERVER['REMOTE_ADDR'] != "127.0.0.1") ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// return an array of the user's languages, sorted by importance
function getLangs()
{
$header = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang = explode(",", $header);
$i = 0;
foreach ($lang as $value)
{
$entry = explode(";", $value);
$langpair = explode("-", $entry[0]);
$langs[$i] = $langpair[0];
$i++;
}
return $langs;
}
// returns the most matching language of the user
function getUserLang()
{
global $langs;
// read out language from header as array
$langlist = getLangs();
// choose most matching language from available langs
foreach ($langlist as $value)
if (array_key_exists($value, $langs))
return $value;
// if no matching language could be found, choose english
return "en";
}
// checks if given offset-parameter is valid and takes standard value if missing
function offset($offset)
{
if (!$offset)
return 0;
if (strlen($offset) > 4)
return 0;
return $offset;
}
// parse given bbox-string and check if given bbox is valid
function getBbox($bbox)
{
// if no bbox is given
if (!$bbox)
return false;
// if bbox isn't too big
if (abs($right - $left) > 0.5 or abs($top - $bottom) > 0.2)
return false;
// parsing values from given string
$coordinates = explode(",", $bbox);
// switch values if they are in wrong order
if ($coordinates[0] > $right)
{
$temp = $coordinates[0];
$coordinates[0] = $coordinates[2];
$coordinates[2] = $temp;
}
if ($bottom > $top)
{
$temp = $coordinates[3];
$coordinates[3] = $coordinates[1];
$coordinates[1] = $temp;
}
return $coordinates;
}
// checks if given type-parameter is valid
function isValidType($type)
{
if (!$type || !isset($type) || !isset($_GET[$type]))
return false;
$type = $_GET[$type];
// check if given object type is invalid
if (($type != "node") && ($type != "way") && ($type != "relation"))
return false;
return true;
}
// checks if given osm id is valid
function isValidId($id)
{
if (!$id || !isset($id) || !isset($_GET[$id]))
return false;
$id = $_GET[$id];
if (!ctype_digit($id))
return false;
return true;
}
// checks if given coordinate is valid
function isValidCoordinate($coord)
{
if (!$coord || !isset($coord) || !isset($_GET[$coord]))
return false;
$coord = $_GET[$coord];
if (!is_numeric($coord))
return false;
return true;
}
// checks if given zoom level is valid
function isValidZoom($zoom)
{
if (!$zoom || !isset($zoom) || !isset($_GET[$zoom]))
return false;
$zoom = $_GET[$zoom];
if (!ctype_digit($zoom))
return false;
return true;
}
// checks if given timezone offset is valid
function isValidOffset($offset)
{
if (!$offset || !isset($offset) || !isset($_GET[$offset]))
return false;
$offset = $_GET[$offset];
if (!is_numeric($offset))
return false;
return true;
}
function urlArgsToParam($checkMobile, $urlbase)
{
echo "<script type=\"text/javascript\">\n";
echo "var params={\n";
echo "urlbase : '" . $urlbase . "',\n";
echo "id : ".(isValidId('id') ? ($_GET['id']) : ("null")).",\n";
echo "type : ".(isValidType('type') ? ("'".$_GET['type']."'") : ("null")).",\n";
echo "lat : ";
if (isValidCoordinate('lat'))
echo $_GET['lat'].",\n";
else
echo "null,\n";
echo "lon : ";
if (isValidCoordinate('lon'))
echo $_GET['lon'].",\n";
else
echo "null,\n";
echo "zoom : " . (isValidZoom('zoom') ? ($_GET['zoom']) : ("null")) . ",\n";
echo "lang : " . (isset($_GET['lang']) ? ("'".$_GET['lang']."'") : ("null")) . ",\n";
echo "offset : " . (isValidOffset('offset') ? ($_GET['offset']) : ("null")) . ",\n";
echo "searchquery : " . (isset($_GET['q']) ? (json_encode($_GET['q'])) : ("''")) . ",\n";
echo "ref : " . (isset($_GET['ref']) ? (json_encode($_GET['ref'])) : ("null")) . ",\n";
echo "name : " . (isset($_GET['name']) ? (json_encode($_GET['name'])) : ("null")) . ",\n";
echo "line : " . (isset($_GET['line']) ? (json_encode($_GET['line'])) : ("null")) . ",\n";
echo "operator : " . (isset($_GET['operator']) ? (json_encode($_GET['operator'])) : ("null")) . ",\n";
if ($checkMobile)
echo "mobile : " . (isset($_GET['mobile']) ? (($_GET['mobile'] != '0' && $_GET['mobile'] != 'false') ? "true" : "false") : ("null")) . ",\n";
echo "style : " . (isset($_GET['style']) ? (json_encode($_GET['style'])) : ("null")) . "\n";
echo "};\n";
echo "</script>\n";
}
?>