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->setIncludePolygonAsText((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($bAsPoints);
67 $oGeocode->setIncludePolygonAsText($bAsText);
68 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
69 $oGeocode->setIncludePolygonAsKML($bAsKML);
70 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
73 // List of excluded Place IDs - used for more acurate pageing
74 if (isset($_GET['exclude_place_ids']) && $_GET['exclude_place_ids'])
76 foreach(explode(',',$_GET['exclude_place_ids']) as $iExcludedPlaceID)
78 $iExcludedPlaceID = (int)$iExcludedPlaceID;
79 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
81 $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
84 // Only certain ranks of feature
85 if (isset($_GET['featureType'])) $oGeocode->setFeatureType($_GET['featureType']);
86 if (isset($_GET['featuretype'])) $oGeocode->setFeatureType($_GET['featuretype']);
89 if (isset($_GET['countrycodes']))
91 $aCountryCodes = array();
92 foreach(explode(',',$_GET['countrycodes']) as $sCountryCode)
94 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
96 $aCountryCodes[] = strtolower($sCountryCode);
99 $oGeocode->setCountryCodeList($aCountryCodes);
102 if (isset($_GET['viewboxlbrt']) && $_GET['viewboxlbrt'])
104 $aCoOrdinatesLBRT = explode(',',$_GET['viewboxlbrt']);
105 $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
108 if (isset($_GET['viewbox']) && $_GET['viewbox'])
110 $aCoOrdinatesLTRB = explode(',',$_GET['viewbox']);
111 $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
114 if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
116 $aPoints = explode(',',$_GET['route']);
117 if (sizeof($aPoints) % 2 != 0)
119 userError("Uneven number of points");
124 foreach($aPoints as $i => $fPoint)
128 $aRoute[] = array((float)$fPoint, $fPrevCoord);
132 $fPrevCoord = (float)$fPoint;
135 $oGeocode->setRoute($aRoute);
139 $sQuery = (isset($_GET['q'])?trim($_GET['q']):'');
140 if (!$sQuery && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
142 $sQuery = substr($_SERVER['PATH_INFO'], 1);
144 // reverse order of '/' separated string
145 $aPhrases = explode('/', $sQuery);
146 $aPhrases = array_reverse($aPhrases);
147 $sQuery = join(', ',$aPhrases);
151 $oGeocode->setStructuredQuery(@$_GET['amenity'], @$_GET['street'], @$_GET['city'], @$_GET['county'], @$_GET['state'], @$_GET['country'], @$_GET['postalcode']);
155 $oGeocode->setQuery($sQuery);
158 $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
160 if (isset($_GET['batch']))
162 $aBatch = json_decode($_GET['batch'], true);
163 foreach($aBatch as $aItem) {
169 $aSearchResults = $oGeocode->lookup();
170 if ($aSearchResults === false) $aSearchResults = array();
172 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
174 logEnd($oDB, $hLog, sizeof($aSearchResults));
176 $bAsText = $oGeocode->getIncludePolygonAsText();
177 $sQuery = $oGeocode->getQueryString();
179 $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
180 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
181 if (isset($_GET['polygon']) && isset($_GET['polygon'])) $sMoreURL .= '&polygon=1';
182 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
183 if (isset($_GET['viewbox']) && $_GET['viewbox']) $sMoreURL .= '&viewbox='.urlencode($_GET['viewbox']);
184 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
185 $sMoreURL .= '&q='.urlencode($sQuery);
187 if (CONST_Debug) exit;
189 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');