Code

Shared Folder- Fixed handling of costum added acls .
[gosa.git] / plugins / admin / groups / class_groupMail.inc
index 544e0cb04b39aa1fc36bb0806abd5da8c2c3ed76..edde1af9411fabee4e29d35906e97df24c55fa2b 100644 (file)
 
 class mailgroup extends plugin
 {
-  /* CLI vars */
-  var $cli_summary= "Manage mail groups/shared folders";
-  var $cli_description= "Some longer text\nfor help";
-  var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-  var $method= "mailMethod";
-
-  /* plugin specific values */
-  var $mail= "";
-  var $uid= "";
-  var $cn= "";
-  var $gosaMailAlternateAddress= array();
-  var $gosaMailForwardingAddress= array();
-  var $gosaMailDeliveryMode= "[L        ]";
-  var $gosaMailServer= "";
-  var $gosaMailQuota= "";
-  var $gosaMailMaxSize= "";
-  var $gosaVacationMessage= "";
-  var $gosaSpamSortLevel= "";
-  var $gosaSpamMailbox= "";
-
-  var $quotaUsage= 0;
-  var $forward_dialog= FALSE;
-  var $members= array();
-  var $mailusers= array();
-  var $perms= array();
-  var $imapacl= array('anyone' => 'p', '%members%' => 'lrsp', '' => 'p');
+  var $uid                        = "";       // User id 
+  var $cn                         = "";       // cn
+  var $orig_cn                    = "";       // cn
+
+  var $method                     = "mailMethod"; // Used Mail method 
+  var $mmethod                    = "";           // Contains the gosa.conf MAILMETHOD
+  var $mail                       = "";           // Default mail address 
+
+  var $gosaMailAlternateAddress   = array();  // Set default Alternate Mail Adresses to empty array
+  var $gosaMailForwardingAddress  = array();  // Forwarding also empty
+
+  var $gosaMailServer             = "";       // Selected mailserver 
+  var $gosaMailQuota              = "";       // Defined Quota 
+  var $quotaUsage                 = 0;        // Currently used quota
+
+  var $gosaVacationMessage        = "";       // Vocation message 
+
+  var $imapacl                    = array('anyone'    => 'p',     // Set acls for everyone
+      '%members%' => 'lrspw',  // %members% are all group-members
+      ''          => 'p');    // Every user added gets this right
+
+
+  var $gosaSpamSortLevel          = "";     
+  var $gosaSpamMailbox            = "";
+  var $gosaSharedFolderTarget     ;
+
+  var $forward_dialog             = FALSE;    
+
+  var $members                    = array();  // Group members
+
+  var $mailusers                  = array();
+  var $perms                      = array();
+  var $gosaMailDeliveryMode       = "[L        ]";   // 
+  var $gosaMailMaxSize            = "";       // 
+  
+  var $remove_folder_from_imap    = true;
 
   /* Helper */
   var $indexed_acl= array();
   var $indexed_user= array();
 
+  /* Copy & paste */
+  var $CopyPasteVars          = array("quotaUsage","imapacl");
+
   /* attribute list for save action */
-  var $attributes= array("mail", "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize",
+  var $attributes= array( "mail",   "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize",
+      "gosaMailAlternateAddress", "gosaMailForwardingAddress",
       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox",
-      "gosaVacationMessage");
+      "acl","gosaSharedFolderTarget", "gosaVacationMessage");
+
   var $objectclasses= array("gosaMailAccount");
 
+  var $kolabFolderType_SubType = "";
+  var $kolabFolderType_Type = "";
 
-  /* constructor, if 'dn' is set, the node loads the given
-     'dn' from LDAP */
-  function mailgroup ($config, $dn= NULL, $ui= NULL)
+  function mailgroup ($config, $dn= NULL, $parent= NULL)
   {
-    /* Configuration is fine, allways */
-    $this->config= $config;
+    /* Initialise all available attributes ... if possible
+     */
+    plugin::plugin($config, $dn, $parent);
 
-    /* Load bases attributes */
-    plugin::plugin($config, $dn);
+    /* Save initial cn */
+    $this->orig_cn = $this->cn;
 
-    /* Set mailMethod to the one defined in gosa.conf */
+    /* Set mailMethod to the one defined in gosa.conf 
+     */
     if (isset($this->config->current['MAILMETHOD'])){
-      $method= $this->config->current['MAILMETHOD'];
-      if (class_exists("mailMethod$method")){
-        $this->method= "mailMethod$method";
-      } else {
-        print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
-      }
+      $this->mmethod= $this->config->current['MAILMETHOD'];
+    }
+
+    /* Check if selected mail method exists 
+     */
+    if (class_exists("mailMethod$this->mmethod")){
+      $this->method= "mailMethod$this->mmethod";
+    } else {
+      print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $this->mmethod));
     }
 
