2 // Copyright 2004-2008 Facebook. All Rights Reserved.
4 // +---------------------------------------------------------------------------+
5 // | Facebook Platform PHP5 client |
6 // +---------------------------------------------------------------------------+
7 // | Copyright (c) 2007 Facebook, Inc. |
8 // | All rights reserved. |
10 // | Redistribution and use in source and binary forms, with or without |
11 // | modification, are permitted provided that the following conditions |
14 // | 1. Redistributions of source code must retain the above copyright |
15 // | notice, this list of conditions and the following disclaimer. |
16 // | 2. Redistributions in binary form must reproduce the above copyright |
17 // | notice, this list of conditions and the following disclaimer in the |
18 // | documentation and/or other materials provided with the distribution. |
20 // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
21 // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
22 // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
23 // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
24 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
25 // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
29 // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 // +---------------------------------------------------------------------------+
31 // | For help with this library, contact developers-help@facebook.com |
32 // +---------------------------------------------------------------------------+
34 include_once 'facebookapi_php5_restlib.php';
36 define('FACEBOOK_API_VALIDATION_ERROR', 1);
42 public $generate_session_secret;
43 public $session_expires;
49 protected $base_domain;
51 * Create a Facebook client like this:
53 * $fb = new Facebook(API_KEY, SECRET);
55 * This will automatically pull in any parameters, validate them against the
56 * session signature, and chuck them in the public $fb_params member variable.
58 * @param api_key your Developer API key
59 * @param secret your Developer API secret
60 * @param generate_session_secret whether to automatically generate a session
61 * if the user doesn't have one, but
62 * there is an auth token present in the url,
64 public function __construct($api_key, $secret, $generate_session_secret=false) {
65 $this->api_key = $api_key;
66 $this->secret = $secret;
67 $this->generate_session_secret = $generate_session_secret;
68 $this->api_client = new FacebookRestClient($api_key, $secret, null);
69 $this->validate_fb_params();
71 // Set the default user id for methods that allow the caller to
72 // pass an explicit uid instead of using a session key.
75 $defaultUser = $this->user;
76 } else if ($this->profile_user) {
77 $defaultUser = $this->profile_user;
78 } else if ($this->canvas_user) {
79 $defaultUser = $this->canvas_user;
82 $this->api_client->set_user($defaultUser);
85 if (isset($this->fb_params['friends'])) {
86 $this->api_client->friends_list = explode(',', $this->fb_params['friends']);
88 if (isset($this->fb_params['added'])) {
89 $this->api_client->added = $this->fb_params['added'];
91 if (isset($this->fb_params['canvas_user'])) {
92 $this->api_client->canvas_user = $this->fb_params['canvas_user'];
97 * Validates that the parameters passed in were sent from Facebook. It does so
98 * by validating that the signature matches one that could only be generated
99 * by using your application's secret key.
101 * Facebook-provided parameters will come from $_POST, $_GET, or $_COOKIE,
102 * in that order. $_POST and $_GET are always more up-to-date than cookies,
103 * so we prefer those if they are available.
105 * For nitty-gritty details of when each of these is used, check out
106 * http://wiki.developers.facebook.com/index.php/Verifying_The_Signature
108 * @param bool resolve_auth_token convert an auth token into a session
110 public function validate_fb_params($resolve_auth_token=true) {
111 $this->fb_params = $this->get_valid_fb_params($_POST, 48*3600, 'fb_sig');
113 // note that with preload FQL, it's possible to receive POST params in
114 // addition to GET, so use a different prefix to differentiate them
115 if (!$this->fb_params) {
116 $fb_params = $this->get_valid_fb_params($_GET, 48*3600, 'fb_sig');
117 $fb_post_params = $this->get_valid_fb_params($_POST, 48*3600, 'fb_post_sig');
118 $this->fb_params = array_merge($fb_params, $fb_post_params);
121 // Okay, something came in via POST or GET
122 if ($this->fb_params) {
123 $user = isset($this->fb_params['user']) ?
124 $this->fb_params['user'] : null;
125 $this->profile_user = isset($this->fb_params['profile_user']) ?
126 $this->fb_params['profile_user'] : null;
127 $this->canvas_user = isset($this->fb_params['canvas_user']) ?
128 $this->fb_params['canvas_user'] : null;
129 $this->base_domain = isset($this->fb_params['base_domain']) ?
130 $this->fb_params['base_domain'] : null;
132 if (isset($this->fb_params['session_key'])) {
133 $session_key = $this->fb_params['session_key'];
134 } else if (isset($this->fb_params['profile_session_key'])) {
135 $session_key = $this->fb_params['profile_session_key'];
139 $expires = isset($this->fb_params['expires']) ?
140 $this->fb_params['expires'] : null;
141 $this->set_user($user,
145 // if no Facebook parameters were found in the GET or POST variables,
146 // then fall back to cookies, which may have cached user information
147 // Cookies are also used to receive session data via the Javascript API
149 $this->get_valid_fb_params($_COOKIE, null, $this->api_key)) {
151 $base_domain_cookie = 'base_domain_' . $this->api_key;
152 if (isset($_COOKIE[$base_domain_cookie])) {
153 $this->base_domain = $_COOKIE[$base_domain_cookie];
156 // use $api_key . '_' as a prefix for the cookies in case there are
157 // multiple facebook clients on the same domain.
158 $expires = isset($cookies['expires']) ? $cookies['expires'] : null;
159 $this->set_user($cookies['user'],
160 $cookies['session_key'],
163 // finally, if we received no parameters, but the 'auth_token' GET var
164 // is present, then we are in the middle of auth handshake,
165 // so go ahead and create the session
166 else if ($resolve_auth_token && isset($_GET['auth_token']) &&
167 $session = $this->do_get_session($_GET['auth_token'])) {
168 if ($this->generate_session_secret &&
169 !empty($session['secret'])) {
170 $session_secret = $session['secret'];
173 if (isset($session['base_domain'])) {
174 $this->base_domain = $session['base_domain'];
177 $this->set_user($session['uid'],
178 $session['session_key'],
180 isset($session_secret) ? $session_secret : null);
183 return !empty($this->fb_params);
186 // Store a temporary session secret for the current session
187 // for use with the JS client library
188 public function promote_session() {
190 $session_secret = $this->api_client->auth_promoteSession();
191 if (!$this->in_fb_canvas()) {
192 $this->set_cookies($this->user, $this->api_client->session_key, $this->session_expires, $session_secret);
194 return $session_secret;
195 } catch (FacebookRestClientException $e) {
196 // API_EC_PARAM means we don't have a logged in user, otherwise who
197 // knows what it means, so just throw it.
198 if ($e->getCode() != FacebookAPIErrorCodes::API_EC_PARAM) {
204 public function do_get_session($auth_token) {
206 return $this->api_client->auth_getSession($auth_token, $this->generate_session_secret);
207 } catch (FacebookRestClientException $e) {
208 // API_EC_PARAM means we don't have a logged in user, otherwise who
209 // knows what it means, so just throw it.
210 if ($e->getCode() != FacebookAPIErrorCodes::API_EC_PARAM) {
216 // Invalidate the session currently being used, and clear any state associated with it
217 public function expire_session() {
218 if ($this->api_client->auth_expireSession()) {
219 if (!$this->in_fb_canvas() && isset($_COOKIE[$this->api_key . '_user'])) {
220 $cookies = array('user', 'session_key', 'expires', 'ss');
221 foreach ($cookies as $name) {
222 setcookie($this->api_key . '_' . $name, false, time() - 3600);
223 unset($_COOKIE[$this->api_key . '_' . $name]);
225 setcookie($this->api_key, false, time() - 3600);
226 unset($_COOKIE[$this->api_key]);
229 // now, clear the rest of the stored state
231 $this->api_client->session_key = 0;
238 public function redirect($url) {
239 if ($this->in_fb_canvas()) {
240 echo '<fb:redirect url="' . $url . '"/>';
241 } else if (preg_match('/^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?/i', $url)) {
242 // make sure facebook.com url's load in the full frame so that we don't
243 // get a frame within a frame.
244 echo "<script type=\"text/javascript\">\ntop.location.href = \"$url\";\n</script>";
246 header('Location: ' . $url);
251 public function in_frame() {
252 return isset($this->fb_params['in_canvas']) || isset($this->fb_params['in_iframe']);
254 public function in_fb_canvas() {
255 return isset($this->fb_params['in_canvas']);
258 public function get_loggedin_user() {
262 public function get_canvas_user() {
263 return $this->canvas_user;
266 public function get_profile_user() {
267 return $this->profile_user;
270 public static function current_url() {
271 return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
274 // require_add and require_install have been removed.
275 // see http://developer.facebook.com/news.php?blog=1&story=116 for more details
276 public function require_login() {
277 if ($user = $this->get_loggedin_user()) {
280 $this->redirect($this->get_login_url(self::current_url(), $this->in_frame()));
283 public function require_frame() {
284 if (!$this->in_frame()) {
285 $this->redirect($this->get_login_url(self::current_url(), true));
289 public static function get_facebook_url($subdomain='www') {
290 return 'http://' . $subdomain . '.facebook.com';
293 public function get_install_url($next=null) {
294 // this was renamed, keeping for compatibility's sake
295 return $this->get_add_url($next);
298 public function get_add_url($next=null) {
299 return self::get_facebook_url().'/add.php?api_key='.$this->api_key .
300 ($next ? '&next=' . urlencode($next) : '');
303 public function get_login_url($next, $canvas) {
304 return self::get_facebook_url().'/login.php?v=1.0&api_key=' . $this->api_key .
305 ($next ? '&next=' . urlencode($next) : '') .
306 ($canvas ? '&canvas' : '');
309 public function set_user($user, $session_key, $expires=null, $session_secret=null) {
310 if (!$this->in_fb_canvas() && (!isset($_COOKIE[$this->api_key . '_user'])
311 || $_COOKIE[$this->api_key . '_user'] != $user)) {
312 $this->set_cookies($user, $session_key, $expires, $session_secret);
315 $this->api_client->session_key = $session_key;
316 $this->session_expires = $expires;
319 public function set_cookies($user, $session_key, $expires=null, $session_secret=null) {
321 $cookies['user'] = $user;
322 $cookies['session_key'] = $session_key;
323 if ($expires != null) {
324 $cookies['expires'] = $expires;
326 if ($session_secret != null) {
327 $cookies['ss'] = $session_secret;
330 foreach ($cookies as $name => $val) {
331 setcookie($this->api_key . '_' . $name, $val, (int)$expires, '', $this->base_domain);
332 $_COOKIE[$this->api_key . '_' . $name] = $val;
334 $sig = self::generate_sig($cookies, $this->secret);
335 setcookie($this->api_key, $sig, (int)$expires, '', $this->base_domain);
336 $_COOKIE[$this->api_key] = $sig;
338 if ($this->base_domain != null) {
339 $base_domain_cookie = 'base_domain_' . $this->api_key;
340 setcookie($base_domain_cookie, $this->base_domain, (int)$expires, '', $this->base_domain);
341 $_COOKIE[$base_domain_cookie] = $this->base_domain;
346 * Tries to undo the badness of magic quotes as best we can
347 * @param string $val Should come directly from $_GET, $_POST, etc.
348 * @return string val without added slashes
350 public static function no_magic_quotes($val) {
351 if (get_magic_quotes_gpc()) {
352 return stripslashes($val);
359 * Get the signed parameters that were sent from Facebook. Validates the set
360 * of parameters against the included signature.
362 * Since Facebook sends data to your callback URL via unsecured means, the
363 * signature is the only way to make sure that the data actually came from
364 * Facebook. So if an app receives a request at the callback URL, it should
365 * always verify the signature that comes with against your own secret key.
366 * Otherwise, it's possible for someone to spoof a request by
367 * pretending to be someone else, i.e.:
368 * www.your-callback-url.com/?fb_user=10101
370 * This is done automatically by verify_fb_params.
372 * @param assoc $params a full array of external parameters.
373 * presumed $_GET, $_POST, or $_COOKIE
374 * @param int $timeout number of seconds that the args are good for.
375 * Specifically good for forcing cookies to expire.
376 * @param string $namespace prefix string for the set of parameters we want
377 * to verify. i.e., fb_sig or fb_post_sig
379 * @return assoc the subset of parameters containing the given prefix,
380 * and also matching the signature associated with them.
381 * OR an empty array if the params do not validate
383 public function get_valid_fb_params($params, $timeout=null, $namespace='fb_sig') {
384 $prefix = $namespace . '_';
385 $prefix_len = strlen($prefix);
386 $fb_params = array();
387 if (empty($params)) {
391 foreach ($params as $name => $val) {
392 // pull out only those parameters that match the prefix
393 // note that the signature itself ($params[$namespace]) is not in the list
394 if (strpos($name, $prefix) === 0) {
395 $fb_params[substr($name, $prefix_len)] = self::no_magic_quotes($val);
399 // validate that the request hasn't expired. this is most likely
400 // for params that come from $_COOKIE
401 if ($timeout && (!isset($fb_params['time']) || time() - $fb_params['time'] > $timeout)) {
405 // validate that the params match the signature
406 $signature = isset($params[$namespace]) ? $params[$namespace] : null;
407 if (!$signature || (!$this->verify_signature($fb_params, $signature))) {
414 * Validates that a given set of parameters match their signature.
415 * Parameters all match a given input prefix, such as "fb_sig".
417 * @param $fb_params an array of all Facebook-sent parameters,
418 * not including the signature itself
419 * @param $expected_sig the expected result to check against
421 public function verify_signature($fb_params, $expected_sig) {
422 return self::generate_sig($fb_params, $this->secret) == $expected_sig;
426 * Generate a signature using the application secret key.
428 * The only two entities that know your secret key are you and Facebook,
429 * according to the Terms of Service. Since nobody else can generate
430 * the signature, you can rely on it to verify that the information
431 * came from Facebook.
433 * @param $params_array an array of all Facebook-sent parameters,
434 * NOT INCLUDING the signature itself
435 * @param $secret your app's secret key
437 * @return a hash to be checked against the signature provided by Facebook
439 public static function generate_sig($params_array, $secret) {
442 ksort($params_array);
443 // Note: make sure that the signature parameter is not already included in
445 foreach ($params_array as $k=>$v) {
453 public function encode_validationError($summary, $message) {
455 array('errorCode' => FACEBOOK_API_VALIDATION_ERROR,
456 'errorTitle' => $summary,
457 'errorMessage' => $message));
460 public function encode_multiFeedStory($feed, $next) {
462 array('method' => 'multiFeedStory',
464 array('next' => $next,
468 public function encode_feedStory($feed, $next) {
470 array('method' => 'feedStory',
472 array('next' => $next,
476 public function create_templatizedFeedStory($title_template, $title_data=array(),
477 $body_template='', $body_data = array(), $body_general=null,
478 $image_1=null, $image_1_link=null,
479 $image_2=null, $image_2_link=null,
480 $image_3=null, $image_3_link=null,
481 $image_4=null, $image_4_link=null) {
482 return array('title_template'=> $title_template,
483 'title_data' => $title_data,
484 'body_template'=> $body_template,
485 'body_data' => $body_data,
486 'body_general' => $body_general,
487 'image_1' => $image_1,
488 'image_1_link' => $image_1_link,
489 'image_2' => $image_2,
490 'image_2_link' => $image_2_link,
491 'image_3' => $image_3,
492 'image_3_link' => $image_3_link,
493 'image_4' => $image_4,
494 'image_4_link' => $image_4_link);