<?php
// $Id: facebook_auth.module,v 1.9 2007/09/06 07:25:03 halkeye Exp $
if (substr(phpversion(), 0, 1) == '4') {
   if (file_exists(dirname(__FILE__) . '/facebook-platform/php4client/facebook.php')) {
      require_once('facebook-platform/php4client/facebook.php');
   }
   else {
      drupal_set_message('Platform needs to be installed. See readme file');
   }
}
else {
   if (file_exists(dirname(__FILE__) . '/facebook-platform/client/facebook.php')) {
      require_once('facebook-platform/client/facebook.php');
   }
   else {
      drupal_set_message('Platform needs to be installed. See readme file');
   }
}

function facebook_auth_help($section) {
   switch($section) {
      case 'admin/settings':
         return t('Configure your API and Secret Key provided by facebook.');
      case 'admin/settings/facebook_auth':
         return t('Sign up for a new application at !url. Fill in provided keys here.',
            array('!url'=>l(t('facebook developer page'), 'http://www.facebook.com/developers/'))
         );
   }
}

function facebook_auth_menu($may_cache) {
   global $user;

   $items = array();
   if ($may_cache) {
      $items[] = array(
         'path' => 'admin/settings/facebook_auth',
         'title' => t('Configure Facebook settings'),
         'callback' => 'drupal_get_form',
         'callback arguments' => 'facebook_auth_admin',
         'access' => user_access('administer facebook'),
         'type' => MENU_NORMAL_ITEM,
      );
      $items[] = array(
         'path' => 'facebook',
         'title' => t('my Facebook account'),
         'callback' => 'facebook_auth',
         // There's no real reason to have this disallowed unless you don't want facebook auth, in that case, you should
         // Probably just disable the module
         'access' => TRUE,
         'type' => MENU_CALLBACK
      );

   }
   else {
      if (arg(0) == 'user' && is_numeric(arg(1))) {
         $account = user_load(array('uid' => arg(1)));

         if ($account->uid) {
            global $user;

            $access = (user_access('administer users') || $user->uid == arg(1));
            $items[] = array(
               'path' => 'user/'. arg(1) .'/facebook_auth',
               'title' => t('Facebook Identities'),
               'callback' => 'facebook_auth_user_identities',
               'callback arguments' => array($account),
               'access' => $access,
               'type' => MENU_LOCAL_TASK
            );
            $items[] = array(
               'path' => 'user/'. arg(1) .'/facebook_auth/delete',
               'title' => t('Delete Facebook'),
               'callback' => 'facebook_auth_user_delete',
               'callback arguments' => array($account),
               'access' => $access,
               'type' => MENU_CALLBACK
            );
         }
      }
   }
   return $items;
}

function facebook_auth_perm() { 
   return array('administer facebook');
}

