/** * Uses the GeoIP PECL module to get a visitor's location based on their IP address. * * This function will return different results based on the data available. If a city * database can be detected by the PECL module, it may return the country code, * region code, city name, area code, latitude, longitude and postal code of the visitor. * * Alternatively, if only the country database can be detected, only the country code * will be returned. * * The GeoIP PECL module will detect the following filenames: * - GeoIP.dat * - GeoIPCity.dat * - GeoIPISP.dat * - GeoIPOrg.dat * * Note how GeoLiteCity.dat, the name for the GeoLite city database, is not detected * by the PECL module. * * @param array $info Must have an 'ip' field. * @return array */ public function getLocation($info) { $ip = $this->getIpFromInfo($info); $result = array(); // get location data if (self::isCityDatabaseAvailable()) { // Must hide errors because missing IPV6: $location = @geoip_record_by_name($ip); if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location['country_code']; $result[self::REGION_CODE_KEY] = $location['region']; if ($location['region'] == "18" && $location['city'] == "Milan") { $result[self::REGION_CODE_KEY] = "09"; } $result[self::CITY_NAME_KEY] = utf8_encode($location['city']); $result[self::AREA_CODE_KEY] = $location['area_code']; $result[self::LATITUDE_KEY] = $location['latitude']; $result[self::LONGITUDE_KEY] = $location['longitude']; $result[self::POSTAL_CODE_KEY] = $location['postal_code']; } } else { if (self::isRegionDatabaseAvailable()) { $location = @geoip_region_by_name($ip); if (!empty($location)) { $result[self::REGION_CODE_KEY] = $location['region']; $result[self::COUNTRY_CODE_KEY] = $location['country_code']; } } else { $result[self::COUNTRY_CODE_KEY] = @geoip_country_code_by_name($ip); } } // get organization data if the org database is available if (self::isOrgDatabaseAvailable()) { $org = @geoip_org_by_name($ip); if ($org !== false) { $result[self::ORG_KEY] = utf8_encode($org); } } // get isp data if the isp database is available if (self::isISPDatabaseAvailable()) { $isp = @geoip_isp_by_name($ip); if ($isp !== false) { $result[self::ISP_KEY] = utf8_encode($isp); } } if (empty($result)) { return false; } $this->completeLocationResult($result); return $result; }
/** * Init */ public function init() { /** * Define */ $time_zone_name = null; /* * Check user ip */ $ip = \Yii::$app->request->getUserIP(); /** * Exclude ip */ foreach ($this->changeByIp as $filter => $timezone) { if ($filter === $ip || ($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos)) { $time_zone_name = $timezone; break; } } /** * Find timezone */ if (!$time_zone_name) { try { $country = geoip_country_code_by_name($ip); $region = geoip_region_by_name($ip); $time_zone_name = geoip_time_zone_by_country_and_region($country, $region['region']); } catch (\Exception $e) { } if (!$time_zone_name) { $time_zone_name = $this->default; } } /** * Set timezone */ \Yii::$app->setTimeZone($time_zone_name); }
public static function getCountry($allow_countory, $deny_countory) { // Block countory via Geolocation $country_code = false; if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) { // CloudFlareを使用している場合、そちらのGeolocationを読み込む // https://www.cloudflare.com/wiki/IP_Geolocation $country_code = $_SERVER['HTTP_CF_IPCOUNTRY']; } else { if (isset($_SERVER['GEOIP_COUNTRY_CODE'])) { // サーバーが$_SERVER['GEOIP_COUNTRY_CODE']を出力している場合 // Apache : http://dev.maxmind.com/geoip/mod_geoip2 // nginx : http://wiki.nginx.org/HttpGeoipModule // cherokee : http://www.cherokee-project.com/doc/config_virtual_servers_rule_types.html $country_code = $_SERVER['GEOIP_COUNTRY_CODE']; } else { if (function_exists('geoip_db_avail') && geoip_db_avail(GEOIP_COUNTRY_EDITION) && function_exists('geoip_region_by_name')) { // それでもダメな場合は、phpのgeoip_region_by_name()からGeolocationを取得 // http://php.net/manual/en/function.geoip-region-by-name.php $geoip = geoip_region_by_name(REMOTE_ADDR); $country_code = $geoip['country_code']; if (DEBUG) { $info[] = !empty($geoip['country_code']) ? 'GeoIP is usable. Your country code from IP is inferred <var>' . $geoip['country_code'] . '</var>.' : 'GeoIP is NOT usable. Maybe database is not installed. Please check <a href="http://www.maxmind.com/app/installation?city=1" rel="external">GeoIP Database Installation Instructions</a>'; } } else { if (function_exists('apache_note')) { // Apacheの場合 $country_code = apache_note('GEOIP_COUNTRY_CODE'); } } } } if (DEBUG) { // 使用可能かをチェック $info[] = isset($country_code) && !empty($country_code) ? 'Your country code from IP is inferred <var>' . $country_code . '</var>.' : 'Seems Geolocation is not available. <var>' . $deny_countory . '</var> value and <var>' . $allow_countory . '</var> value is ignoled.'; } return $country_code; }
/** * 環境変数のチェック */ public static function checkEnv($env) { global $deny_countory, $allow_countory; // 国別設定 $country_code = ''; if (isset($env['HTTP_CF_IPCOUNTRY'])) { // CloudFlareを使用している場合、そちらのGeolocationを読み込む // https://www.cloudflare.com/wiki/IP_Geolocation $country_code = $env['HTTP_CF_IPCOUNTRY']; } else { if (isset($env['GEOIP_COUNTRY_CODE'])) { // サーバーが$_SERVER['GEOIP_COUNTRY_CODE']を出力している場合 // Apache : http://dev.maxmind.com/geoip/mod_geoip2 // nginx : http://wiki.nginx.org/HttpGeoipModule // cherokee : http://www.cherokee-project.com/doc/config_virtual_servers_rule_types.html $country_code = $env['GEOIP_COUNTRY_CODE']; } else { if (function_exists('geoip_db_avail') && geoip_db_avail(GEOIP_COUNTRY_EDITION) && function_exists('geoip_region_by_name')) { // それでもダメな場合は、phpのgeoip_region_by_name()からGeolocationを取得 // http://php.net/manual/en/function.geoip-region-by-name.php $geoip = geoip_region_by_name(REMOTE_ADDR); $country_code = $geoip['country_code']; $info[] = !empty($geoip['country_code']) ? 'GeoIP is usable. Your country code from IP is inferred <var>' . $geoip['country_code'] . '</var>.' : 'GeoIP is NOT usable. Maybe database is not installed. Please check <a href="http://www.maxmind.com/app/installation?city=1" rel="external">GeoIP Database Installation Instructions</a>'; } else { if (function_exists('apache_note')) { // Apacheの場合 $country_code = apache_note('GEOIP_COUNTRY_CODE'); } } } } // 使用可能かをチェック if (!isset($country_code) || empty($country_code)) { $info[] = 'Seems Geolocation is not available. <var>$deny_countory</var> value and <var>$allow_countory</var> value is ignoled.'; } else { $info[] = 'Your country code from IP is inferred <var>' . $country_code . '</var>.'; if (isset($deny_countory) && !empty($deny_countory)) { if (in_array($country_code, $deny_countory)) { die('Sorry, access from your country(' . $geoip['country_code'] . ') is prohibited.'); exit; } } if (isset($allow_countory) && !empty($allow_countory)) { if (!in_array($country_code, $allow_countory)) { die('Sorry, access from your country(' . $geoip['country_code'] . ') is prohibited.'); exit; } } } // INI_FILE: $agents: UserAgentの識別 $user_agent = $matches = array(); $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; // unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua); // safety if (empty($user_agent['agent'])) { die; } // UAが取得できない場合は処理を中断 foreach (self::loadConfig('profile.ini.php') as $agent) { if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) { $user_agent = array('profile' => isset($agent['profile']) ? $agent['profile'] : null, 'name' => isset($matches[1]) ? $matches[1] : null, 'vers' => isset($matches[2]) ? $matches[2] : null); break; } } $ua_file = self::add_homedir($user_agent['profile'] . '.ini.php'); if ($ua_file) { require $ua_file; } define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : null); define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : null); define('UA_CSS', isset($user_agent['css']) ? $user_agent['css'] : null); // HTTP_X_REQUESTED_WITHヘッダーで、ajaxによるリクエストかを判別 define('IS_AJAX', isset($env['HTTP_X_REQUESTED_WITH']) && strtolower($env['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || isset($vars['ajax'])); }
<?php /* Codeine * @author bergstein@trickyplan.com * @description * @package Codeine * @version 8.x */ setFn('LatLon', function ($Call) { $Record = geoip_record_by_name($Call['HTTP']['IP']); return ['lat' => $Record['latitude'], 'lon' => $Record['longitude']]; }); setFn('Country', function ($Call) { return geoip_country_code_by_name($Call['HTTP']['IP']); }); setFn('City', function ($Call) { return geoip_record_by_name($Call['HTTP']['IP'])['city']; }); setFn('Region', function ($Call) { return geoip_region_by_name($Call['HTTP']['IP']); });
/** * Get country code and region. * * @return array|false Region data or FALSE on failure */ public function getRegion() { return geoip_region_by_name($this->ip); }
/** * Returns the timezone for a IP (or the default on error) * * @param string $ip The IP to look up * @param string $default The default timezone to use on error * @return string The timezone (e.g. 'Europe/Dublin') */ public static function getTimezone($ip, $default) { if (!defined('GEOIP_COUNTRY_EDITION')) { return $default; } if (!geoip_db_avail(GEOIP_COUNTRY_EDITION)) { return $default; } $tz = @geoip_time_zone_by_country_and_region(@geoip_country_code_by_name($ip), @geoip_region_by_name($ip)); if ($tz === false) { $tz = @geoip_time_zone_by_country_and_region(@geoip_country_code_by_name($ip)); } if ($tz === false) { return $default; } return $tz; }