View source
<?php
define('GEOIP_LANGUAGE_NEGOTIATION_PATH', 8);
function geoip_language_help($path, $arg) {
switch ($path) {
case 'admin/settings/language/geoip':
$help = t('<p>This page provides an overview of your site\'s IP detection and language negotiation settings. You may configure which language is chosen when each country is detected from the user\'s IP, using the form below. All detectable languages are listed in the <em>Detected country</em> drop-down list, and all installed languages are listed in t the <em>Language</em> drop-down list. If a country is detected but doesn\'t have an entry in this list, the default language will be used.</p>');
return $help;
}
}
function geoip_language_menu() {
$items = array();
$items['admin/settings/language/geoip'] = array(
'title' => 'GeoIP',
'type' => MENU_LOCAL_TASK,
'weight' => 5,
'page callback' => 'geoip_language_settings_overview',
'access arguments' => array(
'administer languages',
),
'file' => 'geoip_language.admin.inc',
);
$items['admin/settings/language/geoip/delete/%'] = array(
'title' => 'Delete GeoIP mapping',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'geoip_admin_delete_mapping',
5,
),
'access arguments' => array(
'administer languages',
),
'type' => MENU_CALLBACK,
'file' => 'geoip_language.admin.inc',
);
return $items;
}
function geoip_language_init() {
$mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
$drush = function_exists('drush_verify_cli') && drush_verify_cli();
if (!$drush && $mode == GEOIP_LANGUAGE_NEGOTIATION_PATH && (int) variable_get('language_count', 1) > 1) {
geoip_language_negotiation();
}
}
function geoip_language_negotiation() {
$languages = language_list('enabled');
$languages = $languages[1];
$query = array();
parse_str($_SERVER['QUERY_STRING'], $query);
$q = $query['q'];
$args = explode('/', $q);
$prefix = array_shift($args);
foreach ($languages as $language) {
if (!empty($language->prefix) && $language->prefix == $prefix) {
$GLOBALS['language'] = $language;
$_GET['q'] = implode('/', $args);
drupal_init_path();
if (count($_SESSION)) {
$_SESSION['geoip_language'] = $language->language;
}
return;
}
}
global $user;
if (isset($_SESSION['geoip_language']) && isset($languages[$_SESSION['geoip_language']])) {
$language = $languages[$_SESSION['geoip_language']];
}
elseif ($user->uid && isset($languages[$user->language])) {
$language = $languages[$user->language];
}
else {
$language = geoip_language_detect_language();
}
$GLOBALS['language'] = $language;
global $base_path;
if ($_SERVER['SCRIPT_NAME'] != $base_path . 'index.php' || strpos($_GET['q'], file_directory_path()) === 0 || $_SERVER['REQUEST_METHOD'] == 'POST') {
return;
}
$url = url($_GET['q'], array(
'language' => $language,
'absolute' => TRUE,
'query' => drupal_query_string_encode($_GET, array(
'q',
)),
));
drupal_goto($url, NULL, NULL, 301);
exit;
}
if (!function_exists('custom_url_rewrite_outbound')) {
function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
global $language;
if (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) == GEOIP_LANGUAGE_NEGOTIATION_PATH && (int) variable_get('language_count', 1) > 1) {
if (!$options['external']) {
if (!isset($options['language'])) {
$options['language'] = $language;
}
if (!empty($options['language']->prefix)) {
$options['prefix'] = $options['language']->prefix . '/';
}
}
}
}
}
function geoip_language_form_locale_languages_configure_form_alter(&$form, $form_state) {
$form['language_negotiation']['#options'][GEOIP_LANGUAGE_NEGOTIATION_PATH] = t('Path prefix with GeoIP detection fallback.');
}
function geoip_language_mapping_create($country, $language) {
$data = array(
'country' => $country,
'language' => $language,
);
drupal_write_record('geoip_language', $data);
geoip_language_mappings(TRUE);
$countries = geoip_country_values();
watchdog('geoip_language', 'GeoIP mapping created for %country', array(
'%country' => $countries[$country],
));
return $data;
}
function geoip_language_mapping_delete($country) {
db_query('DELETE FROM {geoip_language} WHERE country="%s"', $country);
geoip_language_mappings(TRUE);
$countries = geoip_country_values();
watchdog('geoip_language', 'GeoIP mapping deleted for %country', array(
'%country' => $countries[$country],
));
}
function geoip_language_mappings($reset = FALSE) {
static $mapping = NULL;
if ($reset || !isset($mapping)) {
$file = file_directory_path() . '/geoip_language.txt';
if (file_exists($file)) {
$data = unserialize(file_get_contents($file));
$mapping = isset($data['geoip']) ? $data['geoip'] : NULL;
}
if ($reset || !$mapping) {
$mapping = array();
$data = array(
'geoip' => array(),
'default' => language_default('prefix'),
);
$result = db_query('SELECT g.*, l.prefix FROM {geoip_language} g INNER JOIN {languages} l on g.language = l.language ORDER BY g.country ASC');
while ($row = db_fetch_object($result)) {
$mapping[$row->country] = $row->language;
$data['geoip'][$row->country] = $row;
}
$file = file_save_data(serialize($data), 'geoip_language.txt', FILE_EXISTS_REPLACE);
}
}
return $mapping;
}
function geoip_language_detect_language($reset = FALSE) {
static $geoip_language = NULL;
if ($reset || !isset($geoip_language)) {
$languages = language_list('enabled');
$languages = $languages[1];
$mappings = geoip_language_mappings();
$country_code = geoip_country_code();
if ($country_code && $mappings[$country_code] && $languages[$mappings[$country_code]->language]) {
$geoip_language = $languages[$mappings[$country_code]->language];
}
else {
$geoip_language = language_default();
}
}
return $geoip_language;
}
function geoip_language_flush_caches() {
geoip_language_mappings(TRUE);
return array();
}