'opt-in', 'california' => 'opt-out', 'restOfUs' => 'opt-out', 'restOfWorld' => 'opt-out', ); } /** The choice and region rules in a published banner config, or null when it has none. */ function saasaudit_consent_rules_from_config( $data ) { $config = is_array( $data ) && isset( $data['config'] ) && is_array( $data['config'] ) ? $data['config'] : array(); $choice = isset( $config['consentModel'] ) ? $config['consentModel'] : null; if ( ! in_array( $choice, array( 'auto', 'opt-in', 'opt-out' ), true ) ) { return null; } $regions = saasaudit_consent_default_regions(); $given = isset( $config['targeting']['regions'] ) && is_array( $config['targeting']['regions'] ) ? $config['targeting']['regions'] : array(); foreach ( array_keys( $regions ) as $key ) { if ( isset( $given[ $key ] ) && in_array( $given[ $key ], array( 'opt-in', 'opt-out' ), true ) ) { $regions[ $key ] = $given[ $key ]; } } return array( 'choice' => $choice, 'regions' => $regions, // Whether the tenant also applies Global Privacy Control to analytics (marketing only otherwise). 'gpc_analytics' => isset( $config['targeting']['gpcAppliesToAnalytics'] ) && true === $config['targeting']['gpcAppliesToAnalytics'], ); } /** The newest loader build a published banner config names ("latestLoaderBuild", the commit a * build is of), or '' when it names none that looks like one. */ function saasaudit_consent_build_from_config( $data ) { $build = is_array( $data ) && isset( $data['latestLoaderBuild'] ) ? $data['latestLoaderBuild'] : ''; return is_string( $build ) && 1 === preg_match( '/^[0-9a-f]{7,40}$/', $build ) ? $build : ''; } /** Where the banner config answer for this site is kept for five minutes (the rules, and * under the same name with "_build" the newest loader build). */ function saasaudit_consent_rules_key( $settings ) { return 'saasaudit_consent_rules_' . md5( $settings['token'] . $settings['api_base_url'] ); } /** The consent rules the site's own banner config says, asked of our API and kept for five * minutes; null when they cannot be learned (then the visitor's country decides). With * `$refresh` the answer kept is ignored and the API is asked again (the background refresh). */ function saasaudit_consent_api_rules( $refresh = false ) { $settings = saasaudit_consent_settings(); if ( '' === $settings['token'] ) { return null; } $key = saasaudit_consent_rules_key( $settings ); $cached = $refresh ? false : get_transient( $key ); if ( false !== $cached ) { return is_array( $cached ) ? $cached : null; } $host = wp_parse_url( home_url(), PHP_URL_HOST ); if ( ! is_string( $host ) || '' === $host ) { return null; } $base = saasaudit_consent_https_origin( $settings['api_base_url'] ); $base = '' === $base ? SAASAUDIT_DEFAULT_API : $base; $response = wp_remote_get( $base . '/api/v1/domains/banner-config?scriptToken=' . rawurlencode( $settings['token'] ), array( 'timeout' => 2, 'redirection' => 0, // Our API only answers a request that says which site it is for (the Origin a browser sends). 'headers' => array( 'Origin' => 'https://' . $host ), ) ); $rules = null; $build = ''; if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) { $data = json_decode( wp_remote_retrieve_body( $response ), true ); $rules = saasaudit_consent_rules_from_config( $data ); $build = saasaudit_consent_build_from_config( $data ); } set_transient( $key, null === $rules ? 'none' : $rules, null === $rules ? 60 : 300 ); set_transient( $key . '_build', $build, null === $rules ? 60 : 300 ); return $rules; } /** * The model a visitor is treated under. A fixed choice applies to everyone; "auto" follows the * region rules and is opt-in when the country is unknown. `$region` is Cloudflare's region (its * code "CA" or the name "California"), which matters inside the US and for Quebec in Canada. */ function saasaudit_consent_resolve_model( $rules, $country, $region = '' ) { $rules = null === $rules ? array( 'choice' => 'auto', 'regions' => saasaudit_consent_default_regions(), ) : $rules; if ( 'auto' !== $rules['choice'] ) { return $rules['choice']; } $country = strtoupper( trim( (string) $country ) ); if ( 1 !== preg_match( '/^[A-Z]{2}$/', $country ) || 'XX' === $country ) { return 'opt-in'; } $region = trim( (string) $region ); $region = function_exists( 'mb_strtolower' ) ? mb_strtolower( $region, 'UTF-8' ) : strtolower( $region ); if ( in_array( $country, SAASAUDIT_OPT_IN_COUNTRIES, true ) ) { return $rules['regions']['euUk']; } // Quebec is the one part of a country that is opt-in (Canada as a whole is not). if ( 'CA' === $country && in_array( $region, array( 'qc', 'quebec', "qu\xc3\xa9bec" ), true ) ) { return $rules['regions']['euUk']; } if ( 'US' !== $country ) { return $rules['regions']['restOfWorld']; } return 'ca' === $region || 'california' === $region ? $rules['regions']['california'] : $rules['regions']['restOfUs']; } // Cloudflare's published proxy ranges (https://www.cloudflare.com/ips/). The country and region // headers below are the visitor's own to send, so they are believed only on a request that // arrived from one of these. A request from this machine is not believed on its own: a local // reverse proxy can pass a visitor's own headers on, so such a site opts in with the filter below. const SAASAUDIT_CLOUDFLARE_RANGES = array( '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/13', '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22', '2400:cb00::/32', '2606:4700::/32', '2803:f800::/32', '2405:b500::/32', '2405:8100::/32', '2a06:98c0::/29', '2c0f:f248::/32', ); /** Whether the address `$ip` lies inside the range `$cidr` (IPv4 or IPv6); false when either is malformed. */ function saasaudit_consent_ip_in_range( $ip, $cidr ) { $parts = explode( '/', $cidr ); $net = @inet_pton( $parts[0] ); $addr = @inet_pton( (string) $ip ); if ( false === $net || false === $addr || strlen( $net ) !== strlen( $addr ) || ! isset( $parts[1] ) ) { return false; } $bits = (int) $parts[1]; $whole = intdiv( $bits, 8 ); if ( substr( $net, 0, $whole ) !== substr( $addr, 0, $whole ) ) { return false; } $rest = $bits % 8; if ( 0 === $rest ) { return true; } $mask = ( 0xff << ( 8 - $rest ) ) & 0xff; return ( ord( $net[ $whole ] ) & $mask ) === ( ord( $addr[ $whole ] ) & $mask ); } /** * Whether the request reached this site through Cloudflare, so its country headers can be believed. * A visitor who reaches the server directly can send any header, and a made-up country must not * move them from opt-in to opt-out, so such a request is treated as unknown. A site behind another * trusted proxy or tunnel (including one connecting from this machine, which proves nothing about * where the headers came from) can allow it with the `saasaudit_trust_geo_headers` filter. */ function saasaudit_consent_request_is_from_cloudflare() { $remote = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $trusted = false; foreach ( SAASAUDIT_CLOUDFLARE_RANGES as $range ) { if ( $trusted ) { break; } $trusted = saasaudit_consent_ip_in_range( $remote, $range ); } return (bool) apply_filters( 'saasaudit_trust_geo_headers', $trusted, $remote ); } /** The visitor's country and region from Cloudflare's request headers; empty (unknown) when the request did not come through Cloudflare. */ function saasaudit_consent_visitor_location() { if ( ! saasaudit_consent_request_is_from_cloudflare() ) { return array( '', '' ); } $read = static function ( $name ) { return isset( $_SERVER[ $name ] ) ? sanitize_text_field( wp_unslash( $_SERVER[ $name ] ) ) : ''; }; $region = $read( 'HTTP_CF_REGION_CODE' ); return array( $read( 'HTTP_CF_IPCOUNTRY' ), '' !== $region ? $region : $read( 'HTTP_CF_REGION' ) ); } /** The model this visitor is treated under by the site's banner rules; null when they are unknown. */ function saasaudit_consent_visitor_model() { $rules = saasaudit_consent_api_rules(); if ( null === $rules ) { return null; } list( $country, $region ) = saasaudit_consent_visitor_location(); return saasaudit_consent_resolve_model( $rules, $country, $region ); } /** * What a visitor with no consent cookie is allowed. `$rules` are the site's own from our API; null * when unknown, and then the visitor's country alone decides: opt-in in the EU, EEA, UK and * Switzerland (and when Cloudflare reports no country), opt-out elsewhere. Global Privacy Control * refuses marketing under opt-out, and analytics too when the banner's rules say so ('gpc_analytics'). * Null allows nothing optional. */ function saasaudit_consent_default_allowed( $rules, $country, $gpc, $region = '' ) { if ( 'opt-out' !== saasaudit_consent_resolve_model( $rules, $country, $region ) ) { return null; } $refused = '1' === trim( (string) $gpc ); return array( 'functional' => true, 'analytics' => ! ( $refused && ! empty( $rules['gpc_analytics'] ) ), 'marketing' => ! $refused, ); }