Code

Added DNS and DHCP enable disable options into setup step 2
[gosa.git] / include / sieve / class_sieveElement_If.inc
index ef726c91a5c381245f21f2b20ea9cae84142e374..acdcb94d5d6abd1b101abd8efcfb1010236d3fa7 100644 (file)
@@ -20,11 +20,12 @@ class sieve_if
   function sieve_if($elements,$object_id,$parent)
   {
     $this->parent = $parent;
+    $this->object_id       = $object_id;
   
     /* Possible address parts we can select */
     $this->address_parts = array( 
-        ":all"       => _("Complete adress")." ("._("Default").")",
-        ":domain"    => _("Domian part") ,
+        ":all"       => _("Complete address")." ("._("Default").")",
+        ":domain"    => _("Domain part") ,
         ":localpart" => _("Local part"));
 
     /* comparator type */
@@ -36,6 +37,7 @@ class sieve_if
     /* Match types */
     $this->match_types  = array(  
         ":is"         => _("is"),
+        ":regex"      => _("regex"),
         ":contains"   => _("contains"),
         ":matches"    => _("matches"),
         ":count"      => _("count"),
@@ -50,10 +52,22 @@ class sieve_if
         "gt"  => _("greater than"),
         "ne"  => _("not equal"));
 
-    $this->object_id       = $object_id;
-    if($elements!=NULL){
-      $this->elements = $elements;
-      $this->_parsed  = $this->_parse($elements['ELEMENTS'],1);
+    /* Skip parsing if this element is new */
+    if($elements != NULL){
+
+      /* Remove comments from tests */  
+      $tmp = array();
+      foreach($elements['ELEMENTS'] as $ele){
+        if($ele['class'] != "comment"){
+          $tmp[] = $ele;
+        }
+      }
+      $elements['ELEMENTS'] = $tmp;
+
+      if($elements!=NULL){
+        $this->elements = $elements;
+        $this->_parsed  = $this->_parse($elements['ELEMENTS'],1);
+      }
     }
   }
 
@@ -84,6 +98,10 @@ class sieve_if
       $parsed = $this->_parsed;
     }
 
+    if($parsed == NULL) {
+        return(array(_("Can't save empty tests.")));
+    }
+
     /* Walk through all elements */
     foreach($parsed as $key => $data){
 
@@ -114,7 +132,7 @@ class sieve_if
          *******************/
 
         case "true" :
-        case "fasle" : 
+        case "false" : 
         {
           /* Can't fail anyway */
           break;
@@ -381,7 +399,10 @@ class sieve_if
           }          
 
           /* Add allof/anyof tag */
-          $script.= " ".$key." ( ";
+          if($Inverse){
+            $script .= "not ";
+          }
+          $script.= $key." ( ";
 
           /* Add each test parameter */
           foreach($data as $key2 => $dat){
@@ -429,6 +450,7 @@ class sieve_if
         }
         $data[$type]=$values;
 
+        $this->parent->add_require("relational");
         if($type == "envelope"){
           $this->parent->add_require("envelope");
         }
@@ -447,7 +469,7 @@ class sieve_if
         $tmp= array( 
             "Inverse"    => FALSE,
             "Match_type" => ":over",
-            "Value_List" => array(1,"M"));
+            "Value_List" => array("1M"));
 
         $tmp['LastError'] = "";
         $data[$type] = $tmp;
@@ -507,7 +529,9 @@ class sieve_if
       $parsed = $this->_parsed;
     }
 
-    if(!is_array($parsed)) return;
+    if(!is_array($parsed)) {
+      return;
+    }
 
     /* Walk through all elements */
     foreach($parsed as $key => $data){
@@ -580,6 +604,12 @@ class sieve_if
             if(!isset($match_types[$mt])){
               $parsed[$key]['LastError'] = _("Invalid match type given.");
             }
+            if($mt == ":regex"){
+              $this->parent->add_require("regex");
+            }
+            if($mt == ":count"){
+              $this->parent->add_require("comparator-i;ascii-numeric");
+            }
             $parsed[$key]['Match_type'] = $mt;
           }
 
@@ -618,7 +648,11 @@ class sieve_if
 
             $tmp2 = split(",",$vls);
             foreach($tmp2 as $val){
-              $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
+              $tmp[] = trim($val);
+  
+              if(preg_match("/\"/",$val)){
+                $parsed[$key]['LastError'] = _("Invalid character found in address attribute. Quotes are not allowed here.");
+              }
             }
             $parsed[$key]['Key_List'] = $tmp;
           }
@@ -630,7 +664,10 @@ class sieve_if
 
             $tmp2 = split(",",$vls);
             foreach($tmp2 as $val){
-              $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
+              $tmp[] = trim($val);
+              if(preg_match("/\"/",$val)){
+                $parsed[$key]['LastError'] = _("Invalid character found in value attribute. Quotes are not allowed here.");
+              }
             }
             $parsed[$key]['Value_List'] = $tmp;
           }
