Skip to content

Commit

Permalink
Encrypt/decrypt IP
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszarendt committed Mar 12, 2021
1 parent 8d93885 commit 7018d53
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions includes/counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ private function save_cookie( $id, $cookie = array(), $expired = true ) {
private function save_ip( $id ) {
$set_cookie = apply_filters( 'pvc_maybe_set_cookie', true );

// Cookie Notice compatibility
if ( function_exists( 'cn_cookies_accepted' ) && ! cn_cookies_accepted() )
$set_cookie = false;

if ( $set_cookie !== true )
return $id;

Expand All @@ -380,7 +376,7 @@ private function save_ip( $id ) {
$ip_cache = array();

// get user IP address
$user_ip = (string) $this->get_user_ip();
$user_ip = $this->get_user_ip();

// get current time
$current_time = current_time( 'timestamp', true );
Expand Down Expand Up @@ -709,6 +705,8 @@ public function ipv4_in_range( $ip, $range ) {
* @return string
*/
public function get_user_ip() {
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';

foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) {
if ( array_key_exists( $key, $_SERVER ) === true ) {
foreach ( explode( ',', $_SERVER[$key] ) as $ip ) {
Expand All @@ -717,12 +715,12 @@ public function get_user_ip() {

// attempt to validate IP
if ( $this->validate_user_ip( $ip ) )
return $ip;
continue;
}
}
}

return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
return $ip;
}

/**
Expand All @@ -737,6 +735,67 @@ public function validate_user_ip( $ip ) {

return true;
}

/**
* Encrypt user IP
*
* @param int $ip
* @return string $encrypted_ip
*/
public function encrypt_ip( $ip ) {
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
$auth_iv = defined( 'NONCE_KEY' ) ? NONCE_KEY : '';

// mcrypt strong encryption
if ( function_exists( 'mcrypt_encrypt' ) && defined( 'MCRYPT_BLOWFISH' ) ) {
// get max key size of the mcrypt mode
$max_key_size = mcrypt_get_key_size( MCRYPT_BLOWFISH, MCRYPT_MODE_CBC );
$max_iv_size = mcrypt_get_iv_size( MCRYPT_BLOWFISH, MCRYPT_MODE_CBC );

$encrypt_key = mb_strimwidth( $auth_key, 0, $max_key_size );
$encrypt_iv = mb_strimwidth( $auth_iv, 0, $max_iv_size );

$encrypted_ip = strtr( base64_encode( mcrypt_encrypt( MCRYPT_BLOWFISH, $encrypt_key, $ip, MCRYPT_MODE_CBC, $encrypt_iv ) ), '+/=', '-_,' );
// simple encryption
} elseif ( function_exists( 'gzdeflate' ) )
$encrypted_ip = base64_encode( convert_uuencode( gzdeflate( $ip ) ) );
// no encryption
else
$encrypted_ip = strtr( base64_encode( convert_uuencode( $ip ) ), '+/=', '-_,' );

return $encrypted_ip;
}

/**
* Decrypt user IP
*
* @param int $encrypted_ip
* @return string $ip
*/
public function decrypt_ip( $encrypted_ip ) {
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
$auth_iv = defined( 'NONCE_KEY' ) ? NONCE_KEY : '';

// mcrypt strong encryption
if ( function_exists( 'mcrypt_decrypt' ) && defined( 'MCRYPT_BLOWFISH' ) ) {
// get max key size of the mcrypt mode
$max_key_size = mcrypt_get_key_size( MCRYPT_BLOWFISH, MCRYPT_MODE_CBC );
$max_iv_size = mcrypt_get_iv_size( MCRYPT_BLOWFISH, MCRYPT_MODE_CBC );

$encrypt_key = mb_strimwidth( $auth_key, 0, $max_key_size );
$encrypt_iv = mb_strimwidth( $auth_iv, 0, $max_iv_size );

$ip = mcrypt_decrypt( MCRYPT_BLOWFISH, $encrypt_key, base64_decode( strtr( $encrypted_ip, '-_,', '+/=' ) ), MCRYPT_MODE_CBC, $encrypt_iv );
// simple encryption
} elseif ( function_exists( 'gzinflate' ) ) {
$ip = gzinflate( convert_uudecode( base64_decode( $encrypted_ip ) ) );
// no encryption
} else {
$ip = convert_uudecode( base64_decode( strtr( $encrypted_ip, '-_,', '+/=' ) ) );
}

return $ip;
}

/**
* Register REST API endpoints.
Expand Down

0 comments on commit 7018d53

Please sign in to comment.