| | |
| | | |
| | | // Managesieve Protocol: RFC5804 |
| | | |
| | | define('SIEVE_ERROR_CONNECTION', 1); |
| | | define('SIEVE_ERROR_LOGIN', 2); |
| | | define('SIEVE_ERROR_NOT_EXISTS', 3); // script not exists |
| | | define('SIEVE_ERROR_INSTALL', 4); // script installation |
| | | define('SIEVE_ERROR_ACTIVATE', 5); // script activation |
| | | define('SIEVE_ERROR_DELETE', 6); // script deletion |
| | | define('SIEVE_ERROR_INTERNAL', 7); // internal error |
| | | define('SIEVE_ERROR_DEACTIVATE', 8); // script activation |
| | | define('SIEVE_ERROR_OTHER', 255); // other/unknown error |
| | | |
| | | |
| | | class rcube_sieve |
| | | { |
| | | private $sieve; // Net_Sieve object |
| | |
| | | public $script; // rcube_sieve_script object |
| | | public $current; // name of currently loaded script |
| | | private $exts; // array of supported extensions |
| | | |
| | | const ERROR_CONNECTION = 1; |
| | | const ERROR_LOGIN = 2; |
| | | const ERROR_NOT_EXISTS = 3; // script not exists |
| | | const ERROR_INSTALL = 4; // script installation |
| | | const ERROR_ACTIVATE = 5; // script activation |
| | | const ERROR_DELETE = 6; // script deletion |
| | | const ERROR_INTERNAL = 7; // internal error |
| | | const ERROR_DEACTIVATE = 8; // script activation |
| | | const ERROR_OTHER = 255; // other/unknown error |
| | | |
| | | |
| | | /** |
| | |
| | | $this->sieve->setDebug(true, array($this, 'debug_handler')); |
| | | } |
| | | |
| | | if (PEAR::isError($this->sieve->connect($host, $port, $options, $usetls))) { |
| | | return $this->_set_error(SIEVE_ERROR_CONNECTION); |
| | | $result = $this->sieve->connect($host, $port, $options, $usetls); |
| | | |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_CONNECTION); |
| | | } |
| | | |
| | | if (!empty($auth_cid)) { |
| | | $authz = $username; |
| | | $username = $auth_cid; |
| | | } |
| | | if (!empty($auth_pw)) { |
| | | $password = $auth_pw; |
| | | } |
| | | |
| | | if (PEAR::isError($this->sieve->login($username, $password, |
| | | $auth_type ? strtoupper($auth_type) : null, $authz)) |
| | | ) { |
| | | return $this->_set_error(SIEVE_ERROR_LOGIN); |
| | | $result = $this->sieve->login($username, $password, $auth_type ? strtoupper($auth_type) : null, $authz); |
| | | |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_LOGIN); |
| | | } |
| | | |
| | | $this->exts = $this->get_extensions(); |
| | |
| | | */ |
| | | public function error() |
| | | { |
| | | return $this->error ? $this->error : false; |
| | | return $this->error ?: false; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public function save($name = null) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | if (!$this->sieve) { |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | } |
| | | |
| | | if (!$this->script) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | if (!$this->script) { |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | } |
| | | |
| | | if (!$name) |
| | | if (!$name) { |
| | | $name = $this->current; |
| | | } |
| | | |
| | | $script = $this->script->as_text(); |
| | | |
| | | if (!$script) |
| | | if (!$script) { |
| | | $script = '/* empty script */'; |
| | | } |
| | | |
| | | if (PEAR::isError($this->sieve->installScript($name, $script))) |
| | | return $this->_set_error(SIEVE_ERROR_INSTALL); |
| | | $result = $this->sieve->installScript($name, $script); |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_INSTALL); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | |
| | | */ |
| | | public function save_script($name, $content = null) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | if (!$this->sieve) { |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | } |
| | | |
| | | if (!$content) |
| | | if (!$content) { |
| | | $content = '/* empty script */'; |
| | | } |
| | | |
| | | if (PEAR::isError($this->sieve->installScript($name, $content))) |
| | | return $this->_set_error(SIEVE_ERROR_INSTALL); |
| | | $result = $this->sieve->installScript($name, $content); |
| | | |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_INSTALL); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | |
| | | */ |
| | | public function activate($name = null) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | if (!$this->sieve) { |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | } |
| | | |
| | | if (!$name) |
| | | if (!$name) { |
| | | $name = $this->current; |
| | | } |
| | | |
| | | if (PEAR::isError($this->sieve->setActive($name))) |
| | | return $this->_set_error(SIEVE_ERROR_ACTIVATE); |
| | | $result = $this->sieve->setActive($name); |
| | | |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_ACTIVATE); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | |
| | | */ |
| | | public function deactivate() |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | if (!$this->sieve) { |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | } |
| | | |
| | | if (PEAR::isError($this->sieve->setActive(''))) |
| | | return $this->_set_error(SIEVE_ERROR_DEACTIVATE); |
| | | $result = $this->sieve->setActive(''); |
| | | |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_DEACTIVATE); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | |
| | | */ |
| | | public function remove($name = null) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | if (!$this->sieve) { |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | } |
| | | |
| | | if (!$name) |
| | | if (!$name) { |
| | | $name = $this->current; |
| | | } |
| | | |
| | | // script must be deactivated first |
| | | if ($name == $this->sieve->getActive()) |
| | | if (PEAR::isError($this->sieve->setActive(''))) |
| | | return $this->_set_error(SIEVE_ERROR_DELETE); |
| | | if ($name == $this->sieve->getActive()) { |
| | | $result = $this->sieve->setActive(''); |
| | | |
| | | if (PEAR::isError($this->sieve->removeScript($name))) |
| | | return $this->_set_error(SIEVE_ERROR_DELETE); |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_DELETE); |
| | | } |
| | | } |
| | | |
| | | if ($name == $this->current) |
| | | $result = $this->sieve->removeScript($name); |
| | | |
| | | if (is_a($result, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_DELETE); |
| | | } |
| | | |
| | | if ($name == $this->current) { |
| | | $this->current = null; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | |
| | | return $this->exts; |
| | | |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | $ext = $this->sieve->getExtensions(); |
| | | |
| | | if (is_a($ext, 'PEAR_Error')) { |
| | | return array(); |
| | | } |
| | | |
| | | // we're working on lower-cased names |
| | | $ext = array_map('strtolower', (array) $ext); |
| | | |
| | |
| | | if (!$this->list) { |
| | | |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | $list = $this->sieve->listScripts(); |
| | | |
| | | if (PEAR::isError($list)) |
| | | return $this->_set_error(SIEVE_ERROR_OTHER); |
| | | if (is_a($list, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_OTHER); |
| | | } |
| | | |
| | | $this->list = $list; |
| | | } |
| | |
| | | public function get_active() |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | return $this->sieve->getActive(); |
| | | } |
| | |
| | | public function load($name) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | if ($this->current == $name) |
| | | return true; |
| | | |
| | | $script = $this->sieve->getScript($name); |
| | | |
| | | if (PEAR::isError($script)) |
| | | return $this->_set_error(SIEVE_ERROR_OTHER); |
| | | if (is_a($script, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_OTHER); |
| | | } |
| | | |
| | | // try to parse from Roundcube format |
| | | $this->script = $this->_parse($script); |
| | |
| | | public function load_script($script) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | // try to parse from Roundcube format |
| | | $this->script = $this->_parse($script); |
| | |
| | | public function get_script($name) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | $content = $this->sieve->getScript($name); |
| | | |
| | | if (PEAR::isError($content)) |
| | | return $this->_set_error(SIEVE_ERROR_OTHER); |
| | | if (is_a($content, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_OTHER); |
| | | } |
| | | |
| | | return $content; |
| | | } |
| | |
| | | public function copy($name, $copy) |
| | | { |
| | | if (!$this->sieve) |
| | | return $this->_set_error(SIEVE_ERROR_INTERNAL); |
| | | return $this->_set_error(self::ERROR_INTERNAL); |
| | | |
| | | if ($copy) { |
| | | $content = $this->sieve->getScript($copy); |
| | | |
| | | if (PEAR::isError($content)) |
| | | return $this->_set_error(SIEVE_ERROR_OTHER); |
| | | if (is_a($content, 'PEAR_Error')) { |
| | | return $this->_set_error(self::ERROR_OTHER); |
| | | } |
| | | } |
| | | |
| | | |
| | | return $this->save_script($name, $content); |
| | | } |
| | | |