AntonLV пре 12 година
родитељ
комит
93379b244d

+ 11 - 32
studio/classes/BxDolStudioOAuth.php

@@ -52,19 +52,16 @@ class BxDolStudioOAuth extends BxDol
         if(empty($this->sKey) || empty($this->sSecret))
             return _t('_adm_err_oauth_empty_key_secret');
 
-        $mixedResult = $this->authorize($this->sKey, $this->sSecret);
+        $mixedResult = $this->authorize();
         if($mixedResult !== true)
             return $mixedResult;
 
-        $aItems = $this->fetch($this->sKey, $this->sSecret, $aParams);
+        $aItems = $this->fetch($aParams);
         if(is_null($aItems))
             return _t('_adm_err_oauth_cannot_read_answer');
         else if(empty($aItems))
             return MsgBox(_t('_Empty'));
 
-        if($this->isServerError($aItems))
-            return $this->processServerError($aItems);
-
         return $aItems;
     }
 
@@ -73,9 +70,8 @@ class BxDolStudioOAuth extends BxDol
     	if(empty($this->sKey) || empty($this->sSecret))
             return _t('_adm_err_oauth_empty_key_secret');
 
-        return $this->authorize($this->sKey, $this->sSecret);
+        return $this->authorize();
     }
-    
 
     protected function isAuthorized()
     {
@@ -87,35 +83,18 @@ class BxDolStudioOAuth extends BxDol
     	return self::getAuthorizedClient();
     }
 
+	protected function unsetAuthorizedUser()
+	{
+		$this->oSession->unsetValue('sys_oauth_token');
+		$this->oSession->unsetValue('sys_oauth_secret');
+        $this->oSession->unsetValue('sys_oauth_authorized');
+		$this->oSession->unsetValue('sys_oauth_authorized_user');
+	}
+
     protected function isServerError($aResult)
     {
         return isset($aResult[$this->sErrorCode]) && isset($aResult[$this->sErrorMessage]);
     }
-
-    protected function processServerError($aResult)
-    {
-        $iCode = $aResult[$this->sErrorCode];
-        $sMessage = $aResult[$this->sErrorMessage];
-
-        switch($iCode) {
-            case '8':
-            case '16':
-            case '32':
-            case '64':
-            case '256':
-            case '1024':
-            case '2048':
-                bx_import('BxDolSession');
-                $this->oSession = BxDolSession::getInstance();
-                $this->oSession->unsetValue('sys_oauth_token');
-                $this->oSession->unsetValue('sys_oauth_secret');
-                $this->oSession->unsetValue('sys_oauth_authorized');
-                $this->oSession->unsetValue('sys_oauth_authorized_user');
-                break;
-        }
-
-        return $sMessage;
-    }
 }
 
 /** @} */

+ 60 - 38
studio/classes/BxDolStudioOAuthLib.php

@@ -39,16 +39,14 @@ class BxDolStudioOAuthLib extends BxDolStudioOAuth implements iBxDolSingleton
         return $GLOBALS['bxDolClasses'][__CLASS__];
     }
 
