1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file LICENSE for license information (ASL).  If you
6 * did not receive this file, see http://www.horde.org/licenses/apache.
7 *
8 * @author   Mike Cochrane <mike@graftonhall.co.nz>
9 * @author   Jan Schneider <jan@horde.org>
10 * @category Horde
11 * @license  http://www.horde.org/licenses/apache ASL
12 * @package  Ingo
13 */
14
15/**
16 * The Ingo_Script_Sieve_Test_Header class represents a test on the contents
17 * of one or more headers in a message.
18 *
19 * @author   Mike Cochrane <mike@graftonhall.co.nz>
20 * @author   Jan Schneider <jan@horde.org>
21 * @category Horde
22 * @license  http://www.horde.org/licenses/apache ASL
23 * @package  Ingo
24 */
25class Ingo_Script_Sieve_Test_Header extends Ingo_Script_Sieve_Test
26{
27    /**
28     * Constructor.
29     *
30     * @param array $vars  Any required parameters.
31     */
32    public function __construct($vars = array())
33    {
34        $this->_vars['headers'] = isset($vars['headers'])
35            ? $vars['headers']
36            : 'Subject';
37        $this->_vars['comparator'] = isset($vars['comparator'])
38            ? $vars['comparator']
39            : 'i;ascii-casemap';
40        $this->_vars['match-type'] = isset($vars['match-type'])
41            ? $vars['match-type']
42            : ':is';
43        $this->_vars['strings'] = isset($vars['strings'])
44            ? $vars['strings']
45            : '';
46    }
47
48    /**
49     * Checks if the rule parameters are valid.
50     *
51     * @return boolean|string  True if this rule is valid, an error message
52     *                         otherwise.
53     */
54    public function check()
55    {
56        return preg_split('((?<!\\\)\,|\r\n|\n|\r)', $this->_vars['headers']) &&
57               preg_split('((?<!\\\)\,|\r\n|\n|\r)', $this->_vars['strings']);
58    }
59
60    /**
61     * Returns a script snippet representing this rule and any sub-rules.
62     *
63     * @return string  A Sieve script snippet.
64     */
65    public function generate()
66    {
67        $code = 'header ' .
68            ':comparator "' . $this->_vars['comparator'] . '" ' .
69            $this->_vars['match-type'] . ' ';
70
71        $headers = preg_split('(\r\n|\n|\r)', $this->_vars['headers']);
72        $headers = array_filter($headers);
73        if (count($headers) > 1) {
74            $code .= "[";
75            $headerstr = '';
76            foreach ($headers as $header) {
77                $headerstr .= empty($headerstr) ? '"' : ', "';
78                $headerstr .= Ingo_Script_Sieve::escapeString($header, $this->_vars['match-type'] == ':regex') . '"';
79            }
80            $code .= $headerstr . "] ";
81        } elseif (count($headers) == 1) {
82            $code .= '"' . $headers[0] . '" ';
83        } else {
84            return _("No headers specified");
85        }
86
87        $strings = preg_split('(\r\n|\n|\r)', $this->_vars['strings']);
88        $strings = array_filter($strings);
89        if (count($strings) > 1) {
90            $code .= "[";
91            $stringlist = '';
92            foreach ($strings as $str) {
93                $stringlist .= empty($stringlist) ? '"' : ', "';
94                $stringlist .= Ingo_Script_Sieve::escapeString($str, $this->_vars['match-type'] == ':regex') . '"';
95            }
96            $code .= $stringlist . "] ";
97        } elseif (count($strings) == 1) {
98            $code .= '"' . Ingo_Script_Sieve::escapeString(reset($strings), $this->_vars['match-type'] == ':regex') . '" ';
99        } else {
100            return _("No strings specified");
101        }
102
103        return $code;
104    }
105
106    /**
107     * Returns a list of sieve extensions required for this rule and any
108     * sub-rules.
109     *
110     * @return array  A Sieve extension list.
111     */
112    public function requires()
113    {
114        return ($this->_vars['match-type'] == ':regex')
115            ? array('regex')
116            : array();
117    }
118}
119