Skip to content

Commit

Permalink
DEV: implemented function to anonymize ip-addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Trischi80 committed Mar 17, 2020
1 parent 48d60c0 commit 62553bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
16 changes: 16 additions & 0 deletions application/controllers/admin/globalsettings.php
Expand Up @@ -404,6 +404,22 @@ public function surveySettings()
$oSurvey = SurveysGroupsettings::model()->findByPk($gsid);
$oSurvey->setOptions();

// TEST anonymize IP adresses
$ipToAnonymize = "192.168.2.5"; //this one should be anonymized ...
$result = anonymizeIpAddress($ipToAnonymize); // tested OK
// Test already anonymized ip
$result = anonymizeIpAddress($result); // tested OK
// TEST no ip address
$result = anonymizeIpAddress("not.an.ip"); // tested OK
$ipV6 = "2a03:2880:2110:df07:face:b00c::1";
$result = anonymizeIpAddress($ipV6);
$ipV6 = "2a03:2880:2110:df07:face:b00c::";
$result = anonymizeIpAddress($ipV6);
$ipV6 = "2a03::2110:df07:face:b00c::"; //this one is not a valid ipv6
$result = anonymizeIpAddress($ipV6);
$ipV6 = "2a03::2110:df07:face:b00c:3:4"; //this one is not a valid ipv6
$result = anonymizeIpAddress($ipV6);

$sPartial = Yii::app()->request->getParam('partial', '_generaloptions_panel');

if (isset($_POST)) {
Expand Down
31 changes: 25 additions & 6 deletions application/helpers/common_helper.php
Expand Up @@ -4888,14 +4888,33 @@ function resourceExtractFilter($p_event, &$p_header) {
* the ip address without changing it.
*
* @param string $ipAddress
* @return string
* @return string|boolean if ip is not anonymised false will be returned (in case of not a valid ip or ip has already been
* anonymised), else the anonymise ip will be returned as a string
*/
function anonymizeIpAdress($ipAddress){
$anonymizedIp = "";
function anonymizeIpAddress($ipAddress){
$anonymizedIp = false;

if(filter_var($ipAddress,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)){ //check if it is valid ipv4
$ipArray = explode('.', $ipAddress);
$last_digit = array_pop($ipArray);
if($last_digit!=0){ //check if it has already been anonymized
//set last number to 0
$anonymizedIp = implode('.',$ipArray); //without last digit ?!?
$anonymizedIp .= '.0';
}
} elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { //check if it is valid ipv6
$ipArray = explode(':', $ipAddress);
//the last 5 blocks have to be set to 0 ...
for ($i = 0; $i < 5; $i++) {
array_pop($ipArray);
}

//check if it is ipv4 or ipv6
//check if it has already been anonymized
//if not anonymize it ...
$anonymizedIp = implode(':', $ipArray);
//append last 5 blocks with 0
for ($i = 0; $i < 5; $i++) {
$anonymizedIp .= ':0';
}
}

return $anonymizedIp;
}

0 comments on commit 62553bd

Please sign in to comment.