PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Oauth/Token.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 285 lines | 119 code | 23 blank | 143 comment | 10 complexity | 408fa54cec9ebf5620ba907e4f74fad6 MD5 | raw file
  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 license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Oauth
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Token.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** Zend_Oauth_Http_Utility */
  22. require_once 'Zend/Oauth/Http/Utility.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Oauth
  26. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_Oauth_Token
  30. {
  31. /**@+
  32. * Token constants
  33. */
  34. const TOKEN_PARAM_KEY = 'oauth_token';
  35. const TOKEN_SECRET_PARAM_KEY = 'oauth_token_secret';
  36. const TOKEN_PARAM_CALLBACK_CONFIRMED = 'oauth_callback_confirmed';
  37. /**@-*/
  38. /**
  39. * Token parameters
  40. *
  41. * @var array
  42. */
  43. protected $_params = array();
  44. /**
  45. * OAuth response object
  46. *
  47. * @var Zend_Http_Response
  48. */
  49. protected $_response = null;
  50. /**
  51. * @var Zend_Oauth_Http_Utility
  52. */
  53. protected $_httpUtility = null;
  54. /**
  55. * Constructor; basic setup for any Token subclass.
  56. *
  57. * @param null|Zend_Http_Response $response
  58. * @param null|Zend_Oauth_Http_Utility $utility
  59. * @return void
  60. */
  61. public function __construct(
  62. Zend_Http_Response $response = null,
  63. Zend_Oauth_Http_Utility $utility = null
  64. ) {
  65. if ($response !== null) {
  66. $this->_response = $response;
  67. $params = $this->_parseParameters($response);
  68. if (count($params) > 0) {
  69. $this->setParams($params);
  70. }
  71. }
  72. if ($utility !== null) {
  73. $this->_httpUtility = $utility;
  74. } else {
  75. $this->_httpUtility = new Zend_Oauth_Http_Utility;
  76. }
  77. }
  78. /**
  79. * Attempts to validate the Token parsed from the HTTP response - really
  80. * it's just very basic existence checks which are minimal.
  81. *
  82. * @return bool
  83. */
  84. public function isValid()
  85. {
  86. if (isset($this->_params[self::TOKEN_PARAM_KEY])
  87. && !empty($this->_params[self::TOKEN_PARAM_KEY])
  88. && isset($this->_params[self::TOKEN_SECRET_PARAM_KEY])
  89. ) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. /**
  95. * Return the HTTP response object used to initialise this instance.
  96. *
  97. * @return Zend_Http_Response
  98. */
  99. public function getResponse()
  100. {
  101. return $this->_response;
  102. }
  103. /**
  104. * Sets the value for the this Token's secret which may be used when signing
  105. * requests with this Token.
  106. *
  107. * @param string $secret
  108. * @return Zend_Oauth_Token
  109. */
  110. public function setTokenSecret($secret)
  111. {
  112. $this->setParam(self::TOKEN_SECRET_PARAM_KEY, $secret);
  113. return $this;
  114. }
  115. /**
  116. * Retrieve this Token's secret which may be used when signing
  117. * requests with this Token.
  118. *
  119. * @return string
  120. */
  121. public function getTokenSecret()
  122. {
  123. return $this->getParam(self::TOKEN_SECRET_PARAM_KEY);
  124. }
  125. /**
  126. * Sets the value for a parameter (e.g. token secret or other) and run
  127. * a simple filter to remove any trailing newlines.
  128. *
  129. * @param string $key
  130. * @param string $value
  131. * @return Zend_Oauth_Token
  132. */
  133. public function setParam($key, $value)
  134. {
  135. $this->_params[$key] = trim($value, "\n");
  136. return $this;
  137. }
  138. /**
  139. * Sets the value for some parameters (e.g. token secret or other) and run
  140. * a simple filter to remove any trailing newlines.
  141. *
  142. * @param array $params
  143. * @return Zend_Oauth_Token
  144. */
  145. public function setParams(array $params)
  146. {
  147. foreach ($params as $key=>$value) {
  148. $this->setParam($key, $value);
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Get the value for a parameter (e.g. token secret or other).
  154. *
  155. * @param string $key
  156. * @return mixed
  157. */
  158. public function getParam($key)
  159. {
  160. if (isset($this->_params[$key])) {
  161. return $this->_params[$key];
  162. }
  163. return null;
  164. }
  165. /**
  166. * Sets the value for a Token.
  167. *
  168. * @param string $token
  169. * @return Zend_Oauth_Token
  170. */
  171. public function setToken($token)
  172. {
  173. $this->setParam(self::TOKEN_PARAM_KEY, $token);
  174. return $this;
  175. }
  176. /**
  177. * Gets the value for a Token.
  178. *
  179. * @return string
  180. */
  181. public function getToken()
  182. {
  183. return $this->getParam(self::TOKEN_PARAM_KEY);
  184. }
  185. /**
  186. * Generic accessor to enable access as public properties.
  187. *
  188. * @return string
  189. */
  190. public function __get($key)
  191. {
  192. return $this->getParam($key);
  193. }
  194. /**
  195. * Generic mutator to enable access as public properties.
  196. *
  197. * @param string $key
  198. * @param string $value
  199. * @return void
  200. */
  201. public function __set($key, $value)
  202. {
  203. $this->setParam($key, $value);
  204. }
  205. /**
  206. * Convert Token to a string, specifically a raw encoded query string.
  207. *
  208. * @return string
  209. */
  210. public function toString()
  211. {
  212. return $this->_httpUtility->toEncodedQueryString($this->_params);
  213. }
  214. /**
  215. * Convert Token to a string, specifically a raw encoded query string.
  216. * Aliases to self::toString()
  217. *
  218. * @return string
  219. */
  220. public function __toString()
  221. {
  222. return $this->toString();
  223. }
  224. /**
  225. * Parse a HTTP response body and collect returned parameters
  226. * as raw url decoded key-value pairs in an associative array.
  227. *
  228. * @param Zend_Http_Response $response
  229. * @return array
  230. */
  231. protected function _parseParameters(Zend_Http_Response $response)
  232. {
  233. $params = array();
  234. $body = $response->getBody();
  235. if (empty($body)) {
  236. return;
  237. }
  238. // validate body based on acceptable characters...todo
  239. $parts = explode('&', $body);
  240. foreach ($parts as $kvpair) {
  241. $pair = explode('=', $kvpair);
  242. $params[rawurldecode($pair[0])] = rawurldecode($pair[1]);
  243. }
  244. return $params;
  245. }
  246. /**
  247. * Limit serialisation stored data to the parameters
  248. */
  249. public function __sleep()
  250. {
  251. return array('_params');
  252. }
  253. /**
  254. * After serialisation, re-instantiate a HTTP utility class for use
  255. */
  256. public function __wakeup()
  257. {
  258. if ($this->_httpUtility === null) {
  259. $this->_httpUtility = new Zend_Oauth_Http_Utility;
  260. }
  261. }
  262. }