source: extensions/oAuth/include/hybridauth/Hybrid/Providers/Twitter.php @ 26555

Last change on this file since 26555 was 26555, checked in by mistic100, 10 years ago

update Hybridauth, update guides

File size: 7.5 KB
Line 
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/**
9* Hybrid_Providers_Twitter provider adapter based on OAuth1 protocol
10*/
11class Hybrid_Providers_Twitter extends Hybrid_Provider_Model_OAuth1
12{
13        /**
14        * IDp wrappers initializer
15        */
16        function initialize()
17        {
18                parent::initialize();
19
20                // Provider api end-points
21                $this->api->api_base_url      = "https://api.twitter.com/1.1/";
22                $this->api->authorize_url     = "https://api.twitter.com/oauth/authenticate";
23                $this->api->request_token_url = "https://api.twitter.com/oauth/request_token";
24                $this->api->access_token_url  = "https://api.twitter.com/oauth/access_token";
25
26                if ( isset( $this->config['api_version'] ) && $this->config['api_version'] ){
27                        $this->api->api_base_url  = "https://api.twitter.com/{$this->config['api_version']}/";
28                }
29 
30                if ( isset( $this->config['authorize'] ) && $this->config['authorize'] ){
31                        $this->api->authorize_url = "https://api.twitter.com/oauth/authorize";
32                }
33
34                $this->api->curl_auth_header  = false;
35        }
36
37        /**
38         * begin login step
39         */
40        function loginBegin()
41        {
42                $tokens = $this->api->requestToken( $this->endpoint );
43       
44                // request tokens as recived from provider
45                $this->request_tokens_raw = $tokens;
46       
47                // check the last HTTP status code returned
48                if ( $this->api->http_code != 200 ){
49                        throw new Exception( "Authentification failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 5 );
50                }
51       
52                if ( ! isset( $tokens["oauth_token"] ) ){
53                        throw new Exception( "Authentification failed! {$this->providerId} returned an invalid oauth token.", 5 );
54                }
55       
56                $this->token( "request_token"       , $tokens["oauth_token"] );
57                $this->token( "request_token_secret", $tokens["oauth_token_secret"] );
58       
59                // redirect the user to the provider authentication url with force_login
60                if ( isset( $this->config['force_login'] ) && $this->config['force_login'] ){
61                        Hybrid_Auth::redirect( $this->api->authorizeUrl( $tokens, array( 'force_login' => true ) ) );
62                }
63
64                // else, redirect the user to the provider authentication url
65                Hybrid_Auth::redirect( $this->api->authorizeUrl( $tokens ) );
66        }
67
68        /**
69        * load the user profile from the IDp api client
70        */
71        function getUserProfile()
72        {
73                $response = $this->api->get( 'account/verify_credentials.json' );
74
75                // check the last HTTP status code returned
76                if ( $this->api->http_code != 200 ){
77                        throw new Exception( "User profile request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 6 );
78                }
79
80                if ( ! is_object( $response ) || ! isset( $response->id ) ){
81                        throw new Exception( "User profile request failed! {$this->providerId} api returned an invalid response.", 6 );
82                }
83
84                # store the user profile. 
85                $this->user->profile->identifier  = (property_exists($response,'id'))?$response->id:"";
86                $this->user->profile->displayName = (property_exists($response,'screen_name'))?$response->screen_name:"";
87                $this->user->profile->description = (property_exists($response,'description'))?$response->description:"";
88                $this->user->profile->firstName   = (property_exists($response,'name'))?$response->name:""; 
89                $this->user->profile->photoURL    = (property_exists($response,'profile_image_url'))?$response->profile_image_url:"";
90                $this->user->profile->profileURL  = (property_exists($response,'screen_name'))?("http://twitter.com/".$response->screen_name):"";
91                $this->user->profile->webSiteURL  = (property_exists($response,'url'))?$response->url:""; 
92                $this->user->profile->region      = (property_exists($response,'location'))?$response->location:"";
93
94                return $this->user->profile;
95        }
96
97        /**
98        * load the user contacts
99        */
100        function getUserContacts()
101        {
102                $parameters = array( 'cursor' => '-1' ); 
103                $response  = $this->api->get( 'friends/ids.json', $parameters ); 
104
105                // check the last HTTP status code returned
106                if ( $this->api->http_code != 200 ){
107                        throw new Exception( "User contacts request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
108                }
109
110                if( ! $response || ! count( $response->ids ) ){
111                        return ARRAY();
112                }
113
114                // 75 id per time should be okey
115                $contactsids = array_chunk ( $response->ids, 75 );
116
117                $contacts    = ARRAY(); 
118
119                foreach( $contactsids as $chunk ){ 
120                        $parameters = array( 'user_id' => implode( ",", $chunk ) ); 
121                        $response   = $this->api->get( 'users/lookup.json', $parameters ); 
122
123                        // check the last HTTP status code returned
124                        if ( $this->api->http_code != 200 ){
125                                throw new Exception( "User contacts request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
126                        }
127
128                        if( $response && count( $response ) ){
129                                foreach( $response as $item ){ 
130                                        $uc = new Hybrid_User_Contact();
131
132                                        $uc->identifier   = (property_exists($item,'id'))?$item->id:"";
133                                        $uc->displayName  = (property_exists($item,'name'))?$item->name:"";
134                                        $uc->profileURL   = (property_exists($item,'screen_name'))?("http://twitter.com/".$item->screen_name):"";
135                                        $uc->photoURL     = (property_exists($item,'profile_image_url'))?$item->profile_image_url:"";
136                                        $uc->description  = (property_exists($item,'description'))?$item->description:""; 
137
138                                        $contacts[] = $uc;
139                                } 
140                        } 
141                }
142
143                return $contacts;
144        }
145
146        /**
147        * update user status
148        */ 
149        function setUserStatus( $status )
150        {
151                $parameters = array( 'status' => $status ); 
152                $response  = $this->api->post( 'statuses/update.json', $parameters ); 
153
154                // check the last HTTP status code returned
155                if ( $this->api->http_code != 200 ){
156                        throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
157                }
158        }
159
160        /**
161        * load the user latest activity 
162        *    - timeline : all the stream
163        *    - me       : the user activity only 
164        *
165        * by default return the timeline
166        */ 
167        function getUserActivity( $stream )
168        {
169                if( $stream == "me" ){
170                        $response  = $this->api->get( 'statuses/user_timeline.json' ); 
171                }                                                         
172                else{
173                        $response  = $this->api->get( 'statuses/home_timeline.json' ); 
174                }
175
176                // check the last HTTP status code returned
177                if ( $this->api->http_code != 200 ){
178                        throw new Exception( "User activity stream request failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
179                }
180
181                if( ! $response ){
182                        return ARRAY();
183                }
184
185                $activities = ARRAY();
186
187                foreach( $response as $item ){
188                        $ua = new Hybrid_User_Activity();
189
190                        $ua->id                 = (property_exists($item,'id'))?$item->id:"";
191                        $ua->date               = (property_exists($item,'created_at'))?strtotime($item->created_at):"";
192                        $ua->text               = (property_exists($item,'text'))?$item->text:"";
193
194                        $ua->user->identifier   = (property_exists($item->user,'id'))?$item->user->id:"";
195                        $ua->user->displayName  = (property_exists($item->user,'name'))?$item->user->name:""; 
196                        $ua->user->profileURL   = (property_exists($item->user,'screen_name'))?("http://twitter.com/".$item->user->screen_name):"";
197                        $ua->user->photoURL     = (property_exists($item->user,'profile_image_url'))?$item->user->profile_image_url:"";
198                       
199                        $activities[] = $ua;
200                }
201
202                return $activities;
203        }
204}
Note: See TracBrowser for help on using the repository browser.