Verified Commit 56d18e99 authored by Jakob Moser's avatar Jakob Moser
Browse files

Remember consent

parent 5401f47f
Loading
Loading
Loading
Loading
+59 −1
Original line number Diff line number Diff line
@@ -75,11 +75,36 @@ function fscoli_regex_match_group($regex, $str, $group) {
    }
}

/**
 * Helper function: Check if the fscoli consent cookie already contains consent by the user for this particular domain.
 */
function fscoli_is_consent_already_given($provider_url) {
    if(!array_key_exists("fscoli_consent_given_for_urls", $_COOKIE)) {
        // No consent cookie is even set
        return false;
    }

    $urls = json_decode(stripslashes($_COOKIE["fscoli_consent_given_for_urls"]));

    if(!is_array($urls)) {
        // The JSON was invalid somehow.
        return false;
    }

    return in_array($provider_url, $urls);
}

/**
 * Helper function: Builds a consent prompt (HTML and JS) using the given parameters, that already have to be extracted
 * from somewhere.
 */
function fscoli_get_consent_prompt($embed_html, $width_attr, $height_attr, $content_name, $provider_url, $provider_domain, $privacy_policy_url) {
    if(fscoli_is_consent_already_given($provider_url)) {
        // If the cookies indicate the user has already given consent to load data from the particular provider,
        // just return the HTML to embed the data unchanged.
        return $embed_html;
    }

    $embed_html = esc_html($embed_html);
    $width_attr = esc_attr($width_attr);
    $height_attr = esc_attr($height_attr);
@@ -110,13 +135,46 @@ function fscoli_get_consent_prompt($embed_html, $width_attr, $height_attr, $cont

        <section class="privacy-consent-prompt">
            <button onclick="givePrivacyConsent(this.parentElement.parentElement)">$content_name anzeigen</button>
            <p>
                <label><input type="checkbox" class="privacy-consent-remember">Entscheidung merken</label>
            </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>

    <script>
        function givePrivacyConsent(consentPromptArea) {
        function tryParseAsArray(maybeJson) {
            try {
                const result = JSON.parse(maybeJson);

                if(Array.isArray(result)) {
                    return result;
                } else {
                    throw new TypeError("Expected an array, but was something else");
                }
            } catch {
                return [];
            }
        }

        async function givePrivacyConsent(consentPromptArea) {
            consentPromptArea.outerHTML = consentPromptArea.querySelector(".unloaded-content").textContent
            
            if(consentPromptArea.querySelector(".privacy-consent-remember").checked) {
                // Store a cookie to remember consent
                const consentCookie = await cookieStore.get("fscoli_consent_given_for_urls");

                const urls = tryParseAsArray(consentCookie?.value);
                urls.push("$provider_url");

                const newConsentCookie = {
                    name: "fscoli_consent_given_for_urls",
                    expires: Date.now() + 365 * 24 * 60 * 60 * 1000, // in a year
                    value: JSON.stringify(urls),
                }

                await cookieStore.set(newConsentCookie);
            }
        }
    </script>
    HTML;
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
 * Author: personads :: Maximilian Müller-Eberstein (ursprüngliche Version und Design); Jakob Moser
 * Author URI: https://personads.me/
 *
 * Version: 2.15.0
 * Version: 2.16.0
 * Requires PHP: 8.0
 *
 * License: GNU General Public License v3.0