Subversion Repositories web.creative

Rev

Blame | Last modification | View Log | Download

<?php namespace ProcessWire;

/**
 * PrivacyWire
 * This module adds management options for GDPR-relevant elements (loading maps, videos etc. only after accepting
 * external media) and cookies.
 *
 * @author blaueQuelle
 *
 * ProcessWire 3.x
 * Copyright (C) 2011 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */
class PrivacyWire extends WireData implements Module, ConfigurableModule
{

    public static function getModuleInfo()
    {
        return [
            'title' => "PrivacyWire",
            'summary' => "This module adds management options for GDPR-relevant elements (loading maps, videos etc. only after accepting external media) and cookies.",
            'author' => "blaueQuelle",
            'href' => "https://github.com/blaueQuelle/privacywire",
            'version' => 38,
            'autoload' => true,
            'singular' => true,
            'requires' => ["PHP>=7.2", "ProcessWire>=3.0.110"],
            'installs' => ["TextformatterPrivacyWire"],
            'icon' => 'eye-slash'
        ];
    }

    public function ready()
    {
        if (
            $this->wire('page')->template == 'admin' || // exclude admin pages
            $this->wire('page')->template == 'form-builder' // exclude from form-builder iframe
        ) {
            return;
        }

        if (!$this->render_manually) {
            $this->addHookAfter('Page::render', $this, 'render');
        }
    }

    /**
     * checks for a javascript file alternate path
     * @return string   the path of the JS file
     **/
    public function ___getJsFile()
    {
        return ($this->add_basic_css_styling) ? $this->wire('config')->urls->$this . "js/PrivacyWire.js" : $this->wire('config')->urls->$this . "js/PrivacyWireUnstyled.js";
    }

    /**
     * look for current module config and put this into an json array
     * @return string   json object of inline settings
     **/
    public function ___getInlineJs()
    {
        // Multi Language Support
        if ($this->wire('languages')) {
            $userLanguage = $this->wire('user')->language;
            $lang = $userLanguage->isDefault() ? '' : "__$userLanguage->id";
        } else {
            $lang = '';
        }

        $privacyWireSettings = new \StdClass;
        $privacyWireSettings->version = $this->version;
        $privacyWireSettings->dnt = ($this->respectDNT) ? "1" : "0";
        $privacyWireSettings->customFunction = (!empty($this->trigger_custom_js_function)) ? $this->wire('sanitizer')->text($this->trigger_custom_js_function) : "";
        $privacyWireSettings->messageTimeout = ($this->messageTimeout && intval($this->messageTimeout) > 1) ? intval($this->messageTimeout) : 1500;
        $privacyWireSettings->cookieGroups = [
            'necessary' => $this->get("cookies_necessary_label$lang|cookies_necessary_label"),
            'functional' => $this->get("cookies_functional_label$lang|cookies_functional_label"),
            'statistics' => $this->get("cookies_statistics_label$lang|cookies_statistics_label"),
            'marketing' => $this->get("cookies_marketing_label$lang|cookies_marketing_label"),
            'external_media' => $this->get("cookies_external_media_label$lang|cookies_external_media_label"),
        ];
        return json_encode($privacyWireSettings);
    }

    /**
     * checks for a alternate banner template path
     * @return string   the path of the banner template file
     **/
    public function ___getBannerTemplateFile()
    {
        $sanitizedAlternateBannerPath = (substr($this->alternate_banner_template, 0, 1) !== "/") ? $this->alternate_banner_template : substr($this->alternate_banner_template, 1);
        return (!empty($this->alternate_banner_template) && file_exists($this->wire('config')->paths->root . $sanitizedAlternateBannerPath)) ? $this->wire('config')->paths->root . $sanitizedAlternateBannerPath : $this->wire('config')->paths->$this . 'PrivacyWireBanner.php';
    }

    public function ___render(HookEvent $event)
    {
        $headContent = $this->renderHeadContent();
        $event->return = str_replace("</head>", "{$headContent}</head>", $event->return);

        $bodyContent = $this->renderBodyContent();
        $event->return = str_replace("</body>", "{$bodyContent}</body>", $event->return);
    }

    public function renderHeadContent()
    {
        $headContent = "<script>var PrivacyWireSettings={$this->getInlineJs()};</script>";
        $headContent .= ($this->use_procache_minification && $this->wire('modules')->isInstalled('ProCache') && $this->wire('modules')->get('ProCache')) ? "<script defer src='{$this->wire('modules')->get('ProCache')->js($this->getJsFile())}'></script>" : "<script defer src='{$this->getJsFile()}'></script>";
        return $headContent;
    }

    public function renderBodyContent()
    {
        return wireRenderFile($this->getBannerTemplateFile(), ['module' => $this]);
    }

    public function ___install()
    {
    }

    public function ___uninstall()
    {
    }

}