2 namespace PayPal\Auth\Oauth;
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);
14 /*$tmp1=str_replace('%7E', '~', rawurlencode($input));
15 $tmp2= str_replace(".","%2E",$tmp1);
24 public static function parseQueryString($str) {
26 $pairs = explode("&", $str);
27 foreach ($pairs as $pair) {
28 list($k, $v) = array_map("urldecode", explode("=", $pair));
33 //parses string to associative array -modified for PayPal Signature
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);
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) {
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]);
54 if (isset($params['realm'])) {
55 unset($params['realm']);
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();
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
73 foreach ($headers AS $key => $value) {
77 ucwords(strtolower(str_replace("-", " ", $key)))
82 // otherwise we don't have apache and are just going to have to hope
83 // that $_SERVER actually contains what we need
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'];
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
98 ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
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();
113 $pairs = explode('&', $input);
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]) : '';
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
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]);
131 $parsed_parameters[$parameter][] = $value;
133 $parsed_parameters[$parameter] = $value;
136 return $parsed_parameters;
139 public static function build_http_query($params) {
140 if (!$params) return '';
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);
147 // Parameters are sorted by name, using lexicographical byte value ordering.
148 // Ref: Spec: 9.1.1 (1)
149 uksort($params, 'strcmp');
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;
162 $pairs[] = $parameter . '=' . $value;
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);