instapaper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Instapaper uses xAuth, just to be difficult.
  4. */
  5. class Keyring_Service_Instapaper extends Keyring_Service_OAuth1 {
  6. const NAME = 'instapaper';
  7. const LABEL = 'Instapaper';
  8. function __construct() {
  9. parent::__construct();
  10. // Enable "basic" UI for entering key/secret, and the request UI for user/pass
  11. if ( ! KEYRING__HEADLESS_MODE ) {
  12. add_action( 'keyring_instapaper_manage_ui', array( $this, 'basic_ui' ) );
  13. add_filter( 'keyring_instapaper_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  14. add_action( 'keyring_instapaper_request_ui', array( $this, 'request_ui' ) );
  15. }
  16. $this->authorization_header = true;
  17. $this->set_endpoint( 'access_token', 'https://www.instapaper.com/api/1/oauth/access_token', 'POST' );
  18. $this->set_endpoint( 'verify', 'https://www.instapaper.com/api/1/account/verify_credentials', 'POST' );
  19. $creds = $this->get_credentials();
  20. $this->app_id = $creds['app_id'];
  21. $this->key = $creds['key'];
  22. $this->secret = $creds['secret'];
  23. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  24. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  25. $this->requires_token( true );
  26. }
  27. function basic_ui_intro() {
  28. echo '<p>' . sprintf( __( 'To use the Instapaper API, you need to get manually approved. <a href="%s">Apply here</a>, then wait for a reply email.', 'keyring' ), 'http://www.instapaper.com/main/request_oauth_consumer_token' ) . '</p>';
  29. echo '<p>' . __( "Once you get approved, you'll get an email back with your details. Copy the <strong>OAuth consumer key</strong> value into the <strong>API Key</strong> field, and the <strong>OAuth consumer secret</strong> value into the <strong>API Secret</strong> field and click save (you don't need an App ID value for Instapaper).", 'keyring' ) . '</p>';
  30. }
  31. /**
  32. * Mostly duplicated from HTTP Basic
  33. */
  34. function request_ui() {
  35. // Common Header
  36. echo '<div class="wrap">';
  37. screen_icon( 'ms-admin' );
  38. echo '<h2>' . __( 'Account Details', 'keyring' ) . '</h2>';
  39. // Handle errors
  40. if ( isset( $_GET['error'] ) ) {
  41. echo '<div id="keyring-admin-errors" class="updated"><ul>';
  42. switch ( $_GET['error'] ) {
  43. case '401':
  44. echo '<li>' . __( 'Your account details could not be confirmed, please try again.', 'keyring' ) . '</li>';
  45. break;
  46. case 'empty':
  47. echo '<li>' . __( 'Please make sure you enter a username and password.', 'keyring' ) . '</li>';
  48. break;
  49. }
  50. echo '</ul></div>';
  51. }
  52. // Even though it doesn't make too much sense, we support request tokens in HTTP Basic
  53. // to ensure consistency with other services
  54. $request_token = new Keyring_Request_Token(
  55. $this->get_name(),
  56. array(),
  57. apply_filters(
  58. 'keyring_request_token_meta',
  59. array(
  60. 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false
  61. ),
  62. $this->get_name(),
  63. array() // no token
  64. )
  65. );
  66. $request_token = apply_filters( 'keyring_request_token', $request_token, $this );
  67. $request_token_id = $this->store_token( $request_token );
  68. Keyring_Util::debug( 'xAuth/Instapaper Stored Request token ' . $request_token_id );
  69. echo apply_filters( 'keyring_' . $this->get_name() . '_request_ui_intro', '' );
  70. // Output basic form for collecting user/pass
  71. echo '<p>' . sprintf( __( 'Enter your username (or email address) and password for accessing <strong>%s</strong>:', 'keyring' ), $this->get_label() ) . '</p>';
  72. echo '<form method="post" action="">';
  73. echo '<input type="hidden" name="service" value="' . esc_attr( $this->get_name() ) . '" />';
  74. echo '<input type="hidden" name="action" value="verify" />';
  75. echo '<input type="hidden" name="state" value="' . esc_attr( $request_token_id ) . '" />';
  76. wp_nonce_field( 'keyring-verify', 'kr_nonce', false );
  77. wp_nonce_field( 'keyring-verify-' . $this->get_name(), 'nonce', false );
  78. echo '<table class="form-table">';
  79. echo '<tr><th scope="row">' . __( 'Email address', 'keyring' ) . '</th>';
  80. echo '<td><input type="text" name="username" value="" id="username" class="regular-text"></td></tr>';
  81. echo '<tr><th scope="row">' . __( 'Password', 'keyring' ) . '</th>';
  82. echo '<td><input type="password" name="password" value="" id="password" class="regular-text"></td></tr>';
  83. echo '</table>';
  84. echo '<p class="submitbox">';
  85. echo '<input type="submit" name="submit" value="' . __( 'Verify Details', 'keyring' ) . '" id="submit" class="button-primary">';
  86. echo '<a href="' . esc_attr( $_SERVER['HTTP_REFERER'] ) . '" class="submitdelete" id="logincancel" style="margin-left:2em;">' . __( 'Cancel', 'keyring' ) . '</a>';
  87. echo '</p>';
  88. echo '</form>';
  89. echo '</div>';
  90. ?><script type="text/javascript" charset="utf-8">
  91. jQuery( document ).ready( function() {
  92. jQuery( '#username' ).focus();
  93. } );
  94. </script><?php
  95. }
  96. function request_token() { }
  97. function verify_token() {
  98. if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-verify-' . $this->get_name() ) ) {
  99. Keyring::error( __( 'Invalid/missing verification nonce.', 'keyring' ) );
  100. exit;
  101. }
  102. // Load up the request token that got us here and globalize it
  103. if ( $_REQUEST['state'] ) {
  104. global $keyring_request_token;
  105. $state = (int) $_REQUEST['state'];
  106. $keyring_request_token = $this->store->get_token( array( 'id' => $state, 'type' => 'request' ) );
  107. Keyring_Util::debug( 'xAuth/Instapaper Loaded Request Token ' . $_REQUEST['state'] );
  108. Keyring_Util::debug( $keyring_request_token );
  109. // Remove request token, don't need it any more.
  110. $this->store->delete( array( 'id' => $state, 'type' => 'request' ) );
  111. }
  112. if ( !strlen( $_POST['username'] ) ) {
  113. $url = Keyring_Util::admin_url(
  114. $this->get_name(),
  115. array(
  116. 'action' => 'request',
  117. 'error' => 'empty',
  118. 'kr_nonce' => wp_create_nonce( 'keyring-request' )
  119. )
  120. );
  121. Keyring_Util::debug( $url );
  122. wp_safe_redirect( $url );
  123. exit;
  124. }
  125. $body = array(
  126. 'x_auth_mode' => 'client_auth',
  127. 'x_auth_password' => $_POST['password'],
  128. 'x_auth_username' => $_POST['username'],
  129. );
  130. ksort( $body );
  131. $this->set_token( new Keyring_Access_Token( $this->get_name(), null, array() ) );
  132. $res = $this->request( $this->access_token_url, array( 'method' => $this->access_token_method, 'raw_response' => true, 'body' => $body ) );
  133. Keyring_Util::debug( 'OAuth1 Access Token Response' );
  134. Keyring_Util::debug( $res );
  135. // We will get a 401 if they entered an incorrect user/pass combo. ::request
  136. // will then return a Keyring_Error
  137. if ( Keyring_Util::is_error( $res ) ) {
  138. $url = Keyring_Util::admin_url(
  139. $this->get_name(),
  140. array(
  141. 'action' => 'request',
  142. 'error' => '401',
  143. 'kr_nonce' => wp_create_nonce( 'keyring-request' )
  144. )
  145. );
  146. Keyring_Util::debug( $url );
  147. wp_safe_redirect( $url );
  148. exit;
  149. }
  150. parse_str( $res, $token );
  151. $meta = array_merge( array( 'username' => $_POST['username'] ), $this->build_token_meta( $token ) );
  152. $access_token = new Keyring_Access_Token(
  153. $this->get_name(),
  154. new OAuthToken( $token['oauth_token'], $token['oauth_token_secret'] ),
  155. $meta
  156. );
  157. $access_token = apply_filters( 'keyring_access_token', $access_token );
  158. // If we didn't get a 401, then we'll assume it's OK
  159. $id = $this->store_token( $access_token );
  160. $this->verified( $id, $keyring_request_token );
  161. }
  162. function parse_response( $response ) {
  163. return json_decode( $response );
  164. }
  165. function build_token_meta( $token ) {
  166. // Set the token so that we can make requests using it
  167. $this->set_token(
  168. new Keyring_Access_Token(
  169. $this->get_name(),
  170. new OAuthToken(
  171. $token['oauth_token'],
  172. $token['oauth_token_secret']
  173. )
  174. )
  175. );
  176. $response = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) );
  177. if ( Keyring_Util::is_error( $response ) ) {
  178. $meta = array();
  179. } else {
  180. $meta = array(
  181. 'user_id' => $response[0]->user_id,
  182. 'username' => $response[0]->username,
  183. 'name' => $response[0]->username,
  184. '_classname' => get_called_class(),
  185. );
  186. }
  187. return apply_filters( 'keyring_access_token_meta', $meta, 'instapaper', $token, $response, $this );
  188. }
  189. function get_display( Keyring_Access_Token $token ) {
  190. return $token->get_meta( 'username' );
  191. }
  192. function test_connection() {
  193. $response = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) );
  194. if ( !Keyring_Util::is_error( $response ) )
  195. return true;
  196. return $response;
  197. }
  198. }
  199. add_action( 'keyring_load_services', array( 'Keyring_Service_Instapaper', 'init' ) );