Code

Updated Quota progress bar.
[gosa.git] / gosa-plugins / mail / personal / mail / class_mailAccount.inc
index 7931e167cc0c33684dae0ffb87d964d588238c22..295ab5747b801bd6eb374b0fabd985551664e8f1 100644 (file)
@@ -68,10 +68,11 @@ class mailAccount extends plugin
   var $gosaSpamMailbox                    = "";
 
   /* The methods defaults */
+  var $quotaUsage     = -1; // Means unknown
+
   var $mailMethod      = NULL;
   var $MailDomain      = "";
   var $sieveManagementUsed = FALSE;
-  var $multiple_support = FALSE;
   var $vacationTemplates = array();
   var $sieve_management = NULL;
   var $forward_dialog = FALSE;
@@ -88,6 +89,11 @@ class mailAccount extends plugin
       "gosaMailForwardingAddress");
   var $objectclasses= array("gosaMailAccount");
 
+  var $multiple_support = TRUE;
+
+  var $uid = "";
+  var $cn  = "";
+
 
   /*! \brief  Initialize the mailAccount 
    */
@@ -95,6 +101,16 @@ class mailAccount extends plugin
   {
     plugin::plugin($config,$dn); 
 
+    /* Get attributes from parent object 
+     */
+    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;
+      }elseif(isset($this->attrs[$attr])){
+        $this->$attr = $this->attrs[$attr][0];
+      }
+    }
+
     /* Intialize the used mailMethod
      */
     $tmp = new mailMethod($config,$this);
@@ -126,33 +142,36 @@ class mailAccount extends plugin
 
         /* Read quota */
         $this->gosaMailQuota = $this->mailMethod->getQuota($this->gosaMailQuota);
+        $this->quotaUsage    = $this->mailMethod->getQuotaUsage($this->quotaUsage);
         if($this->mailMethod->is_error()){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot read quota settings! Error was: %s."), 
+          msg_dialog::display(_("Mail error"), sprintf(_("Cannot read quota settings: %s"), 
                 $this->mailMethod->get_error()), ERROR_DIALOG);
         }
         
         /* Read mailboxes */
         $this->MailBoxes = $this->mailMethod->getMailboxList($this->MailBoxes);
         if($this->mailMethod->is_error()){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot get list of mailboxes! Error was: %s."), 
+          msg_dialog::display(_("Mail error"), sprintf(_("Cannot get list of mailboxes: %s"), 
                 $this->mailMethod->get_error()), ERROR_DIALOG);
         }
           
       }elseif(!$this->mailMethod->is_connected()){
-        msg_dialog::display(_("Mail error"), sprintf(_("Cannot connect mail method! Error was: %s."), 
+        msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
               $this->mailMethod->get_error()), ERROR_DIALOG);
       }elseif(!$this->mailMethod->account_exists()){
-        msg_dialog::display(_("Mail error"), sprintf(_("Mailbox doesn't exists on mail server: %s."), 
-              $this->gosaMailServer), ERROR_DIALOG);
+        msg_dialog::display(_("Mail error"), sprintf(_("Mailbox '%s' doesn't exists on mail server: %s"), 
+              $this->mailMethod->get_account_id(),$this->gosaMailServer), ERROR_DIALOG);
       }
 
       /* If the doamin part is selectable, we have to split the mail address
        */
-      if($this->mailMethod->domainSelectionEnabled()){
-        $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
-        $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
-        if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
-          $this->mailDomainParts[] = $this->mailDomainPart;
+      if(!(!$this->mailMethod->isModifyableMail() && $this->is_account)){
+        if($this->mailMethod->domainSelectionEnabled()){
+          $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
+          $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
+          if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
+            $this->mailDomainParts[] = $this->mailDomainPart;
+          }
         }
       }
 
