11if (!class_exists(
'OAuthException')) {
 
   15    class OAuthException 
extends Exception
 
   21if (!class_exists(
'OAuthConsumer')) {
 
   28        public function __construct(
$key, $secret, $callback_url = 
null)
 
   31            $this->secret = $secret;
 
   32            $this->callback_url = $callback_url;
 
   35        public function __toString()
 
   37            return "OAuthConsumer[key=$this->key,secret=$this->secret]";
 
   64        return "oauth_token=" .
 
   66        "&oauth_token_secret=" .
 
   68        "&oauth_callback_confirmed=true";
 
  114        if (strlen($built) == 0 || strlen($signature) == 0) {
 
  118        if (strlen($built) != strlen($signature)) {
 
  124        for (
$i = 0; 
$i < strlen($signature); 
$i++) {
 
  148        $base_string = 
$request->get_signature_base_string();
 
  149        $request->base_string = $base_string;
 
  157        $key = implode(
'&', $key_parts);
 
  159        return base64_encode(hash_hmac(
'sha1', $base_string, 
$key, 
true));
 
  192        $key = implode(
'&', $key_parts);
 
  230        $base_string = 
$request->get_signature_base_string();
 
  231        $request->base_string = $base_string;
 
  237        $privatekeyid = openssl_get_privatekey($cert);
 
  240        openssl_sign($base_string, $signature, $privatekeyid);
 
  243        openssl_free_key($privatekeyid);
 
  245        return base64_encode($signature);
 
  250        $decoded_sig = base64_decode($signature);
 
  252        $base_string = 
$request->get_signature_base_string();
 
  258        $publickeyid = openssl_get_publickey($cert);
 
  261        $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
 
  264        openssl_free_key($publickeyid);
 
  319                &&  isset($request_headers[
'Content-Type'])
 
  320                && strstr($request_headers[
'Content-Type'],
 
  321                    'application/x-www-form-urlencoded')
 
  324                    file_get_contents(self::$POST_INPUT)
 
  331            if (isset($request_headers[
'Authorization']) && substr($request_headers[
'Authorization'], 0, 6) == 
'OAuth ') {
 
  333                    $request_headers[
'Authorization']
 
  352                          "oauth_consumer_key" => $consumer->key);
 
  354            $defaults[
'oauth_token'] = 
$token->key;
 
  363        if ($allow_duplicates && isset($this->parameters[
$name])) {
 
  365            if (is_scalar($this->parameters[
$name])) {
 
  368                $this->parameters[
$name] = array($this->parameters[
$name]);
 
  371            $this->parameters[
$name][] = $value;
 
  373            $this->parameters[
$name] = $value;
 
  379        return isset($this->parameters[
$name]) ? $this->parameters[
$name] : 
null;
 
  389        unset($this->parameters[
$name]);
 
  403        if (isset(
$params[
'oauth_signature'])) {
 
  404            unset(
$params[
'oauth_signature']);
 
  427        return implode(
'&', $parts);
 
  435        return strtoupper($this->http_method);
 
  444        $parts = parse_url($this->http_url);
 
  446        $scheme = (isset($parts[
'scheme'])) ? $parts[
'scheme'] : 
'http';
 
  447        $port = (isset($parts[
'port'])) ? $parts[
'port'] : (($scheme == 
'https') ? 
'443' : 
'80');
 
  448        $host = (isset($parts[
'host'])) ? strtolower($parts[
'host']) : 
'';
 
  449        $path = (isset($parts[
'path'])) ? $parts[
'path'] : 
'';
 
  451        if (($scheme == 
'https' && $port != 
'443')
 
  452            || ($scheme == 
'http' && $port != 
'80')) {
 
  453            $host = 
"$host:$port";
 
  455        return "$scheme://$host$path";
 
  466            $out .= 
'?'.$post_data;
 
  489            $out = 
'Authorization: OAuth';
 
  492        foreach ($this->parameters as $k => $v) {
 
  493            if (substr($k, 0, 5) != 
"oauth") {
 
  497                throw new OAuthException(
'Arrays not supported in headers');
 
  499            $out .= ($first) ? 
' ' : 
',';
 
  518            "oauth_signature_method",
 
  519            $signature_method->get_name(),
 
  528        $signature = $signature_method->build_signature($this, $consumer, 
$token);
 
  548        return md5($mt . $rand); 
 
  567        $this->signature_methods[$signature_method->get_name()] =
 
  589        $callback = 
$request->get_parameter(
'oauth_callback');
 
  590        $new_token = $this->data_store->new_request_token($consumer, $callback);
 
  611        $verifier = 
$request->get_parameter(
'oauth_verifier');
 
  612        $new_token = $this->data_store->new_access_token(
$token, $consumer, $verifier);
 
  626        return array($consumer, 
$token);
 
  642            throw new OAuthException(
"OAuth version '$version' not supported");
 
  653            ? 
$request->get_parameter(
"oauth_signature_method")
 
  656        if (!$signature_method) {
 
  659            throw new OAuthException(
'No signature method parameter. This parameter is required');
 
  662        if (!in_array($signature_method,
 
  663            array_keys($this->signature_methods))) {
 
  664            throw new OAuthException(
 
  665                "Signature method '$signature_method' not supported " .
 
  666                "try one of the following: " .
 
  667                implode(
", ", array_keys($this->signature_methods))
 
  670        return $this->signature_methods[$signature_method];
 
  679            ? 
$request->get_parameter(
"oauth_consumer_key")
 
  682        if (!$consumer_key) {
 
  683            throw new OAuthException(
"Invalid consumer key");
 
  686        $consumer = $this->data_store->lookup_consumer($consumer_key);
 
  688            throw new OAuthException(
"Invalid consumer");
 
  700            ? 
$request->get_parameter(
'oauth_token')
 
  703        if (!empty($token_field)) {
 
  704            $token = $this->data_store->lookup_token(
 
  705                $consumer, $token_type, $token_field
 
  708                throw new OAuthException(
"Invalid $token_type token: $token_field");
 
  725            ? 
$request->get_parameter(
'oauth_timestamp')
 
  728            ? 
$request->get_parameter(
'oauth_nonce')
 
  736        $signature = 
$request->get_parameter(
'oauth_signature');
 
  737        $valid_sig = $signature_method->check_signature(
 
  745            throw new OAuthException(
"Invalid signature");
 
  755            throw new OAuthException(
 
  756                'Missing timestamp parameter. The parameter is required' 
  762        if (abs($now - 
$timestamp) > $this->timestamp_threshold) {
 
  763            throw new OAuthException(
 
  764                "Expired timestamp, yours $timestamp, ours $now" 
  775            throw new OAuthException(
 
  776                'Missing nonce parameter. The parameter is required' 
  781        $found = $this->data_store->lookup_nonce(
 
  788            throw new OAuthException(
"Nonce already used: $nonce");
 
  830            return array_map(array(
'OAuthUtil', 
'urlencode_rfc3986'), 
$input);
 
  831        } 
else if (is_scalar(
$input)) {
 
  835                str_replace(
'%7E', 
'~', rawurlencode(
$input))
 
  848        return urldecode($string);
 
  859        if (preg_match_all(
'/('.($only_allow_oauth_parameters ? 
'oauth_' : 
'').
'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', 
$header, $matches)) {
 
  860            foreach ($matches[1] as 
$i => 
$h) {
 
  873        if (function_exists(
'apache_request_headers')) {
 
  876            $headers = apache_request_headers();
 
  883            foreach ($headers as 
$key => $value) {
 
  887                    ucwords(strtolower(str_replace(
"-", 
" ", 
$key)))
 
  895            if (isset(
$_SERVER[
'CONTENT_TYPE'])) {
 
  898            if (isset($_ENV[
'CONTENT_TYPE'])) {
 
  899                $out[
'Content-Type'] = $_ENV[
'CONTENT_TYPE'];
 
  903                if (substr(
$key, 0, 5) == 
"HTTP_") {
 
  910                        ucwords(strtolower(str_replace(
"_", 
" ", substr(
$key, 5))))
 
  916            if (isset(
$out[
'Auth'])) {
 
  917                $out[
'Authorization'] = 
$out[
'Auth'];
 
  932        $pairs = explode(
'&', 
$input);
 
  934        $parsed_parameters = array();
 
  935        foreach ($pairs as $pair) {
 
  936            $split = explode(
'=', $pair, 2);
 
  940            if (isset($parsed_parameters[$parameter])) {
 
  944                if (is_scalar($parsed_parameters[$parameter])) {
 
  947                    $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
 
  950                $parsed_parameters[$parameter][] = $value;
 
  952                $parsed_parameters[$parameter] = $value;
 
  955        return $parsed_parameters;
 
  974        foreach (
$params as $parameter => $value) {
 
  975            if (is_array($value)) {
 
  979                sort($value, SORT_STRING);
 
  980                foreach ($value as $duplicate_value) {
 
  981                    $pairs[] = $parameter . 
'=' . $duplicate_value;
 
  984                $pairs[] = $parameter . 
'=' . $value;
 
  989        return implode(
'&', $pairs);
 
foreach($paths as $path) $request
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)
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=null)
pretty much a helper function to set up the request
__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.
get_signature_base_string()
Returns the base string of this request.
static from_request($http_method=null, $http_url=null, $parameters=null)
attempt to build up a request from what was passed to the server
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
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 urlencode_rfc3986($input)
static parse_parameters($input)
static build_http_query($params)
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']