rcube_sieve.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 $errorLines = array(); // array of line numbers within sieve script which raised an error
  27. private $list = array(); // scripts list
  28. public $script; // rcube_sieve_script object
  29. public $current; // name of currently loaded script
  30. private $exts; // array of supported extensions
  31. const ERROR_CONNECTION = 1;
  32. const ERROR_LOGIN = 2;
  33. const ERROR_NOT_EXISTS = 3; // script not exists
  34. const ERROR_INSTALL = 4; // script installation
  35. const ERROR_ACTIVATE = 5; // script activation
  36. const ERROR_DELETE = 6; // script deletion
  37. const ERROR_INTERNAL = 7; // internal error
  38. const ERROR_DEACTIVATE = 8; // script activation
  39. const ERROR_OTHER = 255; // other/unknown error
  40. /**
  41. * Object constructor
  42. *
  43. * @param string Username (for managesieve login)
  44. * @param string Password (for managesieve login)
  45. * @param string Managesieve server hostname/address
  46. * @param string Managesieve server port number
  47. * @param string Managesieve authentication method
  48. * @param boolean Enable/disable TLS use
  49. * @param array Disabled extensions
  50. * @param boolean Enable/disable debugging
  51. * @param string Proxy authentication identifier
  52. * @param string Proxy authentication password
  53. * @param array List of options to pass to stream_context_create().
  54. */
  55. public function __construct($username, $password='', $host='localhost', $port=2000,
  56. $auth_type=null, $usetls=true, $disabled=array(), $debug=false,
  57. $auth_cid=null, $auth_pw=null, $options=array())
  58. {
  59. $this->sieve = new Net_Sieve();
  60. if ($debug) {
  61. $this->sieve->setDebug(true, array($this, 'debug_handler'));
  62. }
  63. $result = $this->sieve->connect($host, $port, $options, $usetls);
  64. if (is_a($result, 'PEAR_Error')) {
  65. return $this->_set_error(self::ERROR_CONNECTION);
  66. }
  67. if (!empty($auth_cid)) {
  68. $authz = $username;
  69. $username = $auth_cid;
  70. }
  71. if (!empty($auth_pw)) {
  72. $password = $auth_pw;
  73. }
  74. $result = $this->sieve->login($username, $password, $auth_type ? strtoupper($auth_type) : null, $authz);
  75. if (is_a($result, 'PEAR_Error')) {
  76. return $this->_set_error(self::ERROR_LOGIN);
  77. }
  78. $this->exts = $this->get_extensions();
  79. // disable features by config
  80. if (!empty($disabled)) {
  81. // we're working on lower-cased names
  82. $disabled = array_map('strtolower', (array) $disabled);
  83. foreach ($disabled as $ext) {
  84. if (($idx = array_search($ext, $this->exts)) !== false) {
  85. unset($this->exts[$idx]);
  86. }
  87. }
  88. }
  89. }
  90. public function __destruct() {
  91. $this->sieve->disconnect();
  92. }
  93. /**
  94. * Getter for error code
  95. */
  96. public function error()
  97. {
  98. return $this->error ?: false;
  99. }
  100. /**
  101. * Saves current script into server
  102. */
  103. public function save($name = null)
  104. {
  105. if (!$this->sieve) {
  106. return $this->_set_error(self::ERROR_INTERNAL);
  107. }
  108. if (!$this->script) {
  109. return $this->_set_error(self::ERROR_INTERNAL);
  110. }
  111. if (!$name) {
  112. $name = $this->current;
  113. }
  114. $script = $this->script->as_text();
  115. if (!$script) {
  116. $script = '/* empty script */';
  117. }
  118. $result = $this->sieve->installScript($name, $script);
  119. if (is_a($result, 'PEAR_Error')) {
  120. return $this->_set_error(self::ERROR_INSTALL);
  121. }
  122. return true;
  123. }
  124. /**
  125. * Saves text script into server
  126. */
  127. public function save_script($name, $content = null)
  128. {
  129. if (!$this->sieve) {
  130. return $this->_set_error(self::ERROR_INTERNAL);
  131. }
  132. if (!$content) {
  133. $content = '/* empty script */';
  134. }
  135. $result = $this->sieve->installScript($name, $content);
  136. if (is_a($result, 'PEAR_Error')) {
  137. $rawErrorMessage = $result->getMessage();
  138. $errMessages = preg_split("/$name:/", $rawErrorMessage);
  139. if (sizeof($errMessages) > 0) {
  140. foreach ($errMessages as $singleError) {
  141. $matches = array();
  142. $res = preg_match('/line (\d+):(.*)/i', $singleError, $matches);
  143. if ($res === 1 ) {
  144. if (count($matches) > 2) {
  145. $this->errorLines[] = array("line" => $matches[1], "msg" => $matches[2]);
  146. }
  147. else {
  148. $this->errorLines[] = array("line" => $matches[1], "msg" => null);
  149. }
  150. }
  151. }
  152. }
  153. return $this->_set_error(self::ERROR_INSTALL);
  154. }
  155. return true;
  156. }
  157. /**
  158. * Returns the current error line within the saved sieve script
  159. */
  160. public function get_error_lines()
  161. {
  162. return $this->errorLines;
  163. }
  164. /**
  165. * Activates specified script
  166. */
  167. public function activate($name = null)
  168. {
  169. if (!$this->sieve) {
  170. return $this->_set_error(self::ERROR_INTERNAL);
  171. }
  172. if (!$name) {
  173. $name = $this->current;
  174. }
  175. $result = $this->sieve->setActive($name);
  176. if (is_a($result, 'PEAR_Error')) {
  177. return $this->_set_error(self::ERROR_ACTIVATE);
  178. }
  179. return true;
  180. }
  181. /**
  182. * De-activates specified script
  183. */
  184. public function deactivate()
  185. {
  186. if (!$this->sieve) {
  187. return $this->_set_error(self::ERROR_INTERNAL);
  188. }
  189. $result = $this->sieve->setActive('');
  190. if (is_a($result, 'PEAR_Error')) {
  191. return $this->_set_error(self::ERROR_DEACTIVATE);
  192. }
  193. return true;
  194. }
  195. /**
  196. * Removes specified script
  197. */
  198. public function remove($name = null)
  199. {
  200. if (!$this->sieve) {
  201. return $this->_set_error(self::ERROR_INTERNAL);
  202. }
  203. if (!$name) {
  204. $name = $this->current;
  205. }
  206. // script must be deactivated first
  207. if ($name == $this->sieve->getActive()) {
  208. $result = $this->sieve->setActive('');
  209. if (is_a($result, 'PEAR_Error')) {
  210. return $this->_set_error(self::ERROR_DELETE);
  211. }
  212. }
  213. $result = $this->sieve->removeScript($name);
  214. if (is_a($result, 'PEAR_Error')) {
  215. return $this->_set_error(self::ERROR_DELETE);
  216. }
  217. if ($name == $this->current) {
  218. $this->current = null;
  219. }
  220. return true;
  221. }
  222. /**
  223. * Gets list of supported by server Sieve extensions
  224. */
  225. public function get_extensions()
  226. {
  227. if ($this->exts)
  228. return $this->exts;
  229. if (!$this->sieve)
  230. return $this->_set_error(self::ERROR_INTERNAL);
  231. $ext = $this->sieve->getExtensions();
  232. if (is_a($ext, 'PEAR_Error')) {
  233. return array();
  234. }
  235. // we're working on lower-cased names
  236. $ext = array_map('strtolower', (array) $ext);
  237. if ($this->script) {
  238. $supported = $this->script->get_extensions();
  239. foreach ($ext as $idx => $ext_name)
  240. if (!in_array($ext_name, $supported))
  241. unset($ext[$idx]);
  242. }
  243. return array_values($ext);
  244. }
  245. /**
  246. * Gets list of scripts from server
  247. */
  248. public function get_scripts()
  249. {
  250. if (!$this->list) {
  251. if (!$this->sieve)
  252. return $this->_set_error(self::ERROR_INTERNAL);
  253. $list = $this->sieve->listScripts();
  254. if (is_a($list, 'PEAR_Error')) {
  255. return $this->_set_error(self::ERROR_OTHER);
  256. }
  257. $this->list = $list;
  258. }
  259. return $this->list;
  260. }
  261. /**
  262. * Returns active script name
  263. */
  264. public function get_active()
  265. {
  266. if (!$this->sieve)
  267. return $this->_set_error(self::ERROR_INTERNAL);
  268. return $this->sieve->getActive();
  269. }
  270. /**
  271. * Loads script by name
  272. */
  273. public function load($name)
  274. {
  275. if (!$this->sieve)
  276. return $this->_set_error(self::ERROR_INTERNAL);
  277. if ($this->current == $name)
  278. return true;
  279. $script = $this->sieve->getScript($name);
  280. if (is_a($script, 'PEAR_Error')) {
  281. return $this->_set_error(self::ERROR_OTHER);
  282. }
  283. // try to parse from Roundcube format
  284. $this->script = $this->_parse($script);
  285. $this->current = $name;
  286. return true;
  287. }
  288. /**
  289. * Loads script from text content
  290. */
  291. public function load_script($script)
  292. {
  293. if (!$this->sieve)
  294. return $this->_set_error(self::ERROR_INTERNAL);
  295. // try to parse from Roundcube format
  296. $this->script = $this->_parse($script);
  297. }
  298. /**
  299. * Creates rcube_sieve_script object from text script
  300. */
  301. private function _parse($txt)
  302. {
  303. // parse
  304. $script = new rcube_sieve_script($txt, $this->exts);
  305. // fix/convert to Roundcube format
  306. if (!empty($script->content)) {
  307. // replace all elsif with if+stop, we support only ifs
  308. foreach ($script->content as $idx => $rule) {
  309. if (empty($rule['type']) || !preg_match('/^(if|elsif|else)$/', $rule['type'])) {
  310. continue;
  311. }
  312. $script->content[$idx]['type'] = 'if';
  313. // 'stop' not found?
  314. foreach ($rule['actions'] as $action) {
  315. if (preg_match('/^(stop|vacation)$/', $action['type'])) {
  316. continue 2;
  317. }
  318. }
  319. if (!empty($script->content[$idx+1]) && $script->content[$idx+1]['type'] != 'if') {
  320. $script->content[$idx]['actions'][] = array('type' => 'stop');
  321. }
  322. }
  323. }
  324. return $script;
  325. }
  326. /**
  327. * Gets specified script as text
  328. */
  329. public function get_script($name)
  330. {
  331. if (!$this->sieve)
  332. return $this->_set_error(self::ERROR_INTERNAL);
  333. $content = $this->sieve->getScript($name);
  334. if (is_a($content, 'PEAR_Error')) {
  335. return $this->_set_error(self::ERROR_OTHER);
  336. }
  337. return $content;
  338. }
  339. /**
  340. * Creates empty script or copy of other script
  341. */
  342. public function copy($name, $copy)
  343. {
  344. if (!$this->sieve)
  345. return $this->_set_error(self::ERROR_INTERNAL);
  346. if ($copy) {
  347. $content = $this->sieve->getScript($copy);
  348. if (is_a($content, 'PEAR_Error')) {
  349. return $this->_set_error(self::ERROR_OTHER);
  350. }
  351. }
  352. return $this->save_script($name, $content);
  353. }
  354. private function _set_error($error)
  355. {
  356. $this->error = $error;
  357. return false;
  358. }
  359. /**
  360. * This is our own debug handler for connection
  361. */
  362. public function debug_handler(&$sieve, $message)
  363. {
  364. rcube::write_log('sieve', preg_replace('/\r\n$/', '', $message));
  365. }
  366. }