/wp-content/plugins/socialauth-wp/hybridauth/Hybrid/Providers/Twitter.php

https://bitbucket.org/mshmsh5000/wp-demo · PHP · 165 lines · 100 code · 30 blank · 35 comment · 19 complexity · c0897567efc98836da60757c7a768066 MD5 · raw file

  1. <?php
  2. /*!
  3. * HybridAuth
  4. * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
  5. * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
  6. */
  7. /**
  8. * Hybrid_Providers_Twitter provider adapter based on OAuth1 protocol
  9. */
  10. class Hybrid_Providers_Twitter extends Hybrid_Provider_Model_OAuth1
  11. {
  12. /**
  13. * IDp wrappers initializer
  14. */
  15. function initialize()
  16. {
  17. parent::initialize();
  18. // Provider api end-points
  19. $this->api->api_base_url = "https://api.twitter.com/1/";
  20. $this->api->authorize_url = "https://api.twitter.com/oauth/authenticate";
  21. $this->api->request_token_url = "https://api.twitter.com/oauth/request_token";
  22. $this->api->access_token_url = "https://api.twitter.com/oauth/access_token";
  23. $this->api->curl_auth_header = false;
  24. }
  25. /**
  26. * load the user profile from the IDp api client
  27. */
  28. function getUserProfile()
  29. {
  30. $response = $this->api->get( 'account/verify_credentials.json' );
  31. // check the last HTTP status code returned
  32. if ( $this->api->http_code != 200 ){
  33. throw new Exception( "User profile request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 6 );
  34. }
  35. if ( ! is_object( $response ) || ! isset( $response->id ) ){
  36. throw new Exception( "User profile request failed! {$this->providerId} api returned an invalid response.", 6 );
  37. }
  38. # store the user profile.
  39. $this->user->profile->identifier = (property_exists($response,'id'))?$response->id:"";
  40. $this->user->profile->displayName = (property_exists($response,'screen_name'))?$response->screen_name:"";
  41. $this->user->profile->description = (property_exists($response,'description'))?$response->description:"";
  42. $this->user->profile->firstName = (property_exists($response,'name'))?$response->name:"";
  43. $this->user->profile->photoURL = (property_exists($response,'profile_image_url'))?$response->profile_image_url:"";
  44. $this->user->profile->profileURL = (property_exists($response,'screen_name'))?("http://twitter.com/".$response->screen_name):"";
  45. $this->user->profile->webSiteURL = (property_exists($response,'url'))?$response->url:"";
  46. $this->user->profile->region = (property_exists($response,'location'))?$response->location:"";
  47. return $this->user->profile;
  48. }
  49. /**
  50. * load the user contacts
  51. */
  52. function getUserContacts()
  53. {
  54. $parameters = array( 'cursor' => '-1' );
  55. $response = $this->api->get( 'friends/ids.json', $parameters );
  56. // check the last HTTP status code returned
  57. if ( $this->api->http_code != 200 ){
  58. throw new Exception( "User contacts request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
  59. }
  60. if( ! $response || ! count( $response->ids ) ){
  61. return ARRAY();
  62. }
  63. // 75 id per time should be okey
  64. $contactsids = array_chunk ( $response->ids, 75 );
  65. $contacts = ARRAY();
  66. foreach( $contactsids as $chunk ){
  67. $parameters = array( 'user_id' => implode( ",", $chunk ) );
  68. $response = $this->api->get( 'users/lookup.json', $parameters );
  69. // check the last HTTP status code returned
  70. if ( $this->api->http_code != 200 ){
  71. throw new Exception( "User contacts request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
  72. }
  73. if( $response && count( $response ) ){
  74. foreach( $response as $item ){
  75. $uc = new Hybrid_User_Contact();
  76. $uc->identifier = (property_exists($item,'id'))?$item->id:"";
  77. $uc->displayName = (property_exists($item,'name'))?$item->name:"";
  78. $uc->profileURL = (property_exists($item,'screen_name'))?("http://twitter.com/".$item->screen_name):"";
  79. $uc->photoURL = (property_exists($item,'profile_image_url'))?$item->profile_image_url:"";
  80. $uc->description = (property_exists($item,'description'))?$item->description:"";
  81. $contacts[] = $uc;
  82. }
  83. }
  84. }
  85. return $contacts;
  86. }
  87. /**
  88. * update user status
  89. */
  90. function setUserStatus( $status )
  91. {
  92. $parameters = array( 'status' => $status );
  93. $response = $this->api->post( 'statuses/update.json', $parameters );
  94. // check the last HTTP status code returned
  95. if ( $this->api->http_code != 200 ){
  96. throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
  97. }
  98. }
  99. /**
  100. * load the user latest activity
  101. * - timeline : all the stream
  102. * - me : the user activity only
  103. *
  104. * by default return the timeline
  105. */
  106. function getUserActivity( $stream )
  107. {
  108. if( $stream == "me" ){
  109. $response = $this->api->get( 'statuses/user_timeline.json' );
  110. }
  111. else{
  112. $response = $this->api->get( 'statuses/home_timeline.json' );
  113. }
  114. // check the last HTTP status code returned
  115. if ( $this->api->http_code != 200 ){
  116. throw new Exception( "User activity stream request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
  117. }
  118. if( ! $response ){
  119. return ARRAY();
  120. }
  121. $activities = ARRAY();
  122. foreach( $response as $item ){
  123. $ua = new Hybrid_User_Activity();
  124. $ua->id = (property_exists($item,'id'))?$item->id:"";
  125. $ua->date = (property_exists($item,'created_at'))?strtotime($item->created_at):"";
  126. $ua->text = (property_exists($item,'text'))?$item->text:"";
  127. $ua->user->identifier = (property_exists($item->user,'id'))?$item->user->id:"";
  128. $ua->user->displayName = (property_exists($item->user,'name'))?$item->user->name:"";
  129. $ua->user->profileURL = (property_exists($item->user,'screen_name'))?("http://twitter.com/".$item->user->screen_name):"";
  130. $ua->user->photoURL = (property_exists($item->user,'profile_image_url'))?$item->user->profile_image_url:"";
  131. $activities[] = $ua;
  132. }
  133. return $activities;
  134. }
  135. }