[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Zend/Oauth/ -> Client.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to [email protected] so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Oauth
  17   * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  19   * @version    $Id: Client.php 24593 2012-01-05 20:35:02Z matthew $
  20   */
  21  
  22  /** Zend_Oauth */
  23  require_once  'Zend/Oauth.php';
  24  
  25  /** Zend_Http_Client */
  26  require_once  'Zend/Http/Client.php';
  27  
  28  /** Zend_Oauth_Http_Utility */
  29  require_once  'Zend/Oauth/Http/Utility.php';
  30  
  31  /** Zend_Oauth_Config */
  32  require_once  'Zend/Oauth/Config.php';
  33  
  34  /**
  35   * @category   Zend
  36   * @package    Zend_Oauth
  37   * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  38   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  39   */
  40  class Zend_Oauth_Client extends Zend_Http_Client
  41  {
  42      /**
  43       * Flag to indicate that the client has detected the server as supporting
  44       * OAuth 1.0a
  45       */
  46      public static $supportsRevisionA = false;
  47  
  48      /**
  49       * Holds the current OAuth Configuration set encapsulated in an instance
  50       * of Zend_Oauth_Config; it's not a Zend_Config instance since that level
  51       * of abstraction is unnecessary and doesn't let me escape the accessors
  52       * and mutators anyway!
  53       *
  54       * @var Zend_Oauth_Config
  55       */
  56      protected $_config = null;
  57  
  58      /**
  59       * True if this request is being made with data supplied by
  60       * a stream object instead of a raw encoded string.
  61       *
  62       * @var bool
  63       */
  64      protected $_streamingRequest = null;
  65  
  66      /**
  67       * Constructor; creates a new HTTP Client instance which itself is
  68       * just a typical Zend_Http_Client subclass with some OAuth icing to
  69       * assist in automating OAuth parameter generation, addition and
  70       * cryptographioc signing of requests.
  71       *
  72       * @param  array|Zend_Config $oauthOptions
  73       * @param  string            $uri
  74       * @param  array|Zend_Config $config
  75       * @return void
  76       */
  77      public function __construct($oauthOptions, $uri = null, $config = null)
  78      {
  79          if ($config instanceof Zend_Config && !isset($config->rfc3986_strict)) {
  80              $config                   = $config->toArray();
  81              $config['rfc3986_strict'] = true;
  82          } else if (null === $config ||
  83                     (is_array($config) && !isset($config['rfc3986_strict']))) {
  84              $config['rfc3986_strict'] = true;
  85          }
  86          parent::__construct($uri, $config);
  87          $this->_config = new Zend_Oauth_Config;
  88          if ($oauthOptions !== null) {
  89              if ($oauthOptions instanceof Zend_Config) {
  90                  $oauthOptions = $oauthOptions->toArray();
  91              }
  92              $this->_config->setOptions($oauthOptions);
  93          }
  94      }
  95  
  96     /**
  97       * Load the connection adapter
  98       *
  99       * @param Zend_Http_Client_Adapter_Interface $adapter
 100       * @return void
 101       */
 102      public function setAdapter($adapter)
 103      {
 104          if ($adapter == null) {
 105              $this->adapter = $adapter;
 106          } else {
 107                parent::setAdapter($adapter);
 108          }
 109      }
 110  
 111      /**
 112       * Set the streamingRequest variable which controls whether we are
 113       * sending the raw (already encoded) POST data from a stream source.
 114       *
 115       * @param boolean $value The value to set.
 116       * @return void
 117       */
 118      public function setStreamingRequest($value)
 119      {
 120          $this->_streamingRequest = $value;
 121      }
 122  
 123      /**
 124       * Check whether the client is set to perform streaming requests.
 125       *
 126       * @return boolean True if yes, false otherwise.
 127       */
 128      public function getStreamingRequest()
 129      {
 130          if ($this->_streamingRequest) {
 131              return true;
 132          } else {
 133              return false;
 134          }
 135      }
 136  
 137      /**
 138       * Prepare the request body (for POST and PUT requests)
 139       *
 140       * @return string
 141       * @throws Zend_Http_Client_Exception
 142       */
 143      protected function _prepareBody()
 144      {
 145          if($this->_streamingRequest) {
 146              $this->setHeaders(self::CONTENT_LENGTH,
 147                  $this->raw_post_data->getTotalSize());
 148              return $this->raw_post_data;
 149          }
 150          else {
 151              return parent::_prepareBody();
 152          }
 153      }
 154  
 155      /**
 156       * Clear all custom parameters we set.
 157       *
 158       * @return Zend_Http_Client
 159       */
 160      public function resetParameters($clearAll = false)
 161      {
 162          $this->_streamingRequest = false;
 163          return parent::resetParameters($clearAll);
 164      }
 165  
 166      /**
 167       * Set the raw (already encoded) POST data from a stream source.
 168       *
 169       * This is used to support POSTing from open file handles without
 170       * caching the entire body into memory. It is a wrapper around
 171       * Zend_Http_Client::setRawData().
 172       *
 173       * @param string $data The request data
 174       * @param string $enctype The encoding type
 175       * @return Zend_Http_Client
 176       */
 177      public function setRawDataStream($data, $enctype = null)
 178      {
 179          $this->_streamingRequest = true;
 180          return $this->setRawData($data, $enctype);
 181      }
 182  
 183      /**
 184       * Same as Zend_Http_Client::setMethod() except it also creates an
 185       * Oauth specific reference to the method type.
 186       * Might be defunct and removed in a later iteration.
 187       *
 188       * @param  string $method
 189       * @return Zend_Http_Client
 190       */
 191      public function setMethod($method = self::GET)
 192      {
 193          if ($method == self::GET) {
 194              $this->setRequestMethod(self::GET);
 195          } elseif($method == self::POST) {
 196              $this->setRequestMethod(self::POST);
 197          } elseif($method == self::PUT) {
 198              $this->setRequestMethod(self::PUT);
 199          }  elseif($method == self::DELETE) {
 200              $this->setRequestMethod(self::DELETE);
 201          }   elseif($method == self::HEAD) {
 202              $this->setRequestMethod(self::HEAD);
 203          }
 204          return parent::setMethod($method);
 205      }
 206  
 207      /**
 208       * Same as Zend_Http_Client::request() except just before the request is
 209       * executed, we automatically append any necessary OAuth parameters and
 210       * sign the request using the relevant signature method.
 211       *
 212       * @param  string $method
 213       * @return Zend_Http_Response
 214       */
 215      public function request($method = null)
 216      {
 217          if ($method !== null) {
 218              $this->setMethod($method);
 219          }
 220          $this->prepareOauth();
 221          return parent::request();
 222      }
 223  
 224      /**
 225       * Performs OAuth preparation on the request before sending.
 226       *
 227       * This primarily means taking a request, correctly encoding and signing
 228       * all parameters, and applying the correct OAuth scheme to the method
 229       * being used.
 230       *
 231       * @return void
 232       * @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
 233       */
 234      public function prepareOauth()
 235      {
 236          $requestScheme = $this->getRequestScheme();
 237          $requestMethod = $this->getRequestMethod();
 238          $query = null;
 239          if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
 240              $oauthHeaderValue = $this->getToken()->toHeader(
 241                  $this->getUri(true),
 242                  $this->_config,
 243                  $this->_getSignableParametersAsQueryString(),
 244                  $this->getRealm()
 245              );
 246              $this->setHeaders('Authorization', $oauthHeaderValue);
 247          } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
 248              if ($requestMethod == self::GET) {
 249                  require_once  'Zend/Oauth/Exception.php';
 250                  throw new Zend_Oauth_Exception(
 251                      'The client is configured to'
 252                      . ' pass OAuth parameters through a POST body but request method'
 253                      . ' is set to GET'
 254                  );
 255              }
 256              $raw = $this->getToken()->toQueryString(
 257                  $this->getUri(true),
 258                  $this->_config,
 259                  $this->_getSignableParametersAsQueryString()
 260              );
 261              $this->setRawData($raw, 'application/x-www-form-urlencoded');
 262              $this->paramsPost = array();
 263          } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
 264              $params = $this->paramsGet;            
 265              $query = $this->getUri()->getQuery();
 266              if ($query) {
 267                  $queryParts = explode('&', $this->getUri()->getQuery());
 268                  foreach ($queryParts as $queryPart) {
 269                      $kvTuple = explode('=', $queryPart);
 270                      $params[urldecode($kvTuple[0])] =
 271                          (array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : null);
 272                  }
 273              }
 274              if (!empty($this->paramsPost)) {
 275                  $params = array_merge($params, $this->paramsPost);
 276                  $query  = $this->getToken()->toQueryString(
 277                      $this->getUri(true), $this->_config, $params
 278                  );
 279              }
 280              $query = $this->getToken()->toQueryString(
 281                  $this->getUri(true), $this->_config, $params
 282              );
 283              $this->getUri()->setQuery($query);
 284              $this->paramsGet = array();
 285          } else {
 286              require_once  'Zend/Oauth/Exception.php';
 287              throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
 288          }
 289      }
 290  
 291      /**
 292       * Collect all signable parameters into a single array across query string
 293       * and POST body. These are returned as a properly formatted single
 294       * query string.
 295       *
 296       * @return string
 297       */
 298      protected function _getSignableParametersAsQueryString()
 299      {
 300          $params = array();
 301              if (!empty($this->paramsGet)) {
 302                  $params = array_merge($params, $this->paramsGet);
 303                  $query  = $this->getToken()->toQueryString(
 304                      $this->getUri(true), $this->_config, $params
 305                  );
 306              }
 307              if (!empty($this->paramsPost)) {
 308                  $params = array_merge($params, $this->paramsPost);
 309                  $query  = $this->getToken()->toQueryString(
 310                      $this->getUri(true), $this->_config, $params
 311                  );
 312              }
 313              return $params;
 314      }
 315  
 316      /**
 317       * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
 318       * which holds all configuration methods and values this object also presents
 319       * as it's API.
 320       *
 321       * @param  string $method
 322       * @param  array $args
 323       * @return mixed
 324       * @throws Zend_Oauth_Exception if method does not exist in config object
 325       */
 326      public function __call($method, array $args)
 327      {
 328          if (!method_exists($this->_config, $method)) {
 329              require_once  'Zend/Oauth/Exception.php';
 330              throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
 331          }
 332          return call_user_func_array(array($this->_config,$method), $args);
 333      }
 334  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1