From 69f18a09aec6e352ff021cd9c5db806f341b7e48 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 01 May 2009 15:04:26 -0400
Subject: [PATCH] Add plugin hooks for creating/saving/deleting identities and contacts

---
 program/steps/settings/save_identity.inc |  180 ++++++++++++++++++++++++++++-------------------------------
 1 files changed, 85 insertions(+), 95 deletions(-)

diff --git a/program/steps/settings/save_identity.inc b/program/steps/settings/save_identity.inc
index 39bf8fa..754e86c 100644
--- a/program/steps/settings/save_identity.inc
+++ b/program/steps/settings/save_identity.inc
@@ -5,7 +5,7 @@
  | program/steps/settings/save_identity.inc                              |
  |                                                                       |
  | This file is part of the RoundCube Webmail client                     |
- | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
+ | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
@@ -19,118 +19,108 @@
 
 */
 
-$a_save_cols = array('name', 'email', 'organization', 'reply-to', 'bcc', 'default');
+define('IDENTITIES_LEVEL', intval($RCMAIL->config->get('identities_level', 0)));
+
+$a_save_cols = array('name', 'email', 'organization', 'reply-to', 'bcc', 'standard', 'signature', 'html_signature');
+$a_html_cols = array('signature');
+$a_boolean_cols = array('standard', 'html_signature');
+$updated = $default_id = false;
+
+// check input
+if (empty($_POST['_name']) || (empty($_POST['_email']) && IDENTITIES_LEVEL != 1 && IDENTITIES_LEVEL != 3))
+  {
+  $OUTPUT->show_message('formincomplete', 'warning');
+  rcmail_overwrite_action('edit-identity');
+  return;
+  }
+
+
+$save_data = array();
+foreach ($a_save_cols as $col)
+{
+  $fname = '_'.$col;
+  if (isset($_POST[$fname]))
+    $save_data[$col] = get_input_value($fname, RCUBE_INPUT_POST, in_array($col, $a_html_cols));
+}
+
+// set "off" values for checkboxes that were not checked, and therefore
+// not included in the POST body.
+foreach ($a_boolean_cols as $col)
+{
+  $fname = '_' . $col;
+  if (!isset($_POST[$fname]))
+    $save_data[$col] = 0;
+}
+
+// unset email address if user has no rights to change it
+if (IDENTITIES_LEVEL == 1 || IDENTITIES_LEVEL == 3)
+  unset($save_data['email']);
 
 
 // update an existing contact
 if ($_POST['_iid'])
+{
+  $iid = get_input_value('_iid', RCUBE_INPUT_POST);
+  $plugin = $RCMAIL->plugins->exec_hook('save_identity', array('id' => $iid, 'record' => $save_data));
+  $save_data = $plugin['record'];
+
+  if (!$plugin['abort'] && ($updated = $USER->update_identity($iid, $save_data)))
   {
-  $a_write_sql = array();
-
-  foreach ($a_save_cols as $col)
-    {
-    $fname = '_'.$col;
-    if (!isset($_POST[$fname]))
-      continue;
-
-    $a_write_sql[] = sprintf("`%s`='%s'", $col, addslashes($_POST[$fname]));
-    }
-
-  if (sizeof($a_write_sql))
-    {
-    $DB->query(sprintf("UPDATE %s
-                        SET    %s
-                        WHERE  identity_id=%d
-                        AND    user_id=%d
-                        AND    del!='1'",
-                       get_table_name('identities'),
-                       join(', ', $a_write_sql),
-                       $_POST['_iid'],
-                       $_SESSION['user_id']));
-                       
-    $updated = $DB->affected_rows();
-    }
-       
-  if ($updated)
-    {
-    show_message('successfullysaved', 'confirmation');
-
-    // mark all other identities as 'not-default'
-    $DB->query(sprintf("UPDATE %s
-                        SET    `default`='0'
-                        WHERE  identity_id!=%d
-                        AND    user_id=%d
-                        AND    del!='1'",
-                       get_table_name('identities'),
-                       $_POST['_iid'],
-                       $_SESSION['user_id']));
+    $OUTPUT->show_message('successfullysaved', 'confirmation');
+    
+    if (!empty($_POST['_standard']))
+      $default_id = get_input_value('_iid', RCUBE_INPUT_POST);
     
     if ($_POST['_framed'])
-      {
+    {
       // update the changed col in list
       // ...      
-      }
-    }
-  else
-    {
-    // show error message
-
     }
   }
-
-// insert a new contact
-else
+  else if ($plugin['abort'] || $DB->is_error())
   {
-  $a_insert_cols = $a_insert_values = array();
+    // show error message
+    $OUTPUT->show_message('errorsaving', 'error', null, false);
+    rcmail_overwrite_action('edit-identity');
+    return;
+  }
+}
 
-  foreach ($a_save_cols as $col)
-    {
-    $fname = '_'.$col;
-    if (!isset($_POST[$fname]))
-      continue;
+// insert a new identity record
+else if (IDENTITIES_LEVEL < 2)
+{
+  if (IDENTITIES_LEVEL == 1)
+    $save_data['email'] = $RCMAIL->user->get_username();
+
+  $plugin = $RCMAIL->plugins->exec_hook('create_identity', array('id' => $iid, 'record' => $save_data));
+  $save_data = $plugin['record'];
+
+  if (!$plugin['abort'] && $save_data['email'] && ($insert_id = $USER->insert_identity($save_data)))
+  {
+    $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
     
-    $a_insert_cols[] = "`$col`";
-    $a_insert_values[] = sprintf("'%s'", addslashes($_POST[$fname]));
-    }
-    
-  if (sizeof($a_insert_cols))
-    {
-    $DB->query(sprintf("INSERT INTO %s
-                        (user_id, %s)
-                        VALUES (%d, %s)",
-                       get_table_name('identities'),
-                       join(', ', $a_insert_cols),
-                       $_SESSION['user_id'],
-                       join(', ', $a_insert_values)));
-                       
-    $insert_id = $DB->insert_id();
-    }
-    
-  if ($insert_id)
-    {
     $_GET['_iid'] = $insert_id;
 
-    if ($_POST['_framed'])
-      {
-      // add contact row or jump to the page where it should appear
-      // ....
-      }
-    }
-  else
-    {
-    // show error message
-    }
+    if (!empty($_POST['_standard']))
+      $default_id = $insert_id;
   }
+  else
+  {
+    // show error message
+    $OUTPUT->show_message('errorsaving', 'error', null, false);
+    rcmail_overwrite_action('edit-identity');
+    return;
+  }
+}
+else
+  $OUTPUT->show_message('opnotpermitted', 'error');
 
+
+// mark all other identities as 'not-default'
+if ($default_id)
+  $USER->set_default($default_id);
 
 // go to next step
-if ($_POST['_framed'])
-  $_action = 'edit-identitiy';
-else
-  $_action = 'identities';
-  
+rcmail_overwrite_action('identities');
 
-// overwrite action variable  
-$OUTPUT->add_script(sprintf("\n%s.set_env('action', '%s');", $JS_OBJECT_NAME, $_action));  
-
-?>
\ No newline at end of file
+?>

--
Gitblit v1.9.1