PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/oauth.php

https://bitbucket.org/jamierumbelow/codeigniter-oauth/
PHP | 225 lines | 149 code | 55 blank | 21 comment | 6 complexity | d9e48bb08b1c30375a64995cf12c14b4 MD5 | raw file
  1. <?php
  2. /**
  3. * oAuth Consumer Library
  4. *
  5. * Requires the PHP cURL extension and the MY_Input hack (for CI apps),
  6. * which is included in Flame
  7. *
  8. * @package Flame
  9. * @subpackage oAuth
  10. * @copyright 2009, Jamie Rumbelow
  11. * @author Jamie Rumbelow <http://www.jamierumbelow.net>
  12. * @license GPLv3
  13. * @version 1.1.0
  14. */
  15. class Oauth {
  16. public $site = "";
  17. public $request_token_path = "oauth/request_token";
  18. public $access_token_path = "oauth/access_token";
  19. public $authorize_path = "oauth/authorize";
  20. public $shared_key = '';
  21. public $shared_secret = '';
  22. public $signature_method = "HMAC-SHA1";
  23. public $token_secret = '';
  24. private $request_token = '';
  25. private $access_token = '';
  26. public $base_url = "";
  27. public $callback = "";
  28. private $curl;
  29. private $ci;
  30. public function __construct($base_url = FALSE) {
  31. $this->curl = curl_init();
  32. if ($base_url !== FALSE) {
  33. $this->base_url = $base_url;
  34. }
  35. // Are we in a CI app?
  36. if (function_exists("get_instance") && defined("APPPATH")) {
  37. $this->ci =& get_instance();
  38. $this->base_url = $this->ci->config->item("base_url");
  39. }
  40. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
  41. }
  42. public function set_site($site, $key, $secret) {
  43. $this->site = $site;
  44. $this->shared_key = $key;
  45. $this->shared_secret = $secret;
  46. }
  47. public function get_request_token() {
  48. $prot = array(
  49. "oauth_consumer_key" => $this->shared_key,
  50. "oauth_signature_method" => $this->signature_method,
  51. "oauth_timestamp" => time(),
  52. "oauth_nonce" => sha1(time()),
  53. );
  54. $url = $this->site . $this->request_token_path;
  55. $prot['oauth_signature'] = $this->sign_request("POST", $url, $prot);
  56. $request = $this->http_post($url, $prot);
  57. $request = explode('&', $request);
  58. $final = array();
  59. foreach ($request as $item) {
  60. $a = explode('=', $item);
  61. $final[$a[0]] = $a[1];
  62. }
  63. $this->request_token = $final['oauth_token'];
  64. $this->token_secret = $final['oauth_token_secret'];
  65. return $this;
  66. }
  67. public function get_user_authorization() {
  68. $url = $this->site . $this->authorize_path;
  69. $token = $this->request_token;
  70. $url .= "?oauth_token=".$token."&oauth_callback=".urlencode($this->base_url.$this->callback);
  71. header("Location: ".$url);
  72. }
  73. public function get_access_token($request_token) {
  74. $prot = array(
  75. "oauth_consumer_key" => $this->shared_key,
  76. "oauth_token" => $request_token,
  77. "oauth_signature_method" => $this->signature_method,
  78. "oauth_timestamp" => time(),
  79. "oauth_nonce" => sha1(time()),
  80. );
  81. $url = $this->site . $this->access_token_path;
  82. $prot['oauth_signature'] = $this->sign_request("POST", $url, $prot);
  83. $request = $this->http_post($url, $prot);
  84. $request = explode('&', $request);
  85. $final = array();
  86. foreach ($request as $item) {
  87. $a = explode('=', $item);
  88. $final[$a[0]] = $a[1];
  89. }
  90. $this->access_token = $final['oauth_token'];
  91. $this->token_secret = $final['oauth_token_secret'];
  92. return $request;
  93. }
  94. public function request($url, $params, $method = "POST") {
  95. $prot = array(
  96. "oauth_consumer_key" => $this->shared_key,
  97. "oauth_token" => $this->access_token,
  98. "oauth_signature_method" => $this->signature_method,
  99. "oauth_timestamp" => time(),
  100. "oauth_nonce" => sha1(time()),
  101. );
  102. $params = array_merge($params, $prot);
  103. $params['oauth_signature'] = $this->sign_request($method, $url, $params);
  104. $method = "http_".$method;
  105. $request = $this->$method($url, $params);
  106. return $request;
  107. }
  108. private function sign_request($method, $url, $params) {
  109. //Method is fine, so straight onto URL
  110. $url = rawurlencode($url);
  111. //Handle the request parameters
  112. uksort($params, 'strcmp');
  113. // Generate key=value pairs
  114. $pairs = array();
  115. foreach ($params as $key=>$value ) {
  116. if (is_array($value)) {
  117. // If the value is an array, it's because there are multiple
  118. // with the same key, sort them, then add all the pairs
  119. natsort($value);
  120. foreach ($value as $v2) {
  121. $pairs[] = $key . '=' . $v2;
  122. }
  123. } else {
  124. $pairs[] = $key . '=' . $value;
  125. }
  126. }
  127. $params = implode("&", $pairs);
  128. $params = rawurlencode($params);
  129. //Concat them all
  130. $base_string = $method . "&" . $url . "&" . $params;
  131. //Make the key
  132. $key = $this->shared_secret . "&" . $this->token_secret;
  133. switch ($this->signature_method) {
  134. case 'HMAC-SHA1':
  135. $str = hash_hmac('sha1', $base_string, $key, TRUE);
  136. $str = base64_encode($str);
  137. return rawurlencode($str);
  138. break;
  139. case 'PLAINTEXT':
  140. return rawurlencode($key);
  141. break;
  142. }
  143. }
  144. private function http_get($url) {
  145. curl_setopt($this->curl, CURLOPT_HTTPGET, TRUE);
  146. $request = $this->_request($url);
  147. return $request;
  148. }
  149. private function http_post($url, $prot, $params = array()) {
  150. curl_setopt($this->curl, CURLOPT_POST, TRUE);
  151. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
  152. $request = $this->_request($url, $prot);
  153. return $request;
  154. }
  155. private function _request($url, $prot) {
  156. $headers[] = "Expect:";
  157. $headers[] = "Authorization: OAuth realm=\"\", ".$this->_build_protocol_string($prot);
  158. curl_setopt($this->curl, CURLOPT_URL, $url);
  159. curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
  160. curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);
  161. return curl_exec($this->curl);
  162. }
  163. private function _build_protocol_string($prot) {
  164. $array = array();
  165. foreach ($prot as $key => $value) {
  166. $array[] = "$key=$value";
  167. }
  168. return implode(", ", $array);
  169. }
  170. }