Created
January 5, 2012 18:47
-
-
Save mach3/1566606 to your computer and use it in GitHub Desktop.
Fangate class for facebook.
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 | |
/** | |
* Facebook fangate class | |
* | |
* @example | |
* $fangate = new Facebook_Fangate(); | |
* $fangate->setSecret( $your_application_secret ); | |
* $fangate->parse(); | |
* $fangate->isLiked(); // true if the page is liked | |
*/ | |
class Facebook_Fangate { | |
private $secret = null; | |
private $data = array(); | |
public function __construct( $secret = null ){ | |
$this->setSecret( $secret ); | |
} | |
/** | |
* Set application secret | |
* @param String secret | |
* @return Facebook_Fangate this | |
*/ | |
public function setSecret( $secret ){ | |
$this->secret = (string) $secret ; | |
return $this; | |
} | |
/** | |
* Get data ( after parsed ) | |
* @return Array data | |
*/ | |
public function getData(){ | |
return $this->data; | |
} | |
/** | |
* Get value from the array | |
* if $data is empty, use $this->data | |
* @param String key | |
* @param Array data | |
* @return String Value | null | |
*/ | |
private function getValue( $key, $data = null ){ | |
$data = is_null( $data ) ? (array) $this->data : (array) $data ; | |
if( array_key_exists( $key, $data ) ){ | |
return $data[$key]; | |
} | |
return null; | |
} | |
/** | |
* parse the signed_request from facebook | |
* if $signed_requst is empty, use $_REQUEST["signed_request"] | |
* @param String signed_requst | |
* @return Facebook_Fangate this | |
*/ | |
public function parse( $signed_request = null ){ | |
if( empty( $signed_request ) ){ | |
if( array_key_exists( "signed_request", $_REQUEST ) ){ | |
$signed_request = $_REQUEST["signed_request"]; | |
} else { | |
throw new Exception( "Signed request is empty." ); | |
} | |
} | |
list( $encoded_sig, $payload ) = explode( ".", $signed_request, 2 ); | |
$sig = $this->base64_url_decode( $encoded_sig ); | |
$this->data = json_decode( $this->base64_url_decode($payload), true ); | |
if( strtoupper( $this->getValue( "algorithm" ) !== "HMAC-SHA256" ) ){ | |
throw new Exception( "Algorithm is invalid." ); | |
} | |
$expected_sig = hash_hmac( "sha256", $payload, $this->secret, $raw=true ); | |
if( $sig !== $expected_sig ) { | |
throw new Exception( "Signature is invalid." ); | |
} | |
return $this; | |
} | |
/** | |
* Get the page is liked or not | |
* @return Boolean liked | |
*/ | |
public function isLiked(){ | |
if( $page = $this->getValue( "page" ) ){ | |
if( $this->getValue( "liked", $page ) ){ | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* url base64 decode | |
* @param String input | |
*/ | |
private function base64_url_decode( $input ){ | |
return base64_decode(strtr($input, '-_', '+/')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment