Kyle Francis
2016-04-02 65225262f6bb524b93feb49178509cada3390ee3
plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php
@@ -63,7 +63,7 @@
        1 => 'notifyimportancehigh'
    );
    const VERSION  = '8.2';
    const VERSION  = '8.5';
    const PROGNAME = 'Roundcube (Managesieve)';
    const PORT     = 4190;
@@ -122,9 +122,6 @@
                case rcube_sieve::ERROR_CONNECTION:
                case rcube_sieve::ERROR_LOGIN:
                    $this->rc->output->show_message('managesieve.filterconnerror', 'error');
                    rcube::raise_error(array('code' => 403, 'type' => 'php',
                        'file' => __FILE__, 'line' => __LINE__,
                        'message' => "Unable to connect to managesieve on $host:$port"), true, false);
                    break;
                default:
@@ -210,7 +207,18 @@
            $plugin['socket_options']
        );
        return $this->sieve->error();
        $error = $this->sieve->error();
        if ($error) {
            rcube::raise_error(array(
                    'code'    => 403,
                    'file'    => __FILE__,
                    'line'    => __LINE__,
                    'message' => "Unable to connect to managesieve on $host:$port"
                ), true, false);
        }
        return $error;
    }
    /**
@@ -326,7 +334,7 @@
            else if ($action == 'act' && !$error) {
                if (isset($this->script[$fid])) {
                    $rule     = $this->script[$fid];
                    $disabled = $rule['disabled'] ? true : false;
                    $disabled = !empty($rule['disabled']);
                    $rule['disabled'] = !$disabled;
                    $result = $this->sieve->script->update_rule($fid, $rule);
@@ -389,11 +397,14 @@
                }
            }
            else if ($action == 'setget') {
                $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true);
                $script = $this->sieve->get_script($script_name);
                $this->rc->request_security_check(rcube_utils::INPUT_GET);
                if (PEAR::isError($script))
                $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true);
                $script      = $this->sieve->get_script($script_name);
                if (is_a($script, 'PEAR_Error')) {
                    exit;
                }
                $browser = new rcube_browser;
@@ -431,6 +442,11 @@
                $content = $this->action_div($fid, $id, false);
                $this->rc->output->command('managesieve_actionfill', $content, $id, $aid);
            }
            else if ($action == 'addresses') {
                $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST);
                $this->rc->output->command('managesieve_vacation_addresses_update', $aid, $this->user_emails());
            }
            $this->rc->output->send();
@@ -602,6 +618,7 @@
            $addresses      = rcube_utils::get_input_value('_action_addresses', rcube_utils::INPUT_POST, true);
            $intervals      = rcube_utils::get_input_value('_action_interval', rcube_utils::INPUT_POST);
            $interval_types = rcube_utils::get_input_value('_action_interval_type', rcube_utils::INPUT_POST);
            $from           = rcube_utils::get_input_value('_action_from', rcube_utils::INPUT_POST);
            $subject        = rcube_utils::get_input_value('_action_subject', rcube_utils::INPUT_POST, true);
            $flags          = rcube_utils::get_input_value('_action_flags', rcube_utils::INPUT_POST);
            $varnames       = rcube_utils::get_input_value('_action_varname', rcube_utils::INPUT_POST);
@@ -618,8 +635,8 @@
            foreach ($sizeitems as $item)
                $items[] = $item;
            $this->form['disabled'] = $_POST['_disabled'] ? true : false;
            $this->form['join']     = $join=='allof' ? true : false;
            $this->form['disabled'] = !empty($_POST['_disabled']);
            $this->form['join']     = $join == 'allof';
            $this->form['name']     = $name;
            $this->form['tests']    = array();
            $this->form['actions']  = array();
@@ -967,10 +984,11 @@
                    $interval_type = $interval_types[$idx] == 'seconds' ? 'seconds' : 'days';
                    $this->form['actions'][$i]['reason']    = str_replace("\r\n", "\n", $reason);
                    $this->form['actions'][$i]['from']      = $from[$idx];
                    $this->form['actions'][$i]['subject']   = $subject[$idx];
                    $this->form['actions'][$i]['addresses'] = array_shift($addresses);
                    $this->form['actions'][$i][$interval_type] = $intervals[$idx];
// @TODO: vacation :mime, :from, :handle
// @TODO: vacation :mime, :handle
                    foreach ((array)$this->form['actions'][$i]['addresses'] as $aidx => $address) {
                        $this->form['actions'][$i]['addresses'][$aidx] = $address = trim($address);
@@ -982,6 +1000,10 @@
                            $this->errors['actions'][$i]['addresses'] = $this->plugin->gettext('noemailwarning');
                            break;
                        }
                    }
                    if (!empty($this->form['actions'][$i]['from']) && !rcube_utils::check_email($this->form['actions'][$i]['from'])) {
                        $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning');
                    }
                    if ($this->form['actions'][$i]['reason'] == '')
@@ -1393,19 +1415,21 @@
        }
        if (isset($rule['test'])) {
            if (in_array($rule['test'], array('header', 'address', 'envelope'))
                && !is_array($rule['arg1'])
                && ($header = strtolower($rule['arg1']))
                && isset($this->headers[$header])
            ) {
                $test = $header;
            if (in_array($rule['test'], array('header', 'address', 'envelope'))) {
                if (is_array($rule['arg1']) && count($rule['arg1']) == 1) {
                    $rule['arg1'] = $rule['arg1'][0];
                }
                $matches = ($header = strtolower($rule['arg1'])) && isset($this->headers[$header]);
                $test    = $matches ? $header : '...';
            }
            else if ($rule['test'] == 'exists'
                && !is_array($rule['arg'])
                && ($header = strtolower($rule['arg']))
                && isset($this->headers[$header])
            ) {
                $test = $header;
            else if ($rule['test'] == 'exists') {
                if (is_array($rule['arg']) && count($rule['arg']) == 1) {
                    $rule['arg'] = $rule['arg'][0];
                }
                $matches = ($header = strtolower($rule['arg'])) && isset($this->headers[$header]);
                $test    = $matches ? $header : '...';
            }
            else if (in_array($rule['test'], array('size', 'body', 'date', 'currentdate'))) {
                $test = $rule['test'];
@@ -1590,7 +1614,7 @@
            $mout .= '<div id="rule_date_header_div' .$id. '" class="adv"'. ($rule['test'] != 'date' ? ' style="display:none"' : '') .'>';
            $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('dateheader')) . '</span>';
            $mout .= '<input type="text" name="_rule_date_header[]" id="rule_date_header'.$id
                . '" value="'. Q($rule['test'] == 'date' ? $rule['header'] : '')
                . '" value="'. rcube::Q($rule['test'] == 'date' ? $rule['header'] : '')
                . '" size="15"' . $this->error_class($id, 'test', 'dateheader', 'rule_date_header') .' />';
            $mout .= '</div>';
        }
@@ -1663,7 +1687,7 @@
            $test = $rule['type'];
        }
        else if (in_array($rule['test'], $set)) {
            $test = ($rule['not'] ? 'not' : '') . ($rule['type'] ? $rule['type'] : 'is');
            $test = ($rule['not'] ? 'not' : '') . ($rule['type'] ?: 'is');
        }
        else {
            $test = ($rule['not'] ? 'not' : '') . $rule['test'];
@@ -1758,22 +1782,31 @@
            . "</textarea>\n";
        // vacation
        $vsec = in_array('vacation-seconds', $this->exts);
        $vsec      = in_array('vacation-seconds', $this->exts);
        $auto_addr = $this->rc->config->get('managesieve_vacation_addresses_init');
        $addresses = isset($action['addresses']) || !$auto_addr ? (array) $action['addresses'] : $this->user_emails();
        $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">';
        $out .= '<span class="label">'. rcube::Q($this->plugin->gettext('vacationreason')) .'</span><br />'
            .'<textarea name="_action_reason['.$id.']" id="action_reason' .$id. '" '
            .'rows="3" cols="35" '. $this->error_class($id, 'action', 'reason', 'action_reason') . '>'
            . Q($action['reason'], 'strict', false) . "</textarea>\n";
            . rcube::Q($action['reason'], 'strict', false) . "</textarea>\n";
        $out .= '<br /><span class="label">' .rcube::Q($this->plugin->gettext('vacationsubject')) . '</span><br />'
            .'<input type="text" name="_action_subject['.$id.']" id="action_subject'.$id.'" '
            .'value="' . (is_array($action['subject']) ? rcube::Q(implode(', ', $action['subject']), 'strict', false) : $action['subject']) . '" size="35" '
            . $this->error_class($id, 'action', 'subject', 'action_subject') .' />';
        $out .= '<br /><span class="label">' .rcube::Q($this->plugin->gettext('vacationfrom')) . '</span><br />'
            .'<input type="text" name="_action_from['.$id.']" id="action_from'.$id.'" '
            .'value="' . $action['from'] . '" size="35" '
            . $this->error_class($id, 'action', 'from', 'action_from') .' />';
        $out .= '<br /><span class="label">' .rcube::Q($this->plugin->gettext('vacationaddr')) . '</span><br />'
            . $this->list_input($id, 'action_addresses', $action['addresses'], true,
                $this->error_class($id, 'action', 'addresses', 'action_addresses'), 30);
            . $this->list_input($id, 'action_addresses', $addresses, true,
                $this->error_class($id, 'action', 'addresses', 'action_addresses'), 30)
            . html::a(array('href' => '#', 'onclick' => rcmail_output::JS_OBJECT_NAME . ".managesieve_vacation_addresses($id)"),
                rcube::Q($this->plugin->gettext('filladdresses')));
        $out .= '<br /><span class="label">' . rcube::Q($this->plugin->gettext($vsec ? 'vacationinterval' : 'vacationdays')) . '</span><br />'
            .'<input type="text" name="_action_interval['.$id.']" id="action_interval'.$id.'" '
            .'value="' .rcube::Q(isset($action['seconds']) ? $action['seconds'] : $action['days'], 'strict', false) . '" size="2" '
            .'value="' .rcube::Q(rcube_sieve_vacation::vacation_interval($action), 'strict', false) . '" size="2" '
            . $this->error_class($id, 'action', 'interval', 'action_interval') .' />';
        if ($vsec) {
            $out .= '&nbsp;<label><input type="radio" name="_action_interval_type['.$id.']" value="days"'
@@ -2327,7 +2360,7 @@
            if (empty($filter['actions'])) {
                continue;
            }
            $fname = $filter['name'] ? $filter['name'] : "#$i";
            $fname = $filter['name'] ?: "#$i";
            $result[] = array(
                'id'    => $idx,
                'name'  => $fname,
@@ -2344,11 +2377,11 @@
     */
    protected function init_script()
    {
        $this->script = $this->sieve->script->as_array();
        if (!$this->script) {
        if (!$this->sieve->script) {
            return;
        }
        $this->script = $this->sieve->script->as_array();
        $headers    = array();
        $exceptions = array('date', 'currentdate', 'size', 'body');
@@ -2378,4 +2411,21 @@
        $this->headers += $headers;
    }
    /**
     * Get all e-mail addresses of the user
     */
    protected function user_emails()
    {
        $addresses = $this->rc->user->list_emails();
        foreach ($addresses as $idx => $email) {
            $addresses[$idx] = $email['email'];
        }
        $addresses = array_unique($addresses);
        sort($addresses);
        return $addresses;
    }
}