File indexing completed on 2025-01-26 05:29:43
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Oauth 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** Zend_Oauth_Http_Utility */ 0023 // require_once 'Zend/Oauth/Http/Utility.php'; 0024 0025 /** 0026 * @category Zend 0027 * @package Zend_Oauth 0028 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0029 * @license http://framework.zend.com/license/new-bsd New BSD License 0030 */ 0031 abstract class Zend_Oauth_Token 0032 { 0033 /**@+ 0034 * Token constants 0035 */ 0036 const TOKEN_PARAM_KEY = 'oauth_token'; 0037 const TOKEN_SECRET_PARAM_KEY = 'oauth_token_secret'; 0038 const TOKEN_PARAM_CALLBACK_CONFIRMED = 'oauth_callback_confirmed'; 0039 /**@-*/ 0040 0041 /** 0042 * Token parameters 0043 * 0044 * @var array 0045 */ 0046 protected $_params = array(); 0047 0048 /** 0049 * OAuth response object 0050 * 0051 * @var Zend_Http_Response 0052 */ 0053 protected $_response = null; 0054 0055 /** 0056 * @var Zend_Oauth_Http_Utility 0057 */ 0058 protected $_httpUtility = null; 0059 0060 /** 0061 * Constructor; basic setup for any Token subclass. 0062 * 0063 * @param null|Zend_Http_Response $response 0064 * @param null|Zend_Oauth_Http_Utility $utility 0065 * @return void 0066 */ 0067 public function __construct( 0068 Zend_Http_Response $response = null, 0069 Zend_Oauth_Http_Utility $utility = null 0070 ) { 0071 if ($response !== null) { 0072 $this->_response = $response; 0073 $params = $this->_parseParameters($response); 0074 if (count($params) > 0) { 0075 $this->setParams($params); 0076 } 0077 } 0078 if ($utility !== null) { 0079 $this->_httpUtility = $utility; 0080 } else { 0081 $this->_httpUtility = new Zend_Oauth_Http_Utility; 0082 } 0083 } 0084 0085 /** 0086 * Attempts to validate the Token parsed from the HTTP response - really 0087 * it's just very basic existence checks which are minimal. 0088 * 0089 * @return bool 0090 */ 0091 public function isValid() 0092 { 0093 if (isset($this->_params[self::TOKEN_PARAM_KEY]) 0094 && !empty($this->_params[self::TOKEN_PARAM_KEY]) 0095 && isset($this->_params[self::TOKEN_SECRET_PARAM_KEY]) 0096 ) { 0097 return true; 0098 } 0099 return false; 0100 } 0101 0102 /** 0103 * Return the HTTP response object used to initialise this instance. 0104 * 0105 * @return Zend_Http_Response 0106 */ 0107 public function getResponse() 0108 { 0109 return $this->_response; 0110 } 0111 0112 /** 0113 * Sets the value for the this Token's secret which may be used when signing 0114 * requests with this Token. 0115 * 0116 * @param string $secret 0117 * @return Zend_Oauth_Token 0118 */ 0119 public function setTokenSecret($secret) 0120 { 0121 $this->setParam(self::TOKEN_SECRET_PARAM_KEY, $secret); 0122 return $this; 0123 } 0124 0125 /** 0126 * Retrieve this Token's secret which may be used when signing 0127 * requests with this Token. 0128 * 0129 * @return string 0130 */ 0131 public function getTokenSecret() 0132 { 0133 return $this->getParam(self::TOKEN_SECRET_PARAM_KEY); 0134 } 0135 0136 /** 0137 * Sets the value for a parameter (e.g. token secret or other) and run 0138 * a simple filter to remove any trailing newlines. 0139 * 0140 * @param string $key 0141 * @param string $value 0142 * @return Zend_Oauth_Token 0143 */ 0144 public function setParam($key, $value) 0145 { 0146 $this->_params[$key] = trim($value, "\n"); 0147 return $this; 0148 } 0149 0150 /** 0151 * Sets the value for some parameters (e.g. token secret or other) and run 0152 * a simple filter to remove any trailing newlines. 0153 * 0154 * @param array $params 0155 * @return Zend_Oauth_Token 0156 */ 0157 public function setParams(array $params) 0158 { 0159 foreach ($params as $key=>$value) { 0160 $this->setParam($key, $value); 0161 } 0162 return $this; 0163 } 0164 0165 /** 0166 * Get the value for a parameter (e.g. token secret or other). 0167 * 0168 * @param string $key 0169 * @return mixed 0170 */ 0171 public function getParam($key) 0172 { 0173 if (isset($this->_params[$key])) { 0174 return $this->_params[$key]; 0175 } 0176 return null; 0177 } 0178 0179 /** 0180 * Sets the value for a Token. 0181 * 0182 * @param string $token 0183 * @return Zend_Oauth_Token 0184 */ 0185 public function setToken($token) 0186 { 0187 $this->setParam(self::TOKEN_PARAM_KEY, $token); 0188 return $this; 0189 } 0190 0191 /** 0192 * Gets the value for a Token. 0193 * 0194 * @return string 0195 */ 0196 public function getToken() 0197 { 0198 return $this->getParam(self::TOKEN_PARAM_KEY); 0199 } 0200 0201 /** 0202 * Generic accessor to enable access as public properties. 0203 * 0204 * @return string 0205 */ 0206 public function __get($key) 0207 { 0208 return $this->getParam($key); 0209 } 0210 0211 /** 0212 * Generic mutator to enable access as public properties. 0213 * 0214 * @param string $key 0215 * @param string $value 0216 * @return void 0217 */ 0218 public function __set($key, $value) 0219 { 0220 $this->setParam($key, $value); 0221 } 0222 0223 /** 0224 * Convert Token to a string, specifically a raw encoded query string. 0225 * 0226 * @return string 0227 */ 0228 public function toString() 0229 { 0230 return $this->_httpUtility->toEncodedQueryString($this->_params); 0231 } 0232 0233 /** 0234 * Convert Token to a string, specifically a raw encoded query string. 0235 * Aliases to self::toString() 0236 * 0237 * @return string 0238 */ 0239 public function __toString() 0240 { 0241 return $this->toString(); 0242 } 0243 0244 /** 0245 * Parse a HTTP response body and collect returned parameters 0246 * as raw url decoded key-value pairs in an associative array. 0247 * 0248 * @param Zend_Http_Response $response 0249 * @return array 0250 */ 0251 protected function _parseParameters(Zend_Http_Response $response) 0252 { 0253 $params = array(); 0254 $body = $response->getBody(); 0255 if (empty($body)) { 0256 return; 0257 } 0258 0259 // validate body based on acceptable characters...todo 0260 $parts = explode('&', $body); 0261 foreach ($parts as $kvpair) { 0262 $pair = explode('=', $kvpair); 0263 $params[rawurldecode($pair[0])] = rawurldecode($pair[1]); 0264 } 0265 return $params; 0266 } 0267 0268 /** 0269 * Limit serialisation stored data to the parameters 0270 */ 0271 public function __sleep() 0272 { 0273 return array('_params'); 0274 } 0275 0276 /** 0277 * After serialisation, re-instantiate a HTTP utility class for use 0278 */ 0279 public function __wakeup() 0280 { 0281 if ($this->_httpUtility === null) { 0282 $this->_httpUtility = new Zend_Oauth_Http_Utility; 0283 } 0284 } 0285 }