Code

Updated mailForward selection list
[gosa.git] / gosa-plugins / mail / personal / mail / class_mail-methods.inc
index 3d5698a947041a7f81bbc4530ff7bf6ba5d8fa8d..a17d77e4a0462d88de166d22461705d524590a50 100644 (file)
@@ -4,7 +4,10 @@
 class mailMethod{
 
   /* Allow modification of account_ids for existing mail accounts */
-  protected $modifyableMail = TRUE;
+  protected $modifyableMail   = TRUE;
+
+  /* Allow modification of the mail server attribute existing mail accounts */
+  protected $modifyableServer = TRUE;
 
   /* Enforces same value for 'mail' as used for 'cn' */
   protected $mailEqualsCN   = FALSE; 
@@ -16,15 +19,41 @@ class mailMethod{
   protected $user_prefix    = "user.";  
   protected $share_prefix   = "share.";
 
+  /* Account ID creation 
+
+      !!Depends on the attributes 'user_prefix'/'share_prefix' and the option 'cyrusUseSlashes'
+
+      Examples - based on defaults : 
+        %PREFIX%    => "user." or "user/" (Depending on cyrusUseSlashes=FALSE/TRUE) 
+        %CN%        => "technik"          (The groups cn)
+        %UID%       => "herbert"          (The users uid)
+        %MAIL%      => "herbert@domain.de"(The mail address)
+        %DOMAIN%    => "domain.de"        (The domain part of the specified mail)
+        %MAILPART%  => "herbert"          (The mail address without domain)
+        %UATTRIB%   => "herbert"/"herbert@domains.de"
+                                            (Configured in gosa.conf mailAttribute="mail"/"uid")
+   */
+  protected $user_id    = "%PREFIX%%UATTRIB%";  
+  protected $share_id   = "%PREFIX%%UATTRIB%";
+
   /* Create accounts in cyrus style with '/' instead of '.' */
   protected $cyrusUseSlashes= FALSE;
 
+  /* gosaSharedFolderTarget settings, 
+   *  E.g. 
+   *    For an accountID like: 'share/herberts.folder@gonicus.de' the value 'dummy+' 
+   *    will result in gosaSharedFolderTarget: dummy+share/herberts.folder@gonicus.de
+   */
+  protected $gosaSharedPrefix = '';  
+
   /* The atribute mapping for this class  Source --> Destination */
   protected $attributes     = array();
   protected $userObjectClasses = array();
   protected $shareObjectClasses = array();
 
-  /* Enabled mail domain selection. If enabled getMailDomains must the domain parts */ 
+  /* Enabled mail domain selection. If enabled getMailDomains() have to return an array 
+   *  with the domain parts.
+   */ 
   protected $enableDomainSelection= FALSE;
   protected $enableQuota          = TRUE;
   protected $enableSieveManager   = FALSE;
@@ -43,7 +72,20 @@ class mailMethod{
   protected $error              = "";
   protected $parent             = NULL;   
   protected $MailServer         = "";
+
+  protected $default_acls = array("__anyone__" => "p", "__member__" => "lrswp");
+
+  protected $acl_map = array(
+      "lrsw"     => "read",
+      "lrswp"    => "post",
+      "p"        => "external post",
+      "lrswip"   => "append",
+      "lrswipcd" => "write",
+      "lrswipcda"=> "admin",
+      " "         => "none");
+
+  protected $acl_mapping = array();
+
 
   /*! \brief  Constructs the mail class 
       @param  Object  Config  The GOsa configuration object
@@ -68,6 +110,7 @@ class mailMethod{
     }else{
       $this->type = $type;
     }
+
   }
 
   
@@ -79,9 +122,9 @@ class mailMethod{
      */
     if ($this->config->get_cfg_value("cyrusUseSlashes") == "true"){
       $this->cyrusUseSlashes = TRUE;
-      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "cyrusUseSlashes: <b>Enabled</b>","");
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "","<b>MAIL:</b> cyrusUseSlashes: <b>Enabled</b>");
     }else{
-      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "cyrusUseSlashes: <b>Disabled</b>","");
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "","<b>MAIL:</b> cyrusUseSlashes: <b>Disabled</b>");
     }
 
     /* Check if the mail account identification attribute
@@ -93,11 +136,38 @@ class mailMethod{
         $this->uattrib = $new_uattrib;
       }else{
         @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$new_uattrib."</b>",
-            "Unsupported 'mailAttribute' in gosa configuration specified");
+          "<b>MAIL:</b> Unsupported 'mailAttribute' in gosa configuration specified");
         msg_dialog::display(_("Configuration error"), 
             sprintf(_("The configured mail attribute '%s' is unsupported!"), $new_uattrib), ERROR_DIALOG);
       }
     }
+
+    /* Create ACL map */
+    foreach($this->acl_map as $acl => $name){
+      $this->acl_mapping[$acl] = _($name);
+    }
+
+    /* Check if we have an individual user/folder creation syntax 
+     */
+    $tmp = $this->config->get_cfg_value("mailUserCreation");
+    if(!empty($tmp)){
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<i>".$tmp."</i>",
+          "<b>MAIL:</b> User creation set to");
+      $this->user_id  = $tmp;
+    }
+    $tmp = $this->config->get_cfg_value("mailFolderCreation");
+    if(!empty($tmp)){
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<i>".$tmp."</i>",
+          "<b>MAIL:</b> Shared folder creation set to");
+      $this->share_id = $tmp;
+    }
+    $tmp = $this->config->get_cfg_value("gosaSharedPrefix");
+    if(!empty($tmp)){
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<i>".$tmp."</i>",
+          "<b>MAIL:</b> Shared folder prefix set to");
+      $this->gosaSharedPrefix = $tmp;
+    }
+
     $this->build_account_id();
     $this->initial_account_id = $this->account_id;
   }
