singlestore.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Token storage for a normal, single-blog installation. Works with multiple
  4. * users, but not recommended for a Multi-Site install (see multistore.php)
  5. * Uses CPT because that's what we have available to us and it's easy.
  6. *
  7. * @package Keyring
  8. */
  9. class Keyring_SingleStore extends Keyring_Store {
  10. var $unique_id = false;
  11. static function &init() {
  12. static $instance = false;
  13. if ( !$instance ) {
  14. register_post_type( 'kr_request_token', array(
  15. 'label' => __( 'Keyring Request Token', 'keyring' ),
  16. 'description' => __( 'Token or authentication details stored by Keyring. Request tokens are used during the authorization flow.', 'keyring' ),
  17. 'public' => false,
  18. ) );
  19. register_post_type( 'kr_access_token', array(
  20. 'label' => __( 'Keyring Access Token', 'keyring' ),
  21. 'description' => __( 'Token or authentication details stored by Keyring. Access tokens are used to make secure requests.', 'keyring' ),
  22. 'public' => false,
  23. ) );
  24. $instance = new Keyring_SingleStore;
  25. }
  26. return $instance;
  27. }
  28. function insert( $token ) {
  29. // Avoid duplicates by checking to see if this exists already
  30. $found = get_posts( array(
  31. 'numberposts' => 1,
  32. 'post_type' => 'kr_' . $token->type() . '_token',
  33. 'meta_key' => 'service',
  34. 'meta_value' => $token->get_name(),
  35. 'author' => get_current_user_id(),
  36. 's' => serialize( $token->token ), // Search the post content for this token
  37. 'exact' => true, // Require exact content match
  38. ) );
  39. if ( $found ) {
  40. $token->unique_id = $found[0]->ID;
  41. return $this->update( $token );
  42. }
  43. $post = array(
  44. 'post_type' => 'kr_' . $token->type() . '_token',
  45. 'post_status' => 'publish',
  46. 'post_content' => serialize( $token->token ),
  47. );
  48. $id = wp_insert_post( add_magic_quotes( $post ) );
  49. if ( $id ) {
  50. // Always record what service this token is for
  51. update_post_meta( $id, 'service', $token->get_name() );
  52. // Optionally include any meta related to this token
  53. foreach ( (array) $token->get_meta( false, true ) as $key => $val ) {
  54. update_post_meta( $id, $key, $val );
  55. }
  56. return $id;
  57. }
  58. return false;
  59. }
  60. function update( $token ) {
  61. if ( !$token->unique_id )
  62. return false;
  63. $id = $token->unique_id;
  64. $post = get_post( $id );
  65. if ( !$post )
  66. return false;
  67. $post->post_content = serialize( $token->token );
  68. wp_update_post( $post );
  69. foreach ( $token->get_meta( false, true ) as $key => $val ) {
  70. update_post_meta( $id, $key, $val );
  71. }
  72. return $id;
  73. }
  74. function delete( $args = array() ) {
  75. if ( !$args['id'] )
  76. return false;
  77. return wp_delete_post( $args['id'] );
  78. }
  79. function get_tokens( $args = array() ) {
  80. $defaults = array(
  81. 'type' => 'access',
  82. 'service' => false,
  83. 'user_id' => get_current_user_id(),
  84. 'blog_id' => get_current_blog_id(),
  85. );
  86. $args = wp_parse_args( $args, $defaults );
  87. $query = array(
  88. 'numberposts' => -1, // all
  89. 'post_type' => 'kr_' . $args['type'] . '_token',
  90. 'author' => $args['user_id'],
  91. );
  92. // Get tokens for a specific service
  93. if ( $args['service'] ) {
  94. $query['meta_key'] = 'service';
  95. $query['meta_value'] = $args['service'];
  96. }
  97. $token_type = 'request' == $args['type'] ? 'Keyring_Request_Token' : 'Keyring_Access_Token';
  98. $tokens = array();
  99. $posts = get_posts( $query );
  100. if ( count( $posts ) ) {
  101. foreach ( $posts as $post ) {
  102. $meta = get_post_meta( $post->ID );
  103. foreach ( $meta as $mid => $met ) {
  104. $meta[$mid] = $met[0];
  105. }
  106. $tokens[] = new $token_type(
  107. get_post_meta(
  108. $post->ID,
  109. 'service',
  110. true
  111. ),
  112. unserialize( $post->post_content ),
  113. $meta,
  114. $post->ID
  115. );
  116. }
  117. }
  118. return $tokens;
  119. }
  120. function get_token( $args = array() ) {
  121. $defaults = array(
  122. 'id' => false,
  123. 'type' => 'access',
  124. 'service' => false,
  125. 'user_id' => get_current_user_id(),
  126. 'blog_id' => get_current_blog_id(),
  127. );
  128. $args = wp_parse_args( $args, $defaults );
  129. if ( !$args['id'] && !$args['service'] )
  130. return false;
  131. $post = get_post( $args['id'] );
  132. if ( $post ) {
  133. $meta = get_post_meta( $post->ID );
  134. foreach ( $meta as $mid => $met ) {
  135. $meta[$mid] = $met[0];
  136. }
  137. $token_type = 'kr_request_token' == $post->post_type ? 'Keyring_Request_Token' : 'Keyring_Access_Token';
  138. return new $token_type(
  139. get_post_meta(
  140. $post->ID,
  141. 'service',
  142. true
  143. ),
  144. unserialize( $post->post_content ),
  145. $meta,
  146. $post->ID
  147. );
  148. }
  149. return false;
  150. }
  151. function count( $args = array() ) {
  152. return count( $this->get_tokens( $args ) );
  153. }
  154. }