-    protected function authorize($sKey, $sSecret)
+    protected function authorize()
     {
         if($this->isAuthorized())
             return true;
 
-        try {
-            $oConsumer = new OAuth($sKey, $sSecret);
-            $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
-            $oConsumer->enableDebug();
+		$oConsumer = $this->getServiceObject(false);
 
+        try {
             $bToken = bx_get('oauth_token') !== false;
             $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
             if(!$bToken && $mixedSecret !== false) {
@@ -57,60 +55,84 @@ class BxDolStudioOAuthLib extends BxDolStudioOAuth implements iBxDolSingleton
             }
 
             //--- Get request token and redirect to authorize.
-            if(!$bToken && $mixedSecret === false) {
-                $aRequestToken = $oConsumer->getRequestToken(BX_DOL_OAUTH_URL_REQUEST_TOKEN);
-                if(empty($aRequestToken))
-                    return _t('_adm_err_oauth_cannot_get_token');
+            if(!$bToken && $mixedSecret === false)
+                return $this->getRequestToken($oConsumer);
 
-                if($this->isServerError($aRequestToken))
-                    return $this->processServerError($aRequestToken);
+            //--- Get access token.
+            if($bToken && $mixedSecret !== false)
+                return $this->getAccessToken(bx_get('oauth_token'), $mixedSecret, bx_get('oauth_verifier'), (int)bx_get('oauth_user'),  $oConsumer);
+        }
+        catch(OAuthException $e) {
+            return $this->getRequestToken();
+        }
 
-                $this->oSession->setValue('sys_oauth_secret', $aRequestToken['oauth_token_secret']);
-                return _t('_adm_msg_oauth_need_authorize', bx_append_url_params(BX_DOL_OAUTH_URL_AUTHORIZE, array('oauth_token' => $aRequestToken['oauth_token'], 'sid' => bx_site_hash())));
-            }
+        return $this->getRequestToken();
+    }
 
-            //--- Get access token.
-            if($bToken && $mixedSecret !== false) {
-                $oConsumer->setToken(bx_get('oauth_token'), $mixedSecret);
-                $aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => bx_get('oauth_verifier'))));
-                if(empty($aAccessToken))
-                    return _t('_adm_err_oauth_cannot_get_token');
+    protected function getRequestToken($oConsumer = null)
+    {
+    	if(empty($oConsumer))
+    		$oConsumer = $this->getServiceObject();
 
-                if($this->isServerError($aAccessToken))
-                    return $this->processServerError($aAccessToken);
+    	$aRequestToken = $oConsumer->getRequestToken(BX_DOL_OAUTH_URL_REQUEST_TOKEN);
+		if(empty($aRequestToken))
+        	return _t('_adm_err_oauth_cannot_get_token');
 
-                $this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
-                $this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
-                $this->oSession->setValue('sys_oauth_authorized', 1);
-                $this->oSession->setValue('sys_oauth_authorized_user', (int)bx_get('oauth_user'));
+		if($this->isServerError($aRequestToken))
+			throw new OAuthException();
 
-                return true;
-            }
-        } catch(OAuthException $e) {
-            return _t('_adm_err_oauth_cannot_get_token');
-        }
+		$this->oSession->setValue('sys_oauth_secret', $aRequestToken['oauth_token_secret']);
+		return _t('_adm_msg_oauth_need_authorize', bx_append_url_params(BX_DOL_OAUTH_URL_AUTHORIZE, array('oauth_token' => $aRequestToken['oauth_token'], 'sid' => bx_site_hash())));
     }
 
-    protected function fetch($sKey, $sSecret, $aParams = array())
+    protected function getAccessToken($sToken, $mixedSecret, $sVerifier, $iUser,  $oConsumer)
+    {
+		$oConsumer->setToken($sToken, $mixedSecret);
+		$aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => $sVerifier)));
+		if(empty($aAccessToken))
+			return _t('_adm_err_oauth_cannot_get_token');
+
+		if($this->isServerError($aAccessToken))
+			throw new OAuthException();
+
+		$this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
+		$this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
+		$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 {
-            $oConsumer = new OAuth($sKey, $sSecret);
-            $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
-            $oConsumer->enableDebug();
+		$oConsumer = $this->getServiceObject(false);
 
+        try {
             $oConsumer->setToken($this->oSession->getValue('sys_oauth_token'), $this->oSession->getValue('sys_oauth_secret'));
             $oConsumer->fetch(BX_DOL_OAUTH_URL_FETCH_DATA, $aParams, $this->sDataRetrieveMethod);
 
-            
             //echo $oConsumer->getLastResponse(); exit;	//--- Uncomment to debug
             return json_decode($oConsumer->getLastResponse(), true);
-        } catch(OAuthException $e) {
+        } 
+        catch(OAuthException $e) {
             return array();
         }
     }
