AccessPress Twitter Feed – Twitter Feed for WordPress - Version 1.1.0

Version Description

  • API updated
  • Small bug for time elasped date format fixed
Download this release

Release Info

Developer Access Keys
Plugin Icon 128x128 AccessPress Twitter Feed – Twitter Feed for WordPress
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.2 to 1.1.0

APTF.class.php DELETED
@@ -1,125 +0,0 @@
1
- <?php
2
- /*
3
- * Version 2.2.1
4
- * The base class for the storm twitter feed for developers.
5
- * This class provides all the things needed for the wordpress plugin, but in theory means you don't need to use it with wordpress.
6
- * What could go wrong?
7
- */
8
-
9
-
10
- if (!class_exists('TwitterOAuth')) {
11
- require_once('oauth/twitteroauth.php');
12
- } else {
13
- define('TFD_USING_EXISTING_LIBRARY_TWITTEROAUTH',true);
14
- }
15
-
16
- class APTF_Twitter_Class {
17
-
18
- private $defaults = array(
19
- 'directory' => '',
20
- 'key' => '',
21
- 'secret' => '',
22
- 'token' => '',
23
- 'token_secret' => '',
24
- 'screenname' => '',
25
- 'cache_expire' => 1
26
- );
27
-
28
- public $st_last_error = false;
29
-
30
- function __construct($args = array()) {
31
- $this->defaults = array_merge($this->defaults, $args);
32
- }
33
-
34
- function __toString() {
35
- return print_r($this->defaults, true);
36
- }
37
-
38
- function getTweets($screenname = false,$count = 20,$options = false) {
39
- // BC: $count used to be the first argument
40
- if (is_int($screenname)) {
41
- list($screenname, $count) = array($count, $screenname);
42
- }
43
-
44
- if ($count > 20) $count = 20;
45
- if ($count < 1) $count = 1;
46
-
47
- $default_options = array('trim_user'=>true, 'exclude_replies'=>true, 'include_rts'=>false);
48
-
49
- if ($options === false || !is_array($options)) {
50
- $options = $default_options;
51
- } else {
52
- $options = array_merge($default_options, $options);
53
- }
54
-
55
- if ($screenname === false || $screenname === 20) $screenname = $this->defaults['screenname'];
56
-
57
-
58
- //If we're here, we need to load.
59
- $result = $this->oauthGetTweets($screenname,$options);
60
-
61
- if (is_array($result) && isset($result['errors'])) {
62
- if (is_array($result) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
63
- $last_error = $result['errors'][0]['message'];
64
- } else {
65
- $last_error = $result['errors'];
66
- }
67
- return array('error'=>__('Twitter said: ',APTF_TD).json_encode($last_error));
68
- } else {
69
- if (is_array($result)) {
70
- return $this->cropTweets($result,$count);
71
-
72
- } else {
73
- $last_error = __('Something went wrong with the twitter request: ',APTF_TD).json_encode($result);
74
- return array('error'=>$last_error);
75
- }
76
- }
77
-
78
- }
79
-
80
- private function cropTweets($result,$count) {
81
- return array_slice($result, 0, $count);
82
- }
83
-
84
-
85
-
86
- private function getOptionsHash($options) {
87
- $hash = md5(serialize($options));
88
- return $hash;
89
- }
90
-
91
-
92
-
93
- private function oauthGetTweets($screenname,$options) {
94
- $key = $this->defaults['key'];
95
- $secret = $this->defaults['secret'];
96
- $token = $this->defaults['token'];
97
- $token_secret = $this->defaults['token_secret'];
98
-
99
- $cachename = $screenname."-".$this->getOptionsHash($options);
100
-
101
- $options = array_merge($options, array('screen_name' => $screenname, 'count' => 20));
102
-
103
- if (empty($key)) return array('error'=>__('Missing Consumer Key - Check Settings',APTF_TD));
104
- if (empty($secret)) return array('error'=>__('Missing Consumer Secret - Check Settings',APTF_TD));
105
- if (empty($token)) return array('error'=>__('Missing Access Token - Check Settings',APTF_TD));
106
- if (empty($token_secret)) return array('error'=>__('Missing Access Token Secret - Check Settings',APTF_TD));
107
- if (empty($screenname)) return array('error'=>__('Missing Twitter Feed Screen Name - Check Settings',APTF_TD));
108
-
109
- $connection = new TwitterOAuth($key, $secret, $token, $token_secret);
110
- $result = $connection->get('statuses/user_timeline', $options);
111
-
112
- if (isset($result['errors'])) {
113
- if (is_array($results) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
114
- $last_error = '['.date('r').'] Twitter error: '.$result['errors'][0]['message'];
115
- $this->st_last_error = $last_error;
116
- } else {
117
- $last_error = '['.date('r').'] Twitter returned an invalid response. It is probably down.';
118
- $this->st_last_error = $last_error;
119
- }
120
- }
121
-
122
- return $result;
123
-
124
- }
125
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
accesspress-twitter-feed.php CHANGED
@@ -4,7 +4,7 @@ defined('ABSPATH') or die('No script kiddies please!');
4
  * Plugin Name: AccessPress Twitter Feed
5
  * Plugin URI: https://accesspressthemes.com/wordpress-plugins/accesspress-twitter-feed/
6
  * Description: A plugin to show your twitter feed in your site with various configurable settings
7
- * Version: 1.0.2
8
  * Author: AccessPress Themes
9
  * Author URI: http://accesspressthemes.com
10
  * Text Domain: ap-twitter-feed
@@ -24,7 +24,7 @@ if (!defined('APTF_CSS_DIR')) {
24
  define('APTF_CSS_DIR', plugin_dir_url(__FILE__) . 'css');
25
  }
26
  if (!defined('APTF_VERSION')) {
27
- define('APTF_VERSION', '1.0.2');
28
  }
29
 
30
  if (!defined('APTF_TD')) {
@@ -32,6 +32,7 @@ if (!defined('APTF_TD')) {
32
  }
33
  include_once('inc/backend/widget.php');
34
  include_once('inc/backend/slider-widget.php');
 
35
  if (!class_exists('APTF_Class')) {
36
 
37
  class APTF_Class {
@@ -82,6 +83,7 @@ if (!class_exists('APTF_Class')) {
82
  if (!get_option('aptf_settings')) {
83
  update_option('aptf_settings', $default_settings);
84
  }
 
85
  }
86
 
87
  /**
@@ -131,39 +133,7 @@ if (!class_exists('APTF_Class')) {
131
  return $default_settings;
132
  }
133
 
134
- /**
135
- *
136
- * @param string $username
137
- * @param int $count
138
- * @param array $options
139
- * @return array
140
- */
141
- function get_tweets($username = false, $count = 20, $options = false) {
142
- include_once('APTF.class.php');
143
- $aptf_settings = $this->aptf_settings;
144
- $config['key'] = $aptf_settings['consumer_key'];
145
- $config['secret'] = $aptf_settings['consumer_secret'];
146
- $config['token'] = $aptf_settings['access_token'];
147
- $config['token_secret'] = $aptf_settings['access_token_secret'];
148
- $config['screenname'] = $aptf_settings['twitter_username'];
149
- $config['directory'] = plugin_dir_path(__FILE__);
150
- $tweets = get_transient('aptf_tweets');
151
- if (false === $tweets) {
152
- $cache_period = intval($aptf_settings['cache_period']) * 60;
153
- $cache_period = ($cache_period < 1) ? 3600 : $cache_period;
154
- $obj = new APTF_Twitter_Class($config);
155
- $res = $obj->getTweets($username, $count, $options);
156
- if(!isset($res['error'])){
157
- set_transient('aptf_tweets', $res, $cache_period);
158
- }
159
-
160
- } else {
161
- $res = $tweets;
162
- }
163
-
164
-
165
- return $res;
166
- }
167
 
168
  /**
169
  * Prints array in pre format
@@ -250,6 +220,7 @@ if (!class_exists('APTF_Class')) {
250
  $current_date = strtotime(date('h:i A M d Y'));
251
  $tweet_date = strtotime($date);
252
  $total_seconds = $current_date - $tweet_date;
 
253
  $seconds = $total_seconds % 60;
254
  $total_minutes = $total_seconds / 60;
255
  ;
@@ -260,14 +231,37 @@ if (!class_exists('APTF_Class')) {
260
  $days = $total_days % 365;
261
  $years = $total_days / 365;
262
 
263
- if ($years > 1) {
264
- $date = $years . __(' year ago', APTF_TD);
265
- } elseif ($days > 1) {
266
- $date = $days . __(' days ago', APTF_TD);
267
- } elseif ($hours > 1) {
268
- $date = $hours . __(' hours ago', APTF_TD);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  } elseif ($minutes > 1) {
270
- $date = $minutes . __(' minutes ago', APTF_TD);
 
 
271
  } else {
272
  $date = __("1 minute ago", APTF_TD);
273
  }
@@ -297,6 +291,35 @@ if (!class_exists('APTF_Class')) {
297
  wp_enqueue_style('aptf-font-css',APTF_CSS_DIR.'/fonts.css',array(),APTF_VERSION);
298
  }
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
 
302
 
4
  * Plugin Name: AccessPress Twitter Feed
5
  * Plugin URI: https://accesspressthemes.com/wordpress-plugins/accesspress-twitter-feed/
6
  * Description: A plugin to show your twitter feed in your site with various configurable settings
7
+ * Version: 1.1.0
8
  * Author: AccessPress Themes
9
  * Author URI: http://accesspressthemes.com
10
  * Text Domain: ap-twitter-feed
24
  define('APTF_CSS_DIR', plugin_dir_url(__FILE__) . 'css');
25
  }
26
  if (!defined('APTF_VERSION')) {
27
+ define('APTF_VERSION', '1.1.0');
28
  }
29
 
30
  if (!defined('APTF_TD')) {
32
  }
33
  include_once('inc/backend/widget.php');
34
  include_once('inc/backend/slider-widget.php');
35
+ include_once("twitteroauth/twitteroauth.php");
36
  if (!class_exists('APTF_Class')) {
37
 
38
  class APTF_Class {
83
  if (!get_option('aptf_settings')) {
84
  update_option('aptf_settings', $default_settings);
85
  }
86
+ delete_transient('aptf_tweets');
87
  }
88
 
89
  /**
133
  return $default_settings;
134
  }
135
 
136
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  /**
139
  * Prints array in pre format
220
  $current_date = strtotime(date('h:i A M d Y'));
221
  $tweet_date = strtotime($date);
222
  $total_seconds = $current_date - $tweet_date;
223
+
224
  $seconds = $total_seconds % 60;
225
  $total_minutes = $total_seconds / 60;
226
  ;
231
  $days = $total_days % 365;
232
  $years = $total_days / 365;
233
 
234
+ if ($years >= 1) {
235
+ if($years == 1){
236
+ $date = $years . __(' year ago', APTF_TD);
237
+ }
238
+ else
239
+ {
240
+ $date = $years . __(' year ago', APTF_TD);
241
+ }
242
+
243
+ } elseif ($days >= 1) {
244
+ if($days == 1){
245
+ $date = $days . __(' day ago', APTF_TD);
246
+ }
247
+ else
248
+ {
249
+ $date = $days . __(' days ago', APTF_TD);
250
+ }
251
+
252
+ } elseif ($hours >= 1) {
253
+ if($hours == 1){
254
+ $date = $hours . __(' hour ago', APTF_TD);
255
+ }
256
+ else
257
+ {
258
+ $date = $hours . __(' hours ago', APTF_TD);
259
+ }
260
+
261
  } elseif ($minutes > 1) {
262
+ $date = $minutes . __(' minutes ago', APTF_TD);
263
+
264
+
265
  } else {
266
  $date = __("1 minute ago", APTF_TD);
267
  }
291
  wp_enqueue_style('aptf-font-css',APTF_CSS_DIR.'/fonts.css',array(),APTF_VERSION);
292
  }
293
 
294
+ /**
295
+ * New Functions
296
+ * */
297
+ function get_oauth_connection($cons_key, $cons_secret, $oauth_token, $oauth_token_secret){
298
+ $ai_connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
299
+ return $ai_connection;
300
+ }
301
+
302
+ function get_twitter_tweets($username,$tweets_number){
303
+ $tweets = get_transient('aptf_tweets');
304
+ if (false === $tweets) {
305
+ $aptf_settings = $this->aptf_settings;
306
+ $consumer_key = $aptf_settings['consumer_key'];
307
+ $consumer_secret = $aptf_settings['consumer_secret'];
308
+ $access_token = $aptf_settings['access_token'];
309
+ $access_token_secret = $aptf_settings['access_token_secret'];
310
+ $oauth_connection = $this->get_oauth_connection($consumer_key, $consumer_secret, $access_token, $access_token_secret);
311
+ $tweets = $oauth_connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$username."&count=".$tweets_number);
312
+ $cache_period = intval($aptf_settings['cache_period']) * 60;
313
+ $cache_period = ($cache_period < 1) ? 3600 : $cache_period;
314
+ if(!isset($tweets->errors)){
315
+ set_transient('aptf_tweets', $tweets, $cache_period);
316
+ }
317
+
318
+ }
319
+
320
+ return $tweets;
321
+ }
322
+
323
 
324
 
325
 
inc/frontend/shortcode.php CHANGED
@@ -2,7 +2,10 @@
2
  $aptf_settings = $this->aptf_settings;
3
  $username = $aptf_settings['twitter_username'];
4
  $display_name = $aptf_settings['twitter_account_name'];
5
- $tweets = $this->get_tweets($username, $aptf_settings['total_feed']);
 
 
 
6
  if(isset($atts['template'])){
7
  $aptf_settings['feed_template'] = $atts['template'];
8
  }
@@ -15,7 +18,7 @@ if(isset($atts['follow_button'])){
15
  }
16
 
17
  }
18
- if(isset($tweets['error'])){
19
  $fallback_message = ($aptf_settings['fallback_message']=='')?__('Something went wrong with the twitter.',APTF_TD):$aptf_settings['fallback_message'];
20
  ?>
21
  <p><?php echo $fallback_message;?></p>
2
  $aptf_settings = $this->aptf_settings;
3
  $username = $aptf_settings['twitter_username'];
4
  $display_name = $aptf_settings['twitter_account_name'];
5
+ //$tweets = $this->get_tweets($username, $aptf_settings['total_feed']);
6
+ $tweets = $this->get_twitter_tweets($username, $aptf_settings['total_feed']);
7
+ //$this->print_array($tweets);
8
+ //die();
9
  if(isset($atts['template'])){
10
  $aptf_settings['feed_template'] = $atts['template'];
11
  }
18
  }
19
 
20
  }
21
+ if(isset($tweets->errors)){
22
  $fallback_message = ($aptf_settings['fallback_message']=='')?__('Something went wrong with the twitter.',APTF_TD):$aptf_settings['fallback_message'];
23
  ?>
24
  <p><?php echo $fallback_message;?></p>
inc/frontend/slider-shortcode.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  $aptf_settings = $this->aptf_settings;
3
  $username = $aptf_settings['twitter_username'];
4
- $tweets = $this->get_tweets($username, $aptf_settings['total_feed']);
5
  $template = isset($atts['template'])?$atts['template']:'template-1';
6
  $auto_slide = isset($atts['auto_slide'])?$atts['auto_slide']:'true';
7
  $slide_controls = isset($atts['controls'])?$atts['controls']:'true';
@@ -15,7 +15,8 @@ if(isset($atts['follow_button'])){
15
  }
16
 
17
  }
18
- if(isset($tweets['error'])){
 
19
  $fallback_message = ($aptf_settings['fallback_message']=='')?__('Something went wrong with the twitter.',APTF_TD):$aptf_settings['fallback_message'];
20
  ?>
21
  <p><?php echo $fallback_message;?></p>
1
  <?php
2
  $aptf_settings = $this->aptf_settings;
3
  $username = $aptf_settings['twitter_username'];
4
+ $tweets = $this->get_twitter_tweets($username, $aptf_settings['total_feed']);
5
  $template = isset($atts['template'])?$atts['template']:'template-1';
6
  $auto_slide = isset($atts['auto_slide'])?$atts['auto_slide']:'true';
7
  $slide_controls = isset($atts['controls'])?$atts['controls']:'true';
15
  }
16
 
17
  }
