Guest User

Jason

a guest
Dec 21st, 2010
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. <?php
  2.  
  3. class facebook {
  4.  
  5.     static $app_key;
  6.     static $app_secret;
  7.     static $init = false;
  8.    
  9.     public static function init() {
  10.    
  11.     if(self::$init) return true;
  12.    
  13.     self::$app_key = @sanscode::$config['facebook']['app_id'];
  14.     self::$app_secret = @sanscode::$config['facebook']['app_secret'];
  15.    
  16.     self::$init = true;
  17.     }
  18.  
  19.     public static function fetch($login_callback=false, $logout_callback=false) {
  20.     self::init();
  21.    
  22.     $app_key = self::$app_key;
  23.     $var = <<<EOF
  24.     <div id="fb-root"></div>
  25.     <script src="http://connect.facebook.net/en_US/all.js"></script>
  26.     <script>
  27.         FB.init({appId: '$app_key', status: true, cookie: true, xfbml: true});
  28.         FB.Event.subscribe('auth.sessionChange', function(response) {
  29.         if (response.session) {
  30.              $login_callback
  31.                 } else {
  32.              $logout_callback
  33.                 }
  34.             });
  35.     </script>
  36. EOF;
  37.    
  38.     return $var;
  39.     }
  40.  
  41.     public static function get_facebook_cookie() {
  42.     self::init();
  43.    
  44.     $app_id = self::$app_key;
  45.     $application_secret = self::$app_secret;
  46.    
  47.     if(!isset($_COOKIE['fbs_' . $app_id])) return null;
  48.    
  49.     $args = array();
  50.     parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  51.     ksort($args);
  52.     $payload = '';
  53.     foreach ($args as $key => $value) {
  54.         if ($key != 'sig') {
  55.         $payload .= $key . '=' . $value;
  56.         }
  57.     }
  58.     if (md5($payload . $application_secret) != $args['sig']) {
  59.         return null;
  60.     }
  61.     return $args;
  62.     }
  63.     public static function user_info(){
  64.     self::init();
  65.    
  66.     $cookie = self::get_facebook_cookie();
  67.     if($cookie)
  68.     {
  69.         $ch = curl_init();
  70.         curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me?access_token='.$cookie['access_token']);
  71.         curl_setopt($ch, CURLOPT_PORT , 443);
  72.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  73.         $data = curl_exec($ch);
  74.         $user = json_decode($data);
  75.        
  76.         return object2array::convert($user);
  77.     }
  78.     return false;
  79.     }
  80.    
  81.     public static function login_button($perms =false)
  82.     {
  83.     self::init();
  84.     return "<fb:login-button perms=\"$perms\"></fb:login-button>";
  85.     }
  86.     public static function installed(){
  87.     self::init();
  88.    
  89.     if(!empty(self::$app_key) && !empty(self::$app_secret)) return true;
  90.    
  91.     return false;
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment