nsx_trOAuthUtil

The NextScripts: Social Networks Auto-Poster nsx trOAuthUtil class.

Defined (1)

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

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