18
+ if(isset($tweets->errors)){
19
+ //$this->print_array($tweets);
20
  $fallback_message = ($aptf_settings['fallback_message']=='')?__('Something went wrong with the twitter.',APTF_TD):$aptf_settings['fallback_message'];
21
  ?>
22
  <p><?php echo $fallback_message;?></p>
inc/frontend/templates/default/template-1.php CHANGED
@@ -13,8 +13,8 @@
13
 
14
  <div class="aptf-tweet-box">
15
  <?php
16
- if ($tweet['text']) {
17
- $the_tweet = $tweet['text'] . ' '; //adding an extra space to convert hast tag into links
18
  /*
19
  Twitter Developer Display Requirements
20
  https://dev.twitter.com/terms/display-requirements
@@ -27,26 +27,26 @@
27
  */
28
 
29
  // i. User_mentions must link to the mentioned user's profile.
30
- if (is_array($tweet['entities']['user_mentions'])) {
31
- foreach ($tweet['entities']['user_mentions'] as $key => $user_mention) {
32
  $the_tweet = preg_replace(
33
- '/@' . $user_mention['screen_name'] . '/i', '<a href="http://www.twitter.com/' . $user_mention['screen_name'] . '" target="_blank">@' . $user_mention['screen_name'] . '</a>', $the_tweet);
34
  }
35
  }
36
 
37
  // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
38
- if (is_array($tweet['entities']['hashtags'])) {
39
- foreach ($tweet['entities']['hashtags'] as $hashtag) {
40
- $the_tweet = str_replace(' #' . $hashtag['text'] . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag['text'] . '&src=hash" target="_blank">#' . $hashtag['text'] . '</a> ', $the_tweet);
41
  }
42
  }
43
 
44
  // iii. Links in Tweet text must be displayed using the display_url
45
  // field in the URL entities API response, and link to the original t.co url field.
46
- if (is_array($tweet['entities']['urls'])) {
47
- foreach ($tweet['entities']['urls'] as $key => $link) {
48
  $the_tweet = preg_replace(
49
- '`' . $link['url'] . '`', '<a href="' . $link['url'] . '" target="_blank">' . $link['url'] . '</a>', $the_tweet);
50
  }
51
  }
52
 
@@ -79,8 +79,8 @@
79
  ?>
80
 
81
  <p class="aptf-timestamp">
82
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" target="_blank"> -
83
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
84
  </a>
85
  </p>
86
 
13
 
14
  <div class="aptf-tweet-box">
15
  <?php
16
+ if ($tweet->text) {
17
+ $the_tweet = ' '.$tweet->text . ' '; //adding an extra space to convert hast tag into links
18
  /*
19
  Twitter Developer Display Requirements
20
  https://dev.twitter.com/terms/display-requirements
27
  */
28
 
29
  // i. User_mentions must link to the mentioned user's profile.
30
+ if (is_array($tweet->entities->user_mentions)) {
31
+ foreach ($tweet->entities->user_mentions as $key => $user_mention) {
32
  $the_tweet = preg_replace(
33
+ '/@' . $user_mention->screen_name . '/i', '<a href="http://www.twitter.com/' . $user_mention->screen_name . '" target="_blank">@' . $user_mention->screen_name . '</a>', $the_tweet);
34
  }
35
  }
36
 
37
  // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
38
+ if (is_array($tweet->entities->hashtags)) {
39
+ foreach ($tweet->entities->hashtags as $hashtag) {
40
+ $the_tweet = str_replace(' #' . $hashtag->text . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag->text . '&src=hash" target="_blank">#' . $hashtag->text . '</a> ', $the_tweet);
41
  }
42
  }
43
 
44
  // iii. Links in Tweet text must be displayed using the display_url
45
  // field in the URL entities API response, and link to the original t.co url field.
46
+ if (is_array($tweet->entities->urls)) {
47
+ foreach ($tweet->entities->urls as $key => $link) {
48
  $the_tweet = preg_replace(
49
+ '`' . $link->url . '`', '<a href="' . $link->url . '" target="_blank">' . $link->url . '</a>', $the_tweet);
50
  }
51
  }
52
 
79
  ?>
80
 
81
  <p class="aptf-timestamp">
82
+ <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet->id_str; ?>" target="_blank"> -
83
+ <?php echo $this->get_date_format($tweet->created_at, $aptf_settings['time_format']); ?>
84
  </a>
85
  </p>
86
 
inc/frontend/templates/default/template-2.php CHANGED
@@ -12,68 +12,51 @@
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $display_name; ?></a> <span class="aptf-tweet-username"><?php echo $username; ?></span> <?php } ?>
14
  <div class="clear"></div>
15
- <?php
16
- if ($tweet['text']) {
17
- $the_tweet = $tweet['text'] . ' '; //adding an extra space to convert hast tag into links
18
- /*
19
- Twitter Developer Display Requirements
20
- https://dev.twitter.com/terms/display-requirements
21
 
22
- 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
23
- i. User_mentions must link to the mentioned user's profile.
24
- ii. Hashtags must link to a twitter.com search with the hashtag as the query.
25
- iii. Links in Tweet text must be displayed using the display_url
26
- field in the URL entities API response, and link to the original t.co url field.
27
- */
28
 
29
- // i. User_mentions must link to the mentioned user's profile.
30
- if (is_array($tweet['entities']['user_mentions'])) {
31
- foreach ($tweet['entities']['user_mentions'] as $key => $user_mention) {
32
- $the_tweet = preg_replace(
33
- '/@' . $user_mention['screen_name'] . '/i', '<a href="http://www.twitter.com/' . $user_mention['screen_name'] . '" target="_blank">@' . $user_mention['screen_name'] . '</a>', $the_tweet);
 
34
  }
35
- }
36
 
37
- // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
38
- if (is_array($tweet['entities']['hashtags'])) {
39
- foreach ($tweet['entities']['hashtags'] as $hashtag) {
40
- $the_tweet = str_replace(' #' . $hashtag['text'] . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag['text'] . '&src=hash" target="_blank">#' . $hashtag['text'] . '</a> ', $the_tweet);
 
41
  }
42
- }
43
 
44
- // iii. Links in Tweet text must be displayed using the display_url
45
- // field in the URL entities API response, and link to the original t.co url field.
46
- if (is_array($tweet['entities']['urls'])) {
47
- foreach ($tweet['entities']['urls'] as $key => $link) {
48
- $the_tweet = preg_replace(
49
- '`' . $link['url'] . '`', '<a href="' . $link['url'] . '" target="_blank">' . $link['url'] . '</a>', $the_tweet);
 
50
  }
51
- }
52
 
53
- echo $the_tweet . ' ';
54
- ?>
55
  </div><!--tweet content-->
56
  <div class="aptf-tweet-date">
57
- <?php
58
- // 3. Tweet Actions
59
- // Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
60
- // No other social or 3rd party actions similar to Follow, Reply, Retweet and Favorite may be attached to a Tweet.
61
- // get the sprite or images from twitter's developers resource and update your stylesheet
62
- // echo '
63
- // <div class="twitter_intents">
64
- // <p><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'">Reply</a></p>
65
- // <p><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'">Retweet</a></p>
66
- // <p><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'">Favorite</a></p>
67
- // </div>';
68
- // 4. Tweet Timestamp
69
- // The Tweet timestamp must always be visible and include the time and date. e.g., “3:00 PM - 31 May 12”.
70
- // 5. Tweet Permalink
71
- // The Tweet timestamp must always be linked to the Tweet permalink.
72
- ?>
73
-
74
  <p class="aptf-timestamp">
75
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" target="_blank"> -
76
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
77
  </a>
78
  </p>
79
 
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $display_name; ?></a> <span class="aptf-tweet-username"><?php echo $username; ?></span> <?php } ?>
14
  <div class="clear"></div>
15
+ <?php
16
+ if ($tweet->text) {
17
+ $the_tweet = $tweet->text . ' '; //adding an extra space to convert hast tag into links
18
+ /*
19
+ Twitter Developer Display Requirements
20
+ https://dev.twitter.com/terms/display-requirements
21
 
22
+ 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
23
+ i. User_mentions must link to the mentioned user's profile.
24
+ ii. Hashtags must link to a twitter.com search with the hashtag as the query.
25
+ iii. Links in Tweet text must be displayed using the display_url
26
+ field in the URL entities API response, and link to the original t.co url field.
27
+ */
28
 
29
+ // i. User_mentions must link to the mentioned user's profile.
30
+ if (is_array($tweet->entities->user_mentions)) {
31
+ foreach ($tweet->entities->user_mentions as $key => $user_mention) {
32
+ $the_tweet = preg_replace(
33
+ '/@' . $user_mention->screen_name . '/i', '<a href="http://www.twitter.com/' . $user_mention->screen_name . '" target="_blank">@' . $user_mention->screen_name . '</a>', $the_tweet);
34
+ }
35
  }
 
36
 
37
+ // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
38
+ if (is_array($tweet->entities->hashtags)) {
39
+ foreach ($tweet->entities->hashtags as $hashtag) {
40
+ $the_tweet = str_replace(' #' . $hashtag->text . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag->text . '&src=hash" target="_blank">#' . $hashtag->text . '</a> ', $the_tweet);
41
+ }
42
  }
 
43
 
44
+ // iii. Links in Tweet text must be displayed using the display_url
45
+ // field in the URL entities API response, and link to the original t.co url field.
46
+ if (is_array($tweet->entities->urls)) {
47
+ foreach ($tweet->entities->urls as $key => $link) {
48
+ $the_tweet = preg_replace(
49
+ '`' . $link->url . '`', '<a href="' . $link->url . '" target="_blank">' . $link->url . '</a>', $the_tweet);
50
+ }
51
  }
 
52
 
53
+ echo $the_tweet . ' ';
54
+ ?>
55
  </div><!--tweet content-->
56
  <div class="aptf-tweet-date">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  <p class="aptf-timestamp">
58
+ <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet->id_str; ?>" target="_blank"> -
59
+ <?php echo $this->get_date_format($tweet->created_at, $aptf_settings['time_format']); ?>
60
  </a>
61
  </p>
62
 
inc/frontend/templates/default/template-3.php CHANGED
@@ -12,72 +12,54 @@
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" target="_blank"><?php echo $username; ?></a><?php } ?>
14
  <p class="aptf-timestamp">
15
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" target="_blank"> -
16
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
17
  </a>
18
  </p>
19
 
20
  <div class="clear"></div>
21
- <?php
22
- if ($tweet['text']) {
23
- $the_tweet = $tweet['text'] . ' '; //adding an extra space to convert hast tag into links
24
- /*
25
- Twitter Developer Display Requirements
26
- https://dev.twitter.com/terms/display-requirements
27
 
28
- 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
29
- i. User_mentions must link to the mentioned user's profile.
30
- ii. Hashtags must link to a twitter.com search with the hashtag as the query.
31
- iii. Links in Tweet text must be displayed using the display_url
32
- field in the URL entities API response, and link to the original t.co url field.
33
- */
34
 
35
- // i. User_mentions must link to the mentioned user's profile.
36
- if (is_array($tweet['entities']['user_mentions'])) {
37
- foreach ($tweet['entities']['user_mentions'] as $key => $user_mention) {
38
- $the_tweet = preg_replace(
39
- '/@' . $user_mention['screen_name'] . '/i', '<a href="http://www.twitter.com/' . $user_mention['screen_name'] . '" target="_blank">@' . $user_mention['screen_name'] . '</a>', $the_tweet);
 
40
  }
41
- }
42
 
43
- // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
44
- if (is_array($tweet['entities']['hashtags'])) {
45
- foreach ($tweet['entities']['hashtags'] as $hashtag) {
46
- $the_tweet = str_replace(' #' . $hashtag['text'] . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag['text'] . '&src=hash" target="_blank">#' . $hashtag['text'] . '</a> ', $the_tweet);
 
47
  }
48
- }
49
 
50
- // iii. Links in Tweet text must be displayed using the display_url
51
- // field in the URL entities API response, and link to the original t.co url field.
52
- if (is_array($tweet['entities']['urls'])) {
53
- foreach ($tweet['entities']['urls'] as $key => $link) {
54
- $the_tweet = preg_replace(
55
- '`' . $link['url'] . '`', '<a href="' . $link['url'] . '" target="_blank">' . $link['url'] . '</a>', $the_tweet);
 
56
  }
57
- }
58
 
59
- echo $the_tweet . ' ';
60
- ?>
61
  </div><!--tweet content-->
62
- <div class="aptf-tweet-date">
63
- <?php
64
- // 3. Tweet Actions
65
- // Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
66
- // No other social or 3rd party actions similar to Follow, Reply, Retweet and Favorite may be attached to a Tweet.
67
- // get the sprite or images from twitter's developers resource and update your stylesheet
68
- // echo '
69
- // <div class="twitter_intents">
70
- // <p><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'">Reply</a></p>
71
- // <p><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'">Retweet</a></p>
72
- // <p><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'">Favorite</a></p>
73
- // </div>';
74
- // 4. Tweet Timestamp
75
- // The Tweet timestamp must always be visible and include the time and date. e.g., “3:00 PM - 31 May 12”.
76
- // 5. Tweet Permalink
77
- // The Tweet timestamp must always be linked to the Tweet permalink.
78
- ?>
79
-
80
-
81
  <?php
82
  } else {
83
  ?>
@@ -86,7 +68,7 @@
86
  <?php
87
  }
88
  ?>
89
- </div><!--tweet_date-->
90
  <?php if (isset($aptf_settings['display_twitter_actions']) && $aptf_settings['display_twitter_actions'] == 1) { ?>
91
  <!--Tweet Action -->
92
  <?php include(plugin_dir_path(__FILE__) . '../tweet-actions.php'); ?>
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" target="_blank"><?php echo $username; ?></a><?php } ?>
14
  <p class="aptf-timestamp">
15
+ <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet->id_str; ?>" target="_blank"> -
16
+ <?php echo $this->get_date_format($tweet->created_at, $aptf_settings['time_format']); ?>
17
  </a>
18
  </p>
19
 
20
  <div class="clear"></div>
21
+ <?php
22
+ if ($tweet->text) {
23
+ $the_tweet = $tweet->text . ' '; //adding an extra space to convert hast tag into links
24
+ /*
25
+ Twitter Developer Display Requirements
26
+ https://dev.twitter.com/terms/display-requirements
27
 
28
+ 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
29
+ i. User_mentions must link to the mentioned user's profile.
30
+ ii. Hashtags must link to a twitter.com search with the hashtag as the query.
31
+ iii. Links in Tweet text must be displayed using the display_url
32
+ field in the URL entities API response, and link to the original t.co url field.
33
+ */
34
 
35
+ // i. User_mentions must link to the mentioned user's profile.
36
+ if (is_array($tweet->entities->user_mentions)) {
37
+ foreach ($tweet->entities->user_mentions as $key => $user_mention) {
38
+ $the_tweet = preg_replace(
39
+ '/@' . $user_mention->screen_name . '/i', '<a href="http://www.twitter.com/' . $user_mention->screen_name . '" target="_blank">@' . $user_mention->screen_name . '</a>', $the_tweet);
40
+ }
41
  }
 
42
 
43
+ // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
44
+ if (is_array($tweet->entities->hashtags)) {
45
+ foreach ($tweet->entities->hashtags as $hashtag) {
46
+ $the_tweet = str_replace(' #' . $hashtag->text . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag->text . '&src=hash" target="_blank">#' . $hashtag->text . '</a> ', $the_tweet);
47
+ }
48
  }
 
49
 
50
+ // iii. Links in Tweet text must be displayed using the display_url
51
+ // field in the URL entities API response, and link to the original t.co url field.
52
+ if (is_array($tweet->entities->urls)) {
53
+ foreach ($tweet->entities->urls as $key => $link) {
54
+ $the_tweet = preg_replace(
55
+ '`' . $link->url . '`', '<a href="' . $link->url . '" target="_blank">' . $link->url . '</a>', $the_tweet);
56
+ }
57
  }
 
58
 
59
+ echo $the_tweet . ' ';
60
+ ?>
61
  </div><!--tweet content-->
62
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  <?php
64
  } else {
65
  ?>
68
  <?php
69
  }
70
  ?>
71
+
72
  <?php if (isset($aptf_settings['display_twitter_actions']) && $aptf_settings['display_twitter_actions'] == 1) { ?>
73
  <!--Tweet Action -->
74
  <?php include(plugin_dir_path(__FILE__) . '../tweet-actions.php'); ?>
inc/frontend/templates/slider/template-1.php CHANGED
@@ -12,77 +12,54 @@
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $username; ?></a><?php } ?>
14
  <p class="aptf-timestamp">
15
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" class="aptf-tweet-name" target="_blank"> -
16
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
17
  </a>
18
  </p>
19
 
20
  <div class="clear"></div>
21
  <?php
22
- if ($tweet['text']) {
23
- $the_tweet = $tweet['text'] . ' '; //adding an extra space to convert hast tag into links
24
- /*
25
- Twitter Developer Display Requirements
26
- https://dev.twitter.com/terms/display-requirements
27
 
28
- 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
29
- i. User_mentions must link to the mentioned user's profile.
30
- ii. Hashtags must link to a twitter.com search with the hashtag as the query.
31
- iii. Links in Tweet text must be displayed using the display_url
32
- field in the URL entities API response, and link to the original t.co url field.
33
- */
34
 
35
- // i. User_mentions must link to the mentioned user's profile.
36
- if (is_array($tweet['entities']['user_mentions'])) {
37
- foreach ($tweet['entities']['user_mentions'] as $key => $user_mention) {
38
- $the_tweet = preg_replace(
39
- '/@' . $user_mention['screen_name'] . '/i', '<a href="http://www.twitter.com/' . $user_mention['screen_name'] . '" target="_blank">@' . $user_mention['screen_name'] . '</a>', $the_tweet);
 
40
  }
41
- }
42
 
43
- // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
44
- if (is_array($tweet['entities']['hashtags'])) {
45
- foreach ($tweet['entities']['hashtags'] as $hashtag) {
46
- $the_tweet = str_replace(' #' . $hashtag['text'] . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag['text'] . '&src=hash" target="_blank">#' . $hashtag['text'] . '</a> ', $the_tweet);
 
47
  }
48
- }
49
 
50
- // iii. Links in Tweet text must be displayed using the display_url
51
- // field in the URL entities API response, and link to the original t.co url field.
52
- if (is_array($tweet['entities']['urls'])) {
53
- foreach ($tweet['entities']['urls'] as $key => $link) {
54
- $the_tweet = preg_replace(
55
- '`' . $link['url'] . '`', '<a href="' . $link['url'] . '" target="_blank">' . $link['url'] . '</a>', $the_tweet);
 
56
  }
57
- }
58
 
59
- echo $the_tweet . ' ';
60
- ?>
61
  </div><!--tweet content-->
62
- <div class="aptf-tweet-date">
63
- <?php
64
- // 3. Tweet Actions
65
- // Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
66
- // No other social or 3rd party actions similar to Follow, Reply, Retweet and Favorite may be attached to a Tweet.
67
- // get the sprite or images from twitter's developers resource and update your stylesheet
68
- // echo '
69
- // <div class="twitter_intents">
70
- // <p><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'">Reply</a></p>
71
- // <p><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'">Retweet</a></p>
72
- // <p><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'">Favorite</a></p>
73
- // </div>';
74
- // 4. Tweet Timestamp
75
- // The Tweet timestamp must always be visible and include the time and date. e.g., “3:00 PM - 31 May 12”.
76
- // 5. Tweet Permalink
77
- // The Tweet timestamp must always be linked to the Tweet permalink.
78
- ?>
79
-
80
- <!-- <p class="aptf-timestamp">
81
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" target="_blank"> -
82
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
83
- </a>
84
- </p> -->
85
-
86
  <?php
87
  } else {
88
  ?>
@@ -91,7 +68,7 @@
91
  <?php
92
  }
93
  ?>
94
- </div><!--tweet_date-->
95
  <!--Tweet Action -->
96
  <?php include(plugin_dir_path(__FILE__) . '../tweet-actions.php'); ?>
97
  <!--Tweet Action -->
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $username; ?></a><?php } ?>
14
  <p class="aptf-timestamp">
15
+ <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet->id_str; ?>" class="aptf-tweet-name" target="_blank"> -
16
+ <?php echo $this->get_date_format($tweet->created_at, $aptf_settings['time_format']); ?>
17
  </a>
18
  </p>
19
 
20
  <div class="clear"></div>
21
  <?php
22
+ if ($tweet->text) {
23
+ $the_tweet = $tweet->text . ' '; //adding an extra space to convert hast tag into links
24
+ /*
25
+ Twitter Developer Display Requirements
26
+ https://dev.twitter.com/terms/display-requirements
27
 
28
+ 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
29
+ i. User_mentions must link to the mentioned user's profile.
30
+ ii. Hashtags must link to a twitter.com search with the hashtag as the query.
31
+ iii. Links in Tweet text must be displayed using the display_url
32
+ field in the URL entities API response, and link to the original t.co url field.
33
+ */
34
 
35
+ // i. User_mentions must link to the mentioned user's profile.
36
+ if (is_array($tweet->entities->user_mentions)) {
37
+ foreach ($tweet->entities->user_mentions as $key => $user_mention) {
38
+ $the_tweet = preg_replace(
39
+ '/@' . $user_mention->screen_name . '/i', '<a href="http://www.twitter.com/' . $user_mention->screen_name . '" target="_blank">@' . $user_mention->screen_name . '</a>', $the_tweet);
40
+ }
41
  }
 
42
 
43
+ // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
44
+ if (is_array($tweet->entities->hashtags)) {
45
+ foreach ($tweet->entities->hashtags as $hashtag) {
46
+ $the_tweet = str_replace(' #' . $hashtag->text . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag->text . '&src=hash" target="_blank">#' . $hashtag->text . '</a> ', $the_tweet);
47
+ }
48
  }
 
49
 
50
+ // iii. Links in Tweet text must be displayed using the display_url
51
+ // field in the URL entities API response, and link to the original t.co url field.
52
+ if (is_array($tweet->entities->urls)) {
53
+ foreach ($tweet->entities->urls as $key => $link) {
54
+ $the_tweet = preg_replace(
55
+ '`' . $link->url . '`', '<a href="' . $link->url . '" target="_blank">' . $link->url . '</a>', $the_tweet);
56
+ }
57
  }
 
58
 
59
+ echo $the_tweet . ' ';
60
+ ?>
61
  </div><!--tweet content-->
62
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  <?php
64
  } else {
65
  ?>
68
  <?php
69
  }
70
  ?>
71
+
72
  <!--Tweet Action -->
73
  <?php include(plugin_dir_path(__FILE__) . '../tweet-actions.php'); ?>
74
  <!--Tweet Action -->
inc/frontend/templates/slider/template-2.php CHANGED
@@ -12,78 +12,54 @@
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $username; ?></a><?php } ?>
14
  <p class="aptf-timestamp">
15
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" class="aptf-tweet-name" target="_blank"> -
16
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
17
  </a>
18
  </p>
19
 
20
  <div class="clear"></div>
21
  <?php
22
- if ($tweet['text']) {
23
- $the_tweet = $tweet['text'] . ' '; //adding an extra space to convert hast tag into links
24
- /*
25
- Twitter Developer Display Requirements
26
- https://dev.twitter.com/terms/display-requirements
27
 
28
- 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
29
- i. User_mentions must link to the mentioned user's profile.
30
- ii. Hashtags must link to a twitter.com search with the hashtag as the query.
31
- iii. Links in Tweet text must be displayed using the display_url
32
- field in the URL entities API response, and link to the original t.co url field.
33
- */
34
 
35
- // i. User_mentions must link to the mentioned user's profile.
36
- if (is_array($tweet['entities']['user_mentions'])) {
37
- foreach ($tweet['entities']['user_mentions'] as $key => $user_mention) {
38
- $the_tweet = preg_replace(
39
- '/@' . $user_mention['screen_name'] . '/i', '<a href="http://www.twitter.com/' . $user_mention['screen_name'] . '" target="_blank">@' . $user_mention['screen_name'] . '</a>', $the_tweet);
 
40
  }
41
- }
42
 
43
- // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
44
- if (is_array($tweet['entities']['hashtags'])) {
45
- foreach ($tweet['entities']['hashtags'] as $hashtag) {
46
- $the_tweet = str_replace(' #' . $hashtag['text'] . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag['text'] . '&src=hash" target="_blank">#' . $hashtag['text'] . '</a> ', $the_tweet);
 
47
  }
48
- }
49
 
50
- // iii. Links in Tweet text must be displayed using the display_url
51
- // field in the URL entities API response, and link to the original t.co url field.
52
- if (is_array($tweet['entities']['urls'])) {
53
- foreach ($tweet['entities']['urls'] as $key => $link) {
54
- $the_tweet = preg_replace(
55
- '`' . $link['url'] . '`', '<a href="' . $link['url'] . '" target="_blank">' . $link['url'] . '</a>', $the_tweet);
 
56
  }
57
- }
58
 
59
- echo $the_tweet . ' ';
60
- ?>
61
  </div><!--tweet content-->
62
- <div class="aptf-tweet-date">
63
- <?php
64
- // 3. Tweet Actions
65
- // Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
66
- // No other social or 3rd party actions similar to Follow, Reply, Retweet and Favorite may be attached to a Tweet.
67
- // get the sprite or images from twitter's developers resource and update your stylesheet
68
- // echo '
69
- // <div class="twitter_intents">
70
- // <p><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'">Reply</a></p>
71
- // <p><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'">Retweet</a></p>
72
- // <p><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'">Favorite</a></p>
73
- // </div>';
74
- // 4. Tweet Timestamp
75
- // The Tweet timestamp must always be visible and include the time and date. e.g., “3:00 PM - 31 May 12”.
76
- // 5. Tweet Permalink
77
- // The Tweet timestamp must always be linked to the Tweet permalink.
78
- ?>
79
-
80
- <!-- <p class="aptf-timestamp">
81
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" target="_blank"> -
82
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
83
- </a>
84
- </p> -->
85
-
86
- <?php
87
  } else {
88
  ?>
89
 
@@ -91,7 +67,7 @@
91
  <?php
92
  }
93
  ?>
94
- </div><!--tweet_date-->
95
  <!--Tweet Action -->
96
  <?php include(plugin_dir_path(__FILE__) . '../tweet-actions.php'); ?>
97
  <!--Tweet Action -->
12
  <div class="aptf-tweet-content">
13
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $username; ?></a><?php } ?>
14
  <p class="aptf-timestamp">
15
+ <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet->id_str; ?>" class="aptf-tweet-name" target="_blank"> -
16
+ <?php echo $this->get_date_format($tweet->created_at, $aptf_settings['time_format']); ?>
17
  </a>
18
  </p>
19
 
20
  <div class="clear"></div>
21
  <?php
22
+ if ($tweet->text) {
23
+ $the_tweet = $tweet->text . ' '; //adding an extra space to convert hast tag into links
24
+ /*
25
+ Twitter Developer Display Requirements
26
+ https://dev.twitter.com/terms/display-requirements
27
 
28
+ 2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
29
+ i. User_mentions must link to the mentioned user's profile.
30
+ ii. Hashtags must link to a twitter.com search with the hashtag as the query.
31
+ iii. Links in Tweet text must be displayed using the display_url
32
+ field in the URL entities API response, and link to the original t.co url field.
33
+ */
34
 
35
+ // i. User_mentions must link to the mentioned user's profile.
36
+ if (is_array($tweet->entities->user_mentions)) {
37
+ foreach ($tweet->entities->user_mentions as $key => $user_mention) {
38
+ $the_tweet = preg_replace(
39
+ '/@' . $user_mention->screen_name . '/i', '<a href="http://www.twitter.com/' . $user_mention->screen_name . '" target="_blank">@' . $user_mention->screen_name . '</a>', $the_tweet);
40
+ }
41
  }
 
42
 
43
+ // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
44
+ if (is_array($tweet->entities->hashtags)) {
45
+ foreach ($tweet->entities->hashtags as $hashtag) {
46
+ $the_tweet = str_replace(' #' . $hashtag->text . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag->text . '&src=hash" target="_blank">#' . $hashtag->text . '</a> ', $the_tweet);
47
+ }
48
  }
 
49
 
50
+ // iii. Links in Tweet text must be displayed using the display_url
51
+ // field in the URL entities API response, and link to the original t.co url field.
52
+ if (is_array($tweet->entities->urls)) {
53
+ foreach ($tweet->entities->urls as $key => $link) {
54
+ $the_tweet = preg_replace(
55
+ '`' . $link->url . '`', '<a href="' . $link->url . '" target="_blank">' . $link->url . '</a>', $the_tweet);
56
+ }
57
  }
 
58
 
59
+ echo $the_tweet . ' ';
60
+ ?>
61
  </div><!--tweet content-->
62
+ <?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  } else {
64
  ?>
65
 
67
  <?php
68
  }
69
  ?>
70
+
71
  <!--Tweet Action -->
72
  <?php include(plugin_dir_path(__FILE__) . '../tweet-actions.php'); ?>
73
  <!--Tweet Action -->
inc/frontend/templates/slider/template-3.php CHANGED
@@ -13,8 +13,8 @@
13
 
14
  <div class="aptf-tweet-box">
15
  <?php
16
- if ($tweet['text']) {
17
- $the_tweet = $tweet['text'] . ' '; //adding an extra space to convert hast tag into links
18
  /*
19
  Twitter Developer Display Requirements
20
  https://dev.twitter.com/terms/display-requirements
@@ -27,26 +27,26 @@
27
  */
28
 
29
  // i. User_mentions must link to the mentioned user's profile.
30
- if (is_array($tweet['entities']['user_mentions'])) {
31
- foreach ($tweet['entities']['user_mentions'] as $key => $user_mention) {
32
  $the_tweet = preg_replace(
33
- '/@' . $user_mention['screen_name'] . '/i', '<a href="http://www.twitter.com/' . $user_mention['screen_name'] . '" target="_blank">@' . $user_mention['screen_name'] . '</a>', $the_tweet);
34
  }
35
  }
36
 
37
  // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
38
- if (is_array($tweet['entities']['hashtags'])) {
39
- foreach ($tweet['entities']['hashtags'] as $hashtag) {
40
- $the_tweet = str_replace(' #' . $hashtag['text'] . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag['text'] . '&src=hash" target="_blank">#' . $hashtag['text'] . '</a> ', $the_tweet);
41
  }
42
  }
43
 
44
  // iii. Links in Tweet text must be displayed using the display_url
45
  // field in the URL entities API response, and link to the original t.co url field.
46
- if (is_array($tweet['entities']['urls'])) {
47
- foreach ($tweet['entities']['urls'] as $key => $link) {
48
  $the_tweet = preg_replace(
49
- '`' . $link['url'] . '`', '<a href="' . $link['url'] . '" target="_blank">' . $link['url'] . '</a>', $the_tweet);
50
  }
51
  }
52
 
@@ -61,29 +61,13 @@
61
  </div>
62
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $username; ?></a> <?php } ?>
63
  <div class="aptf-tweet-date">
64
- <?php
65
- // 3. Tweet Actions
66
- // Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
67
- // No other social or 3rd party actions similar to Follow, Reply, Retweet and Favorite may be attached to a Tweet.
68
- // get the sprite or images from twitter's developers resource and update your stylesheet
69
- // echo '
70
- // <div class="twitter_intents">
71
- // <p><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'">Reply</a></p>
72
- // <p><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'">Retweet</a></p>
73
- // <p><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'">Favorite</a></p>
74
- // </div>';
75
- // 4. Tweet Timestamp
76
- // The Tweet timestamp must always be visible and include the time and date. e.g., “3:00 PM - 31 May 12”.
77
- // 5. Tweet Permalink
78
- // The Tweet timestamp must always be linked to the Tweet permalink.
79
- ?>
80
-
81
  <p class="aptf-timestamp">
82
- <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet['id_str']; ?>" target="_blank"> -
83
- <?php echo $this->get_date_format($tweet['created_at'], $aptf_settings['time_format']); ?>
84
  </a>
85
  </p>
86
-
87
  <?php
88
  } else {
89
  ?>
@@ -92,7 +76,7 @@
92
  <?php
93
  }
