| | |
| | | * Copyright (C) 2008-2011, The Roundcube Dev Team |
| | | * Copyright (C) 2011, Kolab Systems AG |
| | | * |
| | | * This program is free software; you can redistribute it and/or modify |
| | | * it under the terms of the GNU General Public License version 2 |
| | | * as published by the Free Software Foundation. |
| | | * This program is free software: you can redistribute it and/or modify |
| | | * it under the terms of the GNU General Public License as published by |
| | | * the Free Software Foundation, either version 3 of the License, or |
| | | * (at your option) any later version. |
| | | * |
| | | * This program is distributed in the hope that it will be useful, |
| | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | | * GNU General Public License for more details. |
| | | * |
| | | * You should have received a copy of the GNU General Public License along |
| | | * with this program; if not, write to the Free Software Foundation, Inc., |
| | | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| | | * You should have received a copy of the GNU General Public License |
| | | * along with this program. If not, see http://www.gnu.org/licenses/. |
| | | */ |
| | | |
| | | class rcube_sieve_script |
| | | { |
| | | public $content = array(); // script rules array |
| | | |
| | | private $vars = array(); // "global" variables |
| | | private $prefix = ''; // script header (comments) |
| | | private $supported = array( // Sieve extensions supported by class |
| | | 'fileinto', // RFC5228 |
| | | 'envelope', // RFC5228 |
| | | 'reject', // RFC5429 |
| | | 'ereject', // RFC5429 |
| | | private $vars = array(); // "global" variables |
| | | private $prefix = ''; // script header (comments) |
| | | private $supported = array( // supported Sieve extensions: |
| | | 'body', // RFC5173 |
| | | 'copy', // RFC3894 |
| | | 'vacation', // RFC5230 |
| | | 'relational', // RFC3431 |
| | | 'regex', // draft-ietf-sieve-regex-01 |
| | | 'date', // RFC5260 |
| | | 'duplicate', // RFC7352 |
| | | 'enotify', // RFC5435 |
| | | 'envelope', // RFC5228 |
| | | 'ereject', // RFC5429 |
| | | 'fileinto', // RFC5228 |
| | | 'imapflags', // draft-melnikov-sieve-imapflags-06 |
| | | 'imap4flags', // RFC5232 |
| | | 'include', // draft-ietf-sieve-include-12 |
| | | 'variables', // RFC5229 |
| | | 'body', // RFC5173 |
| | | 'include', // RFC6609 |
| | | 'index', // RFC5260 |
| | | 'notify', // RFC5435 |
| | | 'regex', // draft-ietf-sieve-regex-01 |
| | | 'reject', // RFC5429 |
| | | 'relational', // RFC3431 |
| | | 'subaddress', // RFC5233 |
| | | 'enotify', // RFC5435 |
| | | 'notify', // draft-ietf-sieve-notify-00 |
| | | // @TODO: spamtest+virustest, mailbox, date |
| | | 'vacation', // RFC5230 |
| | | 'vacation-seconds', // RFC6131 |
| | | 'variables', // RFC5229 |
| | | // @TODO: spamtest+virustest, mailbox |
| | | ); |
| | | |
| | | /** |
| | |
| | | |
| | | // rules |
| | | foreach ($this->content as $rule) { |
| | | $extension = ''; |
| | | $script = ''; |
| | | $tests = array(); |
| | | $i = 0; |
| | |
| | | $tests[$i] .= ($test['not'] ? 'not ' : ''); |
| | | $tests[$i] .= 'header'; |
| | | |
| | | if (!empty($test['type'])) { |
| | | // relational operator + comparator |
| | | if (preg_match('/^(value|count)-([gteqnl]{2})/', $test['type'], $m)) { |
| | | array_push($exts, 'relational'); |
| | | array_push($exts, 'comparator-i;ascii-numeric'); |
| | | |
| | | $tests[$i] .= ' :' . $m[1] . ' "' . $m[2] . '" :comparator "i;ascii-numeric"'; |
| | | } |
| | | else { |
| | | $this->add_comparator($test, $tests[$i], $exts); |
| | | |
| | | if ($test['type'] == 'regex') { |
| | | array_push($exts, 'regex'); |
| | | } |
| | | |
| | | $tests[$i] .= ' :' . $test['type']; |
| | | } |
| | | } |
| | | $this->add_index($test, $tests[$i], $exts); |
| | | $this->add_operator($test, $tests[$i], $exts); |
| | | |
| | | $tests[$i] .= ' ' . self::escape_string($test['arg1']); |
| | | $tests[$i] .= ' ' . self::escape_string($test['arg2']); |
| | |
| | | $tests[$i] .= ($test['not'] ? 'not ' : ''); |
| | | $tests[$i] .= $test['test']; |
| | | |
| | | if (!empty($test['part'])) { |
| | | if ($test['test'] != 'envelope') { |
| | | $this->add_index($test, $tests[$i], $exts); |
| | | } |
| | | |
| | | // :all address-part is optional, skip it |
| | | if (!empty($test['part']) && $test['part'] != 'all') { |
| | | $tests[$i] .= ' :' . $test['part']; |
| | | if ($test['part'] == 'user' || $test['part'] == 'detail') { |
| | | array_push($exts, 'subaddress'); |
| | | } |
| | | } |
| | | |
| | | $this->add_comparator($test, $tests[$i], $exts); |
| | | |
| | | if (!empty($test['type'])) { |
| | | if ($test['type'] == 'regex') { |
| | | array_push($exts, 'regex'); |
| | | } |
| | | $tests[$i] .= ' :' . $test['type']; |
| | | } |
| | | $this->add_operator($test, $tests[$i], $exts); |
| | | |
| | | $tests[$i] .= ' ' . self::escape_string($test['arg1']); |
| | | $tests[$i] .= ' ' . self::escape_string($test['arg2']); |
| | |
| | | |
| | | $tests[$i] .= ($test['not'] ? 'not ' : '') . 'body'; |
| | | |
| | | $this->add_comparator($test, $tests[$i], $exts); |
| | | |
| | | if (!empty($test['part'])) { |
| | | $tests[$i] .= ' :' . $test['part']; |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | if (!empty($test['type'])) { |
| | | if ($test['type'] == 'regex') { |
| | | array_push($exts, 'regex'); |
| | | } |
| | | $tests[$i] .= ' :' . $test['type']; |
| | | } |
| | | $this->add_operator($test, $tests[$i], $exts); |
| | | |
| | | $tests[$i] .= ' ' . self::escape_string($test['arg']); |
| | | break; |
| | | |
| | | case 'date': |
| | | case 'currentdate': |
| | | array_push($exts, 'date'); |
| | | |
| | | $tests[$i] .= ($test['not'] ? 'not ' : '') . $test['test']; |
| | | |
| | | $this->add_index($test, $tests[$i], $exts); |
| | | |
| | | if (!empty($test['originalzone']) && $test['test'] == 'date') { |
| | | $tests[$i] .= ' :originalzone'; |
| | | } |
| | | else if (!empty($test['zone'])) { |
| | | $tests[$i] .= ' :zone ' . self::escape_string($test['zone']); |
| | | } |
| | | |
| | | $this->add_operator($test, $tests[$i], $exts); |
| | | |
| | | if ($test['test'] == 'date') { |
| | | $tests[$i] .= ' ' . self::escape_string($test['header']); |
| | | } |
| | | |
| | | $tests[$i] .= ' ' . self::escape_string($test['part']); |
| | | $tests[$i] .= ' ' . self::escape_string($test['arg']); |
| | | |
| | | break; |
| | | |
| | | case 'duplicate': |
| | | array_push($exts, 'duplicate'); |
| | | |
| | | $tests[$i] .= ($test['not'] ? 'not ' : '') . $test['test']; |
| | | |
| | | $tokens = array('handle', 'uniqueid', 'header'); |
| | | foreach ($tokens as $token) |
| | | if ($test[$token] !== null && $test[$token] !== '') { |
| | | $tests[$i] .= " :$token " . self::escape_string($test[$token]); |
| | | } |
| | | |
| | | if (!empty($test['seconds'])) { |
| | | $tests[$i] .= ' :seconds ' . intval($test['seconds']); |
| | | } |
| | | |
| | | if (!empty($test['last'])) { |
| | | $tests[$i] .= ' :last'; |
| | | } |
| | | |
| | | break; |
| | | } |
| | | |
| | | $i++; |
| | | } |
| | | } |
| | |
| | | array_push($exts, $notify); |
| | | $action_script .= 'notify'; |
| | | |
| | | // Here we support only 00 version of notify draft, there |
| | | // were a couple regressions in 00 to 04 changelog, we use |
| | | // the version used by Cyrus |
| | | $method = $action['method']; |
| | | unset($action['method']); |
| | | $action['options'] = (array) $action['options']; |
| | | |
| | | // Here we support draft-martin-sieve-notify-01 used by Cyrus |
| | | if ($notify == 'notify') { |
| | | switch ($action['importance']) { |
| | | case 1: $action_script .= " :high"; break; |
| | | case 2: $action_script .= " :normal"; break; |
| | | //case 2: $action_script .= " :normal"; break; |
| | | case 3: $action_script .= " :low"; break; |
| | | |
| | | } |
| | | |
| | | // Old-draft way: :method "mailto" :options "email@address" |
| | | if (!empty($method)) { |
| | | $parts = explode(':', $method, 2); |
| | | $action['method'] = $parts[0]; |
| | | array_unshift($action['options'], $parts[1]); |
| | | } |
| | | |
| | | unset($action['importance']); |
| | | unset($action['from']); |
| | | unset($method); |
| | | } |
| | | |
| | | foreach (array('from', 'importance', 'options', 'message') as $n_tag) { |
| | | foreach (array('id', 'importance', 'method', 'options', 'from', 'message') as $n_tag) { |
| | | if (!empty($action[$n_tag])) { |
| | | $action_script .= " :$n_tag " . self::escape_string($action[$n_tag]); |
| | | } |
| | | } |
| | | |
| | | if (!empty($action['address'])) { |
| | | $method = 'mailto:' . $action['address']; |
| | | if (!empty($action['body'])) { |
| | | $method .= '?body=' . rawurlencode($action['body']); |
| | | } |
| | | } |
| | | else { |
| | | $method = $action['method']; |
| | | } |
| | | |
| | | // method is optional in notify extension |
| | | if (!empty($method)) { |
| | | $action_script .= ($notify == 'notify' ? " :method " : " ") . self::escape_string($method); |
| | | $action_script .= ' ' . self::escape_string($method); |
| | | } |
| | | |
| | | break; |
| | |
| | | case 'vacation': |
| | | array_push($exts, 'vacation'); |
| | | $action_script .= 'vacation'; |
| | | if (!empty($action['days'])) |
| | | $action_script .= " :days " . $action['days']; |
| | | if (isset($action['seconds'])) { |
| | | array_push($exts, 'vacation-seconds'); |
| | | $action_script .= " :seconds " . intval($action['seconds']); |
| | | } |
| | | else if (!empty($action['days'])) { |
| | | $action_script .= " :days " . intval($action['days']); |
| | | } |
| | | if (!empty($action['addresses'])) |
| | | $action_script .= " :addresses " . self::escape_string($action['addresses']); |
| | | if (!empty($action['subject'])) |
| | |
| | | } |
| | | |
| | | // requires |
| | | if (!empty($exts)) |
| | | $output = 'require ["' . implode('","', array_unique($exts)) . "\"];\n" . $output; |
| | | if (!empty($exts)) { |
| | | $exts = array_unique($exts); |
| | | |
| | | if (in_array('vacation-seconds', $exts) && ($key = array_search('vacation', $exts)) !== false) { |
| | | unset($exts[$key]); |
| | | } |
| | | |
| | | sort($exts); // for convenience use always the same order |
| | | |
| | | $output = 'require ["' . implode('","', $exts) . "\"];\n" . $output; |
| | | } |
| | | |
| | | if (!empty($this->prefix)) { |
| | | $output = $this->prefix . "\n\n" . $output; |
| | |
| | | */ |
| | | private function _parse_text($script) |
| | | { |
| | | $prefix = ''; |
| | | $options = array(); |
| | | $prefix = ''; |
| | | $options = array(); |
| | | $position = 0; |
| | | $length = strlen($script); |
| | | |
| | | while ($script) { |
| | | $script = trim($script); |
| | | $rule = array(); |
| | | while ($position < $length) { |
| | | // skip whitespace chars |
| | | $position = self::ltrim_position($script, $position); |
| | | $rulename = ''; |
| | | |
| | | // Comments |
| | | while (!empty($script) && $script[0] == '#') { |
| | | $endl = strpos($script, "\n"); |
| | | $line = $endl ? substr($script, 0, $endl) : $script; |
| | | while ($script[$position] === '#') { |
| | | $endl = strpos($script, "\n", $position) ?: $length; |
| | | $line = substr($script, $position, $endl - $position); |
| | | |
| | | // Roundcube format |
| | | if (preg_match('/^# rule:\[(.*)\]/', $line, $matches)) { |
| | |
| | | $prefix .= $line . "\n"; |
| | | } |
| | | |
| | | $script = ltrim(substr($script, strlen($line) + 1)); |
| | | $position = $endl + 1; |
| | | } |
| | | |
| | | // handle script header |
| | |
| | | } |
| | | |
| | | // Control structures/blocks |
| | | if (preg_match('/^(if|else|elsif)/i', $script)) { |
| | | $rule = $this->_tokenize_rule($script); |
| | | if (preg_match('/^(if|else|elsif)/i', substr($script, $position, 5))) { |
| | | $rule = $this->_tokenize_rule($script, $position); |
| | | if (strlen($rulename) && !empty($rule)) { |
| | | $rule['name'] = $rulename; |
| | | } |
| | | } |
| | | // Simple commands |
| | | else { |
| | | $rule = $this->_parse_actions($script, ';'); |
| | | $rule = $this->_parse_actions($script, $position, ';'); |
| | | if (!empty($rule[0]) && is_array($rule)) { |
| | | // set "global" variables |
| | | if ($rule[0]['type'] == 'set') { |
| | | unset($rule[0]['type']); |
| | | $this->vars[] = $rule[0]; |
| | | unset($rule); |
| | | } |
| | | else { |
| | | $rule = array('actions' => $rule); |
| | | } |
| | | } |
| | | } |
| | | |
| | | $rulename = ''; |
| | | |
| | | if (!empty($rule)) { |
| | | $this->content[] = $rule; |
| | |
| | | /** |
| | | * Convert text script fragment to rule object |
| | | * |
| | | * @param string Text rule |
| | | * @param string $content The whole script content |
| | | * @param int &$position Start position in the script |
| | | * |
| | | * @return array Rule data |
| | | */ |
| | | private function _tokenize_rule(&$content) |
| | | private function _tokenize_rule($content, &$position) |
| | | { |
| | | $cond = strtolower(self::tokenize($content, 1)); |
| | | |
| | | $cond = strtolower(self::tokenize($content, 1, $position)); |
| | | if ($cond != 'if' && $cond != 'elsif' && $cond != 'else') { |
| | | return null; |
| | | } |
| | | |
| | | $disabled = false; |
| | | $join = false; |
| | | $join_not = false; |
| | | $length = strlen($content); |
| | | |
| | | // disabled rule (false + comment): if false # ..... |
| | | if (preg_match('/^\s*false\s+#/i', $content)) { |
| | | $content = preg_replace('/^\s*false\s+#\s*/i', '', $content); |
| | | if (preg_match('/^\s*false\s+#\s*/i', substr($content, $position, 20), $m)) { |
| | | $position += strlen($m[0]); |
| | | $disabled = true; |
| | | } |
| | | |
| | | while (strlen($content)) { |
| | | $tokens = self::tokenize($content, true); |
| | | while ($position < $length) { |
| | | $tokens = self::tokenize($content, true, $position); |
| | | $separator = array_pop($tokens); |
| | | |
| | | if (!empty($tokens)) { |
| | |
| | | $not = false; |
| | | } |
| | | |
| | | // we support "not allof" as a negation of allof sub-tests |
| | | if ($join_not) { |
| | | $not = !$not; |
| | | } |
| | | |
| | | switch ($token) { |
| | | case 'allof': |
| | | $join = true; |
| | | $join = true; |
| | | $join_not = $not; |
| | | break; |
| | | |
| | | case 'anyof': |
| | | break; |
| | | |
| | | case 'size': |
| | | $size = array('test' => 'size', 'not' => $not); |
| | | $test = array('test' => 'size', 'not' => $not); |
| | | |
| | | $test['arg'] = array_pop($tokens); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) |
| | | && preg_match('/^:(under|over)$/i', $tokens[$i]) |
| | | ) { |
| | | $size['type'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | else { |
| | | $size['arg'] = $tokens[$i]; |
| | | $test['type'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | } |
| | | |
| | | $tests[] = $size; |
| | | $tests[] = $test; |
| | | break; |
| | | |
| | | case 'header': |
| | | $header = array('test' => 'header', 'not' => $not, 'arg1' => '', 'arg2' => ''); |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { |
| | | $header['comparator'] = $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(count|value)$/i', $tokens[$i])) { |
| | | $header['type'] = strtolower(substr($tokens[$i], 1)) . '-' . $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { |
| | | $header['type'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | else { |
| | | $header['arg1'] = $header['arg2']; |
| | | $header['arg2'] = $tokens[$i]; |
| | | } |
| | | } |
| | | |
| | | $tests[] = $header; |
| | | break; |
| | | |
| | | case 'address': |
| | | case 'envelope': |
| | | $header = array('test' => $token, 'not' => $not, 'arg1' => '', 'arg2' => ''); |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { |
| | | $header['comparator'] = $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { |
| | | $header['type'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(localpart|domain|all|user|detail)$/i', $tokens[$i])) { |
| | | $header['part'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | else { |
| | | $header['arg1'] = $header['arg2']; |
| | | $header['arg2'] = $tokens[$i]; |
| | | $test = array('test' => $token, 'not' => $not); |
| | | |
| | | $test['arg2'] = array_pop($tokens); |
| | | $test['arg1'] = array_pop($tokens); |
| | | |
| | | $test += $this->test_tokens($tokens); |
| | | |
| | | if ($token != 'header' && !empty($tokens)) { |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:(localpart|domain|all|user|detail)$/i', $tokens[$i])) { |
| | | $test['part'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | $tests[] = $header; |
| | | $tests[] = $test; |
| | | break; |
| | | |
| | | case 'body': |
| | | $header = array('test' => 'body', 'not' => $not, 'arg' => ''); |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { |
| | | $header['comparator'] = $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { |
| | | $header['type'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(raw|content|text)$/i', $tokens[$i])) { |
| | | $header['part'] = strtolower(substr($tokens[$i], 1)); |
| | | $test = array('test' => 'body', 'not' => $not); |
| | | |
| | | if ($header['part'] == 'content') { |
| | | $header['content'] = $tokens[++$i]; |
| | | $test['arg'] = array_pop($tokens); |
| | | |
| | | $test += $this->test_tokens($tokens); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:(raw|content|text)$/i', $tokens[$i])) { |
| | | $test['part'] = strtolower(substr($tokens[$i], 1)); |
| | | |
| | | if ($test['part'] == 'content') { |
| | | $test['content'] = $tokens[++$i]; |
| | | } |
| | | } |
| | | else { |
| | | $header['arg'] = $tokens[$i]; |
| | | } |
| | | } |
| | | |
| | | $tests[] = $header; |
| | | $tests[] = $test; |
| | | break; |
| | | |
| | | case 'date': |
| | | case 'currentdate': |
| | | $test = array('test' => $token, 'not' => $not); |
| | | |
| | | $test['arg'] = array_pop($tokens); |
| | | $test['part'] = array_pop($tokens); |
| | | |
| | | if ($token == 'date') { |
| | | $test['header'] = array_pop($tokens); |
| | | } |
| | | |
| | | $test += $this->test_tokens($tokens); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:zone$/i', $tokens[$i])) { |
| | | $test['zone'] = $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:originalzone$/i', $tokens[$i])) { |
| | | $test['originalzone'] = true; |
| | | } |
| | | } |
| | | |
| | | $tests[] = $test; |
| | | break; |
| | | |
| | | case 'duplicate': |
| | | $test = array('test' => $token, 'not' => $not); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i])) { |
| | | if (preg_match('/^:(handle|header|uniqueid|seconds)$/i', $tokens[$i], $m)) { |
| | | $test[strtolower($m[1])] = $tokens[++$i]; |
| | | } |
| | | else if (preg_match('/^:last$/i', $tokens[$i])) { |
| | | $test['last'] = true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | $tests[] = $test; |
| | | break; |
| | | |
| | | case 'exists': |
| | | $tests[] = array('test' => 'exists', 'not' => $not, |
| | | $tests[] = array('test' => 'exists', 'not' => $not, |
| | | 'arg' => array_pop($tokens)); |
| | | break; |
| | | |
| | | case 'true': |
| | | $tests[] = array('test' => 'true', 'not' => $not); |
| | | $tests[] = array('test' => 'true', 'not' => $not); |
| | | break; |
| | | |
| | | case 'false': |
| | | $tests[] = array('test' => 'true', 'not' => !$not); |
| | | $tests[] = array('test' => 'true', 'not' => !$not); |
| | | break; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // ...and actions block |
| | | $actions = $this->_parse_actions($content); |
| | | $actions = $this->_parse_actions($content, $position); |
| | | |
| | | if ($tests && $actions) { |
| | | $result = array( |
| | |
| | | /** |
| | | * Parse body of actions section |
| | | * |
| | | * @param string $content Text body |
| | | * @param string $end End of text separator |
| | | * @param string $content The whole script content |
| | | * @param int &$position Start position in the script |
| | | * @param string $end End of text separator |
| | | * |
| | | * @return array Array of parsed action type/target pairs |
| | | */ |
| | | private function _parse_actions(&$content, $end = '}') |
| | | private function _parse_actions($content, &$position, $end = '}') |
| | | { |
| | | $result = null; |
| | | $length = strlen($content); |
| | | |
| | | while (strlen($content)) { |
| | | $tokens = self::tokenize($content, true); |
| | | while ($position < $length) { |
| | | $tokens = self::tokenize($content, true, $position); |
| | | $separator = array_pop($tokens); |
| | | |
| | | if (!empty($tokens)) { |
| | | $token = array_shift($tokens); |
| | | } |
| | | else { |
| | | $token = $separator; |
| | | } |
| | | $token = !empty($tokens) ? array_shift($tokens) : $separator; |
| | | |
| | | switch ($token) { |
| | | case 'discard': |
| | |
| | | |
| | | case 'fileinto': |
| | | case 'redirect': |
| | | $copy = false; |
| | | $target = ''; |
| | | $action = array('type' => $token, 'target' => array_pop($tokens)); |
| | | $args = array('copy'); |
| | | $action += $this->action_arguments($tokens, $args); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (strtolower($tokens[$i]) == ':copy') { |
| | | $copy = true; |
| | | } |
| | | else { |
| | | $target = $tokens[$i]; |
| | | } |
| | | } |
| | | $result[] = $action; |
| | | break; |
| | | |
| | | $result[] = array('type' => $token, 'copy' => $copy, |
| | | 'target' => $target); |
| | | case 'vacation': |
| | | $action = array('type' => 'vacation', 'reason' => array_pop($tokens)); |
| | | $args = array('mime'); |
| | | $vargs = array('seconds', 'days', 'addresses', 'subject', 'handle', 'from'); |
| | | $action += $this->action_arguments($tokens, $args, $vargs); |
| | | |
| | | $result[] = $action; |
| | | break; |
| | | |
| | | case 'reject': |
| | | case 'ereject': |
| | | $result[] = array('type' => $token, 'target' => array_pop($tokens)); |
| | | break; |
| | | |
| | | case 'vacation': |
| | | $vacation = array('type' => 'vacation', 'reason' => array_pop($tokens)); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | $tok = strtolower($tokens[$i]); |
| | | if ($tok == ':days') { |
| | | $vacation['days'] = $tokens[++$i]; |
| | | } |
| | | else if ($tok == ':subject') { |
| | | $vacation['subject'] = $tokens[++$i]; |
| | | } |
| | | else if ($tok == ':addresses') { |
| | | $vacation['addresses'] = $tokens[++$i]; |
| | | } |
| | | else if ($tok == ':handle') { |
| | | $vacation['handle'] = $tokens[++$i]; |
| | | } |
| | | else if ($tok == ':from') { |
| | | $vacation['from'] = $tokens[++$i]; |
| | | } |
| | | else if ($tok == ':mime') { |
| | | $vacation['mime'] = true; |
| | | } |
| | | } |
| | | |
| | | $result[] = $vacation; |
| | | break; |
| | | |
| | | case 'setflag': |
| | | case 'addflag': |
| | | case 'removeflag': |
| | | $result[] = array('type' => $token, |
| | | // Flags list: last token (skip optional variable) |
| | | 'target' => $tokens[count($tokens)-1] |
| | | ); |
| | | $result[] = array('type' => $token, 'target' => array_pop($tokens)); |
| | | break; |
| | | |
| | | case 'include': |
| | | $include = array('type' => 'include', 'target' => array_pop($tokens)); |
| | | $action = array('type' => 'include', 'target' => array_pop($tokens)); |
| | | $args = array('once', 'optional', 'global', 'personal'); |
| | | $action += $this->action_arguments($tokens, $args); |
| | | |
| | | // Parameters: :once, :optional, :global, :personal |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | $tok = strtolower($tokens[$i]); |
| | | if ($tok[0] == ':') { |
| | | $include[substr($tok, 1)] = true; |
| | | } |
| | | } |
| | | |
| | | $result[] = $include; |
| | | $result[] = $action; |
| | | break; |
| | | |
| | | case 'set': |
| | | $set = array('type' => 'set', 'value' => array_pop($tokens), 'name' => array_pop($tokens)); |
| | | $action = array('type' => 'set', 'value' => array_pop($tokens), 'name' => array_pop($tokens)); |
| | | $args = array('lower', 'upper', 'lowerfirst', 'upperfirst', 'quotewildcard', 'length'); |
| | | $action += $this->action_arguments($tokens, $args); |
| | | |
| | | // Parameters: :lower :upper :lowerfirst :upperfirst :quotewildcard :length |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | $tok = strtolower($tokens[$i]); |
| | | if ($tok[0] == ':') { |
| | | $set[substr($tok, 1)] = true; |
| | | } |
| | | } |
| | | |
| | | $result[] = $set; |
| | | $result[] = $action; |
| | | break; |
| | | |
| | | case 'require': |
| | | // skip, will be build according to used commands |
| | | // $result[] = array('type' => 'require', 'target' => $tokens); |
| | | // $result[] = array('type' => 'require', 'target' => array_pop($tokens)); |
| | | break; |
| | | |
| | | case 'notify': |
| | | $notify = array('type' => 'notify'); |
| | | $priorities = array(':high' => 1, ':normal' => 2, ':low' => 3); |
| | | $action = array('type' => 'notify'); |
| | | $priorities = array('high' => 1, 'normal' => 2, 'low' => 3); |
| | | $vargs = array('from', 'id', 'importance', 'options', 'message', 'method'); |
| | | $args = array_keys($priorities); |
| | | $action += $this->action_arguments($tokens, $args, $vargs); |
| | | |
| | | // Parameters: :from, :importance, :options, :message |
| | | // additional (optional) :method parameter for notify extension |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | $tok = strtolower($tokens[$i]); |
| | | if ($tok[0] == ':') { |
| | | // Here we support only 00 version of notify draft, there |
| | | // were a couple regressions in 00 to 04 changelog, we use |
| | | // the version used by Cyrus |
| | | if (isset($priorities[$tok])) { |
| | | $notify['importance'] = $priorities[$tok]; |
| | | // Here we'll convert draft-martin-sieve-notify-01 into RFC 5435 |
| | | if (!isset($action['importance'])) { |
| | | foreach ($priorities as $key => $val) { |
| | | if (isset($action[$key])) { |
| | | $action['importance'] = $val; |
| | | unset($action[$key]); |
| | | } |
| | | else { |
| | | $notify[substr($tok, 1)] = $tokens[++$i]; |
| | | } |
| | | } |
| | | else { |
| | | // unnamed parameter is a :method in enotify extension |
| | | $notify['method'] = $tokens[$i]; |
| | | } |
| | | } |
| | | |
| | | $method_components = parse_url($notify['method']); |
| | | if ($method_components['scheme'] == 'mailto') { |
| | | $notify['address'] = $method_components['path']; |
| | | $method_params = array(); |
| | | if (array_key_exists('query', $method_components)) { |
| | | parse_str($method_components['query'], $method_params); |
| | | } |
| | | $method_params = array_change_key_case($method_params, CASE_LOWER); |
| | | // magic_quotes_gpc and magic_quotes_sybase affect the output of parse_str |
| | | if (ini_get('magic_quotes_gpc') || ini_get('magic_quotes_sybase')) { |
| | | array_map('stripslashes', $method_params); |
| | | } |
| | | $notify['body'] = (array_key_exists('body', $method_params)) ? $method_params['body'] : ''; |
| | | $action['options'] = (array) $action['options']; |
| | | |
| | | // Old-draft way: :method "mailto" :options "email@address" |
| | | if (!empty($action['method']) && !empty($action['options'])) { |
| | | $action['method'] .= ':' . array_shift($action['options']); |
| | | } |
| | | // unnamed parameter is a :method in enotify extension |
| | | else if (!isset($action['method'])) { |
| | | $action['method'] = array_pop($tokens); |
| | | } |
| | | |
| | | $result[] = $notify; |
| | | $result[] = $action; |
| | | break; |
| | | |
| | | } |
| | | |
| | | if ($separator == $end) |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * Add comparator to the test |
| | | */ |
| | | private function add_comparator($test, &$out, &$exts) |
| | | { |
| | |
| | | if ($test['comparator'] != 'i;ascii-casemap') { |
| | | $out .= ' :comparator ' . self::escape_string($test['comparator']); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Add index argument to the test |
| | | */ |
| | | private function add_index($test, &$out, &$exts) |
| | | { |
| | | if (!empty($test['index'])) { |
| | | array_push($exts, 'index'); |
| | | $out .= ' :index ' . intval($test['index']) . ($test['last'] ? ' :last' : ''); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Add operators to the test |
| | | */ |
| | | private function add_operator($test, &$out, &$exts) |
| | | { |
| | | if (empty($test['type'])) { |
| | | return; |
| | | } |
| | | |
| | | // relational operator |
| | | if (preg_match('/^(value|count)-([gteqnl]{2})/', $test['type'], $m)) { |
| | | array_push($exts, 'relational'); |
| | | |
| | | $out .= ' :' . $m[1] . ' "' . $m[2] . '"'; |
| | | } |
| | | else { |
| | | if ($test['type'] == 'regex') { |
| | | array_push($exts, 'regex'); |
| | | } |
| | | |
| | | $out .= ' :' . $test['type']; |
| | | } |
| | | |
| | | $this->add_comparator($test, $out, $exts); |
| | | } |
| | | |
| | | /** |
| | | * Extract test tokens |
| | | */ |
| | | private function test_tokens(&$tokens) |
| | | { |
| | | $test = array(); |
| | | $result = array(); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { |
| | | $test['comparator'] = $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(count|value)$/i', $tokens[$i])) { |
| | | $test['type'] = strtolower(substr($tokens[$i], 1)) . '-' . $tokens[++$i]; |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { |
| | | $test['type'] = strtolower(substr($tokens[$i], 1)); |
| | | } |
| | | else if (!is_array($tokens[$i]) && preg_match('/^:index$/i', $tokens[$i])) { |
| | | $test['index'] = intval($tokens[++$i]); |
| | | if ($tokens[$i+1] && preg_match('/^:last$/i', $tokens[$i+1])) { |
| | | $test['last'] = true; |
| | | $i++; |
| | | } |
| | | } |
| | | else { |
| | | $result[] = $tokens[$i]; |
| | | } |
| | | } |
| | | |
| | | $tokens = $result; |
| | | |
| | | return $test; |
| | | } |
| | | |
| | | /** |
| | | * Extract action arguments |
| | | */ |
| | | private function action_arguments(&$tokens, $bool_args, $val_args = array()) |
| | | { |
| | | $action = array(); |
| | | $result = array(); |
| | | |
| | | for ($i=0, $len=count($tokens); $i<$len; $i++) { |
| | | $tok = $tokens[$i]; |
| | | if (!is_array($tok) && $tok[0] == ':') { |
| | | $tok = strtolower(substr($tok, 1)); |
| | | if (in_array($tok, $bool_args)) { |
| | | $action[$tok] = true; |
| | | } |
| | | else if (in_array($tok, $val_args)) { |
| | | $action[$tok] = $tokens[++$i]; |
| | | } |
| | | else { |
| | | $result[] = $tok; |
| | | } |
| | | } |
| | | else { |
| | | $result[] = $tok; |
| | | } |
| | | } |
| | | |
| | | $tokens = $result; |
| | | |
| | | return $action; |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * Splits script into string tokens |
| | | * |
| | | * @param string &$str The script |
| | | * @param mixed $num Number of tokens to return, 0 for all |
| | | * or True for all tokens until separator is found. |
| | | * Separator will be returned as last token. |
| | | * @param int $in_list Enable to call recursively inside a list |
| | | * @param string $str The script |
| | | * @param mixed $num Number of tokens to return, 0 for all |
| | | * or True for all tokens until separator is found. |
| | | * Separator will be returned as last token. |
| | | * @param int &$position Parsing start position |
| | | * |
| | | * @return mixed Tokens array or string if $num=1 |
| | | */ |
| | | static function tokenize(&$str, $num=0, $in_list=false) |
| | | static function tokenize($str, $num = 0, &$position = 0) |
| | | { |
| | | $result = array(); |
| | | $length = strlen($str); |
| | | |
| | | // remove spaces from the beginning of the string |
| | | while (($str = ltrim($str)) !== '' |
| | | && (!$num || $num === true || count($result) < $num) |
| | | ) { |
| | | switch ($str[0]) { |
| | | while ($position < $length && (!$num || $num === true || count($result) < $num)) { |
| | | // skip whitespace chars |
| | | $position = self::ltrim_position($str, $position); |
| | | |
| | | switch ($str[$position]) { |
| | | |
| | | // Quoted string |
| | | case '"': |
| | | $len = strlen($str); |
| | | |
| | | for ($pos=1; $pos<$len; $pos++) { |
| | | for ($pos = $position + 1; $pos < $length; $pos++) { |
| | | if ($str[$pos] == '"') { |
| | | break; |
| | | } |
| | |
| | | if ($str[$pos] != '"') { |
| | | // error |
| | | } |
| | | |
| | | // we need to strip slashes for a quoted string |
| | | $result[] = stripslashes(substr($str, 1, $pos - 1)); |
| | | $str = substr($str, $pos + 1); |
| | | $result[] = stripslashes(substr($str, $position + 1, $pos - $position - 1)); |
| | | $position = $pos + 1; |
| | | break; |
| | | |
| | | // Parenthesized list |
| | | case '[': |
| | | $str = substr($str, 1); |
| | | $result[] = self::tokenize($str, 0, true); |
| | | $position++; |
| | | $result[] = self::tokenize($str, 0, $position); |
| | | break; |
| | | case ']': |
| | | $str = substr($str, 1); |
| | | $position++; |
| | | return $result; |
| | | break; |
| | | |
| | |
| | | case ')': |
| | | case '{': |
| | | case '}': |
| | | $sep = $str[0]; |
| | | $str = substr($str, 1); |
| | | $sep = $str[$position]; |
| | | $position++; |
| | | if ($num === true) { |
| | | $result[] = $sep; |
| | | break 2; |
| | |
| | | |
| | | // bracket-comment |
| | | case '/': |
| | | if ($str[1] == '*') { |
| | | if ($end_pos = strpos($str, '*/')) { |
| | | $str = substr($str, $end_pos + 2); |
| | | if ($str[$position + 1] == '*') { |
| | | if ($end_pos = strpos($str, '*/', $position + 2)) { |
| | | $position = $end_pos + 2; |
| | | } |
| | | else { |
| | | // error |
| | | $str = ''; |
| | | $position = $length; |
| | | } |
| | | } |
| | | break; |
| | | |
| | | // hash-comment |
| | | case '#': |
| | | if ($lf_pos = strpos($str, "\n")) { |
| | | $str = substr($str, $lf_pos); |
| | | if ($lf_pos = strpos($str, "\n", $position)) { |
| | | $position = $lf_pos + 1; |
| | | break; |
| | | } |
| | | else { |
| | | $str = ''; |
| | | $position = $length; |
| | | } |
| | | |
| | | // String atom |
| | | default: |
| | | // empty or one character |
| | | if ($str === '' || $str === null) { |
| | | if ($position == $length) { |
| | | break 2; |
| | | } |
| | | if (strlen($str) < 2) { |
| | | $result[] = $str; |
| | | $str = ''; |
| | | if ($length - $position < 2) { |
| | | $result[] = substr($str, $position); |
| | | $position = $length; |
| | | break; |
| | | } |
| | | |
| | | // tag/identifier/number |
| | | if (preg_match('/^([a-z0-9:_]+)/i', $str, $m)) { |
| | | $str = substr($str, strlen($m[1])); |
| | | if (preg_match('/[a-zA-Z0-9:_]+/', $str, $m, PREG_OFFSET_CAPTURE, $position) |
| | | && $m[0][1] == $position |
| | | ) { |
| | | $atom = $m[0][0]; |
| | | $position += strlen($atom); |
| | | |
| | | if ($m[1] != 'text:') { |
| | | $result[] = $m[1]; |
| | | if ($atom != 'text:') { |
| | | $result[] = $atom; |
| | | } |
| | | // multiline string |
| | | else { |
| | | // skip whitespace chars (except \r\n) |
| | | $position = self::ltrim_position($str, $position, false); |
| | | |
| | | // possible hash-comment after "text:" |
| | | if (preg_match('/^( |\t)*(#[^\n]+)?\n/', $str, $m)) { |
| | | $str = substr($str, strlen($m[0])); |
| | | } |
| | | // get text until alone dot in a line |
| | | if (preg_match('/^(.*)\r?\n\.\r?\n/sU', $str, $m)) { |
| | | $text = $m[1]; |
| | | // remove dot-stuffing |
| | | $text = str_replace("\n..", "\n.", $text); |
| | | $str = substr($str, strlen($m[0])); |
| | | } |
| | | else { |
| | | $text = ''; |
| | | if ($str[$position] === '#') { |
| | | $endl = strpos($str, "\n", $position); |
| | | $position = $endl ?: $length; |
| | | } |
| | | |
| | | // skip \n or \r\n |
| | | if ($str[$position] == "\n") { |
| | | $position++; |
| | | } |
| | | else if ($str[$position] == "\r" && $str[$position] == "\n") { |
| | | $position += 2; |
| | | } |
| | | |
| | | $text = ''; |
| | | |
| | | // get text until alone dot in a line |
| | | while ($position < $length) { |
| | | $pos = strpos($str, "\n.", $position); |
| | | if ($pos === false) { |
| | | break; |
| | | } |
| | | |
| | | $text .= substr($str, $position, $pos - $position); |
| | | $position = $pos + 2; |
| | | |
| | | if ($str[$pos] == "\n" |
| | | || ($str[$pos] == "\r" && $str[$pos + 1] == "\n") |
| | | ) { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // remove dot-stuffing |
| | | $text = str_replace("\n..", "\n.", $text); |
| | | |
| | | $result[] = $text; |
| | | $position++; |
| | | } |
| | | } |
| | | // fallback, skip one character as infinite loop prevention |
| | | else { |
| | | $str = substr($str, 1); |
| | | $position++; |
| | | } |
| | | |
| | | break; |
| | |
| | | return $num === 1 ? (isset($result[0]) ? $result[0] : null) : $result; |
| | | } |
| | | |
| | | /** |
| | | * Skip whitespace characters in a string from specified position. |
| | | */ |
| | | static function ltrim_position($content, $position, $br = true) |
| | | { |
| | | $blanks = array("\t", "\0", "\x0B", " "); |
| | | |
| | | if ($br) { |
| | | $blanks[] = "\r"; |
| | | $blanks[] = "\n"; |
| | | } |
| | | |
| | | while (isset($content[$position]) && isset($content[$position + 1]) |
| | | && in_array($content[$position], $blanks, true) |
| | | ) { |
| | | $position++; |
| | | } |
| | | |
| | | return $position; |
| | | } |
| | | } |