--- phpbbforum.module.orig	2010-11-24 21:53:34.000000000 +0100
+++ phpbbforum.module	2010-12-08 11:26:00.646527179 +0100
@@ -49,6 +49,8 @@ $_phpbb_result, $_phpbb_embed_mode, $php
 // Needed for handle_message_list_actions()
 global $refresh, $submit, $preview;
 
+$is_new_user ='0';
+
 _phpbbforum_set_globals();
 
 function _phpbbforum_set_globals() {
@@ -536,7 +538,8 @@ 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, PHPBB_SYNC_TO_DRUPAL, $account, array(), $node);
+        phpbbforum_update_user_profile($op, PHPBB_SYNC_TO_MASTER, $account, array(), $node); //edit by Su 17-06.2010
           if ($user->name != $account->name) {
             $phpbb_user->data = $phpbb_user_data;
           }
@@ -554,7 +557,8 @@ 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, PHPBB_SYNC_TO_PHPBB, $account, array(), $node);
+        phpbbforum_update_user_profile($op, PHPBB_SYNC_TO_MASTER, $account, array(), $node);  //edit by Su 17.06.2010
           if ($user->name != $account->name) {
             $phpbb_user->data = $phpbb_user_data;
           }
@@ -1504,7 +1508,8 @@ function phpbbforum_update_user_profile(
 }
 
 function _phpbbforum_set_user_data($op, $sync, &$account, $name, $pass, $mail, $edit) {
-  global $phpbb_user, $phpbb_config, $base_url, $phpbb_root_path, $phpEx;
+//  global $phpbb_user, $phpbb_config, $base_url, $phpbb_root_path, $phpEx;
+global $phpbb_user, $phpbb_config, $base_url, $phpbb_root_path, $phpEx, $is_new_user; //edit by Su
 
   $sync_to_admin = false;
   if ($sync == PHPBB_SYNC_TO_PHPBB) {
@@ -1517,7 +1522,7 @@ function _phpbbforum_set_user_data($op,
   $data = array();
 
   if (!$sync_to_admin) {
-    // not for admin
+    // not for admin: uid = 1
     $sync_data = true;
     if (!empty($name)) {
       if ($op == 'update' && $sync == PHPBB_SYNC_TO_PHPBB)
@@ -1565,42 +1570,43 @@ function _phpbbforum_set_user_data($op,
       }
     }
   }
-
-  if ($sync == PHPBB_SYNC_TO_DRUPAL || count($edit) != 0) {
+  
+  //if ($sync == PHPBB_SYNC_TO_DRUPAL || count($edit) != 0) {
+  if ($sync != PHPBB_SYNC_TO_MASTER || count($edit) != 0) {  //edit by Su 18.06.2010
     // Group sync
     $user_id = $phpbb_user->data['user_id'];
-
     if (variable_get('phpbbforum_user_roles_sync', 0)) {
-
       $user_roles = user_roles(TRUE);
       unset($user_roles[DRUPAL_AUTHENTICATED_RID]);
 
       if (count($user_roles))	{
         $roles = array();
         $sync_data = true;
-        if ($op == 'update' && $sync == PHPBB_SYNC_TO_PHPBB)
-          if (isset($edit['roles']))
+        if ($op == 'update' && $sync == PHPBB_SYNC_TO_PHPBB) {
+          if (isset($edit['roles'])) {
             $roles = $edit['roles'];
-          else
+            } //if
+          else {
             $sync_data = false;
-        else
+            } //else
+          }  
+        //else { 
+        else { //if -> ($sync == PHPBB_SYNC_TO_DRUPAL)
           $roles = $account->roles;
-        
-        $old_roles = $account->roles;  
+        } //else
+        $old_roles = $account->roles;
         unset($old_roles[DRUPAL_AUTHENTICATED_RID]);
         unset($old_roles[DRUPAL_ANONYMOUS_RID]);
         
         unset($roles[DRUPAL_AUTHENTICATED_RID]);
         unset($roles[DRUPAL_ANONYMOUS_RID]);
         $new_roles = $roles;
-
         $roles_is_added = false;
         $roles_is_deleted = false;
         $roles_is_equal = false;
         
         $roles_added = array();
         $roles_deleted = array();
-        
         if (count($new_roles) > count($old_roles)) {
           $roles_is_added = true;
           foreach ($new_roles as $rid => $rname) {
@@ -1623,37 +1629,36 @@ function _phpbbforum_set_user_data($op,
           $roles_is_equal = true;
           $roles_updated = $new_roles;
         }
-
-        if ($sync_data && ($sync != PHPBB_SYNC_TO_PHPBB || !$roles_is_equal)) {
-
+        if (empty($is_new_user)) $is_new_user ='0';
+        //if ($sync_data && ($sync != PHPBB_SYNC_TO_PHPBB || !$roles_is_equal)) {
+        //if ($sync_data && ($sync != PHPBB_SYNC_TO_PHPBB || !$roles_is_equal || $is_new_user)) { //edit by Su
+        if ($sync_data && ($sync != PHPBB_SYNC_TO_MASTER  || !$roles_is_equal || $is_new_user)) { //edit by Su 17.06.2010
+	  $is_new_user ='0'; //edit by Su
           $group_roles = array();
-
           foreach ($roles_updated as $rid => $rname) {
-            $group_ids = variable_get('phpbbforum_map_user_role_'. $rid, 0);
+            $group_ids = variable_get('phpbbforum_map_user_role_'. $rid, 0);          
             if (!empty($group_ids))	{
               $group_id_ary = array();
               foreach ($group_ids as $group_id => $group_id_name) {
                 if (!empty($group_id_name)) {
                   $group_id_ary[$group_id] = $group_id_name;
-                }
-              }
+                } //if
+              } //foreach
               if (!empty($group_id_ary)) {
                 $group_roles[$rid] = $group_id_ary;
-              }
-            }
-          }
-
+              } //if
+            } //if
+          } //foreach
           $phpbb_group_id_ary = array();
           $phpbb_group_row = phpbb_api_group_memberships(false, $user_id);
-          foreach ($phpbb_group_row as $id => $row) {
-            $group_id = $row['group_id'];
-            $phpbb_group_id_ary[$group_id] = $group_id;
-          }
-
+	  if(is_array($phpbb_group_row)){	//edit by Su
+	  	foreach ($phpbb_group_row as $id => $row) {
+          	  $group_id = $row['group_id'];
+          	  $phpbb_group_id_ary[$group_id] = $group_id;
+          	} //foreach
+          } //if	//edit by Su
           if ($sync == PHPBB_SYNC_TO_PHPBB) {
-
             $group_id_default = phpbb_get_user_group_id('REGISTERED');
-
             if ($roles_is_deleted) {
               // Delete user from group
               $group_del = array();
@@ -1663,32 +1668,33 @@ function _phpbbforum_set_user_data($op,
                   if (!empty($phpbb_group_id_ary[$group_id])) {
                     if ($group_id == $group_id_default || ($user_id == 2 && ($group_id == 4 || $group_id == 5))) {
                       continue;
-                    }
+                    } //if
                     $group_del[$group_id] = $group_id;
-                  }
-                }
-              }
+                  } //if
+                } //foreach
+              } //foreach
               foreach ($group_del as $group_id) {
                 $error = phpbb_api_group_user_del($group_id, $user_id);
-              }
-            }
+              } //foreach
+            } //if ($roles_is_deleted) {
             else {
               // Add user to group
               $group_add = array();
               foreach ($group_roles as $rid => $group_id_ary) {
                 foreach ($group_id_ary as $group_id => $group_id_name) {
-                  if (empty($phpbb_group_id_ary[$group_id])) {
+                  //if (empty($phpbb_group_id_ary[$group_id])) { 
+                  if (!empty($group_id)) {	//edit by Su
                     $group_add[$group_id] = $group_id;
-                  }
-                }
-              }
+                  } //if
+                } //foreach
+              } //foreach
               foreach ($group_add as $group_id) {
                 $error = phpbb_api_group_user_add($group_id, $user_id);
-              }
-            }
-          }
-          else { //if ($sync == PHPBB_SYNC_TO_DRUPAL) {
-
+              } //foreach
+            } //else
+          } 
+          //else { // if ($sync == PHPBB_SYNC_TO_DRUPAL) 
+          else  if ($sync == PHPBB_SYNC_TO_DRUPAL) { //edit by Su 18.06.2010
             //$group_user_roles = array();
             $group_user_roles_add = array();
 
@@ -1700,7 +1706,8 @@ function _phpbbforum_set_user_data($op,
                   if (!empty($group_id_name)) {
                     $group_id_ary[$group_id] = $group_id_name;
                     //$group_user_roles[$rid] = $group_id_ary;
-                    if (!isset($new_roles[$rid])) {
+                    if (!isset($new_roles[$rid])) {  //edit by Su 18.06.2010
+                    //if (isset($new_roles[$rid])) {
                       $group_user_roles_add[$rid] = $group_id_ary;
                     }
                   }
@@ -1740,8 +1747,8 @@ function _phpbbforum_set_user_data($op,
               $data += array('roles' => $new_roles);
             }
           }
-        }
-      } //if (!empty($drupal_roles))
+        } // edit by Su 17.06.2010 if ($sync_data && ($sync == PHPBB_SYNC_TO_PHPBB || !$roles_is_equal || $is_new_user)) {
+      } 
     } // if (variable_get('phpbbforum_user_roles_sync', 0)) {
 
     if (variable_get('phpbbforum_sync_timezone', 1)) {
@@ -2142,7 +2149,7 @@ function phpbbforum_update_user($op, $sy
       $profile_data = array();
       $profile_data = _phpbbforum_set_user_profile_data($op, $syncto, $account, $profile_data);
       //if (PHPBB_MODULE_DEBUG)
-        //drupal_set_message('PHPBB_user before update profile data'. print_r($profile_data, true));
+       //drupal_set_message('PHPBB_user before update profile data'. print_r($profile_data, true));
       if ($syncto == PHPBB_SYNC_TO_DRUPAL && count($profile_data) != 0) {
         _phpbbforum_profile_save_profile_data0($profile_data, $account);
       }
@@ -2205,7 +2212,8 @@ function _phpbbforum_user_edit_validate(
 
 
 function phpbbforum_user($op, &$edit, &$account, $category = NULL) {
-  global $phpbb_config, $phpbb_user, $user;
+//  global $phpbb_config, $phpbb_user, $user;
+  global $phpbb_config, $phpbb_user, $user, $is_new_user; //edit by Su
 
   if (!_phpbbforum_settings())
     return;
@@ -2219,7 +2227,8 @@ 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, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail);
+      phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', '', $account->mail); //edit by Su 17.06.2010
     }
     phpbb_api_logout();
     $message = 'Session closed for %name.';
@@ -2258,7 +2267,8 @@ function phpbbforum_user($op, &$edit, &$
           //if (PHPBB_MODULE_DEBUG)
           //  _phpbbforum_msg('phpbbforum_user login '. $name .' id='. $phpbb_user->data['user_id']);
           // Update
-          $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail, $edit);
+          //$update = phpbbforum_update_user($op, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail, $edit);
+          $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', '', $account->mail, $edit);//edit by Su 17.06.2010
           //phpbb_api_get_user($name, $pass);
           //if ($phpbb_user->data['user_id'] == ANONYMOUS)
           //	phpbb_api_login((int) variable_get('phpbbforum_cookie_length', PHPBB_COOKIELENGTH), $name, $pass);
@@ -2282,8 +2292,9 @@ function phpbbforum_user($op, &$edit, &$
           }
           else {
             //if (PHPBB_MODULE_DEBUG)
-              //_phpbbforum_set_message('User account: '. $name .' pass='. $pass .' update from Drupal');
-            $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', $pass, '');
+              _phpbbforum_set_message('User account: '. $name .' pass='. $pass .' update from Drupal');  //edit by Su 17.06.2010
+            //$update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', $pass, '');
+            $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', $pass, '');            
             if ($update) {
               //if (PHPBB_MODULE_DEBUG)
               //_phpbbforum_set_message('User account: '. $name .' pass='. $pass .' updated from Drupal');
@@ -2319,12 +2330,14 @@ function phpbbforum_user($op, &$edit, &$
             }
             else {
               $message = 'User account: %name created in phpBB.';
+              $is_new_user ='1'; //edit by Su
               $var = array('%name' => $name);
               _phpbbforum_watchdog($message, $var);
               _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, PHPBB_SYNC_TO_PHPBB, $account, '', '', '', $edit);
+		phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', '', '', $edit); //edit by Su 17.06.2010
                 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);
@@ -2358,7 +2371,7 @@ function phpbbforum_user($op, &$edit, &$
 
     if (phpbb_api_get_user($account->name, '')) { // user exists in PHPBB
       //if (PHPBB_MODULE_DEBUG)
-      //  _phpbbforum_set_message('User account: '. $account->name .' pass='. $pass .' found in phpBB');
+        _phpbbforum_set_message('User account: '. $account->name .' pass='. $pass .' found in phpBB');  //edit by Su 17.06.2010
       /*
       if (!empty($pass)) {
         $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, $account->name, $pass, '');
@@ -2392,8 +2405,9 @@ function phpbbforum_user($op, &$edit, &$
 
         if (phpbb_api_get_user($account->name, $pass)) {
           //if (PHPBB_MODULE_DEBUG)
-          //  _phpbbforum_set_message('*User account: '. $account->name .' pass='. $pass .' found');
-          phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', '', '', $edit);
+            _phpbbforum_set_message('*User account: '. $account->name .' pass='. $pass .' found'); //edit by Su 17.06.2010
+          //phpbbforum_update_user($op, PHPBB_SYNC_TO_PHPBB, $account, '', '', '', $edit);
+          phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', '', '', $edit); //edit by Su 17.06.2010
         }
         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);
@@ -2456,7 +2470,8 @@ 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, PHPBB_SYNC_TO_PHPBB, $account, $updatename, $pass, $updatemail, $edit);
+        $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, $updatename, $pass, $updatemail, $edit); //edit by Su 17.06.2010
         if ($user->name == $account->name) {
           if (empty($name))
             $name = $account->name;
@@ -2524,7 +2539,8 @@ 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, PHPBB_SYNC_TO_PHPBB, $account);
+        phpbbforum_update_user_profile($op, PHPBB_SYNC_TO_MASTER, $account); //edit by Su 17.06.2010
       }
       
       if ($user->name != $account->name) {
@@ -2542,7 +2558,8 @@ 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, PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail, $edit);
+      $update = phpbbforum_update_user($op, PHPBB_SYNC_TO_MASTER, $account, '', '', $account->mail, $edit); //edit by Su
     }
     
     if ($user->name != $account->name) {
@@ -2756,7 +2773,8 @@ 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', PHPBB_SYNC_TO_DRUPAL, $account, '', $passwd, $email);
+  phpbbforum_update_user('login2', PHPBB_SYNC_TO_MASTER, $account, '', $passwd, $email);  //edit by Su 17.06.2010
   if (!empty($passwd)) {
     if (phpbb_api_get_user($login, $passwd)) {
       variable_set('phpbbforum_user_login', 1);
@@ -2878,7 +2896,8 @@ 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', PHPBB_SYNC_TO_DRUPAL, $account, '', '', $account->mail);
+    phpbbforum_update_user('login2', PHPBB_SYNC_TO_MASTER, $account, '', '', $account->mail); //edit by Su 17.06.2010  
   }
   else if (variable_get('phpbbforum_user_login_external', 1)) {
     phpbbforum_external_login_register($username, '');
