": a ">" inside a quoted // attribute value ("1 > 0") does not end the tag. const SAASAUDIT_INSIDE_TAG = '(?:[^>"\']++|"[^"]*"|\'[^\']*\')*+'; /** The host and path of an absolute or protocol-relative address; null for a relative one. */ function saasaudit_consent_split_url( $address ) { $address = trim( html_entity_decode( (string) $address, ENT_QUOTES ) ); if ( 0 === strpos( $address, '//' ) ) { $address = 'https:' . $address; } if ( 1 !== preg_match( '#^https?://#i', $address ) ) { return null; } $parts = wp_parse_url( $address ); if ( ! is_array( $parts ) || empty( $parts['host'] ) ) { return null; } return array( 'host' => strtolower( $parts['host'] ), 'path' => strtolower( isset( $parts['path'] ) ? $parts['path'] : '/' ), ); } /** * The site's own hold-back rules, from the install tag's value (data-hold-rules): host => array( * category, kind, label). Read again here, as the consent script does: only a valid host, category * and kind make a rule, and no more than 40 are used. */ function saasaudit_consent_site_rules() { $settings = saasaudit_consent_settings(); $value = isset( $settings['hold_rules'] ) ? (string) $settings['hold_rules'] : ''; $rules = array(); if ( '' === $value ) { return $rules; } $categories = array( 'f' => 'functional', 'a' => 'analytics', 'm' => 'marketing', 'p' => 'sensitive' ); $kinds = array( 's' => 'script', 'f' => 'frame' ); foreach ( array_slice( explode( ';', $value ), 0, 40 ) as $entry ) { $fields = explode( '|', $entry ); $host = $fields[0]; if ( count( $fields ) < 3 || 1 !== preg_match( '/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/', $host ) ) { continue; } if ( ! isset( $categories[ $fields[1] ], $kinds[ $fields[2] ] ) ) { continue; } $rules[ $host ] = array( 'category' => $categories[ $fields[1] ], 'kind' => $kinds[ $fields[2] ], ); } return $rules; } /** The site's rule for an address (its host or a parent of it, the most specific first), or null. */ function saasaudit_consent_site_rule_for( $address ) { $url = saasaudit_consent_split_url( $address ); if ( null === $url ) { return null; } $rules = saasaudit_consent_site_rules(); if ( empty( $rules ) ) { return null; } $labels = explode( '.', $url['host'] ); for ( $start = 0; $start <= count( $labels ) - 2; $start++ ) { $candidate = implode( '.', array_slice( $labels, $start ) ); if ( isset( $rules[ $candidate ] ) ) { return $rules[ $candidate ]; } } return null; } /** The service rule (embedded video, map, fonts) an address belongs to, or null. */ function saasaudit_consent_service_for( $address, $kind ) { $url = saasaudit_consent_split_url( $address ); if ( null === $url ) { return null; } if ( 'frame' === $kind ) { $own = saasaudit_consent_site_rule_for( $address ); if ( null !== $own && 'frame' === $own['kind'] ) { return array( 'slug' => 'custom', 'kind' => 'frame' ); } } foreach ( SAASAUDIT_SERVICES as $service ) { if ( $service['kind'] !== $kind || ! in_array( $url['host'], $service['hosts'], true ) ) { continue; } if ( empty( $service['paths'] ) && empty( $service['contains'] ) ) { return $service; } foreach ( $service['paths'] as $prefix ) { if ( 0 === strpos( $url['path'], $prefix ) ) { return $service; } } foreach ( $service['contains'] as $part ) { if ( false !== strpos( $url['path'], $part ) ) { return $service; } } } return null; } /** Whether an address matches a catalog entry: "host" or "host/path-prefix", the host or a subdomain of it. */ function saasaudit_consent_entry_matches( $entry, $url ) { $slash = strpos( $entry, '/' ); $host = false === $slash ? $entry : substr( $entry, 0, $slash ); $path = false === $slash ? '' : substr( $entry, $slash ); $match = $url['host'] === $host || ( strlen( $url['host'] ) > strlen( $host ) && substr( $url['host'], -strlen( $host ) - 1 ) === '.' . $host ); return $match && 0 === strpos( $url['path'], $path ); } /** A service's script widget (a tweet's widgets.js) an address is, or null: held under the service's category and tied to it. */ function saasaudit_consent_widget_for( $url ) { foreach ( SAASAUDIT_WIDGETS as $widget ) { if ( '' !== $widget['contains'] && false === strpos( $url['path'], $widget['contains'] ) ) { continue; } foreach ( $widget['scripts'] as $entry ) { if ( saasaudit_consent_entry_matches( $entry, $url ) ) { return array( 'name' => $widget['name'], 'category' => $widget['category'], 'service' => $widget['service'], ); } } } return null; } /** What a held script leaves for the inline scripts after it: its service, category and the text those scripts use from it, or null when none is used inline (only a service widget has any). */ function saasaudit_consent_inline_owner( $opening ) { $service = saasaudit_consent_attribute( $opening, 'data-consent-service' ); $category = saasaudit_consent_attribute( $opening, 'data-consent-category' ); if ( null === $service || null === $category ) { return null; } foreach ( SAASAUDIT_WIDGETS as $widget ) { if ( $widget['service'] === $service && '' !== $widget['inline'] ) { return array( 'service' => $service, 'category' => $category, 'needle' => $widget['inline'], ); } } return null; } /** An inline script that uses a held script's code, held with that script's service in the order the page wrote it (as the consent script does for a page it reads in the browser); null when it is not one. */ function saasaudit_consent_hold_inline( $opening, $body, $held ) { if ( null !== saasaudit_consent_attribute( $opening, 'src' ) ) { return null; } $type = strtolower( trim( (string) saasaudit_consent_attribute( $opening, 'type' ) ) ); $module = 'module' === $type; if ( ! $module && '' !== $type && ! in_array( $type, SAASAUDIT_CLASSIC_SCRIPT_TYPES, true ) ) { return null; } foreach ( $held as $owner ) { if ( false !== strpos( $body, $owner['needle'] ) ) { $opening = preg_replace( '/\stype\s*=\s*(?:"[^"]*"|\'[^\']*\'|[^\s>]+)/i', '', $opening, 1 ); $marks = '