├── .gitignore ├── README.MacOSX ├── config.php.sample ├── ChangeLog ├── README.markdown ├── plurkAPI.php ├── testOAuth.php ├── testPlurk.php ├── plurkOAuth.php └── OAuth.php /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | 3 | -------------------------------------------------------------------------------- /README.MacOSX: -------------------------------------------------------------------------------- 1 | follow 2 | http://lupomontero.e-noise.com/blog/installing-php-oauth-pecl-extension-on-mac-os-x-snow-leopard 3 | -------------------------------------------------------------------------------- /config.php.sample: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2 | 0.2.2 3 | - fix bug, which translate " " to "+" first then urlencode() 4 | 5 | 0.2.1 6 | - finish authorize(null, null), now you can use this to get access_token 7 | 8 | 0.2.0 9 | - use own OAuth.php, remove oauth-php 10 | 11 | 0.1.0 12 | - first release, use oauth-php 13 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | PlurkOAuth 2 | ====== 3 | 4 | PHP Wrapper of Plurk OAuth API 5 | 6 | About 7 | ---- 8 | PlurkOAuth is a php wrapper for [Plurk API 2.0 beta](https://www.plurk.com/API/2) 9 | You will need to [Sign Up](https://www.plurk.com/PlurkApp/register) for your own CUSTOMER TOKENs. 10 | 11 | Prerequire Packages 12 | ---- 13 | [pecl_http](http://pecl.php.net/package/pecl_http), for http_build_url(), http_*. 14 | 15 | [PHPUniut](http://www.phpunit.de) if you need test it (YOU SHOULD). 16 | 17 | Example 18 | ---- 19 | % cp config.php.sample config.php # and modify it 20 | 21 | ``` php 22 | require('plurkAPI.php'); 23 | 24 | $plurk = new PlurkAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); 25 | $json = $plurk->callAPI('/APP/Profile/getPublicProfile', array('user_id' => 'clsung'), true); 26 | $json = $plurk->callAPI('/APP/Profile/getOwnProfile'); 27 | $json = $plurk->callAPI('/APP/FriendsFans/getFriendsByOffset', array ('user_id' => 'clsung')); 28 | //$json = $plurk->callAPI('/APP/Timeline/getPlurks'); 29 | //$json = $plurk->callAPI('/APP/Timeline/plurkAdd', array ('content' => 'Post by plurkoauth which based on oauth-php', 'qualifier' => 'hates')); 30 | ``` 31 | -------------------------------------------------------------------------------- /plurkAPI.php: -------------------------------------------------------------------------------- 1 | _oauth = new PlurkOAuth($consumer_key, $consumer_secret, $access_token, $access_secret); 18 | $this->_error = array ( 19 | 'content' => null, 20 | 'code' => 0, 21 | 'reason' => null 22 | ); 23 | } 24 | 25 | function authorize_with_verifier($verifier = NULL) { 26 | $this->_oauth->authorize_with_verifier($verifier); 27 | } 28 | 29 | function authorize($access_token = null, $access_secret = null) { 30 | $this->_oauth->authorize($access_token, $access_secret); 31 | } 32 | 33 | function get_access_token() { 34 | return $this->_oauth->get_access_token(); 35 | } 36 | 37 | function get_request_token() { 38 | return $this->_oauth->get_request_token(); 39 | } 40 | 41 | function callAPI($path, $params_array = null) { 42 | $this->_error = $this->_oauth->request($path, null, $params_array); 43 | return $this->_error['content']; 44 | } 45 | 46 | function errno() { 47 | return $this->_error['code']; 48 | } 49 | 50 | function error() { 51 | return $this->_error['reason']; 52 | } 53 | } 54 | ?> 55 | -------------------------------------------------------------------------------- /testOAuth.php: -------------------------------------------------------------------------------- 1 | assertEquals('https://www.plurk.com/OAuth/request_token', 15 | $request->normalized_url); 16 | } 17 | } 18 | */ 19 | 20 | class SignatureMethod_Test extends PHPUnit_Framework_TestCase 21 | { 22 | protected $request; 23 | protected $consumer; 24 | protected $token; 25 | protected $signature_method; 26 | 27 | public function setUp() { 28 | $this->consumer = new Consumer('con_key', 'con_secret'); 29 | $this->token = new Token('auth_token', 'auth_token_secret'); 30 | } 31 | public function tearDown() {} 32 | 33 | public function testPOSTGetRequestToken() { 34 | $request = new Request("POST", 35 | 'https://www.plurk.com/OAuth/request_token'); 36 | $signature_method = new SignatureMethod_HMAC_SHA1(); 37 | $request->sign_request($signature_method, $this->consumer, $this->token); 38 | $header = $request->to_header(); 39 | $gold = array ('Authorization' => 'OAuth realm="", oauth_consumer_key="con_key", oauth_token="auth_token", oauth_signature_method="HMAC_SHA1", oauth_signature="4QJdtxu30u/YhwTgexeStWxy6Ec="'); 40 | $this->assertEquals($gold, $header); 41 | } 42 | 43 | public function testPOSTGetRequestToken2() { 44 | $request = Request::from_consumer_and_token( 45 | $this->consumer, $this->token, "POST", 46 | 'https://www.plurk.com/OAuth/request_token'); 47 | $signature_method = new SignatureMethod_HMAC_SHA1(); 48 | $request->sign_request($signature_method, $this->consumer, $this->token); 49 | $header = $request->to_header(); 50 | $gold = array ('Authorization' => 'OAuth realm="", oauth_consumer_key="con_key", oauth_token="auth_token", oauth_signature_method="HMAC_SHA1", oauth_signature="4QJdtxu30u/YhwTgexeStWxy6Ec="'); 51 | $this->assertNotEquals($gold, $header); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /testPlurk.php: -------------------------------------------------------------------------------- 1 | consumer_key = CONSUMER_KEY; 15 | $this->consumer_secret = CONSUMER_SECRET; 16 | $this->oauth_token = ACCESS_TOKEN; 17 | $this->oauth_token_secret = ACCESS_TOKEN_SECRET; 18 | } 19 | 20 | public function tearDown(){} 21 | 22 | /** 23 | * @expectedException InvalidArgumentException 24 | */ 25 | public function testNoConsumerKey() 26 | { 27 | $plurk = new PlurkAPI(); 28 | } 29 | 30 | public function testInvalidConsumerKey() 31 | { 32 | $plurk = new PlurkAPI("abc", "def"); 33 | $json = $plurk->callAPI('/APP/Profile/getPublicProfile', 34 | array('user_id' => 'clsung'), true); 35 | $this->assertNull($json); 36 | $this->assertContains('40101:unknown application key', $plurk->error()); 37 | } 38 | 39 | public function testValidConsumerKey() 40 | { 41 | $plurk = new PlurkAPI($this->consumer_key, $this->consumer_secret); 42 | $json = $plurk->callAPI('/APP/Profile/getPublicProfile', array('user_id' => 'clsung'), true); 43 | $this->assertNotNull($json); 44 | $this->assertEquals(0, $plurk->errno()); 45 | } 46 | 47 | /** 48 | * @depends testValidConsumerKey 49 | */ 50 | public function testcurrUser() 51 | { 52 | $plurk = new PlurkAPI($this->consumer_key, $this->consumer_secret, 53 | $this->oauth_token, $this->oauth_token_secret 54 | ); 55 | $json = $plurk->callAPI('/APP/Users/currUser'); 56 | $this->assertNotNull($json); 57 | $this->assertEquals(0, $plurk->errno()); 58 | } 59 | 60 | /** 61 | * @depends testValidConsumerKey 62 | */ 63 | public function testGetOwnProfile() 64 | { 65 | $plurk = new PlurkAPI($this->consumer_key, $this->consumer_secret, 66 | $this->oauth_token, $this->oauth_token_secret 67 | ); 68 | $json = $plurk->callAPI('/APP/Profile/getOwnProfile'); 69 | $this->assertNotNull($json); 70 | $this->assertEquals(0, $plurk->errno()); 71 | } 72 | 73 | /** 74 | * @depends testValidConsumerKey 75 | */ 76 | public function testGetAccessToken() 77 | { 78 | $this->markTestIncomplete("Only when you need to get access token"); 79 | $oauth = new PlurkOAuth($this->consumer_key, $this->consumer_secret); 80 | $this->assertTrue($oauth->authorize()); 81 | } 82 | 83 | /** 84 | * @depends testValidConsumerKey 85 | */ 86 | public function testAPIplurkAdd() 87 | { 88 | $plurk = new PlurkAPI($this->consumer_key, $this->consumer_secret, 89 | $this->oauth_token, $this->oauth_token_secret 90 | ); 91 | $json = $plurk->callAPI('/APP/Timeline/plurkAdd', 92 | array ('content' => 'Post by plurkoauth which based on php', 'qualifier' => 'hates') 93 | ); 94 | $this->assertNotNull($json); 95 | $this->assertEquals(0, $plurk->errno()); 96 | } 97 | 98 | /** 99 | * @depends testValidConsumerKey 100 | */ 101 | public function testAPIresponseAdd() 102 | { 103 | $plurk = new PlurkAPI($this->consumer_key, $this->consumer_secret, 104 | $this->oauth_token, $this->oauth_token_secret 105 | ); 106 | # reply to http://www.plurk.com/p/cs770l 107 | $plurk_id = base_convert("cs770l",36,10); 108 | $json = $plurk->callAPI('/APP/Responses/responseAdd', 109 | array ('plurk_id' => $plurk_id, 110 | 'content' => 'Response test by plurkoauth', 'qualifier' => 'says') 111 | ); 112 | $this->assertNotNull($json); 113 | $this->assertEquals(0, $plurk->errno()); 114 | $json = $plurk->callAPI('/APP/Responses/responseAdd', 115 | array ('plurk_id' => $plurk_id, 116 | 'content' => 'plurkoauth is https://github.com/clsung/plurkoauth (here)', 'qualifier' => 'shares') 117 | ); 118 | $this->assertNotNull($json); 119 | $this->assertEquals(0, $plurk->errno()); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /plurkOAuth.php: -------------------------------------------------------------------------------- 1 | consumer = new Consumer($consumer_key, $consumer_secret); 30 | $this->sign_method = new SignatureMethod_HMAC_SHA1(); 31 | $this->params = array(); 32 | if (!empty($access_token) && !empty($access_secret)) { 33 | $this->authorize($access_token, $access_secret); 34 | } 35 | } 36 | 37 | function _get_request_token() { 38 | unset($this->token); 39 | $sapi_type = php_sapi_name(); 40 | if (substr($sapi_type, 0, 3) == 'cli') { # cli mode 41 | $content = $this->request(PLURK_REQUEST_TOKEN_PATH); 42 | } else { # web mode 43 | $content = $this->request(PLURK_REQUEST_TOKEN_PATH, null, array ( 44 | 'oauth_callback' => CALLBACK_URL)); 45 | } 46 | parse_str($content['content'], $this->request_token); 47 | setcookie('token', $this->request_token['oauth_token']); 48 | setcookie('secret', $this->request_token['oauth_token_secret']); 49 | } 50 | 51 | function _redirect_to_authorize() { 52 | printf ('Get Authorized', $this->baseURL.PLURK_AUTHORIZE_PATH, 53 | $this->request_token['oauth_token']); 54 | } 55 | 56 | function _get_verifier() { 57 | printf ("Access the following URL to get authorized: \n"); 58 | printf ("%s?oauth_token=%s\n", $this->baseURL.PLURK_AUTHORIZE_PATH, 59 | $this->request_token['oauth_token']); 60 | $handle = fopen ("php://stdin","r"); 61 | $yes_no = "n"; 62 | while (!strncmp($yes_no, "n", 1)) { 63 | printf("Input the verification number: "); 64 | $this->verifier = trim(fgets($handle)); 65 | printf("Are you sure? (y/N) "); 66 | $yes_no = trim(fgets($handle)); 67 | if (strncmp($yes_no, "y", 1)) $yes_no = "n"; 68 | } 69 | fclose($handle); 70 | } 71 | 72 | function _get_access_token() { 73 | $content = $this->request(PLURK_ACCESS_TOKEN_PATH, null, array ( 74 | 'oauth_token' => $this->request_token['oauth_token'], 75 | 'oauth_verifier' => $this->verifier,) 76 | ); 77 | parse_str($content['content'], $this->access_token); 78 | if (isset($this->access_token['oauth_token'])) { 79 | // XXX: print_r only for your first convenient, 80 | // you should store in config.php 81 | print_r($this->access_token); 82 | unset($this->token); 83 | $this->token = new Token( 84 | $this->access_token['oauth_token'], 85 | $this->access_token['oauth_token_secret']); 86 | return true; 87 | } 88 | return false; 89 | } 90 | 91 | function authorize_with_verifier($verifier = NULL) { 92 | $this->verifier = $verifier; 93 | $this->request_token['oauth_token'] = $_COOKIE['token']; 94 | $this->request_token['oauth_token_secret'] = $_COOKIE['secret']; 95 | $this->token = new Token( 96 | $this->request_token['oauth_token'], 97 | $this->request_token['oauth_token_secret']); 98 | return $this->_get_access_token(); 99 | } 100 | 101 | function get_access_token() { 102 | return $this->access_token; 103 | } 104 | 105 | function authorize($access_token = NULL, $access_secret = NULL) { 106 | $this->access_token['oauth_token'] = $access_token; 107 | if (!empty($access_secret)) { 108 | $this->access_token['oauth_token_secret'] = $access_secret; 109 | return true; 110 | } else { 111 | unset($this->access_token); 112 | $this->_get_request_token(); 113 | $sapi_type = php_sapi_name(); 114 | if (substr($sapi_type, 0, 3) == 'cli') { # cli mode 115 | $this->_get_verifier(); 116 | return $this->_get_access_token(); 117 | } else { # web mode 118 | $this->_redirect_to_authorize(); 119 | return false; 120 | } 121 | } 122 | } 123 | 124 | function request($path, $params = null, $content = null) { 125 | if (isset($params)) 126 | $params = array_merge ($params, $this->params); 127 | else 128 | $params = $this->params; 129 | if (isset ($this->access_token['oauth_token']) && 130 | isset ($this->access_token['oauth_token_secret'])) 131 | $this->token = new Token( 132 | $this->access_token['oauth_token'], 133 | $this->access_token['oauth_token_secret']); 134 | $client = new Client($this->consumer, $this->token); 135 | 136 | $this->status = 0; 137 | $this->response['reason'] = null; 138 | try { 139 | if (isset($content) and is_array($content)) { 140 | $content_params = array(); 141 | foreach ($content as $key => $value) { 142 | $content_params[] = 143 | sprintf('%s=%s', rawurlencode($key), rawurlencode($value)); 144 | } 145 | $content = implode('&', $content_params); 146 | } 147 | $resp = $client->request( 148 | $this->baseURL.$path, "POST", /*$request->to_header()*/null, $content); 149 | if ($json = json_decode($resp)) 150 | $resp = $json; 151 | if (isset($resp->error_text)) { 152 | $this->status = -1; 153 | $this->response['body'] = null; 154 | $this->response['reason'] = $resp->error_text; 155 | } else 156 | $this->response['body'] = $resp; 157 | } catch (PlurkOAuthException $e) { 158 | $this->status = -1; 159 | $this->response['reason'] = $e->getMessage(); 160 | } 161 | return array( 'content' => $this->response['body'], 162 | 'code' => $this->status, 163 | 'reason' => $this->response['reason'], 164 | ); 165 | } 166 | }; 167 | ?> 168 | -------------------------------------------------------------------------------- /OAuth.php: -------------------------------------------------------------------------------- 1 | sign($request, $consumer, $token); 24 | return $built == $signature; 25 | } 26 | } 27 | /** 28 | * SignatureMethod_HMAC_SHA1 29 | */ 30 | class SignatureMethod_HMAC_SHA1 extends SignatureMethod 31 | { 32 | public $name = 'HMAC-SHA1'; 33 | public function signing_base($request, $consumer, $token) { 34 | $sig = array( 35 | rawurlencode($request->method), 36 | rawurlencode($request->normalized_url), 37 | rawurlencode($request->get_normalized_parameters()), 38 | ); 39 | $key = sprintf("%s&", rawurlencode($consumer->secret)); 40 | if (isset($token)) { 41 | $key .= rawurlencode($token->secret); 42 | } 43 | $raw = implode('&', $sig); 44 | return array ($key, $raw); 45 | 46 | } 47 | 48 | public function sign($request, $consumer, $token) { 49 | $key_raw = $this->signing_base($request, $consumer, $token); 50 | $basestring = base64_encode (hash_hmac('sha1', $key_raw[1], $key_raw[0], true)); 51 | return rawurlencode($basestring); 52 | } 53 | 54 | public function check($request, $consumer, $token, $signature) { 55 | $built = $this->sign($request, $consumer, $token); 56 | return $built == $signature; 57 | } 58 | } 59 | 60 | /** 61 | * SignatureMethod_PLAINTEXT 62 | */ 63 | class SignatureMethod_PLAINTEXT extends SignatureMethod 64 | { 65 | public $name = 'PLAINTEXT'; 66 | public function signing_base($request, $consumer, $token) { 67 | $sig = sprintf("%s&", htmlspecialchars($consumer->secret)); 68 | if (isset($token)) 69 | $sig .= sprintf("%s&", htmlspecialchars($token->secret)); 70 | return array ($sig, $sig); 71 | } 72 | 73 | public function sign($request, $consumer, $token) { 74 | $base = $this->signing_base($request, $consumer, $token); 75 | return $base[1]; 76 | } 77 | 78 | } 79 | /** 80 | * Helper functions 81 | */ 82 | function make_timestamp() { 83 | return time(); 84 | } 85 | 86 | function generate_nonce($length = 8) { 87 | return generate_random_string($length); 88 | } 89 | 90 | function generate_verifier($length = 8) { 91 | return generate_random_string($length); 92 | } 93 | 94 | function generate_random_string($length = 8) { 95 | $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; 96 | $string = ''; 97 | for ($p = 0; $p < $length; $p++) { 98 | $string .= $chars[mt_rand(0, strlen($chars))]; 99 | } 100 | return $string; 101 | } 102 | /** 103 | * Consumer 104 | */ 105 | class Consumer 106 | { 107 | public $key; 108 | public $secret; 109 | 110 | function __construct($key, $secret) { 111 | $this->key = $key; 112 | $this->secret = $secret; 113 | } 114 | 115 | function __toString() { 116 | return urlencode( 117 | sprintf("oauth_consumer_key=%s&oauth_consumer_secret=%s", 118 | $this->key, $this->secret) 119 | ); 120 | } 121 | } 122 | 123 | /** 124 | * Client 125 | */ 126 | class Client 127 | { 128 | protected $method; 129 | protected $timeout; 130 | protected $cache; 131 | protected $proxy_info; 132 | const DEFAULT_POST_CONTENT_TYPE = 'application/x-www-form-urlencoded'; 133 | 134 | function __construct($consumer, $token = null, $cache=null, 135 | $timeout=null, $proxy_info=null) { 136 | if(! $consumer instanceof Consumer) 137 | throw new PlurkOAuthException("not Consumer"); 138 | if(isset($token) and ! $token instanceof Token) 139 | throw new PlurkOAuthException("not Token"); 140 | $this->consumer = $consumer; 141 | $this->token = $token; 142 | $this->method = new SignatureMethod_HMAC_SHA1(); 143 | } 144 | 145 | function set_signature_method($method) { 146 | if (!$method instanceof SignatureMethod) 147 | throw new PlurkOAuthException("not SignatureMethod"); 148 | $this->method = $method; 149 | } 150 | 151 | function request($uri, $method="GET", $headers=null, $body='') { 152 | 153 | if (!isset($headers)) 154 | $headers = array(); 155 | 156 | if ($method == "POST") { 157 | if (!isset($header['Content-Type'])) 158 | $headers['Content-Type'] = self::DEFAULT_POST_CONTENT_TYPE; 159 | } 160 | $is_form_encoded = (isset($headers) and 161 | $headers['Content-Type'] == self::DEFAULT_POST_CONTENT_TYPE); 162 | 163 | $parameters = null; 164 | 165 | if ($is_form_encoded and isset($body)) 166 | parse_str($body,$parameters); 167 | 168 | $req = Request::from_consumer_and_token($this->consumer, 169 | $this->token, $method, $uri, 170 | $parameters, $body, $is_form_encoded); 171 | $req->sign_request($this->method, $this->consumer, $this->token); 172 | $headers = array_merge($headers, $req->to_header()); 173 | 174 | $parsed = parse_url($uri); 175 | $realm = http_build_url( 176 | array( 177 | "scheme" => $parsed['scheme'], 178 | "host" => $parsed['host'], 179 | ) 180 | ); 181 | 182 | if ($is_form_encoded) { 183 | return http_parse_message( 184 | http_post_fields($uri, $parameters, null, array(headers => $headers)) 185 | )->body; 186 | } elseif ($method == "GET") { 187 | $uri = $req->to_url(); 188 | return http_get($uri, array(headers => $headers)); 189 | } else { 190 | $headers = $req->to_header($realm); 191 | return http_head($uri, array(headers => $headers)); 192 | } 193 | 194 | return http_request($method, $uri, $body, array(headers => $headers)); 195 | } 196 | } 197 | 198 | /** 199 | * Request 200 | */ 201 | class Request 202 | { 203 | protected static $version = '1.0'; 204 | private $props = array(); 205 | 206 | function __construct($method = "POST", $url = null, $params=null, 207 | $body='', $is_form_encoded=false) { 208 | $this->url = $url; 209 | $this->method = $method; 210 | $this->params = array(); 211 | if (isset($params)) { 212 | foreach ($params as $key => $value) { 213 | $this->params[$key] = $value; 214 | } 215 | } 216 | $this->method = $method; 217 | $this->body = $body; 218 | $this->is_form_encoded = $is_form_encoded; 219 | } 220 | 221 | public static function from_consumer_and_token( 222 | $consumer, $token=null, $http_method="POST", $http_url=null, 223 | $parameters=null, $body='', $is_form_encoded=False) { 224 | if (!isset($parameters)) 225 | $parameters = array(); 226 | 227 | $defaults = array ( 228 | 'oauth_consumer_key' => $consumer->key, 229 | 'oauth_timestamp' => make_timestamp(), 230 | 'oauth_nonce' => generate_nonce(), 231 | 'oauth_version' => Request::$version, 232 | ); 233 | $parameters = array_merge($defaults, $parameters); 234 | 235 | if (isset($token)) { 236 | $parameters['oauth_token'] = $token->key; 237 | if (isset($token->verifier)) 238 | $parameters['oauth_verifier'] = $token->verifier; 239 | } 240 | return new Request($http_method, $http_url, $parameters, 241 | $body, $is_form_encoded); 242 | } 243 | 244 | public function __set($prop, $value) { 245 | $this->props[$prop] = $value; 246 | if ($prop == 'url') { 247 | if (isset($value)) { 248 | $parsed = parse_url($this->props[$prop]); 249 | $this->props['normalized_url'] = 250 | $this->normalized_url = http_build_url( 251 | array( 252 | "scheme" => $parsed['scheme'], 253 | "host" => $parsed['host'], 254 | "path" => $parsed['path'] 255 | ) 256 | ); 257 | } else { 258 | unset($this->url); 259 | unset($this->normalized_url); 260 | } 261 | } 262 | } 263 | 264 | public function &__get($prop) { 265 | if ($prop == "method") { 266 | return strtoupper($this->props[$prop]); 267 | } 268 | return $this->props[$prop]; 269 | } 270 | 271 | function _get_timestamp_nonce() { 272 | return array($this->oauth_timestamp, $this->oauth_nonce); 273 | } 274 | 275 | 276 | function get_nonoauth_parameters() { 277 | foreach ($this->params as $key => $value) { 278 | if (strncmp($key, 'oauth_', 5)) 279 | $kv[$key] = $value; 280 | } 281 | return $kv; 282 | } 283 | 284 | function to_header($realm='') { 285 | $header_params = array(); 286 | foreach ($this->params as $key => $value) { 287 | if (!strncmp($key, 'oauth_', 5)) { 288 | $oauth_params[$key] = $value; 289 | array_push($header_params, sprintf('%s="%s"', $key, htmlspecialchars($value))); 290 | } 291 | } 292 | $params_header = implode(", ", $header_params); 293 | $auth_header = sprintf('OAuth realm="%s"', $realm); 294 | if (isset($params_header)) 295 | $auth_header = sprintf("%s, %s", $auth_header, $params_header); 296 | 297 | return array ('Authorization' => $auth_header); 298 | } 299 | 300 | function to_url() { // for GET 301 | // TODO 302 | } 303 | function to_postdata() { // for POST 304 | return $this->params; 305 | } 306 | 307 | function get_normalized_parameters() { 308 | $items = array(); 309 | foreach ($this->params as $key => $value) { 310 | if ($key == 'oauth_signature') 311 | continue; 312 | if (is_array($value)) { 313 | $mtems = array_merge($value, $items); 314 | } else { 315 | $items[$key] = $value; 316 | } 317 | } 318 | ksort($items); 319 | $item_parts = array(); 320 | foreach ($items as $key => $value) { 321 | $item_parts[] = 322 | sprintf("%s=%s",rawurlencode($key),rawurlencode($value)); 323 | } 324 | return implode('&', $item_parts); 325 | } 326 | 327 | function sign_request($signature_method, $consumer, $token) { 328 | if (!isset($this->params['oauth_consumer_key'])) { 329 | $this->params['oauth_consumer_key'] = $consumer->key; 330 | } 331 | if (isset($token) and !isset($this->params['oauth_token'])) 332 | $this->params['oauth_token'] = $token->key; 333 | $signature_method = new SignatureMethod_HMAC_SHA1(); 334 | $this->params['oauth_signature_method'] = $signature_method->name; 335 | $this->params['oauth_signature'] = 336 | $signature_method->sign($this, $consumer, $token); 337 | } 338 | 339 | } 340 | 341 | /** 342 | * Token 343 | */ 344 | class Token 345 | { 346 | public $key; 347 | public $secret; 348 | protected $callback; 349 | protected $callback_confirmed; 350 | protected $verifier; 351 | 352 | function __construct($key, $secret) { 353 | if (!isset($key) or !isset($secret)) 354 | throw new InvalidArgumentException("Must specify both key/secret!"); 355 | $this->key = $key; 356 | $this->secret = $secret; 357 | $this->callback = null; 358 | $this->callback_confirmed = false; 359 | $this->verifier = null; 360 | } 361 | 362 | function __toString() { 363 | $string = sprintf("oauth_consumer_key=%s&oauth_consumer_secret=%s", 364 | $this->key, $this->secret); 365 | if ($this->callback_confirmed) 366 | $string .= sprintf("&oauth_callback_confirmed=%s", 367 | $this->callback_confirmed); 368 | return urlencode($string); 369 | } 370 | 371 | function set_callback($callback) { 372 | $this->callback = $callback; 373 | $this->callback_confirmed = true; 374 | } 375 | 376 | function get_callback_url() { 377 | if (isset($this->verifier) and isset($this->callback)) { 378 | // TODO 379 | return $this->callback; 380 | } 381 | } 382 | 383 | function set_verifier($verifier = null) { 384 | if (isset($verifier)) 385 | $this->verifier = $verifier; 386 | else 387 | $this->verifier = gererate_verifier(); 388 | $this->callback_confirmed = true; 389 | } 390 | } 391 | ?> 392 | --------------------------------------------------------------------------------