1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | <?php // // Open Web Analytics - An Open Source Web Analytics Framework // // Copyright 2010 Peter Adams. All rights reserved. // // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // $Id$ // require_once(OWA_BASE_DIR.'/owa_location.php'); if (!class_exists('PEAR_Exception')) { set_include_path(get_include_path().PATH_SEPARATOR.OWA_MODULES_DIR.'maxmind_geoip/includes/PEAR-1.9.1/'); } define('OWA_MAXMIND_DIR', OWA_MODULES_DIR. 'maxmind_geoip'.DIRECTORY_SEPARATOR. 'includes'.DIRECTORY_SEPARATOR. 'Net_GeoIP-1.0.0RC3'.DIRECTORY_SEPARATOR); if (!class_exists('Net_GeoIP')) { require_once(OWA_MAXMIND_DIR.'Net/GeoIP.php'); } set_include_path( get_include_path().PATH_SEPARATOR. OWA_MODULES_DIR.'maxmind_geoip/includes/Net_GeoIP-1.0.0RC3/' ); require_once(OWA_MODULES_DIR. 'maxmind_geoip'.DIRECTORY_SEPARATOR. 'includes'.DIRECTORY_SEPARATOR. 'maxmind-ws/GeoCityLocateIspOrg.class.php'); /** * Maxmind Geolocation Wrapper * * See http://www.maxmind.com/app/php for API documentation * * @author Peter Adams <peter@openwebanalytics.com> * @copyright Copyright © 2010 Peter Adams <peter@openwebanalytics.com> * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.4.0 */ class owa_maxmind extends owa_location { /** * URL template for REST based web service * * @var unknown_type */ var $ws_url = ''; var $db_file_dir; var $db_file_name = 'GeoLiteCity.dat'; var $db_file_path; var $db_File_present = false; /** * Constructor * * @return owa_hostip */ function __construct() { if ( ! defined( 'OWA_MAXMIND_DATA_DIR' ) ) { define('OWA_MAXMIND_DATA_DIR', OWA_DATA_DIR.'maxmind'.DIRECTORY_SEPARATOR); } $this->db_file_path = OWA_MAXMIND_DATA_DIR.$this->db_file_name; if ( file_exists( $this->db_file_path ) ) { $this->db_file_present = true; } else { owa_coreAPI::notice('Maxmind DB file could is not present at: ' . OWA_MAXMIND_DATA_DIR); } return parent::__construct(); } function isDbReady() { return $this->db_file_present; } /** * Fetches the location from the Maxmind local db * * @param string $ip */ function getLocation($location_map) { if ( ! $this->isDbReady() ) { return $location_map; } if ( ! array_key_exists( 'ip_address', $location_map ) ) { return $location_map; } // check for shared memory capability if ( function_exists( 'shmop_open' ) ) { $flag = Net_GeoIP::SHARED_MEMORY ; } else { $flag = Net_GeoIp::STANDARD ; } $geoip = Net_GeoIP::getInstance($this->db_file_path, $flag); $location = $geoip->lookupLocation($location_map['ip_address']); if ($location) { $location_map['city'] = strtolower(trim($location->__get('city'))); $location_map['state'] = strtolower(trim($location->__get('region'))); $location_map['country'] = strtolower(trim($location->__get('countryName'))); $location_map['country_code'] = strtoupper(trim($location->__get('countryCode'))); $location_map['country_code3'] = strtoupper(trim($location->__get('countryCode3'))); $location_map['latitude'] = trim($location->__get('latitude')); $location_map['longitude'] = trim($location->__get('longitude')); $location_map['dma_code'] = trim($location->__get('dmaCode')); $location_map['area_code'] = trim($location->__get('areaCode')); $location_map['postal_code'] = trim($location->__get('postalCode')); } return $location_map; } function getLocationFromWebService($location_map) { $license_key = owa_coreAPI::getSetting('maxmind_geoip', 'ws_license_key'); if ( ! array_key_exists( 'ip_address', $location_map ) ) { return $location_map; } $geoloc = GeoCityLocateIspOrg::getInstance(); $geoloc->setLicenceKey( $license_key ); $geoloc->setIP( $location_map['ip_address'] ); if ( $geoloc->isError() ) { owa_coreAPI::debug( $geoloc->isError().": " . $geoloc->getError() ); return $location_map; } $location_map['city'] = strtolower( trim( $geoloc->getCity() ) ); $location_map['state'] = strtolower( trim($geoloc->getState() ) ); $location_map['country'] = strtolower( trim( $geoloc->lookupCountryCode( $geoloc->getCountryCode() ) ) ); $location_map['country_code'] = strtoupper( trim($geoloc->getCountryCode() ) ); $location_map['latitude'] = trim( $geoloc->getLat() ); $location_map['longitude'] = trim( $geoloc->getLong() ); $location_map['dma_code'] = trim( $geoloc->getMetroCode() ); $location_map['dma'] = trim( $geoloc->lookupMetroCode( $geoloc->getMetroCode() ) ); $location_map['area_code'] = trim( $geoloc->getAreaCode() ); $location_map['postal_code'] = trim( $geoloc->getZip() ); $location_map['isp'] = trim( $geoloc->getIsp() ); $location_map['organization'] = trim( $geoloc->getOrganization() ); $location_map['subcountry_code'] = trim( $geoloc->lookupSubCountryCode( $geoloc->getState(), $geoloc->getCountryCode() ) ); return $location_map; } } ?> |