Exemplo n.º 1
0
 function save_rules($id, $vars, &$errors)
 {
     $matches = array('name', 'email', 'subject', 'body', 'header');
     $types = array('equal', 'not_equal', 'contains', 'dn_contain');
     $rules = array();
     for ($i = 1; $i <= 25; $i++) {
         //Expecting no more than 25 rules...
         if ($vars["rule_w{$i}"] || $vars["rule_h{$i}"]) {
             if (!$vars["rule_w{$i}"] || !in_array($vars["rule_w{$i}"], $matches)) {
                 $errors["rule_{$i}"] = 'Invalid match selection';
             } elseif (!$vars["rule_h{$i}"] || !in_array($vars["rule_h{$i}"], $types)) {
                 $errors["rule_{$i}"] = 'Invalid match type selection';
             } elseif (!$vars["rule_v{$i}"]) {
                 $errors["rule_{$i}"] = 'Value required';
             } elseif ($vars["rule_w{$i}"] == 'email' && $vars["rule_h{$i}"] == 'equal' && !Validator::is_email($vars["rule_v{$i}"])) {
                 $errors["rule_{$i}"] = 'Valid email required for the match type';
             } else {
                 //for everything-else...we assume it's valid.
                 $rules[] = array('w' => $vars["rule_w{$i}"], 'h' => $vars["rule_h{$i}"], 'v' => $vars["rule_v{$i}"]);
             }
         } elseif ($vars["rule_v{$i}"]) {
             $errors["rule_{$i}"] = 'Incomplete selection';
         }
     }
     if (!$rules && is_array($vars["rules"])) {
         # XXX: Validation bypass
         $rules = $vars["rules"];
     } elseif (!$rules && !$errors) {
         $errors['rules'] = 'You must set at least one rule.';
     }
     if ($errors) {
         return false;
     }
     if (!$id) {
         return true;
     }
     //When ID is 0 then assume it was just validation...
     //Clear existing rules...we're doing mass replace on each save!!
     db_query('DELETE FROM ' . EMAIL_FILTER_RULE_TABLE . ' WHERE filter_id=' . db_input($id));
     $num = 0;
     foreach ($rules as $rule) {
         $rule['filter_id'] = $id;
         if (FilterRule::create($rule, $errors)) {
             $num++;
         }
     }
     return $num;
 }
Exemplo n.º 2
0
 function save_rules($id, $vars, &$errors)
 {
     $matches = array_keys(self::getSupportedMatchFields());
     $types = array_keys(self::getSupportedMatchTypes());
     $rules = array();
     for ($i = 1; $i <= 25; $i++) {
         //Expecting no more than 25 rules...
         if ($vars["rule_w{$i}"] || $vars["rule_h{$i}"]) {
             // Check for REGEX compile errors
             if (in_array($vars["rule_h{$i}"], array('match', 'not_match'))) {
                 $wrapped = "/" . $vars["rule_v{$i}"] . "/iu";
                 if (false === @preg_match($vars["rule_v{$i}"], ' ') && false !== @preg_match($wrapped, ' ')) {
                     $vars["rule_v{$i}"] = $wrapped;
                 }
             }
             if (!$vars["rule_w{$i}"] || !in_array($vars["rule_w{$i}"], $matches)) {
                 $errors["rule_{$i}"] = __('Invalid match selection');
             } elseif (!$vars["rule_h{$i}"] || !in_array($vars["rule_h{$i}"], $types)) {
                 $errors["rule_{$i}"] = __('Invalid match type selection');
             } elseif (!$vars["rule_v{$i}"]) {
                 $errors["rule_{$i}"] = __('Value required');
             } elseif ($vars["rule_w{$i}"] == 'email' && $vars["rule_h{$i}"] == 'equal' && !Validator::is_email($vars["rule_v{$i}"])) {
                 $errors["rule_{$i}"] = __('Valid email required for the match type');
             } elseif (in_array($vars["rule_h{$i}"], array('match', 'not_match')) && false === @preg_match($vars["rule_v{$i}"], ' ')) {
                 $errors["rule_{$i}"] = sprintf(__('Regex compile error: (#%s)'), preg_last_error());
             } else {
                 //for everything-else...we assume it's valid.
                 $rules[] = array('what' => $vars["rule_w{$i}"], 'how' => $vars["rule_h{$i}"], 'val' => trim($vars["rule_v{$i}"]));
             }
         } elseif ($vars["rule_v{$i}"]) {
             $errors["rule_{$i}"] = __('Incomplete selection');
         }
     }
     if (!$rules && is_array($vars["rules"])) {
         # XXX: Validation bypass
         $rules = $vars["rules"];
     } elseif (!$rules && !$errors) {
         $errors['rules'] = __('You must set at least one rule.');
     }
     if ($errors) {
         return false;
     }
     if (!$id) {
         return true;
     }
     //When ID is 0 then assume it was just validation...
     //Clear existing rules...we're doing mass replace on each save!!
     db_query('DELETE FROM ' . FILTER_RULE_TABLE . ' WHERE filter_id=' . db_input($id));
     $num = 0;
     foreach ($rules as $rule) {
         $rule['filter_id'] = $id;
         if (FilterRule::create($rule, $errors)) {
             $num++;
         }
     }
     return $num;
 }