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');
 
   9         ini_set('memory_limit', '200M');
 
  13         $oGeocode = new Geocode($oDB);
 
  15         $aLangPrefOrder = getPreferredLanguages();
 
  16         $oGeocode->setLanguagePreference($aLangPrefOrder);
 
  18         if (CONST_Search_ReversePlanForAll
 
  19                 || isset($aLangPrefOrder['name:de'])
 
  20                 || isset($aLangPrefOrder['name:ru'])
 
  21                 || isset($aLangPrefOrder['name:ja'])
 
  22                 || isset($aLangPrefOrder['name:pl']))
 
  24                 $oGeocode->setReverseInPlan(true);
 
  28         $sOutputFormat = 'html';
 
  29         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
 
  31                 $sOutputFormat = $_GET['format'];
 
  34         // Show / use polygons
 
  35         if ($sOutputFormat == 'html')
 
  37                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
 
  41                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
 
  42                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
 
  43                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
 
  44                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
 
  45                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
 
  46                 if ( ( ($bAsGeoJSON?1:0)
 
  51                                  ) > CONST_PolygonOutput_MaximumTypes)
 
  53                         if (CONST_PolygonOutput_MaximumTypes)
 
  55                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
 
  59                                 userError("Polygon output is disabled");
 
  63                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
 
  64                 $oGeocode->setIncludePolygonAsText($bAsText);
 
  65                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
 
  66                 $oGeocode->setIncludePolygonAsKML($bAsKML);
 
  67                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
 
  70         // Polygon simplification threshold (optional)
 
  72         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
 
  73         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
 
  75         $oGeocode->loadParamArray($_GET);
 
  77         if (CONST_Search_BatchMode && isset($_GET['batch']))
 
  79                 $aBatch = json_decode($_GET['batch'], true);
 
  80                 $aBatchResults = array();
 
  81                 foreach($aBatch as $aBatchParams)
 
  83                         $oBatchGeocode = clone $oGeocode;
 
  84                         $oBatchGeocode->loadParamArray($aBatchParams);
 
  85                         $oBatchGeocode->setQueryFromParams($aBatchParams);
 
  86                         $aSearchResults = $oBatchGeocode->lookup();
 
  87                         $aBatchResults[] = $aSearchResults;
 
  89                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
 
  94                 if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
 
  96                         $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
 
  98                         // reverse order of '/' separated string
 
  99                         $aPhrases = explode('/', $sQuery);
 
 100                         $aPhrases = array_reverse($aPhrases);
 
 101                         $sQuery = join(', ',$aPhrases);
 
 102                         $oGeocode->setQuery($sQuery);
 
 106                         $oGeocode->setQueryFromParams($_GET);
 
 110         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
 
 112         $aSearchResults = $oGeocode->lookup();
 
 113         if ($aSearchResults === false) $aSearchResults = array();
 
 115         if ($sOutputFormat=='html')
 
 117                 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
 
 119         logEnd($oDB, $hLog, sizeof($aSearchResults));
 
 121         $bAsText = $oGeocode->getIncludePolygonAsText();
 
 122         $sQuery = $oGeocode->getQueryString();
 
 123         $sViewBox = $oGeocode->getViewBoxString();
 
 124         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
 
 125         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
 
 127         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
 
 128         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
 
 129         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
 
 130         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
 
 131         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
 
 132         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
 
 133         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
 
 134         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
 
 135         $sMoreURL .= '&q='.urlencode($sQuery);
 
 137         if (CONST_Debug) exit;
 
 139         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');