/modules/oauth/classes/kohana/oauth.php

https://github.com/Tidwell/HMXSongs-PHP-API · PHP · 217 lines · 90 code · 26 blank · 101 comment · 8 complexity · c90ca5d0926d8be8d6d6b96f251919ad MD5 · raw file

  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * OAuth Library
  4. *
  5. * @package Kohana/OAuth
  6. * @category Base
  7. * @author Kohana Team
  8. * @copyright (c) 2010 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. * @since 3.0.7
  11. */
  12. abstract class Kohana_OAuth {
  13. /**
  14. * @var string OAuth complaince version
  15. */
  16. public static $version = '1.0';
  17. /**
  18. * RFC3986 compatible version of urlencode. Passing an array will encode
  19. * all of the values in the array. Array keys will not be encoded.
  20. *
  21. * $input = OAuth::urlencode($input);
  22. *
  23. * Multi-dimensional arrays are not allowed!
  24. *
  25. * [!!] This method implements [OAuth 1.0 Spec 5.1](http://oauth.net/core/1.0/#rfc.section.5.1).
  26. *
  27. * @param mixed input string or array
  28. * @return mixed
  29. */
  30. public static function urlencode($input)
  31. {
  32. if (is_array($input))
  33. {
  34. // Encode the values of the array
  35. return array_map(array('OAuth', 'urlencode'), $input);
  36. }
  37. // Encode the input
  38. $input = rawurlencode($input);
  39. if (version_compare(PHP_VERSION, '<', '5.3'))
  40. {
  41. // rawurlencode() is RFC3986 compliant in PHP 5.3
  42. // the only difference is the encoding of tilde
  43. $input = str_replace('%7E', '~', $input);
  44. }
  45. return $input;
  46. }
  47. /**
  48. * RFC3986 complaint version of urldecode. Passing an array will decode
  49. * all of the values in the array. Array keys will not be encoded.
  50. *
  51. * $input = OAuth::urldecode($input);
  52. *
  53. * Multi-dimensional arrays are not allowed!
  54. *
  55. * [!!] This method implements [OAuth 1.0 Spec 5.1](http://oauth.net/core/1.0/#rfc.section.5.1).
  56. *
  57. * @param mixed input string or array
  58. * @return mixed
  59. */
  60. public static function urldecode($input)
  61. {
  62. if (is_array($input))
  63. {
  64. // Decode the values of the array
  65. return array_map(array('OAuth', 'urldecode'), $input);
  66. }
  67. // Decode the input
  68. return rawurldecode($input);
  69. }
  70. /**
  71. * Normalize all request parameters into a string.
  72. *
  73. * $query = OAuth::normalize_params($params);
  74. *
  75. * [!!] This method implements [OAuth 1.0 Spec 9.1.1](http://oauth.net/core/1.0/#rfc.section.9.1.1).
  76. *
  77. * @param array request parameters
  78. * @return string
  79. * @uses OAuth::urlencode
  80. */
  81. public static function normalize_params(array $params = NULL)
  82. {
  83. if ( ! $params)
  84. {
  85. // Nothing to do
  86. return '';
  87. }
  88. // Encode the parameter keys and values
  89. $keys = OAuth::urlencode(array_keys($params));
  90. $values = OAuth::urlencode(array_values($params));
  91. // Recombine the parameters
  92. $params = array_combine($keys, $values);
  93. // OAuth Spec 9.1.1 (1)
  94. // "Parameters are sorted by name, using lexicographical byte value ordering."
  95. uksort($params, 'strcmp');
  96. // Create a new query string
  97. $query = array();
  98. foreach ($params as $name => $value)
  99. {
  100. if (is_array($value))
  101. {
  102. // OAuth Spec 9.1.1 (1)
  103. // "If two or more parameters share the same name, they are sorted by their value."
  104. $value = natsort($value);
  105. foreach ($value as $duplicate)
  106. {
  107. $query[] = $name.'='.$duplicate;
  108. }
  109. }
  110. else
  111. {
  112. $query[] = $name.'='.$value;
  113. }
  114. }
  115. return implode('&', $query);
  116. }
  117. /**
  118. * Parse the query string out of the URL and return it as parameters.
  119. * All GET parameters must be removed from the request URL when building
  120. * the base string and added to the request parameters.
  121. *
  122. * // parsed parameters: array('oauth_key' => 'abcdef123456789')
  123. * list($url, $params) = OAuth::parse_url('http://example.com/oauth/access?oauth_key=abcdef123456789');
  124. *
  125. * [!!] This implements [OAuth Spec 9.1.1](http://oauth.net/core/1.0/#rfc.section.9.1.1).
  126. *
  127. * @param string URL to parse
  128. * @return array (clean_url, params)
  129. * @uses OAuth::parse_params
  130. */
  131. public static function parse_url($url)
  132. {
  133. if ($query = parse_url($url, PHP_URL_QUERY))
  134. {
  135. // Remove the query string from the URL
  136. list($url) = explode('?', $url, 2);
  137. // Parse the query string as request parameters
  138. $params = OAuth::parse_params($query);
  139. }
  140. else
  141. {
  142. // No parameters are present
  143. $params = array();
  144. }
  145. return array($url, $params);
  146. }
  147. /**
  148. * Parse the parameters in a string and return an array. Duplicates are
  149. * converted into indexed arrays.
  150. *
  151. * // Parsed: array('a' => '1', 'b' => '2', 'c' => '3')
  152. * $params = OAuth::parse_params('a=1,b=2,c=3');
  153. *
  154. * // Parsed: array('a' => array('1', '2'), 'c' => '3')
  155. * $params = OAuth::parse_params('a=1,a=2,c=3');
  156. *
  157. * @param string parameter string
  158. * @return array
  159. */
  160. public static function parse_params($params)
  161. {
  162. // Split the parameters by &
  163. $params = explode('&', trim($params));
  164. // Create an array of parsed parameters
  165. $parsed = array();
  166. foreach ($params as $param)
  167. {
  168. // Split the parameter into name and value
  169. list($name, $value) = explode('=', $param, 2);
  170. // Decode the name and value
  171. $name = OAuth::urldecode($name);
  172. $value = OAuth::urldecode($value);
  173. if (isset($parsed[$name]))
  174. {
  175. if ( ! is_array($parsed[$name]))
  176. {
  177. // Convert the parameter to an array
  178. $parsed[$name] = array($parsed[$name]);
  179. }
  180. // Add a new duplicate parameter
  181. $parsed[$name][] = $value;
  182. }
  183. else
  184. {
  185. // Add a new parameter
  186. $parsed[$name] = $value;
  187. }
  188. }
  189. return $parsed;
  190. }
  191. } // End OAuth