Code

Added some checks
[gosa.git] / include / sieve / class_sieveManagement.inc
index 439077628b539983df34fe0f6436467565c884ba..4f47fbc721ab91a7816c3671ea397212e94281d3 100644 (file)
@@ -64,7 +64,7 @@ class sieveManagement extends plugin
     plugin::plugin($config,$dn);
 
     /* Get sieve */
-    if(!$sieve = $this->get_sieve()){
+    if(!$this->sieve_handle = $this->get_sieve()){
       print_red(
         sprintf(
           _("Can't log into SIEVE server. Server says '%s'."),
@@ -74,9 +74,9 @@ class sieveManagement extends plugin
 
 
     /* Get all sieve scripts names */
-    if($sieve->sieve_listscripts()){
-      if (is_array($sieve->response)){
-        foreach($sieve->response as $key => $name){
+    if($this->sieve_handle->sieve_listscripts()){
+      if (is_array($this->sieve_handle->response)){
+        foreach($this->sieve_handle->response as $key => $name){
 
           $data = array();
           $data['NAME'] = $name;
@@ -94,10 +94,10 @@ class sieveManagement extends plugin
     /* Get script contents */
     foreach($this->scripts as $key => $script){
       $p = new My_Parser;
-      $sieve->sieve_getscript($script['NAME']);
+      $this->sieve_handle->sieve_getscript($script['NAME']);
 
       $script = "";
-      foreach($sieve->response as $line){
+      foreach($this->sieve_handle->response as $line){
         $script.=$line;
       }
 
@@ -108,7 +108,7 @@ class sieveManagement extends plugin
       $ret = $p->parse($script);
       if(!$ret){
         $this->scripts[$key]['STATUS']   = FALSE;
-        $this->scripts[$key]['MODE']    = "Source-Only";
+        $this->scripts[$key]['MODE']    = "Source";
         $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
       }else{
         $this->scripts[$key]['STATUS']   = TRUE;
@@ -118,7 +118,7 @@ class sieveManagement extends plugin
       $this->scripts[$key]['PARSER'] = $p;
       $this->scripts[$key]['EDITED'] = FALSE;
     }
-    $this->sieve_handle = $sieve;
+    $this->sieve_handle = $this->sieve_handle;
   }
 
 
@@ -132,7 +132,7 @@ class sieveManagement extends plugin
     $this->Sieve_Error = "";
   
     /* Log into the mail server */
-    $sieve= new sieve(
+    $this->sieve_handle= new sieve(
         $cfg["sieve_server"], 
         $cfg["sieve_port"], 
         $this->parent->uid, 
@@ -140,11 +140,11 @@ class sieveManagement extends plugin
         $cfg["admin"]);
 
     /* Try to login */
-    if (!$sieve->sieve_login()){
-      $this->Sieve_Error = $sieve->error_raw;
+    if (!$this->sieve_handle->sieve_login()){
+      $this->Sieve_Error = $this->sieve_handle->error_raw;
       return(FALSE);
     }
-    return($sieve);
+    return($this->sieve_handle);
   }
 
 
@@ -196,6 +196,11 @@ class sieveManagement extends plugin
         if(preg_match("/[^a-z]/i",$name)){
           $err = _("Only a-z are allowed in script names.");
         }
+
+        $tmp = $this->get_used_script_names();
+        if(in_array_ics($name,$tmp)){
+          $err =_("The specified name is already in use.");
+        }
       }
 
       /* Create script if everything is ok */
@@ -274,25 +279,29 @@ class sieveManagement extends plugin
         $script = preg_replace("/_(x|y)/","",$script);
         $once = FALSE;
 
-        /* Get sieve */
-        if(!$sieve = $this->get_sieve()){
-          print_red(
-              sprintf(
-                _("Can't log into SIEVE server. Server says '%s'."),
-                to_string($this->Sieve_Error)));
-        }
+        /* We can only activate existing scripts */
+        if(!$this->scripts[$script]['IS_NEW']){
 
-        /* Try to activate the given script and update 
-         *  class script array. 
-         */
-        if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
-          print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
-        }else{
-          foreach($this->scripts as $key => $data){
-            if($key == $script){
-              $this->scripts[$key]['ACTIVE'] = TRUE;
-            }else{
-              $this->scripts[$key]['ACTIVE'] = FALSE;
+          /* Get sieve */
+          if(!$this->sieve_handle = $this->get_sieve()){
+            print_red(
+                sprintf(
+                  _("Can't log into SIEVE server. Server says '%s'."),
+                  to_string($this->Sieve_Error)));
+          }
+
+          /* Try to activate the given script and update 
+           *  class script array. 
+           */
+          if(!$this->sieve_handle->sieve_setactivescript($this->scripts[$script]['NAME'])){
+            print_red(sprintf(_("Can't activate sieve script on server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
+          }else{
+            foreach($this->scripts as $key => $data){
+              if($key == $script){
+                $this->scripts[$key]['ACTIVE'] = TRUE;
+              }else{
+                $this->scripts[$key]['ACTIVE'] = FALSE;
+              }
             }
           }
         }
@@ -320,14 +329,14 @@ class sieveManagement extends plugin
       }else{
 
         /* Get sieve */
-        if(!$sieve = $this->get_sieve()){
+        if(!$this->sieve_handle = $this->get_sieve()){
           print_red(
               sprintf(
                 _("Can't log into SIEVE server. Server says '%s'."),
                 to_string($this->Sieve_Error)));
         }
 
-        if(!$sieve->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
+        if(!$this->sieve_handle->sieve_deletescript($this->scripts[$this->script_to_delete]['NAME'])){
           print_red(sprintf(_("Can't remove sieve script from server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
         }else{
           unset($this->scripts[$this->script_to_delete]);
@@ -480,7 +489,8 @@ class sieveManagement extends plugin
 
 
         /* Element selected */
-        if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])){
+        if(isset($_POST['element_type']) && isset($element_types[$_POST['element_type']]) 
+           || isset($_POST['element_type']) &&in_array($_POST['element_type'],array("sieve_else","sieve_elsif"))){
           $this->add_element_type = $_POST['element_type'];
         }
 
@@ -490,7 +500,6 @@ class sieveManagement extends plugin
         if(isset($_POST['select_new_element_type'])){
 
           $this->add_new_id;
-
           $data = $this->current_handler->tree_->pap;
 
           /* Get index of the element identified by object_id == $this->add_new_id; */
@@ -511,18 +520,29 @@ class sieveManagement extends plugin
               $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
               $next_free ++;
             }else{
-              $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
               $direction = "down";
+              $next_free = $this->current_handler->tree_->_get_next_free_move_slot($index,$direction);
             }
             $this->add_new_id = $this->current_handler->tree_->pap[$next_free]->object_id;
           }
 
           /* Create elements we should add */
-          $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()));
           if($this->add_element_type == "sieve_if"){
+            $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()));
             $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()));
             $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()));
+          }elseif($this->add_element_type == "sieve_else"){
+            $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()));
+            $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()));
+            $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()));
+          }elseif($this->add_element_type == "sieve_elsif"){
+            $ele[] = new sieve_block_end(NULL,preg_replace("/[^0-9]/","",microtime()));
+            $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()));
+            $ele[] = new sieve_block_start(NULL,preg_replace("/[^0-9]/","",microtime()));
+          }else{
+            $ele[] = new $this->add_element_type(NULL, preg_replace("/[^0-9]/","",microtime()));
           }
+
           $start = $end = array();
           $found = false;
 
@@ -552,6 +572,7 @@ class sieveManagement extends plugin
             }
           }
 
+
           /* Only add, if current element could be located */
           if($found){
             $new = array();
@@ -567,6 +588,7 @@ class sieveManagement extends plugin
             $data= $new;
             $this->current_handler->tree_->pap = $data;
             $this->add_new_element = FALSE;
+          }else{
             print_red(_("Something went wrong while adding a new entry."));
           }
         }
