Aleksander Machniak
2014-08-26 613f96839cbf63d475a4f56cb1841647bb23ad0c
plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
@@ -23,6 +23,8 @@
class rcube_sieve_vacation extends rcube_sieve_engine
{
    protected $error;
    function actions()
    {
        $error = $this->start('vacation');
@@ -90,6 +92,14 @@
        $date_extension  = in_array('date', $this->exts);
        $regex_extension = in_array('regex', $this->exts);
        // set user's timezone
        try {
            $timezone = new DateTimeZone($this->rc->config->get('timezone', 'GMT'));
        }
        catch (Exception $e) {
            $timezone = new DateTimeZone('GMT');
        }
        $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);
@@ -98,6 +108,8 @@
        $interval_type = rcube_utils::get_input_value('vacation_interval_type', rcube_utils::INPUT_POST);
        $date_from     = rcube_utils::get_input_value('vacation_datefrom', rcube_utils::INPUT_POST);
        $date_to       = rcube_utils::get_input_value('vacation_dateto', rcube_utils::INPUT_POST);
        $time_from     = rcube_utils::get_input_value('vacation_timefrom', rcube_utils::INPUT_POST);
        $time_to       = rcube_utils::get_input_value('vacation_timeto', rcube_utils::INPUT_POST);
        $after         = rcube_utils::get_input_value('vacation_after', rcube_utils::INPUT_POST);
        $action        = rcube_utils::get_input_value('vacation_action', rcube_utils::INPUT_POST);
        $target        = rcube_utils::get_input_value('action_target', rcube_utils::INPUT_POST, true);
@@ -133,9 +145,8 @@
        // 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)
            if ($t['test'] == 'currentdate' || $t['test'] == 'true'
                || ($t['test'] == 'header' && $t['type'] == 'regex' && $t['arg1'] == 'received')
                || ($t['test'] == 'true')
            ) {
                unset($vacation_tests[$idx]);
            }
@@ -143,17 +154,28 @@
        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'),
                    );
                $time = ${str_replace('date', 'time', $var)};
                $date = trim($$var . ' ' . $time);
                    $vacation_tests[] = $test;
                if ($date && ($dt = rcube_utils::anytodatetime($date, $timezone))) {
                    if ($time) {
                        $vacation_tests[] = array(
                            'test' => 'currentdate',
                            'part' => 'iso8601',
                            'type' => 'value-' . ($var == 'date_from' ? 'ge' : 'le'),
                            'zone' => $dt->format('O'),
                            'arg'  => str_replace('+00:00', 'Z', strtoupper($dt->format('c'))),
                        );
                    }
                    else {
                        $vacation_tests[] = array(
                            'test' => 'currentdate',
                            'part' => 'date',
                            'type' => 'value-' . ($var == 'date_from' ? 'ge' : 'le'),
                            'zone' => $dt->format('O'),
                            'arg'  => $dt->format('Y-m-d'),
                        );
                    }
                }
            }
        }
@@ -304,15 +326,35 @@
        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_to     = new html_inputfield(array('name' => 'vacation_dateto', 'id' => 'vacation_dateto', 'class' => 'datepicker', 'size' => 12));
            $date_format = $this->rc->config->get('date_format', 'Y-m-d');
        }
        if ($date_extension) {
            $time_from   = new html_inputfield(array('name' => 'vacation_timefrom', 'id' => 'vacation_timefrom', 'size' => 6));
            $time_to     = new html_inputfield(array('name' => 'vacation_timeto', 'id' => 'vacation_timeto', 'size' => 6));
            $time_format = $this->rc->config->get('time_format', 'H:i');
            $date_value  = array();
            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;
                if ($test['test'] == 'currentdate') {
                    $idx = $test['type'] == 'value-ge' ? 'from' : 'to';
                    if ($test['part'] == 'date') {
                        $date_value[$idx]['date'] = $test['arg'];
                    }
                    else if ($test['part'] == 'iso8601') {
                        $date_value[$idx]['datetime'] = $test['arg'];
                    }
                }
            }
            foreach ($date_value as $idx => $value) {
                $date = $value['datetime'] ?: $value['date'];
                $date_value[$idx] = $this->rc->format_date($date, $date_format, false);
                if (!empty($value['datetime'])) {
                    $date_value['time_' . $idx] = $this->rc->format_date($date, $time_format, true);
                }
            }
        }
