-
Notifications
You must be signed in to change notification settings - Fork 3
/
fbWebUserBehavior.php
281 lines (245 loc) · 7.22 KB
/
fbWebUserBehavior.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
*
*
* @author Shiki
*/
class fbWebUserBehavior extends CBehavior
{
/**
*
* @var Facebook
*/
private $_client;
private $_accessToken;
public static function behaviorName()
{
return 'fbWebUser';
}
/**
* @return Facebook
*/
private function getClient()
{
if (!$this->_client)
$this->_client = Yii::app()->fbApplication->createClient();
return $this->_client;
}
/**
*
* @return boolean
*/
public function hasSession()
{
$client = $this->getClient();
return (bool)$client->getSession();
}
public function getUserId()
{
$client = $this->getClient();
return $client->getUser();
}
/**
* Checks whether this user is an Admin of the current App
*/
public function isAdmin()
{
$signedRequest = $this->getClient()->getSignedRequest();
return (isset($signedRequest['page']) && $signedRequest['page']['admin']);
}
/**
* Checks whether this user has liked the Custom Fan Page
* where the current App is embedded
*/
public function hasLiked()
{
$signedRequest = $this->getClient()->getSignedRequest();
return (isset($signedRequest['page']) && $signedRequest['page']['liked']);
}
/**
* Checks if user has authorized the current App
*/
public function hasAuthorized()
{
$signedRequest = $this->getClient()->getSignedRequest();
return (!empty($signedRequest['oauth_token']));
}
public function getUserInfo()
{
$client = $this->getClient();
try {
return $client->api('/me');
} catch (Exception $e) {
Yii::log($e->__toString(), CLogger::LEVEL_ERROR, __CLASS__);
return null;
}
}
/**
* This retrieves the permissions of the specified user.
*
* @see http://developers.facebook.com/docs/authentication/permissions
*
* @param array $permissions
* @param string $uid
* @return array
*/
public function checkPermissions($permissions, $userId = null)
{
if (!is_array($permissions))
$permissions = array($permissions);
$query = "SELECT %s FROM permissions WHERE uid = %s LIMIT 1;";
$client = $this->getClient();
if ($userId == null && $this->hasSession())
$userId = $client->getUser();
try {
$res = $client->api(array(
'method' => 'fql.query',
'query' => sprintf($query, implode(',', $permissions), $userId),
));
if ($res && isset($res[0]) && is_array($res[0]))
return $res[0];
} catch (FacebookApiException $e) {
Yii::log($e, CLogger::LEVEL_ERROR, __CLASS__);
}
return array();
}
/**
* @return Array
*/
public function getFriends()
{
$client = $this->getClient();
$_accessToken = isset($this->_accessToken) ? $this->_accessToken: $client->getAccessToken();
try {
$res = $client->api('/me/friends?access_token='. $_accessToken);
if ($res && isset($res['data']) && is_array($res['data']))
return $res['data'];
} catch (FacebookApiException $e) {
Yii::log($e, CLogger::LEVEL_ERROR, __CLASS__);
}
return array();
}
/**
* @param String $accessToken
* @return fbWebUserBehavior
*/
public function setAccessToken($accessToken)
{
if (!empty($accessToken))
$this->_accessToken = $accessToken;
return $this;
}
/**
* This checks if the user is a Fan of SmartComm
* @return Boolean
*/
public function isPageFan($fbUserId = null)
{
$client = $this->getClient();
if (empty($fbUserId)) {
$fbUserId = $client->getUser();
if (empty($fbUserId))
return false;
}
$query = 'SELECT uid, page_id FROM page_fan WHERE uid=%s AND page_id=%s';
$query = sprintf($query, $fbUserId, Yii::app()->params['smartFanPageId']);
try {
$res = $client->api(array(
'method' => 'fql.query',
'query' => $query,
));
if ($res && is_array($res) && count($res) > 0)
return true;
} catch (FacebookApiException $e) {
Yii::log($e, CLogger::LEVEL_ERROR, __CLASS__);
}
return false;
}
/**
* Publish to profile feed as a status message or a link.
*
* Note: Action links are not yet supported in the Graph API.
* See: http://forum.developers.facebook.net/viewtopic.php?pid=223444
*
* $message param receives (as Array):
* - message : The message
* - picture : If available, a link to the picture included with this post
* - link : The link attached to this post
* - name : The name of the link
* - caption : The caption of the link (appears beneath the link name)
* - description: A description of the link (appears beneath the link caption)
*
* @see http://developers.facebook.com/docs/reference/api/post
*
* @param String $uid User Id
* @param String $accessToken Stored access_token value
* @param Mixed $message Array or String
* @return Mixed Array on success containing the Id of the post.
* False otherwise.
*/
public function streamPublish($uid, $accessToken, $message)
{
if (empty($uid) || empty($accessToken) || empty($message))
return false;
if (!is_array($message))
$message = array('message' => strval($message));
$message = array_merge(array(
'message' => null,
'picture' => null,
'link' => null,
'name' => null,
'caption' => null,
'description' => null,
), $message);
$arguments = array('access_token' => $accessToken);
foreach($message as $key => $value) {
if (isset($value))
$arguments[$key] = $value;
}
$fbClient = $this->getClient();
try {
return $fbClient->api(sprintf('/%s/feed/', $uid), 'POST', $arguments);
} catch (FacebookApiException $e) {
Yii::log('Failed to publish to stream: ' . CJSON::encode($arguments) . ' :: ' . $e->__toString(), CLogger::LEVEL_ERROR, __CLASS__);
return false;
}
}
/**
* Currently, there's no way to retrieve a list of Page Fans by just
* filtering the page_id in the page_fan FQL table. This is because
* page_id field is not indexable yet. So this is how we do it for now.
*
* What this thing does is that we let Fb do the filtering after
* submitting the page_id and the list of valid Fb UIDs.
*
* @see http://developers.facebook.com/docs/reference/fql/page_fan
* @param String $pageId Id of Fb page
* @param Mixed $uid Array for multiple UIDs
* @param String $type There may be lots of possible values for this.
* @return Array Null on error
*/
public function filterFansOf($pageId, $uid, $type = 'APPLICATION')
{
if (empty($uid) || empty($pageId))
return null;
$condition = '';
//if (isset($type))
//$condition .= 'type="'. $type .'"';
$condition .= empty($condition) ? '': ' AND ';
$condition .= 'page_id="'. $pageId .'" AND ';
if (is_array($uid))
$condition .= 'uid IN('. implode(',', $uid) .')';
else
$condition .= 'uid="'. $uid .'"';
$query = sprintf('SELECT uid, page_id, type FROM page_fan WHERE %s', $condition) ;
try {
return $this->getClient()->api(array(
'method' => 'fql.query',
'query' => $query,
));
} catch (FacebookApiException $e) {
Yii::log($e, CLogger::LEVEL_ERROR, __CLASS__);
return null;
}
}
}