11if (!class_exists(
'OAuthException')) {
 
   15    class OAuthException 
extends Exception {
 
   20if (!class_exists(
'OAuthConsumer')) {
 
   25        function __construct(
$key, 
$secret, $callback_url=NULL) {
 
   28            $this->callback_url = $callback_url;
 
   31        function __toString() {
 
   32            return "OAuthConsumer[key=$this->key,secret=$this->secret]";
 
   56        return "oauth_token=" .
 
   58        "&oauth_token_secret=" .
 
   60        "&oauth_callback_confirmed=true";
 
  103        if (strlen($built) == 0 || strlen($signature) == 0) {
 
  107        if (strlen($built) != strlen($signature)) {
 
  113        for (
$i = 0; 
$i < strlen($signature); 
$i++) {
 
  134        $base_string = $request->get_signature_base_string();
 
  135        $request->base_string = $base_string;
 
  139            ($token) ? $token->secret : 
"" 
  143        $key = implode(
'&', $key_parts);
 
  145        return base64_encode(hash_hmac(
'sha1', $base_string, 
$key, 
true));
 
  171            ($token) ? $token->secret : 
"" 
  175        $key = implode(
'&', $key_parts);
 
  176        $request->base_string = 
$key;
 
  210        $base_string = $request->get_signature_base_string();
 
  211        $request->base_string = $base_string;
 
  217        $privatekeyid = openssl_get_privatekey($cert);
 
  220        $ok = openssl_sign($base_string, $signature, $privatekeyid);
 
  223        openssl_free_key($privatekeyid);
 
  225        return base64_encode($signature);
 
  229        $decoded_sig = base64_decode($signature);
 
  231        $base_string = $request->get_signature_base_string();
 
  237        $publickeyid = openssl_get_publickey($cert);
 
  240        $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
 
  243        openssl_free_key($publickeyid);
 
  295                &&  isset($request_headers[
'Content-Type'])
 
  296                && strstr($request_headers[
'Content-Type'],
 
  297                    'application/x-www-form-urlencoded')
 
  300                    file_get_contents(self::$POST_INPUT)
 
  307            if (isset($request_headers[
'Authorization']) && substr($request_headers[
'Authorization'], 0, 6) == 
'OAuth ') {
 
  309                    $request_headers[
'Authorization']
 
  329            $defaults[
'oauth_token'] = $token->key;
 
  337        if ($allow_duplicates && isset($this->parameters[
$name])) {
 
  339            if (is_scalar($this->parameters[
$name])) {
 
  342                $this->parameters[
$name] = array($this->parameters[
$name]);
 
  345            $this->parameters[
$name][] = $value;
 
  347            $this->parameters[
$name] = $value;
 
  352        return isset($this->parameters[
$name]) ? $this->parameters[
$name] : 
null;
 
  360        unset($this->parameters[
$name]);
 
  373        if (isset(
$params[
'oauth_signature'])) {
 
  374            unset(
$params[
'oauth_signature']);
 
  396        return implode(
'&', $parts);
 
  403        return strtoupper($this->http_method);
 
  411        $parts = parse_url($this->http_url);
 
  413        $scheme = (isset($parts[
'scheme'])) ? $parts[
'scheme'] : 
'http';
 
  414        $port = (isset($parts[
'port'])) ? $parts[
'port'] : (($scheme == 
'https') ? 
'443' : 
'80');
 
  415        $host = (isset($parts[
'host'])) ? strtolower($parts[
'host']) : 
'';
 
  416        $path = (isset($parts[
'path'])) ? $parts[
'path'] : 
'';
 
  418        if (($scheme == 
'https' && $port != 
'443')
 
  419            || ($scheme == 
'http' && $port != 
'80')) {
 
  420            $host = 
"$host:$port";
 
  422        return "$scheme://$host$path";
 
  432            $out .= 
'?'.$post_data;
 
  453            $out = 
'Authorization: OAuth';
 
  456        foreach ($this->parameters as $k => $v) {
 
  457            if (substr($k, 0, 5) != 
"oauth") 
continue;
 
  459                throw new OAuthException(
'Arrays not supported in headers');
 
  461            $out .= ($first) ? 
' ' : 
',';
 
  478            "oauth_signature_method",
 
  479            $signature_method->get_name(),
 
  487        $signature = $signature_method->build_signature($this, 
$consumer, $token);
 
  505        return md5($mt . $rand); 
 
  521        $this->signature_methods[$signature_method->get_name()] =
 
  542        $callback = $request->get_parameter(
'oauth_callback');
 
  543        $new_token = $this->data_store->new_request_token(
$consumer, $callback);
 
  563        $verifier = $request->get_parameter(
'oauth_verifier');
 
  564        $new_token = $this->data_store->new_access_token($token, 
$consumer, $verifier);
 
  585        $version = $request->get_parameter(
"oauth_version");
 
  592            throw new OAuthException(
"OAuth version '$version' not supported");
 
  605        if (!$signature_method) {
 
  608            throw new OAuthException(
'No signature method parameter. This parameter is required');
 
  611        if (!in_array($signature_method,
 
  612            array_keys($this->signature_methods))) {
 
  613            throw new OAuthException(
 
  614                "Signature method '$signature_method' not supported " .
 
  615                "try one of the following: " .
 
  616                implode(
", ", array_keys($this->signature_methods))
 
  619        return $this->signature_methods[$signature_method];
 
  630        if (!$consumer_key) {
 
  631            throw new OAuthException(
"Invalid consumer key");
 
  634        $consumer = $this->data_store->lookup_consumer($consumer_key);
 
  636            throw new OAuthException(
"Invalid consumer");
 
  650        if (!empty($token_field)) {
 
  651            $token = $this->data_store->lookup_token(
 
  655                throw new OAuthException(
"Invalid $token_type token: $token_field");
 
  682        $signature = $request->get_parameter(
'oauth_signature');
 
  683        $valid_sig = $signature_method->check_signature(
 
  691            throw new OAuthException(
"Invalid signature");
 
  700            throw new OAuthException(
 
  701                'Missing timestamp parameter. The parameter is required' 
  706        if (abs($now - 
$timestamp) > $this->timestamp_threshold) {
 
  707            throw new OAuthException(
 
  708                "Expired timestamp, yours $timestamp, ours $now" 
  718            throw new OAuthException(
 
  719                'Missing nonce parameter. The parameter is required' 
  723        $found = $this->data_store->lookup_nonce(
 
  730            throw new OAuthException(
"Nonce already used: $nonce");
 
  764        if (is_array($input)) {
 
  765            return array_map(array(
'OAuthUtil', 
'urlencode_rfc3986'), $input);
 
  766        } 
else if (is_scalar($input)) {
 
  770                str_replace(
'%7E', 
'~', rawurlencode($input))
 
  782        return urldecode($string);
 
  792        if (preg_match_all(
'/('.($only_allow_oauth_parameters ? 
'oauth_' : 
'').
'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', 
$header, $matches)) {
 
  793            foreach ($matches[1] as 
$i => 
$h) {
 
  805        if (function_exists(
'apache_request_headers')) {
 
  808            $headers = apache_request_headers();
 
  815            foreach ($headers AS 
$key => $value) {
 
  819                    ucwords(strtolower(str_replace(
"-", 
" ", 
$key)))
 
  827            if( isset(
$_SERVER[
'CONTENT_TYPE']) )
 
  829            if( isset($_ENV[
'CONTENT_TYPE']) )
 
  830                $out[
'Content-Type'] = $_ENV[
'CONTENT_TYPE'];
 
  833                if (substr(
$key, 0, 5) == 
"HTTP_") {
 
  840                        ucwords(strtolower(str_replace(
"_", 
" ", substr(
$key, 5))))
 
  846            if (isset(
$out[
'Auth'])) {
 
  847                $out[
'Authorization'] = 
$out[
'Auth'];
 
  857        if (!isset($input) || !$input) 
return array();
 
  859        $pairs = explode(
'&', $input);
 
  861        $parsed_parameters = array();
 
  862        foreach ($pairs as $pair) {
 
  863            $split = explode(
'=', $pair, 2);
 
  867            if (isset($parsed_parameters[$parameter])) {
 
  871                if (is_scalar($parsed_parameters[$parameter])) {
 
  874                    $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
 
  877                $parsed_parameters[$parameter][] = $value;
 
  879                $parsed_parameters[$parameter] = $value;
 
  882        return $parsed_parameters;
 
  898        foreach (
$params as $parameter => $value) {
 
  899            if (is_array($value)) {
 
  903                sort($value, SORT_STRING);
 
  904                foreach ($value as $duplicate_value) {
 
  905                    $pairs[] = $parameter . 
'=' . $duplicate_value;
 
  908                $pairs[] = $parameter . 
'=' . $value;
 
  913        return implode(
'&', $pairs);
 
foreach($mandatory_scripts as $file) $timestamp
An exception for terminatinating execution or to throw for unit testing.
lookup_consumer($consumer_key)
lookup_token($consumer, $token_type, $token)
new_request_token($consumer, $callback=null)
lookup_nonce($consumer, $token, $nonce, $timestamp)
new_access_token($token, $consumer, $verifier=null)
__construct($http_method, $http_url, $parameters=NULL)
sign_request($signature_method, $consumer, $token)
get_normalized_http_method()
just uppercases the http method
to_postdata()
builds the data one would send in a POST request
get_normalized_http_url()
parses the url and rebuilds it to be scheme://host/path
to_url()
builds a url usable for a GET request
get_signable_parameters()
The request parameters, sorted and concatenated into a normalized string.
static from_request($http_method=NULL, $http_url=NULL, $parameters=NULL)
attempt to build up a request from what was passed to the server
get_signature_base_string()
Returns the base string of this request.
set_parameter($name, $value, $allow_duplicates=true)
to_header($realm=null)
builds the Authorization: header
static generate_timestamp()
util function: current timestamp
build_signature($signature_method, $consumer, $token)
static generate_nonce()
util function: current nonce
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL)
pretty much a helper function to set up the request
add_signature_method($signature_method)
get_token($request, $consumer, $token_type="access")
try to find the token for the provided request's token key
check_nonce($consumer, $token, $nonce, $timestamp)
check that the nonce is not repeated
check_timestamp($timestamp)
check that the timestamp is new enough
fetch_access_token(&$request)
process an access_token request returns the access token on success
get_signature_method($request)
figure out the signature with some defaults
check_signature($request, $consumer, $token)
all-in-one function to check the signature on a request should guess the signature method appropriate...
fetch_request_token(&$request)
process a request_token request returns the request token on success
verify_request(&$request)
verify an api call, checks all the parameters
get_version(&$request)
version 1
get_consumer($request)
try to find the consumer for the provided request's consumer key
The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104] where t...
get_name()
Needs to return the name of the Signature Method (ie HMAC-SHA1)
build_signature($request, $consumer, $token)
Build up the signature NOTE: The output of this function MUST NOT be urlencoded.
The PLAINTEXT method does not provide any security protection and SHOULD only be used over a secure c...
build_signature($request, $consumer, $token)
oauth_signature is set to the concatenated encoded values of the Consumer Secret and Token Secret,...
get_name()
Needs to return the name of the Signature Method (ie HMAC-SHA1)
The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in [RFC3447] ...
fetch_public_cert(&$request)
check_signature($request, $consumer, $token, $signature)
Verifies that a given signature is correct.
fetch_private_cert(&$request)
build_signature($request, $consumer, $token)
Build up the signature NOTE: The output of this function MUST NOT be urlencoded.
get_name()
Needs to return the name of the Signature Method (ie HMAC-SHA1)
A class for implementing a Signature Method See section 9 ("Signing Requests") in the spec.
get_name()
Needs to return the name of the Signature Method (ie HMAC-SHA1)
check_signature($request, $consumer, $token, $signature)
Verifies that a given signature is correct.
build_signature($request, $consumer, $token)
Build up the signature NOTE: The output of this function MUST NOT be urlencoded.
OAuth PECL extension includes an OAuth Exception class, so we need to wrap the definition of this cla...
to_string()
generates the basic string serialization of a token that a server would respond to request_token and ...
__construct($key, $secret)
key = the token secret = the token secret
static split_header($header, $only_allow_oauth_parameters=true)
static urldecode_rfc3986($string)
static parse_parameters( $input)
static urlencode_rfc3986($input)
static build_http_query($params)
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']