Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adoy/oauth2",
"description": "Light PHP wrapper for the OAuth 2.0 protocol (based on OAuth 2.0 Authorization Protocol draft-ietf-oauth-v2-15)",
"name": "tlshaheen/oauth2",
"description": "Light PHP wrapper for the OAuth 2.0 protocol (based on OAuth 2.0 Authorization Protocol draft-ietf-oauth-v2-15). Original source at adoy/oauth2",
"license": "LGPL-2.1",
"authors": [
{
Expand All @@ -10,6 +10,10 @@
{
"name": "Berejeb Anis",
"email": "[email protected]"
},
{
"name": "Luke Shaheen",
"email": "[email protected]"
}
],
"require": {
Expand Down
96 changes: 83 additions & 13 deletions src/OAuth2/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,19 @@ public function fetch($protected_resource_url, $parameters = array(), $http_meth
if (is_array($parameters)) {
$parameters[$this->access_token_param_name] = $this->access_token;
} else {
throw new InvalidArgumentException(
'You need to give parameters as array if you want to give the token within the URI.',
InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY
);
$throwexception = true;

$testparams = json_decode($parameters, true);
if (array_key_exists($this->access_token_param_name, $testparams)) {
$throwexception = false;
}

if ($throwexception) {
throw new InvalidArgumentException(
'You need to give parameters as array if you want to give the token within the URI.',
InvalidArgumentException::REQUIRE_PARAMS_AS_ARRAY
);
}
}
break;
case self::ACCESS_TOKEN_BEARER:
Expand Down Expand Up @@ -406,10 +415,12 @@ private function executeRequest($url, $parameters = array(), $http_method = self
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_CUSTOMREQUEST => $http_method
CURLOPT_CUSTOMREQUEST => $http_method,
CURLOPT_HEADER => true,
);

switch($http_method) {
case self::HTTP_METHOD_DELETE:
case self::HTTP_METHOD_POST:
$curl_options[CURLOPT_POST] = true;
/* No break */
Expand All @@ -429,12 +440,29 @@ private function executeRequest($url, $parameters = array(), $http_method = self
case self::HTTP_METHOD_HEAD:
$curl_options[CURLOPT_NOBODY] = true;
/* No break */
case self::HTTP_METHOD_DELETE:
case self::HTTP_METHOD_GET:
if (is_array($parameters) && count($parameters) > 0) {
$url .= '?' . http_build_query($parameters, null, '&');
} elseif ($parameters) {
$url .= '?' . $parameters;
if ($parameters) {
//Remove any trailing question marks or ampersands
if (substr($url, -1) == '?') {
$url = substr($url, 0, mb_strlen($url) - 1);
} else {
if (substr($url, -1) == '&') {
$url = substr($url, 0, mb_strlen($url) - 1);
}
}
//Check to see if there are existing parameters in the URL
//We want to add the appropriate character to start adding our own params
parse_str(parse_url($url, PHP_URL_QUERY), $existingParams);
if (sizeof($existingParams)) {
$url .= '&';
} else {
$url .= '?';
}
if (is_array($parameters) && count($parameters) > 0) {
$url .= http_build_query($parameters, null, '&');
} else {
$url .= $parameters;
}
}
break;
default:
Expand All @@ -444,6 +472,7 @@ private function executeRequest($url, $parameters = array(), $http_method = self
$curl_options[CURLOPT_URL] = $url;

if (is_array($http_headers)) {
$http_headers['Expect'] = '';
$header = array();
foreach($http_headers as $key => $parsed_urlvalue) {
$header[] = "$key: $parsed_urlvalue";
Expand All @@ -469,17 +498,25 @@ private function executeRequest($url, $parameters = array(), $http_method = self
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

//Split result into body and headers
list($headers, $body) = explode("\r\n\r\n", $result, 2);

//Parse headers to return
$headers = http_parse_headers($headers);

if ($curl_error = curl_error($ch)) {
throw new Exception($curl_error, Exception::CURL_ERROR);
} else {
$json_decode = json_decode($result, true);
$json_decode = json_decode($body, true);
}
curl_close($ch);

return array(
'result' => (null === $json_decode) ? $result : $json_decode,
'result' => (null === $json_decode) ? $body : $json_decode,
'code' => $http_code,
'content_type' => $content_type
'content_type' => $content_type,
'headers' => $headers,
);
}

Expand Down Expand Up @@ -524,3 +561,36 @@ class InvalidArgumentException extends \InvalidArgumentException
const REQUIRE_PARAMS_AS_ARRAY = 0x03;
const MISSING_PARAMETER = 0x04;
}

//http://php.net/manual/en/function.http-parse-headers.php#112986
if (!function_exists('http_parse_headers')) {
function http_parse_headers($raw_headers) {
$headers = array();
$key = '';

foreach(explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);

if (isset($h[1])) {
if (!isset($headers[$h[0]]))
$headers[$h[0]] = trim($h[1]);
elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
}
else {
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])));
}

$key = $h[0];
}
else {
if (substr($h[0], 0, 1) == "\t")
$headers[$key] .= "\r\n\t".trim($h[0]);
elseif (!$key)
$headers[0] = trim($h[0]);
}
}

return $headers;
}
}