You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_sieve.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * Classes for managesieve operations (using PEAR::Net_Sieve)
  4. *
  5. * Copyright (C) 2008-2011, The Roundcube Dev Team
  6. * Copyright (C) 2011, Kolab Systems AG
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see http://www.gnu.org/licenses/.
  20. */
  21. // Managesieve Protocol: RFC5804
  22. class rcube_sieve
  23. {
  24. private $sieve; // Net_Sieve object
  25. private $error = false; // error flag
  26. private $list = array(); // scripts list
  27. public $script; // rcube_sieve_script object
  28. public $current; // name of currently loaded script
  29. private $exts; // array of supported extensions
  30. const ERROR_CONNECTION = 1;
  31. const ERROR_LOGIN = 2;
  32. const ERROR_NOT_EXISTS = 3; // script not exists
  33. const ERROR_INSTALL = 4; // script installation
  34. const ERROR_ACTIVATE = 5; // script activation
  35. const ERROR_DELETE = 6; // script deletion
  36. const ERROR_INTERNAL = 7; // internal error
  37. const ERROR_DEACTIVATE = 8; // script activation
  38. const ERROR_OTHER = 255; // other/unknown error
  39. /**
  40. * Object constructor
  41. *
  42. * @param string Username (for managesieve login)
  43. * @param string Password (for managesieve login)
  44. * @param string Managesieve server hostname/address
  45. * @param string Managesieve server port number
  46. * @param string Managesieve authentication method
  47. * @param boolean Enable/disable TLS use
  48. * @param array Disabled extensions
  49. * @param boolean Enable/disable debugging
  50. * @param string Proxy authentication identifier
  51. * @param string Proxy authentication password
  52. * @param array List of options to pass to stream_context_create().
  53. */
  54. public function __construct($username, $password='', $host='localhost', $port=2000,
  55. $auth_type=null, $usetls=true, $disabled=array(), $debug=false,
  56. $auth_cid=null, $auth_pw=null, $options=array())
  57. {
  58. $this->sieve = new Net_Sieve();
  59. if ($debug) {
  60. $this->sieve->setDebug(true, array($this, 'debug_handler'));
  61. }
  62. $result = $this->sieve->connect($host, $port, $options, $usetls);
  63. if (is_a($result, 'PEAR_Error')) {
  64. return $this->_set_error(self::ERROR_CONNECTION);
  65. }
  66. if (!empty($auth_cid)) {
  67. $authz = $username;
  68. $username = $auth_cid;
  69. }
  70. if (!empty($auth_pw)) {
  71. $password = $auth_pw;
  72. }
  73. $result = $this->sieve->login($username, $password, $auth_type ? strtoupper($auth_type) : null, $authz);
  74. if (is_a($result, 'PEAR_Error')) {
  75. return $this->_set_error(self::ERROR_LOGIN);
  76. }
  77. $this->exts = $this->get_extensions();
  78. // disable features by config
  79. if (!empty($disabled)) {
  80. // we're working on lower-cased names
  81. $disabled = array_map('strtolower', (array) $disabled);
  82. foreach ($disabled as $ext) {
  83. if (($idx = array_search($ext, $this->exts)) !== false) {
  84. unset($this->exts[$idx]);
  85. }
  86. }
  87. }
  88. }
  89. public function __destruct() {
  90. $this->sieve->disconnect();
  91. }
  92. /**
  93. * Getter for error code
  94. */
  95. public function error()
  96. {
  97. return $this->error ?: false;
  98. }
  99. /**
  100. * Saves current script into server
  101. */
  102. public function save($name = null)
  103. {
  104. if (!$this->sieve) {
  105. return $this->_set_error(self::ERROR_INTERNAL);
  106. }
  107. if (!$this->script) {
  108. return $this->_set_error(self::ERROR_INTERNAL);
  109. }
  110. if (!$name) {
  111. $name = $this->current;
  112. }
  113. $script = $this->script->as_text();
  114. if (!$script) {
  115. $script = '/* empty script */';
  116. }
  117. $result = $this->sieve->installScript($name, $script);
  118. if (is_a($result, 'PEAR_Error')) {
  119. return $this->_set_error(self::ERROR_INSTALL);
  120. }
  121. return true;
  122. }
  123. /**
  124. * Saves text script into server
  125. */
  126. public function save_script($name, $content = null)
  127. {
  128. if (!$this->sieve) {
  129. return $this->_set_error(self::ERROR_INTERNAL);
  130. }
  131. if (!$content) {
  132. $content = '/* empty script */';
  133. }
  134. $result = $this->sieve->installScript($name, $content);
  135. if (is_a($result, 'PEAR_Error')) {
  136. return $this->_set_error(self::ERROR_INSTALL);
  137. }
  138. return true;
  139. }
  140. /**
  141. * Activates specified script
  142. */
  143. public function activate($name = null)
  144. {
  145. if (!$this->sieve) {
  146. return $this->_set_error(self::ERROR_INTERNAL);
  147. }
  148. if (!$name) {
  149. $name = $this->current;
  150. }
  151. $result = $this->sieve->setActive($name);
  152. if (is_a($result, 'PEAR_Error')) {
  153. return $this->_set_error(self::ERROR_ACTIVATE);
  154. }
  155. return true;
  156. }
  157. /**
  158. * De-activates specified script
  159. */
  160. public function deactivate()
  161. {
  162. if (!$this->sieve) {
  163. return $this->_set_error(self::ERROR_INTERNAL);
  164. }
  165. $result = $this->sieve->setActive('');
  166. if (is_a($result, 'PEAR_Error')) {
  167. return $this->_set_error(self::ERROR_DEACTIVATE);
  168. }
  169. return true;
  170. }
  171. /**
  172. * Removes specified script
  173. */
  174. public function remove($name = null)
  175. {
  176. if (!$this->sieve) {
  177. return $this->_set_error(self::ERROR_INTERNAL);
  178. }
  179. if (!$name) {
  180. $name = $this->current;
  181. }
  182. // script must be deactivated first
  183. if ($name == $this->sieve->getActive()) {
  184. $result = $this->sieve->setActive('');
  185. if (is_a($result, 'PEAR_Error')) {
  186. return $this->_set_error(self::ERROR_DELETE);
  187. }
  188. }
  189. $result = $this->sieve->removeScript($name);
  190. if (is_a($result, 'PEAR_Error')) {
  191. return $this->_set_error(self::ERROR_DELETE);
  192. }
  193. if ($name == $this->current) {
  194. $this->current = null;
  195. }
  196. return true;
  197. }
  198. /**
  199. * Gets list of supported by server Sieve extensions
  200. */
  201. public function get_extensions()
  202. {
  203. if ($this->exts)
  204. return $this->exts;
  205. if (!$this->sieve)
  206. return $this->_set_error(self::ERROR_INTERNAL);
  207. $ext = $this->sieve->getExtensions();
  208. if (is_a($ext, 'PEAR_Error')) {
  209. return array();
  210. }
  211. // we're working on lower-cased names
  212. $ext = array_map('strtolower', (array) $ext);
  213. if ($this->script) {
  214. $supported = $this->script->get_extensions();
  215. foreach ($ext as $idx => $ext_name)
  216. if (!in_array($ext_name, $supported))
  217. unset($ext[$idx]);
  218. }
  219. return array_values($ext);
  220. }
  221. /**
  222. * Gets list of scripts from server
  223. */
  224. public function get_scripts()
  225. {
  226. if (!$this->list) {
  227. if (!$this->sieve)
  228. return $this->_set_error(self::ERROR_INTERNAL);
  229. $list = $this->sieve->listScripts();
  230. if (is_a($list, 'PEAR_Error')) {
  231. return $this->_set_error(self::ERROR_OTHER);
  232. }
  233. $this->list = $list;
  234. }
  235. return $this->list;
  236. }
  237. /**
  238. * Returns active script name
  239. */
  240. public function get_active()
  241. {
  242. if (!$this->sieve)
  243. return $this->_set_error(self::ERROR_INTERNAL);
  244. return $this->sieve->getActive();
  245. }
  246. /**
  247. * Loads script by name
  248. */
  249. public function load($name)
  250. {
  251. if (!$this->sieve)
  252. return $this->_set_error(self::ERROR_INTERNAL);
  253. if ($this->current == $name)
  254. return true;
  255. $script = $this->sieve->getScript($name);
  256. if (is_a($script, 'PEAR_Error')) {
  257. return $this->_set_error(self::ERROR_OTHER);
  258. }
  259. // try to parse from Roundcube format
  260. $this->script = $this->_parse($script);
  261. $this->current = $name;
  262. return true;
  263. }
  264. /**
  265. * Loads script from text content
  266. */
  267. public function load_script($script)
  268. {
  269. if (!$this->sieve)
  270. return $this->_set_error(self::ERROR_INTERNAL);
  271. // try to parse from Roundcube format
  272. $this->script = $this->_parse($script);
  273. }
  274. /**
  275. * Creates rcube_sieve_script object from text script
  276. */
  277. private function _parse($txt)
  278. {
  279. // parse
  280. $script = new rcube_sieve_script($txt, $this->exts);
  281. // fix/convert to Roundcube format
  282. if (!empty($script->content)) {
  283. // replace all elsif with if+stop, we support only ifs
  284. foreach ($script->content as $idx => $rule) {
  285. if (empty($rule['type']) || !preg_match('/^(if|elsif|else)$/', $rule['type'])) {
  286. continue;
  287. }
  288. $script->content[$idx]['type'] = 'if';
  289. // 'stop' not found?
  290. foreach ($rule['actions'] as $action) {
  291. if (preg_match('/^(stop|vacation)$/', $action['type'])) {
  292. continue 2;
  293. }
  294. }
  295. if (!empty($script->content[$idx+1]) && $script->content[$idx+1]['type'] != 'if') {
  296. $script->content[$idx]['actions'][] = array('type' => 'stop');
  297. }
  298. }
  299. }
  300. return $script;
  301. }
  302. /**
  303. * Gets specified script as text
  304. */
  305. public function get_script($name)
  306. {
  307. if (!$this->sieve)
  308. return $this->_set_error(self::ERROR_INTERNAL);
  309. $content = $this->sieve->getScript($name);
  310. if (is_a($content, 'PEAR_Error')) {
  311. return $this->_set_error(self::ERROR_OTHER);
  312. }
  313. return $content;
  314. }
  315. /**
  316. * Creates empty script or copy of other script
  317. */
  318. public function copy($name, $copy)
  319. {
  320. if (!$this->sieve)
  321. return $this->_set_error(self::ERROR_INTERNAL);
  322. if ($copy) {
  323. $content = $this->sieve->getScript($copy);
  324. if (is_a($content, 'PEAR_Error')) {
  325. return $this->_set_error(self::ERROR_OTHER);
  326. }
  327. }
  328. return $this->save_script($name, $content);
  329. }
  330. private function _set_error($error)
  331. {
  332. $this->error = $error;
  333. return false;
  334. }
  335. /**
  336. * This is our own debug handler for connection
  337. */
  338. public function debug_handler(&$sieve, $message)
  339. {
  340. rcube::write_log('sieve', preg_replace('/\r\n$/', '', $message));
  341. }
  342. }