-    /* Convert cn to uid in case of existing entries */
+    /* Load Mailserver  
+     */
+    if(isset($this->attrs['gosaMailServer'][0])){
+      $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
+    }
+
+    /* Convert cn to uid in case of existing entry
+     */
     if (isset($this->attrs['cn'][0])){
       $this->uid= $this->attrs['cn'][0];
     }
 
-    if ($dn != NULL){
+    /* Get folder type */
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+      if(isset($this->attrs['kolabFolderType'])){
+        $tmp = split("\.",$this->attrs['kolabFolderType'][0]);
+        $this->kolabFolderType_Type = $tmp[0];
+        $this->kolabFolderType_SubType = $tmp[1];
+      }
+    }
+
+    /* If this ins't new mailgroup, read all required data from ldap
+     */
+    if (($dn != "new")&&($dn != NULL)){
 
-      /* Load attributes containing arrays */
+      /* Load attributes which represent multiple entries  
+       */
       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
+        $this->$val = array();
         if (isset($this->attrs["$val"]["count"])){
           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
             array_push($this->$val, $this->attrs["$val"][$i]);
@@ -76,11 +116,16 @@ class mailgroup extends plugin
         }
       }
 
-      /* Only do IMAP actions if gosaMailServer attribute is set */
+      /* Only do IMAP actions if gosaMailServer attribute is set 
+       */
       if (isset ($this->attrs["gosaMailServer"][0])){
+
+        /* Create new instance of our defined mailclass
+         */
         $method= new $this->method($this->config);
+
         if ($method->connect($this->attrs["gosaMailServer"][0])){
-          $quota= $method->getQuota($this->uid);
+        
 
           /* Maybe the entry is not saved in new style, get
              permissions from IMAP and convert them to acl attributes */
@@ -109,24 +154,44 @@ class mailgroup extends plugin
 
             /* Highest count wins as %members%, remove all members
                with the same acl */
-            $this->imapacl['%members%']= $leader;
+            if(!empty($leader)){
+              $this->imapacl['%members%']= $leader;
+            }
             foreach ($this->imapacl as $user => $acl){
               if ($this->acl == $leader && in_array($user, $this->attrs['memberUid'])){
                 unset($this->imapacl[$user]);
               }
             }
-            
-          }
+
+          } // ENDE ! isset ($this->attrs['acl'])
+          
+          /* Adapt attributes if needed */
+          $method->fixAttributesOnLoad($this);
           
+          /*  get Quota */
+          $quota= $method->getQuota($this->uid);
+
           /* Update quota values */
-          $this->quotaUsage= $quota['quotaUsage'];
-          $this->gosaMailQuota= $quota['gosaMailQuota'];
+          if(is_array($quota)){
+            if ($quota['gosaMailQuota'] == 2147483647){
+              $this->quotaUsage= "";
+              $this->gosaMailQuota= "";
+            } else {
+              $this->quotaUsage= $quota['quotaUsage'];
+              $this->gosaMailQuota= $quota['gosaMailQuota'];
+            }
+          }else{
+            $this->quotaUsage     = "";
+            $this->gosaMailQuota  = "";
+//            print_red(sprintf(_("Can't get quota information for '%s'."),$this->uid));
+          }
           $method->disconnect();
