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