@@ -192,6 +211,7 @@ class mailAccount extends plugin
 
   function execute()
   {
+
     /* Call parent execute */
     $display = plugin::execute();
 
@@ -361,6 +381,11 @@ class mailAccount extends plugin
       SMARTY- Assign smarty variables 
      ****************/
     $smarty = get_smarty();
+    $smarty->assign("initially_was_account", $this->initially_was_account);
+    $smarty->assign("isModifyableMail"  , $this->mailMethod->isModifyableMail());
+    $smarty->assign("isModifyableServer", $this->mailMethod->isModifyableServer());
+    $smarty->assign("mailEqualsCN", $this->mailMethod->mailEqualsCN());
+
     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
     $tmp  = $this->plInfo();
     foreach($tmp['plProvidedAcls'] as $name => $transl){
@@ -370,12 +395,10 @@ class mailAccount extends plugin
       $smarty->assign($attr,$this->$attr);
     }
     $smarty->assign("quotaEnabled", $this->mailMethod->quotaEnabled());
-    if($this->mailMethod->is_connected()){
-      $smarty->assign("quotaUsage",   $this->mailMethod->getQuotaUsage());
-    }else{
-      $smarty->assign("quotaUsage",   _("Unknown"));
+    if($this->mailMethod->quotaEnabled()){
+      $smarty->assign("quotaUsage",   mailMethod::quota_to_image($this->quotaUsage,$this->gosaMailQuota));
+      $smarty->assign("gosaMailQuota",$this->gosaMailQuota);
     }
-    $smarty->assign("gosaMailQuota",$this->gosaMailQuota);
     $smarty->assign("domainSelectionEnabled", $this->mailMethod->domainSelectionEnabled());
     $smarty->assign("MailDomains", $this->mailDomainParts);
     $smarty->assign("MailDomain" , $this->mailDomainPart);
@@ -383,6 +406,8 @@ class mailAccount extends plugin
     $smarty->assign("allowSieveManagement", $this->mailMethod->allowSieveManagement());
     $smarty->assign("own_script",  $this->sieveManagementUsed);
 
+    print_a($this->multi_boxes);
+
     /* _Multiple users vars_ */
     foreach($this->attributes as $attr){
       $u_attr = "use_".$attr;
@@ -487,13 +512,24 @@ class mailAccount extends plugin
     if (isset($_POST['mailTab'])){
 
       /* Save ldap attributes */
+      $mail   = $this->mail;
+      $server = $this->gosaMailServer;
       plugin::save_object();
 
-      /* Get posted mail domain part, if necessary  
-       */
-      if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
-        if(in_array(get_post('MailDomain'), $this->mailDomainParts)){
-          $this->mailDomainPart = get_post('MailDomain');
+      if(!$this->mailMethod->isModifyableServer() && $this->initially_was_account){
+        $this->gosaMailServer = $server;
+      }
+
+      if(!$this->mailMethod->isModifyableMail() && $this->initially_was_account){
+        $this->mail = $mail;
+      }else{
+
+        /* Get posted mail domain part, if necessary  
+         */
+        if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
+          if(in_array(get_post('MailDomain'), $this->mailDomainParts)){
+            $this->mailDomainPart = get_post('MailDomain');
+          }
         }
       }
 
@@ -501,7 +537,7 @@ class mailAccount extends plugin
        */
       if (isset($_POST["import_vacation"]) && isset($this->vacationTemplates[$_POST["vacation_template"]])){
         if($this->multiple_support_active){
-          $contents = file_get_contents($_POST["vacation_template"]);
+          $contents = ltrim(preg_replace("/^DESC:.*$/m","",file_get_contents($_POST["vacation_template"])));
         }else{
           $contents = $this->prepare_vacation_template(file_get_contents($_POST["vacation_template"]));
         }
@@ -827,6 +863,25 @@ class mailAccount extends plugin
       $this->mail = $this->mail."@".$this->mailDomainPart;
     }
 
+    /* Update sharedFolder dependencies. 
+       Open each shared folder and remove this account. 
+       Then Save the group to ensure that all necessary 
+        actions will be taken (imap acls updated aso.).
+     */
+    $ldap = $this->config->get_ldap_link();    
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$this->uid."))",array("dn"));
+    while($attrs = $ldap->fetch()){
+      $grp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
+      if(isset($grp->by_object['mailgroup']) && isset($grp->by_object['group'])){
+        $grp->by_object['group']->removeUser($this->uid);
+
+        /* Do not save the complete group! This will quit the complete membership 
+         */
+        $grp->by_object['mailgroup']->save();
+      } 
+    }
+
     /* Remove GOsa attributes */
     plugin::remove_from_parent();
 
@@ -855,15 +910,11 @@ class mailAccount extends plugin
     if (!$this->is_template){
 
       if(!$this->mailMethod->connect()){
-        msg_dialog::display(_("Mail error"), sprintf(_("Cannot connect mail method! Error was: %s."), 
+        msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
               $this->mailMethod->get_error()), ERROR_DIALOG);
       }else{
         if(!$this->mailMethod->deleteMailbox()){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot remove mailbox! Error was: %s."), 
-                $this->mailMethod->get_error()), ERROR_DIALOG);
-        }
-        if(!$this->mailMethod->updateSharedFolder()){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions! Error was: %s."), 
+          msg_dialog::display(_("Mail error"), sprintf(_("Cannot remove mailbox: %s"), 
                 $this->mailMethod->get_error()), ERROR_DIALOG);
         }
       }
@@ -882,13 +933,17 @@ class mailAccount extends plugin
     $ldap= $this->config->get_ldap_link();
 
     /* If domain part was selectable, contruct mail address */
-    if($this->mailMethod->domainSelectionEnabled()){
-      $this->mail = $this->mail."@".$this->mailDomainPart;
+    if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
+
+      if($this->mailMethod->domainSelectionEnabled()){
+        $this->mail = $this->mail."@".$this->mailDomainPart;
+      }
+
+      /* Enforce lowercase mail address and trim whitespaces
+       */
+      $this->mail = trim(strtolower($this->mail));
     }
-    
-    /* Enforce lowercase mail address and trim whitespaces
-     */
-    $this->mail = trim(strtolower($this->mail));
+
 
     /* Call parents save to prepare $this->attrs */
     plugin::save();
@@ -927,37 +982,58 @@ class mailAccount extends plugin
     if (!$this->is_template){
       $this->mailMethod->connect();
       if(!$this->mailMethod->is_connected()){
-        msg_dialog::display(_("Mail error"), sprintf(_("Cannot connect mail method! Error was: %s."), 
+        msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
               $this->mailMethod->get_error()), ERROR_DIALOG);
       }else{
         if(!$this->mailMethod->updateMailbox()){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox! Error was: %s."), 
+          msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox: %s"), 
                 $this->mailMethod->get_error()), ERROR_DIALOG);
         }
         if(!$this->mailMethod->setQuota($this->gosaMailQuota)){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings! Error was: %s."), 
+          msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings: %s"), 
                 $this->mailMethod->get_error()), ERROR_DIALOG);
         }
 
         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
