Commit f7501823 authored by Jakob Moser's avatar Jakob Moser
Browse files

Implement regex data gathering magic

parent 024756cc
Loading
Loading
Loading
Loading
+48 −7
Changes for functions.php: 48 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -55,17 +55,58 @@ function fscoli_embed_umap($regex_matches, $embed_attrs, $url, $raw_embed_attrs)
}
wp_embed_register_handler('umap', '#^https://umap.openstreetmap.de/[a-z]{2}/map/[^/]*#', 'fscoli_embed_umap');

/**
 * Helper function: Matches the given regex against the given string and
 * returns the $group-th match group of the match. Returns null if there is
 * no such group or there was no match.
 */
function fscoli_regex_match_group($regex, $str, $group) {
    preg_match($regex, $str, $matches);
    /* 
     * Q: Wait, where did $matches suddenly come from?
     * A: We passed it by reference (https://www.php.net/manual/en/language.references.pass.php). This means
     * we told the function it should store its results in a variable we can later access as $matches.
     * Yes, that's a thing in some programming languages.
     */ 
    if(array_key_exists($group, $matches)) {
        return $matches[$group];
    } else {
        return null;
    }
}

/*
 * Add privacy protection for embed elements: You have to click a button of a consent prompt
 * before the content is loaded.
 */
function fscoli_show_embed_privacy_consent_prompt($embed_html) {
    // Extract this from the embed_html string with regular expressions and heuristics
    $width              = null;  // TODO
    $height             = null;  // TODO
    $provider_url       = null;  // TODO
    $provider_domain    = null;  // TODO
    $privacy_policy_url = null;  // TODO
    // Try to guess width and height of the consent prompt to match the dimensions of the embedded content
    $width              = fscoli_regex_match_group('/width ?(:|= ?["\']) ?(\d+ ?px)/', $embed_html, 2);
    $height             = fscoli_regex_match_group('/height ?(:|= ?["\']) ?(\d+ ?px)/', $embed_html, 2);

    if($width === null) {
        $width = "auto";
    }
    if($height === null) {
        $height = "auto";
    }

    $provider_url       = fscoli_regex_match_group('/src ?= ?["\'](https?://(.*?))/.*?["\']/', $embed_html, 1);
    $provider_domain    = fscoli_regex_match_group('/src ?= ?["\'](https?://(.*?))/.*?["\']/', $embed_html, 2);

    $privacy_policy_url = null;
    if($provider_domain === "umap.openstreetmap.de") {
        $privacy_policy_url = "https://www.fossgis.de/datenschutzerkl%C3%A4rung";
    }

    if($privacy_policy_url !== null) {
        $privacy_policy_link = <<<HTML
        <a href="${esc_url(privacy_policy_url)}">dort verlinkte Datenschutzerklärung</a>
        HTML;
    } else {
        $privacy_policy_link = "dort verlinkte Datenschutzerklärung";
    }


    /* 
     * We use the heredoc syntax (https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)
@@ -81,7 +122,7 @@ function fscoli_show_embed_privacy_consent_prompt($embed_html) {

        <section class="privacy-consent-prompt">
            <button onclick="givePrivacyConsent(this.parentElement)">Inhalt anzeigen</button>
            <p>Dabei werden Daten an <a href="${esc_url(provider_url)}">${esc_html(provider_domain)}</a> (und ggfs. weitere Anbieter) übertragen. Es gilt die <a href="${esc_url(privacy_policy_url)}">dort verlinkte Datenschutzerklärung</a>.</p>
            <p>Dabei werden Daten an <a href="${esc_url(provider_url)}">${esc_html(provider_domain)}</a> (und ggfs. weitere Anbieter) übertragen. Es gilt die $privacy_policy_link.</p>
        </section>
    </div>