DrupalOAuthConsumer.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. class DrupalOAuthConsumer extends OAuthConsumer {
  3. public $csid = 0;
  4. public $uid = 0;
  5. public $name = '';
  6. public $context = '';
  7. public $created = 0;
  8. public $changed = 0;
  9. public $callback_url = 'oob';
  10. public $configuration = array();
  11. public $provider_consumer = FALSE;
  12. public $in_database = FALSE;
  13. function __construct($key, $secret, $params = array()) {
  14. // Backwards compatibility with 6.x-3.0-beta3
  15. if (is_string($params)) {
  16. $callback_url = $params;
  17. if (func_num_args() > 4) {
  18. $params = func_get_arg(4);
  19. }
  20. else {
  21. $params = array();
  22. }
  23. $params['callback_url'] = $callback_url;
  24. }
  25. foreach ($params as $param_key => $value) {
  26. if (isset($this->$param_key)) {
  27. $this->$param_key = $value;
  28. }
  29. }
  30. if (!empty($this->created)) {
  31. $this->provider_consumer = TRUE;
  32. }
  33. parent::__construct($key, $secret, $this->callback_url);
  34. }
  35. /**
  36. * Writes the consumer to the database
  37. *
  38. * @return void
  39. */
  40. public function write() {
  41. $update = !empty($this->csid);
  42. $primary = $update ? array('csid') : array();
  43. if ($this->provider_consumer) {
  44. $this->changed = REQUEST_TIME;
  45. $values = array(
  46. 'consumer_key' => $this->key,
  47. 'created' => $this->created,
  48. 'changed' => $this->changed,
  49. 'uid' => $this->uid,
  50. 'name' => $this->name,
  51. 'context' => $this->context,
  52. 'callback_url' => $this->callback_url,
  53. );
  54. if ($update) {
  55. $values['csid'] = $this->csid;
  56. }
  57. else {
  58. $this->created = REQUEST_TIME;
  59. $values['created'] = $this->created;
  60. }
  61. $ready = drupal_write_record('oauth_common_provider_consumer', $values, $primary);
  62. if (!$ready) {
  63. throw new OAuthException("Couldn't save consumer");
  64. }
  65. }
  66. $values = array(
  67. 'key_hash' => sha1($this->key),
  68. 'consumer_key' => $this->key,
  69. 'secret' => $this->secret,
  70. 'configuration' => serialize(empty($this->configuration) ? array() : $this->configuration),
  71. );
  72. if ($update) {
  73. $values['csid'] = $this->csid;
  74. }
  75. drupal_write_record('oauth_common_consumer', $values, $primary);
  76. $this->csid = $values['csid'];
  77. $this->in_database = TRUE;
  78. if (!$update) {
  79. $values = array(
  80. 'csid' => $this->csid,
  81. 'consumer_key' => $this->key,
  82. );
  83. drupal_write_record('oauth_common_provider_consumer', $values, array('consumer_key'));
  84. }
  85. }
  86. /**
  87. * Deletes the consumer from the database
  88. *
  89. * @return void
  90. */
  91. public function delete() {
  92. self::deleteConsumer($this->csid);
  93. }
  94. /**
  95. * Deletes the consumer with the id from the database.
  96. *
  97. * @param string $csid
  98. * The consumer id.
  99. * @return void
  100. */
  101. public static function deleteConsumer($csid) {
  102. //TODO: Add compatibility layer?
  103. $condition = db_and()->condition('csid', $csid);
  104. db_delete('oauth_common_provider_token')
  105. ->condition('tid', db_select('oauth_common_token', 't')->condition($condition)->fields('t', array('tid')), 'IN')
  106. ->execute();
  107. foreach (array('oauth_common_token', 'oauth_common_provider_consumer', 'oauth_common_consumer') as $table) {
  108. db_delete($table)
  109. ->condition($condition)
  110. ->execute();
  111. }
  112. }
  113. /**
  114. * Deprecated - Gets the consumer with the specified key
  115. *
  116. * @param string $key
  117. * The key of the consumer to get
  118. * @param bool $provider_consumer
  119. * Optional. Whether the consumer we're about to load is a provider or
  120. * consumer consumer. Defaults to TRUE.
  121. * @return DrupalOAuthConsumer
  122. * The loaded consumer object or FALSE if load failed
  123. */
  124. public static function load($key, $provider_consumer = TRUE) {
  125. return DrupalOAuthConsumer::loadProviderByKey($key, $provider_consumer);
  126. }
  127. /**
  128. * Gets a provider consumer with the specified id
  129. *
  130. * @param int $id
  131. * The id of the consumer to get
  132. * @param boolean $load_provider_data
  133. * Whether to load provider related data or not
  134. * @return DrupalOAuthConsumer
  135. * The loaded consumer object or FALSE if load failed
  136. */
  137. public static function loadById($csid, $load_provider_data = TRUE) {
  138. $query = db_select('oauth_common_consumer', 'c');
  139. $query
  140. ->condition('c.csid', $csid)
  141. ->fields('c', array('csid', 'consumer_key', 'secret', 'configuration'));
  142. if ($load_provider_data) {
  143. $query->leftJoin('oauth_common_provider_consumer', 'pc', 'pc.csid = c.csid');
  144. $query->fields('pc', array('created', 'changed', 'uid', 'name', 'context', 'callback_url'));
  145. }
  146. return self::fromResult($query->execute());
  147. }
  148. /**
  149. * Gets a provider consumer with the specified key
  150. *
  151. * @param string $key
  152. * The key of the consumer to get
  153. * @param boolean $provider
  154. * Used internally for backwards compatibility with ::load()
  155. * @return DrupalOAuthConsumer
  156. * The loaded consumer object or FALSE if load failed
  157. */
  158. public static function loadProviderByKey($key, $provider = TRUE) {
  159. $query = db_select('oauth_common_consumer', 'c');
  160. $query
  161. ->condition('c.key_hash', sha1($key))
  162. ->fields('c', array('secret', 'configuration'));
  163. if ($provider) {
  164. $query->join('oauth_common_provider_consumer', 'pc', 'pc.csid = c.csid');
  165. $query->fields('pc');
  166. }
  167. else {
  168. // For backwards compatibility with deprecated DrupalOAuthConsumer::load() from 6.x-3.0-beta3
  169. $query->leftJoin('oauth_common_provider_consumer', 'pc', 'pc.csid = c.csid');
  170. $query
  171. ->fields('c', array('csid', 'consumer_key'))
  172. ->fields('pc', array('created', 'changed', 'uid', 'name', 'context', 'callback_url'))
  173. ->isNull('pc.csid');
  174. }
  175. return self::fromResult($query->execute());
  176. }
  177. /**
  178. * Constructs a consumer from a db-result resource
  179. *
  180. * @param resource $res
  181. * A database result resource
  182. * @return DrupalOAuthConsumer
  183. * The constructed consumer object or NULL if no rows could be read or construction failed
  184. */
  185. public static function fromResult($res) {
  186. //TODO: Ensure this works with old inputs?
  187. if ($data = $res->fetchAssoc()) {
  188. if (!empty($data['configuration'])) {
  189. $data['configuration'] = unserialize($data['configuration']);
  190. }
  191. $data['in_database'] = TRUE;
  192. return new DrupalOAuthConsumer($data['consumer_key'], $data['secret'], $data);
  193. }
  194. return NULL;
  195. }
  196. }