@@ -575,6 +597,19 @@ class sieveManagement extends plugin
       /* Only display select dialog if it is necessary */
       if($this->add_new_element){
         $smarty = get_smarty();
+    
+        $add_else_elsif = FALSE;
+        foreach($this->current_handler->tree_->pap as $obj){
+          if($obj->object_id == $this->add_new_id && in_array(get_class($obj),array("sieve_if","sieve_elsif"))){
+            $add_else_elsif = TRUE;
+          }
+        }
+
+        if($add_else_elsif && $this->add_above_below == "below"){
+          $element_types['sieve_else'] = _("Else");
+          $element_types['sieve_elsif'] = _("Else if");
+        }
+  
         $smarty->assign("element_types",$element_types );
         $smarty->assign("element_type",$this->add_element_type);
         $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
@@ -655,7 +690,7 @@ class sieveManagement extends plugin
                         "attach" => "style='width:30px;'");
       }
 
-      if($active){
+      if($active || $script['IS_NEW']){
         $field6 = array("string" => "<img src='images/empty.png' alt=' '>".
                                     "<input type='image' name='editscript_".$key."' src='images/edit.png'>".
                                     "<input type='image' name='delscript_".$key."' src='images/edittrash.png'>");
@@ -717,74 +752,86 @@ class sieveManagement extends plugin
           }
         }
       }
