forked from herveh35/Piwik-IntranetGeoIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntranetGeoIP.php
144 lines (122 loc) · 3.69 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
<?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';
public function isTrackerPlugin()
{
return true;
}
/**
*
* @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;
}
/**
* @param string|null $visitorIP
* @return array
*/
public static function getResult($visitorIP = null)
{
if ($visitorIP!='0.0.0.0') {
return self::getNewResult($visitorIP);
}
return [];
}
/**
* @param string $visitorIP
* @return array
*/
private static function getNewResult($visitorIP)
{
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($visitorIP);
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 [];
}
}