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