Ejemplo n.º 1
0
 /**
  * Request an instance of the OAuthStore
  */
 public static function instance($store = 'MySQL', $options = array())
 {
     if (!YotpoOAuthStore::$instance) {
         // Select the store you want to use
         if (strpos($store, '/') === false) {
             $class = 'YotpoOAuthStore' . $store;
             $file = dirname(__FILE__) . '/store/' . $class . '.php';
         } else {
             $file = $store;
             $store = basename($file, '.php');
             $class = $store;
         }
         if (is_file($file)) {
             require_once $file;
             if (class_exists($class)) {
                 YotpoOAuthStore::$instance = new $class($options);
             } else {
                 throw new YotpoOAuthException2('Could not find class ' . $class . ' in file ' . $file);
             }
         } else {
             throw new YotpoOAuthException2('No OAuthStore for ' . $store . ' (file ' . $file . ')');
         }
     }
     return YotpoOAuthStore::$instance;
 }
Ejemplo n.º 2
0
 private function grantOauthAccess($app_key, $secret_token)
 {
     include_once _PS_MODULE_DIR_ . 'yotpo/lib/oauth-php/library/YotpoOAuthStore.php';
     include_once _PS_MODULE_DIR_ . 'yotpo/lib/oauth-php/library/YotpoOAuthRequester.php';
     $yotpo_options = array('consumer_key' => $app_key, 'consumer_secret' => $secret_token, 'client_id' => $app_key, 'client_secret' => $secret_token, 'grant_type' => 'client_credentials');
     YotpoOAuthStore::instance('2Leg', $yotpo_options);
     try {
         $request = new YotpoOAuthRequester(self::YOTPO_OAUTH_TOKEN_URL, 'POST', $yotpo_options);
         $result = $request->doRequest(0);
         $pregResult = preg_match("/access_token[\\W]*[\"'](.*?)[\"']/", $result['body'], $matches);
         $token = $pregResult == 1 ? $matches[1] : '';
         return $token != '' ? $token : null;
     } catch (YotpoOAuthException2 $e) {
         d($e);
         return null;
     }
 }
 /**
  * Construct the request to be signed.  Parses or appends the parameters in the params url.
  * When you supply an params array, then the params should not be urlencoded.
  * When you supply a string, then it is assumed it is of the type application/x-www-form-urlencoded
  * 
  * @param string request	url
  * @param string method		PUT, GET, POST etc.
  * @param mixed params 		string (for urlencoded data, or array with name/value pairs)
  * @param string body		optional body for PUT and/or POST requests
  */
 function __construct($request, $method = null, $params = null, $body = null)
 {
     $this->store = YotpoOAuthStore::instance();
     if (is_string($params)) {
         parent::__construct($request, $method, $params);
     } else {
         parent::__construct($request, $method);
         if (is_array($params)) {
             foreach ($params as $name => $value) {
                 $this->setParam($name, $value);
             }
         }
     }
     // With put/ post we might have a body (not for application/x-www-form-urlencoded requests)
     if (strcasecmp($method, 'PUT') == 0 || strcasecmp($method, 'POST') == 0) {
         $this->setBody($body);
     }
 }
