<?php
# system\applicaton\libraries\Facebook.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Facebook
{
function Facebook()
{
}
function get_facebook_cookie()
{
$args = array();
parse_str(trim($_COOKIE['fbs_' . FACEBOOK_APP_ID], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value)
{
if ($key != 'sig')
{
$payload .= $key . '=' . $value;
}
}
if (md5($payload . FACEBOOK_SECRET) != $args['sig'])
{
return null;
}
return $args;
}
}
?>
<?php
# system\application\helpers\header_helper.php
function prepare_header($t)
{
// Get the Facebook cookie
$cookie = $t->facebook->get_facebook_cookie();
if($cookie === NULL)
return;
// Get the decoded JSON object
$user = $t->user->get( $cookie['uid'] );
if($user == false)
{
// The JSON wasn't found in the database, so get it from Facebook | TODO: Also fetch from FB is the data is too old.
$json = $t->user->getJSON( $cookie['access_token'] );
// Update user in database from JSON
$t->user->update($json);
// Get user from database | TODO: I believe we can save a DB call by just doing: $user = json_decode($json);
$user = $t->user->get( $cookie['uid'] );
}
return $user;
}
?>
<?php
# system\application\config\Facebook.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define('FACEBOOK_APP_ID', 'YOUR_APP_ID'); //TODO: Enter your app ID
define('FACEBOOK_SECRET', 'YOUR_SECRET_KEY'); //TODO: Enter your secret key
?>
<?php
# system\application\models\user.php
class User extends Model
{
function User()
{
parent::Model();
}
/**
* Gets the JSON file string for the logged in user with the access token [from the cookie].
* See http://developers.facebook.com/docs/api for more info on the Graph API.
*/
function getJSON($access_token)
{
if($access_token == "")
{
throw new Exception("Access token may not be blank."); // TODO: Figure out causes. Did you clear your cookies?
}
return file_get_contents('https://graph.facebook.com/me?access_token=' . $access_token);
}
/**
* Given the JSON, updates the user in database. If the user does not exist, the user is added.
*/
function update($json)
{
$decoded = json_decode($json); // object
if($decoded === NULL)
{
throw new Exception("Error decoding JSON file. Last error: " . json_last_error() . "\n" . $json);
}
$this->db->where('uid', $decoded->id);
$query = $this->db->get('users');
if($query->num_rows() == 0)
{
$data = array(
'uid' => $decoded->id,
'name' => $decoded->name,
'json' => $json
);
$this->db->insert('users', $data);
}
else
{
$this->db->where('uid', $decoded->id);
$this->db->update('users', $data);
}
}
/**
* Gets the stored JSON from the database and returns it as a decoded object.
*/
function get($uid)
{
$this->db->where('uid', $uid);
$query = $this->db->get('users');
if($query->num_rows() == 0)
{
return false;
}
else
{
return json_decode($query->row()->json);
}
}
/**
* Gets the user role
*/
function get_role($uid)
{
$this->db->where('uid', $uid);
$query = $this->db->get('users');
if($query->num_rows() == 0)
return false;
else
return $query->row()->role;
}
}
?>
<!-- system\application\views\_header.php -->
<!-- Header for every page -->
<?php
/*
* This expects a user parameter (decoded JSON)
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Site</title>
</head>
<body>
<div class="container">
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
<?php
if ($id) {
echo '<p>Welcome ' . $name . '</p>';
} else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<!-- Your footer would close out these divs. You can figure that out. -->
<?php
/* MySQL for the user table.
CREATE TABLE IF NOT EXISTS `users` (
`uid` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`json` text NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
*/