This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.php
269 lines (233 loc) · 8.31 KB
/
API.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* Piwik PRO - Premium functionality and enterprise-level support for Piwik Analytics
*
* @link http://piwik.pro
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Organisations;
use Piwik\Archive;
use Piwik\Db;
use Piwik\Network\IPUtils;
use Piwik\Piwik;
use Piwik\Plugins\Organisations\Exception\IpRangesEmptyException;
use Piwik\Plugins\Organisations\Exception\IpRangesOverlapException;
class API extends \Piwik\Plugin\API
{
/**
* Returns \Piwik\DataTable for organisation report
*
* @param int $idSite
* @param string $period
* @param string $date
* @param string|boolean $segment
* @return \Piwik\DataTable|\Piwik\DataTable\Map
*/
public function getOrganisation($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTable(Archiver::ORGANISATIONS_RECORD_NAME);
$dataTable->filter('GroupBy', array(
'label',
function ($label) {
$model = new Model();
$organisation = $model->getOrganisation($label);
if (!empty($organisation['name'])) {
return $organisation['name'];
}
return Piwik::translate('General_Unknown');
}
));
$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');
return $dataTable;
}
/**
* Adds an new organisation
*
* @param string $name
* @param array $ipRanges
* @return int
*
* @throws IpRangesOverlapException if ip ranges overlap
* @throws IpRangesEmptyException if no valid ip range was passed
*/
public function addOrganisation($name, $ipRanges)
{
Piwik::checkUserHasSuperUserAccess();
$ipRanges = $this->validateIpRanges($ipRanges, null);
if (0 === count($ipRanges)) {
throw new IpRangesEmptyException(Piwik::translate('Organisations_ErrorIpRangesEmpty'));
}
$idOrg = $this->getModel()->createOrganisation(array(
'name' => $name,
'ipranges' => $ipRanges,
));
return $idOrg;
}
/**
* Updates an existing organisation.
*
* @param int $idOrg
* @param string $name
* @param array $ipRanges
*
* @throws IpRangesOverlapException if ip ranges overlap
* @throws IpRangesEmptyException if no valid ip range was passed
*/
public function updateOrganisation($idOrg, $name, $ipRanges)
{
Piwik::checkUserHasSuperUserAccess();
$ipRanges = $this->validateIpRanges($ipRanges, $ignoreIdOrg = $idOrg);
if (0 === count($ipRanges)) {
throw new IpRangesEmptyException(Piwik::translate('Organisations_ErrorIpRangesEmpty'));
}
$this->getModel()->updateOrganisation($idOrg, array(
'name' => $name,
'ipranges' => $ipRanges,
));
}
/**
* Deletes a specific organisation
*
* @param int $idOrg
*/
public function deleteOrganisation($idOrg)
{
Piwik::checkUserHasSuperUserAccess();
$this->getModel()->deleteOrganisation($idOrg);
}
/**
* Returns the list of organisations
*
* @return array
*/
public function getAvailableOrganisations()
{
Piwik::checkUserHasSomeViewAccess();
return $this->getModel()->getAll();
}
private function getModel()
{
return new Model();
}
/**
* Removes invalid IP ranges from list.
*
* @param array $ipRanges
* @param int $ignoreIdOrg
* @return array
* @throws IpRangesOverlapException if ip ranges overlap
*/
private function validateIpRanges($ipRanges, $ignoreIdOrg = null)
{
$filteredIpRanges = array();
$boundedIpRanges = array();
foreach ($ipRanges as $ipRange) {
$bounds = IPUtils::getIPRangeBounds($ipRange);
if ($bounds) {
$filteredIpRanges[] = $ipRange;
$boundedIpRanges[] = array(
'range' => $ipRange, // used for exception message
'bounds' => $bounds // used for bound checking
);
}
}
usort($boundedIpRanges, function ($a, $b) {
return strcmp($a['bounds'][0], $b['bounds'][0]);
});
$this->checkForInternalOverlap($boundedIpRanges);
$this->checkForGlobalOverlap($boundedIpRanges, $ignoreIdOrg);
return $filteredIpRanges;
}
/**
* Checks if the ip ranges of an organsation overlap with a different one.
*
* @param array $ipRanges
* @param int $ignoreIdOrg
*
* @throws IpRangesOverlapException if ip ranges overlap
*/
private function checkForGlobalOverlap(array $ipRanges, $ignoreIdOrg = null)
{
if (0 === count($ipRanges)) {
// skip check if there is no ip range
return;
}
$orgLowest = $ipRanges[0]['bounds'][0];
$orgHighest = $ipRanges[count($ipRanges) - 1]['bounds'][1];
$organisations = $this->getAvailableOrganisations();
$globalIpRanges = array();
if (0 === count($organisations)) {
// skip check if there are no other organisations
return;
}
foreach ($organisations as $organisation) {
if (null !== $ignoreIdOrg && $ignoreIdOrg == $organisation['idorg']) {
// ignore "own" organisation on update
continue;
}
foreach ($organisation['ipranges'] as $ipRange) {
$bounds = IPUtils::getIPRangeBounds($ipRange);
// completely below organisation range
if (0 < strcmp($orgLowest, $bounds[1])) {
continue;
}
// completely above organisation range
if (0 > strcmp($orgHighest, $bounds[0])) {
continue;
}
$globalIpRanges[] = array(
'name' => $organisation['name'], // used for exception message
'range' => $ipRange, // used for exception message
'bounds' => $bounds // used for bound checking
);
}
}
// skip check if no overlap possible
if (0 === count($globalIpRanges)) {
return;
}
foreach ($ipRanges as $ipRange) {
foreach ($globalIpRanges as $globalIpRange) {
if ((0 <= strcmp($ipRange['bounds'][0], $globalIpRange['bounds'][0]) && 0 >= strcmp($ipRange['bounds'][0], $globalIpRange['bounds'][1])) ||
(0 <= strcmp($ipRange['bounds'][1], $globalIpRange['bounds'][0]) && 0 >= strcmp($ipRange['bounds'][1], $globalIpRange['bounds'][1])) ||
(0 >= strcmp($ipRange['bounds'][0], $globalIpRange['bounds'][0]) && 0 <= strcmp($ipRange['bounds'][1], $globalIpRange['bounds'][1]))) {
throw new IpRangesOverlapException(
Piwik::translate(
'Organisations_ErrorIpRangesOverlapGlobal',
array($ipRange['range'], $globalIpRange['range'], $globalIpRange['name'])
)
);
}
}
}
}
/**
* Checks if the ip ranges that are inside one organisation overlap.
*
* @param array $ipRanges
*
* @throws IpRangesOverlapException if ip ranges overlap
*/
private function checkForInternalOverlap($ipRanges)
{
if (1 >= count($ipRanges)) {
// skip check if there is not more than one ip ranges
return;
}
for ($i = 0; $i < count($ipRanges) - 1; $i += 1) {
$a = $ipRanges[$i];
$b = $ipRanges[$i + 1];
if (0 <= strcmp($a['bounds'][1], $b['bounds'][0])) {
throw new IpRangesOverlapException(
Piwik::translate(
'Organisations_ErrorIpRangesOverlap',
array($b['range'], $a['range'])
)
);
}
}
}
}