@@ -135,6 +205,7 @@ class mailMethod{
     }else{
       $this->parent->attrs['objectClass'] = 
         array_remove_entries_ics($this->shareObjectClasses, $this->parent->attrs['objectClass']);
+      $this->parent->attrs['gosaSharedFolderTarget'] =array();
     }
     foreach($this->attributes as $source => $dest){
       $this->attrs[$dest]   = array();
@@ -163,6 +234,13 @@ class mailMethod{
         $this->parent->attrs['objectClass'][] = $oc;
       }
     }
+  
+    // Add gosaSharedFolderTarget for groups.
+    $this->build_account_id();
+    if($this->type == "group"){ 
+      $this->parent->attrs['gosaSharedFolderTarget'] = 
+        $this->gosaSharedPrefix.$this->account_id;
+    }
   }
 
 
@@ -173,8 +251,8 @@ class mailMethod{
   public function connect()
   {
     $this->reset_error();
-    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Connect method</b>: ".get_class($this),"");
-    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Current server</b>: ".$this->MailServer,"");
+    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>MAIL: Connect method</b>: ".get_class($this));
+    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>MAIL: Current server</b>: ".$this->MailServer);
  
     $this->connected = TRUE;
     return(TRUE);
@@ -197,7 +275,7 @@ class mailMethod{
   {
     $this->reset_error();
     if($this->is_connected()){
-      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Disconnect method</b>: ".get_class($this),"");
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"","<b>MAIL: Disconnect method</b>: ".get_class($this));
       $this->connected =FALSE; 
     }
   }
@@ -228,6 +306,12 @@ class mailMethod{
   }
 
 
+  public function isModifyableServer()
+  {
+    return($this->modifyableServer);
+  }
+
+
   /*! \brief  Returns TRUE if the action caused an error.
       @return Boolean TRUE on error else FALSE
    */
@@ -255,9 +339,11 @@ class mailMethod{
   {
     /* Build account identicator */
     if($this->type == "user"){
-      $str = $this->user_prefix;
+      $prefix = $this->user_prefix;
+      $acc_string = $this->user_id;
     }else{
-      $str = $this->share_prefix;
+      $prefix = $this->share_prefix;
+      $acc_string = $this->share_id;
     }
 
     /* Create account prefix and respect "cyrusUseSlashes" 
@@ -265,14 +351,41 @@ class mailMethod{
      */
     $uattrib = $this->uattrib;
     if($this->cyrusUseSlashes){
-      $str = preg_replace('/([^\\\\])\./',"\\1/",$str);
+      $prefix     = preg_replace('/([^\\\\])\./',"\\1/",$prefix);
+      $acc_string = preg_replace('/([^\\\\])\./',"\\1/",$acc_string); 
+    }
+    $prefix = preg_replace("/\\\\([\.\/])/","\\1",$prefix);
+    $acc_string = preg_replace("/\\\\([\.\/])/","\\1",$acc_string); 
+  
+    $domain = $mailpart = "";
+    $mail = $this->parent->mail;
+    if(preg_match("/\@/",$mail)){
+      list($mailpart,$domain) = explode("@",$mail);
     }
-    $str = preg_replace("/\\\\([\.\/])/","\\1",$str);
-    $str = trim(strtolower($str . $this->parent->$uattrib));
 
-    if($this->account_id != $str){
-      $this->account_id = $str;
-      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "accountID generated: <b>".$str."</b>","");
+    /* Create account_id 
+     */ 
+    $from = array("/%cn%/i","/%uid%/i","/%prefix%/i","/%uattrib%/i","/%domain%/i","/%mailpart%/i","/%mail%/i");
+    $to   = array($this->parent->cn,$this->parent->uid,$prefix,$this->parent->$uattrib, $domain, $mailpart,$mail);
+    $acc_id = trim(strtolower(preg_replace($from,$to,$acc_string)));
+
+    /* Check for not replaced pattern.
+     */
+    if(preg_match("/%/",$acc_id)){
+      $notr = preg_replace("/^[^%]*/","",$acc_id);
+      if(!empty($notr)){
+        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Warning</b>", 
+            sprintf("<b>MAIL: WARNING unknown pattern in account creation string '%s' near '%s'</b>", $acc_id, $notr));
+
+        /* Remove incomprehensible patterns */
+        $acc_id = preg_replace("/%[^%]+%/","",$acc_id);
+      }
+    }
+
+
+    if($this->account_id != $acc_id){
+      $this->account_id = $acc_id;
+      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"", "<b>MAIL:</b> AccountID generated: <b>".$acc_id."</b>");
     }
   }
 
@@ -294,30 +407,55 @@ class mailMethod{
     }
 
     /* Build account identicator */
-    if($type == ""){
-      $type = $this->type;
-    }
-    if($type == "user"){
-      $str = $this->user_prefix;
+    if($this->type == "user"){
+      $prefix = $this->user_prefix;
+      $acc_string = $this->user_id;
     }else{
-      $str = $this->share_prefix;
-    } 
+      $prefix = $this->share_prefix;
+      $acc_string = $this->share_id;
+    }
 
-    /* Create account prefix and respect "cyrusUseSlashes"
+    /* Create account prefix and respect "cyrusUseSlashes" 
        Do not replace escaped dots for cyrusUseSlashes.
      */
     $uattrib = $this->uattrib;
     if($this->cyrusUseSlashes){
-      $str = preg_replace('/([^\\\\])\./',"\\1/",$str);
+      $prefix     = preg_replace('/([^\\\\])\./',"\\1/",$prefix);
+      $acc_string = preg_replace('/([^\\\\])\./',"\\1/",$acc_string); 
+    }
+    $prefix = preg_replace("/\\\\([\.\/])/","\\1",$prefix);
+    $acc_string = preg_replace("/\\\\([\.\/])/","\\1",$acc_string); 
+  
+    $domain = $mailpart = "";
+    $mail = $this->parent->mail;
+    if(preg_match("/\@/",$mail)){
+      list($mailpart,$domain) = explode("@",$mail);
+    }
+
+    /* Create account_id
+     */
+    $from = array("/%cn%/i","/%uid%/i","/%prefix%/i","/%uattrib%/i","/%domain%/i","/%mailpart%/i","/%mail%/i");
+    $to   = array($this->parent->cn,$this->parent->uid,$prefix,$this->parent->$uattrib, $domain, $mailpart,$mail);
+    $acc_id = trim(strtolower(preg_replace($from,$to,$acc_string)));
+
+    /* Check for not replaced pattern.
+     */
+    if(preg_match("/%/",$acc_id)){
+      $notr = preg_replace("/^[^%]*/","",$acc_id);
+      if(!empty($notr)){
+        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"<b>Warning</b>", 
+            sprintf("<b>MAIL: WARNING unknown pattern in account creation string '%s' near '%s'</b>", $acc_id, $notr));
+
+        /* Remove incomprehensible patterns */
+        $acc_id = preg_replace("/%[^%]+%/","",$acc_id);
+      }
     }
