Index: fbconnect.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/fbconnect/fbconnect.info,v
retrieving revision 1.2
diff -u -r1.2 fbconnect.info
--- fbconnect.info	26 Jan 2009 14:08:35 -0000	1.2
+++ fbconnect.info	4 Feb 2009 23:58:32 -0000
@@ -1,13 +1,2 @@
-;$Id: fbconnect.info,v 1.2 2009/01/26 14:08:35 lanets Exp $
-name= Fbconnect
-description = Allows users to connect with Facebook account
-core = 6.x
-package = "Facebook connect"
-php = 5.2
-
-; Information added by drupal.org packaging script on 2009-01-22
-version = "6.x-1.0-beta5"
-core = "6.x"
-project = "fbconnect"
-datestamp = "1232636122"
-
+name = Facebook connect
+description = "Allow facebook users to login with their info"
Index: fbconnect.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/fbconnect/fbconnect.install,v
retrieving revision 1.3
diff -u -r1.3 fbconnect.install
--- fbconnect.install	22 Jan 2009 14:34:31 -0000	1.3
+++ fbconnect.install	4 Feb 2009 23:58:32 -0000
@@ -1,48 +1,84 @@
-<?php
-// $Id: fbconnect.install,v 1.3 2009/01/22 14:34:31 lanets Exp $
-
-/**
- * @file
- * fbconnect install uninstall
- */
+<?php 
+ // $Id$
 
 /** 
  * Implementation of hook_install(). 
  */ 
 function fbconnect_install() {
-  //Use schema API to create database table. 
-  drupal_install_schema('fbconnect');
-  // Facebook user table fields
-  $facebook_fields['options'] = array(
-    'name' => t('Facebook name'),
-    'affiliations' => t('Affiliations'),
-    'religion' => t('Religious Views'),
-    'birthday' => t('Birthday'),
-    'sex' => t('Sex'),
-    'about_me' => t('About me'),
-    'hometown_location' => t('Hometown location'),
-    'current_location' => t('Current location'),
-    'meeting_sex' => t('Meeting sex'),
-    'meeting_for' => t('Meeting for'),
-    'relationship_status' => t('Relationship status'),
-    'political' => t('Political'),
-    'activities' => t('Activities'),
-    'interests' => t('Interests'),
-    'music' => t('Favourite Music'),
-    'tv' => t('Favourite TV'),
-    'movies' => t('Favourite Films'),
-    'books' => t('Favourite Books'),
-    'quotes' => t('Favourite Quotations'),
-  );   
-  variable_set('facebook_user_fields', $facebook_fields['options']);
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      db_query("CREATE TABLE {fbconnect_users} (
+        uid int NOT NULL default 0,
+        fbuid int NOT NULL default 0,
+        timestamp int NOT NULL default 0,
+        import_setting text,
+        avatar tinyint NOT NULL default 1,
+        visibility tinyint NOT NULL default 1,
+        PRIMARY KEY (fbuid)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;"
+      );
+      
+      db_query("CREATE TABLE IF NOT EXISTS {fbconnect_profile} (
+        uid int(10) unsigned NOT NULL default '0',
+        name varchar(60) NOT NULL default '',
+        religion varchar(255) NOT NULL default '',
+        birthday varchar(30) NOT NULL default '',
+        sex varchar(10) NOT NULL default '',
+        relationship_status varchar(30) NOT NULL default '',
+        political varchar(50) NOT NULL default '',
+        activities text,
+        interests text,
+        music text,
+        tv text,
+        movies text,
+        books text,
+        quotes text,
+        about_me text,
+        affiliations mediumtext,
+        current_location text,
+        hometown_location text,
+        meeting_sex text,
+        meeting_for text,
+        PRIMARY KEY  (uid) 
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;"
+      );
+      
+      // Facebook user table fields
+      $facebook_fields['options'] = array(
+        'name' => t('Facebook name'),
+        'affiliations' => t('Affiliations'),
+        'religion' => t('Religious Views'),
+        'birthday' => t('Birthday'),
+        'sex' => t('Sex'),
+        'about_me' => t('About me'),
+        'hometown_location' => t('Hometown location'),
+        'current_location' => t('Current location'),
+        'meeting_sex' => t('Meeting sex'),
+        'meeting_for' => t('Meeting for'),
+        'relationship_status' => t('Relationship status'),
+        'political' => t('Political'),
+        'activities' => t('Activities'),
+        'interests' => t('Interests'),
+        'music' => t('Favourite Music'),
+        'tv' => t('Favourite TV'),
+        'movies' => t('Favourite Films'),
+        'books' => t('Favourite Books'),
+        'quotes' => t('Favourite Quotations'),
+      );   
+      variable_set('facebook_user_fields', $facebook_fields['options']);
+      
+      break;
+  }
 }
