Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/Social/Helper/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/Social/Helper/Connect.php
<?php
/**
 * This file is part of Soon_Social for Magento2.
 *
 * @license All rights reserved
 * @author Christophe SOON <[email protected]>
 * @category Soon
 * @package Soon_Social
 * @copyright Copyright (c) 2015 Agence Soon (http://www.agence-soon.fr)
 */

namespace Soon\Social\Helper;

use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Url;
use Magento\Store\Model\StoreManagerInterface;
use Monolog\Logger;

/**
 * Class Connect
 * @package Soon\Social\Helper
 */
abstract class Connect extends AbstractHelper implements ConnectInterface
{
    /**
     * @var Session
     */
    protected $_session;

    /**
     * @var ScopeConfigInterface
     */
    protected $_configInterface;

    /**
     * @var Connect
     */
    protected $_service;

    /**
     * @var Url
     */
    protected $_url;

    /**
     * @var \Magento\Framework\Event\ManagerInterface
     */
    protected $_eventManager;

    /**
     * @var ManagerInterface
     */
    protected $_messageManager;

    /**
     * @var Logger
     */
    protected $_logger;

    /**
     * @var StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var CustomerFactory
     */
    protected $_customerFactory;

    /**
     * Data constructor.
     * @param Context $context
     * @param Session $session
     * @param Url $url
     * @param ManagerInterface $messageManager
     * @param StoreManagerInterface $storeManager
     * @param CustomerFactory $customerFactory
     */
    public function __construct(
        Context $context,
        Session $session,
        Url $url,
        ManagerInterface $messageManager,
        StoreManagerInterface $storeManager,
        CustomerFactory $customerFactory
    ) {
        parent::__construct($context);
        $this->_session = $session;
        $this->_url = $url;
        $this->_configInterface = $context->getScopeConfig();
        $this->_eventManager = $context->getEventManager();
        $this->_messageManager = $messageManager;
        $this->_logger = new Logger('main');
        $this->_storeManager = $storeManager;
        $this->_customerFactory = $customerFactory;
    }

    /**
     * Login using a service
     *
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function connectLogin(ResponseInterface $response)
    {
        $this->beforeConnectLogin($response);

        $request = $this->_getRequest();
        $referer = $this->getReferer();
        $login = $request->getParam('login') ? true : false;

        $this->setIsLogin($login);

        $this->_session->setBeforeAuthUrl($referer);
        $this->_session->setData('before_' . strtolower($this->getServiceName()) . '_auth_url', $referer);

        try {
            $url = $this->getOAuthUrl();
            $response->setRedirect($url);
        } catch (\Exception $e) {
            $this->_throwError($e, $response);
        }

        return $response;
    }

    /**
     * Manage user coming back from service authentication
     *
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function connectLoginBackAction(ResponseInterface $response)
    {
        $this->beforeConnectLoginBack($response);

        $beforeAuthUrl = $this->_session->getData('before_' . strtolower($this->getServiceName()) . '_auth_url');
        $this->_session->setBeforeAuthUrl($beforeAuthUrl);

        if ($this->_getRequest()->getParam('error')) {
            $response->setRedirect($beforeAuthUrl);
            return $response;
        }

        try {
            $customer = $this->getCustomer();

            $customerFormData = $this->getUserData()->getData();
            $loginSuccess = $customer->getId() ? true : false;

            $this->_session->setCustomerFormData($customerFormData);
            $this->setIsLogin($loginSuccess);

            $response = $this->getResponseWithRedirect($response, $customer);

            return $response;
        } catch (\Exception $e) {
            $this->_throwError($e, $response);
        }

        return $response;
    }

    /**
     *  base64_decode() for URLs dencoding
     *
     * @param    string $url
     * @return   string
     */
    public function urlDecode($url)
    {
        $url = base64_decode(strtr($url, '-_,', '+/='));
        return $url;
    }

    /**
     * @param string $url
     * @return bool
     */
    public function isUrlInternal($url)
    {
        if (strpos($url, 'http') !== false) {
            $directLinkType = \Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK;
            $unsecureBaseUrl = $this->_storeManager->getStore()->getBaseUrl($directLinkType, false);
            $secureBaseUrl = $this->_storeManager->getStore()->getBaseUrl($directLinkType, true);
            return (strpos($url, $unsecureBaseUrl) === 0) || (strpos($url, $secureBaseUrl) === 0);
        }
        return false;
    }

    /**
     * @param ResponseInterface $response
     * @param Customer $customer
     * @return ResponseInterface
     */
    protected function getResponseWithRedirect(ResponseInterface $response, Customer $customer)
    {
        // If customer exists, log him•her
        if ($customer->getId()) {
            $this->_session->loginById($customer->getId());
            $response->setRedirect(
                $this->_session->getData('before_' . strtolower($this->getServiceName()) . '_auth_url')
            );
        } elseif ($this->getIsLogin()) {
            // If customer does not exist but user tried to login with FB, display error
            $this->_session->unsetData(strtolower($this->getServiceName()) . '_login');
            $this->_messageManager->addErrorMessage(__(
                'We do not have an account with your %1 credential. Please fill the missing fields below to complete your registration.',
                $this->getServiceName()
            ));

            $response->setRedirect($this->_getUrl('customer/account/create'));
        } else {
            // If customer does not exist we ask to complete form
            $this->_messageManager->addNoticeMessage(__('Please fill the missing fields below to complete your registration.'));
            $response->setRedirect($this->_getUrl('customer/account/create'));
        }

        return $response;
    }

    /**
     * @return string
     */
    private function getReferer()
    {
        $request = $this->_getRequest();
        $referer = $request->getHeader('referer');

        if (!$this->_configInterface->getValue(CustomerUrl::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD)) {
            $refererParam = $request->getParam('referer');

            if ($refererParam) {
                // Rebuild referer URL to handle the case when SID was changed
                $refererUrl = $this->_url->getRebuiltUrl($this->urlDecode($refererParam));

                if ($this->isUrlInternal($refererUrl)) {
                    $referer = $refererUrl;
                }
            }
        }

        return $referer;
    }

    /**
     * Retrieve Magento customer for logged in user
     * @return Customer
     * @throws \Exception
     */
    protected function getCustomer()
    {
        /** @var Customer $customerModel */
        $customerModel = $this->_customerFactory->create();

        /* @todo Manage website scope login */
        if ($customerModel->getSharingConfig()->isWebsiteScope()) {
            throw new \Exception('Customer account sharing must be global');
        }

        return $customerModel->loadByEmail($this->getUserData()->getData('email'));
    }

    /**
     * @param bool $isLogin
     */
    protected function setIsLogin($isLogin)
    {
        $this->_session->setData(strtolower($this->getServiceName()) . '_login', $isLogin);
    }

    /**
     * @return bool
     */
    protected function getIsLogin()
    {
        return $this->_session->getData(strtolower($this->getServiceName()) . '_login');
    }

    /**
     * @return DataObject|Connect
     */
    protected function _prepareService()
    {
        $this->_service = new DataObject();
        return $this->_service;
    }

    /**
     * For any error with APIs
     *
     * @param \Exception|Exception $e
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    protected function _throwError(\Exception $e, ResponseInterface $response)
    {
        $this->_messageManager->addErrorMessage(sprintf(
            __('Sorry, %s cannot be used at the moment.'),
            $this->getServiceName()
        ));

        $this->_logger->addError($e->getMessage());

        $response->setRedirect($this->_getUrl('customer/account'));
        return $response;
    }
}

Spamworldpro Mini