| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaStudio UNA Studio
- * @{
- */
- use OAuth\ServiceFactory;
- use OAuth\OAuth1\Service\Una;
- use OAuth\Common\Http\Uri\Uri;
- use OAuth\Common\Http\Exception\TokenResponseException;
- use OAuth\Common\Storage\Session;
- use OAuth\Common\Storage\Exception\TokenNotFoundException;
- use OAuth\Common\Consumer\Credentials;
- require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/bootstrap.php');
- class BxDolStudioOAuthPlugin extends BxDolStudioOAuthOAuth1 implements iBxDolSingleton
- {
- protected $sService;
- protected $oStorage;
- protected function __construct()
- {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
- parent::__construct ();
- $this->sKey = getParam('sys_oauth_key');
- $this->sSecret = getParam('sys_oauth_secret');
- $this->sDataRetrieveMethod = 'POST';
- $this->sService = 'Una';
- }
- public function __clone()
- {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
- }
- public static function getInstance()
- {
- if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
- $sClass = __CLASS__;
- $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
- }
- return $GLOBALS['bxDolClasses'][__CLASS__];
- }
- protected function authorize()
- {
- if($this->isAuthorized())
- return true;
- $oService = $this->getServiceObject();
- try {
- $bToken = bx_get('oauth_token') !== false;
- $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
- if(!$bToken && $mixedSecret !== false) {
- $this->oSession->unsetValue('sys_oauth_secret');
- $mixedSecret = false;
- }
- //--- Get request token and redirect to authorize.
- if(!$bToken && $mixedSecret === false)
- return $this->getRequestToken($oService);
- //--- Get access token.
- if($bToken && $mixedSecret !== false)
- return $this->getAccessToken(bx_get('oauth_token'), bx_get('oauth_verifier'), (int)bx_get('oauth_user'), $oService);
- }
- catch(TokenNotFoundException $e) {
- $this->unsetAuthorizedUser();
- return $this->getRequestToken();
- }
- return $this->getRequestToken();
- }
- protected function getRequestToken($oService = null)
- {
- if(empty($oService))
- $oService = $this->getServiceObject();
- $sError = _t('_adm_err_oauth_cannot_get_token');
- try {
- $oToken = $oService->requestRequestToken();
- }
- catch (TokenResponseException $e) {
- if(!$this->isAuthorized())
- return $sError;
- $this->unsetAuthorizedUser();
- return $this->getRequestToken($oService);
- }
- if(empty($oToken))
- return $sError;
- $this->oSession->setValue('sys_oauth_secret', $oToken->getRequestTokenSecret());
- $oUrl = $oService->getAuthorizationUri(array('oauth_token' => $oToken->getRequestToken()));
- return _t('_adm_msg_oauth_need_authorize', bx_append_url_params($oUrl, array('sid' => bx_site_hash())));
- }
- protected function getAccessToken($sToken, $sVerifier, $iUser, $oService)
- {
- $oToken = $this->oStorage->retrieveAccessToken($this->sService);
- $sError = _t('_adm_err_oauth_cannot_get_token');
- try {
- $oAccessToken = $oService->requestAccessToken($sToken, $sVerifier, $oToken->getRequestTokenSecret());
- }
- catch (TokenResponseException $e) {
- $sErrorException = $e->getMessage();
- if(!empty($sErrorException))
- $sError = $sErrorException;
- if(!$this->isAuthorized())
- return $sError;
- $this->unsetAuthorizedUser();
- return $this->getRequestToken($oService);
- }
- if(empty($oAccessToken))
- return $sError;
- $this->oSession->setValue('sys_oauth_token', $oAccessToken->getAccessToken());
- $this->oSession->setValue('sys_oauth_secret', $oAccessToken->getAccessTokenSecret());
- $this->oSession->setValue('sys_oauth_authorized', 1);
- $this->oSession->setValue('sys_oauth_authorized_user', $iUser);
- return true;
- }
- protected function fetch($aParams = array())
- {
- if(!$this->isAuthorized())
- return array();
- try {
- $sResponse = $this->getServiceObject()->request(BX_DOL_OAUTH_URL_FETCH_DATA, $this->sDataRetrieveMethod, $aParams);
- //echo $sResponse; exit; //--- Uncomment to debug
- return json_decode($sResponse, true);
- }
- catch(TokenNotFoundException $e) {
- $this->unsetAuthorizedUser();
- return $this->getRequestToken();
- }
- }
- private function getServiceObject()
- {
- $this->oStorage = new Session();
- $oUrl = new Uri(BX_DOL_URL_STUDIO . 'store.php?page=purchases');
- $oCredentials = new Credentials($this->sKey, $this->sSecret, $oUrl->getAbsoluteUri());
- $oServiceFactory = new ServiceFactory();
- return $oServiceFactory->createService($this->sService, $oCredentials, $this->oStorage);
- }
- }
- /** @} */
|