- 
+
 /** 
- * Implementation of hook_uninstall(). 
+ * Implementation of hook_install(). 
  */ 
-function fbconnect_uninstall() { 
-  //Use schema API to delete database table. 
-  drupal_uninstall_schema('fbconnect');
+function fbconnect_uninstall() {
+  db_query("DROP TABLE {fbconnect_users}");
+  db_query("DROP TABLE {fbconnect_profile}");
+  
   //Delete our module's variable from the variables table. 
   variable_del('fbconnect_api_key');
   variable_del('fbconnect_secret_api_key');
@@ -56,178 +92,6 @@
   variable_del('fbconnect_import');
   variable_del('fbconnect_field_to_import');
   variable_del('facebook_user_fields');
+  
 }
 
-/** 
- * Implementation of hook_schema(). 
- */ 
-function fbconnect_schema() { 
-  $schema['fbconnect_users'] = array( 
-    'description' => t('Stores users Drupal & Facebook UID.'), 
-      'fields' => array( 
-      'uid' => array( 
-        'type' => 'int', 
-        'unsigned' => TRUE, 
-        'not null' => TRUE, 
-        'default' => 0, 
-        'description' => t('The drupal uid.'),
-      ), 
-      'fbuid' => array( 
-        'type' => 'int', 
-        'unsigned' => TRUE, 
-        'not null' => TRUE, 
-        'default' => 0, 
-        'description' => t('The facebook uid.'),
-        ),
-      'timestamp' => array( 
-        'type' => 'int', 
-        'unsigned' => TRUE, 
-        'not null' => TRUE, 
-        'default' => 0, 
-        ),
-        'import_setting' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-        ),
-        'avatar' => array(
-          'type' => 'int', 
-          'unsigned' => TRUE, 
-          'not null' => TRUE,
-          'size' => 'tiny',
-          'default' => 1,
-        ),
-        'visibility' => array( 
-          'type' => 'int', 
-          'unsigned' => TRUE, 
-          'not null' => TRUE,
-          'size' => 'tiny',
-          'default' => 1, 
-        ),
-      ), 
-      'primary key' => array( 
-      'fbuid' 
-    ), 
-  );
-
-  $schema['fbconnect_profile'] = array( 
-    'description' => t('Stores users profile information imported form facebook.'), 
-      'fields' => array( 
-        'uid' => array( 
-          'type' => 'int', 
-          'unsigned' => TRUE, 
-          'not null' => TRUE, 
-          'default' => 0, 
-          'description' => t('The drupal uid.'),
-        ),
-        'name' => array( 
-          'type' => 'varchar',
-          'length' => 60, 
-          'not null' => TRUE, 
-          'default' => '', 
-          'description' => t('The full name of the user'),
-        ),
-        'religion' => array(
-          'type' => 'varchar',
-          'length' => 255,
-          'not null' => TRUE,
-          'default' => '',
-          'description' => t('The religion of the user'),
-        ),
-        'birthday' => array(
-          'type' => 'varchar',
-          'length' => 30,
-          'not null' => TRUE,
-          'default' => '',
-          'description' => t('The birthday of the user'),
-        ),
-        'sex' => array(
-          'type' => 'varchar',
-          'length' => 10,
-          'not null' => TRUE,
-          'default' => '',
-          'description' => t('The sex of the user'),
-        ),
-        'relationship_status' => array(
-          'type' => 'varchar',
-          'length' => 30,
-          'not null' => TRUE,
-          'default' => '',
-          'description' => t('The type of relationship for the use'),
-        ),
-        'political' => array(
-          'type' => 'varchar',
-          'length' => 50,
-          'not null' => TRUE,
-          'default' => '',
-          'description' => t('The political views of the user'),
-        ),
-        'activities' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The activities of the user'),
-        ),
-        'interests' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The interests of the user'),
-        ),
-        'music' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The favorite music of the user'),
-        ),
-        'tv' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The favorite television shows of the user'),
-        ),
-        'movies' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The favorite movies of the user'),
-        ),
-        'books' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The favorite books of the user'),
-        ),
-        'quotes' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('The favorite quotes of the user'),
-        ),
-        'about_me' => array(
-          'type' => 'text',
-          'not null' => FALSE,
-          'description' => t('More information about the user'),
-        ),
-        'affiliations' => array(
-           'type' => 'text',
-           'size' => 'medium',
-           'description' => t('The networks to which the user being queried belongs'),
-         ),
-         'current_location' => array(
-           'type' => 'text',
-           'not null' => FALSE,
-           'description' => t('The current location of the user'),
-         ),
-         'hometown_location' => array(
-           'type' => 'text',
-           'not null' => FALSE,
-           'description' => t('The home town (and state) of the user'),
-         ),
-         'meeting_sex' => array(
-           'type' => 'text',
-           'not null' => FALSE,
-           'description' => t('The sex of the person the user wants to meet'),
-         ),
-         'meeting_for' => array(
-           'type' => 'text',
-           'not null' => FALSE,
-           'description' => t('The reason the user wants to meet someone'),
-         ),
-    ), 
-    'primary key' => array('uid'), 
-  ); 
-  return $schema; 
-}
\ No newline at end of file
Index: fbconnect.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/fbconnect/fbconnect.module,v
retrieving revision 1.7
diff -u -r1.7 fbconnect.module
--- fbconnect.module	26 Jan 2009 14:08:35 -0000	1.7
+++ fbconnect.module	4 Feb 2009 23:58:33 -0000
@@ -1,5 +1,4 @@
-<?php 
-// $Id: fbconnect.module,v 1.7 2009/01/26 14:08:35 lanets Exp $ 
+<?php
 
 /**
  * @file
@@ -16,51 +15,590 @@
 /**
  * Implementation of hook_menu().
  */