-        }
+        }   // ENDE $method->connect($this->attrs["gosaMailServer"][0])){
 
-      }
+      }   // ENDE gosaMailServer
+
+    }   // ENDE dn != "new"
 
-    }
 
     /* Get global filter config */
     if (!is_global("gmailfilter")){
@@ -139,29 +204,122 @@ class mailgroup extends plugin
     }
 
     /* Load permissions */
-    if (isset($this->attrs['acl'])){
-      for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
-        list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
-        $this->imapacl[$user]= $permission;
+    $tmp = array();
+    if(preg_match("/olab/i",$this->mmethod)){
+      $ldap = $this->config->get_ldap_link();
+
+      if (isset($this->attrs['acl'])){
+
+        for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
+          list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
+
+          /* Add to list */
+          $this->imapacl[$user]= $permission;
+
+          /* Get all user permissions sorted by acl, to detect the most used acl
+             This acl is then used for %members%
+           */
+          if ($user != "anyone" && $user != "%members%"){
+            $tmp[$permission][] = $user;
+          }
+
+          /* There is an entry in $this->imapacl like this this ... 
+             $this->attrs['imapacl']['anyone'] = "p";
+             $this->attrs['imapacl']['%members%'] = "lprs";
+             $this->attrs['imapacl'][''] = ""; <------ This is used to diplay an empty 
+             Field for special acls in our template.
+             If there is at least one special acl in out imapacl,
+             we don't need this entry anymore, because it is already displayed. 
+           */
+          if ($user != "anyone" && $user != "%members%"){
+            unset($this->imapacl['']);
+          }
+        }
+      }
+    }else{
+      foreach($this->imapacl as $user => $permission){
+        if ($user != "anyone" && $user != "%members%"){
+          $tmp[$permission][] = $user;
+        }
         if ($user != "anyone" && $user != "%members%"){
           unset($this->imapacl['']);
         }
       }
     }
 
+
+    /**
+     * Detect group member with same acl and replace them with %members%
+     **/
+
+    /* In this section we dectect which acl is tho most used 
+       This will be used as %members% acl  
+     */
+    $tmp2 = array(); 
+    foreach($tmp as $acl => $user){
+      $tmp2[count($tmp[$acl])]=$acl;
+    }
+    /* Most used at last 
+     */
+    ksort($tmp2);      
+
+    /* Assign last (most used acl) to %members% acl 
+     */
+    $str = array_pop($tmp2);
+    if(!empty($str)) {
+      $this->imapacl['%members%']=$str;
+    }
+
+    /* Open ldap connection 
+     */
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+
+    /* Remove those users, that use %members% acl && are member of this group. */
+    foreach($this->imapacl as $mail => $permission){
+      $ldap->search("(&(objectClass=person)(|(mail=".$mail.")(uid=".$mail.")))",array("uid"));
+      $atr = $ldap->fetch();
+      if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
+        if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
+          unset($this->imapacl[$mail]);
+        }
+      }
+    }
+
+    /**
+     * ENDE: Detect group member with same acl and replace them with %members%
+     **/
+
+
+    /* Append an empty entry, for special acl handling */
+    if(count($this->imapacl)==2){
+      $this->imapacl[''] ="lrsw";
+      $this->indexed_user[] = ''; 
+    }
+
+    /* Load Mailserver  
+     */
+    if(isset($this->attrs['gosaMailServer'][0])){
+      $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
+    }
     /* Fill translations */
-    $this->perms["lrs"]= _("read");
-    $this->perms["lrsp"]= _("post");
+    $this->perms["lrsw"]= _("read");
+    $this->perms["lrswp"]= _("post");
     $this->perms["p"]= _("external post");
