2be7e464 by Chris Boden

Updates on Facebook auth

1 parent b58e14b4
......@@ -12,7 +12,9 @@ const REG_METH_VALID_EMAIL = 2;
const FORGOT_METH_VALID_EMAIL = 1;
const FORGOT_METH_RAND_PASS = 2;
const ACTION_ACTIVATE = 'activate_account';
const ACTION_LOGIN = 'auth_login'; // probably don't need
const ACTION_LOGOUT = 'auth_logout'; // probably need, tell FB/etc to remove their cookies
const ACTION_ACTIVATE = 'auth_activate';
const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`)
......@@ -24,12 +26,12 @@ const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name
}
if (is_array(Vars::$options['third_party'])) {
foreach (Vars::$options['third_party'] as $tp => $on) {
if ($on) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . $tp . DIRECTORY_SEPARATOR . $tp . '.php');
foreach (Vars::$options['third_party'] as $tp => $on) {
if ($on) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . $tp . DIRECTORY_SEPARATOR . $tp . '.php');
}
}
}
}
});
/**
......
<?php
/**
* Note: If there is an inconsistent error
* it's due to how I changed the FB load
* process, may need to change how JS is loaded
* Proabably move FB.init and FB.Event.subscribe
* to my init method
*
* This needs to go in the <html tag
* xmlns:fb="http://www.facebook.com/2008/fbml"
*
......@@ -12,60 +18,94 @@
* API Key: 3bcccfd8c28c52197141266d9e417649
* App Secret: 9bfcd828bc6ccef12336dea57df93ecb
* App ID: 138943536118944
*
* Graph API Reference:
* http://developers.facebook.com/docs/reference/api/user
*/
namespace Tz\WordPress\Tools\Auth\Facebook;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\ShortCodes as SC;
use Tz\WordPress\Tools\Auth;
use InvalidArgumentException;
const OPTION_NAME = 'tz_auth_fb';
call_user_func(function() {
SC\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
Vars::$options = new Tools\WP_Option(OPTION_NAME);
Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login'));
Tools\add_actions(__NAMESPACE__ . '\Actions');
Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
}
});
function loadJSSDK() {
return '
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: \'' . Vars::$options['application_id'] . '\', status: true, cookie: true, xfbml: true});
FB.Event.subscribe(\'auth.login\', function(response) { window.location.reload(); });
};
(function() {
var e = document.createElement(\'script\'); e.async = true;
e.src = document.location.protocol + \'//connect.facebook.net/en_US/all.js\';
document.getElementById(\'fb-root\').appendChild(e);
}());
</script>
';
}
function drawLoginButton($echo = true) {
$btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] . '</span></a>';
if (!$echo) {
return $btn;
}
function drawLoginButton($value = 'Login') {
echo '<fb:login-button></fb:login-button>';
// echo '<a class="fb_button fb_button_medium"><span class="fb_button_text">' . $value . '</span></a>';
echo $btn;
}
class Vars {
public static $options = false;
class Actions {
public static function wp() {
global $post; // I want a better way to do this
if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php');
Vars::$sdk = new \FB\Facebook(Array(
'appId' => Vars::$options['application_id']
, 'secret' => Vars::$options['application_secret']
, 'cookie' => true
));
if (Vars::$sdk->getSession()) {
$info = Vars::$sdk->api('/me');
// get email, verify vs database
// register and/or login
}
}
}
public static function wp_enqueue_scripts() {
if (is_admin() || is_user_logged_in()) {
return;
}
_enqueue_script('facebook-all', 'http://connect.facebook.net/en_US/all.js');
_enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent'));
_localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
}
public static function get_footer() {
echo '<div id="fb-root"></div>';
}
}
class ShortCodes {
public static function fb_login_button() {
ob_start();
drawLoginButton();
$btn = ob_get_contents();
ob_end_clean();
if (Vars::$sdk->getSession()) {
ob_start();
print_r(Vars::$sdk->api('/me'));
$data = '<pre>' . ob_get_contents() . '</pre>';
ob_end_clean();
return $btn;
return $data;
} else {
return drawLoginButton(false);
}
}
}
class Vars {
public static $options;
public static $sdk;
}
?>
\ No newline at end of file
......
......@@ -10,7 +10,6 @@ const OPTION_SECTION_CRED = 'fb_creds';
const OPTION_SECTION_SEL = 'fb_opts';
call_user_func(function() {
Vars::$options = new Tools\WP_Option(Facebook\OPTION_NAME);
Tools\add_actions(__NAMESPACE__ . '\Actions');
});
......@@ -19,7 +18,6 @@ function validate($data) {
}
class Vars {
public static $options = false;
public static $data_permissions = Array('email', 'read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests');
}
......@@ -27,7 +25,7 @@ class Actions {
public static function admin_init() {
register_setting(Auth\Settings\OPTION_GROUP, Facebook\OPTION_NAME, __NAMESPACE__ . '\validate');
add_settings_section(OPTION_SECTION_CRED, 'Facebook Credentials', function(){}, Auth\Settings\ADMIN_PAGE);
add_settings_section(OPTION_SECTION_CRED, 'Facebook Credentials', function(){ echo '<p>You can retreive this information from your <a href="http://www.facebook.com/developers/apps.php?app_id=' . Facebook\Vars::$options['application_id'] . '">Facebook | Developers page</a></p>'; }, Auth\Settings\ADMIN_PAGE);
Tools\add_settings_fields(__NAMESPACE__ . '\Cred_Fields', Auth\Settings\ADMIN_PAGE, OPTION_SECTION_CRED);
add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE);
......@@ -39,21 +37,25 @@ class Actions {
class Cred_Fields {
public static function api_key() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />';
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" size="45" />';
}
public static function application_secret() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />';
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" size="45" />';
}
public static function application_id() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" />';
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />';
}
public static function button_title() {
echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />';
}
}
class Opt_Fields {
public function __call($fn, $args) {
echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Vars::$options['ext_perms'][$fn], false) . ' />';
echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Facebook\Vars::$options['ext_perms'][$fn], false) . ' />';
}
}
?>
\ No newline at end of file
......
......@@ -284,13 +284,7 @@ class Facebook
$cookieName = $this->getSessionCookieName();
if (isset($_COOKIE[$cookieName])) {
$session = array();
parse_str(trim(
get_magic_quotes_gpc()
? stripslashes($_COOKIE[$cookieName])
: $_COOKIE[$cookieName],
'"'
), $session);
$session = $this->validateSessionObject($session);
parse_str(trim(stripslashes($_COOKIE[$cookieName]), '"'), $session);
// write only if we need to delete a invalid session cookie
$write_cookie = empty($session);
}
......
window.fbAsyncInit = function() {
FB.init({appId: TzFBData.AppID, status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
};
var TzFB = function() {
var init = function() {
var oBtn = document.getElementById('TzFB');
if (oBtn) {
addEvent(oBtn, 'click', api.login);
}
}
var api = {
login: function() {
FB.login(function() {}, {perms: TzFBData.ext_perms});
}
};
addEvent(window, 'load', init);
return api;
}();
\ No newline at end of file
......@@ -8,6 +8,8 @@ Author: Tenzing
namespace Tz\WordPress\Tools;
use Tz\WordPress\Tools\ShortCodes;
use \ReflectionClass, \ReflectionMethod;
use \Exception;
......@@ -80,6 +82,10 @@ function add_filters($class) {
}
}
function add_shortcodes($class) {
ShortCodes\add_shortcodes($class);
}
function add_settings_fields($class, $page = 'general', $section = 'default') {
if (!class_exists($class)) {
throw new Exception("{$class} does not exist");
......