· 6 years ago · Jan 22, 2019, 07:48 PM
1<?php
2require_once APPPATH.'models/social/social.abstract.php';
3require_once APPPATH.'libraries/twitteroauth/twitteroauth.php';
4
5class Twitter extends Social
6{
7 const TABLE = "network_twitter";
8 const CONSUMER_KEY = 'REMOVED';
9 const CONSUMER_SECRET = 'REMOVED';
10 const OAUTH_CALLBACK = 'tools/oauth/twitter/callback';
11
12 private $id;
13 private $init = FALSE; //used on some of the functions where a uid is needed
14
15 public $twitterId;
16 public $oauthToken;
17 public $oauthTokenSecret;
18 public $username;
19 public $following;
20 public $followers;
21 public $picture;
22 public $tweets;
23 public $bio;
24 public $location;
25 public $url;
26 public $lastUpdated;
27 public $accounts = array();
28
29 function __construct()
30 {
31 parent::__construct();
32 }
33
34 function init($uid,$load=false)
35 {
36 $this->setUid($uid);
37
38 if($this->getUid() > 0) { $this->init = TRUE; }
39
40 $r = $this->db->query("SELECT id,oauthToken,oauthTokenSecret,lastUpdated from ".self::TABLE." where uid = ? LIMIT 1", array($this->getUid()));
41
42 if($r->num_rows() > 0)
43 {
44 $obj = $r->row();
45
46 if(strlen($obj->oauthToken) > 8 && strlen($obj->oauthTokenSecret) > 8)
47 {
48 $this->id = $obj->id;
49 $this->oauthToken = $obj->oauthToken;
50 $this->oauthTokenSecret = $obj->oauthTokenSecret;
51 $this->lastUpdated = $obj->lastUpdated;
52
53 //update twitter before grabbing info
54 $twitterConn = new TwitterOAuth(self::CONSUMER_KEY,self::CONSUMER_SECRET,$this->oauthToken,$this->oauthTokenSecret);
55 $twitterInfo = $twitterConn->get('account/verify_credentials');
56
57 if(!property_exists($twitterInfo, "error"))
58 {
59 $this->username = $twitterInfo->screen_name;
60 $this->following = $twitterInfo->friends_count;
61 $this->followers = $twitterInfo->followers_count;
62 $this->tweets = $twitterInfo->statuses_count;
63 $this->picture = $twitterInfo->profile_image_url;
64 $this->location = $twitterInfo->location;
65 $this->bio = $twitterInfo->description;
66 $this->twitterId = $twitterInfo->id;
67 $this->url = $twitterInfo->url;
68
69 $this->updateAccount();
70 }
71 else
72 {
73 $this->db->query("update ".self::TABLE. " set oauthToken = '', oauthTokenSecret = '' where id = ? LIMIT 1", array($this->id));
74
75 $this->oauthToken = "";
76 $this->oauthTokenSecret = "";
77
78 return FALSE;
79 }
80 }
81 else
82 {
83 return FALSE;
84 }
85 }
86 }
87
88 function updateAccount()
89 {
90 if(strlen($this->oauthToken) > 0)
91 {
92 $d = array($this->oauthToken, $this->oauthTokenSecret, $this->username, $this->followers, $this->tweets, $this->picture, $this->following, $this->twitterId, $this->location, $this->bio, $this->url, $this->twitterId,$this->getUid());
93
94 $this->db->query("UPDATE ".self::TABLE." SET
95 oauthToken = ?,
96 oauthTokenSecret = ?,
97 username = ?,
98 followers = ?,
99 tweets = ?,
100 picture = ?,
101 following = ?,
102 twitterID = ?,
103 location = ?,
104 bio = ?,
105 url = ?
106 WHERE twitterID = ? and uid = ?", $d);
107
108 return ($this->db->affected_rows() > 0 ? TRUE : FALSE);
109 }
110 else
111 {
112 return FALSE;
113 }
114 }
115
116 function addAccount($token="")
117 {
118 $d = array($this->uid,$this->twitterId,$this->oauthToken,$this->oauthTokenSecret,$this->username,$this->followers,$this->following,$this->tweets,$this->bio,$this->url, $this->picture);
119
120 $this->db->query("INSERT INTO ".self::TABLE." (uid,twitterId,oauthToken,oauthTokenSecret,username,followers,following,tweets,bio,url,picture) VALUES(?,?,?,?,?,?,?,?,?,?,?)", $d);
121
122 if($this->db->insert_id() > 0)
123 {
124 $this->id = $this->db->insert_id();
125
126 return TRUE;
127 }
128 else
129 {
130 return FALSE;
131 }
132 }
133
134 function loadAccounts()
135 {
136 if($this->getUid() > 0)
137 {
138 $r = $this->db->query("SELECT username from ".self::TABLE." where uid = ? order by lastUpdated DESC LIMIT 1", array($this->getUid()));
139
140 if($r->num_rows() > 0 )
141 {
142 $this->accounts[] = $r->row()->username;
143 }
144 }
145 }
146
147 function oauthRedirect()
148 {
149 $this->session->set_userdata('twitter_oauth_token_secret', null);
150 $connection = new TwitterOAuth(self::CONSUMER_KEY, self::CONSUMER_SECRET);
151
152 //Get temporary credentials.
153 $base = base_url();
154 $request_token = $connection->getRequestToken($base.self::OAUTH_CALLBACK);
155
156 $this->session->set_userdata('twitter_oauth_token_secret',$request_token['oauth_token_secret']);
157
158 // If last connection failed don't display authorization link.
159 switch ($connection->http_code) {
160 case 200:
161 // Build authorize URL and redirect user to Twitter.
162 $url = $connection->getAuthorizeURL($request_token['oauth_token']);
163 redirect($url);
164
165 break;
166 default:
167 // Show notification if something went wrong.
168 die('Could not connect to Twitter. Refresh the page or try again later.');
169 }
170 }
171
172 function oauthCallback($oauthToken, $oauthVerifier)
173 {
174 $oauth_token_secret = $this->session->userdata('twitter_oauth_token_secret');
175 if(!empty($oauth_token_secret))
176 {
177 // Create TwitteroAuth object with app key/secret and token key/secret from default phase
178 $connection = new TwitterOAuth(self::CONSUMER_KEY, self::CONSUMER_SECRET, $oauthToken, $oauth_token_secret);
179
180 // Request access tokens from twitter
181 $access_token = $connection->getAccessToken($oauthVerifier);
182 //If HTTP response is 200 continue otherwise send to connect page to retry
183 if (200 == $connection->http_code)
184 {
185 // The user has been verified and the access tokens can be saved for future use
186 $this->oauthFullStats($access_token);
187 }
188 else
189 {
190 //Save HTTP status for error dialog on connnect page.
191 redirect('tools/oauth/twitter/redirect');
192 }
193 }
194 }
195
196 function oauthFullStats($access_token)
197 {
198 if (empty($access_token) || empty($access_token['oauth_token']) || empty($access_token['oauth_token_secret']))
199 {
200 redirect('tools/oauth/twitter/redirect');
201 }
202
203 //Create a TwitterOauth object with consumer/user tokens.
204 $connection = new TwitterOAuth(self::CONSUMER_KEY, self::CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
205
206 // If method is set change API call made. Test is called by default.
207 $content = $connection->get('account/verify_credentials');
208
209 //set stuff for sessions based apps
210 $b = addslashes($content->description);
211 $b = str_replace(array("\r", "\r\n", "\n"), '', $b);
212
213 //add this twitter account to the users account
214 try
215 {
216 $this->init($this->session->userdata('uid'));
217
218 $this->twitterId = $content->id;
219 $this->oauthToken = $access_token['oauth_token'];
220 $this->oauthTokenSecret = $access_token['oauth_token_secret'];
221 $this->username = $content->screen_name;
222 $this->followers = $content->followers_count;
223 $this->following = $content->friends_count;
224 $this->tweets = $content->statuses_count;
225 $this->bio = $b;
226 $this->url = $content->url;
227 $this->picture = $content->profile_image_url;
228
229 //done with $content so free it up
230 unset($content);
231
232 if(!$this->updateAccount())
233 {
234 $this->addAccount();
235 }
236
237 //add id to session
238 $this->session->set_userdata('twitter', array('id'=>$this->id, 'username'=>$this->username));
239 $this->session->set_userdata('twitterBadge', 'yes');
240 }
241 catch(Exception $e)
242 {
243 log_message('error', $e->getMessage());
244 }
245 }
246
247 function verify()
248 {
249 if($this->init)
250 {
251 $twitterConn = new TwitterOAuth(self::CONSUMER_KEY,self::CONSUMER_SECRET,$this->oauthToken,$this->oauthTokenSecret);
252 $twitterInfo = $twitterConn->get('account/verify_credentials');
253
254 if(!empty($twitterInfo->id) && $twitterInfo->id > 0)
255 {
256 $this->badge = TRUE;
257 $this->session->set_userdata('twitterBadge', 'yes');
258
259 return TRUE;
260 }
261 else
262 {
263 $this->session->set_userdata('twitterBadge', 'no');
264
265 return FALSE;
266 }
267 }
268 }
269
270 function revokeBadge()
271 {
272 if($this->init)
273 {
274 $this->db->query("UPDATE ". self::TABLE . " set oauthToken = '', oauthTokenSecret = '' WHERE uid = ?", array($this->getUid()));
275
276 $this->session->set_userdata('twitterBadge', 'no');
277
278 return ($this->db->affected_rows() > 0 ? TRUE : FALSE);
279 }
280 }
281}
282?>