Code

Fixed undefined index in sieve authentification method detection
[gosa.git] / gosa-plugins / mail / personal / mail / class_mailAccount.inc
index ccad100f2d001db248428ab9266ff98710a8c131..34c66cb5211b552c2ee3d13d4138e2429ccc9455 100644 (file)
@@ -68,6 +68,8 @@ class mailAccount extends plugin
   var $gosaSpamMailbox                    = "";
 
   /* The methods defaults */
+  var $quotaUsage     = -1; // Means unknown
+
   var $mailMethod      = NULL;
   var $MailDomain      = "";
   var $sieveManagementUsed = FALSE;
@@ -89,12 +91,26 @@ class mailAccount extends plugin
 
   var $multiple_support = TRUE;
 
+  var $uid = "";
+  var $cn  = "";
+
+
   /*! \brief  Initialize the mailAccount 
    */
   function __construct (&$config, $dn= NULL)
   {
     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);
@@ -107,9 +123,9 @@ class mailAccount extends plugin
      */
     $this->initially_was_account = $this->is_account;
 
-    /* Initialize vacation settings 
+    /* Initialize vacation settings, if enabled.
      */   
-    if(empty($this->gosaVacationStart)){
+    if(empty($this->gosaVacationStart) && $this->mailMethod->vacationRangeEnabled()){
       $this->gosaVacationStart = time();
       $this->gosaVacationStop = time();
     }
@@ -126,23 +142,24 @@ 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 '%s' doesn't exists on mail server: %s."), 
+        msg_dialog::display(_("Mail error"), sprintf(_("Mailbox '%s' doesn't exists on mail server: %s"), 
               $this->mailMethod->get_account_id(),$this->gosaMailServer), ERROR_DIALOG);
       }
 
@@ -194,6 +211,7 @@ class mailAccount extends plugin
 
   function execute()
   {
+
     /* Call parent execute */
     $display = plugin::execute();
 
@@ -364,7 +382,8 @@ class mailAccount extends plugin
      ****************/
     $smarty = get_smarty();
     $smarty->assign("initially_was_account", $this->initially_was_account);
-    $smarty->assign("isModifyableMail", $this->mailMethod->isModifyableMail());
+    $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');
@@ -376,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);
@@ -389,8 +406,6 @@ 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;
@@ -475,7 +490,6 @@ class mailAccount extends plugin
       $smarty->assign("months", $months);
       $smarty->assign("years", $years);
       $smarty->assign("days", $days);
-
     }
 
     /* fill filter settings 
@@ -495,9 +509,14 @@ class mailAccount extends plugin
     if (isset($_POST['mailTab'])){
 
       /* Save ldap attributes */
-      $mail = $this->mail;
+      $mail   = $this->mail;
+      $server = $this->gosaMailServer;
       plugin::save_object();
 
+      if(!$this->mailMethod->isModifyableServer() && $this->initially_was_account){
+        $this->gosaMailServer = $server;
+      }
+
       if(!$this->mailMethod->isModifyableMail() && $this->initially_was_account){
         $this->mail = $mail;
       }else{
@@ -577,6 +596,9 @@ class mailAccount extends plugin
           $this->is_modified= TRUE;
         }
         $this->gosaMailDeliveryMode= $tmp;
+
+        /* Get start/stop values for vacation scope of application
+         */
         if($this->mailMethod->vacationRangeEnabled()){
           if($this->acl_is_writeable("gosaVacationMessage") && preg_match("/V/",$this->gosaMailDeliveryMode)){
             if(isset($_POST['gosaVacationStart'])){
@@ -719,6 +741,24 @@ class mailAccount extends plugin
       $attrs  = $obj->attributes;
     }
     if($obj){
+
+      /* Replace vacation start and end time */
+      if($this->mailMethod->vacationRangeEnabled()){
+        if(preg_match("/%start/",$contents)){
+          $contents = preg_replace("/%start/",date("d.m.Y",$this->gosaVacationStart),$contents);
+        }
+        if(preg_match("/%end/",$contents)){
+          $contents = preg_replace("/%end/",date("d.m.Y",$this->gosaVacationStop),$contents);
+        }
+      }else{
+        if(preg_match("/%start/",$contents)){
+          $contents = preg_replace("/%start/", _("unknown"),$contents);
+        }
+        if(preg_match("/%end/",$contents)){
+          $contents = preg_replace("/%end/", _("unknown"), $contents);
+        }
+      }
+
       foreach ($attrs as $val){
         if(preg_match("/dateOfBirth/",$val)){
           if($obj->use_dob){
@@ -729,13 +769,6 @@ class mailAccount extends plugin
               $obj->$val, $contents);
         }
 
-        /* Replace vacation start and end time */
-        if(preg_match("/%start/",$contents)){
-          $contents = preg_replace("/%start/",date("d.m.Y",$this->gosaVacationStart),$contents);
-        }
-        if(preg_match("/%end/",$contents)){
-          $contents = preg_replace("/%end/",date("d.m.Y",$this->gosaVacationStop),$contents);
-        }
       }
     }
     $contents = ltrim(preg_replace("/^DESC:.*$/m","",$contents),"\n ");
@@ -841,6 +874,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();
 
@@ -869,15 +921,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);
         }
       }
@@ -929,8 +977,6 @@ class mailAccount extends plugin
     $ldap->cd($this->dn);
     $this->cleanup();
     $ldap->modify ($this->attrs);
-  
-
 
     if (!$ldap->success()){
       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
@@ -947,21 +993,20 @@ 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"))){
 
-
           /* Do not write sieve settings if this account is new and 
              doesn't seem to exist.
            */
@@ -974,20 +1019,32 @@ class mailAccount extends plugin
             }
           }
         }else{
-      
-         echo "Check sieve management here";
-
           @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 
               "User uses an own sieve script, skipping sieve update.".$str."</b>","");
+          $this->sieve_management->save();
         }
+      }
+    }
+    $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){
@@ -1187,9 +1244,7 @@ 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'];
@@ -1231,7 +1286,7 @@ class mailAccount extends plugin
       $message[]= msgPool::required(_("Spam folder"));
     }
 
-    if (  in_array("use_vacation",$this->multi_boxes) &&
+    if ($this->mailMethod->vacationRangeEnabled() && in_array("use_vacation",$this->multi_boxes) &&
         preg_match('/V/', $this->gosaMailDeliveryMode) && $this->gosaVacationStart > $this->gosaVacationStop){
       $message[]= msgPool::invalid(_("Vacation interval"));
     }