-    $this->perms["lrsip"]= _("append");
+    $this->perms["lrswip"]= _("append");
     $this->perms["lrswipcd"]= _("write");
+    $this->perms["lrswipcda"]= _("admin");
+    $this->perms[""]= _("none");
   }
 
-
   function execute()
   {
+    /* Call parent execute */
+    //plugin::execute();
+
     /* Load templating engine */
     $smarty= get_smarty();
+    $display = "";
     if ($_SESSION['js']==FALSE){
       $smarty->assign("javascript", "false");
     } else {
@@ -170,12 +328,19 @@ class mailgroup extends plugin
 
     /* Do we need to flip is_account state? */
     if (isset($_POST['modify_state'])){
-      $this->is_account= !$this->is_account;
-    }
 
+      /* Onyl change account state if allowed */
+      if($this->is_account && $this->acl == "#all#"){
+        $this->is_account= !$this->is_account;
+      }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
+        $this->is_account= !$this->is_account;
+      }
+    }
+    
     /* Do we represent a valid account? */
     if (!$this->is_account && $this->parent == NULL){
-      $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
+
+      $display.= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
         _("This 'dn' has no valid mail extensions.")."</b>";
       return ($display);
     }
@@ -187,17 +352,33 @@ class mailgroup extends plugin
         $display= $this->show_header(_("Remove mail account"),
             _("This account has mail features enabled. You can disable them by clicking below."));
       } else {
-        $display= $this->show_header(_("Create mail account"),
+        $display.= $this->show_header(_("Create mail account"),
             _("This account has mail features disabled. You can enable them by clicking below."));
+
+        /* Show checkbox that allows us to remove imap entry too*/
+        if($this->initially_was_account){
+        
+          $c = "";
+          if($this->remove_folder_from_imap){
+            $c= " checked ";
+          }
+  
+          $display .= "<h2>Shared folder delete options</h2>
+                       <input class='center' type='checkbox' name='remove_folder_from_imap' value='1' ".$c."  
+                          title='"._("Remove shared folder from mail server database when entry gets removed in LDAP")."'>";
+          $display .= _("Remove the shared folder and all its contents after saving this account"); 
+        }
+
         return ($display);
       }
     }
 
     /* Add ACL? */
