/lib/oauth/core/consumer.php

https://github.com/spontaneus/OAuth · PHP · 231 lines · 81 code · 21 blank · 129 comment · 2 complexity · e8016ff40bea3353ce5c758a6d8b5a2f MD5 · raw file

  1. <?php
  2. namespace OAuth\Core;
  3. use \OAuth\Exception\Consumer as ConsumerException;
  4. /**
  5. * Consumer class that uses OAuthSimple to sign all OAuth requests and cURL to send the request
  6. * to a certain URL. This class can also be used to configure both OAuthSimple and cURL.
  7. *
  8. * ## Usage
  9. *
  10. * Basic usage of this class is as following:
  11. *
  12. * $oauth = new OAuth\Core\Consumer( consumer key, consumer secret );
  13. * $response = $oauth->request( url, options );
  14. *
  15. * By default cURL uses a very minimal configuration, if you want to modify these settings
  16. * (e.g. disabling SSL verification) you can do this as following:
  17. *
  18. * $oauth->curl_config('verify_ssl', FALSE);
  19. *
  20. * The method curl_config() accepts both traditional cURL configuration names such as
  21. * CURLOPT_SSL_VERIFYPEER but also has a few shortcuts such as "verify_ssl" and
  22. * "return_response". For more information see the documentation of the curl_config() method.
  23. *
  24. * @author Yorick Peterse, Isset Internet Professionals
  25. * @link http://yorickpeterse.com/ Website of Yorick Peterse
  26. * @link http://isset.nl/ Website of Isset Internet Professionals
  27. * @license https://github.com/isset/oauth-consumer/blob/master/license.txt The MIT license
  28. */
  29. class Consumer
  30. {
  31. /**
  32. * Array containing the global configuration for each cURL request.
  33. *
  34. * @author Yorick Peterse
  35. * @access private
  36. * @var array
  37. */
  38. private $curl_config = array(
  39. 'verify_ssl' => FALSE,
  40. 'return_output' => TRUE
  41. );
  42. /**
  43. * Variable containing a new instance of OAuthSimple.
  44. *
  45. * @author Yorick Peterse
  46. * @access private
  47. * @var OAuthSimple
  48. */
  49. private $oauth = NULL;
  50. /**
  51. * Array containing global configuration items used by OAuthSimple for each request.
  52. *
  53. * @author Yorick Peterse
  54. * @access public
  55. * @var array
  56. */
  57. public $oauth_config = array();
  58. /**
  59. * Array containing all cURL configuration aliases.
  60. *
  61. * @author Yorick Peterse
  62. * @access private
  63. * @var array
  64. */
  65. private $curl_config_aliases = array(
  66. 'verify_ssl' => CURLOPT_SSL_VERIFYPEER,
  67. 'return_output' => CURLOPT_RETURNTRANSFER,
  68. 'headers' => CURLOPT_HTTPHEADER,
  69. 'url' => CURLOPT_URL,
  70. 'http_get' => CURLOPT_HTTPGET,
  71. 'http_post' => CURLOPT_POST,
  72. 'http_put' => CURLOPT_PUT
  73. );
  74. /**
  75. * Variable containing an instance of cURL.
  76. *
  77. * @author Yorick Peterse
  78. * @access private
  79. * @var resource
  80. */
  81. private $curl = NULL;
  82. /**
  83. * Creates a new instance of both the Consumer and OAuthSimple class and configures
  84. * both classes based on the specified arguments.
  85. *
  86. * @example
  87. * $oauth = new OAuth\Core\Consumer('test.isset.nl', 'ads87as7132jhasd');
  88. *
  89. * @author Yorick Peterse
  90. * @since 0.1
  91. * @param string $consumer_key The consumer key used for all OAuth requests.
  92. * @param string $consumer_secret The consumer secret for all OAuth requests.
  93. * @param array $curl_config The default configuration items to use for all cURL
  94. * requests.
  95. * @return void
  96. */
  97. public function __construct($consumer_key, $consumer_secret, $curl_config = array())
  98. {
  99. $this->oauth = new OAuthSimple();
  100. $this->curl = curl_init();
  101. $this->oauth_config = array(
  102. 'consumer_key' => $consumer_key,
  103. 'shared_secret' => $consumer_secret
  104. );
  105. $curl_config = array_merge($this->curl_config, $curl_config);
  106. // Set the default cURL configuration items
  107. foreach ( $curl_config as $opt => $value )
  108. {
  109. $this->curl_config($opt, $value);
  110. }
  111. }
  112. /**
  113. * Builds and signs an OAuth request using OAuthSimple and returns the array containing
  114. * all details about the request. This method takes the same arguments as the request()
  115. * method but does not directly execute a cURL request.
  116. *
  117. * @author Yorick Peterse
  118. * @access public
  119. * @since 0.1
  120. * @see Consumer::request()
  121. * @return array
  122. */
  123. public function sign($url, $method = 'GET', $options = array(), $curl_options = array())
  124. {
  125. $options = array('parameters' => $options);
  126. $options['path'] = $url;
  127. $options['signatures'] = $this->oauth_config;
  128. foreach ( $curl_options as $opt => $value )
  129. {
  130. $this->curl_config($opt, $value);
  131. }
  132. // Sign the request
  133. $result = $this->oauth->sign($options);
  134. // Set the correct cURL config based on the request method
  135. switch ( strtolower($method) )
  136. {
  137. case 'get':
  138. $this->curl_config('http_get', TRUE);
  139. break;
  140. case 'post':
  141. $this->curl_config('http_post', TRUE);
  142. break;
  143. case 'put':
  144. $this->curl_config('http_post', TRUE);
  145. break;
  146. default:
  147. $this->curl_config(CURLOPT_CUSTOMREQUEST, $method);
  148. break;
  149. }
  150. return $result;
  151. }
  152. /**
  153. * Sends an OAuth request to the specified URL. Optional items can be specified as
  154. * an associative array in the second argument of this method.
  155. *
  156. * @example
  157. * $oauth = new OAuth\Core\Consumer('test.isset.nl', 'ads87as7132jhasd');
  158. * $response = $oauth->request('https://www.google.com/webmasters/tools/feeds/sites/');
  159. *
  160. * @author Yorick Peterse
  161. * @access public
  162. * @since 0.1
  163. * @param string $url The URL to send the OAuth request to.
  164. * @param array $options Associative array containing all options that will be sent
  165. * to OAuthSimple.
  166. * @param string $method The HTTP request method to execute (GET, POST, etc).
  167. * @param array $curl_options Associative array containing request specific options
  168. * for cURL. Global options should be set using curl_config().
  169. * @return string
  170. */
  171. public function request($url, $method = 'GET', $options = array(), $curl_options = array())
  172. {
  173. $result = $this->sign($url, $method, $options, $curl_options);
  174. $this->curl_config('url', $result['signed_url']);
  175. return curl_exec($this->curl);
  176. }
  177. /**
  178. * Helper method that resets the data in the OAuthSimple instance.
  179. *
  180. * @author Yorick Peterse
  181. * @access public
  182. * @since 0.1
  183. * @return void
  184. */
  185. public function reset()
  186. {
  187. $this->oauth->reset();
  188. }
  189. /**
  190. * Method that can be used to set global cURL options used for each request.
  191. * The first argument is a string or a cURL constant (e.g. CURLOPT_URL) and the second
  192. * argument the value for this option. When setting a key you can use the following
  193. * aliases:
  194. *
  195. * * verify_ssl: alias for CURLOPT_SSL_VERIFYPEER
  196. * * return_output: alias for CURLOPT_RETURNTRANSFER
  197. * * headers: alias for CURLOPT_HTTPHEADER, can both be an associave as well as an
  198. * index based array
  199. *
  200. */
  201. public function curl_config($key, $value)
  202. {
  203. // Replace the shortcut
  204. if ( in_array($key, array_keys($this->curl_config_aliases)) )
  205. {
  206. $key = $this->curl_config_aliases[$key];
  207. }
  208. curl_setopt($this->curl, $key, $value);
  209. }
  210. }