forked from Open-Web-Analytics/Open-Web-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacotring IP validation and anonymization. fixes Open-Web-Analytics…
- Loading branch information
padams
committed
Feb 15, 2022
1 parent
613f5f8
commit e223521
Showing
2 changed files
with
64 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,33 +3,13 @@ | |
// | ||
// Open Web Analytics - An Open Source Web Analytics Framework | ||
// | ||
// Copyright 2006 Peter Adams. All rights reserved. | ||
// Copyright Peter Adams. All rights reserved. | ||
// | ||
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// $Id$ | ||
// | ||
|
||
//require_once 'owa_env.php'; | ||
|
||
//require_once(OWA_BASE_CLASS_DIR.'settings.php'); | ||
|
||
/** | ||
* Utility Functions | ||
* | ||
* @author Peter Adams <[email protected]> | ||
* @copyright Copyright © 2006 Peter Adams <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 | ||
* @category owa | ||
* @package owa | ||
* @version $Revision$ | ||
* @since owa 1.0.0 | ||
*/ | ||
class owa_lib { | ||
|
||
|
@@ -1353,50 +1333,46 @@ public static function moveFile( $oldfile, $newfile ) { | |
} | ||
} | ||
} | ||
|
||
public static function anonymizeIp( $ip_address ) { | ||
|
||
$ipv4NetMask = "255.255.255.0"; | ||
$ipv6NetMask = "ffff:ffff:ffff:ffff:0000:0000:0000:0000"; | ||
|
||
$packed_address = inet_pton( $ip_address); | ||
|
||
public static function isValidIp( $ip_address ) { | ||
|
||
// if valid ip address | ||
if ( ! empty( $ip_address ) | ||
&& ip2long( $ip_address ) != -1 | ||
&& ip2long( $ip_address ) != false | ||
) { | ||
|
||
return true; | ||
if ( strlen( $packed_address ) == 4 ) { | ||
|
||
return inet_ntop( inet_pton( $ip_address ) & inet_pton( $ipv4NetMask ) ); | ||
|
||
} elseif ( strlen( $packed_address ) == 16 ) { | ||
|
||
return inet_ntop( inet_pton( $ip_address ) & inet_pton( $ipv6NetMask ) ); | ||
} | ||
} | ||
|
||
public static function isIpv6SupportEnabled() { | ||
|
||
if ( defined( 'AF_INET6' ) ) { | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public static function isValidIp( $ip_address ) { | ||
|
||
return filter_var( $ip_address, FILTER_VALIDATE_IP, [] ); | ||
} | ||
|
||
// check to see if the IP address falls within known private IP ranges | ||
public static function isPrivateIp( $ip_address ) { | ||
|
||
$ip = ip2long( $ip_address); | ||
|
||
$private_ip_ranges = array ( | ||
array('0.0.0.0','2.255.255.255'), | ||
array('10.0.0.0','10.255.255.255'), | ||
array('127.0.0.0','127.255.255.255'), | ||
array('169.254.0.0','169.254.255.255'), | ||
array('172.16.0.0','172.31.255.255'), | ||
array('192.0.2.0','192.0.2.255'), | ||
array('192.168.0.0','192.168.255.255'), | ||
array('255.255.255.0','255.255.255.255') | ||
); | ||
|
||
//check to see if it falls within a known private range | ||
foreach ( $private_ip_ranges as $range ) { | ||
|
||
$min = ip2long( $range[0] ); | ||
$max = ip2long( $range[1] ); | ||
public static function isNotPrivateIp( $ip_address ) { | ||
|
||
if ( ( $ip >= $min ) && ( $ip <= $max ) ) { | ||
|
||
return true; | ||
} | ||
} | ||
|
||
// if it makes it through the checks then it's not private. | ||
return false; | ||
return filter_var( $ip_address, FILTER_VALIDATE_IP, ['flags' => FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ] ); | ||
} | ||
|
||
public static function isValidIpv6( $ip_address ) { | ||
|
||
return filter_var( $ip_address, FILTER_VALIDATE_IP, ['flags' => FILTER_FLAG_IPV6 ] ); | ||
} | ||
|
||
public static function keyExistsNotEmpty( $key, $array ) { | ||
|