+    if (isset($_POST["add_acl"])){
+      $this->imapacl[""]= "lrsw";
+      $this->indexed_user[] = "";
+    }
     foreach ($this->indexed_user as $nr => $user){
-      if (isset($_POST["add_$nr"])){
-        $this->imapacl[""]= "l";
-      }
       if (isset($_POST["del_$nr"])){
         unset ($this->imapacl[$user]);
       }
@@ -262,7 +443,7 @@ class mailgroup extends plugin
 
     /* Delete forward email addresses */
     if (isset($_POST['delete_forwarder'])){
-      if (count($_POST['forwarder_list']) 
+      if (count($_POST['forwarder_list'])
           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
 
         $this->delForwarder ($_POST['forwarder_list']);
@@ -338,7 +519,8 @@ class mailgroup extends plugin
       }
 
       $acl= array($this->config->current['BASE'] => ":all");
-      $res= get_list($acl, "(&(objectClass=gosaMailAccount)$filter)", TRUE, $gmailfilter['depselect'], array("sn", "mail", "givenName"), TRUE);
+      $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $gmailfilter['depselect'],
+                     array("sn", "mail", "givenName"), GL_SUBSEARCH | GL_SIZELIMIT);
       $ldap->cd($gmailfilter['depselect']);
       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
       error_reporting (0);
@@ -356,6 +538,7 @@ class mailgroup extends plugin
 
       /* Show dialog */
       $smarty->assign("search_image", get_template_path('images/search.png'));
+      $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
       $smarty->assign("tree_image", get_template_path('images/tree.png'));
       $smarty->assign("infoimage", get_template_path('images/info.png'));
       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
@@ -377,6 +560,7 @@ class mailgroup extends plugin
     if (isset($this->imapacl['anyone'])){
       $smarty->assign("default_permissions", $this->imapacl['anyone']);
     }
+    $smarty->assign("member_permissions", "lrsp");
     if (isset($this->imapacl['%members%'])){
       $smarty->assign("member_permissions", $this->imapacl['%members%']);
     }
@@ -391,27 +575,26 @@ class mailgroup extends plugin
     foreach($this->imapacl as $user => $acl){
       if ($user != "anyone" && $user != "%members%"){
         $tmp.= "<tr><td><input name=\"user_$nr\" size=20 maxlength=60 ".
-               "value=\"$user\" $perm></td><td><select size=\"1\" name=\"perm_$nr\" $perm>";
+          "value=\"$user\" $perm></td><td><select size=\"1\" name=\"perm_$nr\" $perm>";
         foreach ($this->perms as $key => $value){
           if ($acl == $key){
-            $tmp.= "<option value=$key selected>$value</option>";
+            $tmp.= "<option value=\"$key\" selected>$value</option>";
           } else {
-            $tmp.= "<option value=$key>$value</option>";
+            $tmp.= "<option value=\"$key\">$value</option>";
           }
         }
         $tmp.= "</select>&nbsp;";
         if ($nr == $count - 1){
-          $tmp.= "<input type=submit value=\""._("Add")."\" ".
-                 "name=\"add_$nr\" $perm>";
         }
-        if ($count > 3){
+        if ($count > 2){
           $tmp.= "<input type=submit value=\""._("Remove")."\" ".
-                 "name=\"del_$nr\" $perm></td></tr>";
+            "name=\"del_$nr\" $perm></td></tr>";
         }
       }
       $this->indexed_user[$nr]= $user;
       $this->indexed_acl[$nr++]= $acl;
     }
+    $tmp.= "<tr><td></td><td></td><td><input type=submit value='"._("Add")."' name='add_acl' $perm>";
     $smarty->assign("plusattributes", $tmp);
 
     /* Show main page */
@@ -426,12 +609,35 @@ class mailgroup extends plugin
       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
     }
     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
-      $smarty->assign("quotausage", progressbar(($this->quotaUsage * 100)/ $this->gosaMailQuota,100,15,true));
+      $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
       $smarty->assign("quotadefined", "true");
     } else {
       $smarty->assign("quotadefined", "false");
     }
 
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+
+      $smarty->assign("kolab", TRUE);
+      $smarty->assign("JS",$_SESSION['js']);
+      $smarty->assign("kolabFolderType_Types",    array (   ''      => _('Unspecified'),  'mail' => _('Mails'),
+                                                            'task'  => _('Tasks') ,       'journal' => _('Journals'),
+                                                            'calendar' => _('Calendar'),       'contact' => _('Contacts'), 
+                                                            'note'  => _('Notes')));
+      if($this->kolabFolderType_Type == "mail"){
+        $smarty->assign("kolabFolderType_SubTypes", array(    
+              ''          => _('Unspecified'),  'inbox'     => _("Inbox")   , 
+              'drafts'    => _("Drafts"),       'sentitems' => _("Sent items"),
+              'junkemail' => _("Junk mail")));
+      }else{
+        $smarty->assign("kolabFolderType_SubTypes", array(  'default' => _("Default")));
+      }
+      $smarty->assign("kolabFolderType_Type",     $this->kolabFolderType_Type);
+      $smarty->assign("kolabFolderType_SubType",  $this->kolabFolderType_SubType);
+    }else{
+      $smarty->assign("kolab", FALSE);
+    }
+
+
     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
     return ($display);
   }
@@ -440,6 +646,13 @@ class mailgroup extends plugin
   /* remove object from parent */
   function remove_from_parent()
   {
+    /* Added these ObjectClass and Attributes, because they were not 
+       removed correctly, only in case of kolab ... 
+     */
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+      $this->attributes[]="acl";
+      $this->objectclasses[] = "kolabSharedFolder";
+    }
     /* include global link_info */
     $ldap= $this->config->get_ldap_link();
 
