View source
<?php
define("SIMPLE_FB_CONNECT_DEFAULT_DIMENSIONS_STRING", "1024x1024");
define("SIMPLE_FB_CONNECT_DEFAULT_WIDTH", 1024);
define("SIMPLE_FB_CONNECT_DEFAULT_HEIGHT", 1024);
define("SIMPLE_FB_CONNECT_PERMISSION_DENIED_PARAMETER", 'access_denied');
function simple_fb_connect_menu() {
$items['user/simple-fb-connect'] = array(
'title' => (variable_get('simple_fb_connect_login_only', 0) == 0 ? 'Register/' : '') . 'Login with FB',
'page callback' => 'simple_fb_connect_login',
'access callback' => 'user_is_anonymous',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/people/simple-fb-connect'] = array(
'title' => 'Simple FB Connect Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'simple_fb_connect_api_keys_settings',
),
'access arguments' => array(
'administer simple fb',
),
'file' => 'simple_fb_connect.admin.inc',
);
return $items;
}
function simple_fb_connect_permission() {
$items = array();
$items['administer simple fb'] = array(
'title' => "Administer Simple Facebook Connect Module settings",
);
return $items;
}
function simple_fb_connect_login() {
$facebook = facebook_client();
$fb_user = $facebook
->getUser();
if ($fb_user) {
$fb_user_profile = $facebook
->api('/me');
if (isset($fb_user_profile['email'])) {
$query = db_select('users', 'u');
$query
->condition('u.mail', check_plain($fb_user_profile['email']));
$query
->fields('u', array(
'uid',
));
$query
->range(0, 1);
$drupal_user_id = 0;
$result = $query
->execute();
foreach ($result as $record) {
$drupal_user_id = $record->uid;
}
if ($drupal_user_id) {
$user_obj = user_load($drupal_user_id);
if ($user_obj->status) {
$form_state = array();
$form_state['uid'] = $user_obj->uid;
user_login_submit(array(), $form_state);
drupal_set_message(t('You have been logged in with the username !username', array(
'!username' => $user_obj->name,
)));
drupal_goto(variable_get('simple_fb_connect_post_login_url', 'user'));
}
else {
drupal_set_message(t('You could not be logged in as your account is blocked. Contact site administrator.'), 'error');
drupal_goto('user');
}
}
else {
if (!variable_get('simple_fb_connect_login_only', 0)) {
$fb_username = isset($fb_user_profile['username']) ? $fb_user_profile['username'] : $fb_user_profile['name'];
$drupal_username_generated = simple_fb_connect_unique_user_name(check_plain($fb_username));
$password = user_password(8);
$fields = array(
'name' => $drupal_username_generated,
'mail' => check_plain($fb_user_profile['email']),
'pass' => $password,
'status' => 1,
'init' => 'email address',
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
);
if (variable_get('user_pictures', 0)) {
$dimensions_in_text = variable_get('user_picture_dimensions', SIMPLE_FB_CONNECT_DEFAULT_DIMENSIONS_STRING);
$dimensions = explode('x', $dimensions_in_text);
if (count($dimensions) == 2) {
$width = $dimensions[0];
$height = $dimensions[1];
}
else {
$width = SIMPLE_FB_CONNECT_DEFAULT_WIDTH;
$height = SIMPLE_FB_CONNECT_DEFAULT_HEIGHT;
}
$pic_url = "https://graph.facebook.com/" . check_plain($fb_user_profile['id']) . "/picture?width={$width}&height={$height}";
$response = drupal_http_request($pic_url);
$file = 0;
if ($response->code == 200) {
$picture_directory = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures/');
file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
$file = file_save_data($response->data, $picture_directory . '/' . check_plain($fb_user_profile['id'] . '.jpg', FILE_EXISTS_RENAME));
}
if (is_object($file)) {
$fields['picture'] = $file->fid;
}
}
drupal_alter("simple_fb_connect_register", $fields, $fb_user_profile);
$account = user_save('', $fields);
$account->password = $fields['pass'];
drupal_mail('user', 'register_no_approval_required', $account->mail, NULL, array(
'account' => $account,
), variable_get('site_mail', 'admin@drupalsite.com'));
drupal_set_message(t('You have been registered with the username !username', array(
'!username' => $account->name,
)));
drupal_goto("user/simple-fb-connect");
}
else {
drupal_set_message(t('There was no account with the email addresse !email found. Please register before trying to login.', array(
'!email' => check_plain($fb_user_profile['email']),
)), 'error');
drupal_goto("user");
}
}
}
else {
drupal_set_message(t('Though you have authorised the Facebook app to access your profile, you have revoked the permission to access email address. Please contact site administrator.'), 'error');
drupal_goto("user");
}
}
else {
if (!isset($_REQUEST['error'])) {
if (variable_get('simple_fb_connect_appid', 0)) {
$scope_string = '';
if (sizeof(module_implements('simple_fb_scope_info')) > 0) {
$scopes = module_invoke_all('simple_fb_scope_info', array());
$scope_string = implode(',', $scopes);
}
$scope_string .= ',email';
$login_url_params = array(
'scope' => $scope_string,
'fbconnect' => 1,
'redirect_uri' => 'http://' . $_SERVER['HTTP_HOST'] . request_uri(),
);
$login_url = $facebook
->getLoginUrl($login_url_params);
drupal_goto($login_url);
}
else {
drupal_set_message(t('Facebook App ID Missing. Can not perform Login now. Contact Site administrator.'), 'error');
drupal_goto("user");
}
}
else {
if ($_REQUEST['error'] == SIMPLE_FB_CONNECT_PERMISSION_DENIED_PARAMETER) {
drupal_set_message(t('Could not login with facebook. You did not grant permission for this app on facebook to access your email address.'), 'error');
}
else {
drupal_set_message(t('There was a problem in logging in with facebook. Contact site administrator.'), 'error');
}
drupal_goto('user');
}
}
}
function simple_fb_connect_libraries_info() {
$libraries['facebook-php-sdk'] = array(
'name' => 'Facebook PHP SDK',
'vendor url' => 'https://github.com/facebook/facebook-php-sdk',
'download url' => 'https://github.com/facebook/facebook-php-sdk/archive/v3.2.3.tar.gz',
'version arguments' => array(
'file' => 'readme.md',
'pattern' => '/Facebook PHP SDK \\(v\\.(3\\.\\d\\.\\d)\\)/',
'lines' => 25,
),
'versions' => array(
'3.2.2' => array(
'files' => array(
'php' => array(
'src/base_facebook.php',
'src/facebook.php',
),
),
),
),
);
return $libraries;
}
function simple_fb_connect_facebook_client_load_include() {
if (!class_exists('Facebook')) {
if (function_exists('libraries_load')) {
$library = libraries_load('facebook-php-sdk');
}
else {
$sdk_path = DRUPAL_ROOT . '/sites/all/libraries/facebook-php-sdk/src/facebook.php';
$library = array(
'loaded' => file_exists($sdk_path),
);
if ($library['loaded']) {
require_once $sdk_path;
}
}
if (!$library['loaded']) {
watchdog('simple_fb_connect', 'Unable to load the required Facebook library');
drupal_set_message(t('Facebook Library not found - ' . $library['error message']), 'error');
}
}
return class_exists('Facebook') && defined('Facebook::VERSION');
}
function facebook_client() {
static $fb = NULL;
if (is_null($fb)) {
if (simple_fb_connect_facebook_client_load_include()) {
$init_params = array(
'appId' => variable_get('simple_fb_connect_appid', ''),
'secret' => variable_get('simple_fb_connect_skey', ''),
);
$fb = new Facebook($init_params);
}
}
return $fb;
}
function simple_fb_connect_unique_user_name($fb_name, $i = 0) {
$trimmed_name = '';
$user_to_load = '';
if ($i == 0) {
$trimmed_name = strtolower(trim(str_replace(' ', '_', $fb_name)));
$user_to_load = $trimmed_name;
}
else {
$trimmed_name = $fb_name;
$user_to_load = $trimmed_name . "_" . $i;
}
if (is_object(user_load_by_name($user_to_load))) {
$i++;
return simple_fb_connect_unique_user_name($trimmed_name, $i);
}
else {
return $user_to_load;
}
}