-    $str = preg_replace("/\\\\([\.\/])/","\\1",$str);
-    $str = trim(strtolower($str . $this->parent->$uattrib));
 
-    if(preg_match("/\@/",$this->parent->$uattrib)){
-      list($mail,$domain) = split("\@",$this->parent->$uattrib);
-      $str = trim(strtolower($str . $mail . $folder . "@" . $domain));
+    if(preg_match("/\@/",$acc_id)){
+      list($mail,$domain) = explode("@",$acc_id);
+      $str = trim($mail . $folder . "@" . $domain);
     }else{
-      $str = trim(strtolower($str . $this->parent->$uattrib));
+      $str = trim($acc_id . $folder);
     }
     return($str) ;
   }
@@ -334,13 +472,13 @@ class mailMethod{
       $method= $this->config->get_cfg_value("mailmethod");
       $cls = get_correct_class_name("mailMethod$method");
       if(isset($methods[$cls])){
-        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "Selected mailMethod: <b>".$cls."</b>","");
+        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,"", "<b>MAIL:</b> Selected mailMethod: <b>".$cls."</b>");
         $tmp = new $cls($this->config,$this->parent,$this->type);
         $tmp->init();
         return($tmp);
       }else{
-        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "Invalid mailMethod defined <b>".$cls.
-            "</b> falling back to <b>".get_class($this)."</b>","");
+        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "","<b>MAIL: Invalid mailMethod defined: ".$cls.
+            " falling back to ".get_class($this)."</b>");
 
         /* Print out configuration errors directly, we can't catch them everywhere. 
          */
