Advertisement
skiabox

wp-wpml-geoip-browser-language-redirect-master/WPML_GeoIP_IP

Feb 21st, 2014
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Class for handling the GeoIP conversion
  4.  */
  5.  
  6. class WPML_GeoIP_IPResolver
  7. {
  8.     var $db;
  9.     var $language_mappings;
  10.     var $default_language;
  11.  
  12.     /**
  13.      * Load dependencies, etc
  14.      */
  15.     function __construct()
  16.     {
  17.         /**
  18.          * MaxMind Country codes
  19.          *
  20.          * AP, EU, AD, AE, AF, AG, AI, AL, AM, CW, AO, AQ, AR, AS, AT, AU, AW, AZ, BA, BB, BD, BE, BF, BG,
  21.          * BH, BI, BJ, BM, BN, BO, BR, BS, BT, BV, BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN,
  22.          * CO, CR, CU, CV, CX, CY, CZ, DE, DJ, DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, FI, FJ, FK, FM,
  23.          * FO, FR, SX, GA, GB, GD, GE, GF, GH, GI, GL, GM, GN, GP, GQ, GR, GS, GT, GU, GW, GY, HK, HM, HN,
  24.          * HR, HT, HU, ID, IE, IL, IN, IO, IQ, IR, IS, IT, JM, JO, JP, KE, KG, KH, KI, KM, KN, KP, KR, KW,
  25.          * KY, KZ, LA, LB, LC, LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, MG, MH, MK, ML, MM, MN, MO, MP,
  26.          * MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, NO, NP, NR, NU, NZ, OM, PA,
  27.          * PE, PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, PY, QA, RE, RO, RU, RW, SA, SB, SC, SD, SE, SG,
  28.          * SH, SI, SJ, SK, SL, SM, SN, SO, SR, ST, SV, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TM, TN, TO, TL,
  29.          * TR, TT, TV, TW, TZ, UA, UG, UM, US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, WF, WS, YE, YT, RS, ZA,
  30.          * ZM, ME, ZW, A1, A2, O1, AX, GG, IM, JE, BL, MF, BQ, SS, O1
  31.          */
  32.  
  33.         //Array with structure MaxMind Code => WPML Code
  34.         $this->language_mappings = array(
  35.             'SE' => 'sv', //Sweden
  36.             'NO' => 'nb', //Norway
  37.             'FI' => 'fi', //Finland
  38.             'DK' => 'da', //Denmark
  39.             'US' => 'en', //USA
  40.             'CA' => 'en', //Canada
  41.             'DE' => 'de', //Germany
  42.             'GR' => 'el'  //Greece
  43.         );
  44.  
  45.         //Set the default WPML language which is used if no matching language is found
  46.         $this->default_language = 'en';
  47.  
  48.         include('lib/geoip-api-php/geoip.inc');
  49.         include('lib/geoip-api-php/geoipregionvars.php');
  50.         include('lib/geoip-api-php/timezone/timezone.php');
  51.  
  52.         //MaxMind gets cranky when we don't use the full path
  53.         $this->db = geoip_open(plugin_dir_path(__FILE__) . '/database/GeoIP.dat', GEOIP_STANDARD);
  54.     }
  55.     /**
  56.      * Returns a WPML-compatible country code from an IP address
  57.      *
  58.      * @param $ip
  59.      * @param bool $as_json
  60.      * @return string
  61.      */
  62.     function ip_to_wpml_country_code($ip, $as_json = true)
  63.     {
  64.         //This returns empty string if something went wrong
  65.         $country_code = @geoip_country_code_by_addr($this->db, $ip);
  66.  
  67.         //Try to match against language mappings
  68.         foreach($this->language_mappings as $maxmind_code => $wpml_code)
  69.         {
  70.             if($maxmind_code == $country_code)
  71.                 return $this->return_country_code($wpml_code);
  72.         }
  73.  
  74.         //We didn't match anything, return the default code
  75.         return $this->return_country_code($this->default_language);
  76.     }
  77.  
  78.     function return_country_code($country_code)
  79.     {
  80.         return json_encode(array('country_code' => $country_code));
  81.     }
  82.  
  83.     function set_json_header()
  84.     {
  85.         //http://stackoverflow.com/a/11112311/2572827
  86.         header('Content-Type: application/json');
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement