Modules

OAuth2
extends OAuth2_Core

OAuth v2 class

package
Gleez\OAuth\Core
author
Sandeep Sangamreddi - Gleez
copyright
© 2011-2013 Gleez Technologies
license
http://gleezcms.org/license

Class declared in MODPATH/user/classes/oauth2.php on line 3.

Methods

public static normalize_params( [ array $params = NULL ] ) (defined in OAuth2_Core)

Normalize all request parameters into a string.

$query = OAuth::normalize_params($params);

This method implements OAuth 1.0 Spec 9.1.1.

Parameters

  • array $params = NULL - Request parameters

Tags

Return Values

  • string

Source Code

public static function normalize_params(array $params = NULL)
{
	if ( ! $params)
	{
		// Nothing to do
		return '';
	}

	// Encode the parameter keys and values
	$keys   = OAuth2::urlencode(array_keys($params));
	$values = OAuth2::urlencode(array_values($params));

	// Recombine the parameters
	$params = array_combine($keys, $values);

	// OAuth Spec 9.1.1 (1)
	// "Parameters are sorted by name, using lexicographical byte value ordering."
	uksort($params, 'strcmp');

	// Create a new query string
	$query = array();

	foreach ($params as $name => $value)
	{
		if (is_array($value))
		{
			// OAuth Spec 9.1.1 (1)
			// "If two or more parameters share the same name, they are sorted by their value."
			$value = natsort($value);

			foreach ($value as $duplicate)
			{
				$query[] = $name.'='.$duplicate;
			}
		}
		else
		{
			$query[] = $name.'='.$value;
		}
	}

	return implode('&', $query);
}

public static parse_params( string $params ) (defined in OAuth2_Core)

Parse the parameters in a string and return an array. Duplicates are converted into indexed arrays.

// Parsed: array('a' => '1', 'b' => '2', 'c' => '3')
$params = OAuth::parse_params('a=1,b=2,c=3');

// Parsed: array('a' => array('1', '2'), 'c' => '3')
$params = OAuth::parse_params('a=1,a=2,c=3');

Parameters

  • string $params required - Parameter string

Return Values

  • array

Source Code

public static function parse_params($params)
{
	// Split the parameters by &
	$params = explode('&', trim($params));

	// Create an array of parsed parameters
	$parsed = array();

	foreach ($params as $param)
	{
		// Split the parameter into name and value
		list($name, $value) = explode('=', $param, 2);
		//list($name, $value) = preg_split('#=|:#', $param, 2);

		// Decode the name and value
		$name  = OAuth2::urldecode($name);
		$value = OAuth2::urldecode($value);

		if (isset($parsed[$name]))
		{
			if ( ! is_array($parsed[$name]))
			{
				// Convert the parameter to an array
				$parsed[$name] = array($parsed[$name]);
			}

			// Add a new duplicate parameter
			$parsed[$name][] = $value;
		}
		else
		{
			// Add a new parameter
			$parsed[$name] = $value;
		}
	}

	return $parsed;
}

public static parse_url( string $url ) (defined in OAuth2_Core)

Parse the query string out of the URL and return it as parameters. All GET parameters must be removed from the request URL when building the base string and added to the request parameters.

// parsed parameters: array('oauth_key' => 'abcdef123456789')
list($url, $params) = OAuth::parse_url('http://example.com/oauth/access?oauth_key=abcdef123456789');

This implements OAuth Spec 9.1.1.

Parameters

  • string $url required - URL to parse

Tags

Return Values

  • array - (clean_url, params)

Source Code

public static function parse_url($url)
{
	if ($query = parse_url($url, PHP_URL_QUERY))
	{
		// Remove the query string from the URL
		list($url) = explode('?', $url, 2);

		// Parse the query string as request parameters
		$params = OAuth2::parse_params($query);
	}
	else
	{
		// No parameters are present
		$params = array();
	}

	return array($url, $params);
}

public provider( $name $name [, array $options = NULL ] ) (defined in OAuth2_Core)

Parameters

  • $name $name required - Provider name
  • array $options = NULL - Provider options

