Version Description
- First version
Download this release
Release Info
Developer | stormuk |
Plugin | oAuth Twitter Feed for Developers |
Version | 1.0 |
Comparing to | |
See all releases |
Version 1.0
- README.md +87 -0
- StormTwitter.class.php +114 -0
- oauth/OAuth.php +874 -0
- oauth/twitteroauth.php +245 -0
- readme.txt +60 -0
- twitter-feed-for-developers-settings.php +76 -0
- twitter-feed-for-developers.php +35 -0
README.md
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Storm Twitter Feed for WordPress
|
2 |
+
================================
|
3 |
+
|
4 |
+
A Twitter API 1.1 compliant wordpress plugin that provides an array of a users timeline from Twitter for use by theme developers.
|
5 |
+
|
6 |
+
The new Twitter API requires you be oAuth'd before you can request a list of tweets, this means that all of the existing Twitter plugins that simply make an AJAX request for to the JSON API endpoint will break as of March 2013.
|
7 |
+
|
8 |
+
This is a major problem for the vast majority of websites that are currently using twitter, so we built a PHP class that implements all the new requirements for authentication and gives you an array of tweets out of the other end, for you to use in your PHP applications, or WordPress theme. You can find the stand-alone [StormTwitter](https://github.com/stormuk/storm-twitter) class on GitHub
|
9 |
+
|
10 |
+
This plugin wraps our Twitter class and provides a settings screen for easy integration into WordPress. However, it's definitely for developers - you only get a PHP array out of it that contains Twitter tweet objects. You'll still need to style the output and make it comply with the new display requirements.
|
11 |
+
|
12 |
+
This plugin does not provide sidebar widgets, shortcodes or any other form of drop-in functionality. You still need to do the styling, we've just done the heavy lifting for you!
|
13 |
+
|
14 |
+
Installation
|
15 |
+
============
|
16 |
+
|
17 |
+
Install the plugin using the plugin manager, or upload the files to your wp-content/plugins directory.
|
18 |
+
|
19 |
+
Navigate to the Settings > Twitter Feed Auth.
|
20 |
+
|
21 |
+
Here you'll find settings fields to authenticate with Twitter. You'll need to create a new Application on http://dev.twitter.com/apps.
|
22 |
+
|
23 |
+
Once you've create the app, scroll down the app's details page to find the oAuth section. Copy the consumer secret and consumer key into the settings page for the plugin. Then click the Create Access Token button at the bottom of the Twitter app page. Copy the Access token and Access token secret into the plugin's settings page. Finally, enter the Twitter username of the feed you want to access. Save the settings.
|
24 |
+
|
25 |
+
Now, anywhere in your theme files you can call the `getTweets()` function to retrieve an array of tweets.
|
26 |
+
|
27 |
+
You can then loop over the array and do whatever you want with it.
|
28 |
+
|
29 |
+
`<?php
|
30 |
+
$tweets = getTweets();
|
31 |
+
var_dump($tweets);
|
32 |
+
|
33 |
+
foreach($tweets as $tweet){
|
34 |
+
var_dump($tweet);
|
35 |
+
}
|
36 |
+
?>`
|
37 |
+
|
38 |
+
You can specify a number of tweets to return (up to 20) by passing a parameter to the function. For example, to display just the latest tweet you'd request `getTweets(1)`
|
39 |
+
|
40 |
+
Results are cached for 1 hour to help you avoid hitting the API limits.
|
41 |
+
|
42 |
+
TODO
|
43 |
+
====
|
44 |
+
|
45 |
+
* Move the screen name from the settings page to a function parameter so you can use the plugin to request different timelines
|
46 |
+
* Make the cache duration configurable
|
47 |
+
|
48 |
+
Credits
|
49 |
+
=======
|
50 |
+
|
51 |
+
Uses Abraham Williams's Twitter OAuth class
|
52 |
+
|
53 |
+
About
|
54 |
+
=====
|
55 |
+
|
56 |
+
Version: 1.0
|
57 |
+
|
58 |
+
Written by Liam Gladdy of Storm Consultancy - <http://www.stormconsultancy.co.uk>
|
59 |
+
|
60 |
+
Storm Consultancy are a web design and development agency based in Bath, UK.
|
61 |
+
|
62 |
+
If you are looking for a [Bath WordPress Developer](http://www.stormconsultancy.co.uk/services/bath-wordpress-developers), then [get in touch](http://www.stormconsultancy.co.uk/contact)!
|
63 |
+
|
64 |
+
License
|
65 |
+
=======
|
66 |
+
|
67 |
+
Copyright (c) 2012 Storm Consultancy (EU) Ltd,
|
68 |
+
<http://www.stormconsultancy.co.uk/>
|
69 |
+
|
70 |
+
Permission is hereby granted, free of charge, to any person obtaining
|
71 |
+
a copy of this software and associated documentation files (the
|
72 |
+
"Software"), to deal in the Software without restriction, including
|
73 |
+
without limitation the rights to use, copy, modify, merge, publish,
|
74 |
+
distribute, sublicense, and/or sell copies of the Software, and to
|
75 |
+
permit persons to whom the Software is furnished to do so, subject to
|
76 |
+
the following conditions:
|
77 |
+
|
78 |
+
The above copyright notice and this permission notice shall be
|
79 |
+
included in all copies or substantial portions of the Software.
|
80 |
+
|
81 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
82 |
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
83 |
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
84 |
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
85 |
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
86 |
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
87 |
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
StormTwitter.class.php
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
* The base class for the storm twitter feed for developers.
|
4 |
+
* This class provides all the things needed for the wordpress plugin, but in theory means you don't need to use it with wordpress.
|
5 |
+
* What could go wrong?
|
6 |
+
*/
|
7 |
+
|
8 |
+
class StormTwitter {
|
9 |
+
|
10 |
+
private $defaults = array(
|
11 |
+
'directory' => '',
|
12 |
+
'key' => '',
|
13 |
+
'secret' => '',
|
14 |
+
'token' => '',
|
15 |
+
'token_secret' => '',
|
16 |
+
'screenname' => ''
|
17 |
+
);
|
18 |
+
|
19 |
+
public $st_last_cached = false;
|
20 |
+
public $st_last_error = false;
|
21 |
+
|
22 |
+
function __construct($args = array()) {
|
23 |
+
$this->defaults = array_merge($this->defaults, $args);
|
24 |
+
}
|
25 |
+
|
26 |
+
function __toString() {
|
27 |
+
return print_r($this->defaults, true);
|
28 |
+
}
|
29 |
+
|
30 |
+
function getTweets($count = 20) {
|
31 |
+
if ($count > 20) $count = 20;
|
32 |
+
if ($count < 1) $count = 1;
|
33 |
+
|
34 |
+
$result = $this->checkValidCache();
|
35 |
+
|
36 |
+
if ($result !== false) {
|
37 |
+
return $this->cropTweets($result,$count);
|
38 |
+
}
|
39 |
+
|
40 |
+
//If we're here, we need to load.
|
41 |
+
$result = $this->oauthGetTweets();
|
42 |
+
|
43 |
+
if (isset($result['errors'])) {
|
44 |
+
return array('error'=>'Twitter said: '.$result['errors'][0]['message']);
|
45 |
+
} else {
|
46 |
+
return $this->cropTweets($result,$count);
|
47 |
+
}
|
48 |
+
|
49 |
+
}
|
50 |
+
|
51 |
+
function cropTweets($result,$count) {
|
52 |
+
return array_slice($result, 0, $count);
|
53 |
+
}
|
54 |
+
|
55 |
+
function getCacheLocation() {
|
56 |
+
return $this->defaults['directory'].'.tweetcache';
|
57 |
+
}
|
58 |
+
|
59 |
+
function checkValidCache() {
|
60 |
+
$file = $this->getCacheLocation();
|
61 |
+
if (is_file($file)) {
|
62 |
+
$cache = file_get_contents($file);
|
63 |
+
$cache = @json_decode($cache,true);
|
64 |
+
if (count($cache) != 2) {
|
65 |
+
unlink($file);
|
66 |
+
return false;
|
67 |
+
}
|
68 |
+
if (!isset($cache['time']) || !isset($cache['tweets'])) {
|
69 |
+
unlink($file);
|
70 |
+
return false;
|
71 |
+
}
|
72 |
+
if ($cache['time'] < (time() - 1)) {
|
73 |
+
$result = $this->oauthGetTweets();
|
74 |
+
if (!isset($result['errors'])) {
|
75 |
+
return $result;
|
76 |
+
}
|
77 |
+
}
|
78 |
+
return $cache['tweets'];
|
79 |
+
} else {
|
80 |
+
return false;
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
function oauthGetTweets() {
|
85 |
+
$key = $this->defaults['key'];
|
86 |
+
$secret = $this->defaults['secret'];
|
87 |
+
$token = $this->defaults['token'];
|
88 |
+
$token_secret = $this->defaults['token_secret'];
|
89 |
+
$screenname = $this->defaults['screenname'];
|
90 |
+
|
91 |
+
if (empty($key)) return array('error'=>'Missing Consumer Key - Check Settings');
|
92 |
+
if (empty($secret)) return array('error'=>'Missing Consumer Secret - Check Settings');
|
93 |
+
if (empty($token)) return array('error'=>'Missing Access Token - Check Settings');
|
94 |
+
if (empty($token_secret)) return array('error'=>'Missing Access Token Secret - Check Settings');
|
95 |
+
if (empty($screenname)) return array('error'=>'Missing Twitter Feed Screen Name - Check Settings');
|
96 |
+
|
97 |
+
$connection = new TwitterOAuth($key, $secret, $token, $token_secret);
|
98 |
+
$result = $connection->get('statuses/user_timeline', array('screen_name' => $screenname, 'count' => 20, 'trim_user' => true));
|
99 |
+
|
100 |
+
if (!isset($result['errors'])) {
|
101 |
+
$cache['time'] = time();
|
102 |
+
$cache['tweets'] = $result;
|
103 |
+
$file = $this->getCacheLocation();
|
104 |
+
file_put_contents($file,json_encode($cache));
|
105 |
+
$this->st_last_cached = $cache['time'];
|
106 |
+
} else {
|
107 |
+
$last_error = '['.date('r').'] Twitter error: '.$result['errors'][0]['message'];
|
108 |
+
$this->st_last_error = $last_error;
|
109 |
+
}
|
110 |
+
|
111 |
+
return $result;
|
112 |
+
|
113 |
+
}
|
114 |
+
}
|
oauth/OAuth.php
ADDED
@@ -0,0 +1,874 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
// vim: foldmethod=marker
|
3 |
+
|
4 |
+
/* Generic exception class
|
5 |
+
*/
|
6 |
+
class OAuthException extends Exception {
|
7 |
+
// pass
|
8 |
+
}
|
9 |
+
|
10 |
+
class OAuthConsumer {
|
11 |
+
public $key;
|
12 |
+
public $secret;
|
13 |
+
|
14 |
+
function __construct($key, $secret, $callback_url=NULL) {
|
15 |
+
$this->key = $key;
|
16 |
+
$this->secret = $secret;
|
17 |
+
$this->callback_url = $callback_url;
|
18 |
+
}
|
19 |
+
|
20 |
+
function __toString() {
|
21 |
+
return "OAuthConsumer[key=$this->key,secret=$this->secret]";
|
22 |
+
}
|
23 |
+
}
|
24 |
+
|
25 |
+
class OAuthToken {
|
26 |
+
// access tokens and request tokens
|
27 |
+
public $key;
|
28 |
+
public $secret;
|
29 |
+
|
30 |
+
/**
|
31 |
+
* key = the token
|
32 |
+
* secret = the token secret
|
33 |
+
*/
|
34 |
+
function __construct($key, $secret) {
|
35 |
+
$this->key = $key;
|
36 |
+
$this->secret = $secret;
|
37 |
+
}
|
38 |
+
|
39 |
+
/**
|
40 |
+
* generates the basic string serialization of a token that a server
|
41 |
+
* would respond to request_token and access_token calls with
|
42 |
+
*/
|
43 |
+
function to_string() {
|
44 |
+
return "oauth_token=" .
|
45 |
+
OAuthUtil::urlencode_rfc3986($this->key) .
|
46 |
+
"&oauth_token_secret=" .
|
47 |
+
OAuthUtil::urlencode_rfc3986($this->secret);
|
48 |
+
}
|
49 |
+
|
50 |
+
function __toString() {
|
51 |
+
return $this->to_string();
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
/**
|
56 |
+
* A class for implementing a Signature Method
|
57 |
+
* See section 9 ("Signing Requests") in the spec
|
58 |
+
*/
|
59 |
+
abstract class OAuthSignatureMethod {
|
60 |
+
/**
|
61 |
+
* Needs to return the name of the Signature Method (ie HMAC-SHA1)
|
62 |
+
* @return string
|
63 |
+
*/
|
64 |
+
abstract public function get_name();
|
65 |
+
|
66 |
+
/**
|
67 |
+
* Build up the signature
|
68 |
+
* NOTE: The output of this function MUST NOT be urlencoded.
|
69 |
+
* the encoding is handled in OAuthRequest when the final
|
70 |
+
* request is serialized
|
71 |
+
* @param OAuthRequest $request
|
72 |
+
* @param OAuthConsumer $consumer
|
73 |
+
* @param OAuthToken $token
|
74 |
+
* @return string
|
75 |
+
*/
|
76 |
+
abstract public function build_signature($request, $consumer, $token);
|
77 |
+
|
78 |
+
/**
|
79 |
+
* Verifies that a given signature is correct
|
80 |
+
* @param OAuthRequest $request
|
81 |
+
* @param OAuthConsumer $consumer
|
82 |
+
* @param OAuthToken $token
|
83 |
+
* @param string $signature
|
84 |
+
* @return bool
|
85 |
+
*/
|
86 |
+
public function check_signature($request, $consumer, $token, $signature) {
|
87 |
+
$built = $this->build_signature($request, $consumer, $token);
|
88 |
+
return $built == $signature;
|
89 |
+
}
|
90 |
+
}
|
91 |
+
|
92 |
+
/**
|
93 |
+
* The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104]
|
94 |
+
* where the Signature Base String is the text and the key is the concatenated values (each first
|
95 |
+
* encoded per Parameter Encoding) of the Consumer Secret and Token Secret, separated by an '&'
|
96 |
+
* character (ASCII code 38) even if empty.
|
97 |
+
* - Chapter 9.2 ("HMAC-SHA1")
|
98 |
+
*/
|
99 |
+
class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {
|
100 |
+
function get_name() {
|
101 |
+
return "HMAC-SHA1";
|
102 |
+
}
|
103 |
+
|
104 |
+
public function build_signature($request, $consumer, $token) {
|
105 |
+
$base_string = $request->get_signature_base_string();
|
106 |
+
$request->base_string = $base_string;
|
107 |
+
|
108 |
+
$key_parts = array(
|
109 |
+
$consumer->secret,
|
110 |
+
($token) ? $token->secret : ""
|
111 |
+
);
|
112 |
+
|
113 |
+
$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
|
114 |
+
$key = implode('&', $key_parts);
|
115 |
+
|
116 |
+
return base64_encode(hash_hmac('sha1', $base_string, $key, true));
|
117 |
+
}
|
118 |
+
}
|
119 |
+
|
120 |
+
/**
|
121 |
+
* The PLAINTEXT method does not provide any security protection and SHOULD only be used
|
122 |
+
* over a secure channel such as HTTPS. It does not use the Signature Base String.
|
123 |
+
* - Chapter 9.4 ("PLAINTEXT")
|
124 |
+
*/
|
125 |
+
class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {
|
126 |
+
public function get_name() {
|
127 |
+
return "PLAINTEXT";
|
128 |
+
}
|
129 |
+
|
130 |
+
/**
|
131 |
+
* oauth_signature is set to the concatenated encoded values of the Consumer Secret and
|
132 |
+
* Token Secret, separated by a '&' character (ASCII code 38), even if either secret is
|
133 |
+
* empty. The result MUST be encoded again.
|
134 |
+
* - Chapter 9.4.1 ("Generating Signatures")
|
135 |
+
*
|
136 |
+
* Please note that the second encoding MUST NOT happen in the SignatureMethod, as
|
137 |
+
* OAuthRequest handles this!
|
138 |
+
*/
|
139 |
+
public function build_signature($request, $consumer, $token) {
|
140 |
+
$key_parts = array(
|
141 |
+
$consumer->secret,
|
142 |
+
($token) ? $token->secret : ""
|
143 |
+
);
|
144 |
+
|
145 |
+
$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
|
146 |
+
$key = implode('&', $key_parts);
|
147 |
+
$request->base_string = $key;
|
148 |
+
|
149 |
+
return $key;
|
150 |
+
}
|
151 |
+
}
|
152 |
+
|
153 |
+
/**
|
154 |
+
* The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in
|
155 |
+
* [RFC3447] section 8.2 (more simply known as PKCS#1), using SHA-1 as the hash function for
|
156 |
+
* EMSA-PKCS1-v1_5. It is assumed that the Consumer has provided its RSA public key in a
|
157 |
+
* verified way to the Service Provider, in a manner which is beyond the scope of this
|
158 |
+
* specification.
|
159 |
+
* - Chapter 9.3 ("RSA-SHA1")
|
160 |
+
*/
|
161 |
+
abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
|
162 |
+
public function get_name() {
|
163 |
+
return "RSA-SHA1";
|
164 |
+
}
|
165 |
+
|
166 |
+
// Up to the SP to implement this lookup of keys. Possible ideas are:
|
167 |
+
// (1) do a lookup in a table of trusted certs keyed off of consumer
|
168 |
+
// (2) fetch via http using a url provided by the requester
|
169 |
+
// (3) some sort of specific discovery code based on request
|
170 |
+
//
|
171 |
+
// Either way should return a string representation of the certificate
|
172 |
+
protected abstract function fetch_public_cert(&$request);
|
173 |
+
|
174 |
+
// Up to the SP to implement this lookup of keys. Possible ideas are:
|
175 |
+
// (1) do a lookup in a table of trusted certs keyed off of consumer
|
176 |
+
//
|
177 |
+
// Either way should return a string representation of the certificate
|
178 |
+
protected abstract function fetch_private_cert(&$request);
|
179 |
+
|
180 |
+
public function build_signature($request, $consumer, $token) {
|
181 |
+
$base_string = $request->get_signature_base_string();
|
182 |
+
$request->base_string = $base_string;
|
183 |
+
|
184 |
+
// Fetch the private key cert based on the request
|
185 |
+
$cert = $this->fetch_private_cert($request);
|
186 |
+
|
187 |
+
// Pull the private key ID from the certificate
|
188 |
+
$privatekeyid = openssl_get_privatekey($cert);
|
189 |
+
|
190 |
+
// Sign using the key
|
191 |
+
$ok = openssl_sign($base_string, $signature, $privatekeyid);
|
192 |
+
|
193 |
+
// Release the key resource
|
194 |
+
openssl_free_key($privatekeyid);
|
195 |
+
|
196 |
+
return base64_encode($signature);
|
197 |
+
}
|
198 |
+
|
199 |
+
public function check_signature($request, $consumer, $token, $signature) {
|
200 |
+
$decoded_sig = base64_decode($signature);
|
201 |
+
|
202 |
+
$base_string = $request->get_signature_base_string();
|
203 |
+
|
204 |
+
// Fetch the public key cert based on the request
|
205 |
+
$cert = $this->fetch_public_cert($request);
|
206 |
+
|
207 |
+
// Pull the public key ID from the certificate
|
208 |
+
$publickeyid = openssl_get_publickey($cert);
|
209 |
+
|
210 |
+
// Check the computed signature against the one passed in the query
|
211 |
+
$ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
|
212 |
+
|
213 |
+
// Release the key resource
|
214 |
+
openssl_free_key($publickeyid);
|
215 |
+
|
216 |
+
return $ok == 1;
|
217 |
+
}
|
218 |
+
}
|
219 |
+
|
220 |
+
class OAuthRequest {
|
221 |
+
private $parameters;
|
222 |
+
private $http_method;
|
223 |
+
private $http_url;
|
224 |
+
// for debug purposes
|
225 |
+
public $base_string;
|
226 |
+
public static $version = '1.0';
|
227 |
+
public static $POST_INPUT = 'php://input';
|
228 |
+
|
229 |
+
function __construct($http_method, $http_url, $parameters=NULL) {
|
230 |
+
@$parameters or $parameters = array();
|
231 |
+
$parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
|
232 |
+
$this->parameters = $parameters;
|
233 |
+
$this->http_method = $http_method;
|
234 |
+
$this->http_url = $http_url;
|
235 |
+
}
|
236 |
+
|
237 |
+
|
238 |
+
/**
|
239 |
+
* attempt to build up a request from what was passed to the server
|
240 |
+
*/
|
241 |
+
public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
|
242 |
+
$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
|
243 |
+
? 'http'
|
244 |
+
: 'https';
|
245 |
+
@$http_url or $http_url = $scheme .
|
246 |
+
'://' . $_SERVER['HTTP_HOST'] .
|
247 |
+
':' .
|
248 |
+
$_SERVER['SERVER_PORT'] .
|
249 |
+
$_SERVER['REQUEST_URI'];
|
250 |
+
@$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
|
251 |
+
|
252 |
+
// We weren't handed any parameters, so let's find the ones relevant to
|
253 |
+
// this request.
|
254 |
+
// If you run XML-RPC or similar you should use this to provide your own
|
255 |
+
// parsed parameter-list
|
256 |
+
if (!$parameters) {
|
257 |
+
// Find request headers
|
258 |
+
$request_headers = OAuthUtil::get_headers();
|
259 |
+
|
260 |
+
// Parse the query-string to find GET parameters
|
261 |
+
$parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
|
262 |
+
|
263 |
+
// It's a POST request of the proper content-type, so parse POST
|
264 |
+
// parameters and add those overriding any duplicates from GET
|
265 |
+
if ($http_method == "POST"
|
266 |
+
&& @strstr($request_headers["Content-Type"],
|
267 |
+
"application/x-www-form-urlencoded")
|
268 |
+
) {
|
269 |
+
$post_data = OAuthUtil::parse_parameters(
|
270 |
+
file_get_contents(self::$POST_INPUT)
|
271 |
+
);
|
272 |
+
$parameters = array_merge($parameters, $post_data);
|
273 |
+
}
|
274 |
+
|
275 |
+
// We have a Authorization-header with OAuth data. Parse the header
|
276 |
+
// and add those overriding any duplicates from GET or POST
|
277 |
+
if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
|
278 |
+
$header_parameters = OAuthUtil::split_header(
|
279 |
+
$request_headers['Authorization']
|
280 |
+
);
|
281 |
+
$parameters = array_merge($parameters, $header_parameters);
|
282 |
+
}
|
283 |
+
|
284 |
+
}
|
285 |
+
|
286 |
+
return new OAuthRequest($http_method, $http_url, $parameters);
|
287 |
+
}
|
288 |
+
|
289 |
+
/**
|
290 |
+
* pretty much a helper function to set up the request
|
291 |
+
*/
|
292 |
+
public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
|
293 |
+
@$parameters or $parameters = array();
|
294 |
+
$defaults = array("oauth_version" => OAuthRequest::$version,
|
295 |
+
"oauth_nonce" => OAuthRequest::generate_nonce(),
|
296 |
+
"oauth_timestamp" => OAuthRequest::generate_timestamp(),
|
297 |
+
"oauth_consumer_key" => $consumer->key);
|
298 |
+
if ($token)
|
299 |
+
$defaults['oauth_token'] = $token->key;
|
300 |
+
|
301 |
+
$parameters = array_merge($defaults, $parameters);
|
302 |
+
|
303 |
+
return new OAuthRequest($http_method, $http_url, $parameters);
|
304 |
+
}
|
305 |
+
|
306 |
+
public function set_parameter($name, $value, $allow_duplicates = true) {
|
307 |
+
if ($allow_duplicates && isset($this->parameters[$name])) {
|
308 |
+
// We have already added parameter(s) with this name, so add to the list
|
309 |
+
if (is_scalar($this->parameters[$name])) {
|
310 |
+
// This is the first duplicate, so transform scalar (string)
|
311 |
+
// into an array so we can add the duplicates
|
312 |
+
$this->parameters[$name] = array($this->parameters[$name]);
|
313 |
+
}
|
314 |
+
|
315 |
+
$this->parameters[$name][] = $value;
|
316 |
+
} else {
|
317 |
+
$this->parameters[$name] = $value;
|
318 |
+
}
|
319 |
+
}
|
320 |
+
|
321 |
+
public function get_parameter($name) {
|
322 |
+
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
|
323 |
+
}
|
324 |
+
|
325 |
+
public function get_parameters() {
|
326 |
+
return $this->parameters;
|
327 |
+
}
|
328 |
+
|
329 |
+
public function unset_parameter($name) {
|
330 |
+
unset($this->parameters[$name]);
|
331 |
+
}
|
332 |
+
|
333 |
+
/**
|
334 |
+
* The request parameters, sorted and concatenated into a normalized string.
|
335 |
+
* @return string
|
336 |
+
*/
|
337 |
+
public function get_signable_parameters() {
|
338 |
+
// Grab all parameters
|
339 |
+
$params = $this->parameters;
|
340 |
+
|
341 |
+
// Remove oauth_signature if present
|
342 |
+
// Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
|
343 |
+
if (isset($params['oauth_signature'])) {
|
344 |
+
unset($params['oauth_signature']);
|
345 |
+
}
|
346 |
+
|
347 |
+
return OAuthUtil::build_http_query($params);
|
348 |
+
}
|
349 |
+
|
350 |
+
/**
|
351 |
+
* Returns the base string of this request
|
352 |
+
*
|
353 |
+
* The base string defined as the method, the url
|
354 |
+
* and the parameters (normalized), each urlencoded
|
355 |
+
* and the concated with &.
|
356 |
+
*/
|
357 |
+
public function get_signature_base_string() {
|
358 |
+
$parts = array(
|
359 |
+
$this->get_normalized_http_method(),
|
360 |
+
$this->get_normalized_http_url(),
|
361 |
+
$this->get_signable_parameters()
|
362 |
+
);
|
363 |
+
|
364 |
+
$parts = OAuthUtil::urlencode_rfc3986($parts);
|
365 |
+
|
366 |
+
return implode('&', $parts);
|
367 |
+
}
|
368 |
+
|
369 |
+
/**
|
370 |
+
* just uppercases the http method
|
371 |
+
*/
|
372 |
+
public function get_normalized_http_method() {
|
373 |
+
return strtoupper($this->http_method);
|
374 |
+
}
|
375 |
+
|
376 |
+
/**
|
377 |
+
* parses the url and rebuilds it to be
|
378 |
+
* scheme://host/path
|
379 |
+
*/
|
380 |
+
public function get_normalized_http_url() {
|
381 |
+
$parts = parse_url($this->http_url);
|
382 |
+
|
383 |
+
if (isset($parts['port'])) $port = $parts['port']; else $port = null;
|
384 |
+
$scheme = $parts['scheme'];
|
385 |
+
$host = $parts['host'];
|
386 |
+
if (isset($parts['path'])) $path = $parts['path']; else $path = null;
|
387 |
+
|
388 |
+
$port or $port = ($scheme == 'https') ? '443' : '80';
|
389 |
+
|
390 |
+
if (($scheme == 'https' && $port != '443')
|
391 |
+
|| ($scheme == 'http' && $port != '80')) {
|
392 |
+
$host = "$host:$port";
|
393 |
+
}
|
394 |
+
return "$scheme://$host$path";
|
395 |
+
}
|
396 |
+
|
397 |
+
/**
|
398 |
+
* builds a url usable for a GET request
|
399 |
+
*/
|
400 |
+
public function to_url() {
|
401 |
+
$post_data = $this->to_postdata();
|
402 |
+
$out = $this->get_normalized_http_url();
|
403 |
+
if ($post_data) {
|
404 |
+
$out .= '?'.$post_data;
|
405 |
+
}
|
406 |
+
return $out;
|
407 |
+
}
|
408 |
+
|
409 |
+
/**
|
410 |
+
* builds the data one would send in a POST request
|
411 |
+
*/
|
412 |
+
public function to_postdata() {
|
413 |
+
return OAuthUtil::build_http_query($this->parameters);
|
414 |
+
}
|
415 |
+
|
416 |
+
/**
|
417 |
+
* builds the Authorization: header
|
418 |
+
*/
|
419 |
+
public function to_header($realm=null) {
|
420 |
+
$first = true;
|
421 |
+
if($realm) {
|
422 |
+
$out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
|
423 |
+
$first = false;
|
424 |
+
} else
|
425 |
+
$out = 'Authorization: OAuth';
|
426 |
+
|
427 |
+
$total = array();
|
428 |
+
foreach ($this->parameters as $k => $v) {
|
429 |
+
if (substr($k, 0, 5) != "oauth") continue;
|
430 |
+
if (is_array($v)) {
|
431 |
+
throw new OAuthException('Arrays not supported in headers');
|
432 |
+
}
|
433 |
+
$out .= ($first) ? ' ' : ',';
|
434 |
+
$out .= OAuthUtil::urlencode_rfc3986($k) .
|
435 |
+
'="' .
|
436 |
+
OAuthUtil::urlencode_rfc3986($v) .
|
437 |
+
'"';
|
438 |
+
$first = false;
|
439 |
+
}
|
440 |
+
return $out;
|
441 |
+
}
|
442 |
+
|
443 |
+
public function __toString() {
|
444 |
+
return $this->to_url();
|
445 |
+
}
|
446 |
+
|
447 |
+
|
448 |
+
public function sign_request($signature_method, $consumer, $token) {
|
449 |
+
$this->set_parameter(
|
450 |
+
"oauth_signature_method",
|
451 |
+
$signature_method->get_name(),
|
452 |
+
false
|
453 |
+
);
|
454 |
+
$signature = $this->build_signature($signature_method, $consumer, $token);
|
455 |
+
$this->set_parameter("oauth_signature", $signature, false);
|
456 |
+
}
|
457 |
+
|
458 |
+
public function build_signature($signature_method, $consumer, $token) {
|
459 |
+
$signature = $signature_method->build_signature($this, $consumer, $token);
|
460 |
+
return $signature;
|
461 |
+
}
|
462 |
+
|
463 |
+
/**
|
464 |
+
* util function: current timestamp
|
465 |
+
*/
|
466 |
+
private static function generate_timestamp() {
|
467 |
+
return time();
|
468 |
+
}
|
469 |
+
|
470 |
+
/**
|
471 |
+
* util function: current nonce
|
472 |
+
*/
|
473 |
+
private static function generate_nonce() {
|
474 |
+
$mt = microtime();
|
475 |
+
$rand = mt_rand();
|
476 |
+
|
477 |
+
return md5($mt . $rand); // md5s look nicer than numbers
|
478 |
+
}
|
479 |
+
}
|
480 |
+
|
481 |
+
class OAuthServer {
|
482 |
+
protected $timestamp_threshold = 300; // in seconds, five minutes
|
483 |
+
protected $version = '1.0'; // hi blaine
|
484 |
+
protected $signature_methods = array();
|
485 |
+
|
486 |
+
protected $data_store;
|
487 |
+
|
488 |
+
function __construct($data_store) {
|
489 |
+
$this->data_store = $data_store;
|
490 |
+
}
|
491 |
+
|
492 |
+
public function add_signature_method($signature_method) {
|
493 |
+
$this->signature_methods[$signature_method->get_name()] =
|
494 |
+
$signature_method;
|
495 |
+
}
|
496 |
+
|
497 |
+
// high level functions
|
498 |
+
|
499 |
+
/**
|
500 |
+
* process a request_token request
|
501 |
+
* returns the request token on success
|
502 |
+
*/
|
503 |
+
public function fetch_request_token(&$request) {
|
504 |
+
$this->get_version($request);
|
505 |
+
|
506 |
+
$consumer = $this->get_consumer($request);
|
507 |
+
|
508 |
+
// no token required for the initial token request
|
509 |
+
$token = NULL;
|
510 |
+
|
511 |
+
$this->check_signature($request, $consumer, $token);
|
512 |
+
|
513 |
+
// Rev A change
|
514 |
+
$callback = $request->get_parameter('oauth_callback');
|
515 |
+
$new_token = $this->data_store->new_request_token($consumer, $callback);
|
516 |
+
|
517 |
+
return $new_token;
|
518 |
+
}
|
519 |
+
|
520 |
+
/**
|
521 |
+
* process an access_token request
|
522 |
+
* returns the access token on success
|
523 |
+
*/
|
524 |
+
public function fetch_access_token(&$request) {
|
525 |
+
$this->get_version($request);
|
526 |
+
|
527 |
+
$consumer = $this->get_consumer($request);
|
528 |
+
|
529 |
+
// requires authorized request token
|
530 |
+
$token = $this->get_token($request, $consumer, "request");
|
531 |
+
|
532 |
+
$this->check_signature($request, $consumer, $token);
|
533 |
+
|
534 |
+
// Rev A change
|
535 |
+
$verifier = $request->get_parameter('oauth_verifier');
|
536 |
+
$new_token = $this->data_store->new_access_token($token, $consumer, $verifier);
|
537 |
+
|
538 |
+
return $new_token;
|
539 |
+
}
|
540 |
+
|
541 |
+
/**
|
542 |
+
* verify an api call, checks all the parameters
|
543 |
+
*/
|
544 |
+
public function verify_request(&$request) {
|
545 |
+
$this->get_version($request);
|
546 |
+
$consumer = $this->get_consumer($request);
|
547 |
+
$token = $this->get_token($request, $consumer, "access");
|
548 |
+
$this->check_signature($request, $consumer, $token);
|
549 |
+
return array($consumer, $token);
|
550 |
+
}
|
551 |
+
|
552 |
+
// Internals from here
|
553 |
+
/**
|
554 |
+
* version 1
|
555 |
+
*/
|
556 |
+
private function get_version(&$request) {
|
557 |
+
$version = $request->get_parameter("oauth_version");
|
558 |
+
if (!$version) {
|
559 |
+
// Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present.
|
560 |
+
// Chapter 7.0 ("Accessing Protected Ressources")
|
561 |
+
$version = '1.0';
|
562 |
+
}
|
563 |
+
if ($version !== $this->version) {
|
564 |
+
throw new OAuthException("OAuth version '$version' not supported");
|
565 |
+
}
|
566 |
+
return $version;
|
567 |
+
}
|
568 |
+
|
569 |
+
/**
|
570 |
+
* figure out the signature with some defaults
|
571 |
+
*/
|
572 |
+
private function get_signature_method(&$request) {
|
573 |
+
$signature_method =
|
574 |
+
@$request->get_parameter("oauth_signature_method");
|
575 |
+
|
576 |
+
if (!$signature_method) {
|
577 |
+
// According to chapter 7 ("Accessing Protected Ressources") the signature-method
|
578 |
+
// parameter is required, and we can't just fallback to PLAINTEXT
|
579 |
+
throw new OAuthException('No signature method parameter. This parameter is required');
|
580 |
+
}
|
581 |
+
|
582 |
+
if (!in_array($signature_method,
|
583 |
+
array_keys($this->signature_methods))) {
|
584 |
+
throw new OAuthException(
|
585 |
+
"Signature method '$signature_method' not supported " .
|
586 |
+
"try one of the following: " .
|
587 |
+
implode(", ", array_keys($this->signature_methods))
|
588 |
+
);
|
589 |
+
}
|
590 |
+
return $this->signature_methods[$signature_method];
|
591 |
+
}
|
592 |
+
|
593 |
+
/**
|
594 |
+
* try to find the consumer for the provided request's consumer key
|
595 |
+
*/
|
596 |
+
private function get_consumer(&$request) {
|
597 |
+
$consumer_key = @$request->get_parameter("oauth_consumer_key");
|
598 |
+
if (!$consumer_key) {
|
599 |
+
throw new OAuthException("Invalid consumer key");
|
600 |
+
}
|
601 |
+
|
602 |
+
$consumer = $this->data_store->lookup_consumer($consumer_key);
|
603 |
+
if (!$consumer) {
|
604 |
+
throw new OAuthException("Invalid consumer");
|
605 |
+
}
|
606 |
+
|
607 |
+
return $consumer;
|
608 |
+
}
|
609 |
+
|
610 |
+
/**
|
611 |
+
* try to find the token for the provided request's token key
|
612 |
+
*/
|
613 |
+
private function get_token(&$request, $consumer, $token_type="access") {
|
614 |
+
$token_field = @$request->get_parameter('oauth_token');
|
615 |
+
$token = $this->data_store->lookup_token(
|
616 |
+
$consumer, $token_type, $token_field
|
617 |
+
);
|
618 |
+
if (!$token) {
|
619 |
+
throw new OAuthException("Invalid $token_type token: $token_field");
|
620 |
+
}
|
621 |
+
return $token;
|
622 |
+
}
|
623 |
+
|
624 |
+
/**
|
625 |
+
* all-in-one function to check the signature on a request
|
626 |
+
* should guess the signature method appropriately
|
627 |
+
*/
|
628 |
+
private function check_signature(&$request, $consumer, $token) {
|
629 |
+
// this should probably be in a different method
|
630 |
+
$timestamp = @$request->get_parameter('oauth_timestamp');
|
631 |
+
$nonce = @$request->get_parameter('oauth_nonce');
|
632 |
+
|
633 |
+
$this->check_timestamp($timestamp);
|
634 |
+
$this->check_nonce($consumer, $token, $nonce, $timestamp);
|
635 |
+
|
636 |
+
$signature_method = $this->get_signature_method($request);
|
637 |
+
|
638 |
+
$signature = $request->get_parameter('oauth_signature');
|
639 |
+
$valid_sig = $signature_method->check_signature(
|
640 |
+
$request,
|
641 |
+
$consumer,
|
642 |
+
$token,
|
643 |
+
$signature
|
644 |
+
);
|
645 |
+
|
646 |
+
if (!$valid_sig) {
|
647 |
+
throw new OAuthException("Invalid signature");
|
648 |
+
}
|
649 |
+
}
|
650 |
+
|
651 |
+
/**
|
652 |
+
* check that the timestamp is new enough
|
653 |
+
*/
|
654 |
+
private function check_timestamp($timestamp) {
|
655 |
+
if( ! $timestamp )
|
656 |
+
throw new OAuthException(
|
657 |
+
'Missing timestamp parameter. The parameter is required'
|
658 |
+
);
|
659 |
+
|
660 |
+
// verify that timestamp is recentish
|
661 |
+
$now = time();
|
662 |
+
if (abs($now - $timestamp) > $this->timestamp_threshold) {
|
663 |
+
throw new OAuthException(
|
664 |
+
"Expired timestamp, yours $timestamp, ours $now"
|
665 |
+
);
|
666 |
+
}
|
667 |
+
}
|
668 |
+
|
669 |
+
/**
|
670 |
+
* check that the nonce is not repeated
|
671 |
+
*/
|
672 |
+
private function check_nonce($consumer, $token, $nonce, $timestamp) {
|
673 |
+
if( ! $nonce )
|
674 |
+
throw new OAuthException(
|
675 |
+
'Missing nonce parameter. The parameter is required'
|
676 |
+
);
|
677 |
+
|
678 |
+
// verify that the nonce is uniqueish
|
679 |
+
$found = $this->data_store->lookup_nonce(
|
680 |
+
$consumer,
|
681 |
+
$token,
|
682 |
+
$nonce,
|
683 |
+
$timestamp
|
684 |
+
);
|
685 |
+
if ($found) {
|
686 |
+
throw new OAuthException("Nonce already used: $nonce");
|
687 |
+
}
|
688 |
+
}
|
689 |
+
|
690 |
+
}
|
691 |
+
|
692 |
+
class OAuthDataStore {
|
693 |
+
function lookup_consumer($consumer_key) {
|
694 |
+
// implement me
|
695 |
+
}
|
696 |
+
|
697 |
+
function lookup_token($consumer, $token_type, $token) {
|
698 |
+
// implement me
|
699 |
+
}
|
700 |
+
|
701 |
+
function lookup_nonce($consumer, $token, $nonce, $timestamp) {
|
702 |
+
// implement me
|
703 |
+
}
|
704 |
+
|
705 |
+
function new_request_token($consumer, $callback = null) {
|
706 |
+
// return a new token attached to this consumer
|
707 |
+
}
|
708 |
+
|
709 |
+
function new_access_token($token, $consumer, $verifier = null) {
|
710 |
+
// return a new access token attached to this consumer
|
711 |
+
// for the user associated with this token if the request token
|
712 |
+
// is authorized
|
713 |
+
// should also invalidate the request token
|
714 |
+
}
|
715 |
+
|
716 |
+
}
|
717 |
+
|
718 |
+
class OAuthUtil {
|
719 |
+
public static function urlencode_rfc3986($input) {
|
720 |
+
if (is_array($input)) {
|
721 |
+
return array_map(array('OAuthUtil', 'urlencode_rfc3986'), $input);
|
722 |
+
} else if (is_scalar($input)) {
|
723 |
+
return str_replace(
|
724 |
+
'+',
|
725 |
+
' ',
|
726 |
+
str_replace('%7E', '~', rawurlencode($input))
|
727 |
+
);
|
728 |
+
} else {
|
729 |
+
return '';
|
730 |
+
}
|
731 |
+
}
|
732 |
+
|
733 |
+
|
734 |
+
// This decode function isn't taking into consideration the above
|
735 |
+
// modifications to the encoding process. However, this method doesn't
|
736 |
+
// seem to be used anywhere so leaving it as is.
|
737 |
+
public static function urldecode_rfc3986($string) {
|
738 |
+
return urldecode($string);
|
739 |
+
}
|
740 |
+
|
741 |
+
// Utility function for turning the Authorization: header into
|
742 |
+
// parameters, has to do some unescaping
|
743 |
+
// Can filter out any non-oauth parameters if needed (default behaviour)
|
744 |
+
public static function split_header($header, $only_allow_oauth_parameters = true) {
|
745 |
+
$pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
|
746 |
+
$offset = 0;
|
747 |
+
$params = array();
|
748 |
+
while (preg_match($pattern, $header, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
|
749 |
+
$match = $matches[0];
|
750 |
+
$header_name = $matches[2][0];
|
751 |
+
$header_content = (isset($matches[5])) ? $matches[5][0] : $matches[4][0];
|
752 |
+
if (preg_match('/^oauth_/', $header_name) || !$only_allow_oauth_parameters) {
|
753 |
+
$params[$header_name] = OAuthUtil::urldecode_rfc3986($header_content);
|
754 |
+
}
|
755 |
+
$offset = $match[1] + strlen($match[0]);
|
756 |
+
}
|
757 |
+
|
758 |
+
if (isset($params['realm'])) {
|
759 |
+
unset($params['realm']);
|
760 |
+
}
|
761 |
+
|
762 |
+
return $params;
|
763 |
+
}
|
764 |
+
|
765 |
+
// helper to try to sort out headers for people who aren't running apache
|
766 |
+
public static function get_headers() {
|
767 |
+
if (function_exists('apache_request_headers')) {
|
768 |
+
// we need this to get the actual Authorization: header
|
769 |
+
// because apache tends to tell us it doesn't exist
|
770 |
+
$headers = apache_request_headers();
|
771 |
+
|
772 |
+
// sanitize the output of apache_request_headers because
|
773 |
+
// we always want the keys to be Cased-Like-This and arh()
|
774 |
+
// returns the headers in the same case as they are in the
|
775 |
+
// request
|
776 |
+
$out = array();
|
777 |
+
foreach( $headers AS $key => $value ) {
|
778 |
+
$key = str_replace(
|
779 |
+
" ",
|
780 |
+
"-",
|
781 |
+
ucwords(strtolower(str_replace("-", " ", $key)))
|
782 |
+
);
|
783 |
+
$out[$key] = $value;
|
784 |
+
}
|
785 |
+
} else {
|
786 |
+
// otherwise we don't have apache and are just going to have to hope
|
787 |
+
// that $_SERVER actually contains what we need
|
788 |
+
$out = array();
|
789 |
+
if( isset($_SERVER['CONTENT_TYPE']) )
|
790 |
+
$out['Content-Type'] = $_SERVER['CONTENT_TYPE'];
|
791 |
+
if( isset($_ENV['CONTENT_TYPE']) )
|
792 |
+
$out['Content-Type'] = $_ENV['CONTENT_TYPE'];
|
793 |
+
|
794 |
+
foreach ($_SERVER as $key => $value) {
|
795 |
+
if (substr($key, 0, 5) == "HTTP_") {
|
796 |
+
// this is chaos, basically it is just there to capitalize the first
|
797 |
+
// letter of every word that is not an initial HTTP and strip HTTP
|
798 |
+
// code from przemek
|
799 |
+
$key = str_replace(
|
800 |
+
" ",
|
801 |
+
"-",
|
802 |
+
ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
|
803 |
+
);
|
804 |
+
$out[$key] = $value;
|
805 |
+
}
|
806 |
+
}
|
807 |
+
}
|
808 |
+
return $out;
|
809 |
+
}
|
810 |
+
|
811 |
+
// This function takes a input like a=b&a=c&d=e and returns the parsed
|
812 |
+
// parameters like this
|
813 |
+
// array('a' => array('b','c'), 'd' => 'e')
|
814 |
+
public static function parse_parameters( $input ) {
|
815 |
+
if (!isset($input) || !$input) return array();
|
816 |
+
|
817 |
+
$pairs = explode('&', $input);
|
818 |
+
|
819 |
+
$parsed_parameters = array();
|
820 |
+
foreach ($pairs as $pair) {
|
821 |
+
$split = explode('=', $pair, 2);
|
822 |
+
$parameter = OAuthUtil::urldecode_rfc3986($split[0]);
|
823 |
+
$value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : '';
|
824 |
+
|
825 |
+
if (isset($parsed_parameters[$parameter])) {
|
826 |
+
// We have already recieved parameter(s) with this name, so add to the list
|
827 |
+
// of parameters with this name
|
828 |
+
|
829 |
+
if (is_scalar($parsed_parameters[$parameter])) {
|
830 |
+
// This is the first duplicate, so transform scalar (string) into an array
|
831 |
+
// so we can add the duplicates
|
832 |
+
$parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
|
833 |
+
}
|
834 |
+
|
835 |
+
$parsed_parameters[$parameter][] = $value;
|
836 |
+
} else {
|
837 |
+
$parsed_parameters[$parameter] = $value;
|
838 |
+
}
|
839 |
+
}
|
840 |
+
return $parsed_parameters;
|
841 |
+
}
|
842 |
+
|
843 |
+
public static function build_http_query($params) {
|
844 |
+
if (!$params) return '';
|
845 |
+
|
846 |
+
// Urlencode both keys and values
|
847 |
+
$keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
|
848 |
+
$values = OAuthUtil::urlencode_rfc3986(array_values($params));
|
849 |
+
$params = array_combine($keys, $values);
|
850 |
+
|
851 |
+
// Parameters are sorted by name, using lexicographical byte value ordering.
|
852 |
+
// Ref: Spec: 9.1.1 (1)
|
853 |
+
uksort($params, 'strcmp');
|
854 |
+
|
855 |
+
$pairs = array();
|
856 |
+
foreach ($params as $parameter => $value) {
|
857 |
+
if (is_array($value)) {
|
858 |
+
// If two or more parameters share the same name, they are sorted by their value
|
859 |
+
// Ref: Spec: 9.1.1 (1)
|
860 |
+
natsort($value);
|
861 |
+
foreach ($value as $duplicate_value) {
|
862 |
+
$pairs[] = $parameter . '=' . $duplicate_value;
|
863 |
+
}
|
864 |
+
} else {
|
865 |
+
$pairs[] = $parameter . '=' . $value;
|
866 |
+
}
|
867 |
+
}
|
868 |
+
// For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
|
869 |
+
// Each name-value pair is separated by an '&' character (ASCII code 38)
|
870 |
+
return implode('&', $pairs);
|
871 |
+
}
|
872 |
+
}
|
873 |
+
|
874 |
+
?>
|
oauth/twitteroauth.php
ADDED
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/*
|
4 |
+
* Abraham Williams (abraham@abrah.am) http://abrah.am
|
5 |
+
*
|
6 |
+
* The first PHP Library to support OAuth for Twitter's REST API.
|
7 |
+
*/
|
8 |
+
|
9 |
+
/* Load OAuth lib. You can find it at http://oauth.net */
|
10 |
+
require_once('OAuth.php');
|
11 |
+
|
12 |
+
/**
|
13 |
+
* Twitter OAuth class
|
14 |
+
*/
|
15 |
+
class TwitterOAuth {
|
16 |
+
/* Contains the last HTTP status code returned. */
|
17 |
+
public $http_code;
|
18 |
+
/* Contains the last API call. */
|
19 |
+
public $url;
|
20 |
+
/* Set up the API root URL. */
|
21 |
+
public $host = "https://api.twitter.com/1.1/";
|
22 |
+
/* Set timeout default. */
|
23 |
+
public $timeout = 3;
|
24 |
+
/* Set connect timeout. */
|
25 |
+
public $connecttimeout = 2;
|
26 |
+
/* Verify SSL Cert. */
|
27 |
+
public $ssl_verifypeer = FALSE;
|
28 |
+
/* Respons format. */
|
29 |
+
public $format = 'json';
|
30 |
+
/* Decode returned json data. */
|
31 |
+
public $decode_json = TRUE;
|
32 |
+
/* Contains the last HTTP headers returned. */
|
33 |
+
public $http_info;
|
34 |
+
/* Set the useragnet. */
|
35 |
+
public $useragent = 'Twitter Feed for Wordpress Developers 1.0';
|
36 |
+
/* Immediately retry the API call if the response was not successful. */
|
37 |
+
//public $retry = TRUE;
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
/**
|
43 |
+
* Set API URLS
|
44 |
+
*/
|
45 |
+
function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
|
46 |
+
function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
|
47 |
+
function authorizeURL() { return 'https://twitter.com/oauth/authorize'; }
|
48 |
+
function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
|
49 |
+
|
50 |
+
/**
|
51 |
+
* Debug helpers
|
52 |
+
*/
|
53 |
+
function lastStatusCode() { return $this->http_status; }
|
54 |
+
function lastAPICall() { return $this->last_api_call; }
|
55 |
+
|
56 |
+
/**
|
57 |
+
* construct TwitterOAuth object
|
58 |
+
*/
|
59 |
+
function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
|
60 |
+
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
|
61 |
+
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
|
62 |
+
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
|
63 |
+
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
|
64 |
+
} else {
|
65 |
+
$this->token = NULL;
|
66 |
+
}
|
67 |
+
}
|
68 |
+
|
69 |
+
|
70 |
+
/**
|
71 |
+
* Get a request_token from Twitter
|
72 |
+
*
|
73 |
+
* @returns a key/value array containing oauth_token and oauth_token_secret
|
74 |
+
*/
|
75 |
+
function getRequestToken($oauth_callback = NULL) {
|
76 |
+
$parameters = array();
|
77 |
+
if (!empty($oauth_callback)) {
|
78 |
+
$parameters['oauth_callback'] = $oauth_callback;
|
79 |
+
}
|
80 |
+
$request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
|
81 |
+
$token = OAuthUtil::parse_parameters($request);
|
82 |
+
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
|
83 |
+
return $token;
|
84 |
+
}
|
85 |
+
|
86 |
+
/**
|
87 |
+
* Get the authorize URL
|
88 |
+
*
|
89 |
+
* @returns a string
|
90 |
+
*/
|
91 |
+
function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
|
92 |
+
if (is_array($token)) {
|
93 |
+
$token = $token['oauth_token'];
|
94 |
+
}
|
95 |
+
if (empty($sign_in_with_twitter)) {
|
96 |
+
return $this->authorizeURL() . "?oauth_token={$token}";
|
97 |
+
} else {
|
98 |
+
return $this->authenticateURL() . "?oauth_token={$token}";
|
99 |
+
}
|
100 |
+
}
|
101 |
+
|
102 |
+
/**
|
103 |
+
* Exchange request token and secret for an access token and
|
104 |
+
* secret, to sign API calls.
|
105 |
+
*
|
106 |
+
* @returns array("oauth_token" => "the-access-token",
|
107 |
+
* "oauth_token_secret" => "the-access-secret",
|
108 |
+
* "user_id" => "9436992",
|
109 |
+
* "screen_name" => "abraham")
|
110 |
+
*/
|
111 |
+
function getAccessToken($oauth_verifier = FALSE) {
|
112 |
+
$parameters = array();
|
113 |
+
if (!empty($oauth_verifier)) {
|
114 |
+
$parameters['oauth_verifier'] = $oauth_verifier;
|
115 |
+
}
|
116 |
+
$request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
|
117 |
+
$token = OAuthUtil::parse_parameters($request);
|
118 |
+
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
|
119 |
+
return $token;
|
120 |
+
}
|
121 |
+
|
122 |
+
/**
|
123 |
+
* One time exchange of username and password for access token and secret.
|
124 |
+
*
|
125 |
+
* @returns array("oauth_token" => "the-access-token",
|
126 |
+
* "oauth_token_secret" => "the-access-secret",
|
127 |
+
* "user_id" => "9436992",
|
128 |
+
* "screen_name" => "abraham",
|
129 |
+
* "x_auth_expires" => "0")
|
130 |
+
*/
|
131 |
+
function getXAuthToken($username, $password) {
|
132 |
+
$parameters = array();
|
133 |
+
$parameters['x_auth_username'] = $username;
|
134 |
+
$parameters['x_auth_password'] = $password;
|
135 |
+
$parameters['x_auth_mode'] = 'client_auth';
|
136 |
+
$request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
|
137 |
+
$token = OAuthUtil::parse_parameters($request);
|
138 |
+
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
|
139 |
+
return $token;
|
140 |
+
}
|
141 |
+
|
142 |
+
/**
|
143 |
+
* GET wrapper for oAuthRequest.
|
144 |
+
*/
|
145 |
+
function get($url, $parameters = array()) {
|
146 |
+
$response = $this->oAuthRequest($url, 'GET', $parameters);
|
147 |
+
if ($this->format === 'json' && $this->decode_json) {
|
148 |
+
return json_decode($response,true);
|
149 |
+
}
|
150 |
+
return $response;
|
151 |
+
}
|
152 |
+
|
153 |
+
/**
|
154 |
+
* POST wrapper for oAuthRequest.
|
155 |
+
*/
|
156 |
+
function post($url, $parameters = array()) {
|
157 |
+
$response = $this->oAuthRequest($url, 'POST', $parameters);
|
158 |
+
if ($this->format === 'json' && $this->decode_json) {
|
159 |
+
return json_decode($response,true);
|
160 |
+
}
|
161 |
+
return $response;
|
162 |
+
}
|
163 |
+
|
164 |
+
/**
|
165 |
+
* DELETE wrapper for oAuthReqeust.
|
166 |
+
*/
|
167 |
+
function delete($url, $parameters = array()) {
|
168 |
+
$response = $this->oAuthRequest($url, 'DELETE', $parameters);
|
169 |
+
if ($this->format === 'json' && $this->decode_json) {
|
170 |
+
return json_decode($response,true);
|
171 |
+
}
|
172 |
+
return $response;
|
173 |
+
}
|
174 |
+
|
175 |
+
/**
|
176 |
+
* Format and sign an OAuth / API request
|
177 |
+
*/
|
178 |
+
function oAuthRequest($url, $method, $parameters) {
|
179 |
+
if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
|
180 |
+
$url = "{$this->host}{$url}.{$this->format}";
|
181 |
+
}
|
182 |
+
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
|
183 |
+
$request->sign_request($this->sha1_method, $this->consumer, $this->token);
|
184 |
+
switch ($method) {
|
185 |
+
case 'GET':
|
186 |
+
return $this->http($request->to_url(), 'GET');
|
187 |
+
default:
|
188 |
+
return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
|
189 |
+
}
|
190 |
+
}
|
191 |
+
|
192 |
+
/**
|
193 |
+
* Make an HTTP request
|
194 |
+
*
|
195 |
+
* @return API results
|
196 |
+
*/
|
197 |
+
function http($url, $method, $postfields = NULL) {
|
198 |
+
$this->http_info = array();
|
199 |
+
$ci = curl_init();
|
200 |
+
/* Curl settings */
|
201 |
+
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
|
202 |
+
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
|
203 |
+
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
|
204 |
+
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
|
205 |
+
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
|
206 |
+
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
|
207 |
+
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
|
208 |
+
curl_setopt($ci, CURLOPT_HEADER, FALSE);
|
209 |
+
|
210 |
+
switch ($method) {
|
211 |
+
case 'POST':
|
212 |
+
curl_setopt($ci, CURLOPT_POST, TRUE);
|
213 |
+
if (!empty($postfields)) {
|
214 |
+
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
|
215 |
+
}
|
216 |
+
break;
|
217 |
+
case 'DELETE':
|
218 |
+
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
219 |
+
if (!empty($postfields)) {
|
220 |
+
$url = "{$url}?{$postfields}";
|
221 |
+
}
|
222 |
+
}
|
223 |
+
|
224 |
+
curl_setopt($ci, CURLOPT_URL, $url);
|
225 |
+
$response = curl_exec($ci);
|
226 |
+
$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
|
227 |
+
$this->http_info = array_merge($this->http_info, curl_getinfo($ci));
|
228 |
+
$this->url = $url;
|
229 |
+
curl_close ($ci);
|
230 |
+
return $response;
|
231 |
+
}
|
232 |
+
|
233 |
+
/**
|
234 |
+
* Get the header info to store.
|
235 |
+
*/
|
236 |
+
function getHeader($ch, $header) {
|
237 |
+
$i = strpos($header, ':');
|
238 |
+
if (!empty($i)) {
|
239 |
+
$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
|
240 |
+
$value = trim(substr($header, $i + 2));
|
241 |
+
$this->http_header[$key] = $value;
|
242 |
+
}
|
243 |
+
return strlen($header);
|
244 |
+
}
|
245 |
+
}
|
readme.txt
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Plugin Name ===
|
2 |
+
Contributors: stormuk, lgladdy
|
3 |
+
Donate link: http://www.stormconsultancy.co.uk/
|
4 |
+
Tags: twitter, oauth, feed
|
5 |
+
Requires at least: 3.4
|
6 |
+
Tested up to: 3.5
|
7 |
+
Stable tag: 1.0
|
8 |
+
License: MIT
|
9 |
+
License URI: http://opensource.org/licenses/MIT
|
10 |
+
|
11 |
+
A twitter API 1.1 compliant plugin that provides a function that returns a number of tweets from the auth'd users twitter feed as an array for wordpress (theme) developers to use in their themes.
|
12 |
+
|
13 |
+
== Description ==
|
14 |
+
|
15 |
+
A Twitter API 1.1 compliant wordpress plugin that provides an array of a users timeline from Twitter for use by theme developers.
|
16 |
+
|
17 |
+
The new Twitter API requires you be oAuth'd before you can request a list of tweets, this means that all of the existing Twitter plugins that simply make an AJAX request for to the JSON API endpoint will break as of March 2013.
|
18 |
+
|
19 |
+
This is a major problem for the vast majority of websites that are currently using twitter, so we built a PHP class that implements all the new requirements for authentication and gives you an array of tweets out of the other end, for you to use in your PHP applications, or WordPress theme. You can find the stand-alone [StormTwitter](https://github.com/stormuk/storm-twitter) class on GitHub
|
20 |
+
|
21 |
+
This plugin wraps our Twitter class and provides a settings screen for easy integration into WordPress. However, it's definitely for developers - you only get a PHP array out of it that contains Twitter tweet objects. You'll still need to style the output and make it comply with the new display requirements.
|
22 |
+
|
23 |
+
This plugin does not provide sidebar widgets, shortcodes or any other form of drop-in functionality. You still need to do the styling, we've just done the heavy lifting for you!
|
24 |
+
|
25 |
+
== Installation ==
|
26 |
+
|
27 |
+
Install the plugin using the plugin manager, or upload the files to your wp-content/plugins directory.
|
28 |
+
|
29 |
+
Navigate to the Settings > Twitter Feed Auth.
|
30 |
+
|
31 |
+
Here you'll find settings fields to authenticate with Twitter. You'll need to create a new Application on http://dev.twitter.com/apps.
|
32 |
+
|
33 |
+
Once you've create the app, scroll down the app's details page to find the oAuth section. Copy the consumer secret and consumer key into the settings page for the plugin. Then click the Create Access Token button at the bottom of the Twitter app page. Copy the Access token and Access token secret into the plugin's settings page. Finally, enter the Twitter username of the feed you want to access. Save the settings.
|
34 |
+
|
35 |
+
Now, anywhere in your theme files you can call the `getTweets()` function to retrieve an array of tweets.
|
36 |
+
|
37 |
+
You can then loop over the array and do whatever you want with it.
|
38 |
+
|
39 |
+
`<?php
|
40 |
+
$tweets = getTweets();
|
41 |
+
var_dump($tweets);
|
42 |
+
|
43 |
+
foreach($tweets as $tweet){
|
44 |
+
var_dump($tweet);
|
45 |
+
}
|
46 |
+
?>`
|
47 |
+
|
48 |
+
You can specify a number of tweets to return (up to 20) by passing a parameter to the function. For example, to display just the latest tweet you'd request `getTweets(1)`
|
49 |
+
|
50 |
+
Results are cached for 1 hour to help you avoid hitting the API limits.
|
51 |
+
|
52 |
+
== TODO ==
|
53 |
+
|
54 |
+
* Move the screen name from the settings page to a function parameter so you can use the plugin to request different timelines
|
55 |
+
* Make the cache duration configurable
|
56 |
+
|
57 |
+
== Changelog ==
|
58 |
+
|
59 |
+
= 1.0 =
|
60 |
+
* First version
|
twitter-feed-for-developers-settings.php
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
//Add an option page
|
4 |
+
if (is_admin()) {
|
5 |
+
add_action('admin_menu', 'tdf_menu');
|
6 |
+
add_action('admin_init', 'tdf_register_settings');
|
7 |
+
}
|
8 |
+
|
9 |
+
function tdf_menu() {
|
10 |
+
add_options_page('Twitter Feed for Developers','Twitter Feed Auth','manage_options','tdf_settings','tdf_settings_output');
|
11 |
+
}
|
12 |
+
|
13 |
+
function tdf_settings() {
|
14 |
+
$tdf = array();
|
15 |
+
$tdf[] = array('name'=>'tdf_consumer_key','label'=>'Twitter Application Consumer Key');
|
16 |
+
$tdf[] = array('name'=>'tdf_consumer_secret','label'=>'Twitter Application Consumer Secret');
|
17 |
+
$tdf[] = array('name'=>'tdf_access_token','label'=>'Account Access Token');
|
18 |
+
$tdf[] = array('name'=>'tdf_access_token_secret','label'=>'Account Access Token Secret');
|
19 |
+
$tdf[] = array('name'=>'tdf_user_timeline','label'=>'Twitter Feed Screen Name');
|
20 |
+
return $tdf;
|
21 |
+
}
|
22 |
+
|
23 |
+
function tdf_register_settings() {
|
24 |
+
$settings = tdf_settings();
|
25 |
+
foreach($settings as $setting) {
|
26 |
+
register_setting('tdf_settings',$setting['name']);
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
function tdf_settings_output() {
|
32 |
+
$settings = tdf_settings();
|
33 |
+
|
34 |
+
echo '<div class="wrap">';
|
35 |
+
|
36 |
+
echo '<h2>Twitter Feed for Developers</h2>';
|
37 |
+
|
38 |
+
echo '<p>This plugin requires five fields. Most of which are found on the application overview page on the <a href="http://dev.twitter.com/apps">http://dev.twitter.com</a> website</p>';
|
39 |
+
echo '<p>When creating an application for this plugin, you don\'t need to set a callback location and you only need read access.</p>';
|
40 |
+
echo '<p>You will need to generate an oAuth token once you\'ve created the application. The button for that is on the bottom of the application overview page.</p>';
|
41 |
+
echo '<p>The \'Twitter Feed Screen Name\' setting is the timeline you wish you load when you call the function getTweets(), such as @stormuk</p>';
|
42 |
+
echo '<p>getTweets($limit = 20) takes an optional limit on the number of tweets, up to a maximum of 20, and has default of 20.</p>';
|
43 |
+
echo '<p>The format of the response from getTweets will either be an array of arrays containing tweet objects, as described on the official Twitter documentation <a href="https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline">here</a>, or an 1D array containing an "error" key, with a value of the error that occurred.</p>';
|
44 |
+
|
45 |
+
echo '<hr />';
|
46 |
+
|
47 |
+
echo '<form method="post" action="options.php">';
|
48 |
+
|
49 |
+
settings_fields('tdf_settings');
|
50 |
+
|
51 |
+
echo '<table>';
|
52 |
+
foreach($settings as $setting) {
|
53 |
+
echo '<tr>';
|
54 |
+
echo '<td>'.$setting['label'].'</td>';
|
55 |
+
echo '<td><input type="text" style="width: 400px" name="'.$setting['name'].'" value="'.get_option($setting['name']).'" /></td>';
|
56 |
+
echo '</tr>';
|
57 |
+
}
|
58 |
+
echo '</table>';
|
59 |
+
|
60 |
+
submit_button();
|
61 |
+
|
62 |
+
echo '</form>';
|
63 |
+
|
64 |
+
echo '<hr />';
|
65 |
+
|
66 |
+
echo '<h3>Debug Information</h3>';
|
67 |
+
$last_cached = get_option('tdf_last_cached');
|
68 |
+
if (empty($last_cached)) $last_cached = "Never"; else $last_cached = date('r',$last_cached);
|
69 |
+
echo '<p>Last Cached: '.$last_cached.'<br />';
|
70 |
+
$last_error = get_option('tdf_last_error');
|
71 |
+
if (empty($last_error)) $last_error = "None";
|
72 |
+
echo 'Last Error: '.$last_error.'</p>';
|
73 |
+
|
74 |
+
echo '</div>';
|
75 |
+
|
76 |
+
}
|
twitter-feed-for-developers.php
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Contributors: stormuk, lgladdy
|
4 |
+
Donate link: http://www.stormconsultancy.co.uk/
|
5 |
+
Tags: twitter, oauth, feed
|
6 |
+
Requires at least: 3.4
|
7 |
+
Tested up to: 3.5
|
8 |
+
Stable tag: 1.0
|
9 |
+
Version: 1.0
|
10 |
+
License: MIT
|
11 |
+
License URI: http://opensource.org/licenses/MIT
|
12 |
+
*/
|
13 |
+
|
14 |
+
|
15 |
+
require('StormTwitter.class.php');
|
16 |
+
require('twitter-feed-for-developers-settings.php');
|
17 |
+
require('oauth/twitteroauth.php');
|
18 |
+
|
19 |
+
/* implement getTweets */
|
20 |
+
function getTweets($count = 20) {
|
21 |
+
|
22 |
+
$config['key'] = get_option('tdf_consumer_key');
|
23 |
+
$config['secret'] = get_option('tdf_consumer_secret');
|
24 |
+
$config['token'] = get_option('tdf_access_token');
|
25 |
+
$config['token_secret'] = get_option('tdf_access_token_secret');
|
26 |
+
$config['screenname'] = get_option('tdf_user_timeline');
|
27 |
+
$config['directory'] = plugin_dir_path(__FILE__);
|
28 |
+
|
29 |
+
$obj = new StormTwitter($config);
|
30 |
+
$res = $obj->getTweets($count);
|
31 |
+
update_option('tdf_last_cached',$obj->st_last_cached);
|
32 |
+
update_option('tdf_last_error',$obj->st_last_error);
|
33 |
+
return $res;
|
34 |
+
|
35 |
+
}
|