<?php
/*
    Proof of concept for "Sign in with Facebook" (see http://developers.facebook.com/docs/authentication/).

    Requires PHP 5 with the curl extension (see http://www.php.net/manual/en/book.curl.php).

    Feel free to copy and modify. No guarantees, things may break, use at your own risk.

    tim@strehle.de
    http://www.strehle.de/tim/
*/

/* 
    The file included below contains the consumer key and secret for the app I registered with Facebook.
    Register your own at: https://developers.facebook.com/apps
    
    It looks like this:

    <?php
    define('FACEBOOK_APP_ID', 'my_key');
    define('FACEBOOK_APP_SECRET', 'my_secret');
    ?>
*/

require_once('../facebook_auth_demo_secrets.inc.php');

session_name('facebook_auth_demo');
session_start();

/* API URLs */
define('FACEBOOK_AUTHORIZE_URL''https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s');
define('FACEBOOK_ACCESS_TOKEN_URL''https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s');
define('FACEBOOK_GRAPH_URL''https://graph.facebook.com/me?access_token=%s');

$callback_uri 'http://www.strehle.de/facebook_auth_demo.php';

if (! empty(
$_REQUEST'code' ]))
{    
    
// Second step: Facebook redirected to us, validate and login here

    
$auth_url sprintf
    
(
        
FACEBOOK_ACCESS_TOKEN_URL,
        
FACEBOOK_APP_ID,
        
urlencode($callback_uri),
        
FACEBOOK_APP_SECRET,
        
urlencode($_REQUEST'code' ])
    );

    
$ch curl_init();
    
curl_setopt($chCURLOPT_URL$auth_url);
    
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
    
$content curl_exec($ch);
    
$httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
    
curl_close($ch);
   
    if (
$httpCode === 200)
    {
        
parse_str($content$data);

        
$graph_url sprintf(FACEBOOK_GRAPH_URLurlencode($data'access_token' ]));
        
$user_info_json file_get_contents($graph_url);
        
$user_info json_decode($user_info_json);
    
        
$html =
            
'<p>This is all the information Facebook sends to my site (don\'t worry, I won\'t track or use any of it):</p>'
            
'<pre>' htmlspecialchars(print_r($user_infotrue)) . '</pre>';
    
        
htmlOut($html);
    }
    else
    {
        
htmlOut(sprintf('<p>An error occurred: HTTP %s - %s</p>'htmlspecialchars($http_code), htmlspecialchars($content)));
    }
}
elseif (! empty(
$_REQUEST'error' ]))
{
    
htmlOut('<p>An error occurred: ' htmlspecialchars($_REQUEST'error' ]) . '</p>');
}
else
{
    
// First step: Redirect to Facebook
   
    
$redirect_url sprintf
    
(
        
FACEBOOK_AUTHORIZE_URL,
        
FACEBOOK_APP_ID,
        
urlencode($callback_uri)
    );

    
header('Location: ' $redirect_url);
}


function 
htmlOut($html)
{
    
header('Content-Type: text/html; charset=UTF-8');

    
?>
    <html><head><title>Tim Strehle's "Sign in with Facebook" PHP OAuth Demo</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head><body><h3>Tim Strehle's "Sign in with Facebook" PHP OAuth Demo</h3>
    <?php echo $html?>
    <a href="facebook_auth_demo.phps">PHP source code for this demo</a>
    <br /><a href="auth_demo.html">"Sign in with ..." start page</a>
    <br /><a href="/tim/">Home</a>
    <br /><a href="http://developers.facebook.com/docs/authentication/">Facebook developer documentation</a>
   </body></html>

   <?php
}

?>