-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFb.php
More file actions
96 lines (89 loc) · 2.52 KB
/
Fb.php
File metadata and controls
96 lines (89 loc) · 2.52 KB
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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'fb-php-sdk/facebook.php';
class Fb {
private $fb_app_id = '';
private $fb_app_secret = '';
private $fb = null;
public $user;
public $logout_url;
private function signedRequest(){
$sr = isset($_REQUEST['signed_request'])?$_REQUEST['signed_request']:'';
setcookie("fb_signed_request_val", $sr, time()+(60*60*24),'/',$_SERVER['SERVER_NAME'],1);
set_cookie($cookie);
}
public function init_facebook(){
$this->signedRequest();
$this->load_config_file();
$this->get_fb_object();
if (!$this->have_user()){
$this->redirect_user($this->login_url());
}
$this->user = $this->fb->getUser();
$this->logout_url = $this->logout_url();
}
public function api(){
return call_user_func_array( array($this->fb,'api'), func_get_args() );
}
public function fql($fql){
if ($this->fb){
try {
$sql = $fql;
$ret_obj = $this->fb->api(array(
'method' => 'fql.query',
'query' => $fql,
));
return $ret_obj;
} catch(FacebookApiException $e) {
$this->logErr($e);
}
}
}
private function redirect_user($url){
echo "<script>window.top.location.href='{$url}'</script>";
exit();
}
private function logErr($err){
error_log($err->getType());
error_log($err->getMessage());
}
private function load_config_file() {
$CI =& get_instance();
$CI->config->load('facebook');
$this->fb_app_id = $CI->config->item('fbAppId');
$this->fb_app_secret = $CI->config->item('fbSecret');
$this->fb_app_canvas_url = $CI->config->item('fbCanvas');
$this->fb_app_scope = $CI->config->item('fbScope');
$this->fb_app_display = $CI->config->item('fbDisplay');
}
private function get_fb_object(){
$this->fb = new Facebook(array(
'appId' => $this->fb_app_id,
'secret' => $this->fb_app_secret,
));
}
private function login_url(){
$params = array(
'scope' => $this->fb_app_scope,
'redirect_uri' => $this->fb_app_canvas_url,
'display' => $this->fb_app_display
);
return $this->fb->getLoginUrl($params);
}
private function logout_url(){
return $this->fb->getLogoutUrl();
}
private function have_user(){
$user = $this->fb->getUser();
if ($user) {
try {
$user_profile = $this->fb->api('/me');
return true;
} catch (FacebookApiException $e) {
$this->logErr($e);
return false;
}
}else{
return false;
}
}
}