Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvipsoft <vipsoft@59fd770c-687e-43c8-a1e3-f5a4ff64c105>2010-02-28 19:08:43 +0300
committervipsoft <vipsoft@59fd770c-687e-43c8-a1e3-f5a4ff64c105>2010-02-28 19:08:43 +0300
commitbc760688c6c4b83ebb0b486fcd8c2e58f482a732 (patch)
tree6f41f1add29f639e36f37020e5a66888fef9063c /plugins/AnonymizeIP
parentb0dcc2d1a3a9b4b76a744a367540bc4361c5a90f (diff)
fixes #692 - plugin (deactivated by default) to anonymize visitor IP addresses; the number of octets to mask is configurable; let me know if I've missed any edge cases in the unit tests
Diffstat (limited to 'plugins/AnonymizeIP')
-rw-r--r--plugins/AnonymizeIP/AnonymizeIP.php80
-rw-r--r--plugins/AnonymizeIP/tests/AnonymizeIP.test.php46
2 files changed, 126 insertions, 0 deletions
diff --git a/plugins/AnonymizeIP/AnonymizeIP.php b/plugins/AnonymizeIP/AnonymizeIP.php
new file mode 100644
index 0000000000..67082cb7ac
--- /dev/null
+++ b/plugins/AnonymizeIP/AnonymizeIP.php
@@ -0,0 +1,80 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
+ * @version $Id$
+ *
+ * @category Piwik_Plugins
+ * @package Piwik_AnonymizeIP
+ */
+
+/**
+ * Anonymize visitor IP addresses to comply with the privacy laws/guidelines in countries, such as Germany.
+ *
+ * @package Piwik_AnonymizeIP
+ */
+class Piwik_AnonymizeIP extends Piwik_Plugin
+{
+ /**
+ * Get plugin information
+ */
+ public function getInformation()
+ {
+ return array(
+ 'name' => 'AnonymizeIP',
+ 'description' => Piwik_Translate('AnonymizeIP_PluginDescription'),
+ 'author' => 'Piwik',
+ 'author_homepage' => 'http://piwik.org/',
+ 'version' => Piwik_Version::VERSION,
+ 'TrackerPlugin' => true,
+ );
+ }
+
+ /**
+ * Get list of hooks to register
+ */
+ public function getListHooksRegistered()
+ {
+ return array(
+ 'Tracker.saveVisitorInformation' => 'anonymizeVisitorIpAddress',
+ );
+ }
+
+ /**
+ * Internal function to mask portions of the visitor IP address
+ *
+ * @param $ip Unsigned long representation of IP address
+ * @param $maskLength Number of octets to reset
+ */
+ static public function applyIPMask($ip, $maskLength)
+ {
+ $maskedIP = pack('V', (float)$ip);
+
+ switch($maskLength) {
+ case 4:
+ $maskedIP[3] = "\0";
+ case 3:
+ $maskedIP[2] = "\0";
+ case 2:
+ $maskedIP[1] = "\0";
+ case 1:
+ $maskedIP[0] = "\0";
+ case 0:
+ default:
+ }
+
+ $res = unpack('V', $maskedIP);
+ return sprintf("%u", $res[1]);
+ }
+
+ /**
+ * Hook on Tracker.saveVisitorInformation to anonymize visitor IP addresses
+ */
+ function anonymizeVisitorIpAddress($notification)
+ {
+ $visitorInfo =& $notification->getNotificationObject();
+ $visitorInfo['location_ip'] = self::applyIPMask($visitorInfo['location_ip'], Piwik_Tracker_Config::getInstance()->Tracker['ip_address_mask_length']);
+ }
+}
diff --git a/plugins/AnonymizeIP/tests/AnonymizeIP.test.php b/plugins/AnonymizeIP/tests/AnonymizeIP.test.php
new file mode 100644
index 0000000000..7afd3e68bc
--- /dev/null
+++ b/plugins/AnonymizeIP/tests/AnonymizeIP.test.php
@@ -0,0 +1,46 @@
+<?php
+if(!defined("PIWIK_PATH_TEST_TO_ROOT")) {
+ define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../../..');
+}
+if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
+{
+ require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php";
+}
+
+if(!class_exists('Piwik_AnonymizeIP', false))
+{
+ require_once dirname(__FILE__) . '/../AnonymizeIP.php';
+}
+
+class Test_Piwik_AnonymizeIP extends UnitTestCase
+{
+ // IP addresses and expected results
+ protected $ipAddresses = array(
+ // long => array( expected0, expected1, expected2, expected3, expected4 ),
+ '0' => array( 0, 0, 0, 0, 0 ), // 00 00 00 00
+ '1' => array( 1, 0, 0, 0, 0 ), // 00 00 00 01
+ '255' => array( 255, 0, 0, 0, 0 ), // 00 00 00 FF
+ '256' => array( 256, 256, 0, 0, 0 ), // 00 00 01 00
+ '257' => array( 257, 256, 0, 0, 0 ), // 00 00 01 01
+ '65535' => array( 65535, 65280, 0, 0, 0), // 00 00 FF FF
+ '65536' => array( 65536, 65536, 65536, 0, 0), // 00 01 00 00
+ '65793' => array( 65793, 65792, 65536, 0, 0), // 00 01 01 01
+ '16777215' => array( 16777215, 16776960, 16711680, 0, 0), // 00 FF FF FF
+ '16777216' => array( 16777216, 16777216, 16777216, 16777216, 0), // 01 00 00 00
+ '2147483647' => array( 2147483647, 2147483392, 2147418112, 2130706432, 0), // 7F FF FF FF
+ '2147483648' => array( '2147483648', '2147483648', '2147483648', '2147483648', 0), // 80 00 00 00
+ '4294967295' => array( '4294967295', '4294967040', '4294901760', '4278190080', 0), // FF FF FF FF
+ );
+
+ public function test_applyIPMask()
+ {
+ foreach($this->ipAddresses as $ip => $expected)
+ {
+ // each IP is tested with 0 to 4 octets masked
+ for($maskLength = 0; $maskLength <= 4; $maskLength++)
+ {
+ $this->assertTrue( Piwik_AnonymizeIP::applyIPMask($ip, $maskLength) == $expected[$maskLength] );
+ }
+ }
+ }
+}