-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
180 lines (142 loc) · 4.42 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
Stan Scates
blr | further
stan@sc8s.com
blrfurther.com
Basic OAuth and caching layer for Seaofclouds' tweet.js, designed
to introduce compatibility with Twitter's v1.1 API.
Version: 1.4
Created: 2013.02.20
https://github.com/seaofclouds/tweet
https://github.com/themattharris/tmhOAuth
*/
if(empty($_POST)) { die(); }
class ezTweet {
/*************************************** config ***************************************/
// Your Twitter App Consumer Key
private $consumer_key = 'WLDiByNERUAGCM35S1fcQ';
// Your Twitter App Consumer Secret
private $consumer_secret = 'TTTCMvkFGHfbjoW9eICl0WiBydAeFxf3KmgUk09a8';
// Your Twitter App Access Token
private $user_token = '93647799-nH3gdsKGBMfccWo7PDP8FGtEZ75KG0UnrhaxfKUav';
// Your Twitter App Access Token Secret
private $user_secret = 'WgjPIglQpOXTC9PXJKl5eMe0Nycr25zm6WcWUcw4npp6N';
// Path to tmhOAuth libraries
private $lib = './lib/';
// Enable caching
private $cache_enabled = true;
// Cache interval (minutes)
private $cache_interval = 0.5;
// Path to writable cache directory
private $cache_dir = './cache/';
// Enable debugging
private $debug = true;
/**************************************************************************************/
public function __construct() {
// Initialize paths and etc.
$this->pathify($this->cache_dir);
$this->pathify($this->lib);
$this->message = '';
// Set server-side debug params
if($this->debug === true) {
error_reporting(-1);
} else {
error_reporting(0);
}
}
public function fetch() {
echo json_encode(
array(
'response' => json_decode($this->getJSON(), true),
'message' => ($this->debug) ? $this->message : false
)
);
}
private function getJSON() {
if($this->cache_enabled === true) {
$CFID = $this->generateCFID();
$cache_file = $this->cache_dir.$CFID;
if(file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * intval($this->cache_interval)))) {
return file_get_contents($cache_file, FILE_USE_INCLUDE_PATH);
} else {
$JSONraw = $this->getTwitterJSON();
$JSON = $JSONraw['response'];
// Don't write a bad cache file if there was a CURL error
if($JSONraw['errno'] != 0) {
$this->consoleDebug($JSONraw['error']);
return $JSON;
}
if($this->debug === true) {
// Check for twitter-side errors
$pj = json_decode($JSON, true);
if(isset($pj['errors'])) {
foreach($pj['errors'] as $error) {
$message = 'Twitter Error: "'.$error['message'].'", Error Code #'.$error['code'];
$this->consoleDebug($message);
}
return false;
}
}
if(is_writable($this->cache_dir) && $JSONraw) {
if(file_put_contents($cache_file, $JSON, LOCK_EX) === false) {
$this->consoleDebug("Error writing cache file");
}
} else {
$this->consoleDebug("Cache directory is not writable");
}
return $JSON;
}
} else {
$JSONraw = $this->getTwitterJSON();
if($this->debug === true) {
// Check for CURL errors
if($JSONraw['errno'] != 0) {
$this->consoleDebug($JSONraw['error']);
}
// Check for twitter-side errors
$pj = json_decode($JSONraw['response'], true);
if(isset($pj['errors'])) {
foreach($pj['errors'] as $error) {
$message = 'Twitter Error: "'.$error['message'].'", Error Code #'.$error['code'];
$this->consoleDebug($message);
}
return false;
}
}
return $JSONraw['response'];
}
}
private function getTwitterJSON() {
require $this->lib.'tmhOAuth.php';
require $this->lib.'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'host' => $_POST['request']['host'],
'consumer_key' => $this->consumer_key,
'consumer_secret' => $this->consumer_secret,
'user_token' => $this->user_token,
'user_secret' => $this->user_secret,
'curl_ssl_verifypeer' => false
));
$url = $_POST['request']['url'];
$params = $_POST['request']['parameters'];
$tmhOAuth->request('GET', $tmhOAuth->url($url), $params);
return $tmhOAuth->response;
}
private function generateCFID() {
// The unique cached filename ID
return md5(serialize($_POST)).'.json';
}
private function pathify(&$path) {
// Ensures our user-specified paths are up to snuff
$path = realpath($path).'/';
}
private function consoleDebug($message) {
if($this->debug === true) {
$this->message .= 'tweet.js: '.$message."\n";
}
}
}
$ezTweet = new ezTweet;
$ezTweet->fetch();
?>