-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.php
More file actions
executable file
·229 lines (195 loc) · 6.01 KB
/
Copy pathtwitter.php
File metadata and controls
executable file
·229 lines (195 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/** Module created by Mathew Taylor,
http://mathewtaylor.co.uk - 14/08/2010 **/
require_once('twitteroauth/twitteroauth.php');
include_once('../config/config.inc.php');
include_once('../config/settings.inc.php');
include_once('../classes/Cookie.php');
class TwitterAbrahamWrapper{
private $consumer_key;
private $consumer_secret;
private $connection;
private static $instance;
private $oauth_token;
private $oauth_token_secret;
private $cookie;
public $connected;
public $error = NULL;
private function __construct()
{
$this->load_app_keys();
$this->load_tokens();
if($this->consumer_key && $this->consumer_secret)
{
$this->connect();
$this->connected = TRUE;
if($this->oauth_token && $this->oauth_token_secret){
$this->verify_account();
}
}
$this->cookie = new Cookie('ps');
}
public static function get_instance()
{
if(!self::$instance){
self::$instance = new TwitterAbrahamWrapper();
}
return self::$instance;
}
private function authenticate_user($callback_url)
{
$this->connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret);
$request_token = $this->connection->getRequestToken($callback_url);
$token = $request_token['oauth_token'];
//prestashop cookie
$this->cookie->oauth_token = $token;
$this->cookie->oauth_token_secret = $request_token['oauth_token_secret'];
switch ($this->connection->http_code) {
case 200:
/* Build authorize URL and redirect user to Twitter. */
$url = $this->connection->getAuthorizeURL($token);
header('Location: ' . $url);
break;
default:
print_r($this->connection);
/* Show notification if something went wrong. */
return FALSE;
}
}
private function callback()
{
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$this->connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret,
$this->cookie->oauth_token, $this->cookie->oauth_token_secret);
/* Request access tokens from twitter */
$access_token = $this->connection->getAccessToken($_REQUEST['oauth_verifier']);
$this->cookie->oauth_token = $access_token['oauth_token'];
$this->cookie->oauth_token_secret = $access_token['oauth_token_secret'];
/* If HTTP response is 200 continue otherwise send to connect page to retry */
if (200 == $this->connection->http_code) {
/* The user has been verified and the access tokens can be saved for future use */
$this->cookie->twitter_status = 'verified';
if(!$this->save_tokens()){
return FALSE;
}
return TRUE;
} else {
echo "http error";
print_r($this->connection->http_code);
/* Save HTTP status for error dialog on connnect page.*/
return FALSE;
}
}
private function save_tokens()
{
$query = 'UPDATE `'._DB_PREFIX_.'twesta`
SET `oauth_token` = "'. $this->cookie->oauth_token .'",
`oauth_token_secret` = "'. $this->cookie->oauth_token_secret .'"';
Db::getInstance()->Execute($query);
//TODO: check update.
return TRUE;
}
public function save_app_keys($consumer_key, $consumer_secret)
{
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$query = 'INSERT INTO `'._DB_PREFIX_.'twesta`
(consumer_key, consumer_secret)
VALUES ("'.$this->consumer_key.'", "'.$this->consumer_secret.'")';
Db::getInstance()->Execute($query);
//TODO: check update.
return TRUE;
}
private function load_app_keys()
{
$query = '
SELECT `consumer_key`, `consumer_secret`
FROM `'._DB_PREFIX_.'twesta`
';
if(!$result = Db::getInstance()->ExecuteS($query)){
$result = NULL;
}else{
foreach($result as $row){
$this->consumer_key = $row['consumer_key'];
$this->consumer_secret = $row['consumer_secret'];
}
}
}
private function load_tokens()
{
$query = '
SELECT *
FROM `'._DB_PREFIX_.'twesta`
';
if(!$result = Db::getInstance()->ExecuteS($query)){
$result = NULL;
}else{
foreach($result as $row){
$this->oauth_token = $row['oauth_token'];
$this->oauth_token_secret = $row['oauth_token_secret'];
}
}
}
public function verify_account(){
$result = $this->connection->get('account/verify_credentials');
if(isset($result->error)){
$this->connected = FALSE;
$this->connection = NULL;
$this->error = $result->error;
$this->disconnect();
}
}
public function tweet($tweet)
{
$response = $this->connection->post('statuses/update',
array('status' => $tweet));
print_r($response);
}
private function connect()
{
if($this->oauth_token && $this->oauth_token_secret){
$this->connection = new TwitterOAuth($this->consumer_key,
$this->consumer_secret,
$this->oauth_token,
$this->oauth_token_secret);
}else{
$this->connection = new TwitterOAuth($this->consumer_key,
$this->consumer_secret);
}
}
public function disconnect(){
$query = 'DELETE FROM`'._DB_PREFIX_.'twesta`';
Db::getInstance()->Execute($query);
return TRUE;
}
public function handle_connect($operation){
switch($operation){
case 'connect':
$callback = "http://{$_SERVER["SERVER_NAME"]}{$_SERVER['REQUEST_URI']}&operation=callback";
$status = $this->authenticate_user($callback);
if($status == FALSE){
$error = "Could not connect to Twitter Service";
return $error;
}
break;
case 'callback':
$oauth_token = $this->cookie->oauth_token;
$oauth_token_secret = $this->cookie->oauth_token_secret;
if(isset($oauth_token) && isset($oauth_token_secret)){
$result = $this->callback($oauth_token, $oauth_token_secret);
if($result == TRUE){
$this->connected = TRUE;
}
}else{
$error = 'Please retry your connection';
echo "error: " . $error;
return $error;
}
break;
case 'disconnect':
$this->disconnect();
break;
}
}
}
?>