Ejemplo n.º 4
0
function fn_grant_oauth_access($app_key, $secret_token)
{
    $OAuthStorePath = dirname(__FILE__) . '/lib/oauth-php/library/YotpoOAuthStore.php';
    $OAuthRequesterPath = dirname(__FILE__) . '/lib/oauth-php/library/YotpoOAuthRequester.php';
    require_once $OAuthStorePath;
    require_once $OAuthRequesterPath;
    $yotpo_options = array('consumer_key' => $app_key, 'consumer_secret' => $secret_token, 'client_id' => $app_key, 'client_secret' => $secret_token, 'grant_type' => 'client_credentials');
    YotpoOAuthStore::instance("2Leg", $yotpo_options);
    try {
        $request = new YotpoOAuthRequester(YOTPO_OAUTH_TOKEN_URL, "POST", $yotpo_options);
        $result = $request->doRequest(0);
        $tokenParams = json_decode($result['body'], true);
        if (isset($tokenParams['access_token'])) {
            return $tokenParams['access_token'];
        } else {
            return NULL;
        }
    } catch (YotpoOAuthException2 $e) {
        //Do nothing
        return NULL;
    }
}
 /**
  * Request an access token from the site belonging to consumer_key.
  * Before this we got an request token, now we want to exchange it for
  * an access token.
  * 
  * @param string consumer_key
  * @param string token
  * @param int usr_id		user requesting the access token
  * @param string method (optional) change the method of the request, defaults to POST (as it should be)
  * @param array options (optional) extra options for request, eg token_ttl
  * @param array curl_options	optional extra options for curl request
  *  
  * @exception OAuthException2 when no key could be fetched
  * @exception OAuthException2 when no server with consumer_key registered
  */
 static function requestAccessToken($consumer_key, $token, $usr_id, $method = 'POST', $options = array(), $curl_options = array())
 {
     YotpoOAuthRequestLogger::start();
     $store = YotpoOAuthStore::instance();
     $r = $store->getServerTokenSecrets($consumer_key, $token, 'request', $usr_id);
     $uri = $r['access_token_uri'];
     $token_name = $r['token_name'];
     // Delete the server request token, this one was for one use only
     $store->deleteServerToken($consumer_key, $r['token'], 0, true);
     // Try to exchange our request token for an access token
     $oauth = new YotpoOAuthRequester($uri, $method);
     if (isset($options['oauth_verifier'])) {
         $oauth->setParam('oauth_verifier', $options['oauth_verifier']);
     }
     if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
         $oauth->setParam('xoauth_token_ttl', intval($options['token_ttl']));
     }
     YotpoOAuthRequestLogger::setRequestObject($oauth);
     $oauth->sign($usr_id, $r, '', 'accessToken');
     $text = $oauth->curl_raw($curl_options);
     if (empty($text)) {
         throw new YotpoOAuthException2('No answer from the server "' . $uri . '" while requesting an access token');
     }
     $data = $oauth->curl_parse($text);
     if ($data['code'] != 200) {
         throw new YotpoOAuthException2('Unexpected result from the server "' . $uri . '" (' . $data['code'] . ') while requesting an access token');
     }
     $token = array();
     $params = explode('&', $data['body']);
     foreach ($params as $p) {
         @(list($name, $value) = explode('=', $p, 2));
         $token[$oauth->urldecode($name)] = $oauth->urldecode($value);
     }
     if (!empty($token['oauth_token']) && !empty($token['oauth_token_secret'])) {
         $opts = array();
         $opts['name'] = $token_name;
         if (isset($token['xoauth_token_ttl'])) {
             $opts['token_ttl'] = $token['xoauth_token_ttl'];
         }
         $store->addServerToken($consumer_key, 'access', $token['oauth_token'], $token['oauth_token_secret'], $usr_id, $opts);
     } else {
         throw new YotpoOAuthException2('The server "' . $uri . '" did not return the oauth_token or the oauth_token_secret');
     }
     YotpoOAuthRequestLogger::flush();
 }
 /**
  * Logs the request to the database, sends any cached output.
  * Also called on shutdown, to make sure we always log the request being handled.
  */
 static function flush()
 {
     if (YotpoOAuthRequestLogger::$logging) {
         YotpoOAuthRequestLogger::$logging = false;
         if (is_null(YotpoOAuthRequestLogger::$sent)) {
             // What has been sent to the user-agent?
             $data = ob_get_contents();
             if (strlen($data) > 0) {
                 ob_end_flush();
             } elseif (ob_get_level()) {
                 ob_end_clean();
             }
             $hs = headers_list();
             $sent = implode("\n", $hs) . "\n\n" . $data;
         } else {
             // The request we sent
             $sent = YotpoOAuthRequestLogger::$sent;
         }
         if (is_null(YotpoOAuthRequestLogger::$received)) {
             // Build the request we received
             $hs0 = self::getAllHeaders();
             $hs = array();
             foreach ($hs0 as $h => $v) {
                 $hs[] = "{$h}: {$v}";
             }
             $data = '';
             $fh = @fopen('php://input', 'r');
             if ($fh) {
                 while (!feof($fh)) {
                     $s = fread($fh, 1024);
                     if (is_string($s)) {
                         $data .= $s;
                     }
                 }
                 fclose($fh);
             }
             $received = implode("\n", $hs) . "\n\n" . $data;
         } else {
             // The answer we received
             $received = YotpoOAuthRequestLogger::$received;
         }
         // The request base string
         if (YotpoOAuthRequestLogger::$request_object) {
             $base_string = YotpoOAuthRequestLogger::$request_object->signatureBaseString();
         } else {
             $base_string = '';
         }
         // Figure out to what keys we want to log this request
         $keys = array();
         if (YotpoOAuthRequestLogger::$request_object) {
             $consumer_key = YotpoOAuthRequestLogger::$request_object->getParam('oauth_consumer_key', true);
             $token = YotpoOAuthRequestLogger::$request_object->getParam('oauth_token', true);
             switch (get_class(YotpoOAuthRequestLogger::$request_object)) {
                 // tokens are access/request tokens by a consumer
                 case 'YotpoOAuthServer':
                 case 'YotpoOAuthRequestVerifier':
                     $keys['ocr_consumer_key'] = $consumer_key;
                     $keys['oct_token'] = $token;
                     break;
                     // tokens are access/request tokens to a server
                 // tokens are access/request tokens to a server
                 case 'YotpoOAuthRequester':
                 case 'YotpoOAuthRequestSigner':
                     $keys['osr_consumer_key'] = $consumer_key;
                     $keys['ost_token'] = $token;
                     break;
             }
         }
         // Log the request
         if (YotpoOAuthRequestLogger::$store_log) {
             $store = YotpoOAuthStore::instance();
             $store->addLog($keys, $received, $sent, $base_string, YotpoOAuthRequestLogger::$note, YotpoOAuthRequestLogger::$user_id);
         }
         YotpoOAuthRequestLogger::$log[] = array('keys' => $keys, 'received' => $received, 'sent' => $sent, 'base_string' => $base_string, 'note' => YotpoOAuthRequestLogger::$note);
     }
 }