Your IP : 216.73.216.104


Current Path : /home/poneycluc/www/plugins/system/advanced_cookies_system/
Upload File :
Current File : /home/poneycluc/www/plugins/system/advanced_cookies_system/advanced_cookies_system.php

<?php
/**
 * @package Advanced Cookies
 * @copyright Copyright (C) 2019 HOB-France.com, All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author url: https://www.hob-france.com/
 * @author email info@hob-fr.com
 * @developer Mihaly Varga - michel.varga@hob-fr.com
 */
// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;

jimport('joomla.plugin.plugin');
require_once __DIR__ . '/helper.php';


class plgSystemadvanced_cookies_system extends JPlugin
{
    
    private $_debugs;

    /*
     * The plugin can block the services in all page.
     * In the Modules, in the plugins and in the components also.
     * The goal block the services externe which could use cookies.
     * */

    public function onBeforeCompileHead() {

    }
    public function onBeforeRender()
    {
        // because JAT3 is lame!
        $this->onBeforeCompileHead();
    }
    public function onAfterRender()
    {
        $this->_debugs = array();
        $app = Factory::getApplication();
        if ($app->getName() != 'site') {
            return true;
        }
    
//$app->setBody("DEBUG1");
//return true;
        
        /*
         * Check the parameters in the module.
         * */
        if ($this->getmodparams() === false) {
            return false;
        } else {
            $this->modparams = $this->getmodparams();

            $google_analytics = $this->modparams->get('service_google_analytics');
            $google_ads = $this->modparams->get('service_google_ads');
            $google_maps = $this->modparams->get('service_google_maps');
            $calemeo = $this->modparams->get('service_calameo');
            $reCaptcha_v2 = $this->modparams->get('service_recaptcha_v2');
            $reCaptcha_v3 = $this->modparams->get('service_recaptcha_v3');
            $mailChimp = $this->modparams->get('service_mailchimp');
            $facebook = $this->modparams->get('service_facebook');
            $twitter = $this->modparams->get('service_twitter');
            $google_plus = $this->modparams->get('service_google_plus');
            $linkedin = $this->modparams->get('service_linkedin');
            $pinterest = $this->modparams->get('service_pinterest');
            $instagram = $this->modparams->get('service_instagram');
            $youtube = $this->modparams->get('service_youtube');
            $vimeo = $this->modparams->get('service_vimeo');
            $dailymotion = $this->modparams->get('service_dailymotion');

            $buffer = JFactory::getApplication()->getBody();

            $jInput = JFactory::getApplication()->input;
            $cookieHob = $jInput->cookie->get("hob_cookie", "", "RAW");  
            $this->_addDebugEntry("HOB-cookie, cookie : ".$cookieHob);            
            //GW: if ($this->modparams && isset(..._COOKIE['hob_cookie'])) {
            if ($this->modparams && isset($cookieHob)) {

                /*
                 * Block the service URLs.
                 * The list of  the urls in the helper.php             *
                 * */

                /*
               * CATEGORIE 2
               * */
                if ($google_analytics == 1 && $this->checkhobcookie('hob_cookie', 1) == 'N') {
                    $buffer = $this->replace_iframe_src('google.com', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::google_analytics_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($google_ads == 1 && $this->checkhobcookie('hob_cookie', 1) == 'N') {
                    $buffer = $this->replace_iframe_src('google.com', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::google_ads_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($this->checkhobcookie('hob_cookie', 1) == 'N') {
                    $target_google = array("_ga","_gid","_gat","_gac","__utma","__utmt","__utmb","__utmc","__utmz","__utmv","__utmx","__utmxx","_gaexp");
                    self::google_analytics_cookie($target_google);
                }

                /*
                * CATEGORIE 3
                * */
                if ($google_maps == 1 && $this->checkhobcookie('hob_cookie', 2) == 'N') {
                    $buffer = $this->replace_iframe_src('google.com.maps', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::google_maps_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($calemeo == 1 && $this->checkhobcookie('hob_cookie', 2) == 'N') {
                    $buffer = $this->replace_iframe_src('calameo', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::calameo_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($reCaptcha_v2 == 1 && $this->checkhobcookie('hob_cookie', 2) == 'N') {
                    $buffer = $this->replace_iframe_src('recaptcha', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::recaptcha_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($reCaptcha_v3 == 1 && $this->checkhobcookie('hob_cookie', 2) == 'N') {
                    $buffer = $this->replace_iframe_src('recaptcha', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::recaptcha_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($mailChimp == 1 && $this->checkhobcookie('hob_cookie', 2) == 'N') {
                    $buffer = $this->replace_iframe_src('mailchimp', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::mailchimp_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                if ($this->checkhobcookie('hob_cookie', 2) == 'N') {
                    self::killallcookies();
                }

                /*
                * CATEGORIE 4
                * */
                //Facebook
                if ($facebook == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('facebook', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::facebook_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Google Plus
                if ($google_plus == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('plus.google', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::google_plus_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Linkedin
                if ($linkedin == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('linkedin', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::linkedin_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //TWitter
                if ($twitter == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('twitter', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::twitter_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Instagram
                if ($instagram == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('instagram', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::instagram_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Pintrest
                if ($pinterest == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('pinterest', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::pinterest_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Youtube
                $this->_addDebugEntry("HOB-cookie, youtube : ".$youtube);            
                $this->_addDebugEntry("HOB-cookie, check cookie #3 : ".$this->checkhobcookie('hob_cookie', 3));  
                if ($youtube == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $this->_addDebugEntry("HOB-cookie, youtube to block!");            
                    $buffer = $this->replace_iframe_src('youtube', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::youtube_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Vimeo
                if ($vimeo == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('vimeo', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::vimeo_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                //Dailymotion
                if ($dailymotion == 1 && $this->checkhobcookie('hob_cookie', 3) == 'N') {
                    $buffer = $this->replace_iframe_src('dailymotion', 'data-', $buffer);
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::dailymotion_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }
                /*
                * CATEGORIE 5
                * */
                //Other
                if ($this->checkhobcookie('hob_cookie', 4) == 'N') {
                    $buffer = $this->cookie_blocker(hobrgpdsystemHelper::other_service_address(), 'NotAllowedScript'.uniqid(), $buffer);
                }

            }
            JFactory::getApplication()->setBody($buffer);

            $this->_showDebugs();
            
            return true;
        }
    }
    //Check the hob_cookie COOKIE and return N OR Y
    public function checkhobcookie($cookie_name, $cat_number){
        $jInput = JFactory::getApplication()->input;
        $cookieHob = $jInput->cookie->get($cookie_name, "", "RAW");            
        $this->_addDebugEntry("HOB-cookie, checkhobcookie : ".$cookieHob);

        $result = 'N';            

        //GW: if(isset(..._COOKIE[$cookie_name])) {
        if(isset($cookieHob)) {

            $resultats = explode(' ', $cookieHob);

            if(isset($resultats[$cat_number])) {
                $result = substr($resultats[$cat_number],-1);
            }
        }
        return $result;
    }

    //Put string before src attribute in a specific iframe.
    public function replace_iframe_src( $source,$replace,$input ) {
        $regExpPrefix = '/.*?iframe.*?\K(?=src=".*?(?:';        
        $regExpSuffix = ').*?".*)/i';        
        $regExpSource = $regExpPrefix.$source.$regExpSuffix;
        // GDPR blocking service cookies as asked by the user
        $input = preg_replace($regExpSource, $replace, $input);
        return $input;

    }
    //Put string before src attribute in a specific iframe.
    public function replace_iframe( $source,$replace,$input ) {
        $regExpSource = '/.*?iframe.*?src=".*?(?:'.$source.').*?".*/i';
        $input = preg_replace($regExpSource, $replace, $input);
        return $input;
    }

    public function cookie_blocker($helper_function, $replace, $input){
        // Parcourir la liste de paires clé-valeur dans $helper_function
        foreach ($helper_function as $cookies => $cookie) {
            // Construire une expression régulière pour rechercher la balise <script> avec l'attribut "src" contenant le nom du cookie
            $regex = '/<script\s[^>]*\bsrc\s*=\s*["\'][^"\']*(' . trim(str_replace('.', '\.', $cookie)) . ')[^"\']*["\'][^>]*>/i';
            
            // Rechercher toutes les correspondances de l'expression régulière dans $input
            preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
            
            // Si des correspondances ont été trouvées
            if (!empty($matches)) {
                // Parcourir toutes les correspondances
                foreach ($matches as $matche) {
                    // Si la correspondance n'est pas vide
                    if (!empty($matche)) {
                        // Construire une chaîne de remplacement avec le nom du cookie modifié
                        $replace_input = str_replace($cookie, $replace . $cookie, $matche[0]);
                        
                        // Remplacer la correspondance actuelle par la chaîne de remplacement dans $input
                        $input = str_replace($matche[0], $replace_input, $input);
                    }
                }
            }
        }
        // Retourner la version modifiée de $input
        return $input;
    }


    public function getmodparams()
    {
        jimport('joomla.application.module.helper');
        if(JModuleHelper::isEnabled('mod_advanced_cookies'))
        {
            $module = JModuleHelper::getModule('mod_advanced_cookies');
            $modparams = new JRegistry;
            $modparams->loadString($module->params);
            return $modparams;
        }
        else
        {
            return false;
        }
    }

    public function killallcookies(){
        if (isset($_SERVER['HTTP_COOKIE'])) {
            $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
            foreach($cookies as $cookie) {
                $parts = explode('=', $cookie);
                $name = trim($parts[0]);

                $exceptions = $this->getExceptions();

                //don't kill the hob_cookie and jquery cookie
                if(!in_array($name,$exceptions)) {
                    setcookie($name, '', time() - 3600);
                }
            }
        }
    }

    public function getExceptions(){
        $exceptions = array('hob_cookie','__cfduid');
        
        $user = JFactory::getUser();

            $exceptions[] = 'joomla_user_state';

            $db = JFactory::getDbo();
            $query = $db->getQuery(true)
                    ->select($db->quoteName('session_id'))
                    ->from($db->quoteName('#__session'));
            $db->setQuery($query);
            $ids_session = $db->loadColumn();

            foreach ($ids_session as $id_session) {
                $key_session = array_search($id_session,$_COOKIE);

                if($key_session){
                    $exceptions[] = $key_session;
                }
            }

        return $exceptions;
    }

    public function google_analytics_cookie($target_array){
        if (isset($_SERVER['HTTP_COOKIE'])) {
            foreach($target_array as $cookie) {
                $parts = explode('=', $cookie);
                $name = trim($parts[0]);
                setcookie($name, '', time() - 3600);
            }
        }
    }
    
    private function _addDebugEntry($msg) {
        $this->_debugs[] = $msg;
    }
    
    private function _showDebugs() {
        $buffer = Factory::getApplication()->getBody();
        $traceDebug = implode("<br/>", $this->_debugs);
        //DEBUG : 
        //Factory::getApplication()->setBody($buffer."DEBUG:<br/>".$traceDebug."<hr/>");
    }    
}