Aleksander Machniak
2014-07-16 f72815e1f9e5410a0f9eb66e4eb4fae12e59cfae
plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
@@ -32,7 +32,6 @@
            $this->vacation_rule();
            $this->vacation_post();
        }
        $this->plugin->add_label('vacation.saving');
        $this->rc->output->add_handlers(array(
            'vacationform' => array($this, 'vacation_form'),
@@ -88,6 +87,9 @@
            return;
        }
        $date_extension  = in_array('date', $this->exts);
        $regex_extension = in_array('regex', $this->exts);
        $status        = rcube_utils::get_input_value('vacation_status', rcube_utils::INPUT_POST);
        $subject       = rcube_utils::get_input_value('vacation_subject', rcube_utils::INPUT_POST, true);
        $reason        = rcube_utils::get_input_value('vacation_reason', rcube_utils::INPUT_POST, true);
@@ -128,27 +130,38 @@
            $error = 'managesieve.forbiddenchars';
        }
        foreach (array('date_from', 'date_to') as $var) {
            $date = $$var;
        // find and remove existing date/regex/true rules
        foreach ((array) $vacation_tests as $idx => $t) {
            if (($t['test'] == 'currentdate' && $t['part'] == 'date' && $t['type'] == $type)
                || ($t['test'] == 'header' && $t['type'] == 'regex' && $t['arg1'] == 'received')
                || ($t['test'] == 'true')
            ) {
                unset($vacation_tests[$idx]);
            }
        }
            if ($date && ($dt = rcube_utils::anytodatetime($date))) {
                $type = 'value-' . ($var == 'date_from' ? 'ge' : 'le');
                $test = array(
                    'test' => 'currentdate',
                    'part' => 'date',
                    'type' => $type,
                    'arg'  => $dt->format('Y-m-d'),
                );
        if ($date_extension) {
            foreach (array('date_from', 'date_to') as $var) {
                $date = $$var;
                if ($date && ($dt = rcube_utils::anytodatetime($date))) {
                    $type = 'value-' . ($var == 'date_from' ? 'ge' : 'le');
                    $test = array(
                        'test' => 'currentdate',
                        'part' => 'date',
                        'type' => $type,
                        'arg'  => $dt->format('Y-m-d'),
                    );
                // find existing date rule
                foreach ((array) $vacation_tests as $idx => $t) {
                    if ($t['test'] == 'currentdate' && $t['part'] == 'date' && $t['type'] == $type) {
                        $vacation_tests[$idx] = $test;
                        continue 2;
                    }
                    $vacation_tests[] = $test;
                }
                $vacation_tests[] = $test;
            }
        }
        else if ($regex_extension) {
            // Add date range rules if range specified
            if ($date_from && $date_to) {
                if ($tests = self::build_regexp_tests($date_from, $date_to, $error)) {
                    $vacation_tests = array_merge($vacation_tests, $tests);
                }
            }
        }
@@ -174,7 +187,7 @@
            $rule['name']       = $rule['name'] ?: $this->plugin->gettext('vacation');
            $rule['disabled']   = $status == 'off';
            $rule['tests']      = $vacation_tests;
            $rule['join']       = count($vacation_tests) > 1;
            $rule['join']       = $date_extension ? count($vacation_tests) > 1 : false;
            $rule['actions']    = array($vacation_action);
            if ($action && $action != 'keep') {
@@ -235,6 +248,7 @@
    {
        // check supported extensions
        $date_extension    = in_array('date', $this->exts);
        $regex_extension   = in_array('regex', $this->exts);
        $seconds_extension = in_array('vacation-seconds', $this->exts);
        // build FORM tag
@@ -287,16 +301,25 @@
            $interval_txt .= ' ' . $this->plugin->gettext('days');
        }
        if ($date_extension) {
        if ($date_extension || $regex_extension) {
            $date_from   = new html_inputfield(array('name' => 'vacation_datefrom', 'id' => 'vacation_datefrom', 'class' => 'datepicker', 'size' => 12));
            $date_to     = new html_inputfield(array('name' => 'vacation_dateto', 'class' => 'datepicker', 'size' => 12));
            $date_format = $this->rc->config->get('date_format', 'Y-m-d');
        }
        if ($date_extension) {
            foreach ((array) $this->vacation['tests'] as $test) {
                if ($test['test'] == 'currentdate' && $test['part'] == 'date') {
                    $date = $this->rc->format_date($test['arg'], $date_format, false);
                    $date_value[$test['type'] == 'value-ge' ? 'from' : 'to'] = $date;
                }
            }
        }
        else if ($regex_extension) {
            // Sieve 'date' extension not available, read start/end from RegEx based rules instead
            if ($date_tests = self::parse_regexp_tests($this->vacation['tests'])) {
                $date_value['from'] = $this->rc->format_date($date_tests['from'], $date_format, false);
                $date_value['to']   = $this->rc->format_date($date_tests['to'], $date_format, false);
            }
        }
@@ -335,7 +358,7 @@
        $table->add('title', html::label('vacation_reason', $this->plugin->gettext('vacation.body')));
        $table->add(null, $reason->show($this->vacation['reason']));
        if ($date_extension) {
        if ($date_extension || $regex_extension) {
            $table->add('title', html::label('vacation_datefrom', $this->plugin->gettext('vacation.dates')));
            $table->add(null,
                $this->plugin->gettext('vacation.from'). ' ' . $date_from->show($date_value['from'])
@@ -372,4 +395,68 @@
        return $out;
    }
    public static function build_regexp_tests($date_from, $date_to, &$error)
    {
        $tests    = array();
        $dt_from  = rcube_utils::anytodatetime($date_from);
        $dt_to    = rcube_utils::anytodatetime($date_to);
        $interval = $dt_from->diff($dt_to);
        if ($interval->invert || $interval->days > 365) {
            $error = 'managesieve.invaliddateformat';
            return;
        }
        $dt_i     = $dt_from;
        $interval = new DateInterval('P1D');
        $matchexp = '';
        while (!$dt_i->diff($dt_to)->invert) {
            $days     = (int) $dt_i->format('d');
            $matchexp .= $days < 10 ? "[ 0]$days" : $days;
            if ($days == $dt_i->format('t') || $dt_i->diff($dt_to)->days == 0) {
                $test = array(
                    'test' => 'header',
                    'type' => 'regex',
                    'arg1' => 'received',
                    'arg2' => '('.$matchexp.') '.$dt_i->format('M Y')
                );
                $tests[]  = $test;
                $matchexp = '';
            }
            else {
                $matchexp .= '|';
            }
            $dt_i->add($interval);
        }
        return $tests;
    }
    public static function parse_regexp_tests($tests)
    {
        $rx_from = '/^\(([0-9]{2}).*\)\s([A-Za-z]+)\s([0-9]{4})/';
        $rx_to   = '/^\(.*([0-9]{2})\)\s([A-Za-z]+)\s([0-9]{4})/';
        $result  = array();
        foreach ((array) $tests as $test) {
            if ($test['test'] == 'header' && $test['type'] == 'regex' && $test['arg1'] == 'received') {
                $textexp = preg_replace('/\[ ([^\]]*)\]/', '0', $test['arg2']);
                if (!$result['from'] && preg_match($rx_from, $textexp, $matches)) {
                    $result['from'] = $matches[1]." ".$matches[2]." ".$matches[3];
                }
                if (preg_match($rx_to, $textexp, $matches)) {
                    $result['to'] = $matches[1]." ".$matches[2]." ".$matches[3];
                }
            }
        }
        return $result;
    }
}