Ingo_Script_Maildrop_Recipe::addCondition PHP Method

addCondition() public method

Adds a condition to the recipe.
public addCondition ( $condition = [] )
    public function addCondition($condition = array())
    {
        $flag = !empty($condition['case']) ? 'D' : '';
        if (empty($this->_conditions)) {
            $this->addFlag($flag);
        }
        $string = '';
        $extra = '';
        $match = isset($condition['match']) ? $condition['match'] : null;
        // negate tests starting with 'not ', except 'not equals', which simply uses the != operator
        if ($match != 'not equal' && substr($match, 0, 4) == 'not ') {
            $string .= '! ';
        }
        // convert 'field' to PCRE pattern matching
        if (strpos($condition['field'], ',') == false) {
            $string .= '/^' . $condition['field'] . ':\\s*';
        } else {
            $string .= '/^(' . str_replace(',', '|', $condition['field']) . '):\\s*';
        }
        switch ($match) {
            case 'not regex':
            case 'regex':
                $string .= $condition['value'] . '/:h';
                break;
            case 'filter':
                $string = $condition['value'];
                break;
            case 'exists':
            case 'not exist':
                // Just run a match for the header name
                $string .= '/:h';
                break;
            case 'less than or equal to':
            case 'less than':
            case 'equal':
            case 'not equal':
            case 'greater than or equal to':
            case 'greater than':
                $string .= '(\\d+(\\.\\d+)?)/:h';
                $extra = ' && $MATCH1 ' . $this->_operators[$match] . ' ' . (int) $condition['value'];
                break;
            case 'begins with':
            case 'not begins with':
                $string .= preg_quote($condition['value'], '/') . '/:h';
                break;
            case 'ends with':
            case 'not ends with':
                $string .= '.*' . preg_quote($condition['value'], '/') . '$/:h';
                break;
            case 'is':
            case 'not is':
                $string .= preg_quote($condition['value'], '/') . '$/:h';
                break;
            case 'matches':
            case 'not matches':
                $string .= str_replace(array('\\*', '\\?'), array('.*', '.'), preg_quote($condition['value'], '/') . '$') . '/:h';
                break;
            case 'contains':
            case 'not contain':
            default:
                $string .= '.*' . preg_quote($condition['value'], '/') . '/:h';
                break;
        }
        $this->_conditions[] = array('condition' => $string, 'flags' => $flag, 'extra' => $extra);
    }

Usage Example

示例#1
0
 /**
  * Generates the maildrop script to handle spam as identified by
  * SpamAssassin.
  *
  * @param Ingo_Rule $rule  Rule object.
  */
 protected function _generateSpam(Ingo_Rule $rule)
 {
     $this->_addItem(Ingo::RULE_SPAM, new Ingo_Script_Maildrop_Comment(_("Spam Filter"), $rule->disable, true));
     $recipe = new Ingo_Script_Maildrop_Recipe(array('action-value' => $rule->mailbox, 'action' => strlen($rule->mailbox) ? 'Ingo_Rule_User_Move' : 'Ingo_Rule_User_Discard', 'disable' => $rule->disable), $this->_params);
     if ($this->_params['spam_compare'] == 'numeric') {
         $recipe->addCondition(array('match' => 'greater than or equal to', 'field' => $this->_params['spam_header'], 'value' => $rule->level));
     } elseif ($this->_params['spam_compare'] == 'string') {
         $recipe->addCondition(array('match' => 'contains', 'field' => $this->_params['spam_header'], 'value' => str_repeat($this->_params['spam_char'], $rule->level)));
     }
     $this->_addItem(Ingo::RULE_SPAM, $recipe);
 }
All Usage Examples Of Ingo_Script_Maildrop_Recipe::addCondition