Return Values

  • OAuth2_Provider

Source Code

public function provider($name, array $options = NULL)
{
	return OAuth2_Provider::factory($name, $options);
}

public static remote( string $url [, array $options = NULL ] ) (defined in OAuth2_Core)

Returns the output of a remote URL. Any curl option may be used.

// Do a simple GET request
$data = Remote::get($url);

// Do a POST request
$data = Remote::get($url, array(
    CURLOPT_POST       => TRUE,
    CURLOPT_POSTFIELDS => http_build_query($array),
));

Parameters

  • string $url required - Remote URL
  • array $options = NULL - Curl options

Tags

Return Values

  • string

Source Code

public static function remote($url, array $options = NULL)
{
	// The transfer must always be returned
	$options[CURLOPT_RETURNTRANSFER] = TRUE;

	// Open a new remote connection
	$remote = curl_init($url);

	// Set connection options
	if ( ! curl_setopt_array($remote, $options))
	{
		throw new Kohana_Exception('Failed to set CURL options, check CURL documentation: :url',
			array(':url' => 'http://php.net/curl_setopt_array'));
	}

	// Get the response
	$response = curl_exec($remote);

	// Get the response information
	$code = curl_getinfo($remote, CURLINFO_HTTP_CODE);

	if ($code AND $code < 200 OR $code > 299)
	{
		$error = $response;
	}
	elseif ($response === FALSE)
	{
		$error = curl_error($remote);
	}

	// Close the connection
	curl_close($remote);

	if (isset($error))
	{
		throw new OAuth2_Exception('Error fetching remote :url [ status :code ] :error',
			array(':url' => $url, ':code' => $code, ':error' => $error));
	}

	return $response;
}

public request( string $type , string $method , string $url [, array $options = NULL ] ) (defined in OAuth2_Core)

Get request object

Parameters

  • string $type required - Request type (access, token etc)
  • string $method required - Request method (POST, GET)
  • string $url required - URL
  • array $options = NULL - Request params

Return Values

  • OAuth2_Request

Source Code

public function request($type, $method, $url, array $options = NULL)
{
	return OAuth2_Request::factory($type, $method, $url, $options);
}

public token( $name $name [, array $options = NULL ] ) (defined in OAuth2_Core)

Parameters

  • $name $name required - Token type
  • array $options = NULL - Token options

Return Values

  • OAuth2_Token

Source Code

public function token($name, array $options = NULL)
{
	return OAuth2_Token::factory($name, $options);
}

public static urldecode( mixed $input ) (defined in OAuth2_Core)

RFC3986 complaint version of urldecode. Passing an array will decode all of the values in the array. Array keys will not be encoded.

$input = OAuth::urldecode($input);

Multi-dimensional arrays are not allowed!

This method implements OAuth 1.0 Spec 5.1.

Parameters

  • mixed $input required - Input string or array

Return Values

  • mixed

Source Code

public static function urldecode($input)
{
	if (is_array($input))
	{
		// Decode the values of the array
		return array_map(array('OAuth', 'urldecode'), $input);
	}

	// Decode the input
	return rawurldecode($input);
}

public static urlencode( mixed $input ) (defined in OAuth2_Core)

RFC3986 compatible version of urlencode. Passing an array will encode all of the values in the array. Array keys will not be encoded.

$input = OAuth::urlencode($input);

Multi-dimensional arrays are not allowed!

This method implements OAuth 1.0 Spec 5.1.

Parameters

  • mixed $input required - Input string or array

Return Values

  • mixed

Source Code

public static function urlencode($input)
{
	if (is_array($input))
	{
		// Encode the values of the array
		return array_map(array('OAuth2', 'urlencode'), $input);
	}

	// Encode the input
	$input = rawurlencode($input);

	if (version_compare(PHP_VERSION, '<', '5.3'))
	{
		// rawurlencode() is RFC3986 compliant in PHP 5.3
		// the only difference is the encoding of tilde
		$input = str_replace('%7E', '~', $input);
	}

	return $input;
}
Documentation comments powered by Disqus