2         @define('CONST_ConnectionBucket_PageType', 'Search');
 
   4         require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
 
   5         require_once(CONST_BasePath.'/lib/init-website.php');
 
   6         require_once(CONST_BasePath.'/lib/log.php');
 
   7         require_once(CONST_BasePath.'/lib/Geocode.php');
 
   8         require_once(CONST_BasePath.'/lib/output.php');
 
  10         ini_set('memory_limit', '200M');
 
  14         $oGeocode = new Geocode($oDB);
 
  16         $aLangPrefOrder = getPreferredLanguages();
 
  17         $oGeocode->setLanguagePreference($aLangPrefOrder);
 
  19         if (CONST_Search_ReversePlanForAll
 
  20                 || isset($aLangPrefOrder['name:de'])
 
  21                 || isset($aLangPrefOrder['name:ru'])
 
  22                 || isset($aLangPrefOrder['name:ja'])
 
  23                 || isset($aLangPrefOrder['name:pl']))
 
  25                 $oGeocode->setReverseInPlan(true);
 
  29         $sOutputFormat = getParamSet('format', array('html', 'xml', 'json', 'jsonv2'), 'html');
 
  31         // Show / use polygons
 
  32         if ($sOutputFormat == 'html')
 
  34                 $oGeocode->setIncludePolygonAsText(getParamBool('polygon'));
 
  38                 $bAsPoints = getParamBool('polygon');
 
  39                 $bAsGeoJSON = getParamBool('polygon_geojson');
 
  40                 $bAsKML = getParamBool('polygon_kml');
 
  41                 $bAsSVG = getParamBool('polygon_svg');
 
  42                 $bAsText = getParamBool('polygon_text');
 
  43                 if ( ( ($bAsGeoJSON?1:0)
 
  48                                  ) > CONST_PolygonOutput_MaximumTypes)
 
  50                         if (CONST_PolygonOutput_MaximumTypes)
 
  52                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
 
  56                                 userError("Polygon output is disabled");
 
  60                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
 
  61                 $oGeocode->setIncludePolygonAsText($bAsText);
 
  62                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
 
  63                 $oGeocode->setIncludePolygonAsKML($bAsKML);
 
  64                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
 
  67         // Polygon simplification threshold (optional)
 
  68         $oGeocode->setPolygonSimplificationThreshold(getParamFloat('polygon_threshold', 0.0));
 
  70         $oGeocode->loadParamArray($_GET);
 
  72         if (CONST_Search_BatchMode && isset($_GET['batch']))
 
  74                 $aBatch = json_decode($_GET['batch'], true);
 
  75                 $aBatchResults = array();
 
  76                 foreach($aBatch as $aBatchParams)
 
  78                         $oBatchGeocode = clone $oGeocode;
 
  79                         $oBatchGeocode->loadParamArray($aBatchParams);
 
  80                         $oBatchGeocode->setQueryFromParams($aBatchParams);
 
  81                         $aSearchResults = $oBatchGeocode->lookup();
 
  82                         $aBatchResults[] = $aSearchResults;
 
  84                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
 
  89                 if (!getParamString('q') && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
 
  91                         $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
 
  93                         // reverse order of '/' separated string
 
  94                         $aPhrases = explode('/', $sQuery);
 
  95                         $aPhrases = array_reverse($aPhrases);
 
  96                         $sQuery = join(', ',$aPhrases);
 
  97                         $oGeocode->setQuery($sQuery);
 
 101                         $oGeocode->setQueryFromParams($_GET);
 
 105         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
 
 107         $aSearchResults = $oGeocode->lookup();
 
 108         if ($aSearchResults === false) $aSearchResults = array();
 
 110         if ($sOutputFormat=='html')
 
 112                 $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
 
 114         logEnd($oDB, $hLog, sizeof($aSearchResults));
 
 116         $bAsText = $oGeocode->getIncludePolygonAsText();
 
 117         $sQuery = $oGeocode->getQueryString();
 
 118         $sViewBox = $oGeocode->getViewBoxString();
 
 119         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
 
 120         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
 
 122         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
 
 123         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
 
 124         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
 
 125         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
 
 126         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
 
 127         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
 
 128         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
 
 129         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
 
 130         $sMoreURL .= '&q='.urlencode($sQuery);
 
 132         if (CONST_Debug) exit;
 
 134         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');