-
Notifications
You must be signed in to change notification settings - Fork 17
/
EZAppDotNet.php
235 lines (201 loc) · 7.74 KB
/
EZAppDotNet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* EZAppDotNet.php
* Class for easy web development
* https://github.com/jdolitsky/AppDotNetPHP
*
* This class does as much of the grunt work as possible in helping you to
* access the App.net API. In theory you don't need to know anything about
* oAuth, tokens, or all the ugly details of how it works, it should "just
* work".
*
* Note this class assumes you're running a web site, and you'll be
* accessing it via a web browser (it expects to be able to do things like
* cookies and sessions). If you're not using a web browser in your App.net
* application, or you want more fine grained control over what's being
* done for you, use the included AppDotNet class, which does much
* less automatically.
*/
// comment these two lines out in production
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'EZsettings.php';
require_once 'AppDotNet.php';
// comment this out if session is started elsewhere
session_start();
class EZAppDotNet extends AppDotNet {
private $_callbacks = array();
private $_autoShutdownStreams = array();
public function __construct($clientId=null,$clientSecret=null) {
global $app_clientId,$app_clientSecret;
// if client id wasn't passed, and it's in the settings.php file, use it from there
if (!$clientId && isset($app_clientId)) {
// if it's still the default, warn them
if ($app_clientId == 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') {
throw new AppDotNetException('You must change the values defined in EZsettings.php');
}
$clientId = $app_clientId;
$clientSecret = $app_clientSecret;
}
// call the parent with the variables we have
parent::__construct($clientId,$clientSecret);
// set up ez streaming
$this->registerStreamFunction(array($this,'streamEZCallback'));
// make sure we cleanup/destroy any streams when we exit
register_shutdown_function(array($this,'stopStreaming'));
}
public function getAuthUrl($redirectUri=null,$scope=null) {
global $app_redirectUri,$app_scope;
if (is_null($redirectUri)) {
$redirectUri = $app_redirectUri;
}
if (is_null($scope)) {
$scope = $app_scope;
}
return parent::getAuthUrl($redirectUri,$scope);
}
// user login
public function setSession($cookie=0,$callback=null) {
if (!isset($callback)) {
global $app_redirectUri;
$cb=$app_redirectUri;
} else {
$cb=$callback;
}
// try and set the token the original way (eg: if they're logging in)
$token = $this->getAccessToken($cb);
// if that didn't work, check to see if there's an existing token stored somewhere
if (!$token) {
$token = $this->getSession();
}
$_SESSION['AppDotNetPHPAccessToken']=$token;
// if they want to stay logged in via a cookie, set the cookie
if ($token && $cookie) {
$cookie_lifetime = time()+(60*60*24*7);
setcookie('AppDotNetPHPAccessToken',$token,$cookie_lifetime);
}
return $token;
}
// check if user is logged in
public function getSession() {
// first check for cookie
if (isset($_COOKIE['AppDotNetPHPAccessToken']) && $_COOKIE['AppDotNetPHPAccessToken'] != 'expired') {
$this->setAccessToken($_COOKIE['AppDotNetPHPAccessToken']);
return $_COOKIE['AppDotNetPHPAccessToken'];
}
// else check the session for the token (from a previous page load)
else if (isset($_SESSION['AppDotNetPHPAccessToken'])) {
$this->setAccessToken($_SESSION['AppDotNetPHPAccessToken']);
return $_SESSION['AppDotNetPHPAccessToken'];
}
return false;
}
// log the user out
public function deleteSession() {
// clear the session
unset($_SESSION['AppDotNetPHPAccessToken']);
// unset the cookie
setcookie('AppDotNetPHPAccessToken', null, 1);
// clear the access token
$this->setAccessToken(null);
// done!
return true;
}
/**
* Registers a callback function to be called whenever an event of a certain
* type is received from the app.net streaming API. Your function will recieve
* a PHP associative array containing an app.net object. You must register at
* least one callback function before starting to stream (otherwise your data
* would simply be discarded). You can register multiple event types and even
* multiple functions per event (just call this method as many times as needed).
* If you register multiple functions for a single event, each will be called
* every time an event of that type is received.
*
* Note you should not be doing any significant processing in your callback
* functions. Doing so could cause your scripts to fall behind the stream and
* risk getting disconnected. Ideally your callback functions should simply
* drop the data into a file or database to be collected and processed by
* another program.
* @param string $type The type of even your callback would like to recieve.
* At time of writing the possible options are 'post', 'star', 'user_follow'.
*/
public function registerStreamCallback($type,$callback) {
switch ($type) {
case 'post':
case 'star':
case 'user_follow':
if (!array_key_exists($type,$this->_callbacks)) {
$this->_callbacks[$type] = array();
}
$this->_callbacks[$type][] = $callback;
return true;
break;
default:
throw new AppDotNetException('Unknown callback type: '.$type);
}
}
/**
* This is the easy way to start streaming. Register some callback functions
* using registerCallback(), then call startStreaming(). Every time the stream
* gets sent a type of object you have a callback for, your callback function(s)
* will be called with the proper data. When your script exits the streams will
* be cleaned up (deleted).
*
* Do not use this method if you want to spread out streams across multiple
* processes or multiple servers, since the first script that exits/crashes will
* delete the streams for everyone else. Instead use createStream() and openStream().
* @return true
* @see AppDotNetStream::stopStreaming()
* @see AppDotNetStream::createStream()
* @see AppDotNetStream::openStream()
*/
public function startStreaming() {
// only listen for object types that we have registered callbacks for
if (!$this->_callbacks) {
throw new AppDotNetException('You must register at least one callback function before calling startStreaming');
}
// if there's already a stream running, don't allow another
if ($this->_currentStream) {
throw new AppDotNetException('There is already a stream being consumed, only one stream can be consumed per AppDotNetStream instance');
}
$stream = $this->createStream(array_keys($this->_callbacks));
// when the script exits, delete this stream (if it's still around)
$this->_autoShutdownStreams[] = $response['id'];
// start consuming
$this->openStream($response['id']);
return true;
}
/**
* This is the easy way to stop streaming and cleans up the no longer needed stream.
* This method will be called automatically if you started streaming using
* startStreaming().
*
* Do not use this method if you want to spread out streams across multiple
* processes or multiple servers, since it will delete the streams for everyone
* else. Instead use closeStream().
* @return true
* @see AppDotNetStream::startStreaming()
* @see AppDotNetStream::deleteStream()
* @see AppDotNetStream::closeStream()
*/
public function stopStreaming() {
$this->closeStream();
// delete any auto streams
foreach ($this->_autoShutdownStreams as $streamId) {
$this->deleteStream($streamId);
}
return true;
}
/**
* Internal function used to make your streaming easier. I hope.
*/
protected function streamEZCallback($type,$data) {
// if there are defined callbacks for this object type, then...
if (array_key_exists($type,$this->_callbacks)) {
// loop through the callbacks notifying each one in turn
foreach ($this->_callbacks[$type] as $callback) {
call_user_func($callback,$data);
}
}
}
}