-function fbconnect_menu() {
-  $items['admin/settings/fbconnect'] = array(
-    'title' => 'Fbconnect', 
-    'page callback' => 'drupal_get_form', 
-    'page arguments' => array('fbconnect_admin_settings'), 
-    'access arguments' => array('administer site configuration'),
-    'type' => MENU_NORMAL_ITEM,
-    'file' => 'fbconnect.admin.inc',
-    'description' => 'Configure how Facebook Connect behave.',
-  );
-  $items['fbconnect/register/import'] = array(
-    'page callback' => 'fbconnect_register_page',
-    'type' => MENU_CALLBACK,
-    'access arguments' => array('access content'),
-    'file' => 'fbconnect.pages.inc',
-  );
-  $items['fbconnect/register/create'] = array(
-    'page callback' => 'fbconnect_register_page',
-    'type' => MENU_CALLBACK,
-    'access arguments' => array('access content'),
-    'file' => 'fbconnect.pages.inc',
-  );
-  $items['fbconnect/invite/friends'] = array(
-    'page callback' => 'fbconnect_render_friends_invite_form',
-    'access arguments' => array('access content'),
-    'type' => MENU_CALLBACK,
-  );
-  $items['user/%user/fbconnect'] = array(
-    'title' => 'Facebook connect',
-    'page callback' => 'fbconnect_user_identities',
-    'page arguments' => array(1),
-    'access callback' => 'user_edit_access',
-    'access arguments' => array(1),
-    'type' => MENU_LOCAL_TASK,
-    'file' => 'fbconnect.pages.inc',
+function fbconnect_menu($may_cache) {
+  
+  $items = array();
+  if ($may_cache) { 
+    $items[] = array(
+        'path'        => 'admin/settings/fbconnect',
+        'title'       => t('Fbconnect'),
+        'callback'    => 'drupal_get_form',
+        'callback arguments' => array('fbconnect_admin_settings'),
+        'type'        => MENU_NORMAL_ITEM,
+      );
+      
+    $items[] = array(
+        'path'        => 'fbconnect/register/import',
+        'callback'    => 'fbconnect_register_page',
+        'type'        => MENU_CALLBACK,
+        'access'      => TRUE
+      );
+      
+    $items[] = array(
+        'path'        => 'fbconnect/register/create',
+        'callback'    => 'fbconnect_register_page',
+        'type'        => MENU_CALLBACK,
+        'access'      => TRUE
+      );
+      
+    $items[] = array(
+        'path'        => 'fbconnect/invite/friends',
+        'callback'    => 'fbconnect_render_friends_invite_form',
+        'type'        => MENU_CALLBACK,
+        'access'      => TRUE
+      );
+    global $user;
+    if ($user->uid) {  
+      $items[] = array(
+          'path'        => 'user/'.arg(1).'/fbconnect',
+          'title'       => t('Fbconnect'),
+          'callback'    => 'fbconnect_user_identities',
+          'access'      => TRUE,
+          'callback arguments' => $user,
+          'type'        => MENU_LOCAL_TASK,
+        );
+    }
+      
+    $items[] = array(
+        'path'        => 'fbconnect/logout',
+        'callback'    => 'fbconnect_logout_page',
+        'type'        => MENU_CALLBACK,
+        'access'      => TRUE
+      );
+ 
+    // Renders the JS necessary for any Facebook interaction to work.
+    _fbconnect_render_js();
+    fbconnect_startup();
+  }  
+  return $items; 
+}
+
+/**
+ * Menu callback.
+ * Called when user perform facebook registration
+ */
+function fbconnect_register_page() {
+ if (($fbuid = fbconnect_get_fbuid()) && !user_is_logged_in()) {   
+   switch (arg(2)) {
+     case 'import':
+       drupal_set_title(t('Import your profile information from Facebook'));  
+       $output = fbconnect_render_avatar($fbuid);
+       $output .= drupal_get_form('fbconnect_registerForm');
+     break;
+     case 'create':
+       drupal_set_title(t('Create a new account'));
+       $output  = drupal_get_form('fbconnect_registerForm');
+       if ($friends = fbconnect_get_connected_friends($fbuid)) {
+        $output .= t('Facebook friend already registered');
+        $output .= theme('render_friends_list_fbconnect', $friends);
+       }
+     break;
+   }    
+   return $output;
+ }
+}
+
+ /**
+  * This form is display when we register a new user with facebook account
+  */
+function fbconnect_registerForm() {
+ $fbuid = fbconnect_get_fbuid();
+ switch (arg(2)) {
+   case 'import':
+    // display the form if the facebook importation settings is activated
+    if (variable_get('fbconnect_import', array('fbconnect_import'))) {
+      $form['imp'] = array(
+        '#type' => 'fieldset',
+        '#collapsible' => FALSE,
+      );       
+      $form['imp']['field_import'] = array(
+        '#type' => 'checkboxes',
+        '#options' => fbconnect_available_import_fields($fbuid),
+        '#default_value' => variable_get('fbconnect_field_to_import', ''),
+        '#description' => t('Select the public Facebook profile information you wish to import.
+        This information will be displayed on your public profile.'),
+      );
+    }
+   break;
+   case 'create':
+     $form['reg'] = array(
+       '#type' => 'fieldset',
+       '#collapsible' => FALSE,
+     );
+     $form['reg']['username'] = array(
+       '#type' => 'textfield',
+       '#title' => t('Username'),
+       '#required' => TRUE,
+       '#default_value' => fbconnect_get_fbname($fbuid),
+       '#description' => t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
+     );
+     $form['reg']['mail'] = array(
+       '#type' => 'textfield',
+       '#title' => t('E-mail address'),
+       '#required' => TRUE,
+       '#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address.')
+     );
+     $form['reg']['visibility'] = array(
+       '#type' => 'checkbox',
+       '#title' => t('Let my Facebook friends see me on @sitename', 
+          array('@sitename' => variable_get('site_name', 'this website'))),
+       '#description' => t('My Facebook friends will be able to see that I own an account on @sitename.',
+        array('@sitename' => variable_get('site_name', t('this website')))),
+       '#default_value' => 1,
+     );
+   break;
+ }
+ $form['submit'] = array( 
+ '#type' => 'submit',
+ '#value' => t('Submit') 
+ ); 
+ return $form; 
+}
+
+/** 
+ * Validate the register form. 
+ */ 
+function fbconnect_registerForm_validate($form, &$form_state) {
+ switch (arg(2)) {
+   case 'import':
+     if ($fields = array_filter((array)$form_state['field_import'], 'fbconnect_import_filter')) {
+      $_SESSION['fb_reg_import'] = $fields;
+     }
+   break;
+   case 'create':
+     // Validate the username
+     $name = $form_state['username'];
+     if (user_validate_name($name)) {
+       form_set_error('username', user_validate_name($name));
+     }
+     else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE LOWER(name) = LOWER('%s')", $name)) > 0) {
+       form_set_error('username', t('The name %name is already taken.',
+        array('%name' => $name)));
+     }
+     else if (drupal_is_denied('user', $name)) {
+       form_set_error('username', t('The name %name is a reserved username.',
+        array('%name' => $name)));
+     }
+     // Validate e-mail
+     $mail = $form_state['mail'];
+     if (user_validate_mail($mail)) {
+       form_set_error('mail', user_validate_mail($mail));
+     }
+     else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE LOWER(mail) = LOWER('%s')", $mail)) > 0) {
+       form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>',
+        array('%email' => $mail, '@password' => url('user/password'))));
+     }
+     else if (drupal_is_denied('mail', $mail)) {
+       form_set_error('mail', t('The e-mail address %email has been denied access.',
+        array('%email' => $mail)));
+     }
+   break;
+ }
+}
+
+/**
+* Implementing not existent function for D5 (from api.drupal.org)
+*/
+function user_external_login_register($name, $module) {
+  global $user;
+
+  $existing_user = user_load(array('name' => $name));
+  if (isset($existing_user->uid)) {
+    $user = $existing_user;
+  }
+  else {
+    // Register this new user.
+    $userinfo = array(
+      'name' => $name,
+      'pass' => user_password(),
+      'init' => $name,
+      'status' => 1,
+      "authname_$module" => $name,
+      'access' => time()
     );
-  $items['fbconnect/logout'] = array(
-    'page callback' => 'fbconnect_logout_page',
-    'type' => MENU_CALLBACK,
-    'access arguments' => array('access content'),
+    $account = user_save('', $userinfo);
+    // Terminate if an error occured during user_save().
+    if (!$account) {
+      drupal_set_message(t("Error saving user account."), 'error');
+      return;
+    }
+    $user = $account;
+    watchdog('user', 'New external user: %name using module %module.', array('%name' => $name, '%module' => $module), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $user->uid .'/edit'));
+  }
+}
+ 
+
+/** 
+ * Handle post-validation register form submission. 
+ */ 
+function fbconnect_registerForm_submit($form, &$form_state) {
+ switch (arg(2)) {
+   case 'import':
+   drupal_goto('fbconnect/register/create');
+   return;
+
+   case 'create':            
+   // If sucess, user_external_login_register will return NULL
+   $register_result = user_external_login_register($form_state['username'], 'fbconnect');
+   // In case of register false
+   if ($register_result != NULL) {
+     watchdog('fbconnect', 'User external register failed for %user.', array('%user' => $form_state['username']));
+     drupal_set_message(t('Register error'), 'error');
+     return;
+   }
+   global $user;
+   $fbuid = fbconnect_get_fbuid();
+   fbconnect_register($user->uid, $fbuid);
+   // Update user visibility, default = 1
+   if ($form_state['visibility'] == 0) {
+    fbconnect_set_user_visibility($user, $form_state['visibility']);
+   }
+   $extra_data['mail'] = $extra_data['init'] = $form_state['mail'];
+   $extra_data['login'] = time();
+   // Save facebook picture as avatar.
+   if (variable_get('user_pictures', 0)) {
+     $picture = fbconnect_get_fb_avatar($fbuid);
+     if ($picture) {
+       $extra_data['picture'] = $picture;
+     }
+   }
+   user_save($user, $extra_data);
+   if ($_SESSION['fb_reg_import']) {
+     fbconnect_insert_user_info($user, $_SESSION['fb_reg_import']);
+   }
+   if (variable_get('fbconnect_reg_feed', 1) && $form_state['visibility']) {
+     $_SESSION['fbconnect_feed']['type'] = 'registration';
+   }
+   if ($form_state['visibility']) {
+     drupal_goto('fbconnect/invite/friends');
+     drupal_set_message(t('Registration successful. You may now invite friends to join you on @sitename', 
+      array('@sitename' => variable_get('site_name', t('this website')))), 'status');
+   }
+   else {
+     drupal_set_message(t('Registration successful.'));
+   }
+   return;
+ }
+}
+
+/**
+ * Menu callback fbconnect identities
+ */
+function fbconnect_user_identities($account) {
+  drupal_set_title(check_plain($account->name));
+  $header = array(t('Facebook Connect identities'));
+  $rows = array();  
+  $rows[] = array(t('This website supports Facebook Connect and allow you to log in with your Facebook account.'));  
+  if ($account->fbuid) {    
+    $rows[] = array(t('Your account is linked with your Facebook account.'));
+    $rows[] = array(t('Your Facebook user id : @fbuid', array('@fbuid' => $account->fbuid))); 
+       
+    if ((fbconnect_get_fbuid() > 0) && $account->fbuid != fbconnect_get_fbuid()) {
+      drupal_set_message('Invalid Facebook session, you\'re not authorized to modify the parameters.');
+      return theme('table', $header, $rows);
+    }    
+    if (!fbconnect_get_fbuid()) {
+      $rows[] = array(t('Open a Facebook session for more settings options.'));
+      $rows[] = array(fbconnect_render_button());
+    }
+    $output = theme('table', $header, $rows);
+    $output .= drupal_get_form('fbconnect_user_settingsForm');
+    return $output;
+  }  
+  if (!fbconnect_get_fbuid()) {
+    $rows[] = array(t('Click on login button below to link your account on @sitename with your Facebook account.', 
+      array('@sitename' => variable_get('site_name', t('this website')))));
+    $rows[] = array(fbconnect_render_button());
+    return theme('table', $header, $rows);
+  } 
+  $rows[] = array(t('Facebook session detected, please confirm that you wish to link your @sitename account and Facebook account.',
+   array('@sitename' => variable_get('site_name', t('this website')))));
+  $output = theme('table', $header, $rows);
+  $output .= drupal_get_form('fbconnect_associationForm');  
+  return $output;
+}
+
+/**
+ * Render the user fbconnect association form
+ */
+function fbconnect_associationForm() {
+  $form['link'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Link my account on @sitename and my Facebook account', 
+      array('@sitename' => variable_get('sitename', t('this website')))),
+    '#default_value' => 1,
   );
