AnonSec Team
Server IP : 10.106.20.4  /  Your IP : 216.73.216.140
Web Server : Apache
System : Linux webm004.cluster106.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64
User : sylvaineey ( 605664)
PHP Version : 7.4.33
Disable Function : _dyuweyrj4,_dyuweyrj4r,dl
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/sylvaineey/www/wp-content/plugins/tlmq-consents/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/sylvaineey/www/wp-content/plugins/tlmq-consents/tlmq-consents.php
<?php

/**
 * @package tlmq-consent
 */
/*
  Plugin Name: tlmq-consent
  Plugin URI: http://www.telemaque.fr
  Description: get consents
  Version: 1.0.0
  Author: telemaque
  Author URI: http://www.telemaque.fr/
 */

require_once dirname( __FILE__ ) . '/config/config.php';

class Consents {

    private $consentData;
    public $html;
    public $consents;

    public function getConfig() {
        $config = consentConfig($_SERVER['HTTP_HOST']);
        return array (
            'website' => $config['site'],
            'token' => $config['token'],
            'api_tlmq_token' => $config['api_tlmq_token'],
            'get_url' => $config['get_url'],
            'post_url' => $config['post_url'],
            'website_id' => $config['website_id'],
            'consent_company' => $config['consent_company'],
        );
    }

    public function getAllConsents($formId) {
        $this->html = '';
        $this->consents = $this->getDMPConsents($formId);
        $this->makeConsentHtml();
        $this->html .= '<input type="hidden" name="path" value="' . $_SERVER["REQUEST_URI"] . '">';
        $this->html .= '<input type="hidden" name="formId" value="' . $formId . '">';
        return '<div class="consentDiv">'.$this->html.'</div>';
    }

    public function getDMPConsents($formId) {
        $config = $this->getConfig();
        $consentData = $this->curl($formId);
        return $consentData;
    }

