Created
May 14, 2012 16:54
-
-
Save krolow/2695038 to your computer and use it in GitHub Desktop.
Facebook Cake bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!session_id()) { session_start(); } | |
App::import('Vendor', 'Facebook'); | |
class FacebookCake | |
{ | |
public function __construct($name = '') | |
{ | |
Configure::load('facebook'); | |
$this->facebook = new Facebook(array( | |
'appId' => Configure::read('Facebook.' . $name . '.id'), | |
'secret' => Configure::read('Facebook.' . $name . '.secret') | |
)); | |
$this->name = $name; | |
$this->url = Router::url(Configure::read('Facebook.' . $name . '.url'), true); | |
} | |
public function getUser() | |
{ | |
// Get User ID | |
$user = $this->facebook->getUser(); | |
if ($user) { | |
if (CakeSession::check('FacebookSession.' . $this->name)) { | |
return CakeSession::read('FacebookSession.' . $this->name); | |
} | |
try { | |
$userInfo = $this->facebook->api('/me'); | |
CakeSession::write('FacebookSession.' . $this->name, $userInfo); | |
return $userInfo; | |
} catch (FacebookApiException $e) { | |
throw new CakeException('Something went wrong' . $e->getMessage()); | |
} | |
} | |
CakeSession::delete('FacebookSession.' . $this->name); | |
$params = array('redirect_uri' => $this->url); | |
$perm = Configure::read('Facebook.' . $this->name . '.permission'); | |
if (!empty($perm)) { | |
$params['scope'] = $perm; | |
} | |
$params['canvas'] = 1; | |
$params['fbconnect'] = 0; | |
$facebookUrl = $this->facebook->getLoginUrl($params); | |
echo ' | |
<html><head> | |
<script type="text/javascript"> | |
top.location.href = "'. $facebookUrl .';"; | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> | |
'; | |
exit; | |
} | |
/** | |
* Return app_data passed to Page Tabs | |
* | |
* This method can return entire or parcial app_data passed to | |
* page tabs as urlencoded json | |
* | |
* @param $key Key inside app_data array | |
* @return Mixed Array/Boolean | |
*/ | |
public function getAppData($key = false) | |
{ | |
$signedRequest = $this->facebook->getSignedRequest(); | |
if (empty($signedRequest['app_data'])) { | |
return false; | |
} | |
$appData = json_decode($signedRequest['app_data']); | |
if (!$key) { | |
return $appData; | |
} | |
if (array_key_exists($key, $appData)) { | |
return $appData->$key; | |
} | |
return false; | |
} | |
/** | |
* Build a json array encoded and urlencoded | |
* | |
* @return String | |
*/ | |
public function buildAppData($appData = array()) | |
{ | |
$urlencoded = urlencode( | |
json_encode($appData) | |
); | |
return sprintf('app_data=%s', $urlencoded); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment