Aleksander Machniak
2014-08-26 613f96839cbf63d475a4f56cb1841647bb23ad0c
plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php
@@ -73,7 +73,7 @@
     */
    function __construct($plugin)
    {
        $this->rc     = rcmail::get_instance();
        $this->rc     = rcube::get_instance();
        $this->plugin = $plugin;
    }
@@ -91,56 +91,11 @@
            'filtersetform'  => array($this, 'filterset_form'),
        ));
        // Get connection parameters
        $host = $this->rc->config->get('managesieve_host', 'localhost');
        $port = $this->rc->config->get('managesieve_port');
        $tls  = $this->rc->config->get('managesieve_usetls', false);
        // connect to managesieve server
        $error = $this->connect($_SESSION['username'], $this->rc->decrypt($_SESSION['password']));
        $host = rcube_utils::parse_host($host);
        $host = rcube_utils::idn_to_ascii($host);
        // remove tls:// prefix, set TLS flag
        if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) {
            $tls = true;
        }
        if (empty($port)) {
            $port = getservbyname('sieve', 'tcp');
            if (empty($port)) {
                $port = self::PORT;
            }
        }
        $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array(
            'user'      => $_SESSION['username'],
            'password'  => $this->rc->decrypt($_SESSION['password']),
            'host'      => $host,
            'port'      => $port,
            'usetls'    => $tls,
            'auth_type' => $this->rc->config->get('managesieve_auth_type'),
            'disabled'  => $this->rc->config->get('managesieve_disabled_extensions'),
            'debug'     => $this->rc->config->get('managesieve_debug', false),
            'auth_cid'  => $this->rc->config->get('managesieve_auth_cid'),
            'auth_pw'   => $this->rc->config->get('managesieve_auth_pw'),
            'socket_options' => $this->rc->config->get('managesieve_conn_options'),
        ));
        // try to connect to managesieve server and to fetch the script
        $this->sieve = new rcube_sieve(
            $plugin['user'],
            $plugin['password'],
            $plugin['host'],
            $plugin['port'],
            $plugin['auth_type'],
            $plugin['usetls'],
            $plugin['disabled'],
            $plugin['debug'],
            $plugin['auth_cid'],
            $plugin['auth_pw'],
            $plugin['socket_options']
        );
        if (!($error = $this->sieve->error())) {
        // load current/active script
        if (!$error) {
            // Get list of scripts
            $list = $this->list_scripts();
@@ -158,46 +113,14 @@
                }
            }
            if ($script_name === null || $script_name === '') {
                // get (first) active script
                if (!empty($this->active[0])) {
                    $script_name = $this->active[0];
                }
                else if ($list) {
                    $script_name = $list[0];
                }
                // create a new (initial) script
                else {
                    // if script not exists build default script contents
                    $script_file = $this->rc->config->get('managesieve_default');
                    $script_name = $this->rc->config->get('managesieve_script_name');
                    if (empty($script_name))
                        $script_name = 'roundcube';
                    if ($script_file && is_readable($script_file))
                        $content = file_get_contents($script_file);
                    // add script and set it active
                    if ($this->sieve->save_script($script_name, $content)) {
                        $this->activate_script($script_name);
                        $this->list[] = $script_name;
                    }
                }
            }
            if ($script_name) {
                $this->sieve->load($script_name);
            }
            $error = $this->sieve->error();
            $error = $this->load_script($script_name);
        }
        // finally set script objects
        if ($error) {
            switch ($error) {
                case SIEVE_ERROR_CONNECTION:
                case SIEVE_ERROR_LOGIN:
                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__,
@@ -228,6 +151,120 @@
        return $error;
    }
    /**
     * Connect to configured managesieve server
     *
     * @param string $username User login
     * @param string $password User password
     *
     * @return int Connection status: 0 on success, >0 on failure
     */
    public function connect($username, $password)
    {
        // Get connection parameters
        $host = $this->rc->config->get('managesieve_host', 'localhost');
        $port = $this->rc->config->get('managesieve_port');
        $tls  = $this->rc->config->get('managesieve_usetls', false);
        $host = rcube_utils::parse_host($host);
        $host = rcube_utils::idn_to_ascii($host);
        // remove tls:// prefix, set TLS flag
        if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) {
            $tls = true;
        }
        if (empty($port)) {
            $port = getservbyname('sieve', 'tcp');
            if (empty($port)) {
                $port = self::PORT;
            }
        }
        $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array(
            'user'      => $username,
            'password'  => $password,
            'host'      => $host,
            'port'      => $port,
            'usetls'    => $tls,
            'auth_type' => $this->rc->config->get('managesieve_auth_type'),
            'disabled'  => $this->rc->config->get('managesieve_disabled_extensions'),
            'debug'     => $this->rc->config->get('managesieve_debug', false),
            'auth_cid'  => $this->rc->config->get('managesieve_auth_cid'),
            'auth_pw'   => $this->rc->config->get('managesieve_auth_pw'),
            'socket_options' => $this->rc->config->get('managesieve_conn_options'),
        ));
        // try to connect to managesieve server and to fetch the script
        $this->sieve = new rcube_sieve(
            $plugin['user'],
            $plugin['password'],
            $plugin['host'],
            $plugin['port'],
            $plugin['auth_type'],
            $plugin['usetls'],
            $plugin['disabled'],
            $plugin['debug'],
            $plugin['auth_cid'],
            $plugin['auth_pw'],
            $plugin['socket_options']
        );
        return $this->sieve->error();
    }
    /**
     * Load specified (or active) script
     *
     * @param string $script_name Optional script name
     *
     * @return int Connection status: 0 on success, >0 on failure
     */
    public function load_script($script_name = null)
    {
        // Get list of scripts
        $list = $this->list_scripts();
        if ($script_name === null || $script_name === '') {
            // get (first) active script
            if (!empty($this->active[0])) {
               $script_name = $this->active[0];
            }
            else if ($list) {
                $script_name = $list[0];
            }
            // create a new (initial) script
            else {
                // if script not exists build default script contents
                $script_file = $this->rc->config->get('managesieve_default');
                $script_name = $this->rc->config->get('managesieve_script_name');
                if (empty($script_name)) {
                    $script_name = 'roundcube';
                }
                if ($script_file && is_readable($script_file)) {
                    $content = file_get_contents($script_file);
                }
                // add script and set it active
                if ($this->sieve->save_script($script_name, $content)) {
                    $this->activate_script($script_name);
                    $this->list[] = $script_name;
                }
            }
        }
        if ($script_name) {
            $this->sieve->load($script_name);
        }
        return $this->sieve->error();
    }
    /**
     * User interface actions handler
     */
    function actions()
    {
        $error = $this->start();