94
  ?>
95
- </div><!--tweet_date-->
96
 
97
 
98
  </div><!-- single_tweet_wrap-->
13
 
14
  <div class="aptf-tweet-box">
15
  <?php
16
+ if ($tweet->text) {
17
+ $the_tweet = $tweet->text . ' '; //adding an extra space to convert hast tag into links
18
  /*
19
  Twitter Developer Display Requirements
20
  https://dev.twitter.com/terms/display-requirements
27
  */
28
 
29
  // i. User_mentions must link to the mentioned user's profile.
30
+ if (is_array($tweet->entities->user_mentions)) {
31
+ foreach ($tweet->entities->user_mentions as $key => $user_mention) {
32
  $the_tweet = preg_replace(
33
+ '/@' . $user_mention->screen_name . '/i', '<a href="http://www.twitter.com/' . $user_mention->screen_name . '" target="_blank">@' . $user_mention->screen_name . '</a>', $the_tweet);
34
  }
35
  }
36
 
37
  // ii. Hashtags must link to a twitter.com search with the hashtag as the query.
38
+ if (is_array($tweet->entities->hashtags)) {
39
+ foreach ($tweet->entities->hashtags as $hashtag) {
40
+ $the_tweet = str_replace(' #' . $hashtag->text . ' ', ' <a href="https://twitter.com/search?q=%23' . $hashtag->text . '&src=hash" target="_blank">#' . $hashtag->text . '</a> ', $the_tweet);
41
  }
42
  }
43
 
44
  // iii. Links in Tweet text must be displayed using the display_url
45
  // field in the URL entities API response, and link to the original t.co url field.
46
+ if (is_array($tweet->entities->urls)) {
47
+ foreach ($tweet->entities->urls as $key => $link) {
48
  $the_tweet = preg_replace(
49
+ '`' . $link->url . '`', '<a href="' . $link->url . '" target="_blank">' . $link->url . '</a>', $the_tweet);
50
  }
51
  }
52
 
61
  </div>
62
  <?php if ($aptf_settings['display_username'] == 1) { ?><a href="http://twitter.com/<?php echo $username; ?>" class="aptf-tweet-name" target="_blank"><?php echo $username; ?></a> <?php } ?>
63
  <div class="aptf-tweet-date">
64
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  <p class="aptf-timestamp">
66
+ <a href="https://twitter.com/<?php echo $username; ?>/status/<?php echo $tweet->id_str; ?>" target="_blank"> -
67
+ <?php echo $this->get_date_format($tweet->created_at, $aptf_settings['time_format']); ?>
68
  </a>
69
  </p>
70
+ </div><!--tweet_date-->
71
  <?php
72
  } else {
73
  ?>
76
  <?php
77
  }