@@ -689,8 +726,14 @@ class sieve_if
           $Match_types = array( ":over" => _("greater than") ,
                                 ":under" => _("lower than"));
 
-          $Units       = array( "M" => _("Megabyte") ,
-                                "K" => _("Kilobyte"));
+          $Units       = array( "M" => _("Megabyte"),
+                                "K" => _("Kilobyte"),
+                                ""  => _("Bytes"));
+
+          /* Toggle Inverse ? */
+          if(isset($_POST['toggle_inverse_'.$element_id])){
+            $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
+          }
 
           /* Reset error */
           $parsed[$key]['LastError'] ="";
@@ -792,7 +835,7 @@ class sieve_if
     if($this->TYPE == "if"){
       $name .= " - "._("If");
     }elseif($this->TYPE == "elsif"){
-      $name .= " - "._("Else if");
+      $name .= " - "._("Else If");
     }else{
       $name .= " - "._("Else");
     }
@@ -800,12 +843,8 @@ class sieve_if
     $smarty = get_smarty();
     $smarty->assign("ID", $this->object_id);
 
-    /* Only display navigation elements if necessary */
-    if($this->TYPE == "if"){
-      $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
-    }else{
-      $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
-    }
+    /* Get navigation element container */
+    $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
 
     $smarty->assign("Name", $name);
     $smarty->assign("Contents", $this->get_as_html());
@@ -815,7 +854,7 @@ class sieve_if
     }else{
       $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
     }
-    $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
+    $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
     return($str);
   }
 
@@ -1001,10 +1040,12 @@ class sieve_if
             $values .= $key.", ";
           }
           $values = preg_replace("/,$/","",trim($values));
+
           $smarty->assign("keys",$keys);
-          $smarty->assign("values",$values);
+          $smarty->assign("values", $values);
           $smarty->assign("ID"  , $element_id); 
-          $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
+          $str = $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
+          $ret .= $str;
           break;
         }
       
@@ -1018,8 +1059,9 @@ class sieve_if
           $Match_types = array( ":over" => _("greater than") , 
                                 ":under" => _("lower than"));
 
-          $Units       = array( "M" => _("Megabyte") , 
-                                "K" => _("Kilobyte")); 
+          $Units       = array( "M" => _("Megabyte"),
+                                "K" => _("Kilobyte"),
+                                ""  => _("Bytes"));
 
           $Match_type   = $data['Match_type'];
           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
@@ -1089,7 +1131,7 @@ class sieve_if
           $smarty->assign("DisplayAdd",TRUE);
           $smarty->assign("DisplayDel",FALSE);
           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
-          $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
+          $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/","<b>"._("Click here to add a new test")."</b>",$cont_tmp);
 
           $smarty->assign("Inverse",$Inverse);
           $smarty->assign("Contents",$cont_tmp.$Contents);
@@ -1119,7 +1161,7 @@ class sieve_if
           $smarty->assign("DisplayAdd",TRUE);
           $smarty->assign("DisplayDel",FALSE);
           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
-          $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
+          $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
 
           $smarty->assign("Inverse",$Inverse);
           $smarty->assign("Contents",$cont_tmp.$Contents);
@@ -1131,13 +1173,7 @@ class sieve_if
         } 
         default : 
         {
-          $ret = "<table width='100%'  cellspacing=0 cellpadding=0>
-                    <tr>
-                      <td style='background-color: #FEDCA9 ; border: solid 1px        #EEEEEE'>";
-          $ret.= $key."<br>"; 
-          $ret.= "    </td>
-                    </tr>
-                  </table>";
+          trigger_error(_("Unhandled switch type"));
         }
       }
     }
@@ -1150,7 +1186,7 @@ class sieve_if
     $smarty->assign("DisplayAdd",FALSE);
     $smarty->assign("DisplayDel",TRUE);
     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
-    $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
+    $ret = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($ret,"\\"),$str);
     return($ret);
   }
 
@@ -1160,8 +1196,16 @@ class sieve_if
    */
   function _parse($data,$id = 0)
   {
-    $av_methods   = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
-    $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
+    $av_match_type = array();
+    foreach($this->match_types as $name => $description){
+      $av_match_type[] = $name;
+    }
+    $av_match_type[] = ":over";
+    $av_match_type[] = ":under";
+
+
+
+    $av_methods= array("address","allof","anyof","exists","false","header","not","size","true","envelope");
     $type = $data[$id]['text'];
     $tmp = array();
 
@@ -1196,7 +1240,7 @@ class sieve_if
    
         
         $part     = "(:all|:localpart|:domain)";
-        $operator = "(:contains|:is|:matches|:count|:value)";
+        $operator = "(:regex|:contains|:is|:matches|:count|:value)";
         $value_op = "(lt|le|eq|ge|gt|ne)";
 
         $Address_Part     = "";