-          if(!$this->mailMethod->saveSieveSettings()){
-            msg_dialog::display(_("Mail error"), $this->mailMethod->get_error(), ERROR_DIALOG);
+
+          /* Do not write sieve settings if this account is new and 
+             doesn't seem to exist.
+           */
+          if(!$this->initially_was_account && !$this->mailMethod->account_exists()){
+            @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
+                "Skipping sieve settings, the account doesn't seem to be created already.</b>","");
+          }else{
+            if(!$this->mailMethod->saveSieveSettings()){
+              msg_dialog::display(_("Mail error"), $this->mailMethod->get_error(), ERROR_DIALOG);
+            }
           }
         }else{
-      
          echo "Check sieve management here";
-
           @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 
               "User uses an own sieve script, skipping sieve update.".$str."</b>","");
         }
+      }
+    }
+    $this->mailMethod->disconnect();
 
-        if(!$this->mailMethod->updateSharedFolder()){
-          msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions! Error was: %s."), 
-                $this->mailMethod->get_error()), ERROR_DIALOG);
+    /* Update sharedFolder dependencies.
+       Open each shared folder and remove this account.
+       Then Save the group to ensure that all necessary
+       actions will be taken (imap acls updated aso.).
+     */
+    if(!$this->initially_was_account){
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cd($this->config->current['BASE']);
+      $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$this->uid."))",array("dn"));
+      while($attrs = $ldap->fetch()){
+        $grp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
+        if(isset($grp->by_object['mailgroup'])){
+          /* Do not save the complete group! This will quit the complete membership
+           */
+          $grp->by_object['mailgroup']->save();
         }
       }
     }
