=')) {
return;
}
}
deactivate_plugins(basename(__FILE__)); // Deactivate ourself
wp_die("The base SFC plugin must be activated before this plugin will run.");
}
register_activation_hook(__FILE__, 'sfc_login_activation_check');
// add the section on the user profile page
add_action('profile_personal_options','sfc_login_profile_page');
function sfc_login_profile_page($profile) {
$options = get_option('sfc_options');
?>
ID, 'fbuid');
if (empty($fbuid)) {
?>
Connect this WordPress account to Facebook
Connected as
.
user_email);
// load facebook platform
include_once 'facebook-platform/facebook.php';
$fb=new Facebook($options['api_key'], $options['app_secret']);
// user ids can be bigger than 32 bits, but are all digits
$fbuid = trim($_POST['fbuid']);
if(!preg_match('/^[0-9]+$/i', $fbuid)) {
$fbuid = 0;
}
if ($fbuid) {
// verify that users WP email address is a match to the FB email address (for security reasons)
$aa[0]['email_hash'] = $hash;
$aa[0]['account_id'] = $user->ID;
$ret = $fb->api_client->connect_registerUsers(json_encode($aa));
if (empty($ret)) {
// return value is empty, not good
echo 'Facebook did not know your email address.';
exit();
} else {
// now we check to see if that user gives the email_hash back to us
$user_details = $fb->api_client->users_getInfo($fbuid, array('email_hashes'));
if (!empty($user_details[0]['email_hashes'])) {
// go through the hashes returned by getInfo, make sure the one we want is in them
$valid = false;
foreach($user_details[0]['email_hashes'] as $check) {
if ($check == $hash) $valid = true;
}
if (!$valid) {
// no good
echo 'Facebook could not confirm your email address.';
exit();
}
}
}
} else {
if (!SFC_ALLOW_DISCONNECT) {
// disconnect not allowed
echo 1;
exit();
}
// user disconnecting, so disconnect them in FB too
$aa[0] = $hash;
$ret = $fb->api_client->connect_unregisterUsers(json_encode($aa));
// we could check here, but why bother? just assume it worked.
}
update_usermeta($user->ID, 'fbuid', $fbuid);
echo 1;
exit();
}
// computes facebook's email hash thingy. See http://wiki.developers.facebook.com/index.php/Connect.registerUsers
function sfc_login_fb_hash_email($email) {
$email = strtolower(trim($email));
$c = crc32($email);
$m = md5($email);
$fbhash = sprintf('%u_%s',$c,$m);
return $fbhash;
}
add_action('login_form','sfc_login_add_login_button');
function sfc_login_add_login_button() {
global $action;
?>
Connect with Facebook ';
}
add_filter('authenticate','sfc_login_check',90);
function sfc_login_check($user) {
if ( is_a($user, 'WP_User') ) { return $user; } // check if user is already logged in, skip FB stuff
$options = get_option('sfc_options');
// load facebook platform
include_once 'facebook-platform/facebook.php';
$fb=new Facebook($options['api_key'], $options['app_secret']);
$fbuid=$fb->get_loggedin_user();
if($fbuid):
try {
$test = $fb->api_client->fql_query('SELECT uid, pic_square, first_name FROM user WHERE uid = ' . $fbuid);
if ($test) {
global $wpdb;
$user_id = $wpdb->get_var( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'fbuid' AND meta_value = %s", $fbuid) );
if ($user_id) {
$user = new WP_User($user_id);
} else {
do_action('sfc_login_new_fb_user',$fb); // hook for creating new users if desired
global $error;
$error = 'ERROR: Facebook user not recognized.';
}
}
} catch (Exception $ex) {
$fb->clear_cookie_state();
}
endif;
return $user;
}
add_action('wp_logout','sfc_login_logout');
function sfc_login_logout() {
$options = get_option('sfc_options');
// load facebook platform
include_once 'facebook-platform/facebook.php';
$fb=new Facebook($options['api_key'], $options['app_secret']);
$fbuid=$fb->get_loggedin_user();
if ($fbuid) {
$fb->logout(wp_login_url().'?loggedout=true');
}
}
add_action('login_head','sfc_login_featureloader');
function sfc_login_featureloader() {
if ($_SERVER['HTTPS'] == 'on')
echo "";
else
echo "";
}
add_action('login_form','sfc_add_base_js');
/*
// generate facebook avatar code for users who login with Facebook
// NOTE: This overrides Gravatar.
//
add_filter('get_avatar','sfc_login_avatar', 10, 5);
function sfc_login_avatar($avatar, $id_or_email, $size = '96', $default = '', $alt = false) {
// check to be sure this is for a user id
if ( !is_numeric($id_or_email) ) return $avatar;
$fbuid = get_usermeta( $id_or_email, 'fbuid');
if ($fbuid) {
// return the avatar code
return "";
}
return $avatar;
}
*/