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/Google/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/Social/Helper/Google/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\Google;

use Magento\Framework\App\ResponseInterface;
use Magento\Framework\DataObject;
use Soon\Social\Helper\Connect as AbstractConnect;

/**
 * Class Connect
 * @package Soon\Social\Helper\Google
 */
class Connect extends AbstractConnect
{
    /**
     * @var string
     */
    const SERVICE_NAME = 'google';

    /**
     * @var \Google_Client
     */
    private $google;

    /**
     * Facebook app config
     *
     * @var array
     */
    protected $_appConfig;

    /**
     * Get service name
     *
     * @return string
     */
    public function getServiceName()
    {
        return self::SERVICE_NAME;
    }

    /**
     * Retrieve URL login for Google login
     *
     * @param bool $loginFlag
     * @return string
     */
    public function getLoginUrl($loginFlag = false)
    {
        $loginUri = 'connect/google/googleLogin';
        $params = $this->_getRequest()->getParams();

        if ($loginFlag) {
            $params['login'] = 1;
        }
        return $this->_getUrl($loginUri, $params);
    }

    /**
     * Retrieve URL for post Google login process
     *
     * @return string
     */
    public function getRedirectUrl()
    {
        return $this->_getUrl('connect/google/googleLoginBack');
    }

    /**
     * Generate Google OAuth URL
     *
     * @return string
     */
    public function getOAuthUrl()
    {
        return $this->google()->createAuthUrl();
    }

    /**
     * Get user data from connector
     *
     * @return DataObject
     */
    public function getUserData()
    {
        return $this->_getUserData();
    }

    /**
     * @param ResponseInterface $response
     */
    public function beforeConnectLogin(ResponseInterface $response)
    {
    }

    /**
     * @param ResponseInterface $response
     */
    public function beforeConnectLoginBack(ResponseInterface $response)
    {
        $code = '';
        if (!is_null($this->_getRequest()->getParam('code'))) {
            $code = $this->_getRequest()->getParam('code');
        }
        $this->setAccessToken($code);
    }

    /**
     * Set access token to $google object
     *
     * @param string $code
     * @return Connect
     */
    public function setAccessToken($code = '')
    {
        $google = $this->google();

        if ($google->isAccessTokenExpired() && $code != '') {
            $google->fetchAccessTokenWithAuthCode($code);
        } else {
            return false;
        }

        $accessToken = $google->getAccessToken();
        $google->setAccessToken($accessToken);

        return $this;
    }

    /**
     * Retrieve Google user data and create a DataObject with it
     *
     * @return DataObject
     */
    protected function _getUserData()
    {
        $googleService = new \Google_Service_Oauth2($this->google());
        $userinfo = $googleService->userinfo->get();

        $googleUserData = new DataObject();
        foreach ($userinfo as $k => $v) {
            $googleUserData->setData(str_replace('_', '', $k), $v);
        }

        $googleUserData->setData('social_referrer', self::SERVICE_NAME);

        return $this->convertUserData($googleUserData);
    }

    /**
     * Convert keys from Google to create form key
     *
     * @param DataObject $googleUserData
     * @return DataObject
     */
    private function convertUserData($googleUserData)
    {
        $convertValues = $this->scopeConfig->getValue('soon_social/google_convert_user_data');
        $convertKeys = array_keys($convertValues);

        foreach ($googleUserData->getData() as $key => $value) {
            if (in_array($key, $convertKeys)) {
                $googleUserData->setData($convertValues[$key], $value);
                $googleUserData->unsetData($key);
            }
        }

        return $googleUserData;
    }

    /**
     * Google SDK instanciation
     *
     * @return \Google_Client
     */
    private function google()
    {
        if (is_null($this->google)) {
            $client = new \Google_Client();
            $client->setApplicationName($this->getAppConfig()['app_name']);
            $client->setClientId($this->getAppConfig()['app_id']);
            $client->setClientSecret($this->getAppConfig()['secret']);
            $client->setRedirectUri($this->getRedirectUrl());

            $client->addScope(\Google_Service_Oauth2::USERINFO_EMAIL);
            $client->addScope(\Google_Service_Oauth2::USERINFO_PROFILE);

            $this->google = $client;
        }

        return $this->google;
    }

    /**
     * Retrieve Google app config
     * @return array[]
     * @throws \Exception
     */
    private function getAppConfig()
    {
        if (is_null($this->_appConfig)) {
            $configFields = [
                'app_name' => ['is_required' => true],
                'app_id' => ['is_required' => true],
                'secret' => ['is_required' => true],
            ];

            $appConfig = [];

            foreach ($configFields as $field => $v) {
                $config = $this->scopeConfig->getValue('soon_social_connect/google_connect/' . $field);

                if (trim($config) == '' && $v['is_required']) {
                    throw new \Exception(sprintf('Cannot find Google info for required field: %s', $field));
                }

                $appConfig[$field] = $config;
            }

            $this->_appConfig = $appConfig;
        }

        return $this->_appConfig;
    }
}

Spamworldpro Mini