-    $this->mailMethod->disconnect();
 
     /* Optionally execute a command after we're done */
     if ($this->initially_was_account == $this->is_account){
@@ -989,31 +1065,34 @@ class mailAccount extends plugin
 
     /* Mail address checks */
     $mail = $this->mail;
-    if($this->mailMethod->domainSelectionEnabled()){
-      $mail.= "@".$this->mailDomainPart;
-    }
+    if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
 
-    if (empty($mail)){
-      $message[]= msgPool::required(_("Primary address"));
-    }
-    if ($this->is_template){
-      if (!tests::is_email($mail, TRUE)){
-        $message[]= msgPool::invalid(_("Mail address"),"","","%givenName.%sn@your-domain.com");
+      if($this->mailMethod->domainSelectionEnabled()){
+        $mail.= "@".$this->mailDomainPart;
       }
-    } else {
-      if (!tests::is_email($mail)){
-        $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
+
+      if (empty($mail)){
+        $message[]= msgPool::required(_("Primary address"));
+      }
+      if ($this->is_template){
+        if (!tests::is_email($mail, TRUE)){
+          $message[]= msgPool::invalid(_("Mail address"),"","","%givenName.%sn@your-domain.com");
+        }
+      } else {
+        if (!tests::is_email($mail)){
+          $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
+        }
       }
-    }
 
-    /* Check if this mail address is already in use */
-    $ldap->cd($this->config->current['BASE']);
-    $filter = "(&(!(objectClass=gosaUserTemplate))(!(uid=".$this->uid."))".
-      "(objectClass=gosaMailAccount)".
-      "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
-    $ldap->search($filter,array("uid"));
-    if ($ldap->count() != 0){
-      $message[]= msgPool::duplicated(_("Mail address"));
+      /* Check if this mail address is already in use */
+      $ldap->cd($this->config->current['BASE']);
+      $filter = "(&(!(objectClass=gosaUserTemplate))(!(uid=".$this->uid."))".
+        "(objectClass=gosaMailAccount)".
+        "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
+      $ldap->search($filter,array("uid"));
+      if ($ldap->count() != 0){
+        $message[]= msgPool::duplicated(_("Mail address"));
+      }
     }
 
 
@@ -1153,6 +1232,15 @@ class mailAccount extends plugin
     }
     $ret['flag_add'] = $flag_add;
     $ret['flag_remove'] = $flag_remove;
+
+    echo "1";
+    if($this->mailMethod->vacationRangeEnabled()){
+      echo "2";
+      if(in_array("V",$flag_add)){
+        $ret['gosaVacationStart'] =  $this->gosaVacationStart = $_POST['gosaVacationStart'];
+        $ret['gosaVacationStop'] =  $this->gosaVacationStop = $_POST['gosaVacationStop'];
+      }
+    }
     return($ret);
   }
 
@@ -1237,8 +1325,8 @@ class mailAccount extends plugin
     }
   }
 
-  
-  /*! \brief  
+  /*! \brief
    */
   function get_multi_init_values()
   {
@@ -1247,7 +1335,7 @@ class mailAccount extends plugin
     return($attrs);
   }
 
-  
+
   /*! \brief  Display multiple edit dialog 
    */
   function multiple_execute()
@@ -1294,6 +1382,17 @@ class mailAccount extends plugin
   }
 
 
+  function allow_remove()
+  {
+    $resason = "";
+    if(!$this->mailMethod->allow_remove($reason)){
+      return($reason);
+    }
+    return("");
+  }
+
+
+
   /*! \brief  ACL settings 
    */
   static function plInfo()