PluginProbe
Loginizer / 1.9.9
Loginizer v1.9.9
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / lib / hybridauth / Thirdparty / OAuth / OAuthUtil.php

OAuthUtil.php in Loginizer 1.9.9, at lib/hybridauth/Thirdparty/OAuth/OAuthUtil.php

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