forked from pklaus/IntranetSubNetwork
-
Notifications
You must be signed in to change notification settings - Fork 4
/
IntranetSubNetwork.php
127 lines (114 loc) · 4.81 KB
/
IntranetSubNetwork.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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik_Plugins
* @package IntranetSubNetwork
*/
namespace Piwik\Plugins\IntranetSubNetwork;
use Exception;
use Piwik\Common;
use Piwik\Db;
use Piwik\Network;
use Piwik\Piwik;
use Piwik\WidgetsList;
class IntranetSubNetwork extends \Piwik\Plugin
{
/**
* @see Piwik\Plugin::getListHooksRegistered
*/
public function getListHooksRegistered()
{
return array(
'Tracker.newVisitorInformation' => 'logIntranetSubNetworkInfo',
'WidgetsList.addWidgets' => 'addWidget',
'API.getReportMetadata' => 'getReportMetadata',
'API.getSegmentDimensionMetadata' => 'getSegmentsMetadata',
);
}
public function getReportMetadata(&$reports)
{
$reports[] = array(
'category' => Piwik::translate('General_Visitors'),
'name' => Piwik::translate('IntranetSubNetwork_WidgetIntranetSubNetwork'),
'module' => 'IntranetSubNetwork',
'action' => 'getIntranetSubNetwork',
'dimension' => Piwik::translate('IntranetSubNetwork_ColumnIntranetSubNetwork'),
'documentation' => Piwik::translate('IntranetSubNetwork_WidgetIntranetSubNetworkDocumentation', '<br />'),
'metrics' => array(
'nb_visits',
'nb_uniq_visitors',
'nb_visits_percentage' => Piwik::translate('General_ColumnPercentageVisits'),
),
// There is no processedMetrics for this report
'processedMetrics' => array(),
'order' => 50
);
}
public function getSegmentsMetadata(&$segments)
{
$segments[] = array(
'type' => 'dimension',
'category' => 'Visit',
'name' => Piwik::translate('IntranetSubNetwork_ColumnIntranetSubNetwork'),
'segment' => 'subnetwork',
'acceptedValues' => 'Global IPv4, Global IPv6 etc.',
'sqlSegment' => 'log_visit.location_subnetwork'
);
}
public function install()
{
// add column location_IntranetSubNetwork in the visit table
$query = "ALTER IGNORE TABLE `" . Common::prefixTable('log_visit') . "` ADD `location_subnetwork` VARCHAR( 100 ) NULL";
// if the column already exist do not throw error. Could be installed twice...
try {
Db::exec($query);
}
catch(Exception $e){
if(!Db::get()->isErrNo($e, '1060'))
throw $e;
}
}
public function uninstall()
{
// remove column location_IntranetSubNetwork from the visit table
$query = "ALTER TABLE `" . Common::prefixTable('log_visit') . "` DROP `location_subnetwork`";
Db::exec($query);
}
function addWidget()
{
WidgetsList::add('General_Visitors', 'IntranetSubNetwork_WidgetIntranetSubNetwork', 'IntranetSubNetwork', 'getIntranetSubNetwork');
}
/**
* Logs the IntranetSubNetwork in the log_visit table
*/
public function logIntranetSubNetworkInfo(&$visitorInfo)
{
$ip = Network\IP::fromBinaryIP($visitorInfo['location_ip']);
// by default, we want the network name to be the IP address:
$networkName = $ip;
/**
*********************************************************************************************
****************** adopt the following lines according to your subnets **********************
**/
// Some default subnets:
if ($ip->isInRanges(array('0.0.0.0/0'))) { $networkName = 'Global IPv4'; } // all IPv4 addresses
if ($ip->isInRanges(array('::/0'))) { $networkName = 'Global IPv6'; } // IPv6 addresses
if ($ip->isInRanges(array('::ffff:0:0/96'))) { $networkName = 'Global IPv4'; } // IPv4 mapped IPv6 addresses
// You may include your custom subnets:
//if $ip->isInRanges(array('141.2.0.0/16'))) { $networkName = 'University Frankfurt'; }
//if $ip->isInRanges(array('192.0.2.0/24'))) { $networkName = 'TEST-NET'; }
//if $ip->isInRanges(array('198.51.100.0/24'))) { $networkName = 'TEST-NET-2'; }
//if $ip->isInRanges(array('2001:db8::/33',
// '2001:db8:8000::/33'))) { $networkName = 'Doc-IPv6'; }
/**
******************* end adopt here to your subnets *****************************************
*********************************************************************************************
**/
// add the IntranetSubNetwork value in the table log_visit
$visitorInfo['location_subnetwork'] = substr($networkName, 0, 100);
}
}