<?php

    
//PHP FacebookLogin Class by Sloenthran

    
class FacebookLogin
    
{
    
        var 
$PublicKey;
        var 
$PrivateKey;
        var 
$URL;
    
        function 
__construct($PublicKey$PrivateKey$URL)
        {
        
            
$this->PublicKey $PublicKey;
            
$this->PrivateKey $PrivateKey;
            
$this->URL $URL;
        
        }
        
        function 
GenerateURL()
        {
        
            return 
"https://www.facebook.com/dialog/oauth?client_id=".$this->PublicKey."&redirect_uri=".$this->URL."&scope=email";
        
        }
        
        function 
Login()
        {
        
            global 
$_REQUEST;
            
            
$cURL curl_init('https://graph.facebook.com/oauth/access_token');
        
            
curl_setopt($cURLCURLOPT_RETURNTRANSFER1);
            
curl_setopt($cURLCURLOPT_CUSTOMREQUEST'POST');
            
curl_setopt($cURLCURLOPT_POST1);
            
curl_setopt($cURLCURLOPT_POSTFIELDS'client_id='.$this->PublicKey.'&redirect_uri='.$this->URL.'&client_secret='.$this->PrivateKey.'&code='.$_REQUEST['code'].'');

            
$Query curl_exec($cURL);
        
            
curl_close($cURL);
   
            
$Data explode('&'$Query);
    
            foreach(
$Data as $Key)
            {
            
                
$Item explode('='$Key);
                
$GiveKey[$Item[0]] = $Item[1];
        
            }
            
            
$cURL curl_init('https://graph.facebook.com/me?access_token='.$GiveKey['access_token'].'');
    
            
curl_setopt($cURLCURLOPT_RETURNTRANSFER1);
            
curl_setopt($cURLCURLOPT_CUSTOMREQUEST'GET');
   
            
$Query curl_exec($cURL);
    
            
curl_close($cURL);
   
            
$Data json_decode($Query);
    
            return 
$Data->{'email'};
            
        }
    
    }
    
?>