PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/library/twitter/oauth.php

http://github.com/pateketrueke/tetlphp
PHP | 205 lines | 100 code | 40 blank | 65 comment | 14 complexity | bb74b3dd81b22e84700f358eacec12d1 MD5 | raw file
  1. <?php
  2. /**
  3. * Basic OAuth library
  4. */
  5. if ( ! function_exists('curl_init')) {
  6. raise(ln('extension_missing', array('name' => 'cURL')));
  7. }
  8. // TODO: should be a standalone helper?
  9. /**
  10. * Encoding constants
  11. */
  12. define('MD5', 'md5');
  13. define('SHA1', 'sha1');
  14. /**#@-*/
  15. /**
  16. * Initialize OAuth object
  17. *
  18. * @param string Consumer key
  19. * @param string Consumer secret
  20. * @param string Auth user token
  21. * @param string Auth user token secret
  22. * @return mixed
  23. */
  24. function oauth_init($consumer_key = '', $consumer_secret = '', $token = '', $token_secret = '') {
  25. $R = new stdClass;
  26. $R->info = array();
  27. $R->token = $token;
  28. $R->token_secret = $token_secret;
  29. $R->consumer_key = $consumer_key;
  30. $R->consumer_secret = $consumer_secret;
  31. return $R;
  32. }
  33. /**
  34. * Prepare OAuth request
  35. *
  36. * @param mixed OAuth object
  37. * @param string Request URL
  38. * @param array Request vars
  39. * @param string Method
  40. * @param string SHA1|MD5
  41. * @return array
  42. */
  43. function oauth_parse($request, $url, $vars = array(), $method = GET, $callback = SHA1) {// TODO: improve?
  44. $data['oauth_version'] = '1.0';
  45. $data['oauth_timestamp'] = time();
  46. $data['oauth_signature_method'] = strtoupper("hmac-$callback");
  47. $data['oauth_consumer_key'] = $request->consumer_key;
  48. $data['oauth_nonce'] = md5(uniqid(mt_rand(), TRUE));
  49. $data['oauth_token'] = $request->token;
  50. foreach ($vars as $key => $val) {
  51. $vars[$key] = $val;
  52. }
  53. $test = array_merge($data, $vars);
  54. uksort($test, 'strcmp');
  55. $data['oauth_signature'] = oauth_encode(oauth_sign($request, $url, $test, $method, $callback));
  56. return array(
  57. 'request' => $vars,
  58. 'oauth' => $data,
  59. );
  60. }
  61. /**
  62. * Execute OAuth request
  63. *
  64. * @param mixed OAuth Object
  65. * @param string Request url
  66. * @param array Request vars
  67. * @param string Method
  68. * @return mixed
  69. */
  70. function oauth_exec($request, $url, $vars = array(), $method = GET) {
  71. // normalize URL
  72. $parts = @parse_url($url);
  73. $scheme = strtolower($parts['scheme']);
  74. $host = strtolower($parts['host']);
  75. $port = ! empty($parts['port']) ? (int) $parts['port'] : 80;
  76. $url = "$scheme://$host";
  77. ($port > 0) && (($scheme === 'http') && ($port !== 80)) OR (($scheme === 'https') && ($port !== 443)) && $out .= ":$port";
  78. $url .= $parts['path'];
  79. @parse_str($parts['query'], $test);
  80. ! empty($test) && $vars = array_merge($vars, $test);
  81. $vars = oauth_parse($request, $url, $vars, $method);
  82. $query = str_replace('+', '%20', http_build_query($vars['request'], NULL, '&'));
  83. $headers = array('Expect:');
  84. $resource = curl_init();
  85. // define method
  86. switch ($method) {
  87. case POST; // TODO: manage @uploads?
  88. ! empty($query) && curl_setopt($resource, CURLOPT_POSTFIELDS, trim($query, '='));
  89. curl_setopt($resource, CURLOPT_SSL_VERIFYHOST, FALSE);
  90. curl_setopt($resource, CURLOPT_SSL_VERIFYPEER, FALSE);
  91. curl_setopt($resource, CURLOPT_POST, TRUE);
  92. break;
  93. default;
  94. ! empty($query) && $url .= '?' . trim($query, '=');
  95. $method <> GET && curl_setopt($resource, CURLOPT_CUSTOMREQUEST, $method);
  96. break;
  97. }
  98. // request headers
  99. $oauth = 'Authorization: OAuth realm="' . $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '"';
  100. $oauth .= str_replace(' ', ',', attrs($vars['oauth']));
  101. $headers []= $oauth;
  102. curl_setopt($resource, CURLOPT_HTTPHEADER, $headers);
  103. // execute!
  104. curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);
  105. curl_setopt($resource, CURLOPT_URL, $url);
  106. $out = curl_exec($resource);
  107. $request->info = curl_getinfo($resource);
  108. $request->info['content_out'] = $out;
  109. return $out;
  110. }
  111. /**
  112. * Sign OAuth request
  113. *
  114. * @param mixed OAuth object
  115. * @param string Request url
  116. * @param array Request vars
  117. * @param string Method
  118. * @param string SHA1|MD5
  119. * @return string
  120. */
  121. function oauth_sign($request, $url, $vars = array(), $method = GET, $callback = SHA1) {
  122. $key = oauth_encode($request->consumer_secret) . '&' . oauth_encode($request->token_secret);
  123. $old = oauth_encode(str_replace('+', '%20', http_build_query($vars, NULL, '&')));
  124. $test = sprintf('%s&%s&%s', $method, oauth_encode($url), $old);
  125. if (function_exists('hash_hmac')) {
  126. $test = hash_hmac($callback, $test, $key, TRUE);
  127. } else {//TODO: fallback is still needed?
  128. if (strlen($key) > 64) {
  129. $key = pack('H*', $callback($key));
  130. }
  131. $key = str_pad($key, 64, chr(0x00));
  132. $lpad = str_repeat(chr(0x36), 64);
  133. $rpad = str_repeat(chr(0x5c), 64);
  134. $hmac = pack('H*', $callback(($key ^ $lpad) . $test));
  135. $test = pack('H*', $callback(($key ^ $rpad) . $hmac));
  136. }
  137. return base64_encode($test);
  138. }
  139. /**
  140. * RFC3986 encoding
  141. *
  142. * @param mixed Input string|Array
  143. * @return mixed
  144. */
  145. function oauth_encode($test) {
  146. if (is_scalar($test)) {
  147. $test = str_replace('%7E', '~', rawurlencode($test));
  148. } elseif (is_array($test)) {
  149. $test = array_map(__FUNCTION__, $test);
  150. }
  151. return $test;
  152. }
  153. /**
  154. * Assign tokens
  155. *
  156. * @param mixed OAuth object
  157. * @param string Token
  158. * @param string Token secret
  159. * @return void
  160. */
  161. function oauth_set($request, $token, $secret = NULL) {
  162. $request->token = $token;
  163. $request->token_secret = $secret;
  164. }
  165. /* EOF: ./library/twitter/oauth.php */