    public function curl($formId, $post = false, $data = array())
    {
        $config = $this->getConfig();
        $ch = curl_init();
        $headers = [
            'Content-type: application/json; charset=UTF-8',
            'apikey: ' . $config['api_tlmq_token']
        ];
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_URL, $config['post_url']);
        } else {
            $url = $config['get_url'] . $formId;
            curl_setopt($ch, CURLOPT_URL, sprintf($url, $formId));
        }
        $output = curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curl_errno = curl_errno($ch);
        curl_close($ch);

        return ($post) ? true : ( ($curl_errno != 0 || !$output || strlen(trim($output)) == 0 || $httpCode != 200) ? json_decode(file_get_contents(__DIR__ . "/consents-defaults/$formId.json"), true) : json_decode($output, true) );

    }

    public function makeConsentHtml()
    {
        if (isset($this->consents['consents'])) {
            $this->html .= '<p>' . $this->consents['ableToContractText'] . '</p>';
        }

        if (isset($this->consents['consents'])) {
            foreach ($this->consents['consents'] as $consent) {
                foreach ($consent['linksProperties'] as $linkProperty) {
                    $consent['displayedText'] = $this->linkReplace($linkProperty, $consent['displayedText']);
                }
                $this->addCheckbox($consent['name'], $consent['displayedText'], $consent['required']);
            }
        }

        if (preg_match_all('/%.*?%/', $this->consents['dataTreatmentText'], $matches) > 0) {
            foreach ($matches[0] as $match) {
                $matchName = explode('%', $match);
                $matchName = $matchName[1];
                $linksProperty = array_filter($this->consents['linksProperties'], function($arr) use ($matchName){
                    return $arr['name'] == $matchName;
                });
                if (!empty($linksProperty)) {
                    $this->consents['dataTreatmentText'] = $this->linkReplace(array_shift($linksProperty), $this->consents['dataTreatmentText']);
                }
            }
        }

        $this->html .= '<p class="dataTreatmentText">'.$this->consents['dataTreatmentText'].'</p>';

        if (preg_match_all('/%.*?%/', $this->consents['dataTreatmentDetailsText'], $matches) > 0) {
            foreach ($matches[0] as $match) {
                $matchName = explode('%', $match);
                $matchName = $matchName[1];
                $linksProperty = array_filter($this->consents['linksProperties'], function($arr) use ($matchName){
                    return $arr['name'] == $matchName;
                });
                if (!empty($linksProperty)) {
                    $this->consents['dataTreatmentDetailsText'] = $this->linkReplace(array_shift($linksProperty), $this->consents['dataTreatmentDetailsText']);
                }
            }
        }

        if (isset($this->consents['dataTreatmentDetailsText'])) {
            $this->html .= '<p>'.$this->consents['dataTreatmentDetailsText'].'</p>';
        }

        return $this->html;
    }

    public function linkReplace($linkProperty, $consent) {
        return str_replace('%' . $linkProperty['name'] . '%', "<a style='color: inherit; text-decoration: underline;' title='" . $linkProperty['text'] . "' target='" . $linkProperty['target'] . "' href='" . $linkProperty['link'] . "' rel ='noopener noreferrer' > " . $linkProperty['text'] . "</a>", $consent);
    }

    public function addCheckbox($name, $text, $required = 'false')
    {
        $required = ($required) ? "required='$required'" : "";
        $this->html .= "<div class='checkbox'>
                            <label class='required'>
                                <input type='checkbox' id='$name' name='$name' $required>
                                 $text
                            </label>
                        </div>";
    }

    // POST
    public function sendConsents($userData, $post)
    {
        $config = $this->getConfig();

        $formId = 1;
        $this->consents = $this->getDMPConsents($formId);
        $filteredConsents = array();
        foreach ($post as $key => $val) {
            if (strpos($key, '_AGREED') !== false ) {
                $filteredConsents[$key] = $val;
            }
        }
        $agreedConsents = array_filter($filteredConsents, function($consent) {
            return $consent == 'on';
        });

        $finalConsents = (array) $agreedConsents;
        foreach ($agreedConsents as $key => $val) {
            $consents = array_filter($this->consents['consents'], function($consent) use ($key) {
                return $consent['type'] == $key;
            });
            foreach ($consents as $consent) {
                foreach ($consent['implicitConsents'] as $implicitConsent) {
                    $finalConsents[$implicitConsent] = 'on';
                }
            }
        }
        if (count($agreedConsents) > 0) {
            $data = [
                'contactId' => 1,
                'consents' => array_map(function($consent, $key) {
                    return [
                        'type' => $key,
                        'agreed' => true,
                    ];
                }, $finalConsents, array_keys($finalConsents)),
                'path' => $_SERVER['REQUEST_URI'],
                'emailAddress' => $userData['email'],
                'phoneNumber' => $this->toInternationalPhoneNumber($userData['phone_number']),
                'consentObtainedBy' => $config['consent_company'],
                'websiteId' => $config['website_id'],
            ];

            $this->curl($formId, true, $data);
        }
    }


    public function toInternationalPhoneNumber($phoneNumber)
    {
        if (empty($phoneNumber)) {
            return '';
        }
        $toRemove = array("-", " ", "(", ")");
        $phoneNumber = str_replace($toRemove, "", $phoneNumber);
        if (strpos($phoneNumber, '00') === 0) {
            $phoneNumber = '+' . substr($phoneNumber, 2);
        }
        if (!empty($phoneNumber) && strpos($phoneNumber, '+') !== 0) {
            $phoneNumber = '+33' . substr($phoneNumber, 1);
        }
        return $phoneNumber;
    }

    public function formatConsents($consents)
    {
        $config = $this->getConfig();
        $data = [];

        $formId = 1;
        $this->consents = $this->getDMPConsents($formId);
        $filteredConsents = array();
        foreach ($consents as $key => $val) {
            if (strpos($key, '_AGREED') !== false ) {
                $filteredConsents[$key] = $val;
            }
        }
        $agreedConsents = array_filter($filteredConsents, function($consent) {
            return $consent == 'on';
        });

        $finalConsents = (array) $agreedConsents;

        foreach ($agreedConsents as $key => $val) {
            $consents = array_filter($this->consents['consents'], function($consent) use ($key) {
                return $consent['type'] == $key;
            });
            foreach ($consents as $consent) {
                foreach ($consent['implicitConsents'] as $implicitConsent) {
                    $finalConsents[$implicitConsent] = 'on';
                }
            }
        }


        if (count($agreedConsents) > 0) {
            $data = [
                'consents' => array_map(function($consent, $key) {
                    return [
                        'type' => $key,
                        'value' => true,
                    ];
                }, $finalConsents, array_keys($finalConsents)),
            ];
        }

        return $data;
    }

    public function sendProspectApiMarket($prospectData)
    {
        $config = consentConfig($_SERVER['HTTP_HOST']);

        $data = [
            'email' => $config['api_market_email'],
            'password' => $config['api_market_password'],
        ];

        $token = json_decode($this->http_post($config['api_market_url'] . $config['login_route'], $data, false, 'login'));

        $partner = $prospectData['partner'] ? $prospectData['partner'] : 'default';

        $urlCampaign = $config['api_market_url'] . $config['campaign_route'] . $partner . '?website_id=' . $config['api_market_id'] . '&country_code=fr';

        $response = $this->http_post($urlCampaign, false, $token->token, 'campaign');
        $campaignJson = json_decode($response);

        $consents = $prospectData['consents'] ? $this->formatConsents($prospectData['consents']) : null;


        $params = array(
            'firstname' => $prospectData['firstname'] ? $prospectData['firstname'] : null,
            'email' => $prospectData['email'] ? $prospectData['email'] : null,
            'birthdate' => $prospectData['birthdate'] ? $prospectData['birthdate'] : null,
            'phone_number' => $prospectData['phone_number'] ? $this->toInternationalPhoneNumber($prospectData['phone_number']) : null,
            'campaign_id' => $campaignJson->campaign->id,
            'editor_id' => $prospectData['editor_id'] ? $prospectData['editor_id'] : null,
            'type' => $prospectData['type'],
            'question' => $prospectData['question'] ? $prospectData['question'] : null,
            'consents' => $consents['consents'],
            'pathname' => $prospectData['pathname'] ? $prospectData['pathname'] : '/',
            'course' => 'cap',
            'oriannaQG' => $prospectData['oriannaQG'] && $prospectData['oriannaQG'] === 'false' ? false : true,
            'gender_id' => $prospectData['sexe'] ? $prospectData['sexe'] : null,
            'lastname' => $prospectData['lastname'] ? $prospectData['lastname'] : null,
        );

        $args = array(
            'method' => 'POST',
            'headers' => array(
                'Authorization' => 'Bearer ' . $token->token,
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
            ),
            'body' => json_encode($params),
        );
        $response = $this->http_post_prospect($config['api_market_url'] . $config['post_prospect_route'], $params, $token->token);
        return $response;
    }

    public function http_post($url, $data = [], $token = null, $type = null)
    {
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

        if ($type === 'campaign') {

            curl_setopt($c, CURLOPT_HTTPHEADER, [
                'Authorization: Bearer ' . $token,
            ]);

        } else if ($type === 'login') {

            curl_setopt($c, CURLOPT_HEADER, false);
            curl_setopt($c, CURLOPT_POST, true);
            curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($c, CURLOPT_TIMEOUT, 5);
            curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($data));
        }

        $output = curl_exec($c);

        curl_close($c);

        if ($output === false) {
            return false;
        } else {
            return $output;
        }
    }

    public function http_post_prospect($url, $data, $token)
    {
        $c = curl_init();

        $headers = [
            'Content-type: application/json; charset=UTF-8',
            'Authorization: Bearer ' . $token
        ];

        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt($c, CURLOPT_TIMEOUT, 1);
        curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($c, CURLOPT_POST, true);
        curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($c, CURLOPT_URL, $url);
        $output = curl_exec($c);
        curl_close($c);

        if ($output === false) {
            return false;
        } else {
            return $output;
        }
    }

}

function getConsents($formId = 1)
{
    $consent = new Consents();
    $consents = $consent->getAllConsents($formId);
    return $consents;
}

function sendConsents($userData, $post)
{
    $consent = new Consents();
    $consent->sendConsents($userData, $post);
}

function sendProspect($prospectData)
{
    $consent = new Consents();
    $consent->sendProspectApiMarket($prospectData);
}





AnonSec - 2021