]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/lib.php
Merge pull request #2390 from lonvia/responsible-disclosure
[nominatim.git] / lib-php / lib.php
1 <?php
2
3 function loadSettings($sProjectDir)
4 {
5     @define('CONST_InstallDir', $sProjectDir);
6     // Temporary hack to set the direcory via environment instead of
7     // the installed scripts. Neither setting is part of the official
8     // set of settings.
9     defined('CONST_DataDir') or define('CONST_DataDir', $_SERVER['NOMINATIM_DATADIR']);
10     defined('CONST_SqlDir') or define('CONST_SqlDir', $_SERVER['NOMINATIM_SQLDIR']);
11     defined('CONST_ConfigDir') or define('CONST_ConfigDir', $_SERVER['NOMINATIM_CONFIGDIR']);
12     defined('CONST_Default_ModulePath') or define('CONST_Default_ModulePath', $_SERVER['NOMINATIM_DATABASE_MODULE_SRC_PATH']);
13 }
14
15 function getSetting($sConfName, $sDefault = null)
16 {
17     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
18
19     if ($sDefault !== null && !$sValue) {
20         return $sDefault;
21     }
22
23     return $sValue;
24 }
25
26 function getSettingBool($sConfName)
27 {
28     $sVal = strtolower(getSetting($sConfName));
29
30     return strcmp($sVal, 'yes') == 0
31            || strcmp($sVal, 'true') == 0
32            || strcmp($sVal, '1') == 0;
33 }
34
35 function getSettingConfig($sConfName, $sSystemConfig)
36 {
37     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
38
39     if (!$sValue) {
40         return CONST_ConfigDir.'/'.$sSystemConfig;
41     }
42
43     return $sValue;
44 }
45
46 function fail($sError, $sUserError = false)
47 {
48     if (!$sUserError) $sUserError = $sError;
49     error_log('ERROR: '.$sError);
50     var_dump($sUserError)."\n";
51     exit(-1);
52 }
53
54
55 function getProcessorCount()
56 {
57     $sCPU = file_get_contents('/proc/cpuinfo');
58     preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
59     return count($aMatches[0]);
60 }
61
62
63 function getTotalMemoryMB()
64 {
65     $sCPU = file_get_contents('/proc/meminfo');
66     preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
67     return (int)($aMatches[1]/1024);
68 }
69
70
71 function getCacheMemoryMB()
72 {
73     $sCPU = file_get_contents('/proc/meminfo');
74     preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
75     return (int)($aMatches[1]/1024);
76 }
77
78 function getDatabaseDate(&$oDB)
79 {
80     // Find the newest node in the DB
81     $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
82     // Lookup the timestamp that node was created
83     $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
84     $sLastNodeXML = file_get_contents($sLastNodeURL);
85
86     if ($sLastNodeXML === false) {
87         return false;
88     }
89
90     preg_match('#timestamp="(([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z)"#', $sLastNodeXML, $aLastNodeDate);
91
92     return $aLastNodeDate[1];
93 }
94
95
96 function byImportance($a, $b)
97 {
98     if ($a['importance'] != $b['importance'])
99         return ($a['importance'] > $b['importance']?-1:1);
100
101     return $a['foundorder'] <=> $b['foundorder'];
102 }
103
104
105 function javascript_renderData($xVal, $iOptions = 0)
106 {
107     $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
108     if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
109         // Unset, we call javascript_renderData again during exception handling
110         unset($_GET['json_callback']);
111         throw new Exception('Invalid json_callback value', 400);
112     }
113
114     $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
115     if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
116         $iOptions |= JSON_PRETTY_PRINT;
117     }
118
119     $jsonout = json_encode($xVal, $iOptions);
120
121     if ($sCallback) {
122         header('Content-Type: application/javascript; charset=UTF-8');
123         echo $_GET['json_callback'].'('.$jsonout.')';
124     } else {
125         header('Content-Type: application/json; charset=UTF-8');
126         echo $jsonout;
127     }
128 }
129
130 function addQuotes($s)
131 {
132     return "'".$s."'";
133 }
134
135 function parseLatLon($sQuery)
136 {
137     $sFound    = null;
138     $fQueryLat = null;
139     $fQueryLon = null;
140
141     if (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9.]*)[°\s]+([0-9.]+)?[′\']*[,\s]+([EW])[\s]+([0-9]+)[°\s]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
142         /*               1          2                    3                     4          5             6
143          * degrees decimal minutes
144          * N 40 26.767, W 79 58.933
145          * N 40°26.767′, W 79°58.933′
146          */
147         $sFound    = $aData[0];
148         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
149         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
150     } elseif (preg_match('/\\s*([0-9]+)[°\s]+([0-9]+[0-9.]*)?[′\']*[\s]+([NS])[,\s]+([0-9]+)[°\s]+([0-9]+[0-9.]*)?[′\'\s]+([EW])\\s*/', $sQuery, $aData)) {
151         /*                     1             2                          3           4             5                       6
152          * degrees decimal minutes
153          * 40 26.767 N, 79 58.933 W
154          * 40° 26.767′ N 79° 58.933′ W
155          */
156         $sFound    = $aData[0];
157         $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
158         $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
159     } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+)[″"]*[,\s]+([EW])[\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
160         /*                     1          2             3               4                  5          6             7               8
161          * degrees decimal seconds
162          * N 40 26 46 W 79 58 56
163          * N 40° 26′ 46″, W 79° 58′ 56″
164          */
165         $sFound    = $aData[0];
166         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
167         $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
168     } elseif (preg_match('/\\s*([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+[0-9.]*)[″"\s]+([NS])[,\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+[0-9.]*)[″"\s]+([EW])\\s*/', $sQuery, $aData)) {
169         /*                     1             2               3                     4           5             6               7                     8
170          * degrees decimal seconds
171          * 40 26 46 N 79 58 56 W
172          * 40° 26′ 46″ N, 79° 58′ 56″ W
173          * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
174          */
175         $sFound    = $aData[0];
176         $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
177         $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
178     } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
179         /*                     1          2                                3          4
180          * degrees decimal
181          * N 40.446° W 79.982°
182          */
183         $sFound    = $aData[0];
184         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
185         $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
186     } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
187         /*                     1                            2           3                            4
188          * degrees decimal
189          * 40.446° N 79.982° W
190          */
191         $sFound    = $aData[0];
192         $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
193         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
194     } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
195         /*                 1                   2                              3                        4
196          * degrees decimal
197          * 12.34, 56.78
198          * 12.34 56.78
199          * [12.456,-78.90]
200          */
201         $sFound    = $aData[0];
202         $fQueryLat = $aData[2];
203         $fQueryLon = $aData[3];
204     } else {
205         return false;
206     }
207
208     return array($sFound, $fQueryLat, $fQueryLon);
209 }
210
211 function closestHouseNumber($aRow)
212 {
213     $fHouse = $aRow['startnumber']
214                 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
215
216     switch ($aRow['interpolationtype']) {
217         case 'odd':
218             $iHn = (int)($fHouse/2) * 2 + 1;
219             break;
220         case 'even':
221             $iHn = (int)(round($fHouse/2)) * 2;
222             break;
223         default:
224             $iHn = (int)(round($fHouse));
225             break;
226     }
227
228     return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
229 }
230
231 if (!function_exists('array_key_last')) {
232     function array_key_last(array $array)
233     {
234         if (!empty($array)) return key(array_slice($array, -1, 1, true));
235     }
236 }