2 @define('CONST_ConnectionBucket_PageType', 'Search');
4 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5 require_once(CONST_BasePath.'/lib/log.php');
6 require_once(CONST_BasePath.'/lib/Geocode.php');
8 ini_set('memory_limit', '200M');
13 $fLat = CONST_Default_Lat;
14 $fLon = CONST_Default_Lon;
15 $iZoom = CONST_Default_Zoom;
17 $oGeocode =& new Geocode($oDB);
19 $aLangPrefOrder = getPreferredLanguages();
20 $oGeocode->setLanguagePreference($aLangPrefOrder);
22 if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
23 if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
24 if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
25 if (isset($aLangPrefOrder['name:pl'])) $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');
92 if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
94 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
96 // reverse order of '/' separated string
97 $aPhrases = explode('/', $sQuery);
98 $aPhrases = array_reverse($aPhrases);
99 $sQuery = join(', ',$aPhrases);
100 $oGeocode->setQuery($sQuery);
104 $oGeocode->setQueryFromParams($_GET);
109 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
111 $aSearchResults = $oGeocode->lookup();
112 if ($aSearchResults === false) $aSearchResults = array();
114 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
116 logEnd($oDB, $hLog, sizeof($aSearchResults));
118 $bAsText = $oGeocode->getIncludePolygonAsText();
119 $sQuery = $oGeocode->getQueryString();
120 $sViewBox = $oGeocode->getViewBoxString();
121 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
122 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
124 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
125 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
126 if ($bShowPolygons) $sMoreURL .= '&polygon=1';
127 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=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');