Commit 65be26a1 authored by Jakob Moser's avatar Jakob Moser
Browse files

Fix bugs, re-structure code, hope everything works now

parent a8020af1
Loading
Loading
Loading
Loading
+39 −29
Original line number Diff line number Diff line
@@ -75,54 +75,41 @@ function fscoli_regex_match_group($regex, $str, $group) {
    }
}

/*
 * Add privacy protection for embed elements: You have to click a button of a consent prompt
 * before the content is loaded.
/**
 * Helper function: Builds a consent prompt (HTML and JS) using the given parameters, that already have to be extracted
 * from somewhere.
 */
function fscoli_show_embed_privacy_consent_prompt($embed_html) {
    // 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";
    }
function fscoli_get_consent_prompt($embed_html, $width_attr, $height_attr, $provider_url, $provider_domain, $privacy_policy_url) {
    $embed_html = esc_html($embed_html);
    $width_attr = esc_attr($width_attr);
    $height_attr = esc_attr($height_attr);
    $provider_url = esc_url($provider_url);
    $provider_domain = esc_html($provider_domain);
    $privacy_policy_url = esc_url($privacy_policy_url);

    if($privacy_policy_url !== null) {
        $privacy_policy_link = <<<HTML
        <a href="${esc_url(privacy_policy_url)}">dort verlinkte Datenschutzerklärung</a>
        <a href="$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)
     * to embed a multiline string in this document. Heredocs can be delimited by any string - we chose HTML, but EOT and END
     * are more commonly used options. Funny enough, when we use HTML to delimit the heredoc, at least Visual Studio Code
     * applies HTML syntax highlighting to the string...
     */ 
    $privacy_consent_prompt = <<<HTML
    <div class="privacy-consent-prompt-area" style="width: ${esc_attr(width)}; height: ${esc_attr(height)};">
    return <<<HTML
    <div class="privacy-consent-prompt-area" style="width: $width_attr; height: $height_attr;">
        <div class="unloaded-content">
            ${esc_html($embed_html)}
            $embed_html
        </div>

        <section class="privacy-consent-prompt">
            <button onclick="givePrivacyConsent(this.parentElement.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 $privacy_policy_link.</p>
            <p>Dabei werden Daten an <a href="$provider_url">$provider_domain</a> (und ggfs. weitere Anbieter) übertragen. Es gilt die $privacy_policy_link.</p>
        </section>
    </div>

@@ -132,8 +119,31 @@ function fscoli_show_embed_privacy_consent_prompt($embed_html) {
        }
    </script>
    HTML;
}

/*
 * 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) {
    // Try to guess width and height of the consent prompt to match the dimensions of the embedded content
    // We try to extract them from CSS properties (e.g. width: 150px) or HTML attributes (width="150px").
    $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"; }

    // Try to guess the URL of the content provider, its domain and, using a hardcoded list of policies,
    // its privacy policy.
    $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";
    }

    return $privacy_consent_prompt;   
    return fscoli_get_consent_prompt($embed_html, $width, $height, $provider_url, $provider_domain, $privacy_policy_url);   
}

add_filter('embed_handler_html', 'fscoli_show_embed_privacy_consent_prompt');