[wp-trac] [WordPress Trac] #43545: Helper functions: Anonymizing data in a standardized way

WordPress Trac noreply at wordpress.org
Wed Mar 21 09:36:35 UTC 2018


#43545: Helper functions: Anonymizing data in a standardized way
--------------------------------+------------------------------
 Reporter:  dejliglama          |       Owner:
     Type:  enhancement         |      Status:  new
 Priority:  normal              |   Milestone:  Awaiting Review
Component:  Options, Meta APIs  |     Version:  trunk
 Severity:  normal              |  Resolution:
 Keywords:  needs-patch gdpr    |     Focuses:
--------------------------------+------------------------------

Comment (by birgire):

 If we use a wrapper like this one:

 {{{
 function wp_privacy_anonymize_ip( $ip_to_anonymize ) {
         return wp_privacy_anonymize_data( $ip_to_anonymize, 'ip' );
 }

 }}}

 then we could use:

 {{{
 case 'ip':
         $data_to_anonymize = _wp_privacy_anonymize_ip_handler(
 $data_to_anonymize );
         break;

 }}}

 in {{{wp_privacy_anonymize_data()}}}, where we could use the IP
 anonymizing part from the
 {{{WP_Community_Events::get_unsafe_client_ip()}}} method in
 [https://core.trac.wordpress.org/browser/tags/4.9/src/wp-admin/includes
 /class-wp-community-events.php#L235 (src)]:

 {{{
 /**
  * Partially anonymize the IP address by reducing it to the corresponding
 network ID.
  *
  * Modified from https://github.com/geertw/php-ip-anonymizer, MIT license.
  *
  * @since 5.0.0
  * @access private

  * @param  string $ip IP address.
  * @return string $ip Partially anonymized IP address.
  */
 function _wp_privacy_anonymize_ip( $ip ) {

         // Detect what kind of IP address this is.
         $is_ipv6 = substr_count( $ip, ':' ) > 1;
         $is_ipv4 = ( 3 === substr_count( $ip, '.' ) );

         if ( $is_ipv6 && $is_ipv4 ) {
                 // IPv6 compatibility mode, temporarily strip the IPv6
 part, and treat it like IPv4.
                 $ip_prefix = '::ffff:';
                 $ip = preg_replace( '/^\[?[0-9a-f:]*:/i', '', $ip );
                 $ip = str_replace( ']', '', $ip );
                 $is_ipv6   = false;
         }

         if ( $is_ipv6 ) {
                 // IPv6 addresses will always be enclosed in [] if there's
 a port.
                 $ip_start = 1;
                 $ip_end   = (int) strpos( $ip, ']' ) - 1;
                 $netmask  = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';

                 // Strip the port (and [] from IPv6 addresses), if they
 exist.
                 if ( $ip_end > 0 ) {
                         $ip = substr( $ip, $ip_start, $ip_end );
                 }

                 // Partially anonymize the IP by reducing it to the
 corresponding network ID.
                 if ( function_exists( 'inet_pton' ) && function_exists(
 'inet_ntop' ) ) {
                         $ip = inet_ntop( inet_pton( $ip ) & inet_pton(
 $netmask ) );
                 }
         } elseif ( $is_ipv4 ) {
                 // Strip any port and partially anonymize the IP.
                 $last_octet_position = strrpos( $ip, '.' );
                 $ip                  = substr( $ip, 0,
 $last_octet_position ) . '.0';
         } else {
                 return false;
         }

         // Restore the IPv6 prefix to compatibility mode addresses.
         $ip = $ip_prefix . $ip;

         return $ip;
 }

 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43545#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list