-      
+
       /* Skip Mode changes and Parse tests 
        *  if we are currently in a subdialog 
        */
-      if(1==1 || !$this->add_new_element && !isset($_POST['Save_Copy']) && !$this->Import_Script &&!isset($_POST['add_type'])) {
-
-
 
-        $Mode = $this->scripts[$this->current_script]['MODE'];
-        $skip_mode_change = false;
-        if(in_array($Mode,array("Source-Only","Source"))){
-          if(isset($_POST['script_contents'])){
-            $sc = stripslashes($_POST['script_contents']);
-            $this->scripts[$this->current_script]['SCRIPT'] = $sc;
-            $p = new My_Parser;
-            if($p -> parse($sc)){
-#              $this->current_handler = $p;
-              $this->Script_Error = "";
-            } else {
-              $this->Script_Error = $p->status_text;
-              $skip_mode_change = TRUE;
-            }
-          }
-        }
-        if(in_array($Mode,array("Structured"))){
-          $this->current_handler->save_object();
-          $sc = $this->current_handler->get_sieve_script();
+      $this->current_handler->save_object();
+      $Mode = $this->scripts[$this->current_script]['MODE'];
+      $skip_mode_change = false;
+      if(in_array($Mode,array("Source-Only","Source"))){
+        if(isset($_POST['script_contents'])){
+          $sc = stripslashes($_POST['script_contents']);
           $this->scripts[$this->current_script]['SCRIPT'] = $sc;
           $p = new My_Parser;
           if($p -> parse($sc)){
- #           $this->current_handler = $p;
             $this->Script_Error = "";
           } else {
             $this->Script_Error = $p->status_text;
             $skip_mode_change = TRUE;
           }
         }
-        if(!$skip_mode_change){
-          if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
-            if(isset($_POST['View_Source'])){
-              $this->scripts[$this->current_script]['MODE'] = "Source";
-            }
-            if(isset($_POST['View_Structured'])){
-              $this->scripts[$this->current_script]['MODE'] = "Structured";
-            }
+      }
+      if(in_array($Mode,array("Structured"))){
+        $sc = $this->current_handler->get_sieve_script();
+        $this->scripts[$this->current_script]['SCRIPT'] = $sc;
+        $p = new My_Parser;
+        if($p -> parse($sc)){
+          $this->Script_Error = "";
+        } else {
+          $this->Script_Error = $p->status_text;
+          $skip_mode_change = TRUE;
+        }
+      }
+      if(!$skip_mode_change){
+        if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
+          $old_mode = $this->scripts[$this->current_script]['MODE'];
+          if(isset($_POST['View_Source'])){
+            $this->scripts[$this->current_script]['MODE'] = "Source";
+          }
+          if(isset($_POST['View_Structured'])){
+            $this->scripts[$this->current_script]['MODE'] = "Structured";
           }
+          $new_mode = $this->scripts[$this->current_script]['MODE'];
+
+          if($old_mode != $new_mode){
+
+            $sc = $this->scripts[$this->current_script]['SCRIPT'];
+            $p = new My_Parser;
+
+            if($p -> parse($sc)){
+              $this->current_handler->parse($sc);
+              $this->Script_Error = "";
+            } else {
+              $this->Script_Error = $p->status_text;
+            }
+          } 
         }
       }
-      $this->current_handler->save_object();
     }
   }
 
-
-  function save()
+  
+  function get_used_script_names()
   {
-    /* Connect to sieve class and try to get all available sieve scripts */
-    $cfg=  $this->config->data['SERVERS']['IMAP'][$this->parent->gosaMailServer];
+    $ret = array();
+    foreach($this->scripts as $script){
+      $ret[] = $script['NAME'];
+    }
+    return($ret);
+  }
 
-    $this->sieve_handle= 
-        new sieve(  $cfg["sieve_server"], 
-                    $cfg["sieve_port"], 
-                    $this->parent->mail,
-                    $cfg["password"], 
-                    $cfg["admin"]);
 
-    if (!$this->sieve_handle->sieve_login()){
-      print_red(sprintf(_("Can't log into SIEVE server. Server says '%s'."),to_string($this->sieve_handle->error_raw)));
-      return;
+
+  function save()
+  {
+    /* Get sieve */
+    if(!$this->sieve_handle = $this->get_sieve()){
+      print_red(
+          sprintf(
+            _("Can't log into SIEVE server. Server says '%s'."),
+            to_string($this->Sieve_Error)));
     }
 
     $everything_went_fine = TRUE;