Code

Replaced in_array calls for gosa-plugins
[gosa.git] / gosa-plugins / mail / admin / groups / mail / class_groupMail.inc
index 5bdd24b240087ad5e9fb0f9587beaa8ae454a5ca..95acdd231d7b109120ea8721aae0585c3c2a826f 100644 (file)
@@ -27,7 +27,7 @@ class mailgroup extends plugin
   var $mailusers                  = array();  // Group member with mail account
   var $folder_acls                = array();
   var $MailMethod = NULL; 
-  var $forward_dialog             = FALSE;    
+  var $mailAddressSelect             = FALSE;    
   var $remove_folder_from_imap    = true;
   var $view_logged                = FALSE;
   var $mailDomainPart             = "";
@@ -54,7 +54,7 @@ class mailgroup extends plugin
      */
     foreach(array("uid","cn") as $attr){
       if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
-        $this->$attr = &$this->parent->by_object['group']->$attr;
+        $this->$attr = $this->parent->by_object['group']->$attr;
       }elseif(isset($this->attrs[$attr])){
         $this->$attr = $this->attrs[$attr][0];
       }
@@ -79,7 +79,7 @@ class mailgroup extends plugin
      *  mail address with the accounts cn.
      */ 
     if(!$this->mailMethod->isModifyableMail() && !$this->initially_was_account){
-      $this->mail = &$base_object->cn;
+      $this->mail = $base_object->cn;
     }
 
     /* Load folder_acls with defaults.
@@ -96,12 +96,11 @@ class mailgroup extends plugin
     $ldap = $this->config->get_ldap_link();
     if(isset($this->attrs['acl'])){
       for($i = 0; $i < $this->attrs['acl']['count'] ; $i++){
-        $str = trim($this->attrs['acl'][$i]);
 
         /* Be carefull here, since kolab22 uses spaces in the acls (herbert read anon/post)
          */
-        $name = trim(preg_replace("/^([^\s]*).*$/","\\1",$str));
-        $acl  = trim(preg_replace("/^[^\s]*+\s/","",$str));
+        $str = $this->attrs['acl'][$i];
+        list($name, $acl) = preg_split("/[ ]{1}/", $str, 2);
         if($name == "anyone") $name = "__anyone__";
         $this->folder_acls[$name] = $acl;
       }
@@ -155,7 +154,7 @@ class mailgroup extends plugin
         if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
           $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
           $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
-          if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
+          if(!in_array_strict($this->mailDomainPart,$this->mailDomainParts)){
             $this->mailDomainParts[] = $this->mailDomainPart;
           }
         }
