3 function loadSettings($sProjectDir)
 
   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
 
   9     defined('CONST_DataDir') or define('CONST_DataDir', $_SERVER['NOMINATIM_DATADIR']);
 
  10     defined('CONST_BinDir') or define('CONST_BinDir', $_SERVER['NOMINATIM_BINDIR']);
 
  11     defined('CONST_SqlDir') or define('CONST_SqlDir', $_SERVER['NOMINATIM_SQLDIR']);
 
  12     defined('CONST_ConfigDir') or define('CONST_ConfigDir', $_SERVER['NOMINATIM_CONFIGDIR']);
 
  13     defined('CONST_Default_ModulePath') or define('CONST_Default_ModulePath', $_SERVER['NOMINATIM_DATABASE_MODULE_SRC_PATH']);
 
  16 function getSetting($sConfName, $sDefault = null)
 
  18     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
 
  20     if ($sDefault !== null && !$sValue) {
 
  27 function getSettingBool($sConfName)
 
  29     $sVal = strtolower(getSetting($sConfName));
 
  31     return strcmp($sVal, 'yes') == 0
 
  32            || strcmp($sVal, 'true') == 0
 
  33            || strcmp($sVal, '1') == 0;
 
  36 function getSettingConfig($sConfName, $sSystemConfig)
 
  38     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
 
  41         return CONST_ConfigDir.'/'.$sSystemConfig;
 
  47 function fail($sError, $sUserError = false)
 
  49     if (!$sUserError) $sUserError = $sError;
 
  50     error_log('ERROR: '.$sError);
 
  51     var_dump($sUserError)."\n";
 
  56 function getProcessorCount()
 
  58     $sCPU = file_get_contents('/proc/cpuinfo');
 
  59     preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
 
  60     return count($aMatches[0]);
 
  64 function getTotalMemoryMB()
 
  66     $sCPU = file_get_contents('/proc/meminfo');
 
  67     preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
 
  68     return (int)($aMatches[1]/1024);
 
  72 function getCacheMemoryMB()
 
  74     $sCPU = file_get_contents('/proc/meminfo');
 
  75     preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
 
  76     return (int)($aMatches[1]/1024);
 
  79 function getDatabaseDate(&$oDB)
 
  81     // Find the newest node in the DB
 
  82     $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
 
  83     // Lookup the timestamp that node was created
 
  84     $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
 
  85     $sLastNodeXML = file_get_contents($sLastNodeURL);
 
  87     if ($sLastNodeXML === false) {
 
  91     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);
 
  93     return $aLastNodeDate[1];
 
  97 function byImportance($a, $b)
 
  99     if ($a['importance'] != $b['importance'])
 
 100         return ($a['importance'] > $b['importance']?-1:1);
 
 102     return $a['foundorder'] <=> $b['foundorder'];
 
 106 function javascript_renderData($xVal, $iOptions = 0)
 
 108     $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
 
 109     if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
 
 110         // Unset, we call javascript_renderData again during exception handling
 
 111         unset($_GET['json_callback']);
 
 112         throw new Exception('Invalid json_callback value', 400);
 
 115     $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
 
 116     if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
 
 117         $iOptions |= JSON_PRETTY_PRINT;
 
 120     $jsonout = json_encode($xVal, $iOptions);
 
 123         header('Content-Type: application/javascript; charset=UTF-8');
 
 124         echo $_GET['json_callback'].'('.$jsonout.')';
 
 126         header('Content-Type: application/json; charset=UTF-8');
 
 131 function addQuotes($s)
 
 136 function fwriteConstDef($rFile, $sConstName, $value)
 
 140     if (is_bool($value)) {
 
 141         $sEscapedValue = $value ? 'true' : 'false';
 
 142     } elseif (is_numeric($value)) {
 
 143         $sEscapedValue = strval($value);
 
 145         $sEscapedValue = 'false';
 
 147         $sEscapedValue = addQuotes(str_replace("'", "\\'", (string)$value));
 
 150     fwrite($rFile, "@define('CONST_$sConstName', $sEscapedValue);\n");
 
 154 function parseLatLon($sQuery)
 
 160     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)) {
 
 162          * degrees decimal minutes
 
 163          * N 40 26.767, W 79 58.933
 
 164          * N 40°26.767′, W 79°58.933′
 
 167         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
 
 168         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
 
 169     } 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)) {
 
 171          * degrees decimal minutes
 
 172          * 40 26.767 N, 79 58.933 W
 
 173          * 40° 26.767′ N 79° 58.933′ W
 
 176         $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
 
 177         $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
 
 178     } 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)) {
 
 180          * degrees decimal seconds
 
 181          * N 40 26 46 W 79 58 56
 
 182          * N 40° 26′ 46″, W 79° 58′ 56″
 
 185         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
 
 186         $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
 
 187     } 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)) {
 
 189          * degrees decimal seconds
 
 190          * 40 26 46 N 79 58 56 W
 
 191          * 40° 26′ 46″ N, 79° 58′ 56″ W
 
 192          * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
 
 195         $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
 
 196         $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
 
 197     } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
 
 200          * N 40.446° W 79.982°
 
 203         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
 
 204         $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
 
 205     } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
 
 208          * 40.446° N 79.982° W
 
 211         $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
 
 212         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
 
 213     } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
 
 221         $fQueryLat = $aData[2];
 
 222         $fQueryLon = $aData[3];
 
 227     return array($sFound, $fQueryLat, $fQueryLon);
 
 230 function createPointsAroundCenter($fLon, $fLat, $fRadius)
 
 232     $iSteps = max(8, min(100, ($fRadius * 40000)^2));
 
 233     $fStepSize = (2*pi())/$iSteps;
 
 234     $aPolyPoints = array();
 
 235     for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
 
 236         $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
 
 241 function closestHouseNumber($aRow)
 
 243     $fHouse = $aRow['startnumber']
 
 244                 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
 
 246     switch ($aRow['interpolationtype']) {
 
 248             $iHn = (int)($fHouse/2) * 2 + 1;
 
 251             $iHn = (int)(round($fHouse/2)) * 2;
 
 254             $iHn = (int)(round($fHouse));
 
 258     return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
 
 261 function getSearchRankLabel($iRank)
 
 263     if (!isset($iRank)) return 'unknown';
 
 264     if ($iRank < 2) return 'continent';
 
 265     if ($iRank < 4) return 'sea';
 
 266     if ($iRank < 8) return 'country';
 
 267     if ($iRank < 12) return 'state';
 
 268     if ($iRank < 16) return 'county';
 
 269     if ($iRank == 16) return 'city';
 
 270     if ($iRank == 17) return 'town / island';
 
 271     if ($iRank == 18) return 'village / hamlet';
 
 272     if ($iRank == 20) return 'suburb';
 
 273     if ($iRank == 21) return 'postcode area';
 
 274     if ($iRank == 22) return 'croft / farm / locality / islet';
 
 275     if ($iRank == 23) return 'postcode area';
 
 276     if ($iRank == 25) return 'postcode point';
 
 277     if ($iRank == 26) return 'street / major landmark';
 
 278     if ($iRank == 27) return 'minory street / path';
 
 279     if ($iRank == 28) return 'house / building';
 
 280     return 'other: ' . $iRank;