Version 3.1.4
This commit is contained in:
1 parent
1d4720f3e2
commit
ae5c01cc78
2563 files changed
+283048
-240689
No files matched your search
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
// Copyright 2004-2008 Facebook. All Rights Reserved.
|
||||
// Copyright 2004-2009 Facebook. All Rights Reserved.
|
||||
//
|
||||
// +---------------------------------------------------------------------------+
|
||||
// | Facebook Platform PHP5 client |
|
||||
@@ -30,13 +30,12 @@
|
||||
// +---------------------------------------------------------------------------+
|
||||
// | For help with this library, contact developers-help@facebook.com |
|
||||
// +---------------------------------------------------------------------------+
|
||||
//
|
||||
|
||||
include_once 'facebookapi_php5_restlib.php';
|
||||
|
||||
define('FACEBOOK_API_VALIDATION_ERROR', 1);
|
||||
class Facebook {
|
||||
public $api_client;
|
||||
|
||||
public $api_key;
|
||||
public $secret;
|
||||
public $generate_session_secret;
|
||||
@@ -46,6 +45,8 @@ class Facebook {
|
||||
public $user;
|
||||
public $profile_user;
|
||||
public $canvas_user;
|
||||
public $ext_perms = array();
|
||||
protected $base_domain;
|
||||
|
||||
/*
|
||||
* Create a Facebook client like this:
|
||||
@@ -65,11 +66,26 @@ class Facebook {
|
||||
$this->api_key = $api_key;
|
||||
$this->secret = $secret;
|
||||
$this->generate_session_secret = $generate_session_secret;
|
||||
$this->api_client = new FacebookRestClient($api_key, $secret);
|
||||
|
||||
$this->api_client = new FacebookRestClient($api_key, $secret, null);
|
||||
$this->validate_fb_params();
|
||||
|
||||
// Set the default user id for methods that allow the caller to
|
||||
// pass an explicit uid instead of using a session key.
|
||||
$defaultUser = null;
|
||||
if ($this->user) {
|
||||
$defaultUser = $this->user;
|
||||
} else if ($this->profile_user) {
|
||||
$defaultUser = $this->profile_user;
|
||||
} else if ($this->canvas_user) {
|
||||
$defaultUser = $this->canvas_user;
|
||||
}
|
||||
|
||||
$this->api_client->set_user($defaultUser);
|
||||
|
||||
|
||||
if (isset($this->fb_params['friends'])) {
|
||||
$this->api_client->friends_list = explode(',', $this->fb_params['friends']);
|
||||
$this->api_client->friends_list =
|
||||
array_filter(explode(',', $this->fb_params['friends']));
|
||||
}
|
||||
if (isset($this->fb_params['added'])) {
|
||||
$this->api_client->added = $this->fb_params['added'];
|
||||
@@ -90,17 +106,17 @@ class Facebook {
|
||||
*
|
||||
* For nitty-gritty details of when each of these is used, check out
|
||||
* http://wiki.developers.facebook.com/index.php/Verifying_The_Signature
|
||||
*
|
||||
* @param bool resolve_auth_token convert an auth token into a session
|
||||
*/
|
||||
public function validate_fb_params($resolve_auth_token=true) {
|
||||
$this->fb_params = $this->get_valid_fb_params($_POST, 48*3600, 'fb_sig');
|
||||
public function validate_fb_params() {
|
||||
$this->fb_params = $this->get_valid_fb_params($_POST, 48 * 3600, 'fb_sig');
|
||||
|
||||
// note that with preload FQL, it's possible to receive POST params in
|
||||
// addition to GET, so use a different prefix to differentiate them
|
||||
if (!$this->fb_params) {
|
||||
$fb_params = $this->get_valid_fb_params($_GET, 48*3600, 'fb_sig');
|
||||
$fb_post_params = $this->get_valid_fb_params($_POST, 48*3600, 'fb_post_sig');
|
||||
$fb_params = $this->get_valid_fb_params($_GET, 48 * 3600, 'fb_sig');
|
||||
$fb_post_params = $this->get_valid_fb_params($_POST,
|
||||
48 * 3600, // 48 hours
|
||||
'fb_post_sig');
|
||||
$this->fb_params = array_merge($fb_params, $fb_post_params);
|
||||
}
|
||||
|
||||
@@ -112,6 +128,11 @@ class Facebook {
|
||||
$this->fb_params['profile_user'] : null;
|
||||
$this->canvas_user = isset($this->fb_params['canvas_user']) ?
|
||||
$this->fb_params['canvas_user'] : null;
|
||||
$this->base_domain = isset($this->fb_params['base_domain']) ?
|
||||
$this->fb_params['base_domain'] : null;
|
||||
$this->ext_perms = isset($this->fb_params['ext_perms']) ?
|
||||
explode(',', $this->fb_params['ext_perms'])
|
||||
: array();
|
||||
|
||||
if (isset($this->fb_params['session_key'])) {
|
||||
$session_key = $this->fb_params['session_key'];
|
||||
@@ -125,12 +146,16 @@ class Facebook {
|
||||
$this->set_user($user,
|
||||
$session_key,
|
||||
$expires);
|
||||
}
|
||||
// if no Facebook parameters were found in the GET or POST variables,
|
||||
// then fall back to cookies, which may have cached user information
|
||||
// Cookies are also used to receive session data via the Javascript API
|
||||
else if ($cookies =
|
||||
$this->get_valid_fb_params($_COOKIE, null, $this->api_key)) {
|
||||
} else if ($cookies =
|
||||
$this->get_valid_fb_params($_COOKIE, null, $this->api_key)) {
|
||||
// if no Facebook parameters were found in the GET or POST variables,
|
||||
// then fall back to cookies, which may have cached user information
|
||||
// Cookies are also used to receive session data via the Javascript API
|
||||
$base_domain_cookie = 'base_domain_' . $this->api_key;
|
||||
if (isset($_COOKIE[$base_domain_cookie])) {
|
||||
$this->base_domain = $_COOKIE[$base_domain_cookie];
|
||||
}
|
||||
|
||||
// use $api_key . '_' as a prefix for the cookies in case there are
|
||||
// multiple facebook clients on the same domain.
|
||||
$expires = isset($cookies['expires']) ? $cookies['expires'] : null;
|
||||
@@ -138,20 +163,6 @@ class Facebook {
|
||||
$cookies['session_key'],
|
||||
$expires);
|
||||
}
|
||||
// finally, if we received no parameters, but the 'auth_token' GET var
|
||||
// is present, then we are in the middle of auth handshake,
|
||||
// so go ahead and create the session
|
||||
else if ($resolve_auth_token && isset($_GET['auth_token']) &&
|
||||
$session = $this->do_get_session($_GET['auth_token'])) {
|
||||
if ($this->generate_session_secret &&
|
||||
!empty($session['secret'])) {
|
||||
$session_secret = $session['secret'];
|
||||
}
|
||||
$this->set_user($session['uid'],
|
||||
$session['session_key'],
|
||||
$session['expires'],
|
||||
isset($session_secret) ? $session_secret : null);
|
||||
}
|
||||
|
||||
return !empty($this->fb_params);
|
||||
}
|
||||
@@ -186,28 +197,63 @@ class Facebook {
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate the session currently being used, and clear any state associated with it
|
||||
// Invalidate the session currently being used, and clear any state associated
|
||||
// with it. Note that the user will still remain logged into Facebook.
|
||||
public function expire_session() {
|
||||
if ($this->api_client->auth_expireSession()) {
|
||||
if (!$this->in_fb_canvas() && isset($_COOKIE[$this->api_key . '_user'])) {
|
||||
$cookies = array('user', 'session_key', 'expires', 'ss');
|
||||
foreach ($cookies as $name) {
|
||||
setcookie($this->api_key . '_' . $name, false, time() - 3600);
|
||||
unset($_COOKIE[$this->api_key . '_' . $name]);
|
||||
}
|
||||
setcookie($this->api_key, false, time() - 3600);
|
||||
unset($_COOKIE[$this->api_key]);
|
||||
try {
|
||||
if ($this->api_client->auth_expireSession()) {
|
||||
$this->clear_cookie_state();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// now, clear the rest of the stored state
|
||||
$this->user = 0;
|
||||
$this->api_client->session_key = 0;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
} catch (Exception $e) {
|
||||
$this->clear_cookie_state();
|
||||
}
|
||||
}
|
||||
|
||||
/** Logs the user out of all temporary application sessions as well as their
|
||||
* Facebook session. Note this will only work if the user has a valid current
|
||||
* session with the application.
|
||||
*
|
||||
* @param string $next URL to redirect to upon logging out
|
||||
*
|
||||
*/
|
||||
public function logout($next) {
|
||||
$logout_url = $this->get_logout_url($next);
|
||||
|
||||
// Clear any stored state
|
||||
$this->clear_cookie_state();
|
||||
|
||||
$this->redirect($logout_url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears any persistent state stored about the user, including
|
||||
* cookies and information related to the current session in the
|
||||
* client.
|
||||
*
|
||||
*/
|
||||
public function clear_cookie_state() {
|
||||
if (!$this->in_fb_canvas() && isset($_COOKIE[$this->api_key . '_user'])) {
|
||||
$cookies = array('user', 'session_key', 'expires', 'ss');
|
||||
foreach ($cookies as $name) {
|
||||
setcookie($this->api_key . '_' . $name,
|
||||
false,
|
||||
time() - 3600,
|
||||
'',
|
||||
$this->base_domain);
|
||||
unset($_COOKIE[$this->api_key . '_' . $name]);
|
||||
}
|
||||
setcookie($this->api_key, false, time() - 3600, '', $this->base_domain);
|
||||
unset($_COOKIE[$this->api_key]);
|
||||
}
|
||||
|
||||
// now, clear the rest of the stored state
|
||||
$this->user = 0;
|
||||
$this->api_client->session_key = 0;
|
||||
}
|
||||
|
||||
public function redirect($url) {
|
||||
if ($this->in_fb_canvas()) {
|
||||
echo '<fb:redirect url="' . $url . '"/>';
|
||||
@@ -222,7 +268,8 @@ class Facebook {
|
||||
}
|
||||
|
||||
public function in_frame() {
|
||||
return isset($this->fb_params['in_canvas']) || isset($this->fb_params['in_iframe']);
|
||||
return isset($this->fb_params['in_canvas'])
|
||||
|| isset($this->fb_params['in_iframe']);
|
||||
}
|
||||
public function in_fb_canvas() {
|
||||
return isset($this->fb_params['in_canvas']);
|
||||
@@ -246,11 +293,28 @@ class Facebook {
|
||||
|
||||
// require_add and require_install have been removed.
|
||||
// see http://developer.facebook.com/news.php?blog=1&story=116 for more details
|
||||
public function require_login() {
|
||||
if ($user = $this->get_loggedin_user()) {
|
||||
public function require_login($required_permissions = '') {
|
||||
$user = $this->get_loggedin_user();
|
||||
$has_permissions = true;
|
||||
|
||||
if ($required_permissions) {
|
||||
$this->require_frame();
|
||||
$permissions = array_map('trim', explode(',', $required_permissions));
|
||||
foreach ($permissions as $permission) {
|
||||
if (!in_array($permission, $this->ext_perms)) {
|
||||
$has_permissions = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($user && $has_permissions) {
|
||||
return $user;
|
||||
}
|
||||
$this->redirect($this->get_login_url(self::current_url(), $this->in_frame()));
|
||||
|
||||
$this->redirect(
|
||||
$this->get_login_url(self::current_url(), $this->in_frame(),
|
||||
$required_permissions));
|
||||
}
|
||||
|
||||
public function require_frame() {
|
||||
@@ -260,7 +324,7 @@ class Facebook {
|
||||
}
|
||||
|
||||
public static function get_facebook_url($subdomain='www') {
|
||||
return 'http://' . $subdomain . '.new.facebook.com';
|
||||
return 'http://' . $subdomain . '.facebook.com';
|
||||
}
|
||||
|
||||
public function get_install_url($next=null) {
|
||||
@@ -269,14 +333,43 @@ class Facebook {
|
||||
}
|
||||
|
||||
public function get_add_url($next=null) {
|
||||
return self::get_facebook_url().'/add.php?api_key='.$this->api_key .
|
||||
($next ? '&next=' . urlencode($next) : '');
|
||||
$page = self::get_facebook_url().'/add.php';
|
||||
$params = array('api_key' => $this->api_key);
|
||||
|
||||
if ($next) {
|
||||
$params['next'] = $next;
|
||||
}
|
||||
|
||||
return $page . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
public function get_login_url($next, $canvas) {
|
||||
return self::get_facebook_url().'/login.php?v=1.0&api_key=' . $this->api_key .
|
||||
($next ? '&next=' . urlencode($next) : '') .
|
||||
($canvas ? '&canvas' : '');
|
||||
public function get_login_url($next, $canvas, $req_perms = '') {
|
||||
$page = self::get_facebook_url().'/login.php';
|
||||
$params = array('api_key' => $this->api_key,
|
||||
'v' => '1.0',
|
||||
'req_perms' => $req_perms);
|
||||
|
||||
if ($next) {
|
||||
$params['next'] = $next;
|
||||
}
|
||||
if ($canvas) {
|
||||
$params['canvas'] = '1';
|
||||
}
|
||||
|
||||
return $page . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
public function get_logout_url($next) {
|
||||
$page = self::get_facebook_url().'/logout.php';
|
||||
$params = array('app_key' => $this->api_key,
|
||||
'session_key' => $this->api_client->session_key);
|
||||
|
||||
if ($next) {
|
||||
$params['connect_next'] = 1;
|
||||
$params['next'] = $next;
|
||||
}
|
||||
|
||||
return $page . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
public function set_user($user, $session_key, $expires=null, $session_secret=null) {
|
||||
@@ -299,13 +392,20 @@ class Facebook {
|
||||
if ($session_secret != null) {
|
||||
$cookies['ss'] = $session_secret;
|
||||
}
|
||||
|
||||
foreach ($cookies as $name => $val) {
|
||||
setcookie($this->api_key . '_' . $name, $val, (int)$expires);
|
||||
setcookie($this->api_key . '_' . $name, $val, (int)$expires, '', $this->base_domain);
|
||||
$_COOKIE[$this->api_key . '_' . $name] = $val;
|
||||
}
|
||||
$sig = self::generate_sig($cookies, $this->secret);
|
||||
setcookie($this->api_key, $sig, (int)$expires);
|
||||
setcookie($this->api_key, $sig, (int)$expires, '', $this->base_domain);
|
||||
$_COOKIE[$this->api_key] = $sig;
|
||||
|
||||
if ($this->base_domain != null) {
|
||||
$base_domain_cookie = 'base_domain_' . $this->api_key;
|
||||
setcookie($base_domain_cookie, $this->base_domain, (int)$expires, '', $this->base_domain);
|
||||
$_COOKIE[$base_domain_cookie] = $this->base_domain;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -376,7 +476,20 @@ class Facebook {
|
||||
return $fb_params;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Validates the account that a user was trying to set up an
|
||||
* independent account through Facebook Connect.
|
||||
*
|
||||
* @param user The user attempting to set up an independent account.
|
||||
* @param hash The hash passed to the reclamation URL used.
|
||||
* @return bool True if the user is the one that selected the
|
||||
* reclamation link.
|
||||
*/
|
||||
public function verify_account_reclamation($user, $hash) {
|
||||
return $hash == md5($user . $this->secret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that a given set of parameters match their signature.
|
||||
* Parameters all match a given input prefix, such as "fb_sig".
|
||||
*
|
||||
@@ -388,6 +501,37 @@ class Facebook {
|
||||
return self::generate_sig($fb_params, $this->secret) == $expected_sig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the given signed public session data structure with
|
||||
* public key of the app that
|
||||
* the session proof belongs to.
|
||||
*
|
||||
* @param $signed_data the session info that is passed by another app
|
||||
* @param string $public_key Optional public key of the app. If this
|
||||
* is not passed, function will make an API call to get it.
|
||||
* return true if the session proof passed verification.
|
||||
*/
|
||||
public function verify_signed_public_session_data($signed_data,
|
||||
$public_key = null) {
|
||||
|
||||
// If public key is not already provided, we need to get it through API
|
||||
if (!$public_key) {
|
||||
$public_key = $this->api_client->auth_getAppPublicKey(
|
||||
$signed_data['api_key']);
|
||||
}
|
||||
|
||||
// Create data to verify
|
||||
$data_to_serialize = $signed_data;
|
||||
unset($data_to_serialize['sig']);
|
||||
$serialized_data = implode('_', $data_to_serialize);
|
||||
|
||||
// Decode signature
|
||||
$signature = base64_decode($signed_data['sig']);
|
||||
$result = openssl_verify($serialized_data, $signature, $public_key,
|
||||
OPENSSL_ALGO_SHA1);
|
||||
return $result == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a signature using the application secret key.
|
||||
*
|
||||
@@ -462,4 +606,3 @@ class Facebook {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user