Index: l10n_client.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/l10n_client/l10n_client.module,v
retrieving revision 1.1.2.9
diff -u -r1.1.2.9 l10n_client.module
--- l10n_client.module	22 Oct 2008 20:22:44 -0000	1.1.2.9
+++ l10n_client.module	16 Jan 2009 18:18:37 -0000
@@ -42,6 +42,17 @@
       'access' => $access,
       'type' => MENU_CALLBACK,
     );
+    
+    // admin interface for "Localization sharing"
+    $items[] = array(
+      'path' => 'admin/settings/locale/l10n_client',
+      'title' => 'Localization sharing',  
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('l10n_client_settings_form'),
+      'access' => array('administer languages'),
+      'weight' => 5,
+      'type' => MENU_LOCAL_TASK,
+    );    
   }
   
   return $items;
@@ -327,7 +338,7 @@
  * Extended: it can now save strings by lid or source, preferred the first one.
  */
 function l10n_client_save_string() {
-  global $locale;
+  global $user, $locale;
   
   if (user_access('use on-page translation')) {
     if (isset($_POST['source']) && isset($_POST['target']) && !empty($_POST['form_token']) && drupal_valid_token($_POST['form_token'], 'l10n_client_form')) {
@@ -335,6 +346,11 @@
       $report = array(0, 0, 0);
       _l10n_client_import_one_string_db($report, $locale, $_POST['lid'], $_POST['source'], $_POST['target'], $_POST['location']);
       locale_refresh_cache();
+
+      // Submit to remote server if enabled.
+      if (variable_get('l10n_client_use_server', FALSE) && user_access('submit translations to localization server') && !empty($user->l10n_client_key)) {
+        l10n_client_submit_translation($locale, $_POST['source'], $_POST['target'], $user->l10n_client_key, l10n_client_user_token($user));
+      }      
     }
   }
 }
@@ -400,3 +416,118 @@
 
   return $lid;
 }
+
+// -----------------------------------------------------------------------------
+
+/**
+ * Settings form for l10n_client.
+ *
+ * Enable users to set up a central server to share translations with.
+ */
+function l10n_client_settings_form() {
+  $form = array();
+  $form['l10n_client_use_server'] = array(
+    '#title' => t('Enable sharing translations with server'),
+    '#type' => 'checkbox',
+    '#default_value' => variable_get('l10n_client_use_server', FALSE),
+  );
+  $form['l10n_client_server'] = array(
+    '#title' => t('Address of localization server to use'),
+    '#type' => 'textfield',
+    '#description' => t('This server will be used to share translations submitted through the localization client interface. Each local submission will result in a call to this server as well. To be able to submit a translation there, you should be logged in there, but from then on, everything is automated. A list of servers you can use is available from the <a href="@project">Localization server project page</a>.', array('@project' => 'http://drupal.org/project/l10n_server')),
+    '#default_value' => variable_get('l10n_client_server', ''),
+  );
+  return system_settings_form($form);
+}
+
+/**
+ * Validation to make sure the provided server can handle our submissions.
+ *
+ * Make sure it supports the exact version of the API we will try to use.
+ */
+function l10n_client_settings_form_validate($form_id, $form_values) {
+  if ($form_values['l10n_client_use_server']) {
+    
+    // Try to invoke the remote string submission with a test request.
+    $response = xmlrpc($form_values['l10n_client_server'] .'/xmlrpc.php', 'l10n.server.test', '2.0');
+   
+    if ($response && !empty($response['name']) && !empty($response['version'])) {
+      if (empty($response['supported']) || !$response['supported']) {
+        form_set_error('l10n_client_server', t('The given server could not handle the v2.0 remote submission API.'));
+      }
+      else {
+        drupal_set_message(t('Verified that the specified server can handle remote string submissions. Supported languages: %languages.', array('%languages' => $response['languages'])));
+      }
+    }
+    else {
+      form_set_error('l10n_client_server', t('Invalid localization server address specified. Make sure you specified the right server address.'));
+    }
+  }
+}
+
+
+/**
+ * Implementation of hook_user().
+ * 
+ * Set up API key for localization server.
+ */
+function l10n_client_user($type, $edit, &$account, $category = NULL) { 
+  if ($type == 'form' && $category == 'account' && variable_get('l10n_client_use_server', FALSE) && user_access('submit translations to localization server', $account)) {
+    $form['l10n_client'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Localization client'),
+      '#weight' => 1,
+    );
+    // Build link to retrieve user key.
+    $server_link = variable_get('l10n_client_server', '') .'?q=translate/remote/userkey/'. l10n_client_user_token($account);
+    $form['l10n_client']['l10n_client_key'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Your Loalization Server API key'),
+      '#default_value' => !empty($account->l10n_client_key) ? $account->l10n_client_key : '',
+      '#description' => t('This is a unique key that will allow you to send translations to the remote server. To get your API key go to !server-link.', array('!server-link' => l($server_link, $server_link))),
+    );
+    return $form;
+  }
+}
+
+
+/**
+ * Get user based semi unique token. This will ensure user keys are unique for each client.
+ */
+function l10n_client_user_token($account = NULL) {
+  global $user;
+  $account = isset($account) ? $account : $user;
+  return md5('l10n_client'. $account->uid . drupal_get_private_key());
+}
+
+/**
+ * Submit translation to the server.
+ */
+function l10n_client_submit_translation($langcode, $source, $translation, $user_key, $user_token) {
+  $server_uid = current(split(':', $user_key));
+  $signature = md5($user_key . $langcode . $source . $translation . $user_token);
+  
+  $response = xmlrpc(
+    variable_get('l10n_client_server', '') .'/xmlrpc.php',
+    'l10n.submit.translation', 
+    $langcode,
+    $source,
+    $translation,
+    (int)$server_uid,
+    $user_token,
+    $signature
+  );
+
+  if (!empty($response) && isset($response['status'])) {
+    if ($response['status']) {
+      watchdog('l10n_client', t('Translation sent and accepted by remote server.'));
+    } else {
+      watchdog('l10n_client', t('Translation rejected by remote server. Reason: %reason', array('%reason' => $response['reason'])), WATCHDOG_WARNING);
+    }
+    return $response['status'];
+  }
+  else {
+    watchdog('l10n_client', t('The connection with the remote server failed with the following error: %error_code: %error_message.', array('%error_code' => xmlrpc_errno(), '%error_message' => xmlrpc_error_msg())), WATCHDOG_ERROR);
+    return FALSE;
+  }
+}