-  return $items; 
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit'),
+  );
+  return $form;
+}
+
+/**
+ * Render the user fbconnect setting form
+ */
+function fbconnect_user_settingsForm() {
+  $user = user_load(array('uid' => arg(1)));
+  $form['view'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => FALSE,
+  );
+  $form['visibility_hid'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $user->fb_visibility,
+  );
+  $form['view']['visibility'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Let my Facebook friends see me on @sitename', 
+    array('@sitename' => variable_get('site_name', 'this website'))),
+    '#description' => t('My Facebook friends will be able to see that I own an account on this website.'),
+    '#default_value' => $user->fb_visibility,
+  );
+
+  // This fields is visible only if we got a active Facebook session.
+  if (fbconnect_get_fbuid() > 0) {
+    // If user avatar is actived
+    // The user can choose to use his Facebook avatar as drupal avatar.
+    if (variable_get('user_pictures', 0)) {
+      $form['avatar'] = array(
+        '#type' => 'fieldset',
+        '#collapsible' => FALSE,
+      );
+      $form['avatar']['fb_avatar'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Use my Facebook picture on @sitename', 
+          array('@sitename' => variable_get('sitename', t('this website')))),
+        '#description' => t('Your picture will be updated every 24 hours.'),
+        '#default_value' => fbconnect_user_avatar_setting($user),
+      );
+    }
+    // Importation settings
+    $fields = array_filter(variable_get('fbconnect_field_to_import', ''), 'fbconnect_import_filter');
+    if (variable_get('fbconnect_import', array('fbconnect_import')) && $fields) {
+      $form['import'] = array(
+        '#type' => 'fieldset',
+        '#collapsible' => FALSE,
+      );
+      $default_value = fbconnect_get_user_import_setting($user);    
+      $form['import']['import_setting'] = array(
+        '#type' => 'checkboxes',
+        '#description' => t('This information is displayed on my profile.'),
+        '#options' => fbconnect_available_import_fields(fbconnect_get_fbuid()),
+        '#default_value' => ($default_value) ? $default_value : array(''),
+      );
+    }
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save settings'),
+  );
+  return $form;
+}
+
+/**
+ * Handle post-validation user_settingsForm submission. 
+ */
+function fbconnect_user_settingsForm_submit($form, &$form_state) {
+  $user = user_load(array('uid' => arg(1)));  
+  // Visibility settings
+  if ($form_state['visibility_hid'] != $form_state['visibility']) {
+    fbconnect_set_user_visibility($user, $form_state['visibility']);
+  }
+  if (fbconnect_get_fbuid() == 0) {
+    return;
+  }
+  if (variable_get('user_pictures', 0)) {  
+    fbconnect_user_avatar_setting($user, $form_state['fb_avatar']);        
+    if ($form_state['fb_avatar'] == 1) {
+      fbconnect_refresh_user_avatar($user->fbuid, $user->uid);
+    }
+    else {
+      if ($user->picture) {
+        file_delete($user->picture);
+        db_query("UPDATE {users} SET picture = '' WHERE uid = %d", $user->uid);
+      }
+    }
+  }
+  if (variable_get('fbconnect_import', array('fbconnect_import'))) {
+    if ($fields = array_filter((array)$form_state['import_setting'], 'fbconnect_import_filter')) {
+      fbconnect_insert_user_info($user, $fields);
+    }
+    else {
+      db_query("UPDATE {fbconnect_users} SET import_setting = '' WHERE uid = %d", $user->uid);
+      db_query('DELETE FROM {fbconnect_profile} WHERE uid = %d', $user->uid);
+    }
+  }
+}
+
+/** 
+ * Handle post-validation association_Form submission. 
+ */ 
+function fbconnect_associationForm_submit($form, &$form_state) {
+  if ($fbuid = fbconnect_get_fbuid()) {
+    if ($form_state['link'] == 1) {
+      $user = user_load(array('uid' => arg(1)));
+      if (fbconnect_register($user->uid, $fbuid)) {
+        drupal_set_message(t('Congratulations! Your account is now linked to your Facebook account'), 'status');
+        watchdog('fbconnect', 'User uid: %user is now associated with the fbuid: %fbuid.', 
+        array('%user' => $user->uid, '%fbuid' => $fbuid));        
+      }
+      else {
+        drupal_set_message(t('Association failed'), 'error');
+        watchdog('fbconnect', 'User association failed for uid: %user - fbuid: %fbuid.', 
+        array('%user' => $user->uid, '%fbuid' => $fbuid));        
+      }
+    }
+  }
+}
+
+/** 
+ * Form builder. Configure fbconnect. 
+ * 
+ * @ingroup forms
+ * @see system_settings_form(). 
+ */
+function fbconnect_admin_settings() {
+  global $base_url;
+  $form['api'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Facebook API config'),
+    '#collapsible' => FALSE,
+  );
+  $form['api']['fbconnect_api_key'] = array( 
+    '#type' => 'textfield', 
+    '#title' => t('Facebook API KEY'), 
+    '#default_value' => variable_get('fbconnect_api_key', ''), 
+  );   
+  $form['api']['fbconnect_secret_api_key'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Facebook Secret API KEY'),
+    '#default_value' => variable_get('fbconnect_secret_api_key', ''),
+  );
+  $form['friend'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Friends invite'),
+    '#collapsible' => FALSE,
+  );   
+  $form['friend']['fbconnect_invitef_content'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Invite message'),      
+    '#default_value' => variable_get('fbconnect_invitef_content', t('Enjoy the new drupal facebook connect module')),
+  );
+  $form['friend']['fbconnect_invitef_type'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Your site name'),      
+    '#default_value' => variable_get('fbconnect_invitef_type', variable_get('site_name', '')),
+  );
+  $form['button'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Connect button type'),
+    '#collapsible' => FALSE,
+  );
+  $module_path = drupal_get_path('module', 'fbconnect') .'/images/';
+  $button_options = array(
+    'small_short' => theme_image($module_path .'Connect_white_small_short.gif'),
+    'medium_short' => theme_image($module_path .'Connect_white_medium_short.gif'),
+    'medium_long' => theme_image($module_path .'Connect_white_medium_long.gif'),
+    'large_short' => theme_image($module_path .'Connect_white_large_short.gif'),
+    'large_long' => theme_image($module_path .'Connect_white_large_long.gif'),
+  );
+  $form['button']['fbconnect_button_type'] = array(
+    '#type' => 'radios',
+    '#default_value' => variable_get('fbconnect_button_type', 'large_long'),
+    '#options' => $button_options,
+  );
+  $form['feed'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Story feed'),
+    '#collapsible' => FALSE,
+  );
+  $form['feed']['fbconnect_reg_feed'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Display story feed prompt after user registration'),
+    '#default_value' => variable_get('fbconnect_reg_feed', array('fbconnect_reg_feed')),
+  );
+  $form['feed']['fbconnect_com_feed'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Display story feed prompt after comment post'),      
+    '#default_value' => variable_get('fbconnect_com_feed', array('fbconnect_com_feed')),
+  );
+  $form['feed']['fbconnect_reg_feed_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Registration feed bundle ID'),
+    '#default_value' => variable_get('fbconnect_reg_feed_id', FBCONNECT_REG_FEED_BUNDLEID),
+  );
+  $form['feed']['fbconnect_com_feed_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Comment feed bundle ID'),
+    '#default_value' => variable_get('fbconnect_com_feed_id', FBCONNECT_COMMENT_FEED_BUNDLEID),
+  );
+  $form['friend']['fbconnect_invitef_redirect'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Redirect url, when user valid or skip invite friend form'),
+    '#default_value' => variable_get('fbconnect_invitef_redirect', $base_url),
+  );
+  $form['import'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Import settings'),
+    '#collapsible' => FALSE,
+  );
+  $form['import']['fbconnect_import'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Activate Facebook informations import'),      
+    '#default_value' => variable_get('fbconnect_import', array('fbconnect_import')),
+  );
+  $form['import']['fields'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Importation settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['import']['fields']['fbconnect_field_to_import'] = array(
+    '#type' => 'checkboxes',
+    '#options' => variable_get('facebook_user_fields', NULL),
+    '#default_value' => variable_get('fbconnect_field_to_import', array_keys(variable_get('facebook_user_fields', NULL))),
+  );
+  //$form['#submit'][] = 'fbconnect_admin_settings_submit';
+  return system_settings_form($form);
+}
+
+/**
+ *  Get the facebook application settings.
+ */
+function fbconnect_get_app_settings($properties) {
+  try {
+    return facebook_client()->api_client->admin_getAppProperties((array)$properties);
+  } catch (Exception $e) {
+      watchdog('fbconnect', 'Exception thrown while calling admin_getAppProperties: %code',
+        array('%code' => $e->getMessage()), WATCHDOG_WARNING);
+  }
 }
 
 /**
+ *  Check my application setup.
+ *
+function fbconnect_admin_settings_submit($form, &$form_state) {
+  
+  if (facebook_client()) {
+    $res = fbconnect_get_app_settings(array('callback_url', 'dev_mode')); 
+    if (!$res['callback_url']) {
+      $msg = t('Fbconnect, You must provide a valid callback_url! 
+      check your facebook application settings.');
+      drupal_set_message($msg, 'warning');
+    }
+    if ($res['dev_mode'] == 1) {
+      $msg  = t('Fbconnect, Your application is set on Sandbox Mode, 
+      users can not use Facebook Connect function on your site. 
+      You can deactivate it in your Facebook application settings');
+      drupal_set_message($msg, 'warning');
+    }
+  }
+}
+*/
+
+
+/**
  * Recupere l'avatar Facebook de l'utilisateur.
  * @param Int $fbuid
  */