+
+	private function getServiceObject($bClearStorage = true)
+    {
+    	if($bClearStorage)
+    		$this->unsetAuthorizedUser();
+
+    	$oConsumer = new OAuth($this->sKey, $this->sSecret);
+		$oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
+        $oConsumer->enableDebug();
+
+        return $oConsumer;
+    }
 }
 
 /** @} */

+ 49 - 34
studio/classes/BxDolStudioOAuthPlugin.php

@@ -36,7 +36,6 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuth implements iBxDolSingleton
         $this->sDataRetrieveMethod = 'POST';
 
         $this->sService = 'Dolphin';
-        $this->oStorage = new Session();
     }
 
     public function __clone()
@@ -55,14 +54,14 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuth implements iBxDolSingleton
         return $GLOBALS['bxDolClasses'][__CLASS__];
     }
 
-    protected function authorize($sKey, $sSecret)
+    protected function authorize()
     {
-        if($this->isAuthorized())
-            return true;
+		if($this->isAuthorized())
+			return true;
 
-        try {
-        	$oService = $this->getServiceObject();
+		$oService = $this->getServiceObject();
 
+        try {
             $bToken = bx_get('oauth_token') !== false;
             $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
             if(!$bToken && $mixedSecret !== false) {
@@ -71,48 +70,62 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuth implements iBxDolSingleton
             }
 
             //--- Get request token and redirect to authorize.
-            if(!$bToken && $mixedSecret === false) {
-            	$oToken = $oService->requestRequestToken();
-            	if(empty($oToken))
-                    return _t('_adm_err_oauth_cannot_get_token');
-
-				$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())));
-            }
+            if(!$bToken && $mixedSecret === false)
+            	return $this->getRequestToken($oService);
 
             //--- Get access token.
-            if($bToken && $mixedSecret !== false) {
-            	$oToken = $this->oStorage->retrieveAccessToken($this->sService);
-            	$oAccessToken = $oService->requestAccessToken(bx_get('oauth_token'), bx_get('oauth_verifier'), $oToken->getRequestTokenSecret());
-            	if(empty($oAccessToken))
-                    return _t('_adm_err_oauth_cannot_get_token');
-
-                $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', (int)bx_get('oauth_user'));
-
-                return true;
-            }
+            if($bToken && $mixedSecret !== false)
+            	return $this->getAccessToken(bx_get('oauth_token'), bx_get('oauth_verifier'), (int)bx_get('oauth_user'), $oService);
         }
         catch(TokenResponseException $e) {
-            return _t('_adm_err_oauth_cannot_get_token');
+        	$this->unsetAuthorizedUser();
+            return $this->getRequestToken();
         }
         catch(TokenNotFoundException $e) {
-            return _t('_adm_err_oauth_cannot_get_token');
+        	$this->unsetAuthorizedUser();
+            return $this->getRequestToken();
         }
+
+        return $this->getRequestToken();
     }
 
-    protected function fetch($sKey, $sSecret, $aParams = array())
+    protected function getRequestToken($oService = null)
+    {
+    	if(empty($oService))
+    		$oService = $this->getServiceObject();
+
+		$oToken = $oService->requestRequestToken();
+		if(empty($oToken))
+			return _t('_adm_err_oauth_cannot_get_token');
+
+		$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);
+		$oAccessToken = $oService->requestAccessToken($sToken, $sVerifier, $oToken->getRequestTokenSecret());
+		if(empty($oAccessToken))
+			return _t('_adm_err_oauth_cannot_get_token');
+
+		$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 {
-        	$oService = $this->getServiceObject();
-			$sResponse = $oService->request(BX_DOL_OAUTH_URL_FETCH_DATA, $this->sDataRetrieveMethod, $aParams);
+			$sResponse = $this->getServiceObject()->request(BX_DOL_OAUTH_URL_FETCH_DATA, $this->sDataRetrieveMethod, $aParams);
 
             //echo $sResponse; exit;	//--- Uncomment to debug
             return json_decode($sResponse, true);
@@ -124,6 +137,8 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuth implements iBxDolSingleton
 
     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());