| 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 OAuthRequest |
| 12 |
* |
| 13 |
* @package Hybridauth\Thirdparty\OAuth |
| 14 |
*/ |
| 15 |
class OAuthRequest |
| 16 |
{ |
| 17 |
public $parameters; |
| 18 |
public $http_method; |
| 19 |
public $http_url; |
| 20 |
// for debug purposes |
| 21 |
public $base_string; |
| 22 |
public static $version = '1.0'; |
| 23 |
public static $POST_INPUT = 'php://input'; |
| 24 |
|
| 25 |
/** |
| 26 |
* OAuthRequest constructor. |
| 27 |
* |
| 28 |
* @param $http_method |
| 29 |
* @param $http_url |
| 30 |
* @param null $parameters |
| 31 |
*/ |
| 32 |
public function __construct($http_method, $http_url, $parameters = null) |
| 33 |
{ |
| 34 |
$parameters = ($parameters) ? $parameters : array(); |
| 35 |
$parameters = array_merge(OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters); |
| 36 |
$this->parameters = $parameters; |
| 37 |
$this->http_method = $http_method; |
| 38 |
$this->http_url = $http_url; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* attempt to build up a request from what was passed to the server |
| 43 |
* |
| 44 |
* @param null $http_method |
| 45 |
* @param null $http_url |
| 46 |
* @param null $parameters |
| 47 |
* |
| 48 |
* @return OAuthRequest |
| 49 |
*/ |
| 50 |
public static function from_request($http_method = null, $http_url = null, $parameters = null) |
| 51 |
{ |
| 52 |
$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https'; |
| 53 |
$http_url = ($http_url) ? $http_url : $scheme . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']; |
| 54 |
$http_method = ($http_method) ? $http_method : $_SERVER['REQUEST_METHOD']; |
| 55 |
|
| 56 |
// We weren't handed any parameters, so let's find the ones relevant to |
| 57 |
// this request. |
| 58 |
// If you run XML-RPC or similar you should use this to provide your own |
| 59 |
// parsed parameter-list |
| 60 |
if (!$parameters) { |
| 61 |
// Find request headers |
| 62 |
$request_headers = OAuthUtil::get_headers(); |
| 63 |
|
| 64 |
// Parse the query-string to find GET parameters |
| 65 |
$parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']); |
| 66 |
|
| 67 |
// It's a POST request of the proper content-type, so parse POST |
| 68 |
// parameters and add those overriding any duplicates from GET |
| 69 |
if ($http_method == "POST" && isset($request_headers['Content-Type']) && strstr($request_headers['Content-Type'], 'application/x-www-form-urlencoded')) { |
| 70 |
$post_data = OAuthUtil::parse_parameters(file_get_contents(self::$POST_INPUT)); |
| 71 |
$parameters = array_merge($parameters, $post_data); |
| 72 |
} |
| 73 |
|
| 74 |
// We have a Authorization-header with OAuth data. Parse the header |
| 75 |
// and add those overriding any duplicates from GET or POST |
| 76 |
if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') { |
| 77 |
$header_parameters = OAuthUtil::split_header($request_headers['Authorization']); |
| 78 |
$parameters = array_merge($parameters, $header_parameters); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return new OAuthRequest($http_method, $http_url, $parameters); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* pretty much a helper function to set up the request |
| 87 |
* @param $consumer |
| 88 |
* @param $token |
| 89 |
* @param $http_method |
| 90 |
* @param $http_url |
| 91 |
* @param null $parameters |
| 92 |
* @return OAuthRequest |
| 93 |
*/ |
| 94 |
public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = null) |
| 95 |
{ |
| 96 |
$parameters = ($parameters) ? $parameters : array(); |
| 97 |
$defaults = array( |
| 98 |
"oauth_version" => OAuthRequest::$version, |
| 99 |
"oauth_nonce" => OAuthRequest::generate_nonce(), |
| 100 |
"oauth_timestamp" => OAuthRequest::generate_timestamp(), |
| 101 |
"oauth_consumer_key" => $consumer->key |
| 102 |
); |
| 103 |
if ($token) { |
| 104 |
$defaults['oauth_token'] = $token->key; |
| 105 |
} |
| 106 |
|
| 107 |
$parameters = array_merge($defaults, $parameters); |
| 108 |
|
| 109 |
return new OAuthRequest($http_method, $http_url, $parameters); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param $name |
| 114 |
* @param $value |
| 115 |
* @param bool $allow_duplicates |
| 116 |
*/ |
| 117 |
public function set_parameter($name, $value, $allow_duplicates = true) |
| 118 |
{ |
| 119 |
if ($allow_duplicates && isset($this->parameters[$name])) { |
| 120 |
// We have already added parameter(s) with this name, so add to the list |
| 121 |
if (is_scalar($this->parameters[$name])) { |
| 122 |
// This is the first duplicate, so transform scalar (string) |
| 123 |
// into an array so we can add the duplicates |
| 124 |
$this->parameters[$name] = array( |
| 125 |
$this->parameters[$name] |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
$this->parameters[$name][] = $value; |
| 130 |
} else { |
| 131 |
$this->parameters[$name] = $value; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param $name |
| 137 |
* |
| 138 |
* @return |null |
| 139 |
*/ |
| 140 |
public function get_parameter($name) |
| 141 |
{ |
| 142 |
return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public function get_parameters() |
| 149 |
{ |
| 150 |
return $this->parameters; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @param $name |
| 155 |
*/ |
| 156 |
public function unset_parameter($name) |
| 157 |
{ |
| 158 |
unset($this->parameters[$name]); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* The request parameters, sorted and concatenated into a normalized string. |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
public function get_signable_parameters() |
| 167 |
{ |
| 168 |
$params = []; |
| 169 |
|
| 170 |
// Grab all parameters. |
| 171 |
foreach ($this->parameters as $key_param => $value_param) { |
| 172 |
// Process only scalar values. |
| 173 |
if (is_scalar($value_param)) { |
| 174 |
$params[$key_param] = $value_param; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// Remove oauth_signature if present |
| 179 |
// Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.") |
| 180 |
if (isset($params['oauth_signature'])) { |
| 181 |
unset($params['oauth_signature']); |
| 182 |
} |
| 183 |
|
| 184 |
return OAuthUtil::build_http_query($params); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the base string of this request |
| 189 |
* |
| 190 |
* The base string defined as the method, the url |
| 191 |
* and the parameters (normalized), each urlencoded |
| 192 |
* and the concated with &. |
| 193 |
*/ |
| 194 |
public function get_signature_base_string() |
| 195 |
{ |
| 196 |
$parts = array( |
| 197 |
$this->get_normalized_http_method(), |
| 198 |
$this->get_normalized_http_url(), |
| 199 |
$this->get_signable_parameters() |
| 200 |
); |
| 201 |
|
| 202 |
$parts = OAuthUtil::urlencode_rfc3986($parts); |
| 203 |
|
| 204 |
return implode('&', $parts); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* just uppercases the http method |
| 209 |
*/ |
| 210 |
public function get_normalized_http_method() |
| 211 |
{ |
| 212 |
return strtoupper($this->http_method); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* parses the url and rebuilds it to be |
| 217 |
* scheme://host/path |
| 218 |
*/ |
| 219 |
public function get_normalized_http_url() |
| 220 |
{ |
| 221 |
$parts = parse_url($this->http_url); |
| 222 |
|
| 223 |
$scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http'; |
| 224 |
$port = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80'); |
| 225 |
$host = (isset($parts['host'])) ? strtolower($parts['host']) : ''; |
| 226 |
$path = (isset($parts['path'])) ? $parts['path'] : ''; |
| 227 |
|
| 228 |
if (($scheme == 'https' && $port != '443') || ($scheme == 'http' && $port != '80')) { |
| 229 |
$host = "$host:$port"; |
| 230 |
} |
| 231 |
return "$scheme://$host$path"; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* builds a url usable for a GET request |
| 236 |
*/ |
| 237 |
public function to_url() |
| 238 |
{ |
| 239 |
$post_data = $this->to_postdata(); |
| 240 |
$out = $this->get_normalized_http_url(); |
| 241 |
if ($post_data) { |
| 242 |
$out .= '?' . $post_data; |
| 243 |
} |
| 244 |
return $out; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* builds the data one would send in a POST request |
| 249 |
*/ |
| 250 |
public function to_postdata() |
| 251 |
{ |
| 252 |
return OAuthUtil::build_http_query($this->parameters); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* builds the Authorization: header |
| 257 |
* @param null $realm |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
public function to_header($realm = null) |
| 261 |
{ |
| 262 |
$first = true; |
| 263 |
if ($realm) { |
| 264 |
$out = 'OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"'; |
| 265 |
$first = false; |
| 266 |
} else { |
| 267 |
$out = 'OAuth'; |
| 268 |
} |
| 269 |
|
| 270 |
foreach ($this->parameters as $k => $v) { |
| 271 |
if (substr($k, 0, 5) != "oauth") { |
| 272 |
continue; |
| 273 |
} |
| 274 |
if (is_array($v)) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
$out .= ($first) ? ' ' : ','; |
| 278 |
$out .= OAuthUtil::urlencode_rfc3986($k) . '="' . OAuthUtil::urlencode_rfc3986($v) . '"'; |
| 279 |
$first = false; |
| 280 |
} |
| 281 |
|
| 282 |
return array( |
| 283 |
'Authorization' => $out |
| 284 |
); //- hacked into this to make it return an array. 15/11/2014. |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* @return string |
| 289 |
*/ |
| 290 |
public function __toString() |
| 291 |
{ |
| 292 |
return $this->to_url(); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* @param $signature_method |
| 297 |
* @param $consumer |
| 298 |
* @param $token |
| 299 |
*/ |
| 300 |
public function sign_request($signature_method, $consumer, $token) |
| 301 |
{ |
| 302 |
$this->set_parameter("oauth_signature_method", $signature_method->get_name(), false); |
| 303 |
$signature = $this->build_signature($signature_method, $consumer, $token); |
| 304 |
$this->set_parameter("oauth_signature", $signature, false); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* @param $signature_method |
| 309 |
* @param $consumer |
| 310 |
* @param $token |
| 311 |
* |
| 312 |
* @return mixed |
| 313 |
*/ |
| 314 |
public function build_signature($signature_method, $consumer, $token) |
| 315 |
{ |
| 316 |
$signature = $signature_method->build_signature($this, $consumer, $token); |
| 317 |
return $signature; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* util function: current timestamp |
| 322 |
*/ |
| 323 |
private static function generate_timestamp() |
| 324 |
{ |
| 325 |
return time(); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* util function: current nonce |
| 330 |
*/ |
| 331 |
private static function generate_nonce() |
| 332 |
{ |
| 333 |
$mt = microtime(); |
| 334 |
$rand = mt_rand(); |
| 335 |
|
| 336 |
return md5($mt . $rand); // md5s look nicer than numbers |
| 337 |
} |
| 338 |
} |
| 339 |
|