@@ -451,21 +664,24 @@ class mailgroup extends plugin
     $this->attrs['gosaMailForwardingAddress']= array();
     $this->attrs['gosaSharedFolderTarget']= array();
 
-    /* Keep uid */
-    unset ($this->attrs['uid']);
-    $ldap->cd($this->dn);
-    $ldap->modify($this->attrs);
-    show_ldap_error($ldap->get_error());
-
     /* Connect to IMAP server for account deletion */
-    if ($this->is_account){
+    if ($this->initially_was_account){
       $method= new $this->method($this->config);
-      if ($method->connect($this->attrs["gosaMailServer"][0])){
+      $method->fixAttributesOnRemove($this);
+      if ($method->connect($this->gosaMailServer) && $this->remove_folder_from_imap){
+
         /* Remove account from IMAP server */
         $method->deleteMailbox($this->uid);
         $method->disconnect();
       }
     }
+    /* Keep uid */
+    unset ($this->attrs['uid']);
+
+    $ldap->cd($this->dn);
+    $ldap->modify ($this->attrs); 
+    show_ldap_error($ldap->get_error(), _("Removing group mail settings failed"));
 
     /* Optionally execute a command after we're done */
     $this->handle_post_events("remove");
@@ -475,18 +691,36 @@ class mailgroup extends plugin
   /* Save data to object */
   function save_object()
   {
+
+    /* Add special kolab attributes */    
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+      if(isset($_POST['kolabFolderType_Type'])){
+        $this->kolabFolderType_Type = get_post("kolabFolderType_Type");
+        $this->kolabFolderType_SubType = get_post("kolabFolderType_SubType");
+      }
+    }
+  
+    /* Check if user wants to remove the shared folder from imap too */
+    if($this->initially_was_account && !$this->is_account){
+      if(isset($_POST['remove_folder_from_imap'])){
+        $this->remove_folder_from_imap = true;
+      }else{
+        $this->remove_folder_from_imap = false;
+      }
+    }
+
     /* Assemble mail delivery mode
        The mode field in ldap consists of values between braces, this must
        be called when 'mail' is set, because checkboxes may not be set when
        we're in some other dialog.
 
        Example for gosaMailDeliveryMode [LR        ]
-       L: Local delivery
-       R: Reject when exceeding mailsize limit
-       S: Use spam filter
-       V: Use vacation message
-       C: Use custom sieve script
-       I: Only insider delivery */
+L: Local delivery
+R: Reject when exceeding mailsize limit
+S: Use spam filter
+V: Use vacation message
+C: Use custom sieve script
+I: Only insider delivery */
     if (isset($_POST['mailedit'])){
 
       /* Save ldap attributes */
@@ -526,11 +760,12 @@ class mailgroup extends plugin
           if (!isset($_POST["user_$nr"])){
             continue;
           }
-          if ($_POST["user_$nr"] != $user || 
+          if ($_POST["user_$nr"] != $user ||
               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
             $this->is_modified= TRUE;
           }
           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
+          $this->indexed_user[$nr] = $_POST["user_$nr"];
         }
       }
     }
@@ -538,60 +773,125 @@ class mailgroup extends plugin
   }
 
 
+
   /* Save data to LDAP, depending on is_account we save or delete */
   function save()
   {
     $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
 
     /* Call parents save to prepare $this->attrs */
     plugin::save();
 
     /* Save arrays */
-    $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
-    $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
+    $this->attrs['gosaMailAlternateAddress']  = $this->gosaMailAlternateAddress;
+    $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
+    $this->attrs['gosaSharedFolderTarget']    = "share+".$this->uid;
+
+    /* Only do IMAP actions if we are not a template */
+    if(preg_match("/olab/i",$this->mmethod)){
+      if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
+        if(isset($this->attrs['gosaMailServer'][0])){
+          $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
+        }
+      }
+    }  
 
