Skip to content

Instantly share code, notes, and snippets.

@psaia
Created July 2, 2010 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaia/461720 to your computer and use it in GitHub Desktop.
Save psaia/461720 to your computer and use it in GitHub Desktop.
<?php
include(APPPATH.'libraries/facebook-client/facebook.php');
class Facebook_connect {
private $_obj;
private $_api_key = NULL;
private $_secret_key = NULL;
public $user = NULL;
public $user_id = FALSE;
public $session_ended = FALSE;
public $fb;
public $client;
function Facebook_connect()
{
$this->_obj =& get_instance();
$this->_obj->load->config('facebook');
$this->_obj->load->library('session');
$this->_api_key = $this->_obj->config->item('facebook_api_key');
$this->_secret_key = $this->_obj->config->item('facebook_secret_key');
;
$this->fb = new Facebook($this->_api_key, $this->_secret_key);
$this->client = $this->fb->api_client;
$this->user_id = $this->fb->get_loggedin_user();
$this->_manage_session();
}
private function _manage_session()
{
if($this->user_id){//test to make sure session is not expired
try {
$this->fb->api_client->fql_query('SELECT uid FROM user WHERE uid='.$this->user_id); //is it actually still fresh???
} catch (Exception $ex) {
//if it's expired
$this->fb->clear_cookie_state();
$this->session_ended = TRUE;
}
}
$user = $this->_obj->session->userdata('facebook_user');
if ( $user === FALSE && $this->user_id !== NULL ) //if there is id & facebook_user session hasn't been set yet
{
$this->session_ended = FALSE;
$profile_data = array('uid','first_name', 'last_name', 'name', 'locale', 'pic_square', 'profile_url');
$info = $this->fb->api_client->users_getInfo($this->user_id, $profile_data);
$user = $info[0];
$this->_obj->session->set_userdata('facebook_user', $user);
}
elseif ( $user !== FALSE && $this->user_id === NULL ) //logged in but logged out of fb, should redirect.
{
$this->fb->clear_cookie_state();
$this->session_ended = TRUE;
}
if ( $user !== FALSE )//set $this->user variable...
{
$this->user = $user;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment