flickr.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Flickr service definition for Keyring. Implementation of OAuth1a
  4. */
  5. class Keyring_Service_Flickr extends Keyring_Service_OAuth1 {
  6. const NAME = 'flickr';
  7. const LABEL = 'Flickr';
  8. function __construct() {
  9. parent::__construct();
  10. // Enable "basic" UI for entering key/secret
  11. if ( ! KEYRING__HEADLESS_MODE ) {
  12. add_action( 'keyring_flickr_manage_ui', array( $this, 'basic_ui' ) );
  13. add_filter( 'keyring_flickr_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  14. }
  15. $this->set_endpoint( 'request_token', 'http://www.flickr.com/services/oauth/request_token', 'GET' );
  16. $this->set_endpoint( 'authorize', 'http://www.flickr.com/services/oauth/authorize', 'GET' );
  17. $this->set_endpoint( 'access_token', 'http://www.flickr.com/services/oauth/access_token', 'GET' );
  18. $creds = $this->get_credentials();
  19. $this->app_id = $creds['app_id'];
  20. $this->key = $creds['key'];
  21. $this->secret = $creds['secret'];
  22. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  23. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  24. $this->requires_token( true );
  25. }
  26. function basic_ui_intro() {
  27. echo '<p>' . __( "To connect to Flickr, you'll need to <a href='http://www.flickr.com/services/apps/create/apply/?'>create an application at Flickr.com</a>. If this is a personal website then you can use a non-commercial key (which will be approved automatically).", 'keyring' ) . '</p>';
  28. echo '<p>' . __( "Once you've created your app, enter the API <strong>Key</strong> and <strong>Secret</strong> below (App ID is not required for Flickr apps).", 'keyring' ) . '</p>';
  29. }
  30. function build_token_meta( $token ) {
  31. // Need to make a request to get full information
  32. $this->set_token(
  33. new Keyring_Access_Token(
  34. $this->get_name(),
  35. new OAuthToken(
  36. $token['oauth_token'],
  37. $token['oauth_token_secret']
  38. )
  39. )
  40. );
  41. $url = "http://api.flickr.com/services/rest/?";
  42. $params = array(
  43. 'method' => 'flickr.people.getInfo',
  44. 'api_key' => $this->key,
  45. 'user_id' => $token['user_nsid'],
  46. );
  47. $url = $url . http_build_query( $params );
  48. $response = $this->request( $url, array( 'method' => 'GET' ) );
  49. if ( Keyring_Util::is_error( $response ) ) {
  50. $meta = array();
  51. } else {
  52. $meta = array(
  53. 'user_id' => $token['user_nsid'],
  54. 'username' => $token['username'],
  55. 'name' => $token['fullname'],
  56. 'picture' => "http://farm{$response->person->iconfarm}.staticflickr.com/{$response->person->iconserver}/buddyicons/{$token['user_nsid']}.jpg",
  57. );
  58. }
  59. return apply_filters( 'keyring_access_token_meta', $meta, 'flickr', $token, $response, $this );
  60. }
  61. function get_display( Keyring_Access_Token $token ) {
  62. $return = '';
  63. $meta = $token->get_meta();
  64. if ( !empty( $meta['name'] ) )
  65. return $meta['name'];
  66. else if ( !empty( $meta['username'] ) )
  67. return $meta['username'];
  68. }
  69. /**
  70. * Custom request method so that we can force JSON for Flickr, which otherwise
  71. * uses XML.
  72. * @param string $url The URL to request
  73. * @param array $params Any additional parameters requried for this reqeust
  74. * @return Mixed with either a Keyring_Error, or a decoded JSON response object
  75. */
  76. function request( $url, array $params = array() ) {
  77. // http://www.flickr.com/services/api/response.json.html
  78. $url = add_query_arg(
  79. array(
  80. 'format' => 'json', // Always return JSON
  81. 'nojsoncallback' => 1, // Don't wrap it in a callback
  82. ),
  83. $url );
  84. return parent::request( $url, $params );
  85. }
  86. /**
  87. * Since we're forcing all requests to be for JSON data, we can decode
  88. * all responses as JSON as well.
  89. * @param string $response Full content of the response
  90. * @return JSON object representation of the response
  91. */
  92. function parse_response( $response ) {
  93. return json_decode( $response );
  94. }
  95. }
  96. add_action( 'keyring_load_services', array( 'Keyring_Service_Flickr', 'init' ) );