-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipredirect.module
More file actions
84 lines (70 loc) · 2.13 KB
/
ipredirect.module
File metadata and controls
84 lines (70 loc) · 2.13 KB
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
<?php
/**
* @file
* Redirect to user's language by IP location.
*/
/**
* Implements of hook_menu()
*/
function ipredirect_menu() {
$items = array();
$items['select-language'] = array(
'title' => 'Welcome to my page',
'access callback' => TRUE,
'page callback' => 'ipredirect_select_lang_page',
'file' => 'ipredirect.pages.inc',
);
return $items;
}
/**
* Implements hook_init()
*/
function ipredirect_init() {
if (drupal_is_cli()) {
// Do nothing on comment line interface (CLI).
return ;
}
global $base_url, $base_path, $language;
$path = drupal_get_path('module', 'ipredirect');
// Include library.
include($path . "/lib/geoip.inc");
// Open IP database.
$gi = geoip_open($path . "/lib/data/GeoIP.dat", GEOIP_STANDARD);
// Get user IP.
$ip = $_SERVER['REMOTE_ADDR'];
// $ip = "115.76.143.46"; // vn
// $ip = "176.32.98.44"; // nl
// $ip = "192.168.1.23"; // LAN
// $ip = "127.0.0.1"; // localhost
$country_code = strtolower(geoip_country_code_by_addr($gi, $ip));
// Close db file.
geoip_close($gi);
// # homepage or localhost or LAN IP.
// homepage: $_GET['q'] = node.
if ($_GET['q'] !== 'node' || empty($country_code) || isset($_COOKIE['user_lang'])) {
return ;
}
if (!isset($_COOKIE['user_lang'])) {
// Redirect to select language page.
drupal_goto($base_url . '/select-language' );
}
// else {
// // Redirect to selected language.
// if ($language->language != $_COOKIE['user_lang'])
// drupal_goto($base_url . '/' . $_COOKIE['user_lang']);
// }
// $current_language = empty($language->language) ? 'en' : $language->language;
// // drupal_set_message($country_code . '; lg: ' . $language->language);
// $new_language = '';
// if ($country_code == 'vn' && $current_language != 'vi') {
// $new_language = 'vi';
// }
// else if ($country_code != 'vn' && $current_language == 'vi') {
// $new_language = 'en';
// }
// // Redirect to base URL.
// if ($new_language != '') {
// setcookie("user_lang", $new_language, time() + (7 * (24 * 60 * 60)), $base_path);
// drupal_goto($base_url . '/' . $new_language);
// }
}