78
  ?>
79
+
80
 
81
 
82
  </div><!-- single_tweet_wrap-->
inc/frontend/templates/tweet-actions.php CHANGED
@@ -1,5 +1,5 @@
1
  <div class="aptf-tweet-actions-wrapper aptf-tweet-actions">
2
- <a href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $tweet['id_str']; ?>" class="aptf-tweet-reply aptf-tweet-action-reply" target="_blank">h</a>
3
- <a href="https://twitter.com/intent/retweet?tweet_id=<?php echo $tweet['id_str']; ?>" class="aptf-tweet-retweet aptf-tweet-action-retweet" target="_blank">J</a>
4
- <a href="https://twitter.com/intent/favorite?tweet_id=<?php echo $tweet['id_str']; ?>" class="aptf-tweet-fav aptf-tweet-action-favourite" target="_blank">R</a>
5
  </div>
1
  <div class="aptf-tweet-actions-wrapper aptf-tweet-actions">
2
+ <a href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $tweet->id_str; ?>" class="aptf-tweet-reply aptf-tweet-action-reply" target="_blank">h</a>
3
+ <a href="https://twitter.com/intent/retweet?tweet_id=<?php echo $tweet->id_str; ?>" class="aptf-tweet-retweet aptf-tweet-action-retweet" target="_blank">J</a>
4
+ <a href="https://twitter.com/intent/favorite?tweet_id=<?php echo $tweet->id_str; ?>" class="aptf-tweet-fav aptf-tweet-action-favourite" target="_blank">R</a>
5
  </div>
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: twitter, twitter feeds, twitter slider, twitter feeds slider, twitter plug
4
  Donate link: http://accesspressthemes.com/donation/
5
  Requires at least: 3.8
6
  Tested up to: 4.1
7
- Stable tag: 1.0.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -83,6 +83,10 @@ Once you install the plugin , you can check some general documentation about how
83
 
84
 
85
  == Changelog ==
 
 
 
 
86
  = 1.0.2 =
87
  * Fixed fallback message for default shortcode and widget
88
 
4
  Donate link: http://accesspressthemes.com/donation/
5
  Requires at least: 3.8
6
  Tested up to: 4.1
7
+ Stable tag: 1.1.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
83
 
84
 
85
  == Changelog ==
86
+ = 1.1.0 =
87
+ * API updated
88
+ * Small bug for time elasped date format fixed
89
+
90
  = 1.0.2 =
91
  * Fixed fallback message for default shortcode and widget
92
 
