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.

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