|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace HappyDemon\UitDatabank\OAuth; |
| 4 | + |
| 5 | +class OAuth |
| 6 | +{ |
| 7 | + /** |
| 8 | + * All OAuth 1.0 requests use the same basic algorithm for creating a |
| 9 | + * signature base string and a signature. The signature base string is |
| 10 | + * composed of the HTTP method being used, followed by an ampersand ("&") |
| 11 | + * and then the URL-encoded base URL being accessed, complete with path |
| 12 | + * (but not query parameters), followed by an ampersand ("&"). Then, you |
| 13 | + * take all query parameters and POST body parameters (when the POST body is |
| 14 | + * of the URL-encoded type, otherwise the POST body is ignored), including |
| 15 | + * the OAuth parameters necessary for negotiation with the request at hand, |
| 16 | + * and sort them in lexicographical order by first parameter name and then |
| 17 | + * parameter value (for duplicate parameters), all the while ensuring that |
| 18 | + * both the key and the value for each parameter are URL encoded in |
| 19 | + * isolation. Instead of using the equals ("=") sign to mark the key/value |
| 20 | + * relationship, you use the URL-encoded form of "%3D". Each parameter is |
| 21 | + * then joined by the URL-escaped ampersand sign, "%26". |
| 22 | + * |
| 23 | + * @param string $url The URL. |
| 24 | + * @param string $method The method to use. |
| 25 | + * @param array $parameters The parameters. |
| 26 | + * @return string |
| 27 | + */ |
| 28 | + public static function calculateBaseString($url, $method, array $parameters = null) |
| 29 | + { |
| 30 | + // redefine |
| 31 | + $url = (string) $url; |
| 32 | + $parameters = (array) $parameters; |
| 33 | + |
| 34 | + // init var |
| 35 | + $chunks = array(); |
| 36 | + |
| 37 | + // sort parameters by key |
| 38 | + uksort($parameters, 'strcmp'); |
| 39 | + |
| 40 | + // loop parameters |
| 41 | + foreach ($parameters as $key => $value) { |
| 42 | + // sort by value |
| 43 | + if (is_array($value)) { |
| 44 | + $parameters[$key] = natsort($value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // process queries |
| 49 | + foreach ($parameters as $key => $value) { |
| 50 | + // only add if not already in the url |
| 51 | + if (substr_count($url, $key . '=' . $value) == 0) { |
| 52 | + $chunks[] = self::urlencode_rfc3986($key) . '%3D' . |
| 53 | + self::urlencode_rfc3986($value); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // buils base |
| 58 | + $base = $method . '&'; |
| 59 | + $base .= urlencode($url); |
| 60 | + $base .= (substr_count($url, '?')) ? '%26' : '&'; |
| 61 | + $base .= implode('%26', $chunks); |
| 62 | + $base = str_replace('%3F', '&', $base); |
| 63 | + |
| 64 | + // return |
| 65 | + return $base; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * URL-encode method for internal use |
| 70 | + * |
| 71 | + * @param mixed $value The value to encode. |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + private static function urlencode_rfc3986($value) |
| 75 | + { |
| 76 | + if (is_array($value)) { |
| 77 | + return array_map(array(__CLASS__, 'urlencode_rfc3986'), $value); |
| 78 | + } else { |
| 79 | + $search = array('+', ' ', '%7E', '%'); |
| 80 | + $replace = array('%20', '%20', '~', '%25'); |
| 81 | + |
| 82 | + return str_replace($search, $replace, urlencode($value)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Build the Authorization header |
| 88 | + * @later: fix me |
| 89 | + * |
| 90 | + * @param array $parameters The parameters. |
| 91 | + * @param string $url The URL. |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + public static function calculateHeader(array $parameters, $url) |
| 95 | + { |
| 96 | + // redefine |
| 97 | + $url = (string) $url; |
| 98 | + |
| 99 | + // divide into parts |
| 100 | + $parts = parse_url($url); |
| 101 | + |
| 102 | + // init var |
| 103 | + $chunks = array(); |
| 104 | + |
| 105 | + // process queries |
| 106 | + foreach ($parameters as $key => $value) { |
| 107 | + $chunks[] = str_replace( |
| 108 | + '%25', |
| 109 | + '%', |
| 110 | + self::urlencode_rfc3986($key) . '="' . self::urlencode_rfc3986($value) . '"' |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + // build return |
| 115 | + $return = 'Authorization: OAuth realm="' . $parts['scheme'] . '://' . |
| 116 | + $parts['host']; |
| 117 | + if (isset($parts['path'])) { |
| 118 | + $return .= $parts['path']; |
| 119 | + } |
| 120 | + $return .= '", '; |
| 121 | + $return .= implode(',', $chunks); |
| 122 | + |
| 123 | + // prepend name and OAuth part |
| 124 | + return $return; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Build the signature for the data |
| 129 | + * |
| 130 | + * @param string $key The key to use for signing. |
| 131 | + * @param string $data The data that has to be signed. |
| 132 | + * @return string |
| 133 | + */ |
| 134 | + public static function hmacsha1($key, $data) |
| 135 | + { |
| 136 | + return base64_encode(hash_hmac('SHA1', $data, $key, true)); |
| 137 | + } |
| 138 | +} |
0 commit comments