-    /* Save shared folder target */
-    $this->attrs['gosaSharedFolderTarget']= "share+".$this->uid;
+    /* Exchange '%member%' pseudo entry */
+    $memberacl= $this->imapacl['%members%'];
+    foreach ($this->members as $user){
+      if (!isset($this->imapacl[$user])){
+        $this->imapacl[$user]= $memberacl;
+      }
+    }
 
-    /* Save acl's */
-    $this->attrs['acl']= array();
+    /* Prepare kolab attributes to be written  */
+    if(preg_match("/olab/i",$this->mmethod)){
+      $this->attrs['acl']= array();
+      if(!empty($this->kolabFolderType_Type)){ 
+        $this->attrs['kolabFolderType'] = $this->kolabFolderType_Type.".".$this->kolabFolderType_SubType;
+      }else{
+        $this->attrs['kolabFolderType'] = array();
+      }
+    }else{
+      $this->attrs['acl']= array();;
+      unset($this->attrs['acl']);
+    }
+
+    /* Get naming attribute for mail accounts */  
+    $tmp = new $this->method($this->config);
+    $uattrib = $tmp->uattrib;
+
+    /* Create ACL array 
+     *  In case of kolab also create attrs['acl'] to save acls in ldap.
+     */
     foreach ($this->imapacl as $user => $acl){
-      if ($user == ""){
+
+      if (empty($user) || $user == ""){
+        unset($this->imapacl[$user]);
+      }
+  
+      /* Skip placeholder */
+      if (empty($user) || $user == "" || preg_match("/%members%/",$user)){
         continue;
       }
-      $this->attrs['acl'][]= "$user $acl";
-    }
 
-    /* Save data to LDAP */
-    $ldap->cd($this->dn);
-    $ldap->modify($this->attrs);
-    show_ldap_error($ldap->get_error());
+      /* Check if your is a real GOsa user  
+       * If user is a real GOsa user but do not have an email address - SKIP adding acls
+       * If user is a real GOsa user with an emal address - add acls  
+       */
+      $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail","uid"));
+      if($ldap->count()){
+        $attrs = $ldap->fetch();
 
-    /* Only do IMAP actions if we are not a template */
-    if (!$this->is_template){
-      $method= new $this->method($this->config);
-      if ($method->connect($this->gosaMailServer)){
-        $method->updateMailbox($this->uid);
-        $method->setQuota($this->uid, $this->gosaMailQuota);
+        /* Has the user a valid mail account? */
+        if(isset($attrs['mail'][0])){
 
-        /* Exchange '%member%' pseudo entry */
-        $memberacl= $this->imapacl['%members%'];
-        unset ($this->imapacl['%members%']);
-        foreach ($this->members as $user){
-          if (!isset($this->imapacl[$user])){
-            $this->imapacl[$user]= $memberacl;
+          $name = $attrs[$uattrib][0];
+
+          /* In case of kolab methods add acl attribute too */
+          if(preg_match("/olab/i",$this->mmethod)){
+            $this->attrs['acl'][]= $name." ".$acl;
+            unset($this->imapacl[$user]);
           }
+          $this->imapacl[$name] = $acl;
+        }else{
+
+          /* User has no mail extension */
+          unset($this->imapacl[$user]);
         }
-        
+      }else{
+        /* Seems to be a manually added acl */
+
+        /* In case of kolab methods add acl attribute too */
+        if(preg_match("/olab/i",$this->mmethod)){
+          $this->attrs['acl'][]= $user." ".$acl;
+        }
+        $this->imapacl[$user] = $acl;
+      }
+    }
+    if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
+      $method= new $this->method($this->config);
+      $method->fixAttributesOnStore($this);
+      if (($method->connect($this->gosaMailServer))){
+        $method->updateMailbox($this->uid);
+        $method->setQuota($this->uid, $this->gosaMailQuota);
         $method->setSharedFolderPermissions($this->uid, $this->imapacl);
         $method->disconnect();
       }
     }
 
+    /* Save data to LDAP */
+    $ldap->cd($this->dn);
+    $this->cleanup();
+    $ldap->modify ($this->attrs); 
+    show_ldap_error($ldap->get_error(), _("Saving group mail settings failed"));
+
     /* Optionally execute a command after we're done */
     if ($this->initially_was_account == $this->is_account){
       if ($this->is_modified){
-        $this->handle_post_events("mofify");
+        $this->handle_post_events("modify");
       }
     } else {
       $this->handle_post_events("add");
@@ -603,7 +903,12 @@ class mailgroup extends plugin
   {
     $ldap= $this->config->get_ldap_link();
 
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+
+    if(!$this->is_account) return array();
+    
+    //$message[] = $str;      
 
     /* must: mail */
     if ($this->mail == ""){
@@ -613,12 +918,12 @@ class mailgroup extends plugin
       $message[]= _("Please enter a valid email addres in 'Primary address' field.");
     }
     $ldap->cd($this->config->current['BASE']);
-    $ldap->search ("(&(!(objectClass=gosaUserTemplate))(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
-        $this->mail."))(!(uid=".$this->cn."))(!(cn=".$this->cn.")))");
+    $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
+        $this->mail."))(!(uid=".$this->orig_cn."))(!(cn=".$this->orig_cn.")))");
     if ($ldap->count() != 0){
       $message[]= _("The primary address you've entered is already in use.");
     }
-
+  
     /* Check quota */
     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
       if (!is_numeric($this->gosaMailQuota)) {
@@ -641,13 +946,13 @@ class mailgroup extends plugin
     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
       $message[]= _("You need to set the maximum mail size in order to reject anything.");
     }
-    
+
     if(ord($this->imapacl['anyone'][0])==194){
       $message[] = _("Please choose valid permission settings. Default permission can't be emtpy.");
     }
 
     if(empty($this->gosaMailServer)){
-      $message[] = _("Please Select a valid Mailserver.");
+      $message[] = _("Please select a valid mail server.");
     }
 
     return ($message);
@@ -689,7 +994,7 @@ class mailgroup extends plugin
   function delForwarder($addresses)
   {
     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
-                                      $this->gosaMailForwardingAddress);
+        $this->gosaMailForwardingAddress);
     $this->is_modified= TRUE;
   }
 
@@ -727,7 +1032,7 @@ class mailgroup extends plugin
   function delAlternate($addresses)
   {
     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
-                                     $this->gosaMailAlternateAddress);
+        $this->gosaMailAlternateAddress);
     $this->is_modified= TRUE;
   }
 
@@ -752,7 +1057,51 @@ class mailgroup extends plugin
     return ($name);
   }
 
-}
+  function getCopyDialog()
+  {
+    if(!$this->is_account) return("");
+
+    $smarty = get_smarty();
+    $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
+    $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
+    $smarty->assign("mail",$this->mail);
+    $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE));
+    $ret = array();
+    $ret['string'] = $display;
+    $ret['status'] = "";
+    return($ret);
+  }
+
+  function saveCopyDialog()
+  {
+    if(!$this->is_account) return;
+
+    /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
+    */
+    $this->execute();
+    if(isset($_POST['mail'])){
+      $this->mail = $_POST['mail'];
+    }
+  }
 
+  function PrepareForCopyPaste($source)
+  {
+    plugin::PrepareForCopyPaste($source);
+
+    /* Reset alternate mail addresses */
+    $this->gosaMailAlternateAddress = array();
+  }
+
+  
+  /* Remove given ACL for given member (uid,mail) ..
+   */
+  function removeUserAcl($index )
+  {
+    if(isset($this->imapacl[$index])){
+      unset($this->imapacl[$index]);
+    }
+  }
+
+}
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>