Index: bakery.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/bakery/bakery.module,v
retrieving revision 1.57
diff -u -p -r1.57 bakery.module
--- bakery.module	29 Apr 2010 23:51:54 -0000	1.57
+++ bakery.module	5 May 2010 21:04:07 -0000
@@ -109,6 +109,11 @@ function bakery_user_logout($account) {
  */
 function bakery_user_presave(&$edit, $account, $category) {
   if (variable_get('bakery_is_master', 0)) {
+    // Invoke implementations of Bakery transmit hook for syncing arbitrary data.
+    $transmit = module_invoke_all('bakery_transmit', $edit, $account, $category);
+    if (!empty($transmit)) {
+      $_SESSION['bakery'] = $transmit;
+    }
     // We store email/name if they changed. We want to wait with doing
     // anything else until the changes are saved locally.
     foreach (variable_get('bakery_supported_fields', array('mail' => 'mail', 'name' => 'name')) as $type => $enabled) {
@@ -144,10 +149,10 @@ function bakery_user_update(&$edit, $acc
       $result = drupal_http_request($slave .'bakery/update', $options);
       if ($result->code != 200) {
         drupal_set_message(t('Error %error for site at %url', array('%error' => $result->code .' '. $result->error, '%url' => $slave)));
+        watchdog('bakery', 'Update error %error for user %uid on site %url', array('%error' => $result->code .' '. $result->error, '%uid' => $account->uid, '%url' => $slave), WATCHDOG_ERROR);
       }
       else {
         drupal_set_message($result->data);
-        // TODO: Roll back the change.
       }
     }
   }
@@ -566,7 +571,7 @@ function bakery_taste_stroopwafel_cookie
 
     if ($signature == $cookie['signature'] && $cookie['timestamp'] + variable_get('bakery_freshness', '3600') >= $_SERVER['REQUEST_TIME']) {
       $valid = TRUE;
-      $_SESSION['bakery'] = unserialize($cookie['data']);
+      $_SESSION['bakery']['data'] = unserialize($cookie['data']);
       $_SESSION['bakery']['uid'] = $cookie['uid'];
       $_SESSION['bakery']['category'] = $cookie['category'];
     }
@@ -639,24 +644,49 @@ function bakery_eat_stroopwafel_cookie()
     $message = t('Account not found on %slave.', array('%slave' => variable_get('site_name', '')));
   }
   else {
-    $fields = array();
+    $fields = $previous = array();
     foreach (variable_get('bakery_supported_fields', array('mail' => 'mail', 'name' => 'name')) as $type => $value) {
       if ($value) {
-        $fields[$type] = isset($stroopwafel[$type]) ? $stroopwafel[$type] : $account->$type;
+        $fields[$type] = isset($stroopwafel['data'][$type]) ? $stroopwafel['data'][$type] : $account->$type;
+        // Unset this value since we've put it into $fields.
+        unset($stroopwafel['data'][$type]);
+        // Hold onto previous values in case we need to roll back.
+        $previous[$type] = $account->$type;
       }
     }
     $status = user_save($account, $fields, $stroopwafel['category']);
     if ($status === FALSE) {
       watchdog('bakery', 'User update from name %name_old to %name_new, mail %mail_old to %mail_new failed.', array('%name_old' => $account->name, '%name_new' => $stroopwafel['name'], '%mail_old' => $account->mail, '%mail_new' => $stroopwafel['mail']), WATCHDOG_ERROR);
-      $message = t('There was a problem updating your account on %slave. Please contact the administrator.', array('%slave' => variable_get('site_name', '')));
-      header('HTTP/1.1 409 Conflict');
+      // Roll back.
+      user_save($account, $previous, $stroopwafel['category']);
     }
     else {
       watchdog('bakery', 'user updated name %name_old to %name_new, mail %mail_old to %mail_new.', array('%name_old' => $account->name, '%name_new' => $stroopwafel['name'], '%mail_old' => $account->mail, '%mail_new' => $stroopwafel['mail']));
-      $message = t('Successfully updated account on %slave.', array('%slave' => variable_get('site_name', '')));
+    }
+    if (!empty($stroopwafel['data'])) {
+      // More data to sync, invoke Bakery receive hook.
+      foreach (module_implements('bakery_receive') as $module) {
+        $function = $module .'_'. 'bakery_receive';
+        $result = call_user_func($function, $account, $stroopwafel['data']);
+        if ($result) {
+          watchdog('bakery', '!module bakery update success for user !uid.', array('!module' => $module, '!uid' => $account->uid));
+        }
+        else {
+          watchdog('bakery', '!module bakery update failed for user !uid.', array('!module' => $module, '!uid' => $account->uid), WATCHDOG_EMERG);
+          $status = FALSE;
+        }
+      }
     }
   }
-
+  
+  if ($status) {
+    $message = t('Your account was successfully updated on %slave.', array('%slave' => variable_get('site_name', '')));
+  }
+  else {
+    $message = t('There was a problem updating your account on %slave. Please contact the administrator.', array('%slave' => variable_get('site_name', '')));
+    header('HTTP/1.1 409 Conflict');
+  }
+  
   module_invoke_all('exit');
   print $message;
   exit();
