OAuthUtil

The All In One SEO Pack OAuthUtil class.

Defined (1)

The class is defined in the following location(s).

/inc/extlib/OAuth.php  
  1. class OAuthUtil { 
  2. public static function urlencode_rfc3986($input) { 
  3. if (is_array($input)) { 
  4. return array_map(array('OAuthUtil', 'urlencode_rfc3986'), $input); 
  5. } else if (is_scalar($input)) { 
  6. return str_replace( 
  7. '+',  
  8. ' ',  
  9. str_replace('%7E', '~', rawurlencode($input)) 
  10. ); 
  11. } else { 
  12. return ''; 
  13.  
  14. // This decode function isn't taking into consideration the above 
  15. // modifications to the encoding process. However, this method doesn't 
  16. // seem to be used anywhere so leaving it as is. 
  17. public static function urldecode_rfc3986($string) { 
  18. return urldecode($string); 
  19.  
  20. // Utility function for turning the Authorization: header into 
  21. // parameters, has to do some unescaping 
  22. // Can filter out any non-oauth parameters if needed (default behaviour) 
  23. // May 28th, 2010 - method updated to tjerk.meesters for a speed improvement. 
  24. // see http://code.google.com/p/oauth/issues/detail?id=163 
  25. public static function split_header($header, $only_allow_oauth_parameters = true) { 
  26. $params = array(); 
  27. if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^, ]*))/', $header, $matches)) { 
  28. foreach ($matches[1] as $i => $h) { 
  29. $params[$h] = OAuthUtil::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]); 
  30. if (isset($params['realm'])) { 
  31. unset($params['realm']); 
  32. return $params; 
  33.  
  34. // helper to try to sort out headers for people who aren't running apache 
  35. public static function get_headers() { 
  36. if (function_exists('apache_request_headers')) { 
  37. // we need this to get the actual Authorization: header 
  38. // because apache tends to tell us it doesn't exist 
  39. $headers = apache_request_headers(); 
  40.  
  41. // sanitize the output of apache_request_headers because 
  42. // we always want the keys to be Cased-Like-This and arh() 
  43. // returns the headers in the same case as they are in the 
  44. // request 
  45. $out = array(); 
  46. foreach ($headers AS $key => $value) { 
  47. $key = str_replace( 
  48. " ",  
  49. "-",  
  50. ucwords(strtolower(str_replace("-", " ", $key))) 
  51. ); 
  52. $out[$key] = $value; 
  53. } else { 
  54. // otherwise we don't have apache and are just going to have to hope 
  55. // that $_SERVER actually contains what we need 
  56. $out = array(); 
  57. if( isset($_SERVER['CONTENT_TYPE']) ) 
  58. $out['Content-Type'] = $_SERVER['CONTENT_TYPE']; 
  59. if( isset($_ENV['CONTENT_TYPE']) ) 
  60. $out['Content-Type'] = $_ENV['CONTENT_TYPE']; 
  61.  
  62. foreach ($_SERVER as $key => $value) { 
  63. if (substr($key, 0, 5) == "HTTP_") { 
  64. // this is chaos, basically it is just there to capitalize the first 
  65. // letter of every word that is not an initial HTTP and strip HTTP 
  66. // code from przemek 
  67. $key = str_replace( 
  68. " ",  
  69. "-",  
  70. ucwords(strtolower(str_replace("_", " ", substr($key, 5)))) 
  71. ); 
  72. $out[$key] = $value; 
  73. return $out; 
  74.  
  75. // This function takes a input like a=b&a=c&d=e and returns the parsed 
  76. // parameters like this 
  77. // array('a' => array('b', 'c'), 'd' => 'e') 
  78. public static function parse_parameters( $input ) { 
  79. if (!isset($input) || !$input) return array(); 
  80.  
  81. $pairs = explode('&', $input); 
  82.  
  83. $parsed_parameters = array(); 
  84. foreach ($pairs as $pair) { 
  85. $split = explode('=', $pair, 2); 
  86. $parameter = OAuthUtil::urldecode_rfc3986($split[0]); 
  87. $value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : ''; 
  88.  
  89. if (isset($parsed_parameters[$parameter])) { 
  90. // We have already recieved parameter(s) with this name, so add to the list 
  91. // of parameters with this name 
  92.  
  93. if (is_scalar($parsed_parameters[$parameter])) { 
  94. // This is the first duplicate, so transform scalar (string) into an array 
  95. // so we can add the duplicates 
  96. $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]); 
  97.  
  98. $parsed_parameters[$parameter][] = $value; 
  99. } else { 
  100. $parsed_parameters[$parameter] = $value; 
  101. return $parsed_parameters; 
  102.  
  103. public static function build_http_query($params) { 
  104. if (!$params) return ''; 
  105.  
  106. // Urlencode both keys and values 
  107. $keys = OAuthUtil::urlencode_rfc3986(array_keys($params)); 
  108. $values = OAuthUtil::urlencode_rfc3986(array_values($params)); 
  109. $params = array_combine($keys, $values); 
  110.  
  111. // Parameters are sorted by name, using lexicographical byte value ordering. 
  112. // Ref: Spec: 9.1.1 (1) 
  113. uksort($params, 'strcmp'); 
  114.  
  115. $pairs = array(); 
  116. foreach ($params as $parameter => $value) { 
  117. if (is_array($value)) { 
  118. // If two or more parameters share the same name, they are sorted by their value 
  119. // Ref: Spec: 9.1.1 (1) 
  120. // June 12th, 2010 - changed to sort because of issue 164 by hidetaka 
  121. sort($value, SORT_STRING); 
  122. foreach ($value as $duplicate_value) { 
  123. $pairs[] = $parameter . '=' . $duplicate_value; 
  124. } else { 
  125. $pairs[] = $parameter . '=' . $value; 
  126. // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61) 
  127. // Each name-value pair is separated by an '&' character (ASCII code 38) 
  128. return implode('&', $pairs);