-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_lookup.module
184 lines (167 loc) · 5.35 KB
/
ip_lookup.module
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
178
179
180
181
182
183
184
<?php
/**
* @file
* The main module file.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_user_login().
*/
function ip_lookup_user_login($account) {
_ip_lookup_log_data($account);
}
/**
* Implements hook_user_insert().
*/
function ip_lookup_user_insert($account) {
$user = \Drupal::currentUser();
if (!$user->id()) {
_ip_lookup_log_data($account);
}
}
/**
* The function to get User Data.
*/
function _ip_lookup_log_data($account) {
// Get browser name, version, and platform.
$browser = _ip_lookup_get_browser();
$iplocation = \Drupal::service('ip_lookup.iplocation');
if (!empty($iplocation->getLocationQuery())) {
$location = $iplocation->getLocationQuery();
}
else {
$location = $iplocation->getLocation();
}
$keys = [
'mlid' => NULL,
];
$fields = [
'uid' => $account->id(),
'date' => time(),
'username' => $account->getDisplayName(),
'browser_name' => $browser['name'],
'browser_version' => $browser['version'],
'browser_platform' => $browser['platform'],
'ip' => $location['ip'],
'city' => $location['city'],
'region' => $location['region'],
];
\Drupal::database()->merge('ip_lookup')
->key($keys)
->fields($fields)
->execute();
}
/**
* Implements hook_help().
*/
function ip_lookup_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the ip_lookup module.
case 'help.page.ip_lookup':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('A simple module that tracks each time a user logs in and stores browser name, platform, browser version, city, region and user ID into your database.') . '</p>';
$output .= '<p>' . t('Go to configration <a href="/admin/config/ip-lookup/settings">page</a>.') . '<p>';
$output .= '<p>' . t('Put ipdata API key into the form and save configuration.(default key is test)') . '<p>';
$output .= '<p>' . t('You can get ipdata API key from <a href="https://ipdata.co/" target="_blank">https://ipdata.co/</a>.') . '<p>';
$output .= '<p>' . t('Ipdata provide a fast, highly available IP Geolocation API with reliable performance.') . '<p>';
$output .= '<p>' . t('Default ipdata test key will let you lookup few ip Geolocation.') . '<p>';
$output .= '<p>' . t('User IP lookup table will show up under the Manage -> People and then <a href="/admin/people/ip-lookup">User IP Lookup tab</a>.') . '<p>';
$output .= '<p>' . t('github: <a href="https://github.com/jiangzhan/ip_lookup" target="_blank">https://github.com/jiangzhan/ip_lookup</a>') . '<p>';
return $output;
}
}
/**
* Didn't use get_browser function.
*
* Because it needs a browscap.ini file on the server side.
*
* Two disadvantage about putting browscap.ini on the server that
* I quote from Drupal browscap module.
*
* It can be difficult or impossible to configure for
* shared hosting environments.
*
* The data used to identify browsers
* and determine their capabilities requires consistent
* maintenance to keep up-to-date. and also get_browser is slow.
*
* Copied this getBrowser function from php documentation page
* https://www.php.net/manual/en/function.get-browser.php
*
* It's not as fancy as other browsers detect library
* but it's enough for this module.
*/
function _ip_lookup_get_browser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
// First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'Linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'Mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'Windows';
}
// Next get the name of the useragent yes seperately and for good reason.
if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif (preg_match('/Firefox/i', $u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif (preg_match('/Chrome/i', $u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif (preg_match('/Safari/i', $u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif (preg_match('/Opera/i', $u_agent)) {
$bname = 'Opera';
$ub = "Opera";
}
elseif (preg_match('/Netscape/i', $u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}
// Finally get the correct version number.
$known = ['Version', $ub, 'other'];
$pattern = '#(?<browser>' . implode('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// We have no matching number just continue.
}
// See how many we have.
$i = count($matches['browser']);
if ($i != 1) {
// We will have two since we are not using 'other' argument yet
// see if version is before or after the name.
if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
$version = $matches['version'][0];
}
else {
$version = $matches['version'][1];
}
}
else {
$version = $matches['version'][0];
}
// Check if we have a number.
if ($version == NULL || $version == "") {
$version = "?";
}
return [
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern,
];
}