@@ -360,11 +402,10 @@
        $table->add(null, $reason->show($this->vacation['reason']));
        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'])
                . ' ' . $this->plugin->gettext('vacation.to'). ' ' . $date_to->show($date_value['to'])
            );
            $table->add('title', html::label('vacation_datefrom', $this->plugin->gettext('vacation.start')));
            $table->add(null, $date_from->show($date_value['from']) . ($time_from ? ' ' . $time_from->show($date_value['time_from']) : ''));
            $table->add('title', html::label('vacation_dateto', $this->plugin->gettext('vacation.end')));
            $table->add(null, $date_to->show($date_value['to']) . ($time_to ? ' ' . $time_to->show($date_value['time_to']) : ''));
        }
        $table->add('title', html::label('vacation_status', $this->plugin->gettext('vacation.status')));
@@ -393,6 +434,10 @@
        $out .= '</form>';
        $this->rc->output->add_gui_object('sieveform', $form_id);
        if ($time_format) {
            $this->rc->output->set_env('time_format', $time_format);
        }
        return $out;
    }
@@ -460,4 +505,232 @@
        return $result;
    }
    /**
     * API: get vacation rule
     *
     * @return array Vacation rule information
     */
    public function get_vacation()
    {
        $this->exts = $this->sieve->get_extensions();
        $this->init_script();
        $this->vacation_rule();
        // 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);
        // set user's timezone
        try {
            $timezone = new DateTimeZone($this->rc->config->get('timezone', 'GMT'));
        }
        catch (Exception $e) {
            $timezone = new DateTimeZone('GMT');
        }
        if ($date_extension) {
            $date_value = array();
            foreach ((array) $this->vacation['tests'] as $test) {
                if ($test['test'] == 'currentdate') {
                    $idx = $test['type'] == 'value-ge' ? 'start' : 'end';
                    if ($test['part'] == 'date') {
                        $date_value[$idx]['date'] = $test['arg'];
                    }
                    else if ($test['part'] == 'iso8601') {
                        $date_value[$idx]['datetime'] = $test['arg'];
                    }
                }
            }
            foreach ($date_value as $idx => $value) {
                $$idx = new DateTime($value['datetime'] ?: $value['date'], $timezone);
            }
        }
        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'])) {
                $from = new DateTime($date_tests['from'] . ' ' . '00:00:00', $timezone);
                $to   = new DateTime($date_tests['to'] . ' ' . '23:59:59', $timezone);
            }
        }
        if (isset($this->vacation['seconds'])) {
            $interval = $this->vacation['seconds'] . 's';
        }
        else if (isset($this->vacation['days'])) {
            $interval = $this->vacation['days'] . 'd';
        }
        $vacation = array(
            'supported' => $this->exts,
            'interval'  => $interval,
            'start'     => $start,
            'end'       => $end,
            'enabled'   => $this->vacation['reason'] && empty($this->vacation['disabled']),
            'message'   => $this->vacation['reason'],
            'subject'   => $this->vacation['subject'],
            'action'    => $this->vacation['action'],
            'target'    => $this->vacation['target'],
            'addresses' => $this->vacation['addresses'],
        );
        return $vacation;
    }
    /**
     * API: set vacation rule
     *
     * @param array $vacation Vacation rule information (see self::get_vacation())
     *
     * @return bool True on success, False on failure
     */
    public function set_vacation($data)
    {
        $this->exts  = $this->sieve->get_extensions();
        $this->error = false;
        $this->init_script();
        $this->vacation_rule();
        // 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);
        $vacation['type']      = 'vacation';
        $vacation['reason']    = $this->strip_value(str_replace("\r\n", "\n", $data['message']));
        $vacation['addresses'] = $data['addresses'];
        $vacation['subject']   = $data['subject'];
        $vacation_tests        = (array) $this->vacation['tests'];
        foreach ((array) $vacation['addresses'] as $aidx => $address) {
            $vacation['addresses'][$aidx] = $address = trim($address);
            if (empty($address)) {
                unset($vacation['addresses'][$aidx]);
            }
            else if (!rcube_utils::check_email($address)) {
                $this->error = "Invalid address in vacation addresses: $address";
                return false;
            }
        }
        if ($vacation['reason'] == '') {
            $this->error = "No vacation message specified";
            return false;
        }
        if ($data['interval']) {
            if (!preg_match('/^([0-9]+)\s*([sd])$/', $data['interval'], $m)) {
                $this->error = "Invalid vacation interval value: " . $data['interval'];
                return false;
            }
            else if ($m[1]) {
                $vacation[strtolower($m[2]) == 's' ? 'seconds' : 'days'] = $m[1];
            }
        }
        // find and remove existing date/regex/true rules
        foreach ((array) $vacation_tests as $idx => $t) {
            if ($t['test'] == 'currentdate' || $t['test'] == 'true'
                || ($t['test'] == 'header' && $t['type'] == 'regex' && $t['arg1'] == 'received')
            ) {
                unset($vacation_tests[$idx]);
            }
        }
        if ($date_extension) {
            foreach (array('start', 'end') as $var) {
                if ($dt = $data[$var]) {
                    $vacation_tests[] = array(
                        'test' => 'currentdate',
                        'part' => 'iso8601',
                        'type' => 'value-' . ($var == 'start' ? 'ge' : 'le'),
                        'zone' => $dt->format('O'),
                        'arg'  => str_replace('+00:00', 'Z', strtoupper($dt->format('c'))),
                    );
                }
            }
        }
        else if ($regex_extension) {
            // Add date range rules if range specified
            if ($data['start'] && $data['end']) {
                if ($tests = self::build_regexp_tests($data['start'], $data['end'], $error)) {
                    $vacation_tests = array_merge($vacation_tests, $tests);
                }
                if ($error) {
                    $this->error = "Invalid dates specified or unsupported period length";
                    return false;
                }
            }
        }
        if ($data['action'] == 'redirect' || $data['action'] == 'copy') {
            if (empty($data['target']) || !rcube_utils::check_email($data['target'])) {
                $this->error = "Invalid address in action taget: " . $data['target'];
                return false;
            }
        }
        else if ($data['action'] && $data['action'] != 'keep' && $data['action'] != 'discard') {
            $this->error = "Unsupported vacation action: " . $data['action'];
            return false;
        }
        if (empty($vacation_tests)) {
            $vacation_tests = $this->rc->config->get('managesieve_vacation_test', array(array('test' => 'true')));
        }
        // @TODO: handle situation when there's no active script
        $rule             = $this->vacation;
        $rule['type']     = 'if';
        $rule['name']     = $rule['name'] ?: 'Out-of-Office';
        $rule['disabled'] = isset($data['enabled']) && !$data['enabled'];
        $rule['tests']    = $vacation_tests;
        $rule['join']     = $date_extension ? count($vacation_tests) > 1 : false;
        $rule['actions']  = array($vacation);
        if ($data['action'] && $data['action'] != 'keep') {
            $rule['actions'][] = array(
                'type'   => $data['action'] == 'discard' ? 'discard' : 'redirect',
                'copy'   => $data['action'] == 'copy',
                'target' => $data['action'] != 'discard' ? $data['target'] : '',
            );
        }
        // reset original vacation rule
        if (isset($this->vacation['idx'])) {
            $this->script[$this->vacation['idx']] = null;
        }
        array_unshift($this->script, $rule);
        $this->sieve->script->content = array_values(array_filter($this->script));
        return $this->save_script();
    }
    /**
     * API: connect to managesieve server
     */
    public function connect($username, $password)
    {
        if (!parent::connect($username, $password)) {
            return $this->load_script();
        }
    }
    /**
     * API: Returns last error
     *
     * @return string Error message
     */
    public function get_error()
    {
        return $this->error;
    }
}