Index: phpbbforum.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phpbbforum/phpbbforum.module,v
retrieving revision 1.60
diff -u -u -p -r1.60 phpbbforum.module
--- phpbbforum.module	13 Feb 2011 14:05:54 -0000	1.60
+++ phpbbforum.module	18 Feb 2011 00:13:41 -0000
@@ -23,7 +23,6 @@ define('PHPBB_PAGE', 'phpbbforum');
 
 define('PHPBB_SYNC_TO_PHPBB', 'to phpBB');
 define('PHPBB_SYNC_TO_DRUPAL', 'to Drupal');
-define('PHPBB_SYNC_TO_MASTER', 'to master');
 
 @define('A_SYNC_TO_UNDEFINED', 0);
 @define('A_SYNC_TO_ME', 1);
@@ -528,7 +527,7 @@ function phpbbforum_nodeapi(&$node, $op,
         $account = user_load($node->uid);
         $phpbb_user_data = $phpbb_user->data;
         if (phpbb_api_get_user($account->name, '')) {
-          phpbbforum_update_user_profile($op, PHPBB_SYNC_TO_DRUPAL, $account, array(), $node);
+          phpbbforum_update_user_profile($op, $account, array(), $node);
           if ($user->name != $account->name) {
             $phpbb_user->data = $phpbb_user_data;
           }
@@ -547,7 +546,7 @@ function phpbbforum_nodeapi(&$node, $op,
         $account = user_load($node->uid);
         $phpbb_user_data = $phpbb_user->data;
         if (phpbb_api_get_user($account->name, '')) {
-          phpbbforum_update_user_profile($op, PHPBB_SYNC_TO_PHPBB, $account, array(), $node);
+          phpbbforum_update_user_profile($op, $account, array(), $node);
           if ($user->name != $account->name) {
             $phpbb_user->data = $phpbb_user_data;
           }
@@ -961,10 +960,12 @@ function _phpbbforum_msg($op = 'info', $
   return $message;
 }
 
-function _phpbbforum_set_user_profile_data($op, $sync, &$account, $data = array(),
+function _phpbbforum_set_user_profile_data($op, &$account, $data = array(),
   $has_profile = true, $has_content_profile = false, $content_profile = null) {
   global $phpbb_config, $phpbb_user;
 
+  $sync = _phpbbforum_secondary_reg();
+
   if ($phpbb_config['allow_birthdays']) {
     $var = variable_get('phpbbforum_map_user_birthday', 'profile_birthday');
     $has_val = false;
@@ -1464,7 +1465,21 @@ function _phpbbforum_set_user_profile_da
   return $data;
 }
 
-function phpbbforum_update_user_profile($op, $sync, &$account, $data = array(), $content_profile = null) {
+/**
+ * Which is the secondary registration db
+ */
+function _phpbbforum_secondary_reg() {
+  $phpbb_master = variable_get('phpbbforum_master', 1);
+  if ($phpbb_master) {
+    $syncto = PHPBB_SYNC_TO_DRUPAL;
+  }
+  else {
+    $syncto = PHPBB_SYNC_TO_PHPBB;
+  }
+  return $syncto;
+}
+
+function phpbbforum_update_user_profile($op, &$account, $data = array(), $content_profile = null) {
   global $phpbb_config, $phpbb_user, $user;
 
   $update = false;
@@ -1476,17 +1491,7 @@ function phpbbforum_update_user_profile(
     return $update;
   }
 
-  $syncto = $sync;
-  if ($sync == PHPBB_SYNC_TO_MASTER) {
-    $phpbb_master = variable_get('phpbbforum_master', 1);
-    if ($phpbb_master) {
-      $syncto = PHPBB_SYNC_TO_DRUPAL;
-    }
-    else {
-      $syncto = PHPBB_SYNC_TO_PHPBB;
-    }
-  }
-
+  $syncto = _phpbbforum_secondary_reg($sync);
 
   if ($has_content_profile) {
     if ($syncto == PHPBB_SYNC_TO_PHPBB) {
@@ -1505,7 +1510,7 @@ function phpbbforum_update_user_profile(
         }
         $content_profile_changed = false;
         $data = array();
-        $data = _phpbbforum_set_user_profile_data($op, $syncto, $account, $data, $has_profile, $has_content_profile, $content_profile);
+        $data = _phpbbforum_set_user_profile_data($op, $account, $data, $has_profile, $has_content_profile, $content_profile);
         if (count($data) != 0) {
           if ($syncto == PHPBB_SYNC_TO_PHPBB) {
             $update = phpbb_api_update_user($phpbb_user->data['user_id'], '', '', '', $data);
@@ -1547,7 +1552,7 @@ function phpbbforum_update_user_profile(
 
   //$content_profile_changed = false;
   $data = array();
-  $data = _phpbbforum_set_user_profile_data($op, $syncto, $account, $data, $has_profile, $has_content_profile, $content_profile);
+  $data = _phpbbforum_set_user_profile_data($op, $account, $data, $has_profile, $has_content_profile, $content_profile);
   if (count($data) != 0) {
     if ($syncto == PHPBB_SYNC_TO_PHPBB) {
       $update = phpbb_api_update_user($phpbb_user->data['user_id'], '', '', '', $data);
@@ -1569,10 +1574,11 @@ function phpbbforum_update_user_profile(
   return $update;
 }
 
-function _phpbbforum_set_user_data($op, $sync, &$account, $name, $pass, $mail, $edit) {
+function _phpbbforum_set_user_data($op, &$account, $name, $pass, $mail, $edit) {
   global $phpbb_user, $phpbb_config, $base_url, $phpbb_root_path, $phpEx;
 
   $sync_to_admin = false;
+  $sync = _phpbbforum_secondary_reg();
   if ($sync == PHPBB_SYNC_TO_PHPBB) {
     if ($phpbb_user->data['user_id'] == variable_get('phpbbforum_admin_user_id', 2)) {
       $sync_to_admin = true;
@@ -2181,6 +2187,7 @@ function _phpbbforum_set_user_data($op, 
 
   return $data;
 }
+
 function _phpbbforum_profile_save_profile(&$edit, $data0, &$user, $category, $register = FALSE) {
   if (PHPBB_DRUPAL_MODULE_VERSION == 5) {
     $result = _profile_get_fields($category);
@@ -2223,30 +2230,21 @@ function _phpbbforum_profile_save_profil
   return $profile_data;
 }
 
-function phpbbforum_update_user($op, $sync, &$account, $name, $pass, $mail, $edit = array()) {
+function phpbbforum_update_user($op, &$account, $name, $pass, $mail, $edit = array()) {
   global $phpbb_config, $phpbb_user, $user;
 
   $update = false;
 
-  $syncto = $sync;
-  if ($sync == PHPBB_SYNC_TO_MASTER) {
-    $phpbb_master = variable_get('phpbbforum_master', 1);
-    if ($phpbb_master) {
-      $syncto = PHPBB_SYNC_TO_DRUPAL;
-    }
-    else {
-      $syncto = PHPBB_SYNC_TO_PHPBB;
-    }
-  }
+  $syncto = _phpbbforum_secondary_reg();
 
   $data = array();
 
-  $data = _phpbbforum_set_user_data($op, $syncto, $account, $name, $pass, $mail, $edit);
+  $data = _phpbbforum_set_user_data($op, $account, $name, $pass, $mail, $edit);
 
   if (module_exists('profile')) {
     if (empty($pass) || $syncto == PHPBB_SYNC_TO_DRUPAL || count($edit) != 0 ) {
       $profile_data = array();
-      $profile_data = _phpbbforum_set_user_profile_data($op, $syncto, $account, $profile_data);
+      $profile_data = _phpbbforum_set_user_profile_data($op, $account, $profile_data);
       //if (PHPBB_MODULE_DEBUG)
       //drupal_set_message('PHPBB_user before update profile data'. print_r($profile_data, true));
       if ($syncto == PHPBB_SYNC_TO_DRUPAL && count($profile_data) != 0) {
@@ -2331,7 +2329,7 @@ function phpbbforum_user($op, &$edit, &$
   if ($op == 'logout') {
     if (phpbb_api_authenticate_user()) {
       // Update data
-      phpbbforum_update_user($op, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail);
+      phpbbforum_update_user($op, $account, '', '', $account->mail);
     }
     phpbb_api_logout();
     $message = 'Session closed for %name.';
@@ -2379,7 +2377,7 @@ function phpbbforum_user($op, &$edit, &$
           //  _phpbbforum_msg('phpbbforum_user login '. $name .' id='. $phpbb_user->data['user_id']);
           if (!$openid_login) {
             // Update
-            $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail, $edit);
+            $update = phpbbforum_update_user($op, $account, '', '', $account->mail, $edit);
           }
           //phpbb_api_get_user($name, $pass);
           //if ($phpbb_user->data['user_id'] == ANONYMOUS)
@@ -2409,7 +2407,7 @@ function phpbbforum_user($op, &$edit, &$
               $update = true;
             }
             else {
-              $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', $pass, '');
+              $update = phpbbforum_update_user($op, $account, '', $pass, '');
             }
             if ($update) {
               //if (PHPBB_MODULE_DEBUG)
@@ -2419,7 +2417,7 @@ function phpbbforum_user($op, &$edit, &$
                 //_phpbbforum_set_message('User account: '. $name .' pass='. $pass .' found in phpBB');
                 if (!$reset && !$openid_login) {
                   // Update
-                  $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', '', $account->mail, $edit);
+                  $update = phpbbforum_update_user($op, $account, '', '', $account->mail, $edit);
                 }
 
                 //phpbb_api_get_user($name, $pass);
@@ -2454,7 +2452,7 @@ function phpbbforum_user($op, &$edit, &$
               _phpbbforum_set_message($message, $var);
               // Update
               if (phpbb_api_get_user($name, $pass)) {
-                phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', '', '', $edit);
+                phpbbforum_update_user($op, $account, '', '', '', $edit);
                 if (phpbb_api_login($name, $pass)) {
                   //phpbb_api_login((int) variable_get('phpbbforum_cookie_length', PHPBB_COOKIELENGTH), $name, $pass);
                   variable_set('phpbbforum_user_login', 1);
@@ -2496,10 +2494,10 @@ function phpbbforum_user($op, &$edit, &$
       //  _phpbbforum_set_message('User account: '. $account->name .' pass='. $pass .' found in phpBB');
       /*
        if (!empty($pass)) {
-       $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, $account->name, $pass, '');
+       $update = phpbbforum_update_user($op, $account, $account->name, $pass, '');
        if ($update) {
        if (phpbb_api_get_user($account->name, $pass)) {
-       $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, $account->name, '', $edit['mail'], $edit);
+       $update = phpbbforum_update_user($op, $account, $account->name, '', $edit['mail'], $edit);
        if ((empty($user->name) || $user->name == $account->name) && phpbb_api_get_user($account->name, $pass)) {
        //phpbb_api_login((int) variable_get('phpbbforum_cookie_length', PHPBB_COOKIELENGTH), $name, $pass);
        variable_set('phpbbforum_user_login', 1);
@@ -2533,7 +2531,7 @@ function phpbbforum_user($op, &$edit, &$
           //if (PHPBB_MODULE_DEBUG)
           //  _phpbbforum_set_message('*User account: '. $account->name .' pass='. $pass .' found');
           if (!$openid_login) {
-            phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', '', '', $edit);
+            phpbbforum_update_user($op, $account, '', '', '', $edit);
           }
         }
         if ((empty($user->name) || $user->name == $account->name) && phpbb_api_get_user($account->name, $pass)) {
@@ -2576,7 +2574,7 @@ function phpbbforum_user($op, &$edit, &$
        _phpbbforum_set_message($message, $var);
 
        if ($user->name != $account->name && empty($updatemail))
-       phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, $updatename, $pass, $updatemail, $edit);
+       phpbbforum_update_user($op, $account, $updatename, $pass, $updatemail, $edit);
        else
        $edit['name'] = $account->name;
        }
@@ -2597,7 +2595,7 @@ function phpbbforum_user($op, &$edit, &$
       //if (PHPBB_MODULE_DEBUG)
       //  _phpbbforum_set_message('*User:'. $user->name .' account:'. $account->name .' found');
       // Sync account changes back to PHPBB
-      $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, $updatename, $pass, $updatemail, $edit);
+      $update = phpbbforum_update_user($op, $account, $updatename, $pass, $updatemail, $edit);
       if ($user->name == $account->name) {
         if (empty($name)) {
           $name = $account->name;
@@ -2629,7 +2627,7 @@ function phpbbforum_user($op, &$edit, &$
         _phpbbforum_watchdog($message, $var);
         _phpbbforum_set_message($message, $var);
         // Update
-        $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', '', '', $edit);
+        $update = phpbbforum_update_user($op, $account, '', '', '', $edit);
         if ($user->name == $account->name && phpbb_api_get_user($name, $pass)) {
           //phpbb_api_login((int) variable_get('phpbbforum_cookie_length', PHPBB_COOKIELENGTH), $name, $pass);
           variable_set('phpbbforum_user_login', 1);
@@ -2645,7 +2643,7 @@ function phpbbforum_user($op, &$edit, &$
         //_phpbbforum_watchdog($message, $var);
         _phpbbforum_set_message($message, $var);
         //$edit['name'] = $account->name;
-        phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, $updatename, '', $updatemail, $edit);
+        phpbbforum_update_user($op, $account, $updatename, '', $updatemail, $edit);
       }
     }
 
@@ -2669,7 +2667,7 @@ function phpbbforum_user($op, &$edit, &$
         //  _phpbbforum_set_message('User update profile: '. $account->name .' profile='. $account->profile_username);
 
         // Sync profile changes back to PHPBB
-        phpbbforum_update_user_profile($op, PHPBB_SYNC_TO_PHPBB, $account);
+        phpbbforum_update_user_profile($op, $account);
       }
 
       if ($user->name != $account->name) {
@@ -2687,7 +2685,7 @@ function phpbbforum_user($op, &$edit, &$
       //if (PHPBB_MODULE_DEBUG)
       //  _phpbbforum_set_message('User update view: '. $account->name .' profile='. $account->profile_username);
       // Sync data and profile changes
-      $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail, $edit);
+      $update = phpbbforum_update_user($op, $account, '', '', $account->mail, $edit);
     }
 
     if ($user->name != $account->name) {
@@ -2868,36 +2866,41 @@ function phpbbforum_external_login_regis
     $account = user_load(array('name' => $login));
   }
 
-  if (!$account->uid) { // Register new user
-    if (db_result(db_query("SELECT uid FROM {users} WHERE LOWER(mail) = LOWER('%s') AND uid <> %d", $login, $account->uid))) {
-      $message = 'The phpBB username %name has already been taken by another user.';
-      //_phpbbforum_watchdog($message, $var);
-      _phpbbforum_set_message($message, $var);
+  $sync = _phpbbforum_secondary_reg();
+  if (!$account->uid) { // Register new user in Drupal
+    if ($sync == PHPBB_SYNC_TO_PHPBB) { // XXX the rest of the function will self-descruct it seems
       return false;
     }
+    elseif ($sync == PHPBB_SYNC_TO_DRUPAL) {
+      if (db_result(db_query("SELECT uid FROM {users} WHERE LOWER(mail) = LOWER('%s') AND uid <> %d", $login, $account->uid))) {
+        $message = 'The phpBB username %name has already been taken by another user.';
+        //_phpbbforum_watchdog($message, $var);
+        _phpbbforum_set_message($message, $var);
+        return false;
+      }
 
-    $account = user_save('', array(
-      'name' => $login,
-      'mail' => $email,
-      'timezone' => $phpbb_user->data['user_timezone'] * 60 * 60,
-      'init' => $email,
-      'status' => $status,
-      'roles' => $roles,
-    ));
-
-    if (!$account) {
-      //drupal_set_message(t("Error saving user account."), 'error');
-      return false;
+      $account = user_save('', array(
+        'name' => $login,
+        'mail' => $email,
+        'timezone' => $phpbb_user->data['user_timezone'] * 60 * 60,
+        'init' => $email,
+        'status' => $status,
+        'roles' => $roles,
+      ));
+
+      if (!$account) {
+        //drupal_set_message(t("Error saving user account."), 'error');
+        return false;
+      }
+      //$message = 'Successfully registered new user: %name.';
+      $message = 'New user: %name (%email).';
+      $var = array(
+        '%name' => $login,
+        '%email' => $email,
+      );
+      _phpbbforum_watchdog($message, $var, WATCHDOG_NOTICE, l('edit', 'user/' . $account->uid . '/edit'));
+      _phpbbforum_set_message($message, $var);
     }
-    //$message = 'Successfully registered new user: %name.';
-    $message = 'New user: %name (%email).';
-    $var = array(
-      '%name' => $login,
-      '%email' => $email,
-    );
-    _phpbbforum_watchdog($message, $var, WATCHDOG_NOTICE, l('edit', 'user/' . $account->uid . '/edit'));
-    _phpbbforum_set_message($message, $var);
-
   }
 
   if ($account->status == 0 && empty($passwd)) {
@@ -2907,7 +2910,7 @@ function phpbbforum_external_login_regis
 
   //variable_set('phpbbforum_user_login_external', 1);
   // Update changed user settings
-  phpbbforum_update_user('login2', PHPBB_SYNC_TO_DRUPAL, $account, '', $passwd, $email);
+  phpbbforum_update_user('login2', $account, '', $passwd, $email);
   if (!empty($passwd)) {
     if (phpbb_api_get_user($login, $passwd)) {
       variable_set('phpbbforum_user_login', 1);
@@ -3032,7 +3035,7 @@ function _phpbbforum_drupal_login() {
 
     variable_set('phpbbforum_user_login', 1);
     // Update changed user settings
-    phpbbforum_update_user('login2', PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail);
+    phpbbforum_update_user('login2', $account, '', '', $account->mail);
   }
   elseif (variable_get('phpbbforum_user_login_external', 1)) {
     phpbbforum_external_login_register($username, '');