function facebook_auth() {
   global $user;
   $api_key = variable_get('facebook_apikey', '');
   $secret = variable_get('facebook_secretkey', '');
   // initiate class
   $facebook = new Facebook($api_key, $secret);
   $u = $facebook->require_login();

   # http://developers.facebook.com/documentation.php?v=1.0&method=users.getInfo
   if ($facebook->api_client->error_code) {
      return t('Unable to load profile from facebook');
   }
   $info = $facebook->api_client->users_getInfo($u, array('first_name', 'last_name'));
   $username = $info[0]['first_name'] .' '. $info[0]['last_name'];

   // user should be authenticated by Facebook now
   $account = user_external_load($u .'@facebook');
   if ($user->uid && $account) {
      return t('Account has already been associated with an account.');
   }
   else if ($user->uid && !$account) {
      return drupal_get_form('facebook_auth_user_assoc');
   }

   
   // If we don't have a user, register them
   if (!$account->uid) {
      // WS: Bug fix...check there is not a user who has this username but is not yet attached to facebook
      // attach them if they do exist
      $searchuser->name = $username;
      $existing = user_load($searchuser);
      if ($existing) {
      	// update users facebook entry
      	$user = $existing;
      	$user_change = array(
      		'authname_facebook_auth' => $u .'@facebook',
      		);
      	$user = user_save($user, $user_change);
      	watchdog('user', 'assigned to facebook user: '. $user->name .' (FB)', l(t('edit user'), 'admin/user/edit/'. $user->uid));      	
      } else {
      	// Create account
	$user_default = array(
		'name' => $username,
		'pass' => user_password(),
		'init' => db_escape_string($username),
		'status' => 1,
		'authname_facebook_auth' => $u .'@facebook',
	);
	$user_default['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';

	$user = user_save('', $user_default);
	watchdog('user', 'new user: '. $user->name .' (FB)', l(t('edit user'), 'admin/user/edit/'. $user->uid));
      }
	// WS: End patch
      if (($user->uid) && ($user->uid > 0)) {
	drupal_goto('user/'. $user->uid .'/edit');
      }
   }
   else {
      $user = $account;
   }

   // final check
   if ($user->uid && $user->uid > 0) {
      drupal_set_message('Logged in with Facebook as '. $user->name .'.');
      return drupal_goto('<front>');
   }
   # Its not going to get here if all goes well
   # TODO: add some error handling here if it fails everything else
   $page_content = '<H1>Hello '. $username .'</H1>';
   return $page_content;
}


function facebook_auth_admin() {

   $form['facebook_apikey'] = array(
      '#type' => 'textfield',
      '#title' => t('API key'),
      '#default_value' => variable_get('facebook_apikey', ''),
   );
   $form['facebook_secretkey'] = array(
      '#type' => 'textfield',
      '#title' => t('Secret key'),
      '#default_value' => variable_get('facebook_secretkey', ''),
   );
   $form['facebook_auth_enable_loginblock'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show button in login block'),
      '#description' => t('Enable if you want to include a button to login with facebook under the normal login forum.'),
      '#default_value' => variable_get('facebook_auth_enable_loginblock', FALSE),
   );

   return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter : adds Facebook login to the login forms.
 */
function facebook_auth_form_alter($form_id, &$form) {
   $api_key = variable_get('facebook_apikey', '');
   $secret = variable_get('facebook_secretkey', '');
   if (!$secret || !$api_key) { return; }
   if (!variable_get('facebook_auth_enable_loginblock', FALSE)) { return; }
   if ($form_id == 'user_login_block' || $form_id == 'user_login') {
      $img = theme('image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE);
      $form['facebook_link'] = array(
         '#type' => 'markup',
         '#value' => l($img, 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE),
      );
   }
   return $form;
}

function facebook_auth_block($op = 'list', $delta = 0, $edit = array()) {
   global $user;
   if ($op == 'list') {
      $blocks[0] = array(
         'info' => t('Facebook Login Button'),
         'weight' => 0,
         'enabled' => 0, 
         'region' => 'left'
      );
      return $blocks;
   }
   else if ($op == 'view') {
      switch($delta) {
      case 0:
         if ($user && $user->uid) { return; }
         $block = array(
            'subject' => t('Login with facebook'),
            'content' => l(theme('image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE), 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE),
         );
         break;
      }
   return $block;
   }

} 

function facebook_auth_user_assoc() {
   global $user;
   $api_key = variable_get('facebook_apikey', '');
   $secret = variable_get('facebook_secretkey', '');
   // initiate class
   $facebook = new Facebook($api_key, $secret);
   $u = $facebook->require_login();
   $form = array();
   $form['fb_uid'] = array('#type' => 'value', '#value' => $u);
   return confirm_form(
      $form,
      t('Are you sure you want to link %fbname to %username?',array('%fbname' => $username, '%username' => $user->name)),
      '<front>', // Not sure what to provide here
      NULL,
      t('Link'),
      t('Cancel')
   );
}

function facebook_auth_user_assoc_submit($form_id, $form_values) {
   global $user;
   user_save($user, array('auth_facebook_auth' => $form_values['fb_uid'] . '@facebook' ), '');
   return drupal_goto('user/'. $user->uid .'/edit');
}

function facebook_auth_user_identities($account) {
   $header = array(t('Facebook'), t('Operations'));
   $rows = array();

   $result = db_query("SELECT * FROM {authmap} WHERE module='facebook_auth' AND uid=%d", $account->uid);
   while ($identity = db_fetch_object($result)) {
      list($userId) = explode('@',$identity->authname);
      $rows[] = array(l($userId,'http://www.facebook.com/profile.php?id='.$userId), l(t('Delete'), 'user/'. $account->uid .'/facebook_auth/delete/'. $identity->aid));
   }

   $output = theme('table', $header, $rows);
   $output .= l(theme('image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE), 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE);
   
   return $output;
}

function facebook_auth_user_delete($account, $aid = 0) {
  db_query("DELETE FROM {authmap} WHERE uid=%d AND aid=%d AND module='facebook_auth'", $account->uid, $aid);
  if (db_affected_rows()) {
    drupal_set_message(t('Facebook association deleted.'));
  }
  drupal_goto('user/'. $account->uid .'/facebook_auth');
}
