-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnonymIPPlugin.inc.php
78 lines (69 loc) · 2.12 KB
/
AnonymIPPlugin.inc.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
<?php
/**
* @file AnonymIPPlugin.inc.php
*
* Copyright (c) 2013-2015 Simon Fraser University Library
* Copyright (c) 2003-2015 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @package plugins.generic.anonymIP
* @class AnonymIPPlugin
*
* Remove the last octet from the IP addresses before saving them in the database
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class AnonymIPPlugin extends GenericPlugin {
/**
* @copydoc PKPPlugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.anonymIP.name');
}
/**
* @copydoc PKPPlugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.anonymIP.description');
}
/**
* copydoc PKPPlugin::isSitePlugin()
*/
function isSitePlugin() {
return true;
}
/**
* @copydoc LazyLoadPlugin::register()
*/
function register($category, $path) {
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
HookRegistry::register('eventlogdao::_insertobject', array($this, 'anonymize'));
HookRegistry::register('emaillogdao::_insertobject', array($this, 'anonymize'));
HookRegistry::register('commentdao::_insertcomment', array($this, 'anonymize'));
HookRegistry::register('commentdao::_updatecomment', array($this, 'anonymize'));
if (!Config::getVar('security', 'session_check_ip')) {
HookRegistry::register('sessiondao::_insertsession', array($this, 'anonymize'));
HookRegistry::register('sessiondao::_updateobject', array($this, 'anonymize'));
}
}
return $success;
}
/**
* Hook callback: Handle requests.
* Anonymize the IP adress -- set the last octet to zero.
* @param $hookName string The name of the hook being invoked
* @param $args array The parameters to the invoked hook
*/
function anonymize($hookName, $args) {
$params =& $args[1];
$ipPattern = '/([0-9]+\\.[0-9]+\\.[0-9]+)\\.[0-9]+/';
foreach ($params as $index => $param) {
if (preg_match($ipPattern, $param)) {
$anonymizedIP = preg_replace($ipPattern, '\\1.0', $param);
$params[$index] = $anonymizedIP;
}
}
return false;
}
}
?>