@@ -391,28 +529,8 @@ class mailMethod{
     $this->reset_error();
 
     @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>" ,
-        "<b>Remove account</b> from server :".$this->MailServer);
-
-    return(TRUE);
+        "<b>MAIL: Remove account</b> from server :".$this->MailServer);
 
-    /* No imap actions here, just updated shared folder membership 
-     */
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd($this->config->current['BASE']);
-    $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$account."))",array('dn','cn'));
-    if(class_exists("grouptabs")){
-      while($attrs = $ldap->fetch()){
-        $tmp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
-        if(isset($tmp->by_object['mailgroup'])){
-          $tmp->by_object['mailgroup']->members= $tmp->by_object['group']->memberUid;
-          if(!$this->is_account){
-            $tmp->by_object['mailgroup']->removeUserAcl($account);
-            $tmp->by_object['mailgroup']->removeUserAcl($this->mail);
-          }
-          $tmp->by_object['mailgroup']->save();
-        }
-      }
-    }
     return(TRUE);
   }
 
@@ -591,13 +709,8 @@ class mailMethod{
   public function getAvailableFolderTypes()
   {
     $ret = array();
-    $ret['CAT']['mail'] = _("Mail"); 
-    $ret['CAT']['peter']= _("User"); 
-    $ret['SUB_CAT']['mail']['a'] = "a";
-    $ret['SUB_CAT']['mail']['b'] = "b";
-    $ret['SUB_CAT']['peter']['tt'] = "tt";
-    $ret['SUB_CAT']['peter']['dd'] = "dd";
-    $ret['SUB_CAT']['peter']['did'] = "did";
+    $ret['CAT'][''] = _("None"); 
+    $ret['SUB_CAT'][''][''] = _("None");
     return($ret);
   }
 
@@ -608,12 +721,7 @@ class mailMethod{
    */
   public function getFolderType($default)
   {
-    if($this->enableFolderTypes && $this->parent->attrs['kolabFolderType'][0]){
-      list($cat,$sub) = split("\.",$this->parent->attrs['kolabFolderType'][0]);
-      return(array("CAT" => $cat, "SUB_CAT" => $sub));
-    }else{
-      return($default);
-    }
+    return($default);
   }
 
  
@@ -623,10 +731,7 @@ class mailMethod{
    */
   public function setFolderType($type)
   {
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd ($this->parent->dn);
-    $ldap->modify(array("kolabFolderType" => $type['CAT'].".".$type['SUB_CAT']));
-    echo "Move me to KOLAB";
+    return(TRUE) ;   
   }
 
 
@@ -634,6 +739,15 @@ class mailMethod{
    */
   public function  getFolderACLs($folder_acls)
   {
+    /* Merge given ACL with acl mapping 
+       This ensures that no ACL will accidentally overwritten by gosa.
+     */
+    foreach($folder_acls as $user => $acl){
+      if(!isset($this->acl_mapping[$acl])){
+        $this->acl_mapping[$acl] = $acl;
+      }
+    }
+
     return($folder_acls);
   }
 
@@ -651,14 +765,7 @@ class mailMethod{
   */
   public function getAclTypes()
   {
-    $acls["lrsw"]     = _("read");
-    $acls["lrswp"]    = _("post");
-    $acls["p"]        = _("external post");
-    $acls["lrswip"]   = _("append");
-    $acls["lrswipcd"] = _("write");
-    $acls["lrswipcda"]= _("admin");
-    $acls[" "]= _("none");
-    return($acls);
+    return( $this->acl_mapping);
   }
 
   public function folderTypesEnabled()
@@ -670,6 +777,48 @@ class mailMethod{
   {
     return(TRUE);
   }
+
+
+  /*! \brief  Returns the configured mailMethod
+      @return String  the configured mail method or ""
+   */
+  static public function get_current_method($config)
+  {
+    global $class_mapping;
+    $method= $config->get_cfg_value("mailmethod");
+    $cls = get_correct_class_name("mailMethod$method");
+    foreach($class_mapping as $class => $path){
+      if($class == $cls){
+        return($class);
+      }
+    }
+    return("");
+  }
+
+
+  static function quota_to_image($use,$quota)
+  {
+    if($use == -1){
+      return("&nbsp;&nbsp;"._("Unknown"));
+    }elseif(empty($quota)){
+      return("&nbsp;&nbsp;"._("Unlimited"));
+    }else{
+      $usage =round(($use/$quota) * 100);
+      return("<img src='progress.php?x=100&amp;y=17&amp;p=$usage'>");
+    }
+  }
+
+  /*! \brief  Returns the default sharedFolder ACLs for this method.
+      @return Array Returns an array containg acls for __member__ and __anyone__
+   */ 
+  public function getDefaultACLs()
+  {
+    $tmp = $this->default_acls;
+    if(!isset($tmp['__member__'])) $tmp['__member__'] = " ";
+    if(!isset($tmp['__anyone__'])) $tmp['__anyone__'] = " ";
+    return($tmp);
+  }
 }