-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_oauth.php
209 lines (182 loc) · 5.89 KB
/
twitter_oauth.php
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
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* DON'T BE A DICK PUBLIC LICENSE
*
* Version 1, December 2009
*
* Copyright (C) 2013 Webcomm Pty Ltd <[email protected]>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DON'T BE A DICK PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 1. Do whatever you like with the original work, just don't be a dick.
*
* Being a dick includes - but is not limited to - the following instances:
* 1a. Outright copyright infringement - Don't just copy this and change the name.
* 1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
* 1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
*
* 2. If you become rich through modifications, related works/services, or supporting the original work,
* share the love. Only a dick would make loads off this work and not buy the original works
* creator(s) a pint.
*
* 3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes
* you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.
*
* @package Twitter Widget
* @version 1.0.0
* @author Webcomm Pty Ltd
* @license DBAD
* @copyright (c) 2013, Webcomm Pty Ltd
* @link http://www.webcomm.com.au
*/
class Widget_Twitter_oauth extends Widgets
{
public $title = array(
'en' => 'Twitter Feed (1.1 API)',
);
public $description = array(
'en' => 'Display Twitter feeds on your website, with support for Twitter\'s 1.1 API',
);
public $author = 'Webcomm';
public $website = 'http://www.webcomm.com.au/';
public $version = '1.0';
public $fields = array(
array(
'field' => 'screen_name',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'consumer_key',
'label' => 'Consumer Key',
'rules' => 'required',
),
array(
'field' => 'consumer_secret',
'label' => 'Consumer Secret',
'rules' => 'required',
),
array(
'field' => 'access_token',
'label' => 'Access Token',
'rules' => 'required',
),
array(
'field' => 'access_token_secret',
'label' => 'Access Token',
'rules' => 'required',
),
array(
'field' => 'count',
'label' => 'Number of tweets',
'rules' => 'numeric'
),
);
public function run($options)
{
$cache_key = 'twitter-'.'-'.md5(serialize($options));
if ( ! $tweets = $this->pyrocache->get($cache_key))
{
try
{
$tweets = $this->fetch_tweets($options);
}
catch (\Exception $e)
{
return array(
'error' => $e->getMessage(),
'code' => $e->getCode(),
);
}
$this->pyrocache->write($tweets, $cache_key, 300);
}
$patterns = array(
// Detect URL's
'((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)' => '<a href="$0" target="_blank">$0</a>',
// Detect Email
'|[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,6}|i' => '<a href="mailto:$0">$0</a>',
// Detect Twitter @screen_names
'|@([a-z0-9-_]+)|i' => '<a href="http://twitter.com/$1" target="_blank">$0</a>',
// Detect Twitter #tags
'|#([a-z0-9-_]+)|i' => '<a href="http://twitter.com/search?q=%23$1" target="_blank">$0</a>'
);
foreach ($tweets as &$tweet)
{
$tweet->text = str_replace($options['screen_name'] . ': ', '', $tweet->text);
$tweet->text = preg_replace(array_keys($patterns), $patterns, $tweet->text);
}
// Store the feed items
return array(
'screen_name' => $options['screen_name'],
'tweets' => $tweets
);
}
protected function fetch_tweets($options)
{
// Variables
$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json';
$oauth = array(
'oauth_consumer_key' => $options['consumer_key'],
'oauth_token' => $options['access_token'],
'oauth_nonce' => (string)mt_rand(),
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0'
);
unset($options['consumer_key']);
unset($options['access_token']);
$consumer_secret = $options['consumer_secret'];
unset($options['consumer_secret']);
$access_token_secret = $options['access_token_secret'];
unset($options['access_token_secret']);
// Encode and merge perams
$options = array_map('rawurlencode', $options);
$params = array_merge($oauth, $options);
// Sort the params
asort($params);
ksort($params);
// Build request headers
$query = urldecode(http_build_query($params, '', '&'));
$url = 'https://'.$host.$path;
$base = $method.'&'.rawurlencode($url).'&'.rawurlencode($query);
$key = rawurlencode($consumer_secret).'&'.rawurlencode($access_token_secret);
$sign = rawurlencode(base64_encode(hash_hmac('sha1', $base, $key, true)));
// Build and format URL
$url .= '?'.http_build_query($options);
$url = str_replace('&', '&', $url);
// Assign the signature
$oauth['oauth_signature'] = $sign;
ksort($oauth);
// Twitter demo does this, so just incase
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);
// Setup CURL
$feed = curl_init();
curl_setopt_array($feed, array(
CURLOPT_HTTPHEADER => array('Authorization: OAuth '.urldecode(http_build_query($oauth, '', ', '))),
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
));
// Make request
$json = curl_exec($feed);
$http_status = curl_getinfo($feed, CURLINFO_HTTP_CODE);
curl_close($feed);
// Decode and return
$data = json_decode($json);
if ($http_status != 200)
{
$error = reset($data->errors);
throw new \Exception($error->message, $error->code);
}
return $data;
}
}