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';
28 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
29 * @license http://framework.zend.com/license/new-bsd New BSD License
31 abstract class Zend_Oauth_Token
36 const TOKEN_PARAM_KEY = 'oauth_token';
37 const TOKEN_SECRET_PARAM_KEY = 'oauth_token_secret';
38 const TOKEN_PARAM_CALLBACK_CONFIRMED = 'oauth_callback_confirmed';
46 protected $_params = array();
49 * OAuth response object
51 * @var Zend_Http_Response
53 protected $_response = null;
56 * @var Zend_Oauth_Http_Utility
58 protected $_httpUtility = null;
61 * Constructor; basic setup for any Token subclass.
63 * @param null|Zend_Http_Response $response
64 * @param null|Zend_Oauth_Http_Utility $utility
67 public function __construct(
68 Zend_Http_Response $response = null,
69 Zend_Oauth_Http_Utility $utility = null
71 if ($response !== null) {
72 $this->_response = $response;
73 $params = $this->_parseParameters($response);
74 if (count($params) > 0) {
75 $this->setParams($params);
78 if ($utility !== null) {
79 $this->_httpUtility = $utility;
81 $this->_httpUtility = new Zend_Oauth_Http_Utility;
86 * Attempts to validate the Token parsed from the HTTP response - really
87 * it's just very basic existence checks which are minimal.
91 public function isValid()
93 if (isset($this->_params[self::TOKEN_PARAM_KEY])
94 && !empty($this->_params[self::TOKEN_PARAM_KEY])
95 && isset($this->_params[self::TOKEN_SECRET_PARAM_KEY])
103 * Return the HTTP response object used to initialise this instance.
105 * @return Zend_Http_Response
107 public function getResponse()
109 return $this->_response;
113 * Sets the value for the this Token's secret which may be used when signing
114 * requests with this Token.
116 * @param string $secret
117 * @return Zend_Oauth_Token
119 public function setTokenSecret($secret)
121 $this->setParam(self::TOKEN_SECRET_PARAM_KEY, $secret);
126 * Retrieve this Token's secret which may be used when signing
127 * requests with this Token.
131 public function getTokenSecret()
133 return $this->getParam(self::TOKEN_SECRET_PARAM_KEY);
137 * Sets the value for a parameter (e.g. token secret or other) and run
138 * a simple filter to remove any trailing newlines.
141 * @param string $value
142 * @return Zend_Oauth_Token
144 public function setParam($key, $value)
146 $this->_params[$key] = trim($value, "\n");
151 * Sets the value for some parameters (e.g. token secret or other) and run
152 * a simple filter to remove any trailing newlines.
154 * @param array $params
155 * @return Zend_Oauth_Token
157 public function setParams(array $params)
159 foreach ($params as $key=>$value) {
160 $this->setParam($key, $value);
166 * Get the value for a parameter (e.g. token secret or other).
171 public function getParam($key)
173 if (isset($this->_params[$key])) {
174 return $this->_params[$key];
180 * Sets the value for a Token.
182 * @param string $token
183 * @return Zend_Oauth_Token
185 public function setToken($token)
187 $this->setParam(self::TOKEN_PARAM_KEY, $token);
192 * Gets the value for a Token.
196 public function getToken()
198 return $this->getParam(self::TOKEN_PARAM_KEY);
202 * Generic accessor to enable access as public properties.
206 public function __get($key)
208 return $this->getParam($key);
212 * Generic mutator to enable access as public properties.
215 * @param string $value
218 public function __set($key, $value)
220 $this->setParam($key, $value);
224 * Convert Token to a string, specifically a raw encoded query string.
228 public function toString()
230 return $this->_httpUtility->toEncodedQueryString($this->_params);
234 * Convert Token to a string, specifically a raw encoded query string.
235 * Aliases to self::toString()
239 public function __toString()
241 return $this->toString();
245 * Parse a HTTP response body and collect returned parameters
246 * as raw url decoded key-value pairs in an associative array.
248 * @param Zend_Http_Response $response
251 protected function _parseParameters(Zend_Http_Response $response)
254 $body = $response->getBody();
259 // validate body based on acceptable characters...todo
260 $parts = explode('&', $body);
261 foreach ($parts as $kvpair) {
262 $pair = explode('=', $kvpair);
263 $params[rawurldecode($pair[0])] = rawurldecode($pair[1]);
269 * Limit serialisation stored data to the parameters
271 public function __sleep()
273 return array('_params');
277 * After serialisation, re-instantiate a HTTP utility class for use
279 public function __wakeup()
281 if ($this->_httpUtility === null) {
282 $this->_httpUtility = new Zend_Oauth_Http_Utility;