@@ -178,6 +716,14 @@
   }
 }
 
+/** 
+* Just helper function for D5
+*/
+function user_is_logged_in() {
+  global $user;
+  return (bool)$user->uid;
+}
+
 /**
  * Render data for fbconnect block
  */
@@ -201,17 +747,16 @@
 /**
  * Implementation of hook_init().
  */
-function fbconnect_init() {
+function fbconnect_startup() {
   global $user;
-  // Renders the JS necessary for any Facebook interaction to work.
-  _fbconnect_render_js();
-  $fbuid = fbconnect_get_fbuid();  
-  if (!$fbuid || user_is_logged_in() || (arg(0) == 'fbconnect' && arg(1) == 'logout')) {
+  $fbuid = fbconnect_get_fbuid(); 
+  $pathArr = explode("/",$_GET['q']);
+  if (!$fbuid || user_is_logged_in() || ($pathArr[0] == 'fbconnect' && $pathArr[1] == 'logout')) {
     return;
   }
   // This function check if we have a drupal id associate with facebook uid.
   $uid = _is_fbconnect_user($fbuid); 
-  if (!$uid && arg(0) != 'fbconnect' && arg(1) != 'register') {
+  if (!$uid && $pathArr[0] != 'fbconnect' && $pathArr[1] != 'register') {
     $fields = array_filter(variable_get('fbconnect_field_to_import', ''), 'fbconnect_import_filter');
     $path = (variable_get('fbconnect_import', array('fbconnect_import')) && $fields) ? 'fbconnect/register/import' : 'fbconnect/register/create';
     drupal_goto($path);
@@ -230,8 +775,10 @@
  * Impletementation of hook_form_alter.
  * Adds facebook connect login button to the login forms
  */
-function fbconnect_form_alter(&$form, $form_state, $form_id) {
+function fbconnect_form_alter($form_id, &$form) {
+  
   if (($form_id == 'user_login_block' || $form_id == 'user_login') && fbconnect_get_config()) {
+    
     $items[] = array(
       'data' => fbconnect_render_button(),
     );    
@@ -255,8 +802,8 @@
 }
 
 function fbconnect_comment_feed_submit($form, &$form_state) {
-  if ($form_state['values']['fbconnect_feed'] == 1) {
-    $node = node_load(array('nid' => $form_state['values']['nid']));
+  if ($form_state['fbconnect_feed'] == 1) {
+    $node = node_load(array('nid' => $form_state['nid']));
     $_SESSION['fbconnect_feed'] = array(
       'type' => 'comment',
       'node_title' => $node->title,
@@ -527,7 +1074,7 @@
 function fbconnect_get_config() {
   $config = array();
   $config['api_key'] = variable_get('fbconnect_api_key', NULL);
-  $config['secret_api_key'] = variable_get('fbconnect_secret_api_key', NULL);  
+  $config['secret_api_key'] = variable_get('fbconnect_secret_api_key', NULL); 
   if ($config['api_key'] && $config['secret_api_key']) {
     return $config;
   }
@@ -540,10 +1087,12 @@
  */
 function facebook_client() {
   static $fb = NULL;
+  
   if (!$fb instanceof Facebook) {
+    
     if ($conf = fbconnect_get_config()) {
       // Facebook php client API
-      $lib_path = drupal_get_path('module', 'fbconnect') .'/facebook-client/';
+      $lib_path = 'sites/all/modules/fbconnect/facebook-client/';
       $lib_files = array(
         'facebook.php',
         'facebook_desktop.php',
@@ -558,10 +1107,11 @@
           return;
         }
       }
+      
       // Include facebook.php
       include_once($lib_path.$lib_files[0]);
       if (class_exists('Facebook')) {
-        $fb = new Facebook($conf['api_key'], $conf['secret_api_key']);
+         $fb = new Facebook($conf['api_key'], $conf['secret_api_key']);
       }
     }
   }
@@ -754,4 +1304,4 @@
     $onload_js .= sprintf("facebook_onload(%s);", $log_stat);
   }
   drupal_add_js('window.onload = function() {'. $onload_js .'};', 'inline', 'footer');
-}
\ No newline at end of file
+}
