Your IP : 216.73.217.13


Current Path : /var/www/magento.test.indacotrentino.com/www/app/code/Torresani/Consent/Block/
Upload File :
Current File : /var/www/magento.test.indacotrentino.com/www/app/code/Torresani/Consent/Block/YourPrivacy.php

<?php
namespace Torresani\Consent\Block;

use \Magento\Framework\App\DeploymentConfig;
class YourPrivacy extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;
    private DeploymentConfig $deploymentConfig;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Customer\Model\Session $customer,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        DeploymentConfig $deploymentConfig,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->customer = $customer;
        $this->redirect = $redirect;
        $this->deploymentConfig = $deploymentConfig;
    }

    public function getHelloWorld()
    {
        $customer = $this->customer;
        $customerEmail = $customer->getCustomer()->getEmail();

        if(!$customer->isLoggedIn()) {
            $customer->authenticate();
        }

        $req = curl_init();
        curl_setopt($req, CURLOPT_URL, 'https://consent.iubenda.com/consent/?subject_email_exact='.$customerEmail);
        curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($req, CURLOPT_HTTPHEADER, array(
            'ApiKey: ' . $this->deploymentConfig->get('indaco/iubendaApiKey'),
            'Content-Type: application/json'
        ));
        $response = curl_exec($req);

        return $response;
    }

    public function anonymousPrivacy()
    {
        $customer = $this->customer;
        $key = 'my_secret_key';

        if (hash_hmac('sha256', ($_GET['m'] ?? ''), $key) == ($_GET['k'] ?? '')) {
            $customerEmail = urldecode($_GET['m']);
        } else {
            $customer->authenticate();
        }

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerData = $objectManager->create('Magento\Customer\Model\Customer');
        $customerData->setWebsiteId(1);
        $customerData->loadByEmail($customerEmail);
        if ($customerData->getId() > 0) {
            $customer->authenticate();
        }

        $req = curl_init();
        curl_setopt($req, CURLOPT_URL, 'https://consent.iubenda.com/consent/?subject_email_exact='.$_GET['m']);
        curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($req, CURLOPT_HTTPHEADER, array(
            'ApiKey: ' . $this->deploymentConfig->get('indaco/iubendaApiKey'),
            'Content-Type: application/json'
        ));
        $response = curl_exec($req);

        if (empty($response)) {
            $customer->authenticate();
        }

        return $response;
    }

}
?>