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 license@zend.com so we can send you a copy immediately.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Oauth_Http_Utility */
23 require_once 'Zend/Oauth/Http/Utility.php';
26 require_once 'Zend/Uri/Http.php';
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
37 * Array of all custom service parameters to be sent in the HTTP request
38 * in addition to the usual OAuth parameters.
42 protected $_parameters = array();
45 * Reference to the Zend_Oauth_Consumer instance in use.
49 protected $_consumer = null;
52 * OAuth specifies three request methods, this holds the current preferred
53 * one which by default uses the Authorization Header approach for passing
54 * OAuth parameters, and a POST body for non-OAuth custom parameters.
58 protected $_preferredRequestScheme = null;
61 * Request Method for the HTTP Request.
65 protected $_preferredRequestMethod = Zend_Oauth::POST;
68 * Instance of the general Zend_Oauth_Http_Utility class.
70 * @var Zend_Oauth_Http_Utility
72 protected $_httpUtility = null;
77 * @param Zend_Oauth_Consumer $consumer
78 * @param null|array $parameters
79 * @param null|Zend_Oauth_Http_Utility $utility
82 public function __construct(
83 Zend_Oauth_Consumer $consumer,
84 array $parameters = null,
85 Zend_Oauth_Http_Utility $utility = null
87 $this->_consumer = $consumer;
88 $this->_preferredRequestScheme = $this->_consumer->getRequestScheme();
89 if ($parameters !== null) {
90 $this->setParameters($parameters);
92 if ($utility !== null) {
93 $this->_httpUtility = $utility;
95 $this->_httpUtility = new Zend_Oauth_Http_Utility;
100 * Set a preferred HTTP request method.
102 * @param string $method
103 * @return Zend_Oauth_Http
105 public function setMethod($method)
107 if (!in_array($method, array(Zend_Oauth::POST, Zend_Oauth::GET))) {
108 require_once 'Zend/Oauth/Exception.php';
109 throw new Zend_Oauth_Exception('invalid HTTP method: ' . $method);
111 $this->_preferredRequestMethod = $method;
116 * Preferred HTTP request method accessor.
120 public function getMethod()
122 return $this->_preferredRequestMethod;
126 * Mutator to set an array of custom parameters for the HTTP request.
128 * @param array $customServiceParameters
129 * @return Zend_Oauth_Http
131 public function setParameters(array $customServiceParameters)
133 $this->_parameters = $customServiceParameters;
138 * Accessor for an array of custom parameters.
142 public function getParameters()
144 return $this->_parameters;
148 * Return the Consumer instance in use.
150 * @return Zend_Oauth_Consumer
152 public function getConsumer()
154 return $this->_consumer;
158 * Commence a request cycle where the current HTTP method and OAuth
159 * request scheme set an upper preferred HTTP request style and where
160 * failures generate a new HTTP request style further down the OAuth
161 * preference list for OAuth Request Schemes.
162 * On success, return the Request object that results for processing.
164 * @param array $params
165 * @return Zend_Http_Response
166 * @throws Zend_Oauth_Exception on HTTP request errors
167 * @todo Remove cycling?; Replace with upfront do-or-die configuration
169 public function startRequestCycle(array $params)
175 $response = $this->_attemptRequest($params);
176 } catch (Zend_Http_Client_Exception $e) {
177 require_once 'Zend/Oauth/Exception.php';
178 throw new Zend_Oauth_Exception('Error in HTTP request', null, $e);
180 if ($response !== null) {
181 $body = $response->getBody();
182 $status = $response->getStatus();
184 if ($response === null // Request failure/exception
185 || $status == 500 // Internal Server Error
186 || $status == 400 // Bad Request
187 || $status == 401 // Unauthorized
188 || empty($body) // Missing token
190 $this->_assessRequestAttempt($response);
191 $response = $this->startRequestCycle($params);
197 * Return an instance of Zend_Http_Client configured to use the Query
198 * String scheme for an OAuth driven HTTP request.
200 * @param array $params
202 * @return Zend_Http_Client
204 public function getRequestSchemeQueryStringClient(array $params, $url)
206 $client = Zend_Oauth::getHttpClient();
207 $client->setUri($url);
208 $client->getUri()->setQuery(
209 $this->_httpUtility->toEncodedQueryString($params)
211 $client->setMethod($this->_preferredRequestMethod);
216 * Manages the switch from OAuth request scheme to another lower preference
217 * scheme during a request cycle.
219 * @param Zend_Http_Response
221 * @throws Zend_Oauth_Exception if unable to retrieve valid token response
223 protected function _assessRequestAttempt(Zend_Http_Response $response = null)
225 switch ($this->_preferredRequestScheme) {
226 case Zend_Oauth::REQUEST_SCHEME_HEADER:
227 $this->_preferredRequestScheme = Zend_Oauth::REQUEST_SCHEME_POSTBODY;
229 case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
230 $this->_preferredRequestScheme = Zend_Oauth::REQUEST_SCHEME_QUERYSTRING;
233 require_once 'Zend/Oauth/Exception.php';
234 throw new Zend_Oauth_Exception(
235 'Could not retrieve a valid Token response from Token URL:'
236 . ($response !== null
237 ? PHP_EOL . $response->getBody()
238 : ' No body - check for headers')
244 * Generates a valid OAuth Authorization header based on the provided
245 * parameters and realm.
247 * @param array $params
248 * @param string $realm
251 protected function _toAuthorizationHeader(array $params, $realm = null)
253 $headerValue = array();
254 $headerValue[] = 'OAuth realm="' . $realm . '"';
255 foreach ($params as $key => $value) {
256 if (!preg_match("/^oauth_/", $key)) {
259 $headerValue[] = Zend_Oauth_Http_Utility::urlEncode($key)
261 . Zend_Oauth_Http_Utility::urlEncode($value)
264 return implode(",", $headerValue);