forked from ThaDafinser/Piwik-IntranetGeoIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntranetGeoIP.php
186 lines (155 loc) · 5.65 KB
/
IntranetGeoIP.php
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
185
186
<?php
/**
* @author https://github.com/ThaDafinser
*/
namespace Piwik\Plugins\IntranetGeoIP;
use Piwik\Plugin;
use Piwik\Network;
use Piwik\Log;
use Piwik\Notification;
use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig;
use Piwik\Tracker\Request as TrackerRequest;
class IntranetGeoIP extends Plugin
{
const DATA_EXAMPLE_FILE_PATH = __DIR__ . '/data.example.php';
const DATA_FILE_PATH = PIWIK_INCLUDE_PATH . '/config/IntranetGeoIP.data.php';
private static $result = [];
/**
*
* @return string
*/
private function getDataExampleFilePath()
{
return self::DATA_EXAMPLE_FILE_PATH;
}
/**
*
* @return string
*/
private function getDataFilePath()
{
return self::DATA_FILE_PATH;
}
/**
*
* @see \Piwik\Plugin::install()
*/
public function install()
{
return $this->copyDataFile();
}
/**
*
* @see \Piwik\Plugin::uninstall()
*/
public function uninstall()
{
if (file_exists($this->getDataFilePath())) {
unlink($this->getDataFilePath());
}
}
/**
*
* @see \Piwik\Plugin::activate()
*/
public function activate()
{
return $this->copyDataFile();
}
private function copyDataFile()
{
if (! file_exists($this->getDataFilePath()) && file_exists($this->getDataExampleFilePath())) {
copy($this->getDataExampleFilePath(), $this->getDataFilePath());
}
$notification = new Notification('Please edit the file ' . $this->getDataFilePath() . ' and fill in your data');
$notification->raw = true;
$notification->context = Notification::CONTEXT_INFO;
Notification\Manager::notify('IntranetGeoIp_DATA_ERROR', $notification);
return;
}
public static function getResult(array $userInfo)
{
if (!array_key_exists($userInfo['ip'], self::$result)) {
self::$result[$userInfo['ip']] = self::getNewResult($userInfo);
}
return self::$result[$userInfo['ip']];
}
private static function getNewResult(array $userInfo)
{
if (! file_exists(IntranetGeoIP::DATA_FILE_PATH)) {
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . IntranetGeoIP::DATA_FILE_PATH);
return [];
}
$data = include IntranetGeoIP::DATA_FILE_PATH;
if ($data === false) {
// no data file found
// @todo ...inform the user/ log something
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . IntranetGeoIP::DATA_FILE_PATH);
return [];
}
if (! is_array($data)) {
Log::error('Your data file seems to be not valid. The content is: ' . print_r($data, true) . ', File used: ' . IntranetGeoIP::DATA_FILE_PATH);
return [];
}
$ip = Network\IP::fromStringIP($userInfo['ip']);
foreach ($data as $value) {
if (isset($value['networks']) && $ip->isInRanges($value['networks']) === true) {
// values with the same key are not overwritten by right!
// http://www.php.net/manual/en/language.operators.array.php
if (isset($value['visitorInfo'])) {
return $value['visitorInfo'];
}
return [];
}
}
// if nothing was matched, you can define default values if you want to
if (isset($data['noMatch']) && isset($data['noMatch']['visitorInfo'])) {
return $data['noMatch']['visitorInfo'];
}
return [];
}
/**
* Called by event `Tracker.newVisitorInformation`
*
* @see registerEvents()
*/
public function logIntranetSubNetworkInfo(&$visitorInfo, TrackerRequest $request)
{
if (! file_exists($this->getDataFilePath())) {
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath());
return;
}
$data = include $this->getDataFilePath();
if ($data === false) {
// no data file found
// @todo ...inform the user/ log something
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath());
return;
}
if (! is_array($data)) {
Log::error('Your data file seems to be not valid. The content is: ' . print_r($data, true) . ', File used: ' . $this->getDataFilePath());
return;
}
$privacyConfig = new PrivacyManagerConfig();
$ipBinary = $request->getIp();
if ($privacyConfig->useAnonymizedIpForVisitEnrichment === true) {
$ipBinary = $visitorInfo['location_ip'];
}
$ip = Network\IP::fromBinaryIP($ipBinary);
foreach ($data as $value) {
if (isset($value['networks']) && $ip->isInRanges($value['networks']) === true) {
// values with the same key are not overwritten by right!
// http://www.php.net/manual/en/language.operators.array.php
if (isset($value['visitorInfo'])) {
$visitorInfo = $value['visitorInfo'] + $visitorInfo;
}
return;
}
}
// if nothing was matched, you can define default values if you want to
if (isset($data['noMatch']) && isset($data['noMatch']['visitorInfo'])) {
$visitorInfo = $data['noMatch']['visitorInfo'] + $visitorInfo;
return;
}
}
}