From: <wis...@us...> - 2017-10-06 09:17:14
|
Revision: 13294 http://sourceforge.net/p/xoops/svn/13294 Author: wishcraft Date: 2017-10-06 09:17:10 +0000 (Fri, 06 Oct 2017) Log Message: ----------- resubmit places.xoops.org Removed 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/class/ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/crons/ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/data/ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/help.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/include/ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/index.php *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/install/ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/jquery/ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/style.css *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/verify.php Deleted: *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md =================================================================== --- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md 2017-10-06 07:04:29 UTC (rev 13293) +++ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/README.md 2017-10-06 09:17:10 UTC (rev 13294) @@ -1,34 +0,0 @@ -# Chronolabs Cooperative ~ GeoSpatial Places Lookups API - -## Author: Simon Antony Roberts <wis...@us...> - -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. - -# SEO Friendly URL - Apache Module Rewrite - -This goes in your API_ROOT_PATH/.htaccess file - - RewriteEngine On - RewriteRule ^v([0-9]{1,2})/([0-9a-zA-Z\ \(\)\-\,\:]+)/([0-9a-zA-Z\ \(\)\-\,\:]+)/([0-9\.]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&place=$3&radius=$4&output=$5 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/([0-9a-zA-Z\ \(\)\-\,\:]+)/([0-9a-zA-Z\ \(\)\-\,\:]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&place=$3&output=$4 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(nearby|exactly)/([0-9\.\-]+)/([0-9\.\-]+)/([0-9\.]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&latitude=$3&longitude=$4&radius=$5&output=$6 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(list|maps|details)/([a-zA-Z0-9\ \(\)\-\,]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&place=$3&output=$4 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(address)/([0-9a-z\-\,\ ]+)/(.*?)/([0-9\.]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&type=$3&address=$4&radius=$5&output=$6 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(address)/([0-9a-z\-\,\ ]+)/(.*?)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&type=$3&address=$4&output=$5 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(address)/(.*?)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&address=$3&output=$4 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(venues)/([0-9a-z\-\,\ ]+)/([0-9a-zA-Z\ \(\)\-\,\:]+)/([0-9\.]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&type=$3&place=$4&radius=$5&output=$6 [L,NC,QSA] - RewriteRule ^v([0-9]{1,2})/(venues)/([0-9a-z\-\,\ ]+)/([0-9a-zA-Z\ \(\)\-\,\:]+)/(raw|html|serial|json|xml).api ./index.php?version=$1&country=$2&type=$3&place=$4&output=$5 [L,NC,QSA] - -# Installing API - -Copy the contents of this distribution to your visually routable path via http(s) etc. Then poll the path required and run the install; - -it requires apache2, nixi, iis etc and best with php5+; - -# Cronjobs/Scheduled Tasks - -There is a couple of cronjobs to be set they are the following after running: $ crontab -e - - */15 * * * * /usr/bin/php -q /var/www/places.snails.email/crons/align-postcode-states.php - */25 * * * * /usr/bin/php -q /var/www/places.snails.email/crons/mining-localities.php - */15 */6 * * * /usr/bin/php -q /var/www/places.snails.email/crons/updating-details.php Deleted: *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php =================================================================== --- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php 2017-10-06 07:04:29 UTC (rev 13293) +++ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/apiconfig.php 2017-10-06 09:17:10 UTC (rev 13294) @@ -1,95 +0,0 @@ -<?php -/** - * Chronolabs REST Geospatial Places 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 Places Services API - * @see http://internetfounder.wordpress.com - * @see http://sourceoforge.net/projects/chronolabsapis - * @see https://github.com/Chronolabs-Cooperative/Places-API-PHP - */ - - -if (!is_file(__DIR__ . DIRECTORY_SEPARATOR . 'mainfile.php') || !is_file(__DIR__ . DIRECTORY_SEPARATOR . 'include' . DIRECTORY_SEPARATOR . 'license.php')) -{ - header('Location: ' . "./install"); - exit(0); -} - -require_once __DIR__ . DIRECTORY_SEPARATOR . 'mainfile.php'; - -/** - * Paths - */ -$pu = parse_url($_SERVER['REQUEST_URI']); -$source = (isset($_SERVER['HTTPS'])?'https://':'http://').strtolower($_SERVER['HTTP_HOST']).$pu['path']; -unset($pu); -define('PLACES_URL', $source); -define('API_ROOT_PATH', __DIR__); -define('API_CACHE_PATH', DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'places' . DIRECTORY_SEPARATOR . 'caches'); -define('API_TMP_PATH', DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'places'); - -/** - * Cronjob Constants and Defines - */ -define('API_CRON_NUMBER_COUNTRIES', 7); -define('API_CRON_NUMBER_REGIONS', 250); - -/** - * Times && Limits - */ -define('API_RADIUS_DEFAULT', 0.469); -define('API_GOOGLE_PAGES_RESULTS', 13); -define('API_CACHE_SECONDS', 8444); -define('MAXIMUM_QUERIES', 25); -ini_set('memory_limit', '128M'); - -/** - * 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|Places|Details|key|distance'); -define('API_CONTINENT_FIELDS', 'Continent|key|Population|Records|Places|Details|Updates'); -define('API_VENUE_FIELDS','key|Name|Icon|Id|Reference|Longitude|Latitude|View_NE_Longitude|View_NE_Latitude|View_SW_Longitude|View_SW_Latitude|Vicinity'); -define('API_ADDRESS_FIELDS','key|Unit|Building|Street|Suburb|State|Country|Country_ISO2|Postcode|Council|Longitude|Latitude|View_NE_Longitude|View_NE_Latitude|View_SW_Longitude|View_SW_Latitude|Formatted'); - -/** - * 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'; - -/** - * Opens Access Origin Via networking Route NPN - */ -header('Access-Control-Allow-Origin: *'); -header('Origin: *'); - -/** - * Turns of GZ Lib Compression for Document Incompatibility - */ -ini_set("zlib.output_compression", 'Off'); -ini_set("zlib.output_compression_level", -1); - -/** - * - * @var constants - */ -define("API_FILE_IO_PEERS", __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'peers.diz'); -define("API_FILE_IO_DOMAINS", __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'domains.diz'); -define("API_FILE_IO_FOOTER", __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'api-localhost.html'); -?> \ No newline at end of file Deleted: *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php =================================================================== --- *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php 2017-10-06 07:04:29 UTC (rev 13293) +++ *.xoops.org (Subdomain APIs & Sites)/places.xoops.org/functions.php 2017-10-06 09:17:10 UTC (rev 13294) @@ -1,2079 +0,0 @@ -<?php -/** - * Chronolabs REST Geospatial Places 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 Places Services API - * @see http://internetfounder.wordpress.com - * @see http://sourceoforge.net/projects/chronolabsapis - * @see https://github.com/Chronolabs-Cooperative/Places-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("randomPlacesKey")) { - - /* function randomPlacesKey() - * - * Get a random forensic identify key for a place from the database - * @author Simon Roberts (Chronolabs) simon@snails.email - * - * @return string(32) - */ - function randomPlacesKey() - { - $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(`CountryID`, `CordID`))) 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; - } - $ret = array(); - } - } -} - -if (!function_exists("randomGeoPlace")) { - - /* function randomPlacesKey() - * - * 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']); - } - $ret = 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` GROUP BY `CountryID` ORDER BY `Country` ASC "; - - } elseif (strlen($country)==3) { - $sql = "SELECT *, md5(concat(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `CordID`))) 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(`CountryID`, `CordID`))) 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(`CountryID`, `CordID`))) as `key` FROM `" . $GLOBALS['APIDB']->prefix($table) . "` ORDER BY RAND()"; - } else { - $sql = "SELECT *, concat(`RegionName`, ', ', '" . $country['Country'] . "') as `Address`, concat(`CountryID`, ':', md5(concat(`CountryID`, `CordID`))) 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) as `key` FROM `" . $GLOBALS['APIDB']->prefix('countries') . "` WHERE md5(concat(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) LIKE '".$key."' 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `CordID`))) as `key`, concat(`RegionName`, ', ', '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE md5(concat(`CountryID`, `CordID`)) LIKE '" . $key . "' 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `CordID`))) as `key`, concat(`RegionName`, ', ', '".$cntry['Country']."') as `Address` FROM `" . $cntry['Table'] . "` WHERE md5(concat(`CountryID`, `CordID`)) LIKE '" . $key . "' 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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('".$cntry['Table']."_venues', `VenueID`, `CordID`, `CountryID`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE md5(concat('".$cntry['Table']."_venues',`VenueID`,`CordID`,`CountryID`)) LIKE '" . $key . "' 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, ':', md5(concat('".$cntry['Table']."_venues', `VenueID`, `CordID`, `CountryID`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE md5(concat('".$cntry['Table']."_venues',`VenueID`,`CordID`,`CountryID`)) LIKE '" . $key . "' 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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('".$cntry['Table']."_address', `AddressID`, `CordID`, `CountryID`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE md5(concat('".$cntry['Table']."_address',`AddressID`,`CordID`,`CountryID`)) LIKE '" . $key . "' 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, ':', md5(concat('".$cntry['Table']."_address', `AddressID`, `CordID`, `CountryID`))) as `key` FROM `" . $cntry['Table'] . "_venues` WHERE md5(concat('".$cntry['Table']."_address',`AddressID`,`CordID`,`CountryID`)) LIKE '" . $key . "' 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(`CountryID`, `CordID`))) 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(`CountryID`, `Country`, max(`CountryID`) - `CountryID` + 1)) 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(`CountryID`, `CordID`))) 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_na... [truncated message content] |