From: <chr...@us...> - 2018-04-18 22:23:08
|
Revision: 13345 http://sourceforge.net/p/xoops/svn/13345 Author: chronolabscoop Date: 2018-04-18 22:21:32 +0000 (Wed, 18 Apr 2018) Log Message: ----------- GeoSpatial + Locational Places Services API 3.0.11! Modified Paths: -------------- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/common.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/functions.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/version.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/index.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/include/config.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/include/page.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/language/english/install.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/page_configsave.php Added Paths: ----------- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/verify.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/include/constants.dist.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/page_extrasettings.php Removed Paths: ------------- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/common.inc.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/config.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/createconfigform.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/dbconfig.dist.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/install_tpl.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/license.dist.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/mainfile.dist.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/makedata.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/page.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/verify.php Modified: *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md =================================================================== --- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md 2018-02-24 13:38:12 UTC (rev 13344) +++ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md 2018-04-18 22:21:32 UTC (rev 13345) @@ -1,10 +1,10 @@ ## Chronolabs Cooperative presents -# GeoSpatial + Locational Places Services API 3.0.9 +# GeoSpatial + Locational Places Services API 3.0.10 -### Author: Simon Antony Roberts <simon@snails.email> +### Author: Simon Antony Roberts <wis...@us...> -#### Demo: http://places.snails.email or http://places.xortify.com +#### Demo: http://places.snails.email This API is a Geospatial service provided by PHP7.0 - it utilises file caches as well as a mysqli database interface to provide and provision both locality keys as well as country and continent keys. Modified: *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php =================================================================== --- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php 2018-02-24 13:38:12 UTC (rev 13344) +++ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php 2018-04-18 22:21:32 UTC (rev 13345) @@ -59,7 +59,6 @@ /** * Keys & Identify Strings */ -define('API_GOOGLE_KEY','AIzaSyD0tstaeCtH0gugLJUeYcUSUT_TdXL24Uo'); // Get from: https://developers.google.com/places/web-service/get-api-key = API_GOOGLE_KEY define('API_PLACES_FIELDS', 'Address|Distance|RegionName|mapref_latitude|mapref_longitude|Latitude_Float|Longitude_Float|Altitude_Feet|Altitude_Meters|key'); define('API_COUNTRY_FIELDS', 'Country|ISO2|FIPS104|ISO3|ISON|TLD|Capital|Continent|NationalitySingular|NationalityPlural|Currency|CurrencyCode|Population|Records|Updates|API|Details|key|distance'); define('API_CONTINENT_FIELDS', 'Continent|key|Population|Records|API|Details|Updates'); @@ -69,7 +68,6 @@ /** * Include libraries */ -require_once __DIR__ . DIRECTORY_SEPARATOR . 'functions.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'file' . DIRECTORY_SEPARATOR . 'apifile.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'apicache.php'; Deleted: *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php =================================================================== --- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php 2018-02-24 13:38:12 UTC (rev 13344) +++ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php 2018-04-18 22:21:32 UTC (rev 13345) @@ -1,2113 +0,0 @@ -<?php -/** - * Chronolabs REST Geospatial API Services API - * - * You may not change or alter any portion of this comment or credits - * of supporting developers from this source code or any supporting source code - * which is considered copyrighted (c) material of the original comment or credit authors. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * @copyright Chronolabs Cooperative http://snails.email - * @license GNU GPL 3 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) - * @package api - * @since 2.0.1 - * @author Simon Roberts <wis...@us...> - * @subpackage places - * @description Geospatial API Services API - * @see http://internetfounder.wordpress.com - * @see http://sourceoforge.net/projects/chronolabsapis - * @see https://github.com/Chronolabs-Cooperative/API-API-PHP - */ - - -if (!function_exists("whitelistGetIP")) { - - /* function whitelistGetIPAddy() - * - * provides an associative array of whitelisted IP Addresses - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @return array - */ - function whitelistGetIPAddy() { - return array_merge(whitelistGetNetBIOSIP(), file(dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'whitelist.txt')); - } -} - -if (!function_exists("whitelistGetNetBIOSIP")) { - - /* function whitelistGetNetBIOSIP() - * - * provides an associative array of whitelisted IP Addresses base on TLD and NetBIOS Addresses - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @return array - */ - function whitelistGetNetBIOSIP() { - $ret = array(); - foreach(file(dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'whitelist-domains.txt') as $domain) { - $ip = gethostbyname($domain); - $ret[$ip] = $ip; - } - return $ret; - } -} - -if (!function_exists("whitelistGetIP")) { - - /* function whitelistGetIP() - * - * get the True IPv4/IPv6 address of the client using the API - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @param $asString boolean Whether to return an address or network long integer - * - * @return mixed - */ - function whitelistGetIP($asString = true){ - // Gets the proxy ip sent by the user - $proxy_ip = ''; - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { - $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; - } else - if (!empty($_SERVER['HTTP_X_FORWARDED'])) { - $proxy_ip = $_SERVER['HTTP_X_FORWARDED']; - } else - if (! empty($_SERVER['HTTP_FORWARDED_FOR'])) { - $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR']; - } else - if (!empty($_SERVER['HTTP_FORWARDED'])) { - $proxy_ip = $_SERVER['HTTP_FORWARDED']; - } else - if (!empty($_SERVER['HTTP_VIA'])) { - $proxy_ip = $_SERVER['HTTP_VIA']; - } else - if (!empty($_SERVER['HTTP_X_COMING_FROM'])) { - $proxy_ip = $_SERVER['HTTP_X_COMING_FROM']; - } else - if (!empty($_SERVER['HTTP_COMING_FROM'])) { - $proxy_ip = $_SERVER['HTTP_COMING_FROM']; - } - if (!empty($proxy_ip) && $is_ip = preg_match('/^([0-9]{1,3}.){3,3}[0-9]{1,3}/', $proxy_ip, $regs) && count($regs) > 0) { - $the_IP = $regs[0]; - } else { - $the_IP = $_SERVER['REMOTE_ADDR']; - } - - $the_IP = ($asString) ? $the_IP : ip2long($the_IP); - return $the_IP; - } -} - -if (!function_exists("randomAPIKey")) { - - /* function randomAPIKey() - * - * Get a random forensic identify key for a place from the database - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @return string(32) - */ - function randomAPIKey() - { - $sql = "SELECT * FROM `".$GLOBALS['APIDB']->prefix("countries") . "` WHERE `Records` > 1000 ORDER BY RAND() LIMIT 1 "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - $country = $GLOBALS['APIDB']->fetchArray($result); - $sql = "SELECT concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key` FROM `" . $GLOBALS['APIDB']->prefix($country['Table']) . "` ORDER BY RAND() LIMIT 1 "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - if (list($key) = $GLOBALS['APIDB']->fetchRow($result)) - return $key; - } - return array(); - } - } -} - -if (!function_exists("randomGeoPlace")) { - - /* function randomAPIKey() - * - * Get a random forensic identify key for a place from the database - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @return string(32) - */ - function randomGeoPlace() - { - $sql = "SELECT * FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `Records` > 1000 ORDER BY RAND() LIMIT 1 "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - $country = $GLOBALS['APIDB']->fetchArray($result); - $sql = "SELECT * FROM `" . $GLOBALS['APIDB']->prefix($country['Table']) . "` ORDER BY RAND() LIMIT 1 "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - if ($place = $GLOBALS['APIDB']->fetchArray($result)) - return array('country' => $country['Country'], 'iso3' => $country['ISO3'], 'iso2' => $country['ISO2'], 'region' => $place['RegionName']); - } - } - return false; - } -} -if (!function_exists("randomGeoLocation")) { - - /* function randomGeoLocation() - * - * Get a random latitude & longitude for a place from the database - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @return array - */ - function randomGeoLocation() - { - $sql = "SELECT * FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `Records` > 1000 ORDER BY RAND() LIMIT 1 "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - $country = $GLOBALS['APIDB']->fetchArray($result); - $sql = "SELECT * FROM `" . $GLOBALS['APIDB']->prefix($country['Table']) . "` ORDER BY RAND() LIMIT 1 "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - if ($place = $GLOBALS['APIDB']->fetchArray($result)) - return array('latitude'=>$place['Latitude_Float'], 'longitude' => $place['Longitude_Float']); - } - return array('latitude'=> 0, 'longitude' => 0); - } - } -} - -if (!function_exists("findPlace")) { - - /* function findPlace() - * - * Function that finds a location(s) based in the information passed to the URL - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @param $country string Country Name, ISO2 or ISO3 or keyword 'Random' for a country to search - * @param $place string Name of place or component of name of places to search for or keyword 'Random' - * @param $format string API Output mode (JSON, XML, SERIAL, HTML, RAW) - * @param $return integer Number of Result to Limit the selection and return of the API - * - * @return array - */ - function findPlace($country = '', $place = '', $format = 'json', $radius = 1) - { - if (empty($radius) || $radius < 1) - $radius = API_RADIUS_DEFAULT; - - session_start(); - if (!in_array(whitelistGetIP(true), whitelistGetIPAddy())) { - if (isset($_SESSION['places']['queries']['time'])) { - if ($_SESSION['places']['queries']['time']>time()) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - } elseif (!isset($_SESSION['places']['queries']['time'])) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - if ($_SESSION['places']['queries']['number']>MAXIMUM_QUERIES) { - header("HTTP/1.0 404 Not Found"); - exit; - } - $_SESSION['places']['queries']['number']++; - } - - $numberof = 0; - $sql = ''; - $ret = array(); - if (strlen($country)>3&&strtolower($country)!='list'&&strtolower($country)!='random') { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE lower(`Country`) LIKE '".strtolower($country)."' GROUP BY `CountryID` ORDER BY RAND() LIMIT 1"; - - } elseif (strlen($country)>3&&strtolower($country)=='random') { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY RAND() LIMIT 1"; - - } elseif (strlen($country)>3&&strtolower($country)=='list') { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY `Country` ASC "; - - } elseif (strlen($country)==3) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE lower(`ISO3`) LIKE '".strtolower($country)."' GROUP BY `CountryID` ORDER BY RAND() LIMIT 1 "; - } elseif (strlen($country)==2) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE lower(`ISO2`) LIKE '".strtolower($country)."' GROUP BY `CountryID` ORDER BY RAND() LIMIT 1 "; - } elseif (strlen($country)!=2 && strlen($country)!=3) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE lower(`Country`) LIKE '%".strtolower($country)."%' GROUP BY `CountryID` ORDER BY RAND() LIMIT 1 "; - } - $numberof=$seconds=$start=$end=0; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while($country = $GLOBALS['APIDB']->fetchArray($result)) { - $sql = "SELECT count(*) as records FROM `" . $country['Table'] . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - $table = $country['Table']; - if ($format!='xml') - $ret['countries'][$table] = strippedArray($country, explode('|', API_COUNTRY_FIELDS)); - else - $ret['countries'][$table] = strippedArray($country, explode('|', API_COUNTRY_FIELDS)); - - if (strtolower($place)!='random') { - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` WHERE LOWER(`RegionName`) LIKE '" . strtolower($place) . "%' ORDER BY RAND()"; - } elseif (strtolower($place) == 'random' && strtolower($country) == 'random') - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` ORDER BY RAND()"; - elseif (strtolower($place) == 'random' && strtolower($country) != 'random') { - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` ORDER BY RAND()"; - } else { - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` WHERE LOWER(`RegionName`) LIKE '%" . strtolower($place) . "%' ORDER BY RAND()"; - } - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - while ($region = $GLOBALS['APIDB']->fetchArray($resultb)) { - if ($seconds==0) - $seconds = 120; - elseif ($end!=0&&$start!=0) - $seconds = $seconds + ($end - $start * 3); - $start = microtime(true); - set_time_limit($seconds); - if (strpos($region['RegionName'], ',')) { - $parts = explode(',',$region['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $region['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($region['RegionName'])); - if ($format!='xml') - $ret['places'][$table][$region['key']] = strippedArray($region, explode('|', API_PLACES_FIELDS)); - else - $ret['places'][$table][$key] = strippedArray($region, explode('|', API_PLACES_FIELDS)); - $numberof++; - - if ($radius != API_RADIUS_DEFAULT) - { - $latitude = $region['Latitude_Float']; - $longitude = $region['Longitude_Float']; - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key`, 3956 * 2 * ASIN(SQRT(POWER(SIN((" . abs($latitude) . " - abs(`Latitude_Float`)) * pi() / 180 / 2), 2) + COS(" . abs($latitude) . " * pi() / 180 ) * COS(abs(`Latitude_Float`) * pi() / 180) * POWER(SIN((" . $longitude . " - `Longitude_Float`) * pi() / 180 / 2), 2) )) as `Distance` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` HAVING `Distance` <= ".$radius. " ORDER BY `Distance`"; - $ret['search']['countries']++; - unset($resultb); - if ($resultc = $GLOBALS['APIDB']->queryF($sql)) { - while ($nearby = $GLOBALS['APIDB']->fetchArray($resultc)) { - if (strpos($nearby['RegionName'], ',')) { - $parts = explode(',',$nearby['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $nearby['RegionName'] = implode(' ', $parts); - } - $keyb = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($nearby['RegionName'])); - if ($format!='xml') - $ret['places'][$table][$region['key']]['nearby'][$keyb] = strippedArray($nearby, explode('|',API_PLACES_FIELDS)); - else - $ret['places'][$table][$key]['nearby'][$keyb] = strippedArray($nearby, explode('|',API_PLACES_FIELDS)); - } - } - } - $end = microtime(true); - } - } - } - } - return $ret; - } -} - - - -if (!function_exists("findNearby")) { - - /* function findNearby() - * - * Function that finds a location(s) near a latitude & longitude - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @param $latitude float Latitude to search for places near by - * @param $longitude float Longitude to search for places near by - * @param $radius integer Radius that the search bounded by in kilometers (integer only) - * @param $format string API Output mode (JSON, XML, SERIAL, HTML, RAW) - * - * @return array - */ - function findNearby($latitude = 0, $longitude = 0, $radius = 0, $format = 'json') - { - session_start(); - if (!in_array(whitelistGetIP(true), whitelistGetIPAddy())) { - if (isset($_SESSION['places']['queries']['time'])) { - if ($_SESSION['places']['queries']['time']>time()) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - } elseif (!isset($_SESSION['places']['queries']['time'])) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - if ($_SESSION['places']['queries']['number']>MAXIMUM_QUERIES) { - header("HTTP/1.0 404 Not Found"); - exit; - } - $_SESSION['places']['queries']['number']++; - } - - $ret = array(); - $ret['search']['places'] = $ret['search']['countries'] = 0; - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while($country = $GLOBALS['APIDB']->fetchArray($result)) { - $table = $country['Table']; - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key`, 3956 * 2 * ASIN(SQRT(POWER(SIN((" . abs($latitude) . " - abs(`Latitude_Float`)) * pi() / 180 / 2), 2) + COS(" . abs($latitude) . " * pi() / 180 ) * COS(abs(`Latitude_Float`) * pi() / 180) * POWER(SIN((" . $longitude . " - `Longitude_Float`) * pi() / 180 / 2), 2) )) as `Distance` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` HAVING `Distance` <= ".$radius. " ORDER BY `Distance`"; - $ret['search']['countries']++; - unset($resultb); - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - while ($place = $GLOBALS['APIDB']->fetchArray($resultb)) { - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['search']['places']++; - } - } - } - } - $ret['search']['type'] = 'nearby'; - return $ret; - } -} - - - - -if (!function_exists("findExactly")) { - - /* function findNearby() - * - * Function that finds a location(s) near a latitude & longitude - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @param $latitude float Latitude to search for places near by - * @param $longitude float Longitude to search for places near by - * @param $radius integer Radius that the search bounded by in kilometers (integer only) - * @param $format string API Output mode (JSON, XML, SERIAL, HTML, RAW) - * - * @return array - */ - function findExactly($latitude = 0, $longitude = 0, $radius = 0, $format = 'json') - { - session_start(); - if (!in_array(whitelistGetIP(true), whitelistGetIPAddy())) { - if (isset($_SESSION['places']['queries']['time'])) { - if ($_SESSION['places']['queries']['time']>time()) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - } elseif (!isset($_SESSION['places']['queries']['time'])) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - if ($_SESSION['places']['queries']['number']>MAXIMUM_QUERIES) { - header("HTTP/1.0 404 Not Found"); - exit; - } - $_SESSION['places']['queries']['number']++; - } - - $ret = array(); - $places = $ret['search']['countries'] = $ret['search']['places'] = 0; - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while($country = $GLOBALS['APIDB']->fetchArray($result)) { - $table = $country['Table']; - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - $ret['search']['countries']++; - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key`, 3956 * 2 * ASIN(SQRT(POWER(SIN((" . abs($latitude) . " - abs(`Latitude_Float`)) * pi() / 180 / 2), 2) + COS(" . abs($latitude) . " * pi() / 180 ) * COS(abs(`Latitude_Float`) * pi() / 180) * POWER(SIN((" . $longitude . " - `Longitude_Float`) * pi() / 180 / 2), 2) )) as `Distance` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` WHERE `Latitude_Float` >= ".($latitude - (111.32 * ($radius / 1000)))." AND `Longitude_Float` >= ".($longitude - (111.32 * ($radius / 1000)))." AND `Latitude_Float` <= ".($latitude + (111.32 * ($radius / 1000)))." AND `Longitude_Float` <= ".($longitude + (111.32 * ($radius / 1000))). " ORDER BY `Distance`"; - unset($resultb); - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - while ($place = $GLOBALS['APIDB']->fetchArray($resultb)) { - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['search']['places']++; - } - } - } - } - $ret['search']['type'] = 'exactly'; - return $ret; - } -} - -if (!function_exists("findKey")) { - - /* function findKey() - * - * Function that reverse lookups a forensic identifier MD6 of country or region and return one or more locations - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @param $key string the MD5 32 Character Checksum for the place or country to lookup - * @param $radius integer Radius that the search bounded by in kilometers (integer only) - * @param $format string API Output mode (JSON, XML, SERIAL, HTML, RAW) - * - * @return array - */ - function findKey($key = '', $radius = 0, $format = 'json') - { - - session_start(); - if (!in_array(whitelistGetIP(true), whitelistGetIPAddy())) { - if (isset($_SESSION['places']['queries']['time'])) { - if ($_SESSION['places']['queries']['time']>time()) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - } elseif (!isset($_SESSION['places']['queries']['time'])) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - if ($_SESSION['places']['queries']['number']>MAXIMUM_QUERIES) { - header("HTTP/1.0 404 Not Found"); - exit; - } - $_SESSION['places']['queries']['number']++; - } - - error_reporting(E_ERROR); - session_start(); - if (isset($_SESSION['places']['queries']['time'])) { - if ($_SESSION['places']['queries']['time']>time()) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - } elseif (!isset($_SESSION['places']['queries']['time'])) { - $_SESSION['places']['queries']['number'] = 0; - $_SESSION['places']['queries']['time'] = time()+3600; - } - if ($_SESSION['places']['queries']['number']>MAXIMUM_QUERIES) { - header("HTTP/1.0 404 Not Found"); - exit; - } - $_SESSION['places']['queries']['number']++; - - $sql = ''; - $ret = array(); - $found = false; - $ret['results']['count'] = 0; - - if ( strpos($key, ":") > 0 ) - { - $countryid = substr($key, 0, strpos($key, ":") ); - $key = substr($key, strpos($key, ":") + 1); - } else - $countryid = false; - - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE '" . places_oldhash($key, 'countries') . "' LIKE concat('%', md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)), '%') GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - if($country = $GLOBALS['APIDB']->fetchArray($result)) { - $table = $country['Table']; - } - } - if (!isset($table) && !empty($countryid) && $countryid != false) - { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `CountryID` = '".$countryid."' GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - if($country = $GLOBALS['APIDB']->fetchArray($result)) { - $table = $country['Table']; - } - } - } - - if (empty($countryid) && $countryid == false && $found==false) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while(($cntry = $GLOBALS['APIDB']->fetchArray($result)) && $found == false) { - $sql = "SELECT *, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $cntry['Country'] . "'))) as `key`, concat(`RegionName`, '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE '" . places_oldhash($key, $cntry['Table']) . "' LIKE concat('%',md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "')),'%') ORDER BY RAND() LIMIT 1"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $place = $GLOBALS['APIDB']->fetchArray($resultb); - $table = $cntry['Table']; - $found = true; - if (!isset($country)) - $country = $cntry; - } - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby'] = array(); - $ret['results']['type'] = 'place'; - $ret['results']['count'] = 1; - $ret['results']['nearby'] = 0; - $ret['longitude'] = $place['Longitude_Float']; - $ret['latitude'] = $place['Latitude_Float']; - $ret['address'] = $places['Address']; - } - } - } elseif ($found==false && !empty($countryid)) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `CountryID` = '$countryid' ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while(($cntry = $GLOBALS['APIDB']->fetchArray($result)) && $found == false) { - $sql = "SELECT *, concat(`RegionName`, '" . $cntry['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $cntry['Country'] . "'))) as `key`, concat(`RegionName`, '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE '" . places_oldhash($key, $cntry['Table']) . "' LIKE concat('%',md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "')),'%') ORDER BY RAND() LIMIT 1"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $place = $GLOBALS['APIDB']->fetchArray($resultb); - $table = $cntry['Table']; - $found=true; - if (!isset($country)) - $country = $cntry; - } - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby'] = array(); - $ret['results']['type'] = 'place'; - $ret['results']['count'] = 1; - $ret['results']['nearby'] = 0; - $ret['longitude'] = $place['Longitude_Float']; - $ret['latitude'] = $place['Latitude_Float']; - $ret['address'] = $places['Address']; - } - } - } - - if (empty($countryid) && $countryid == false && $found==false) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while(($cntry = $GLOBALS['APIDB']->fetchArray($result)) && $found == false) { - $sql = "SELECT *, concat('" . !empty($country['CountryID'])?$country['CountryID'].":":"" . "', md5(concat(`Name`, `Vicinity`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE '" . places_oldhash($key, $cntry['Table']) . "' LIKE concat('%', md5(concat(`Name`, `Vicinity`)), '%') ORDER BY RAND() LIMIT 1"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $venue = $GLOBALS['APIDB']->fetchArray($resultb); - $sql = "SELECT *, concat(`RegionName`, '" . $cntry['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $cntry['Country'] . "'))) as `key`, concat(`RegionName`, '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE `CordID` = '" . $venue['CordID']. "'"; - if ($resultc = $GLOBALS['APIDB']->queryF($sql)) - $place = $GLOBALS['APIDB']->fetchArray($resultc); - $table = $ctry['Table']; - $found = true; - if (!isset($country)) - $country = $cntry; - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby'] = array(); - $ret['results']['type'] = 'place'; - $ret['results']['count'] = 1; - $ret['results']['nearby'] = 0; - $ret['longitude'] = $venue['Longitude']; - $ret['latitude'] = $venue['Latitude']; - $ret['address'] = $venue['Vicinity']; - } - } - } - } elseif ($found==false && !empty($countryid)) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `CountryID` = '$countryid' ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while(($cntry = $GLOBALS['APIDB']->fetchArray($result)) && $found == false) { - $sql = "SELECT *, concat('" . !empty($cntry['CountryID'])?$cntry['CountryID'].":":"" . "', md5(concat(`Name`, `Vicinity`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE '" . places_oldhash($key, $cntry['Table']) . "' LIKE concat('%', md5(concat(`Name`, `Vicinity`)), '%') ORDER BY RAND() LIMIT 1"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $venue = $GLOBALS['APIDB']->fetchArray($resultb); - $sql = "SELECT *, concat(`RegionName`, '" . $cntry['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $cntry['Country'] . "'))) as `key`, concat(`RegionName`, '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE `CordID` = '" . $venue['CordID']. "'"; - if ($resultc = $GLOBALS['APIDB']->queryF($sql)) - $place = $GLOBALS['APIDB']->fetchArray($resultc); - $table = $cntry['Table']; - $found=true; - if (!isset($country)) - $country = $cntry; - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby'] = array(); - $ret['results']['type'] = 'place'; - $ret['results']['count'] = 1; - $ret['results']['nearby'] = 0; - $ret['longitude'] = $venue['Longitude']; - $ret['latitude'] = $venue['Latitude']; - $ret['address'] = $venue['Vicinity']; - } - } - } - } - - if (empty($countryid) && $countryid == false && $found==false) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while(($cntry = $GLOBALS['APIDB']->fetchArray($result)) && $found == false) { - $sql = "SELECT *, concat('" . !empty($cntry['CountryID'])?$cntry['CountryID'].":":"" . "', md5(concat(`Name`, `Vicinity`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE '" . places_oldhash($key, $cntry['Table']) . "' LIKE concat('%', md5(concat(`Name`, `Vicinity`)), '%') ORDER BY RAND() LIMIT 1"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $address = $GLOBALS['APIDB']->fetchArray($resultb); - $sql = "SELECT *, concat(`RegionName`, '" . $cntry['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $cntry['Country'] . "'))) as `key`, concat(`RegionName`, '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE `CordID` = '" . $address['CordID']. "'"; - if ($resultc = $GLOBALS['APIDB']->queryF($sql)) - $place = $GLOBALS['APIDB']->fetchArray($resultc); - $table = $cntry['Table']; - $found = true; - if (!isset($country)) - $country = $cntry; - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby'] = array(); - $ret['results']['type'] = 'place'; - $ret['results']['count'] = 1; - $ret['results']['nearby'] = 0; - $ret['longitude'] = $address['Longitude']; - $ret['latitude'] = $address['Latitude']; - $ret['address'] = $address['Formatted']; - } - } - } - } elseif ($found==false && !empty($countryid)) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `CountryID` = '$countryid' ORDER BY RAND() "; - if ($result = $GLOBALS['APIDB']->queryF($sql)) { - while(($cntry = $GLOBALS['APIDB']->fetchArray($result)) && $found == false) { - $sql = "SELECT *, concat('" . !empty($cntry['CountryID'])?$cntry['CountryID'].":":"" . "', md5(concat(`Name`, `Vicinity`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE '" . places_oldhash($key, $cntry['Table']) . "' LIKE concat('%', md5(concat(`Name`, `Vicinity`)), '%') ORDER BY RAND() LIMIT 1"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $address = $GLOBALS['APIDB']->fetchArray($resultb); - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $cntry['Country'] . "'))) as `key`, concat(`RegionName`, '".$country['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE `CordID` = '" . $address['CordID']. "'"; - if ($resultc = $GLOBALS['APIDB']->queryF($sql)) - $place = $GLOBALS['APIDB']->fetchArray($resultc); - $table = $cntry['Table']; - $found=true; - if (!isset($country)) - $country = $cntry; - $sql = "SELECT count(*) as records FROM `" . $GLOBALS['APIDB']->prefix($table) . "`"; - if ($resultb = $GLOBALS['APIDB']->queryF($sql)) { - $records = $GLOBALS['APIDB']->fetchArray($resultb); - $country['records'] = $records['records']; - } else - $country['records'] = 0 ; - if ($format!='xml') - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby'] = array(); - $ret['results']['type'] = 'place'; - $ret['results']['count'] = 1; - $ret['results']['nearby'] = 0; - $ret['longitude'] = $address['Longitude']; - $ret['latitude'] = $address['Latitude']; - $ret['address'] = $address['Formatted']; - } - } - } - } - - if ($radius <= 0 || $radius == API_RADIUS_DEFAULT) - return $ret; - - if ($found == true && $radius > 0) { - if (isset($ret['results']['places'])) - $places = $ret['results']['places']; - elseif (isset($ret['results']['place'])) - $places = $ret['results']['place']; - foreach($places as $table => $values) { - foreach($values as $key => $place) { - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `Table` LIKE '$table' GROUP BY `CountryID` ORDER BY RAND() "; - $result = $GLOBALS['APIDB']->queryF($sql); - while($country = $GLOBALS['APIDB']->fetchArray($result)) { - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key`, 3956 * 2 * ASIN(SQRT(POWER(SIN((" . abs($place['Latitude_Float']) . " - abs(`Latitude_Float`)) * pi() / 180 / 2), 2) + COS(" . abs($place['Latitude_Float']) . " * pi() / 180 ) * COS(abs(`Latitude_Float`) * pi() / 180) * POWER(SIN((" . $place['Longitude_Float'] . " - `Longitude_Float`) * pi() / 180 / 2), 2) )) as `Distance`, concat(`RegionName`, '".$country['Country']."') as `Address` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` HAVING `Distance` <= ".$radius. " ORDER BY `Distance`"; - $resultb = $GLOBALS['APIDB']->queryF($sql); - while ($place = $GLOBALS['APIDB']->fetchArray($resultb)) { - if ($format!='xml') - $ret['results']['nearby']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - else - $ret['results']['nearby']['countries'][$table] = strippedArray($country, explode('|',API_COUNTRY_FIELDS)); - if (strpos($place['RegionName'], ',')) { - $parts = explode(',',$place['RegionName']); - array_reverse($parts); - foreach($parts as $key => $value) - $parts[$key] = trim($value); - $place['RegionName'] = implode(' ', $parts); - } - $key = str_replace(array(" ", "'", "-", "_", "\"", "`" , ",", "(", ")"), "", strtolower($place['RegionName'])); - if ($format!='xml') - $ret['results']['nearby']['places'][$table][$place['key']] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - else - $ret['results']['nearby']['places'][$table][$key] = strippedArray($place, explode('|',API_PLACES_FIELDS)); - $ret['results']['nearby']++; - } - } - } - } - if ($ret['results']['nearby']>0) - $ret['results']['type'] = 'keyed'; - } - return $ret; - } -} - -if (!function_exists("getAddressGeoMapping")) -{ - /** - * gets Geo Longitude + Latitude for an known address - * - * @param string $address - * @return number[]|unknown[]|mixed[] - */ - function getAddressGeoMapping($address = '') - { - $geo = json_decode(getURIData('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false'), true); - $latitude = $longitude = 0.0001; - if ($geo['status'] == 'OK') { - $address = array(); - foreach($geo['results'][0]['address_components'] as $values) - { - if (in_array('administrative_area_level_1', $values['types']) && in_array('political', $values['types'])) - $address['State'] = $values['long_name']; - elseif (in_array('administrative_area_level_2', $values['types']) && in_array('political', $values['types'])) - $address['Council'] = $values['short_name']; - elseif (in_array('postal_code', $values['types'])) - $address['Postcode'] = $values['long_name']; - elseif (in_array('subpremise', $values['types'])) - $address['Unit'] = $values['long_name']; - elseif (in_array('street_number', $values['types'])) - $address['Building'] = $values['long_name']; - elseif (in_array('route', $values['types'])) - $address['Street'] = $values['long_name']; - elseif (in_array('locality', $values['types'])) - $address['Suburb'] = $values['long_name']; - elseif (in_array('country', $values['types'])) { - $address['Country'] = $values['long_name']; - $address['Country_ISO2'] = $values['short_name']; - } - } - if (!isset($address['State']) && isset($address['Council'])) - { - $address['State'] = $address['Council']; - unset($address['Council']); - } - $address['Formatted'] = $geo['results'][0]['formatted_address']; - $address['GoogleID'] = $geo['results'][0]['place_id']; - $address['Longitude'] = $geo['results'][0]['geometry']['location']['lng']; - $address['Latitude'] = $geo['results'][0]['geometry']['location']['lat']; - $address['View_NE_Longitude'] = $geo['results'][0]['geometry']['viewport']['northeast']['lng']; - $address['View_NE_Latitude'] = $geo['results'][0]['geometry']['viewport']['northeast']['lat']; - $address['View_SW_Longitude'] = $geo['results'][0]['geometry']['viewport']['southwest']['lng']; - $address['View_SW_Latitude'] = $geo['results'][0]['geometry']['viewport']['southwest']['lat']; - - $sql = "SELECT *, md5(concat(`Country`, `Capital`, `Continent`, `CurrencyCode`)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE `Country` LIKE '%".$address['Country']."%' GROUP BY `CountryID` ORDER BY RAND() "; - $result = $GLOBALS['APIDB']->queryF($sql); - if ($country = $GLOBALS['APIDB']->fetchArray($result)) { - $table = $country['Table']; - $sql = "SELECT *, concat(`RegionName`, '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`Longitude_Float`, `Latitude_Float`, `RegionName`, '" . $country['Country'] . "'))) as `key`, 3956 * 2 * ASIN(SQRT(POWER(SIN((" . abs($geo['results'][0]['geometry']['location']['lat']) . " - abs(`Latitude_Float`)) * pi() / 180 / 2), 2) + COS(" . abs($geo['results'][0]['geometry']['location']['lat']) . " * pi() / 180 ) * COS(abs(`Latitude_Float`) * pi() / 180) * POWER(SIN((" . $geo['results'][0]['geometry']['location']['lng'] . " - `Longitude_Float`) * pi() / 180 / 2), 2) )) as `Distance`, concat(`RegionName`, '".$country['Country']."') as `Address` FROM `" . $country['Table'] . "` WHERE `RegionName` LIKE '%$suburb%' ORDER BY `Distance`"; - $resultb = $GLOBALS['APIDB']->queryF($sql); - $place = $GLOBALS['APIDB']->fetchArray($resultb); - $address['CordID'] = $place['CordID']; - $address['CountryID'] = $country['CountryID']; - - $types = array(); - foreach($geo['results'][0]['types'] as $tno => $type) - { - $sql = "SELECT count(*) as `count` from `" . $GLOBALS['APIDB']->prefix($table . "_address_types") . "` WHERE `Type` LIKE '$type'"; - list($count) = $GLOBALS['APIDB']->fetchRow( $GLOBALS['APIDB']->queryF($sql)); - $sql = "SELECT * from `" . $GLOBALS['APIDB']->prefix($table . "_address_types") ."` WHERE `Type`... [truncated message content] |