PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MaxCDN/OAuth/OAuthUtil.php

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