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_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']);
 
  15 function getSetting($sConfName, $sDefault = null)
 
  17     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
 
  19     if ($sDefault !== null && !$sValue) {
 
  26 function getSettingBool($sConfName)
 
  28     $sVal = strtolower(getSetting($sConfName));
 
  30     return strcmp($sVal, 'yes') == 0
 
  31            || strcmp($sVal, 'true') == 0
 
  32            || strcmp($sVal, '1') == 0;
 
  35 function getSettingConfig($sConfName, $sSystemConfig)
 
  37     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
 
  40         return CONST_ConfigDir.'/'.$sSystemConfig;
 
  46 function fail($sError, $sUserError = false)
 
  48     if (!$sUserError) $sUserError = $sError;
 
  49     error_log('ERROR: '.$sError);
 
  50     var_dump($sUserError)."\n";
 
  55 function getProcessorCount()
 
  57     $sCPU = file_get_contents('/proc/cpuinfo');
 
  58     preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
 
  59     return count($aMatches[0]);
 
  63 function getTotalMemoryMB()
 
  65     $sCPU = file_get_contents('/proc/meminfo');
 
  66     preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
 
  67     return (int)($aMatches[1]/1024);
 
  71 function getCacheMemoryMB()
 
  73     $sCPU = file_get_contents('/proc/meminfo');
 
  74     preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
 
  75     return (int)($aMatches[1]/1024);
 
  78 function getDatabaseDate(&$oDB)
 
  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);
 
  86     if ($sLastNodeXML === false) {
 
  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);
 
  92     return $aLastNodeDate[1];
 
  96 function byImportance($a, $b)
 
  98     if ($a['importance'] != $b['importance'])
 
  99         return ($a['importance'] > $b['importance']?-1:1);
 
 101     return $a['foundorder'] <=> $b['foundorder'];
 
 105 function javascript_renderData($xVal, $iOptions = 0)
 
 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);
 
 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;
 
 119     $jsonout = json_encode($xVal, $iOptions);
 
 122         header('Content-Type: application/javascript; charset=UTF-8');
 
 123         echo $_GET['json_callback'].'('.$jsonout.')';
 
 125         header('Content-Type: application/json; charset=UTF-8');
 
 130 function addQuotes($s)
 
 135 function parseLatLon($sQuery)
 
 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)) {
 
 143          * degrees decimal minutes
 
 144          * N 40 26.767, W 79 58.933
 
 145          * N 40°26.767′, W 79°58.933′
 
 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)) {
 
 152          * degrees decimal minutes
 
 153          * 40 26.767 N, 79 58.933 W
 
 154          * 40° 26.767′ N 79° 58.933′ W
 
 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)) {
 
 161          * degrees decimal seconds
 
 162          * N 40 26 46 W 79 58 56
 
 163          * N 40° 26′ 46″, W 79° 58′ 56″
 
 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)) {
 
 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
 
 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)) {
 
 181          * N 40.446° W 79.982°
 
 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)) {
 
 189          * 40.446° N 79.982° W
 
 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)) {
 
 202         $fQueryLat = $aData[2];
 
 203         $fQueryLon = $aData[3];
 
 208     return array($sFound, $fQueryLat, $fQueryLon);
 
 211 function closestHouseNumber($aRow)
 
 213     $fHouse = $aRow['startnumber']
 
 214                 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
 
 216     switch ($aRow['interpolationtype']) {
 
 218             $iHn = (int)($fHouse/2) * 2 + 1;
 
 221             $iHn = (int)(round($fHouse/2)) * 2;
 
 224             $iHn = (int)(round($fHouse));
 
 228     return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);