PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/OAuth/Request.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 371 lines | 153 code | 33 blank | 185 comment | 13 complexity | 6e227f562015d2098f8a9499b1fe378b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * OAuth Request
  4. *
  5. * Adapted from Andy Smith's OAuth library for PHP
  6. *
  7. * @link http://oauth.net/core/1.0
  8. * @link http://oauth.googlecode.com/svn/spec/ext/consumer_request/1.0/drafts/1/spec.html
  9. * @link http://oauth.googlecode.com/svn/code/php/
  10. * @link http://term.ie/oauth/example/
  11. *
  12. * @package OAuth
  13. *
  14. * @author jhart
  15. * @copyright Copyright (c) 2008, Photobucket, Inc.
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. */
  18. /**
  19. * Utilities
  20. */
  21. require_once ('OAuth/Utils.php');
  22. /**
  23. * Signing methods
  24. */
  25. require_once ('OAuth/Signature.php');
  26. /**
  27. * Consumer Model
  28. */
  29. require_once ('OAuth/Consumer.php');
  30. /**
  31. * Token Model
  32. */
  33. require_once ('OAuth/Token.php');
  34. /**
  35. * OAuth Request Representation
  36. *
  37. * @package OAuth
  38. */
  39. class OAuth_Request
  40. {
  41. /**
  42. * holds all parameters for request
  43. *
  44. * @access protected
  45. * @var array
  46. */
  47. protected $parameters = array();
  48. /**
  49. * Holds HTTP method (normalized, strtoupper)
  50. *
  51. * @var string
  52. */
  53. protected $http_method = '';
  54. /**
  55. * Holds url (normalized, per function)
  56. *
  57. * @var string
  58. */
  59. protected $http_url = '';
  60. /**
  61. * generated base string for this request (debugging)
  62. *
  63. * @var string
  64. */
  65. public $base_string = '';
  66. /**
  67. * generated key string for this request (debugging)
  68. *
  69. * @var string
  70. */
  71. public $key_string = '';
  72. /**
  73. * Allowed version that we support with this library
  74. *
  75. * @var string
  76. */
  77. public static $version = '1.0';
  78. /**
  79. * Request Constructor
  80. *
  81. * @uses getNormalizedHttpUrl()
  82. * @param string $http_method http method
  83. * @param string $http_url url
  84. * @param array $parameters array of parameters
  85. */
  86. public function __construct($http_method, $http_url, $parameters = null)
  87. {
  88. @$parameters or $parameters = array();
  89. $this->parameters = $parameters;
  90. $this->http_method = strtoupper($http_method);
  91. $this->http_url = self :: getNormalizedHttpUrl($http_url);
  92. }
  93. /**
  94. * build up a request from what was passed to the server
  95. *
  96. * @param string $http_method [optional, default=_SERVER[HTTP_METHOD]] HTTP method (get|put|post|delete)
  97. * @param string $http_url [optional, default=http://_SERVER[HTTP_HOST]._SERVER[REQUEST_URI]] request url to sign
  98. * @param array $parameters [optional, default=_REQUEST] parameters to sign
  99. * @return self
  100. */
  101. public static function fromRequest($http_method = null, $http_url = null, $parameters = null)
  102. {
  103. @$http_url or $http_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  104. @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
  105. if ($parameters)
  106. {
  107. $req = new self($http_method, $http_url, $parameters);
  108. }
  109. else
  110. {
  111. $parameters = array_diff_assoc($_REQUEST, $_COOKIE);
  112. $request_headers = apache_request_headers();
  113. if (array_key_exists('Authorization', $request_headers) && substr($request_headers['Authorization'], 0, 5) == 'OAuth')
  114. {
  115. $header_parameters = self :: splitHeader($request_headers['Authorization']);
  116. $parameters = array_merge($header_parameters, $parameters);
  117. }
  118. $req = new self($http_method, $http_url, $parameters);
  119. }
  120. return $req;
  121. }
  122. /**
  123. * build up a request form just a URL+querystring
  124. *
  125. * @param string $url a whole url, querystring included.
  126. * @param string $http_method [optional, default=GET] http method
  127. * @param OAuth_Consumer $consumer [optional] consumer to sign with
  128. * @param OAuth_Token $token [optional] token to sign with
  129. * @return self
  130. */
  131. public static function fromUrl($url, $http_method = 'GET', $consumer = null, $token = null)
  132. {
  133. $parts = parse_url($url);
  134. $qs = array();
  135. parse_str($parts['query'], $qs);
  136. if (! $consumer)
  137. {
  138. return self :: fromRequest($http_method, $url, $qs);
  139. }
  140. else
  141. {
  142. return self :: fromConsumerAndToken($consumer, $token, $http_method, $url, $qs);
  143. }
  144. }
  145. /**
  146. * Create request from consumer and token as well (for a new request)
  147. *
  148. * @param OAuth_Consumer $consumer consumer
  149. * @param OAuth_Token $token token
  150. * @param string $http_method method
  151. * @param string $http_url url
  152. * @param array $parameters parameters
  153. * @return self
  154. */
  155. public static function fromConsumerAndToken(OAuth_Consumer $consumer, $token, $http_method, $http_url, $parameters)
  156. {
  157. @$parameters or $parameters = array();
  158. $defaults = array('oauth_version' => self :: $version, 'oauth_nonce' => self :: getNonce(),
  159. 'oauth_timestamp' => self :: getTimestamp(), 'oauth_consumer_key' => $consumer->getKey());
  160. $parameters = array_merge($defaults, $parameters);
  161. if ($token)
  162. {
  163. $parameters['oauth_token'] = $token->getKey();
  164. }
  165. return new self($http_method, $http_url, $parameters);
  166. }
  167. /**
  168. * set a parameter
  169. *
  170. * @param string $name
  171. * @param string $value
  172. */
  173. public function setParameter($name, $value)
  174. {
  175. $this->parameters[$name] = $value;
  176. }
  177. /**
  178. * get a parameter
  179. *
  180. * @param string $name
  181. * @return string
  182. */
  183. public function getParameter($name)
  184. {
  185. if (! array_key_exists($name, $this->parameters))
  186. return null;
  187. return $this->parameters[$name];
  188. }
  189. /**
  190. * Get parameters array
  191. *
  192. * @return array of key=>value
  193. */
  194. public function getParameters()
  195. {
  196. return $this->parameters;
  197. }
  198. /**
  199. * normalize input url
  200. *
  201. * @param string $url url to normalize
  202. * @return string normalized url
  203. */
  204. public static function getNormalizedHttpUrl($url)
  205. {
  206. $parts = parse_url($url);
  207. $port = '';
  208. if (array_key_exists('port', $parts) && $parts['port'] != '80')
  209. {
  210. $port = ':' . $parts['port'];
  211. }
  212. return $parts['scheme'] . '://' . $parts['host'] . $port . '/' . trim($parts['path'], '/');
  213. }
  214. /**
  215. * get HTTP url in this request (normalized)
  216. *
  217. * @return string
  218. */
  219. public function getHttpUrl()
  220. {
  221. return $this->http_url;
  222. }
  223. /**
  224. * get HTTP method in this request (normalized)
  225. *
  226. * @return unknown
  227. */
  228. public function getHttpMethod()
  229. {
  230. return $this->http_method;
  231. }
  232. /**
  233. * Build whole url for request
  234. *
  235. * @uses toPostdata()
  236. * @uses getHttpUrl()
  237. * @return string http://httpurl?parameters
  238. */
  239. public function toUrl()
  240. {
  241. $out = $this->getHttpUrl() . '?';
  242. $out .= $this->toPostdata();
  243. return $out;
  244. }
  245. /**
  246. * build querystring for post or get
  247. *
  248. * @return string param=value&param=value...
  249. */
  250. public function toPostdata()
  251. {
  252. return OAuth_Utils :: normalizeKeyValueParameters($this->getParameters());
  253. }
  254. /**
  255. * Builds Authorization: header for request
  256. *
  257. * @return string Authorization: OAuth ...
  258. */
  259. public function toHeader()
  260. {
  261. $out = '"Authorization: OAuth realm="",';
  262. return $out . OAuth_Utils :: normalizeKeyValueParameters($this->getParameters(), ',');
  263. }
  264. /**
  265. * gets url
  266. *
  267. * @uses toUrl()
  268. * @return string
  269. */
  270. public function __toString()
  271. {
  272. return $this->toUrl();
  273. }
  274. /**
  275. * Signs this request - adds parameters for signature method and the signed signature
  276. *
  277. * @param string $signature_method signing method identifier
  278. * @param OAuth_Consumer $consumer to sign against
  279. * @param OAuth_Token $token to sign against
  280. */
  281. public function signRequest($signature_method, OAuth_Consumer $consumer, $token = null)
  282. {
  283. if (! ($method = OAuth_Signature :: getSignatureMethod($signature_method)))
  284. return false;
  285. $this->setParameter('oauth_signature_method', $method->getMethodName());
  286. $consumer_secret = $consumer->getSecret();
  287. $token_secret = ($token) ? $token->getSecret() : '';
  288. $signature = $method->signRequest($this, $consumer_secret, $token_secret);
  289. $this->setParameter('oauth_signature', $signature);
  290. }
  291. /**
  292. * Get current timestamp
  293. *
  294. * @return int
  295. */
  296. public static function getTimestamp()
  297. {
  298. //return 1191242096; //example from spec
  299. return time();
  300. }
  301. /**
  302. * get current nonce
  303. *
  304. * @return string
  305. */
  306. public static function getNonce()
  307. {
  308. //return 'kllo9940pd9333jh'; //example from spec
  309. $mt = microtime();
  310. $rand = mt_rand();
  311. return md5($mt . $rand); // md5s look nicer than numbers
  312. }
  313. /**
  314. * util function for turning the Authorization: header into
  315. * parameters, has to do some unescaping
  316. *
  317. * @param string $header string to split up
  318. * @return array array of oauth params
  319. */
  320. public static function splitHeader($header)
  321. {
  322. // error cases: commas in parameter values
  323. $parts = explode(',', $header);
  324. $out = array();
  325. foreach ($parts as $param)
  326. {
  327. $param = trim($param);
  328. // skip the 'realm' param, nobody ever uses it anyway
  329. if (substr($param, 0, 5) != 'oauth')
  330. continue;
  331. $param_parts = explode('=', $param);
  332. // rawurldecode() used because urldecode() will turn a '+' in the
  333. // value into a space
  334. $out[OAuth_Utils :: urldecodeRFC3986($param_parts[0])] = OAuth_Utils :: urldecodeRFC3986_UTF8($param_parts[1]);
  335. }
  336. return $out;
  337. }
  338. }