@@ -186,7 +185,7 @@ class mailgroup extends plugin
       $member = $this->get_member();
       foreach($this->folder_acls as $user => $acl){
         if(preg_match("/^__/",$user)) continue;
-        if(!in_array($user,$member['mail'])) continue; 
+        if(!in_array_strict($user,$member['mail'])) continue; 
         if(!isset($acl_usage[$acl])) $acl_usage[$acl]=0;
         $acl_usage[$acl] ++;
         if($acl_usage[$acl] > $most_cnt){
@@ -197,7 +196,7 @@ class mailgroup extends plugin
       $this->folder_acls['__member__'] = $most_acl;  
       foreach($this->folder_acls as $name => $acl){
         if(preg_match("/^__/",$name)) continue;
-        if($acl == $most_acl && in_array($name,$member['mail'])){
+        if($acl == $most_acl && in_array_strict($name,$member['mail'])){
           unset($this->folder_acls[$name]);
         }
       }
@@ -226,6 +225,7 @@ class mailgroup extends plugin
     $ldap->cd($this->config->current['BASE']);
     if(isset($this->parent->by_object['group'])){
       foreach($this->parent->by_object['group']->memberUid as $uid){
+        if(!isset($this->parent->by_object['group']->dnMapping[$uid])) continue;
         $dn = $this->parent->by_object['group']->dnMapping[$uid];
         $member['all'][$uid] = $uid;
         if($ldap->object_match_filter($dn,"(&(objectClass=gosaMailAccount)(".$this->mailMethod->getUAttrib()."=*))")){
@@ -243,8 +243,8 @@ class mailgroup extends plugin
       foreach($users as $user){
         $member['all'][$user['uid'][0]] = $user['dn'];
         if(isset($user[$uattrib]) 
-            && in_array("gosaMailAccount",$user['objectClass']) 
-            && (in_array($user['uid'][0], $this->attrs['memberUid']))){
+            && in_array_strict("gosaMailAccount",$user['objectClass']) 
+            && (in_array_strict($user['uid'][0], $this->attrs['memberUid']))){
           $member['mail'][$user['uid'][0]] = $user[$uattrib][0];
         }
       }
@@ -324,32 +324,45 @@ class mailgroup extends plugin
     /****************
       Forward addresses
      ****************/
+
     if (isset($_POST['add_local_forwarder'])){
-      $this->forward_dialog= TRUE;
+      $this->mailAddressSelect= new mailAddressSelect($this->config, get_userinfo());
       $this->dialog= TRUE;
     }
-    if (isset($_POST['add_locals_cancel'])){
-      $this->forward_dialog= FALSE;
+
+    if (isset($_POST['mailAddressSelect_cancel'])){
+      $this->mailAddressSelect= FALSE;
       $this->dialog= FALSE;
     }
-    if (isset($_POST['add_locals_finish'])){
-      if (isset($_POST['local_list'])){
-        if($this->acl_is_writeable("gosaMailForwardingAddress")){
-          foreach ($_POST['local_list'] as $val){
-            if (!in_array ($val, $this->gosaMailAlternateAddress) &&
-                $val != $this->mail){
-              $this->addForwarder($val);
-              $this->is_modified= TRUE;
-            }
+
+    if (isset($_POST['mailAddressSelect_save'])){
+      if($this->acl_is_writeable("gosaMailForwardingAddress")){
+        $list = $this->mailAddressSelect->save();
+        foreach ($list as $entry){
+          $val = $entry['mail'][0];
+          if (!in_array_strict($val, $this->gosaMailAlternateAddress) && $val != $this->mail){
+            $this->addForwarder($val);
+            $this->is_modified= TRUE;
           }
         }
-        $this->forward_dialog= FALSE;
+        $this->mailAddressSelect= FALSE;
         $this->dialog= FALSE;
       } else {
         msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
       }
     }
 
+    if($this->mailAddressSelect instanceOf mailAddressSelect){
+      $used  = array();
+      $used['mail'] = array_values($this->gosaMailAlternateAddress);
+      $used['mail'] = array_merge($used['mail'], array_values($this->gosaMailForwardingAddress));
+      $used['mail'][] = $this->mail;
+
+      // Build up blocklist
+      session::set('filterBlacklist', $used);
+      return($this->mailAddressSelect->execute());
+    }
+
     if (isset($_POST['add_forwarder'])){
       if ($_POST['forward_address'] != ""){
         $address= $_POST['forward_address'];
@@ -364,7 +377,7 @@ class mailgroup extends plugin
             }
           }
         } elseif ($address == $this->mail
-            || in_array($address, $this->gosaMailAlternateAddress)) {
+            || in_array_strict($address, $this->gosaMailAlternateAddress)) {
           msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
         } else {
           $valid= TRUE;
@@ -380,9 +393,6 @@ class mailgroup extends plugin
     if (isset($_POST['delete_forwarder'])){
       $this->delForwarder ($_POST['forwarder_list']);
     }
-    if ($this->forward_dialog){
-      return($this->display_forward_dialog());
-    } 
 
  
     /****************
@@ -408,10 +418,14 @@ class mailgroup extends plugin
       }
       if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
         $ui= get_userinfo();
-        if ($user != $ui->username){
-          msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."&nbsp;".
-              sprintf(_("Address is already in use by user '%s'."), $user), ERROR_DIALOG);
+        $addon= "";
+        if ($user[0] == "!") {
+          $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
+        } else {
+          $addon= sprintf(_("Address is already in use by user '%s'."), $user);
         }
+        msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
+            "$addon</i>", ERROR_DIALOG);
       }
     }
     if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
@@ -485,7 +499,7 @@ class mailgroup extends plugin
       Multi edit support 
      ******/
     foreach($this->attributes as $attr){
-      if(in_array($attr,$this->multi_boxes)){
+      if(in_array_strict($attr,$this->multi_boxes)){
         $smarty->assign("use_".$attr,TRUE);
       }else{
         $smarty->assign("use_".$attr,FALSE);
@@ -494,7 +508,7 @@ class mailgroup extends plugin
 
     /* Multiple support handling */
     foreach(array("kolabFolderType") as $attr){
-      if(in_array($attr,$this->multi_boxes)){
+      if(in_array_strict($attr,$this->multi_boxes)){
         $smarty->assign("use_".$attr,TRUE);
       }else{
         $smarty->assign("use_".$attr,FALSE);
@@ -607,7 +621,7 @@ class mailgroup extends plugin
       /* Get posted mail domain part, if necessary
        */
       if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
-        if(in_array(get_post('MailDomain'), $this->mailDomainParts)){
+        if(in_array_strict(get_post('MailDomain'), $this->mailDomainParts)){
           $this->mailDomainPart = get_post('MailDomain');
         }
       }
@@ -895,7 +909,7 @@ class mailgroup extends plugin
 
     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
  
-      if (in_array($val, $skip)){
+      if (in_array_strict($val, $skip)){
         continue;
       }
 
@@ -1026,7 +1040,7 @@ class mailgroup extends plugin
     $this->gosaMailForwardingAddress_Some = array();
     if(isset($all['gosaMailForwardingAddress'])){
       for($i = 0 ; $i < $all['gosaMailForwardingAddress']['count'] ; $i++){
-        if(!in_array($all['gosaMailForwardingAddress'][$i],$this->gosaMailForwardingAddress)){
+        if(!in_array_strict($all['gosaMailForwardingAddress'][$i],$this->gosaMailForwardingAddress)){
           $this->gosaMailForwardingAddress_Some[] = $all['gosaMailForwardingAddress'][$i];
         }
       }
@@ -1078,11 +1092,11 @@ class mailgroup extends plugin
     $ret = plugin::get_multi_edit_values();
     $ret['Forward_some'] = $this->gosaMailForwardingAddress_Some;    
     $ret['Forward_all'] = $this->gosaMailForwardingAddress;    
-    if(in_array('kolabFolderType',$this->multi_boxes)){
+    if(in_array_strict('kolabFolderType',$this->multi_boxes)){
       $ret['kolabFolderTypeType'] = $this->kolabFolderTypeType;
       $ret['kolabFolderTypeSubType'] = $this->kolabFolderTypeSubType;
     }
-    if(in_array("acl",$this->multi_boxes)){
+    if(in_array_strict("acl",$this->multi_boxes)){
       $ret['imapacl'] = $this->imapacl;
     }
     return($ret);
@@ -1092,7 +1106,7 @@ class mailgroup extends plugin
   {
     $forward = array();
     foreach($attrs['Forward_some'] as $addr){
-      if(in_array($addr,$this->gosaMailForwardingAddress)){
+      if(in_array_strict($addr,$this->gosaMailForwardingAddress)){
         $forward[] = $addr;
       }
     }
@@ -1104,90 +1118,6 @@ class mailgroup extends plugin
   }
 
 
-  /*! \brief  Displays a dialog that allows mail address selection.
-   */
-  function display_forward_dialog()
-  {
-    restore_error_handler();
-
-    $smarty = get_smarty();
-    $ldap= $this->config->get_ldap_link();
-
-    /* Save data */
-    $mailfilter= session::get("mailfilter");
-    foreach( array("depselect", "muser", "regex") as $type){
-      if (isset($_POST[$type])){
-        $mailfilter[$type]= $_POST[$type];
-      }
-    }
-    if (isset($_GET['search'])){
-      $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
-      if ($s == "**"){
-        $s= "*";
-      }
-      $mailfilter['regex']= $s;
-    }
-    session::set("mailfilter", $mailfilter);
-
-    /* Get actual list */
-    $mailusers= array ();
-    if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
-      $regex= $mailfilter['regex'];
-      $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
-    } else {
-      $filter= "";
-    }
-    if ($mailfilter['muser'] != ""){
-      $user= $mailfilter['muser'];
-      $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
-    }
-
-    /* Add already present people to the filter */
-    $exclude= "";
-    foreach ($this->gosaMailForwardingAddress as $mail){
-      $exclude.= "(mail=$mail)";
-    }
-    if ($exclude != ""){
-      $filter.= "(!(|$exclude))";
-    }
-    $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $mailfilter['depselect'],
-        array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
-    $ldap->cd($mailfilter['depselect']);
-    $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
-    while ($attrs= $ldap->fetch()){
-      if(preg_match('/%/', $attrs['mail'][0])){
-        continue;
-      }
-      $name= $this->make_name($attrs);
-      $mailusers[$attrs['mail'][0]]= $name."&lt;".
-        $attrs['mail'][0]."&gt;";
-    }
-    natcasesort ($mailusers);
-    reset ($mailusers);
-
-    /* Show dialog */
-    $smarty->assign("search_image", get_template_path('images/lists/search.png'));
-    $smarty->assign("usearch_image", get_template_path('images/lists/search-user.png'));
-    $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
-    $smarty->assign("infoimage", get_template_path('images/info.png'));
-    $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
-    $smarty->assign("mailusers", $mailusers);
-    if (isset($_POST['depselect'])){
-      $smarty->assign("depselect", $_POST['depselect']);
-    }
-    $smarty->assign("deplist", $this->config->idepartments);
-    $smarty->assign("apply", apply_filter());
-    $smarty->assign("alphabet", generate_alphabet());
-    $smarty->assign("hint", print_sizelimit_warning());
-    foreach( array("depselect", "muser", "regex") as $type){
-      $smarty->assign("$type", $mailfilter[$type]);
-    }
-    $smarty->assign("hint", print_sizelimit_warning());
-    $display= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
-    return ($display);
-  }
-
-
   /*! \brief  Add given mail address to the list of forwarders.
    */
   function addForwarder($address)
@@ -1238,15 +1168,18 @@ class mailgroup extends plugin
     /* Is this address already assigned in LDAP? */
     $ldap->cd ($this->config->current['BASE']);
     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
-        "(gosaMailAlternateAddress=$address)))");
+        "(gosaMailAlternateAddress=$address)))", array("cn", "uid"));
 
     if ($ldap->count() > 0){
       $attrs= $ldap->fetch ();
+      if (!isset($attrs["uid"])) {
+        return ("!".$attrs["cn"][0]);
+      }
       return ($attrs["uid"][0]);
     }
 
     /* Add to list of alternates */
-    if (!in_array($address, $this->gosaMailAlternateAddress)){
+    if (!in_array_strict($address, $this->gosaMailAlternateAddress)){
       $this->gosaMailAlternateAddress[]= $address;
     }
 
@@ -1262,7 +1195,7 @@ class mailgroup extends plugin
    */
   function delAlternate($addresses)
   {
-    if(empty($address)) continue;
+    if(!count($addresses)) return;
     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
         $this->gosaMailAlternateAddress);
     $this->is_modified= TRUE;