forked from thomaswelton/composer-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook.php
executable file
·132 lines (104 loc) · 3.58 KB
/
facebook.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CI_Facebook extends Facebook {
private static $ogTags = array();
var $myApiConfig = array(
'cookie' => true,
'language_code' => 'en_GB',
'debug' => false
);
function __construct($config = null){
if(is_null($config)){
error_log('No Facebook Config file found');
return;
}
$this->myApiConfig = array_merge($this->myApiConfig, $config);
parent::__construct($this->myApiConfig);
//If enabled in the config force the aplication to redirect to the canvas
if(array_key_exists('canvasRedirect', $config) && $config['canvasRedirect']){
if(!array_key_exists('HTTP_REFERER', $_SERVER)){
redirect($this->getCanvasUrl(), 'refresh');
}
}
$openGraph = array(
'og:type' => 'website',
'og:url' => current_url(),
'fb:app_id' => $config['appId']
);
if(array_key_exists('graph',$config) && is_array($config['graph'])){
$openGraph = array_merge($openGraph,$config['graph']);
}
$this->setOpenGraphTags($openGraph);
}
public function getSignedRequest($useSession = true){
$CI = get_instance();
$CI->load->library('session');
$signedRequest = parent::getSignedRequest();
if(is_array($signedRequest)){
// Save the found sigednRequest to a session
$CI->session->set_userdata('signedRequest', $signedRequest);
}else if(is_null($signedRequest) && $useSession){
// If not found return the signedRequest from the session
$sessionSigendRequest = $CI->session->userdata('signedRequest');
if($sessionSigendRequest){
return $sessionSigendRequest;
}
}
return $signedRequest;
}
/**
* Checks to see if the user has "liked" the page by checking a signed request
* @return int -1 don't know, 0 doesn't like, 1 liked
*/
public function hasLiked(){
$signedRequest = $this->getSignedRequest();
if(is_null($signedRequest) || !array_key_exists('page', $signedRequest)){
// We dont know
return -1;
}else{
// Return the value Facebook told us
return $signedRequest['page']['liked'];
}
}
public function jsRedirect($location){
echo '<script type="text/javascript">';
echo "window.parent.location = '{$location}'";
echo '</script>';
die;
}
public function getNamespace(){
return (array_key_exists('namespace', $this->myApiConfig)) ? $this->myApiConfig['namespace'] : null;
}
public function getPageId(){
return (array_key_exists('pageId', $this->myApiConfig)) ? $this->myApiConfig['pageId'] : null;
}
public function getTabAppUrl(){
$pageId = $this->getPageId();
if($pageId){
$appId = $this->getAppId();
return "http://www.facebook.com/pages/null/{$pageId}?sk=app_{$appId}";
}
return null;
}
public function getShareUrl($data){
$shareUrl = 'https://www.facebook.com/dialog/feed';
$defaults = array( 'app_id' => $this->getAppId(),
'redirect_uri' => site_url());
$shareParams = array_merge($defaults, $data);
return $shareUrl . '?' . http_build_query($shareParams);
}
public function getCanvasUrl($path = ''){
return (array_key_exists('namespace', $this->myApiConfig)) ? 'http://apps.facebook.com/'.$this->myApiConfig['namespace'].'/'.$path : null;
}
public function setOpenGraphTags($tags){
self::$ogTags = array_merge(self::$ogTags,$tags);
}
public function openGraphMeta(){
$html = '';
foreach(self::$ogTags as $key => $value){
if(trim($value) != ''){
$html .= '<meta property="'.$key.'" content="'.htmlentities($value).'">'."\n\t";
}
}
return $html;
}
}