-
Notifications
You must be signed in to change notification settings - Fork 3
/
fbApplicationComponent.php
240 lines (207 loc) · 5.86 KB
/
fbApplicationComponent.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
<?php
/**
*
*
* @author Shiki
*/
class fbApplicationComponent extends CApplicationComponent
{
/**
* Intentional but temporary duplicate with fbSafariIframeFixAction
* @todo fixme
*/
const SKEY_SAFARI_COOKIE_SET = 'safari_cookie_set';
public $sdkFilePath;
public $applicationId;
public $apiKey;
public $applicationSecret;
/**
* The url of the app as seen on Facebook (e.g. http://apps.facebook.com/myapp)
* @var string
*/
public $appBaseUrl;
/**
* The actual url of the application (e.g. http://mydomain.com/canvas)
* @var string
*/
public $realBaseUrl;
/**
* The url pointing to the tab page.
* @var String
*/
public $fanPageTabUrl;
/**
* @see http://developers.facebook.com/docs/authentication/permissions
* @var array
*/
public $permissions;
/**
*
* @var Facebook
*/
protected $_client;
public function init()
{
Yii::import($this->sdkFilePath, true);
parent::init();
}
/**
* @return Facebook
*/
public function getClient()
{
if (!$this->_client) {
$this->_client = $this->createClient();
}
return $this->_client;
}
public function getPermissionsAsString()
{
if (!is_array($this->permissions))
return '';
else
return implode(',', $this->permissions);
}
/**
* @return Facebook
*/
public function createClient($options = null)
{
if (!is_array($options))
$options = array();
$options = array_merge(array('cookie' => true), $options);
$client = new Facebook(array(
'appId' => $this->applicationId,
'secret' => $this->applicationSecret,
'cookie' => $options['cookie'],
));
return $client;
}
/**
*
* @param string $url
*/
public function redirectAbsolute($url, $message = null)
{
if (!empty($message)) {
echo $message;
echo '<script type="text/javascript">setTimeout(function(){top.location.href = "' . $url . '";}, 2000);</script>';
} else {
echo '<script type="text/javascript">top.location.href = "' . $url . '";</script>';
}
Yii::app()->end();
}
/**
* Redirect to the url relative to $appBaseUrl.
* @see $appBaseUrl
* @param string $relativeUrl
*/
public function redirect($relativeUrl)
{
$this->redirectAbsolute($this->appBaseUrl . '/' . $relativeUrl);
}
public function isInTabPage()
{
$signedRequest = $this->getClient()->getSignedRequest();
return isset($signedRequest['page']);
}
/**
*
* @param <type> $uid
* @return Array
*/
public static function getPublicUserData($uid)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://graph.facebook.com/'. $uid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
if ($json)
return CJSON::decode($json);
return false;
}
/**
* @param Int $appId Defaults to its `applicationId` property
* @return Array
*/
public function getCommentsInfo($appId = null)
{
$appId = empty($appId) ? $this->applicationId: $appId;
$query = "SELECT xid, count, updated_time FROM comments_info WHERE app_id = '{$appId}'";
try {
$client = $this->getClient();
return $client->api(array(
'method' => 'fql.query',
'query' => $query,
));
} catch(FacebookApiException $e) {
Yii::log($e->__toString(), CLogger::LEVEL_ERROR, __CLASS__);
return null;
}
}
/**
* To know how many comments per XID, you first have to
* query `comments_info` FQL table.
*
* @param String $xid
* @param Int $offset
* @param Int $limit // Limit per query is 100 rows
* @return Array
*/
public function getComments($xid, $offset = 0, $limit = 100)
{
if (!$xid)
return null;
// throttle SSL connect timeout
// May not be necessary for one or two succeeding `getComments()` calls
Facebook::$CURL_OPTS[CURLOPT_CONNECTTIMEOUT] = 720; // 12 minutes
$query1 = "SELECT id, xid, fromid, time, text"
. " FROM comment WHERE xid='{$xid}' "
. " ORDER BY time DESC"
. " LIMIT {$limit} OFFSET {$offset}";
$query2 = "SELECT id, name, url, pic_square FROM profile WHERE id IN (SELECT fromid FROM #query1)";
$multiQuery = sprintf('{"query1": "%s", "query2": "%s"}', $query1, $query2);
try {
$client = $this->getClient();
return $client->api(array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
));
} catch (FacebookApiException $e) {
Yii::log($e->__toString(), CLogger::LEVEL_ERROR, __CLASS__);
return null;
}
}
public function removeComment($commentId, $xid)
{
try {
// This prevents FB from throwing: `SSL certificate problem, verify that the CA cert is OK.`
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$client = $this->getClient();
return $client->api(array(
'method' => 'comments.remove',
'comment_id' => $commentId,
'xid' => $xid,
));
} catch (FacebookApiException $e) {
Yii::log($e->__toString(), CLogger::LEVEL_ERROR, __CLASS__);
return false;
}
}
public function ensureSafariIFrameFix()
{
// hack for safari not persisting cookies on iframes
// http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safari/
if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
// this browser detection might not be the best solution, but I think it works fine
// If there's problem, we could just use this I think: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/
$session = Yii::app()->session;
if (!isset($session[self::SKEY_SAFARI_COOKIE_SET])) {
$this->render('ext.facebookapp.safari-cookie-fix');
return;
}
}
}
}