· 7 years ago · May 30, 2018, 03:30 PM
1<?php
2
3include(APPPATH.'libraries/facebook-client/facebook.php');
4
5class Facebook_connect {
6
7 private $_obj;
8 private $_api_key = NULL;
9 private $_secret_key = NULL;
10 public $user = NULL;
11 public $user_id = FALSE;
12 public $session_ended = FALSE;
13
14 public $fb;
15 public $client;
16
17 function Facebook_connect()
18 {
19 $this->_obj =& get_instance();
20
21 $this->_obj->load->config('facebook');
22 $this->_obj->load->library('session');
23
24 $this->_api_key = $this->_obj->config->item('facebook_api_key');
25 $this->_secret_key = $this->_obj->config->item('facebook_secret_key');
26 ;
27 $this->fb = new Facebook($this->_api_key, $this->_secret_key);
28
29 $this->client = $this->fb->api_client;
30
31 $this->user_id = $this->fb->get_loggedin_user();
32
33 $this->_manage_session();
34 }
35
36 private function _manage_session()
37 {
38
39 if($this->user_id){//test to make sure session is not expired
40 try {
41 $this->fb->api_client->fql_query('SELECT uid FROM user WHERE uid='.$this->user_id); //is it actually still fresh???
42 } catch (Exception $ex) {
43 //if it's expired
44 $this->fb->clear_cookie_state();
45 $this->session_ended = TRUE;
46 }
47 }
48
49 $user = $this->_obj->session->userdata('facebook_user');
50
51 if ( $user === FALSE && $this->user_id !== NULL ) //if there is id & facebook_user session hasn't been set yet
52 {
53 $this->session_ended = FALSE;
54 $profile_data = array('uid','first_name', 'last_name', 'name', 'locale', 'pic_square', 'profile_url');
55 $info = $this->fb->api_client->users_getInfo($this->user_id, $profile_data);
56
57 $user = $info[0];
58
59 $this->_obj->session->set_userdata('facebook_user', $user);
60 }
61 elseif ( $user !== FALSE && $this->user_id === NULL ) //logged in but logged out of fb, should redirect.
62 {
63 $this->fb->clear_cookie_state();
64 $this->session_ended = TRUE;
65 }
66
67 if ( $user !== FALSE )//set $this->user variable...
68 {
69 $this->user = $user;
70 }
71 }
72}