{oauth → twitteroauth}/OAuth.php RENAMED
@@ -1,874 +1,874 @@
1
- <?php
2
- // vim: foldmethod=marker
3
-
4
- /* Generic exception class
5
- */
6
- if (!class_exists('OAuthException')) {
7
- class OAuthException extends Exception {
8
- // pass
9
- }
10
- }
11
-
12
- class OAuthConsumer {
13
- public $key;
14
- public $secret;
15
-
16
- function __construct($key, $secret, $callback_url=NULL) {
17
- $this->key = $key;
18
- $this->secret = $secret;
19
- $this->callback_url = $callback_url;
20
- }
21
-
22
- function __toString() {
23
- return "OAuthConsumer[key=$this->key,secret=$this->secret]";
24
- }
25
- }
26
-
27
- class OAuthToken {
28
- // access tokens and request tokens
29
- public $key;
30
- public $secret;
31
-
32
- /**
33
- * key = the token
34
- * secret = the token secret
35
- */
36
- function __construct($key, $secret) {
37
- $this->key = $key;
38
- $this->secret = $secret;
39
- }
40
-
41
- /**
42
- * generates the basic string serialization of a token that a server
43
- * would respond to request_token and access_token calls with
44
- */
45
- function to_string() {
46
- return "oauth_token=" .
47
- OAuthUtil::urlencode_rfc3986($this->key) .
48
- "&oauth_token_secret=" .
49
- OAuthUtil::urlencode_rfc3986($this->secret);
50
- }
51
-
52
- function __toString() {
53
- return $this->to_string();
54
- }
55
- }
56
-
57
- /**
58
- * A class for implementing a Signature Method
59
- * See section 9 ("Signing Requests") in the spec
60
- */
61
- abstract class OAuthSignatureMethod {
62
- /**
63
- * Needs to return the name of the Signature Method (ie HMAC-SHA1)
64
- * @return string
65
- */
66
- abstract public function get_name();
67
-
68
- /**
69
- * Build up the signature
70
- * NOTE: The output of this function MUST NOT be urlencoded.
71
- * the encoding is handled in OAuthRequest when the final
72
- * request is serialized
73
- * @param OAuthRequest $request
74
- * @param OAuthConsumer $consumer
75
- * @param OAuthToken $token
76
- * @return string
77
- */
78
- abstract public function build_signature($request, $consumer, $token);
79
-
80
- /**
81
- * Verifies that a given signature is correct
82
- * @param OAuthRequest $request
83
- * @param OAuthConsumer $consumer
84
- * @param OAuthToken $token
85
- * @param string $signature
86
- * @return bool
87
- */
88
- public function check_signature($request, $consumer, $token, $signature) {
89
- $built = $this->build_signature($request, $consumer, $token);
90
- return $built == $signature;
91
- }
92
- }
93
-
94
- /**
95
- * The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104]
96
- * where the Signature Base String is the text and the key is the concatenated values (each first
97
- * encoded per Parameter Encoding) of the Consumer Secret and Token Secret, separated by an '&'
98
- * character (ASCII code 38) even if empty.
99
- * - Chapter 9.2 ("HMAC-SHA1")
100
- */
101
- class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {
102
- function get_name() {
103
- return "HMAC-SHA1";
104
- }
105
-
106
- public function build_signature($request, $consumer, $token) {
107
- $base_string = $request->get_signature_base_string();
108
- $request->base_string = $base_string;
109
-
110
- $key_parts = array(
111
- $consumer->secret,
112
- ($token) ? $token->secret : ""
113
- );
114
-
115
- $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
116
- $key = implode('&', $key_parts);
117
-
118
- return base64_encode(hash_hmac('sha1', $base_string, $key, true));
119
- }
120
- }
121
-
122
- /**
123
- * The PLAINTEXT method does not provide any security protection and SHOULD only be used
124
- * over a secure channel such as HTTPS. It does not use the Signature Base String.
125
- * - Chapter 9.4 ("PLAINTEXT")
126
- */
127
- class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {
128
- public function get_name() {
129
- return "PLAINTEXT";
130
- }
131
-
132
- /**
133
- * oauth_signature is set to the concatenated encoded values of the Consumer Secret and
134
- * Token Secret, separated by a '&' character (ASCII code 38), even if either secret is
135
- * empty. The result MUST be encoded again.
136
- * - Chapter 9.4.1 ("Generating Signatures")
137
- *
138
- * Please note that the second encoding MUST NOT happen in the SignatureMethod, as
139
- * OAuthRequest handles this!
140
- */
141
- public function build_signature($request, $consumer, $token) {
142
- $key_parts = array(
143
- $consumer->secret,
144
- ($token) ? $token->secret : ""
145
- );
146
-
147
- $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
148
- $key = implode('&', $key_parts);
149
- $request->base_string = $key;
150
-
151
- return $key;
152
- }
153
- }
154
-
155
- /**
156
- * The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in
157
- * [RFC3447] section 8.2 (more simply known as PKCS#1), using SHA-1 as the hash function for
158
- * EMSA-PKCS1-v1_5. It is assumed that the Consumer has provided its RSA public key in a
159
- * verified way to the Service Provider, in a manner which is beyond the scope of this
160
- * specification.
161
- * - Chapter 9.3 ("RSA-SHA1")
162
- */
163
- abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
164
- public function get_name() {
165
- return "RSA-SHA1";
166
- }
167
-
168
- // Up to the SP to implement this lookup of keys. Possible ideas are:
169
- // (1) do a lookup in a table of trusted certs keyed off of consumer
170
- // (2) fetch via http using a url provided by the requester
171
- // (3) some sort of specific discovery code based on request
172
- //
173
- // Either way should return a string representation of the certificate
174
- protected abstract function fetch_public_cert(&$request);
175
-
176
- // Up to the SP to implement this lookup of keys. Possible ideas are:
177
- // (1) do a lookup in a table of trusted certs keyed off of consumer
178
- //
179
- // Either way should return a string representation of the certificate
180
- protected abstract function fetch_private_cert(&$request);
181
-
182
- public function build_signature($request, $consumer, $token) {
183
- $base_string = $request->get_signature_base_string();
184
- $request->base_string = $base_string;
185
-
186
- // Fetch the private key cert based on the request
187
- $cert = $this->fetch_private_cert($request);
188
-
189
- // Pull the private key ID from the certificate
190
- $privatekeyid = openssl_get_privatekey($cert);
191
-
192
- // Sign using the key
193
- $ok = openssl_sign($base_string, $signature, $privatekeyid);
194
-
195
- // Release the key resource
196
- openssl_free_key($privatekeyid);
197
-
198
- return base64_encode($signature);
199
- }
200
-
201
- public function check_signature($request, $consumer, $token, $signature) {
202
- $decoded_sig = base64_decode($signature);
203
-
204
- $base_string = $request->get_signature_base_string();
205
-
206
- // Fetch the public key cert based on the request
207
- $cert = $this->fetch_public_cert($request);
208
-
209
- // Pull the public key ID from the certificate
210
- $publickeyid = openssl_get_publickey($cert);
211
-
212
- // Check the computed signature against the one passed in the query
213
- $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
214
-
215
- // Release the key resource
216
- openssl_free_key($publickeyid);
217
-
218
- return $ok == 1;
219
- }
220
- }
221
-
222
- class OAuthRequest {
223
- private $parameters;
224
- private $http_method;
225
- private $http_url;
226
- // for debug purposes
227
- public $base_string;
228
- public static $version = '1.0';
229
- public static $POST_INPUT = 'php://input';
230
-
231
- function __construct($http_method, $http_url, $parameters=NULL) {
232
- @$parameters or $parameters = array();
233
- $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
234
- $this->parameters = $parameters;
235
- $this->http_method = $http_method;
236
- $this->http_url = $http_url;
237
- }
238
-
239
-
240
- /**
241
- * attempt to build up a request from what was passed to the server
242
- */
243
- public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
244
- $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
245
- ? 'http'
246
- : 'https';
247
- @$http_url or $http_url = $scheme .
248
- '://' . $_SERVER['HTTP_HOST'] .
249
- ':' .
250
- $_SERVER['SERVER_PORT'] .
251
- $_SERVER['REQUEST_URI'];
252
- @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
253
-
254
- // We weren't handed any parameters, so let's find the ones relevant to
255
- // this request.
256
- // If you run XML-RPC or similar you should use this to provide your own
257
- // parsed parameter-list
258
- if (!$parameters) {
259
- // Find request headers
260
- $request_headers = OAuthUtil::get_headers();
261
-
262
- // Parse the query-string to find GET parameters
263
- $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
264
-
265
- // It's a POST request of the proper content-type, so parse POST
266
- // parameters and add those overriding any duplicates from GET
267
- if ($http_method == "POST"
268
- && @strstr($request_headers["Content-Type"],
269
- "application/x-www-form-urlencoded")
270
- ) {
271
- $post_data = OAuthUtil::parse_parameters(
272
- file_get_contents(self::$POST_INPUT)
273
- );
274
- $parameters = array_merge($parameters, $post_data);
275
- }
276
-
277
- // We have a Authorization-header with OAuth data. Parse the header
278
- // and add those overriding any duplicates from GET or POST
279
- if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
280
- $header_parameters = OAuthUtil::split_header(
281
- $request_headers['Authorization']
282
- );
283
- $parameters = array_merge($parameters, $header_parameters);
284
- }
285
-
286
- }
287
-
288
- return new OAuthRequest($http_method, $http_url, $parameters);
289
- }
290
-
291
- /**
292
- * pretty much a helper function to set up the request
293
- */
294
- public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
295
- @$parameters or $parameters = array();
296
- $defaults = array("oauth_version" => OAuthRequest::$version,
297
- "oauth_nonce" => OAuthRequest::generate_nonce(),
298
- "oauth_timestamp" => OAuthRequest::generate_timestamp(),
299
- "oauth_consumer_key" => $consumer->key);
300
- if ($token)
301
- $defaults['oauth_token'] = $token->key;
302
-
303
- $parameters = array_merge($defaults, $parameters);
304
-
305
- return new OAuthRequest($http_method, $http_url, $parameters);
306
- }
307
-
308
- public function set_parameter($name, $value, $allow_duplicates = true) {
309
- if ($allow_duplicates && isset($this->parameters[$name])) {
310
- // We have already added parameter(s) with this name, so add to the list
311
- if (is_scalar($this->parameters[$name])) {
312
- // This is the first duplicate, so transform scalar (string)
313
- // into an array so we can add the duplicates
314
- $this->parameters[$name] = array($this->parameters[$name]);
315
- }
316
-
317
- $this->parameters[$name][] = $value;
318
- } else {
319
- $this->parameters[$name] = $value;
320
- }
321
- }
322
-
323
- public function get_parameter($name) {
324
- return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
325
- }
326
-
327
- public function get_parameters() {
328
- return $this->parameters;
329
- }
330
-
331
- public function unset_parameter($name) {
332
- unset($this->parameters[$name]);
333
- }
334
-
335
- /**
336
- * The request parameters, sorted and concatenated into a normalized string.
337
- * @return string
338
- */
339
- public function get_signable_parameters() {
340
- // Grab all parameters
341
- $params = $this->parameters;
342
-
343
- // Remove oauth_signature if present
344
- // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
345
- if (isset($params['oauth_signature'])) {
346
- unset($params['oauth_signature']);
347
- }
348
-
349
- return OAuthUtil::build_http_query($params);
350
- }
351
-
352
- /**
353
- * Returns the base string of this request
354
- *
355
- * The base string defined as the method, the url
356
- * and the parameters (normalized), each urlencoded
357
- * and the concated with &.
358
- */
359
- public function get_signature_base_string() {
360
- $parts = array(
361
- $this->get_normalized_http_method(),
362
- $this->get_normalized_http_url(),
363
- $this->get_signable_parameters()
364
- );
365
-
366
- $parts = OAuthUtil::urlencode_rfc3986($parts);
367
-
368
- return implode('&', $parts);
369
- }
370
-
371
- /**
372
- * just uppercases the http method
373
- */
374
- public function get_normalized_http_method() {
375
- return strtoupper($this->http_method);
376
- }
377
-
378
- /**
379
- * parses the url and rebuilds it to be
380
- * scheme://host/path
381
- */
382
- public function get_normalized_http_url() {
383
- $parts = parse_url($this->http_url);
384
-
385
- $port = @$parts['port'];
386
- $scheme = $parts['scheme'];
387
- $host = $parts['host'];
388
- $path = @$parts['path'];
389
-
390
- $port or $port = ($scheme == 'https') ? '443' : '80';
391
-
392
- if (($scheme == 'https' && $port != '443')
393
- || ($scheme == 'http' && $port != '80')) {
394
- $host = "$host:$port";
395
- }
396
- return "$scheme://$host$path";
397
- }
398
-
399
- /**
400
- * builds a url usable for a GET request
401
- */
402
- public function to_url() {
403
- $post_data = $this->to_postdata();
404
- $out = $this->get_normalized_http_url();
405
- if ($post_data) {
406
- $out .= '?'.$post_data;
407
- }
408
- return $out;
409
- }
410
-
411
- /**
412
- * builds the data one would send in a POST request
413
- */
414
- public function to_postdata() {
415
- return OAuthUtil::build_http_query($this->parameters);
416
- }
417
-
418
- /**
419
- * builds the Authorization: header
420
- */
421
- public function to_header($realm=null) {
422
- $first = true;
423
- if($realm) {
424
- $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
425
- $first = false;
426
- } else
427
- $out = 'Authorization: OAuth';
428
-
429
- $total = array();
430
- foreach ($this->parameters as $k => $v) {
431
- if (substr($k, 0, 5) != "oauth") continue;
432
- if (is_array($v)) {
433
- throw new OAuthException('Arrays not supported in headers');
434
- }
435
- $out .= ($first) ? ' ' : ',';
436
- $out .= OAuthUtil::urlencode_rfc3986($k) .
437
- '="' .
438
- OAuthUtil::urlencode_rfc3986($v) .
439
- '"';
440
- $first = false;
441
- }
442
- return $out;
443
- }
444
-
445
- public function __toString() {
446
- return $this->to_url();
447
- }
448
-
449
-
450
- public function sign_request($signature_method, $consumer, $token) {
451
- $this->set_parameter(
452
- "oauth_signature_method",
453
- $signature_method->get_name(),
454
- false
455
- );
456
- $signature = $this->build_signature($signature_method, $consumer, $token);
457
- $this->set_parameter("oauth_signature", $signature, false);
458
- }
459
-
460
- public function build_signature($signature_method, $consumer, $token) {
461
- $signature = $signature_method->build_signature($this, $consumer, $token);
462
- return $signature;
463
- }
464
-
465
- /**
466
- * util function: current timestamp
467
- */
468
- private static function generate_timestamp() {
469
- return time();
470
- }
471
-
472
- /**
473
- * util function: current nonce
474
- */
475
- private static function generate_nonce() {
476
- $mt = microtime();
477
- $rand = mt_rand();
478
-
479
- return md5($mt . $rand); // md5s look nicer than numbers
480
- }
481
- }
482
-
483
- class OAuthServer {
484
- protected $timestamp_threshold = 300; // in seconds, five minutes
485
- protected $version = '1.0'; // hi blaine
486
- protected $signature_methods = array();
487
-
488
- protected $data_store;
489
-
490
- function __construct($data_store) {
491
- $this->data_store = $data_store;
492
- }
493
-
494
- public function add_signature_method($signature_method) {
495
- $this->signature_methods[$signature_method->get_name()] =
496
- $signature_method;
497
- }
498
-
499
- // high level functions
500
-
501
- /**
502
- * process a request_token request
503
- * returns the request token on success
504
- */
505
- public function fetch_request_token(&$request) {
506
- $this->get_version($request);
507
-
508
- $consumer = $this->get_consumer($request);
509
-
510
- // no token required for the initial token request
511
- $token = NULL;
512
-
513
- $this->check_signature($request, $consumer, $token);
514
-
515
- // Rev A change
516
- $callback = $request->get_parameter('oauth_callback');
517
- $new_token = $this->data_store->new_request_token($consumer, $callback);
518
-
519
- return $new_token;
520
- }
521
-
522
- /**
523
- * process an access_token request
524
- * returns the access token on success
525
- */
526
- public function fetch_access_token(&$request) {
527
- $this->get_version($request);
528
-
529
- $consumer = $this->get_consumer($request);
530
-
531
- // requires authorized request token
532
- $token = $this->get_token($request, $consumer, "request");
533
-
534
- $this->check_signature($request, $consumer, $token);
535
-
536
- // Rev A change
537
- $verifier = $request->get_parameter('oauth_verifier');
538
- $new_token = $this->data_store->new_access_token($token, $consumer, $verifier);
539
-
540
- return $new_token;
541
- }
542
-
543
- /**
544
- * verify an api call, checks all the parameters
545
- */
546
- public function verify_request(&$request) {
547
- $this->get_version($request);
548
- $consumer = $this->get_consumer($request);
549
- $token = $this->get_token($request, $consumer, "access");
550
- $this->check_signature($request, $consumer, $token);
551
- return array($consumer, $token);
552
- }
553
-
554
- // Internals from here
555
- /**
556
- * version 1
557
- */
558
- private function get_version(&$request) {
559
- $version = $request->get_parameter("oauth_version");
560
- if (!$version) {
561
- // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present.
562
- // Chapter 7.0 ("Accessing Protected Ressources")
563
- $version = '1.0';
564
- }
565
- if ($version !== $this->version) {
566
- throw new OAuthException("OAuth version '$version' not supported");
567
- }
568
- return $version;
569
- }
570
-
571
- /**
572
- * figure out the signature with some defaults
573
- */
574
- private function get_signature_method(&$request) {
575
- $signature_method =
576
- @$request->get_parameter("oauth_signature_method");
577
-
578
- if (!$signature_method) {
579
- // According to chapter 7 ("Accessing Protected Ressources") the signature-method
580
- // parameter is required, and we can't just fallback to PLAINTEXT
581
- throw new OAuthException('No signature method parameter. This parameter is required');
582
- }
583
-
584
- if (!in_array($signature_method,
585
- array_keys($this->signature_methods))) {
586
- throw new OAuthException(
587
- "Signature method '$signature_method' not supported " .
588
- "try one of the following: " .
589
- implode(", ", array_keys($this->signature_methods))
590
- );
591
- }
592
- return $this->signature_methods[$signature_method];
593
- }
594
-
595
- /**
596
- * try to find the consumer for the provided request's consumer key
597
- */
598
- private function get_consumer(&$request) {
599
- $consumer_key = @$request->get_parameter("oauth_consumer_key");
600
- if (!$consumer_key) {
601
- throw new OAuthException("Invalid consumer key");
602
- }
603
-
604
- $consumer = $this->data_store->lookup_consumer($consumer_key);
605
- if (!$consumer) {
606
- throw new OAuthException("Invalid consumer");
607
- }
608
-
609
- return $consumer;
610
- }
611
-
612
- /**
613
- * try to find the token for the provided request's token key
614
- */
615
- private function get_token(&$request, $consumer, $token_type="access") {
616
- $token_field = @$request->get_parameter('oauth_token');
617
- $token = $this->data_store->lookup_token(
618
- $consumer, $token_type, $token_field
619
- );
620
- if (!$token) {
621
- throw new OAuthException("Invalid $token_type token: $token_field");
622
- }
623
- return $token;
624
- }
625
-
626
- /**
627
- * all-in-one function to check the signature on a request
628
- * should guess the signature method appropriately
629
- */
630
- private function check_signature(&$request, $consumer, $token) {
631
- // this should probably be in a different method
632
- $timestamp = @$request->get_parameter('oauth_timestamp');
633
- $nonce = @$request->get_parameter('oauth_nonce');
634
-
635
- $this->check_timestamp($timestamp);
636
- $this->check_nonce($consumer, $token, $nonce, $timestamp);
637
-
638
- $signature_method = $this->get_signature_method($request);
639
-
640
- $signature = $request->get_parameter('oauth_signature');
641
- $valid_sig = $signature_method->check_signature(
642
- $request,
643
- $consumer,
644
- $token,
645
- $signature
646
- );
647
-
648
- if (!$valid_sig) {
649
- throw new OAuthException("Invalid signature");
650
- }
651
- }
652
-
653
- /**
654
- * check that the timestamp is new enough
655
- */
656
- private function check_timestamp($timestamp) {
657
- if( ! $timestamp )
658
- throw new OAuthException(
659
- 'Missing timestamp parameter. The parameter is required'
660
- );
661
-
662
- // verify that timestamp is recentish
663
- $now = time();
664
- if (abs($now - $timestamp) > $this->timestamp_threshold) {
665
- throw new OAuthException(
666
- "Expired timestamp, yours $timestamp, ours $now"
667
- );
668
- }
669
- }
670
-
671
- /**
672
- * check that the nonce is not repeated
673
- */
674
- private function check_nonce($consumer, $token, $nonce, $timestamp) {
675
- if( ! $nonce )
676
- throw new OAuthException(
677
- 'Missing nonce parameter. The parameter is required'
678
- );
679
-
680
- // verify that the nonce is uniqueish
681
- $found = $this->data_store->lookup_nonce(
682
- $consumer,
683
- $token,
684
- $nonce,
685
- $timestamp
686
- );
687
- if ($found) {
688
- throw new OAuthException("Nonce already used: $nonce");
689
- }
690
- }
691
-
692
- }
693
-
694
- class OAuthDataStore {
695
- function lookup_consumer($consumer_key) {
696
- // implement me
697
- }
698
-
699
- function lookup_token($consumer, $token_type, $token) {
700
- // implement me
701
- }
702
-
703
- function lookup_nonce($consumer, $token, $nonce, $timestamp) {
704
- // implement me
705
- }
706
-
707
- function new_request_token($consumer, $callback = null) {
708
- // return a new token attached to this consumer
709
- }
710
-
711
- function new_access_token($token, $consumer, $verifier = null) {
712
- // return a new access token attached to this consumer
713
- // for the user associated with this token if the request token
714
- // is authorized
715
- // should also invalidate the request token
716
- }
717
-
718
- }
719
-
720
- class OAuthUtil {
721
- public static function urlencode_rfc3986($input) {
722
- if (is_array($input)) {
723
- return array_map(array('OAuthUtil', 'urlencode_rfc3986'), $input);
724
- } else if (is_scalar($input)) {
725
- return str_replace(
726
- '+',
727
- ' ',
728
- str_replace('%7E', '~', rawurlencode($input))
729
- );
730
- } else {
731
- return '';
732
- }
733
- }
734
-
735
-
736
- // This decode function isn't taking into consideration the above
737
- // modifications to the encoding process. However, this method doesn't
738
- // seem to be used anywhere so leaving it as is.
739
- public static function urldecode_rfc3986($string) {
740
- return urldecode($string);
741
- }
742
-
743
- // Utility function for turning the Authorization: header into
744
- // parameters, has to do some unescaping
745
- // Can filter out any non-oauth parameters if needed (default behaviour)
746
- public static function split_header($header, $only_allow_oauth_parameters = true) {
747
- $pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
748
- $offset = 0;
749
- $params = array();
750
- while (preg_match($pattern, $header, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
751
- $match = $matches[0];
752
- $header_name = $matches[2][0];
753
- $header_content = (isset($matches[5])) ? $matches[5][0] : $matches[4][0];
754
- if (preg_match('/^oauth_/', $header_name) || !$only_allow_oauth_parameters) {
755
- $params[$header_name] = OAuthUtil::urldecode_rfc3986($header_content);
756
- }
757
- $offset = $match[1] + strlen($match[0]);
758
- }
759
-
760
- if (isset($params['realm'])) {
761
- unset($params['realm']);
762
- }
763
-
764
- return $params;
765
- }
766
-
767
- // helper to try to sort out headers for people who aren't running apache
768
- public static function get_headers() {
769
- if (function_exists('apache_request_headers')) {
770
- // we need this to get the actual Authorization: header
771
- // because apache tends to tell us it doesn't exist
772
- $headers = apache_request_headers();
773
-
774
- // sanitize the output of apache_request_headers because
775
- // we always want the keys to be Cased-Like-This and arh()
776
- // returns the headers in the same case as they are in the
777
- // request
778
- $out = array();
779
- foreach( $headers AS $key => $value ) {
780
- $key = str_replace(
781
- " ",
782
- "-",
783
- ucwords(strtolower(str_replace("-", " ", $key)))
784
- );
785
- $out[$key] = $value;
786
- }
787
- } else {
788
- // otherwise we don't have apache and are just going to have to hope
789
- // that $_SERVER actually contains what we need
790
- $out = array();
791
- if( isset($_SERVER['CONTENT_TYPE']) )
792
- $out['Content-Type'] = $_SERVER['CONTENT_TYPE'];
793
- if( isset($_ENV['CONTENT_TYPE']) )
794
- $out['Content-Type'] = $_ENV['CONTENT_TYPE'];
795
-
796
- foreach ($_SERVER as $key => $value) {
797
- if (substr($key, 0, 5) == "HTTP_") {
798
- // this is chaos, basically it is just there to capitalize the first
799
- // letter of every word that is not an initial HTTP and strip HTTP
800
- // code from przemek
801
- $key = str_replace(
802
- " ",
803
- "-",
804
- ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
805
- );
806
- $out[$key] = $value;
807
- }
808
- }
809
- }
810
- return $out;
811
- }
812
-
813
- // This function takes a input like a=b&a=c&d=e and returns the parsed
814
- // parameters like this
815
- // array('a' => array('b','c'), 'd' => 'e')
816
- public static function parse_parameters( $input ) {
817
- if (!isset($input) || !$input) return array();
818
-
819
- $pairs = explode('&', $input);
820
-
821
- $parsed_parameters = array();
822
- foreach ($pairs as $pair) {
823
- $split = explode('=', $pair, 2);
824
- $parameter = OAuthUtil::urldecode_rfc3986($split[0]);
825
- $value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : '';
826
-
827
- if (isset($parsed_parameters[$parameter])) {
828
- // We have already recieved parameter(s) with this name, so add to the list
829
- // of parameters with this name
830
-
831
- if (is_scalar($parsed_parameters[$parameter])) {
832
- // This is the first duplicate, so transform scalar (string) into an array
833
- // so we can add the duplicates
834
- $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
835
- }
836
-
837
- $parsed_parameters[$parameter][] = $value;
838
- } else {
839
- $parsed_parameters[$parameter] = $value;
840
- }
841
- }
842
- return $parsed_parameters;
843
- }
844
-
845
- public static function build_http_query($params) {
846
- if (!$params) return '';
847
-
848
- // Urlencode both keys and values
849
- $keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
850
- $values = OAuthUtil::urlencode_rfc3986(array_values($params));
851
- $params = array_combine($keys, $values);
852
-
853
- // Parameters are sorted by name, using lexicographical byte value ordering.
854
- // Ref: Spec: 9.1.1 (1)
855
- uksort($params, 'strcmp');
856
-
857
- $pairs = array();
858
- foreach ($params as $parameter => $value) {
859
- if (is_array($value)) {
860
- // If two or more parameters share the same name, they are sorted by their value
861
- // Ref: Spec: 9.1.1 (1)
862
- natsort($value);
863
- foreach ($value as $duplicate_value) {
864
- $pairs[] = $parameter . '=' . $duplicate_value;
865
- }
866
- } else {
867
- $pairs[] = $parameter . '=' . $value;
868
- }
869
- }
870
- // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
871
- // Each name-value pair is separated by an '&' character (ASCII code 38)
872
- return implode('&', $pairs);
873
- }
874
- }
1
+ <?php
2
+ // vim: foldmethod=marker
3
+
4
+ /* Generic exception class
5
+ */
6
+ if (!class_exists('OAuthException')) {
7
+ class OAuthException extends Exception {
8
+ // pass
9
+ }
10
+ }
11
+
12
+ class OAuthConsumer {
13
+ public $key;
14
+ public $secret;
15
+
16
+ function __construct($key, $secret, $callback_url=NULL) {
17
+ $this->key = $key;
18
+ $this->secret = $secret;
19
+ $this->callback_url = $callback_url;
20
+ }
21
+
22
+ function __toString() {
23
+ return "OAuthConsumer[key=$this->key,secret=$this->secret]";
24
+ }
25
+ }
26
+
27
+ class OAuthToken {
28
+ // access tokens and request tokens
29
+ public $key;
30
+ public $secret;
31
+
32
+ /**
33
+ * key = the token
34
+ * secret = the token secret
35
+ */
36
+ function __construct($key, $secret) {
37
+ $this->key = $key;
38
+ $this->secret = $secret;
39
+ }
40
+
41
+ /**
42
+ * generates the basic string serialization of a token that a server
43
+ * would respond to request_token and access_token calls with
44
+ */
45
+ function to_string() {
46
+ return "oauth_token=" .
47
+ OAuthUtil::urlencode_rfc3986($this->key) .
48
+ "&oauth_token_secret=" .
49
+ OAuthUtil::urlencode_rfc3986($this->secret);
50
+ }
51
+
52
+ function __toString() {
53
+ return $this->to_string();
54
+ }
55
+ }
56
+
57
+ /**
58
+ * A class for implementing a Signature Method
59
+ * See section 9 ("Signing Requests") in the spec
60
+ */
61
+ abstract class OAuthSignatureMethod {
62
+ /**
63
+ * Needs to return the name of the Signature Method (ie HMAC-SHA1)
64
+ * @return string
65
+ */
66
+ abstract public function get_name();
67
+
68
+ /**
69
+ * Build up the signature
70
+ * NOTE: The output of this function MUST NOT be urlencoded.
71
+ * the encoding is handled in OAuthRequest when the final
72
+ * request is serialized
73
+ * @param OAuthRequest $request
74
+ * @param OAuthConsumer $consumer
75
+ * @param OAuthToken $token
76
+ * @return string
77
+ */
78
+ abstract public function build_signature($request, $consumer, $token);
79
+
80
+ /**
81
+ * Verifies that a given signature is correct
82
+ * @param OAuthRequest $request
83
+ * @param OAuthConsumer $consumer
84
+ * @param OAuthToken $token
85
+ * @param string $signature
86
+ * @return bool
87
+ */
88
+ public function check_signature($request, $consumer, $token, $signature) {
89
+ $built = $this->build_signature($request, $consumer, $token);
90
+ return $built == $signature;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104]
96
+ * where the Signature Base String is the text and the key is the concatenated values (each first
97
+ * encoded per Parameter Encoding) of the Consumer Secret and Token Secret, separated by an '&'
98
+ * character (ASCII code 38) even if empty.
99
+ * - Chapter 9.2 ("HMAC-SHA1")
100
+ */
101
+ class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {
102
+ function get_name() {
103
+ return "HMAC-SHA1";
104
+ }
105
+
106
+ public function build_signature($request, $consumer, $token) {
107
+ $base_string = $request->get_signature_base_string();
108
+ $request->base_string = $base_string;
109
+
110
+ $key_parts = array(
111
+ $consumer->secret,
112
+ ($token) ? $token->secret : ""
113
+ );
114
+
115
+ $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
116
+ $key = implode('&', $key_parts);
117
+
118
+ return base64_encode(hash_hmac('sha1', $base_string, $key, true));
119
+ }
120
+ }
121
+
122
+ /**
123
+ * The PLAINTEXT method does not provide any security protection and SHOULD only be used
124
+ * over a secure channel such as HTTPS. It does not use the Signature Base String.
125
+ * - Chapter 9.4 ("PLAINTEXT")
126
+ */
127
+ class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {
128
+ public function get_name() {
129
+ return "PLAINTEXT";
130
+ }
131
+
132
+ /**
133
+ * oauth_signature is set to the concatenated encoded values of the Consumer Secret and
134
+ * Token Secret, separated by a '&' character (ASCII code 38), even if either secret is
135
+ * empty. The result MUST be encoded again.
136
+ * - Chapter 9.4.1 ("Generating Signatures")
137
+ *
138
+ * Please note that the second encoding MUST NOT happen in the SignatureMethod, as
139
+ * OAuthRequest handles this!
140
+ */
141
+ public function build_signature($request, $consumer, $token) {
142
+ $key_parts = array(
143
+ $consumer->secret,
144
+ ($token) ? $token->secret : ""
145
+ );
146
+
147
+ $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
148
+ $key = implode('&', $key_parts);
149
+ $request->base_string = $key;
150
+
151
+ return $key;
152
+ }
153
+ }
154
+
155
+ /**
156
+ * The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in
157
+ * [RFC3447] section 8.2 (more simply known as PKCS#1), using SHA-1 as the hash function for
158
+ * EMSA-PKCS1-v1_5. It is assumed that the Consumer has provided its RSA public key in a
159
+ * verified way to the Service Provider, in a manner which is beyond the scope of this
160
+ * specification.
161
+ * - Chapter 9.3 ("RSA-SHA1")
162
+ */
163
+ abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
164
+ public function get_name() {
165
+ return "RSA-SHA1";
166
+ }
167
+
168
+ // Up to the SP to implement this lookup of keys. Possible ideas are:
169
+ // (1) do a lookup in a table of trusted certs keyed off of consumer
170
+ // (2) fetch via http using a url provided by the requester
171
+ // (3) some sort of specific discovery code based on request
172
+ //
173
+ // Either way should return a string representation of the certificate
174
+ protected abstract function fetch_public_cert(&$request);
175
+
176
+ // Up to the SP to implement this lookup of keys. Possible ideas are:
177
+ // (1) do a lookup in a table of trusted certs keyed off of consumer
178
+ //
179
+ // Either way should return a string representation of the certificate
180
+ protected abstract function fetch_private_cert(&$request);
181
+
182
+ public function build_signature($request, $consumer, $token) {
183
+ $base_string = $request->get_signature_base_string();
184
+ $request->base_string = $base_string;
185
+
186
+ // Fetch the private key cert based on the request
187
+ $cert = $this->fetch_private_cert($request);
188
+
189
+ // Pull the private key ID from the certificate
190
+ $privatekeyid = openssl_get_privatekey($cert);
191
+
192
+ // Sign using the key
193
+ $ok = openssl_sign($base_string, $signature, $privatekeyid);
194
+
195
+ // Release the key resource
196
+ openssl_free_key($privatekeyid);
197
+
198
+ return base64_encode($signature);
199
+ }
200
+
201
+ public function check_signature($request, $consumer, $token, $signature) {
202
+ $decoded_sig = base64_decode($signature);
203
+
204
+ $base_string = $request->get_signature_base_string();
205
+
206
+ // Fetch the public key cert based on the request
207
+ $cert = $this->fetch_public_cert($request);
208
+
209
+ // Pull the public key ID from the certificate
210
+ $publickeyid = openssl_get_publickey($cert);
211
+
212
+ // Check the computed signature against the one passed in the query
213
+ $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
214
+
215
+ // Release the key resource
216
+ openssl_free_key($publickeyid);
217
+
218
+ return $ok == 1;
219
+ }
220
+ }
221
+
222
+ class OAuthRequest {
223
+ private $parameters;
224
+ private $http_method;
225
+ private $http_url;
226
+ // for debug purposes
227
+ public $base_string;
228
+ public static $version = '1.0';
229
+ public static $POST_INPUT = 'php://input';
230
+
231
+ function __construct($http_method, $http_url, $parameters=NULL) {
232
+ @$parameters or $parameters = array();
233
+ $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
234
+ $this->parameters = $parameters;
235
+ $this->http_method = $http_method;
236
+ $this->http_url = $http_url;
237
+ }
238
+
239
+
240
+ /**
241
+ * attempt to build up a request from what was passed to the server
242
+ */
243
+ public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
244
+ $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
245
+ ? 'http'
246
+ : 'https';
247
+ @$http_url or $http_url = $scheme .
248
+ '://' . $_SERVER['HTTP_HOST'] .
249
+ ':' .
250
+ $_SERVER['SERVER_PORT'] .
251
+ $_SERVER['REQUEST_URI'];
252
+ @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
253
+
254
+ // We weren't handed any parameters, so let's find the ones relevant to
255
+ // this request.
256
+ // If you run XML-RPC or similar you should use this to provide your own
257
+ // parsed parameter-list
258
+ if (!$parameters) {
259
+ // Find request headers
260
+ $request_headers = OAuthUtil::get_headers();
261
+
262
+ // Parse the query-string to find GET parameters
263
+ $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
264
+
265
+ // It's a POST request of the proper content-type, so parse POST
266
+ // parameters and add those overriding any duplicates from GET
267
+ if ($http_method == "POST"
268
+ && @strstr($request_headers["Content-Type"],
269
+ "application/x-www-form-urlencoded")
270
+ ) {
271
+ $post_data = OAuthUtil::parse_parameters(
272
+ file_get_contents(self::$POST_INPUT)
273
+ );
274
+ $parameters = array_merge($parameters, $post_data);
275
+ }
276
+
277
+ // We have a Authorization-header with OAuth data. Parse the header
278
+ // and add those overriding any duplicates from GET or POST
279
+ if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
280
+ $header_parameters = OAuthUtil::split_header(
281
+ $request_headers['Authorization']
282
+ );
283
+ $parameters = array_merge($parameters, $header_parameters);
284
+ }
285
+
286
+ }
287
+
288
+ return new OAuthRequest($http_method, $http_url, $parameters);
289
+ }
290
+
291
+ /**
292
+ * pretty much a helper function to set up the request
293
+ */
294
+ public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
295
+ @$parameters or $parameters = array();
296
+ $defaults = array("oauth_version" => OAuthRequest::$version,
297
+ "oauth_nonce" => OAuthRequest::generate_nonce(),
298
+ "oauth_timestamp" => OAuthRequest::generate_timestamp(),
299
+ "oauth_consumer_key" => $consumer->key);
300
+ if ($token)
301
+ $defaults['oauth_token'] = $token->key;
302
+
303
+ $parameters = array_merge($defaults, $parameters);
304
+
305
+ return new OAuthRequest($http_method, $http_url, $parameters);
306
+ }
307
+
308
+ public function set_parameter($name, $value, $allow_duplicates = true) {
309
+ if ($allow_duplicates && isset($this->parameters[$name])) {
310
+ // We have already added parameter(s) with this name, so add to the list
311
+ if (is_scalar($this->parameters[$name])) {
312
+ // This is the first duplicate, so transform scalar (string)
313
+ // into an array so we can add the duplicates
314
+ $this->parameters[$name] = array($this->parameters[$name]);
315
+ }
316
+
317
+ $this->parameters[$name][] = $value;
318
+ } else {
319
+ $this->parameters[$name] = $value;
320
+ }
321
+ }
322
+
323
+ public function get_parameter($name) {
324
+ return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
325
+ }
326
+
327
+ public function get_parameters() {
328
+ return $this->parameters;
329
+ }
330
+
331
+ public function unset_parameter($name) {
332
+ unset($this->parameters[$name]);
333
+ }
334
+
335
+ /**
336
+ * The request parameters, sorted and concatenated into a normalized string.
337
+ * @return string
338
+ */
339
+ public function get_signable_parameters() {
340
+ // Grab all parameters
341
+ $params = $this->parameters;
342
+
343
+ // Remove oauth_signature if present
344
+ // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
345
+ if (isset($params['oauth_signature'])) {
346
+ unset($params['oauth_signature']);
347
+ }
348
+
349
+ return OAuthUtil::build_http_query($params);
350
+ }
351
+
352
+ /**
353
+ * Returns the base string of this request
354
+ *
355
+ * The base string defined as the method, the url
356
+ * and the parameters (normalized), each urlencoded
357
+ * and the concated with &.
358
+ */
359
+ public function get_signature_base_string() {
360
+ $parts = array(
361
+ $this->get_normalized_http_method(),
362
+ $this->get_normalized_http_url(),
363
+ $this->get_signable_parameters()
364
+ );
365
+
366
+ $parts = OAuthUtil::urlencode_rfc3986($parts);
367
+
368
+ return implode('&', $parts);
369
+ }
370
+
371
+ /**
372
+ * just uppercases the http method
373
+ */
374
+ public function get_normalized_http_method() {
375
+ return strtoupper($this->http_method);
376
+ }
377
+
378
+ /**
379
+ * parses the url and rebuilds it to be
380
+ * scheme://host/path
381
+ */
382
+ public function get_normalized_http_url() {
383
+ $parts = parse_url($this->http_url);
384
+
385
+ $port = @$parts['port'];
386
+ $scheme = $parts['scheme'];
387
+ $host = $parts['host'];
388
+ $path = @$parts['path'];
389
+
390
+ $port or $port = ($scheme == 'https') ? '443' : '80';
391
+
392
+ if (($scheme == 'https' && $port != '443')
393
+ || ($scheme == 'http' && $port != '80')) {
394
+ $host = "$host:$port";
395
+ }
396
+ return "$scheme://$host$path";
397
+ }
398
+
399
+ /**
400
+ * builds a url usable for a GET request
401
+ */
402
+ public function to_url() {
403
+ $post_data = $this->to_postdata();
404
+ $out = $this->get_normalized_http_url();
405
+ if ($post_data) {
406
+ $out .= '?'.$post_data;
407
+ }
408
+ return $out;
409
+ }
410
+
411
+ /**
412
+ * builds the data one would send in a POST request
413
+ */
414
+ public function to_postdata() {
415
+ return OAuthUtil::build_http_query($this->parameters);
416
+ }
417
+
418
+ /**
419
+ * builds the Authorization: header
420
+ */
421
+ public function to_header($realm=null) {
422
+ $first = true;
423
+ if($realm) {
424
+ $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
425
+ $first = false;
426
+ } else
427
+ $out = 'Authorization: OAuth';
428
+
429
+ $total = array();
430
+ foreach ($this->parameters as $k => $v) {
431
+ if (substr($k, 0, 5) != "oauth") continue;
432
+ if (is_array($v)) {
433
+ throw new OAuthException('Arrays not supported in headers');
434
+ }
435
+ $out .= ($first) ? ' ' : ',';
436
+ $out .= OAuthUtil::urlencode_rfc3986($k) .
437
+ '="' .
438
+ OAuthUtil::urlencode_rfc3986($v) .
439
+ '"';
440
+ $first = false;
441
+ }
442
+ return $out;
443
+ }
444
+
445
+ public function __toString() {
446
+ return $this->to_url();
447
+ }
448
+
449
+
450
+ public function sign_request($signature_method, $consumer, $token) {
451
+ $this->set_parameter(
452
+ "oauth_signature_method",
453
+ $signature_method->get_name(),
454
+ false
455
+ );
456
+ $signature = $this->build_signature($signature_method, $consumer, $token);
457
+ $this->set_parameter("oauth_signature", $signature, false);
458
+ }
459
+
460
+ public function build_signature($signature_method, $consumer, $token) {
461
+ $signature = $signature_method->build_signature($this, $consumer, $token);
462
+ return $signature;
463
+ }
464
+
465
+ /**
466
+ * util function: current timestamp
467
+ */
468
+ private static function generate_timestamp() {
469
+ return time();
470
+ }
471
+
472
+ /**
473
+ * util function: current nonce
474
+ */
475
+ private static function generate_nonce() {
476
+ $mt = microtime();
477
+ $rand = mt_rand();
478
+
479
+ return md5($mt . $rand); // md5s look nicer than numbers
480
+ }
481
+ }
482
+
483
+ class OAuthServer {
484
+ protected $timestamp_threshold = 300; // in seconds, five minutes
485
+ protected $version = '1.0'; // hi blaine
486
+ protected $signature_methods = array();
487
+
488
+ protected $data_store;
489
+
490
+ function __construct($data_store) {
491
+ $this->data_store = $data_store;
492
+ }
493
+
494
+ public function add_signature_method($signature_method) {
495
+ $this->signature_methods[$signature_method->get_name()] =
496
+ $signature_method;
497
+ }
498
+
499
+ // high level functions
500
+
501
+ /**
502
+ * process a request_token request
503
+ * returns the request token on success
504
+ */
505
+ public function fetch_request_token(&$request) {
506
+ $this->get_version($request);
507
+
508
+ $consumer = $this->get_consumer($request);
509
+
510
+ // no token required for the initial token request
511
+ $token = NULL;
512
+
513
+ $this->check_signature($request, $consumer, $token);
514
+
515
+ // Rev A change
516
+ $callback = $request->get_parameter('oauth_callback');
517
+ $new_token = $this->data_store->new_request_token($consumer, $callback);
518
+
519
+ return $new_token;
520
+ }
521
+
522
+ /**
523
+ * process an access_token request
524
+ * returns the access token on success
525
+ */
526
+ public function fetch_access_token(&$request) {
527
+ $this->get_version($request);
528
+
529
+ $consumer = $this->get_consumer($request);
530
+
531
+ // requires authorized request token
532
+ $token = $this->get_token($request, $consumer, "request");
533
+
534
+ $this->check_signature($request, $consumer, $token);
535
+
536
+ // Rev A change
537
+ $verifier = $request->get_parameter('oauth_verifier');
538
+ $new_token = $this->data_store->new_access_token($token, $consumer, $verifier);
539
+
540
+ return $new_token;
541
+ }
542
+
543
+ /**
544
+ * verify an api call, checks all the parameters
545
+ */
546
+ public function verify_request(&$request) {
547
+ $this->get_version($request);
548
+ $consumer = $this->get_consumer($request);
549
+ $token = $this->get_token($request, $consumer, "access");
550
+ $this->check_signature($request, $consumer, $token);
551
+ return array($consumer, $token);
552
+ }
553
+
554
+ // Internals from here
555
+ /**
556
+ * version 1
557
+ */
558
+ private function get_version(&$request) {
559
+ $version = $request->get_parameter("oauth_version");
560
+ if (!$version) {
561
+ // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present.
562
+ // Chapter 7.0 ("Accessing Protected Ressources")
563
+ $version = '1.0';
564
+ }
565
+ if ($version !== $this->version) {
566
+ throw new OAuthException("OAuth version '$version' not supported");
567
+ }
568
+ return $version;
569
+ }
570
+
571
+ /**
572
+ * figure out the signature with some defaults
573
+ */
574
+ private function get_signature_method(&$request) {
575
+ $signature_method =
576
+ @$request->get_parameter("oauth_signature_method");
577
+
578
+ if (!$signature_method) {
579
+ // According to chapter 7 ("Accessing Protected Ressources") the signature-method
580
+ // parameter is required, and we can't just fallback to PLAINTEXT
581
+ throw new OAuthException('No signature method parameter. This parameter is required');
582
+ }
583
+
584
+ if (!in_array($signature_method,
585
+ array_keys($this->signature_methods))) {
586
+ throw new OAuthException(
587
+ "Signature method '$signature_method' not supported " .
588
+ "try one of the following: " .
589
+ implode(", ", array_keys($this->signature_methods))
590
+ );
591
+ }
592
+ return $this->signature_methods[$signature_method];
593
+ }
594
+
595
+ /**
596
+ * try to find the consumer for the provided request's consumer key
597
+ */
598
+ private function get_consumer(&$request) {
599
+ $consumer_key = @$request->get_parameter("oauth_consumer_key");
600
+ if (!$consumer_key) {
601
+ throw new OAuthException("Invalid consumer key");
602
+ }
603
+
604
+ $consumer = $this->data_store->lookup_consumer($consumer_key);
605
+ if (!$consumer) {
606
+ throw new OAuthException("Invalid consumer");
607
+ }
608
+
609
+ return $consumer;
610
+ }
611
+
612
+ /**
613
+ * try to find the token for the provided request's token key
614
+ */
615
+ private function get_token(&$request, $consumer, $token_type="access") {
616
+ $token_field = @$request->get_parameter('oauth_token');
617
+ $token = $this->data_store->lookup_token(
618
+ $consumer, $token_type, $token_field
619
+ );
620
+ if (!$token) {
621
+ throw new OAuthException("Invalid $token_type token: $token_field");
622
+ }
623
+ return $token;
624
+ }
625
+
626
+ /**
627
+ * all-in-one function to check the signature on a request
628
+ * should guess the signature method appropriately
629
+ */
630
+ private function check_signature(&$request, $consumer, $token) {
631
+ // this should probably be in a different method
632
+ $timestamp = @$request->get_parameter('oauth_timestamp');
633
+ $nonce = @$request->get_parameter('oauth_nonce');
634
+
635
+ $this->check_timestamp($timestamp);
636
+ $this->check_nonce($consumer, $token, $nonce, $timestamp);
637
+
638
+ $signature_method = $this->get_signature_method($request);
639
+
640
+ $signature = $request->get_parameter('oauth_signature');
641
+ $valid_sig = $signature_method->check_signature(
642
+ $request,
643
+ $consumer,
644
+ $token,
645
+ $signature
646
+ );
647
+
648
+ if (!$valid_sig) {
649
+ throw new OAuthException("Invalid signature");
650
+ }
651
+ }
652
+
653
+ /**
654
+ * check that the timestamp is new enough
655
+ */
656
+ private function check_timestamp($timestamp) {
657
+ if( ! $timestamp )
658
+ throw new OAuthException(
659
+ 'Missing timestamp parameter. The parameter is required'
660
+ );
661
+
662
+ // verify that timestamp is recentish
663
+ $now = time();
664
+ if (abs($now - $timestamp) > $this->timestamp_threshold) {
665
+ throw new OAuthException(
666
+ "Expired timestamp, yours $timestamp, ours $now"
667
+ );
668
+ }
669
+ }
670
+
671
+ /**
672
+ * check that the nonce is not repeated
673
+ */
674
+ private function check_nonce($consumer, $token, $nonce, $timestamp) {
675
+ if( ! $nonce )
676
+ throw new OAuthException(
677
+ 'Missing nonce parameter. The parameter is required'
678
+ );
679
+
680
+ // verify that the nonce is uniqueish
681
+ $found = $this->data_store->lookup_nonce(
682
+ $consumer,
683
+ $token,
684
+ $nonce,
685
+ $timestamp
686
+ );
687
+ if ($found) {
688
+ throw new OAuthException("Nonce already used: $nonce");
689
+ }
690
+ }
691
+
692
+ }
693
+
694
+ class OAuthDataStore {
695
+ function lookup_consumer($consumer_key) {
696
+ // implement me
697
+ }
698
+
699
+ function lookup_token($consumer, $token_type, $token) {
700
+ // implement me
701
+ }
702
+
703
+ function lookup_nonce($consumer, $token, $nonce, $timestamp) {
704
+ // implement me
705
+ }
706
+
707
+ function new_request_token($consumer, $callback = null) {
708
+ // return a new token attached to this consumer
709
+ }
710
+
711
+ function new_access_token($token, $consumer, $verifier = null) {
712
+ // return a new access token attached to this consumer
713
+ // for the user associated with this token if the request token
714
+ // is authorized
715
+ // should also invalidate the request token
716
+ }
717
+
718
+ }
719
+
720
+ class OAuthUtil {
721
+ public static function urlencode_rfc3986($input) {
722
+ if (is_array($input)) {
723
+ return array_map(array('OAuthUtil', 'urlencode_rfc3986'), $input);
724
+ } else if (is_scalar($input)) {
725
+ return str_replace(
726
+ '+',
727
+ ' ',
728
+ str_replace('%7E', '~', rawurlencode($input))
729
+ );
730
+ } else {
731
+ return '';
732
+ }
733
+ }
734
+
735
+
736
+ // This decode function isn't taking into consideration the above
737
+ // modifications to the encoding process. However, this method doesn't
738
+ // seem to be used anywhere so leaving it as is.
739
+ public static function urldecode_rfc3986($string) {
740
+ return urldecode($string);
741
+ }
742
+
743
+ // Utility function for turning the Authorization: header into
744
+ // parameters, has to do some unescaping
745
+ // Can filter out any non-oauth parameters if needed (default behaviour)
746
+ public static function split_header($header, $only_allow_oauth_parameters = true) {
747
+ $pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
748
+ $offset = 0;
749
+ $params = array();
750
+ while (preg_match($pattern, $header, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
751
+ $match = $matches[0];
752
+ $header_name = $matches[2][0];
753
+ $header_content = (isset($matches[5])) ? $matches[5][0] : $matches[4][0];
754
+ if (preg_match('/^oauth_/', $header_name) || !$only_allow_oauth_parameters) {
755
+ $params[$header_name] = OAuthUtil::urldecode_rfc3986($header_content);
756
+ }
757
+ $offset = $match[1] + strlen($match[0]);
758
+ }
759
+
760
+ if (isset($params['realm'])) {
761
+ unset($params['realm']);
762
+ }
763
+
764
+ return $params;
765
+ }
766
+
767
+ // helper to try to sort out headers for people who aren't running apache
768
+ public static function get_headers() {
769
+ if (function_exists('apache_request_headers')) {
770
+ // we need this to get the actual Authorization: header
771
+ // because apache tends to tell us it doesn't exist
772
+ $headers = apache_request_headers();
773
+
774
+ // sanitize the output of apache_request_headers because
775
+ // we always want the keys to be Cased-Like-This and arh()
776
+ // returns the headers in the same case as they are in the
777
+ // request
778
+ $out = array();
779
+ foreach( $headers AS $key => $value ) {
780
+ $key = str_replace(
781
+ " ",
782
+ "-",
783
+ ucwords(strtolower(str_replace("-", " ", $key)))
784
+ );
785
+ $out[$key] = $value;
786
+ }
787
+ } else {
788
+ // otherwise we don't have apache and are just going to have to hope
789
+ // that $_SERVER actually contains what we need
790
+ $out = array();
791
+ if( isset($_SERVER['CONTENT_TYPE']) )
792
+ $out['Content-Type'] = $_SERVER['CONTENT_TYPE'];
793
+ if( isset($_ENV['CONTENT_TYPE']) )
794
+ $out['Content-Type'] = $_ENV['CONTENT_TYPE'];
795
+
796
+ foreach ($_SERVER as $key => $value) {
797
+ if (substr($key, 0, 5) == "HTTP_") {
798
+ // this is chaos, basically it is just there to capitalize the first
799
+ // letter of every word that is not an initial HTTP and strip HTTP
800
+ // code from przemek
801
+ $key = str_replace(
802
+ " ",
803
+ "-",
804
+ ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
805
+ );
806
+ $out[$key] = $value;
807
+ }
808
+ }
809
+ }
810
+ return $out;
811
+ }
812
+
813
+ // This function takes a input like a=b&a=c&d=e and returns the parsed
814
+ // parameters like this
815
+ // array('a' => array('b','c'), 'd' => 'e')
816
+ public static function parse_parameters( $input ) {
817
+ if (!isset($input) || !$input) return array();
818
+
819
+ $pairs = explode('&', $input);
820
+
821
+ $parsed_parameters = array();
822
+ foreach ($pairs as $pair) {
823
+ $split = explode('=', $pair, 2);
824
+ $parameter = OAuthUtil::urldecode_rfc3986($split[0]);
825
+ $value = isset($split[1]) ? OAuthUtil::urldecode_rfc3986($split[1]) : '';
826
+
827
+ if (isset($parsed_parameters[$parameter])) {
828
+ // We have already recieved parameter(s) with this name, so add to the list
829
+ // of parameters with this name
830
+
831
+ if (is_scalar($parsed_parameters[$parameter])) {
832
+ // This is the first duplicate, so transform scalar (string) into an array
833
+ // so we can add the duplicates
834
+ $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
835
+ }
836
+
837
+ $parsed_parameters[$parameter][] = $value;
838
+ } else {
839
+ $parsed_parameters[$parameter] = $value;
840
+ }
841
+ }
842
+ return $parsed_parameters;
843
+ }
844
+
845
+ public static function build_http_query($params) {
846
+ if (!$params) return '';
847
+
848
+ // Urlencode both keys and values
849
+ $keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
850
+ $values = OAuthUtil::urlencode_rfc3986(array_values($params));
851
+ $params = array_combine($keys, $values);
852
+
853
+ // Parameters are sorted by name, using lexicographical byte value ordering.
854
+ // Ref: Spec: 9.1.1 (1)
855
+ uksort($params, 'strcmp');
856
+
857
+ $pairs = array();
858
+ foreach ($params as $parameter => $value) {
859
+ if (is_array($value)) {
860
+ // If two or more parameters share the same name, they are sorted by their value
861
+ // Ref: Spec: 9.1.1 (1)
862
+ natsort($value);
863
+ foreach ($value as $duplicate_value) {
864
+ $pairs[] = $parameter . '=' . $duplicate_value;
865
+ }
866
+ } else {
867
+ $pairs[] = $parameter . '=' . $value;
868
+ }
869
+ }
870
+ // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
871
+ // Each name-value pair is separated by an '&' character (ASCII code 38)
872
+ return implode('&', $pairs);
873
+ }
874
+ }
{oauth → twitteroauth}/twitteroauth.php RENAMED
@@ -1,246 +1,241 @@
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
- * Modified for compatibility with other plugins which could provide OAuthConsumer by Liam Gladdy, 2014.
8
- */
9
-
10
- /* Load OAuth lib. You can find it at http://oauth.net */
11
- if (!class_exists('OAuthConsumer')) {
12
- require_once('OAuth.php');
13
- } else {
14
- define('THR_USING_EXISTING_LIBRARY_OAUTH',true);
15
- }
16
-
17
- /**
18
- * Twitter OAuth class
19
- */
20
- class TwitterOAuth {
21
- /* Contains the last HTTP status code returned. */
22
- public $http_code;
23
- /* Contains the last API call. */
24
- public $url;
25
- /* Set up the API root URL. */
26
- public $host = "https://api.twitter.com/1.1/";
27
- /* Set timeout default. */
28
- public $timeout = 3;
29
- /* Set connect timeout. */
30
- public $connecttimeout = 2;
31
- /* Verify SSL Cert. */
32
- public $ssl_verifypeer = FALSE;
33
- /* Respons format. */
34
- public $format = 'json';
35
- /* Decode returned json data. */
36
- public $decode_json = TRUE;
37
- /* Contains the last HTTP headers returned. */
38
- public $http_info;
39
- /* Set the useragnet. */
40
- public $useragent = 'Twitter Feed for Wordpress Developers 2.2.0';
41
- /* Immediately retry the API call if the response was not successful. */
42
- //public $retry = TRUE;
43
-
44
-
45
-
46
-
47
- /**
48
- * Set API URLS
49
- */
50
- function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
51
- function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
52
- function authorizeURL() { return 'https://api.twitter.com/oauth/authorize'; }
53
- function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
54
-
55
- /**
56
- * Debug helpers
57
- */
58
- function lastStatusCode() { return $this->http_status; }
59
- function lastAPICall() { return $this->last_api_call; }
60
-
61
- /**
62
- * construct TwitterOAuth object
63
- */
64
- function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
65
- $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
66
- $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
67
- if (!empty($oauth_token) && !empty($oauth_token_secret)) {
68
- $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
69
- } else {
70
- $this->token = NULL;
71
- }
72
- }
73
-
74
-
75
- /**
76
- * Get a request_token from Twitter
77
- *
78
- * @returns a key/value array containing oauth_token and oauth_token_secret
79
- */
80
- function getRequestToken($oauth_callback) {
81
- $parameters = array();
82
- $parameters['oauth_callback'] = $oauth_callback;
83
- $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
84
- $token = OAuthUtil::parse_parameters($request);
85
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
86
- return $token;
87
- }
88
-
89
- /**
90
- * Get the authorize URL
91
- *
92
- * @returns a string
93
- */
94
- function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
95
- if (is_array($token)) {
96
- $token = $token['oauth_token'];
97
- }
98
- if (empty($sign_in_with_twitter)) {
99
- return $this->authorizeURL() . "?oauth_token={$token}";
100
- } else {
101
- return $this->authenticateURL() . "?oauth_token={$token}";
102
- }
103
- }
104
-
105
- /**
106
- * Exchange request token and secret for an access token and
107
- * secret, to sign API calls.
108
- *
109
- * @returns array("oauth_token" => "the-access-token",
110
- * "oauth_token_secret" => "the-access-secret",
111
- * "user_id" => "9436992",
112
- * "screen_name" => "abraham")
113
- */
114
- function getAccessToken($oauth_verifier) {
115
- $parameters = array();
116
- $parameters['oauth_verifier'] = $oauth_verifier;
117
- $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
118
- $token = OAuthUtil::parse_parameters($request);
119
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
120
- return $token;
121
- }
122
-
123
- /**
124
- * One time exchange of username and password for access token and secret.
125
- *
126
- * @returns array("oauth_token" => "the-access-token",
127
- * "oauth_token_secret" => "the-access-secret",
128
- * "user_id" => "9436992",
129
- * "screen_name" => "abraham",
130
- * "x_auth_expires" => "0")
131
- */
132
- function getXAuthToken($username, $password) {
133
- $parameters = array();
134
- $parameters['x_auth_username'] = $username;
135
- $parameters['x_auth_password'] = $password;
136
- $parameters['x_auth_mode'] = 'client_auth';
137
- $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
138
- $token = OAuthUtil::parse_parameters($request);
139
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
140
- return $token;
141
- }
142
-
143
- /**
144
- * GET wrapper for oAuthRequest.
145
- */
146
- function get($url, $parameters = array()) {
147
- $response = $this->oAuthRequest($url, 'GET', $parameters);
148
- if ($this->format === 'json' && $this->decode_json) {
149
- return json_decode($response,true);
150
- }
151
- return $response;
152
- }
153
-
154
- /**
155
- * POST wrapper for oAuthRequest.
156
- */
157
- function post($url, $parameters = array()) {
158
- $response = $this->oAuthRequest($url, 'POST', $parameters);
159
- if ($this->format === 'json' && $this->decode_json) {
160
- return json_decode($response,true);
161
- }
162
- return $response;
163
- }
164
-
165
- /**
166
- * DELETE wrapper for oAuthReqeust.
167
- */
168
- function delete($url, $parameters = array()) {
169
- $response = $this->oAuthRequest($url, 'DELETE', $parameters);
170
- if ($this->format === 'json' && $this->decode_json) {
171
- return json_decode($response,true);
172
- }
173
- return $response;
174
- }
175
-
176
- /**
177
- * Format and sign an OAuth / API request
178
- */
179
- function oAuthRequest($url, $method, $parameters) {
180
- if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
181
- $url = "{$this->host}{$url}.{$this->format}";
182
- }
183
- $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
184
- $request->sign_request($this->sha1_method, $this->consumer, $this->token);
185
- switch ($method) {
186
- case 'GET':
187
- return $this->http($request->to_url(), 'GET');
188
- default:
189
- return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
190
- }
191
- }
192
-
193
- /**
194
- * Make an HTTP request
195
- *
196
- * @return API results
197
- */
198
- function http($url, $method, $postfields = NULL) {
199
- $this->http_info = array();
200
- $ci = curl_init();
201
- /* Curl settings */
202
- curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
203
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
204
- curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
205
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
206
- curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
207
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
208
- curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
209
- curl_setopt($ci, CURLOPT_HEADER, FALSE);
210
-
211
- switch ($method) {
212
- case 'POST':
213
- curl_setopt($ci, CURLOPT_POST, TRUE);
214
- if (!empty($postfields)) {
215
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
216
- }
217
- break;
218
- case 'DELETE':
219
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
220
- if (!empty($postfields)) {
221
- $url = "{$url}?{$postfields}";
222
- }
223
- }
224
-
225
- curl_setopt($ci, CURLOPT_URL, $url);
226
- $response = curl_exec($ci);
227
- $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
228
- $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
229
- $this->url = $url;
230
- curl_close ($ci);
231
- return $response;
232
- }
233
-
234
- /**
235
- * Get the header info to store.
236
- */
237
- function getHeader($ch, $header) {
238
- $i = strpos($header, ':');
239
- if (!empty($i)) {
240
- $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
241
- $value = trim(substr($header, $i + 2));
242
- $this->http_header[$key] = $value;
243
- }
244
- return strlen($header);
245
- }
246
- }
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 = 30;
24
+ /* Set connect timeout. */
25
+ public $connecttimeout = 30;
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 = 'TwitterOAuth v0.2.0-beta2';
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://api.twitter.com/oauth/authenticate'; }
47
+ function authorizeURL() { return 'https://api.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) {
76
+ $parameters = array();
77
+ $parameters['oauth_callback'] = $oauth_callback;
78
+ $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
79
+ $token = OAuthUtil::parse_parameters($request);
80
+ $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
81
+ return $token;
82
+ }
83
+
84
+ /**
85
+ * Get the authorize URL
86
+ *
87
+ * @returns a string
88
+ */
89
+ function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
90
+ if (is_array($token)) {
91
+ $token = $token['oauth_token'];
92
+ }
93
+ if (empty($sign_in_with_twitter)) {
94
+ return $this->authorizeURL() . "?oauth_token={$token}";
95
+ } else {
96
+ return $this->authenticateURL() . "?oauth_token={$token}";
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Exchange request token and secret for an access token and
102
+ * secret, to sign API calls.
103
+ *
104
+ * @returns array("oauth_token" => "the-access-token",
105
+ * "oauth_token_secret" => "the-access-secret",
106
+ * "user_id" => "9436992",
107
+ * "screen_name" => "abraham")
108
+ */
109
+ function getAccessToken($oauth_verifier) {
110
+ $parameters = array();
111
+ $parameters['oauth_verifier'] = $oauth_verifier;
112
+ $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
113
+ $token = OAuthUtil::parse_parameters($request);
114
+ $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
115
+ return $token;
116
+ }
117
+
118
+ /**
119
+ * One time exchange of username and password for access token and secret.
120
+ *
121
+ * @returns array("oauth_token" => "the-access-token",
122
+ * "oauth_token_secret" => "the-access-secret",
123
+ * "user_id" => "9436992",
124
+ * "screen_name" => "abraham",
125
+ * "x_auth_expires" => "0")
126
+ */
127
+ function getXAuthToken($username, $password) {
128
+ $parameters = array();
129
+ $parameters['x_auth_username'] = $username;
130
+ $parameters['x_auth_password'] = $password;
131
+ $parameters['x_auth_mode'] = 'client_auth';
132
+ $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
133
+ $token = OAuthUtil::parse_parameters($request);
134
+ $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
135
+ return $token;
136
+ }
137
+
138
+ /**
139
+ * GET wrapper for oAuthRequest.
140
+ */
141
+ function get($url, $parameters = array()) {
142
+ $response = $this->oAuthRequest($url, 'GET', $parameters);
143
+ if ($this->format === 'json' && $this->decode_json) {
144
+ return json_decode($response);
145
+ }
146
+ return $response;
147
+ }
148
+
149
+ /**
150
+ * POST wrapper for oAuthRequest.
151
+ */
152
+ function post($url, $parameters = array()) {
153
+ $response = $this->oAuthRequest($url, 'POST', $parameters);
154
+ if ($this->format === 'json' && $this->decode_json) {
155
+ return json_decode($response);
156
+ }
157
+ return $response;
158
+ }
159
+
160
+ /**
161
+ * DELETE wrapper for oAuthReqeust.
162
+ */
163
+ function delete($url, $parameters = array()) {
164
+ $response = $this->oAuthRequest($url, 'DELETE', $parameters);
165
+ if ($this->format === 'json' && $this->decode_json) {
166
+ return json_decode($response);
167
+ }
168
+ return $response;
169
+ }
170
+
171
+ /**
172
+ * Format and sign an OAuth / API request
173
+ */
174
+ function oAuthRequest($url, $method, $parameters) {
175
+ if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
176
+ $url = "{$this->host}{$url}.{$this->format}";
177
+ }
178
+ $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
179
+ $request->sign_request($this->sha1_method, $this->consumer, $this->token);
180
+ switch ($method) {
181
+ case 'GET':
182
+ return $this->http($request->to_url(), 'GET');
183
+ default:
184
+ return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Make an HTTP request
190
+ *
191
+ * @return API results
192
+ */
193
+ function http($url, $method, $postfields = NULL) {
194
+ $this->http_info = array();
195
+ $ci = curl_init();
196
+ /* Curl settings */
197
+ curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
198
+ curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
199
+ curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
200
+ curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
201
+ curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
202
+ curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
203
+ curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
204
+ curl_setopt($ci, CURLOPT_HEADER, FALSE);
205
+
206
+ switch ($method) {
207
+ case 'POST':
208
+ curl_setopt($ci, CURLOPT_POST, TRUE);
209
+ if (!empty($postfields)) {
210
+ curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
211
+ }
212
+ break;
213
+ case 'DELETE':
214
+ curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
215
+ if (!empty($postfields)) {
216
+ $url = "{$url}?{$postfields}";
217
+ }
218
+ }
219
+
220
+ curl_setopt($ci, CURLOPT_URL, $url);
221
+ $response = curl_exec($ci);
222
+ $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
223
+ $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
224
+ $this->url = $url;
225
+ curl_close ($ci);
226
+ return $response;
227
+ }
228
+
229
+ /**
230
+ * Get the header info to store.
231
+ */
232
+ function getHeader($ch, $header) {
233
+ $i = strpos($header, ':');
234
+ if (!empty($i)) {
235
+ $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
236
+ $value = trim(substr($header, $i + 2));
237
+ $this->http_header[$key] = $value;
238
+ }
239
+ return strlen($header);
240
+ }
241
+ }