Created
July 26, 2012 14:24
-
-
Save datamosh/3182345 to your computer and use it in GitHub Desktop.
Educaedu Facebook UI
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 | |
require 'facebook.php'; | |
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); | |
class Facebook_UI extends Facebook { | |
// Get secret app secret | |
function getSecret() { | |
return $this->appSecret; | |
} | |
// Get Signed Request | |
// More info: https://developers.facebook.com/docs/authentication/signed_request/ | |
function getSignedRequest() { | |
if($this->user == 0 || $this->getSecret() == '') | |
return 0; | |
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2); | |
$sig = base64_decode(strtr($encoded_sig, '-_', '+/')); | |
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); | |
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { | |
error_log('Unknown algorithm. Expected HMAC-SHA256'); | |
return null; | |
} | |
$expected_sig = hash_hmac('sha256', $payload, $this->getSecret(), $raw = true); | |
if ($sig !== $expected_sig) { | |
error_log('Bad Signed JSON signature!'); | |
return null; | |
} | |
return $data; | |
} | |
// Get anything | |
function get($string = '19292868552') { | |
$this->user = $this->getUserFromAvailableData(); | |
if($this->user == 0) | |
return $this->user; | |
return $this->api("/$string"); | |
} | |
// Get user (user id, empty for 'me') | |
function getUser($userId = 0) { | |
$this->user = $this->getUserFromAvailableData(); | |
if($this->user == 0) | |
return $this->user; | |
if($userId == 0) { | |
return $this->api("/me"); | |
} else { | |
return $this->api("/$userId"); | |
} | |
} | |
// Get friends | |
function getFriends() { | |
$this->user = $this->getUserFromAvailableData(); | |
if($this->user == 0) | |
return $this->user; | |
$response = $this->api("/me/friends"); | |
return $response['data']; | |
} | |
// Get accepted permissions | |
function getPermissions() { | |
$this->user = $this->getUserFromAvailableData(); | |
if($this->user == 0) | |
return $this->user; | |
$response = $this->api('/me/permissions'); | |
return $response['data'][0]; | |
} | |
// Send application request | |
function sendRequest($users_array, $message) { | |
$ids = implode(",", $users_array); | |
$message = urlencode($message); | |
if($this->user == 0) | |
return $this->user; | |
$reponse = $this->api("/apprequests/?ids=$ids&message=$message&method=post&access_token=".$this->getAccessToken()); | |
} | |
// Get login status | |
function isLogged() { | |
$this->user = $this->getUserFromAvailableData(); | |
if($this->user == 0) { | |
return 0; | |
} else { | |
return 1; | |
} | |
} | |
// Run FQL Query | |
function fqlQuery($query) { | |
$this->user = $this->getUserFromAvailableData(); | |
if($this->user != 0 && $query != '') { | |
try { | |
$fql = urlencode($query); | |
$response = $this->api("/fql?q=$fql"); | |
return $response['data']; | |
} catch (FacebookApiException $e) { | |
$this->error($e); | |
} | |
} | |
} | |
// Throw exception | |
function error($error) { | |
throw new Exception($error->getMessage()); | |
} | |
} | |
// Example | |
// --------------------------------------------------------------------------- | |
$fbui = new Facebook_UI(array( | |
'appId' => '357032601034491', | |
'secret' => '93bbbc286e43115af9fc4890c885955e', | |
'cookie' => true | |
)); | |
echo "<pre>"; | |
echo "<h3>App id:</h3>"; | |
echo $fbui->getAppId(); | |
echo "<h3>App secret:</h3>"; | |
echo $fbui->getSecret(); | |
echo "<h3>is logged?:</h3>"; | |
print_r($fbui->isLogged()); | |
echo "<h3>Get signed request:</h3>"; | |
print_r($fbui->getSignedRequest()); | |
echo "<h3>Login URL:</h3>"; | |
$loginURL = $fbui->getLoginUrl(array( | |
scope => '' | |
)); | |
echo '<a target="_top" href="'.$loginURL.'">'.$loginURL.'</a>'; | |
echo "<h3>Logout URL:</h3>"; | |
$loginURL = $fbui->getLogoutUrl(array( | |
'next' => 'https://apps.facebook.com/educaedu_fbclass/' | |
)); | |
echo '<a target="_top" href="'.$loginURL.'">'.$loginURL.'</a>'; | |
echo "<h3>Get:</h3>"; | |
print_r($fbui->get()); | |
echo "<h3>Get user (myself):</h3>"; | |
print_r($fbui->getUser()); | |
echo "<h3>User user (a friend):</h3>"; | |
print_r($fbui->getUser('100001271711241')); | |
echo "<h3>FQL Query:</h3>"; | |
print_r($fbui->fqlQuery('SELECT name FROM user WHERE uid = me()')); | |
echo "<h3>Get permissions:</h3>"; | |
print_r($fbui->getPermissions()); | |
echo "<h3>Get my friends:</h3>"; | |
print_r($fbui->getFriends()); | |
echo "<h3>Send app request:</h3>"; | |
echo ($fbui->sendRequest(array('100002888807310'), 'Mensaje de prueba')); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment