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;
16 $sSuggestionURL = false;
18 $oGeocode =& new Geocode($oDB);
20 $aLangPrefOrder = getPreferredLanguages();
21 $oGeocode->setLanguagePreference($aLangPrefOrder);
23 if (isset($_GET['addressdetails'])) $oGeocode->setIncludeAddressDetails((bool)$_GET['addressdetails']);
24 if (isset($_GET['bounded'])) $oGeocode->setBounded((bool)$_GET['bounded']);
25 if (isset($_GET['dedupe'])) $oGeocode->setDedupe((bool)$_GET['dedupe']);
27 if (isset($_GET['limit'])) $oGeocode->setLimit((int)$_GET['limit']);
28 if (isset($_GET['offset'])) $oGeocode->setOffset((int)$_GET['offset']);
31 $sOutputFormat = 'html';
32 if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
34 $sOutputFormat = $_GET['format'];
37 // Show / use polygons
38 if ($sOutputFormat == 'html')
40 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsPoints((bool)$_GET['polygon']);
44 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
45 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
46 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
47 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
48 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
49 if ( ( ($bAsGeoJSON?1:0)
54 ) > CONST_PolygonOutput_MaximumTypes)
56 if (CONST_PolygonOutput_MaximumTypes)
58 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
62 userError("Polygon output is disabled");
66 $oGeocode->setIncludePolygonAsText($bAsText);
67 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
68 $oGeocode->setIncludePolygonAsKML($bAsKML);
69 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
72 // List of excluded Place IDs - used for more acurate pageing
73 if (isset($_GET['exclude_place_ids']) && $_GET['exclude_place_ids'])
75 foreach(explode(',',$_GET['exclude_place_ids']) as $iExcludedPlaceID)
77 $iExcludedPlaceID = (int)$iExcludedPlaceID;
78 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
80 $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
83 // Only certain ranks of feature
84 if (isset($_GET['featureType'])) $oGeocode->setFeatureType($_GET['featureType']);
85 if (isset($_GET['featuretype'])) $oGeocode->setFeatureType($_GET['featuretype']);
88 if (isset($_GET['countrycodes']))
90 $aCountryCodes = array();
91 foreach(explode(',',$_GET['countrycodes']) as $sCountryCode)
93 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
95 $aCountryCodes[] = strtolower($sCountryCode);
98 $oGeocode->setCountryCodeList($aCountryCodes);
101 if (isset($_GET['viewboxlbrt']) && $_GET['viewboxlbrt'])
103 $aCoOrdinatesLBRT = explode(',',$_GET['viewboxlbrt']);
104 $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
107 if (isset($_GET['viewbox']) && $_GET['viewbox'])
109 $aCoOrdinatesLTRB = explode(',',$_GET['viewbox']);
110 $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
113 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
115 $aPoints = explode(',',$_GET['route']);
116 if (sizeof($aPoints) % 2 != 0)
118 userError("Uneven number of points");
123 foreach($aPoints as $i => $fPoint)
127 $aRoute[] = array((float)$fPoint, $fPrevCoord);
131 $fPrevCoord = (float)$fPoint;
134 $oGeocode->setRoute($aRoute);
138 $sQuery = (isset($_GET['q'])?trim($_GET['q']):'');
139 if (!$sQuery && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
141 $sQuery = substr($_SERVER['PATH_INFO'], 1);
143 // reverse order of '/' separated string
144 $aPhrases = explode('/', $sQuery);
145 $aPhrases = array_reverse($aPhrases);
146 $sQuery = join(', ',$aPhrases);
150 $oGeocode->setStructuredQuery(@$_GET['amenity'], @$_GET['street'], @$_GET['city'], @$_GET['county'], @$_GET['state'], @$_GET['country'], @$_GET['postalcode']);
154 $oGeocode->setQuery($sQuery);
157 $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
159 if (isset($_GET['batch']))
161 $aBatch = json_decode($_GET['batch'], true);
162 foreach($aBatch as $aItem) {
168 $aSearchResults = $oGeocode->lookup();
169 if ($aSearchResults === false) $aSearchResults = array();
171 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
173 logEnd($oDB, $hLog, sizeof($aSearchResults));
175 $bAsText = $oGeocode->getIncludePolygonAsText();
176 $sQuery = $oGeocode->getQueryString();
178 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
179 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
180 if ($oGeocode->getIncludePolygonAsPoints()) $sMoreURL .= '&polygon=1';
181 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
182 if (isset($_GET['viewbox']) && $_GET['viewbox']) $sMoreURL .= '&viewbox='.urlencode($_GET['viewbox']);
183 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
184 $sMoreURL .= '&q='.urlencode($sQuery);
186 if (CONST_Debug) exit;
188 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');