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_engine.php 120KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760
  1. <?php
  2. /**
  3. * Managesieve (Sieve Filters) Engine
  4. *
  5. * Engine part of Managesieve plugin implementing UI and backend access.
  6. *
  7. * Copyright (C) 2008-2014, The Roundcube Dev Team
  8. * Copyright (C) 2011-2014, Kolab Systems AG
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see http://www.gnu.org/licenses/.
  22. */
  23. class rcube_sieve_engine
  24. {
  25. protected $rc;
  26. protected $sieve;
  27. protected $errors;
  28. protected $form;
  29. protected $list;
  30. protected $tips = array();
  31. protected $script = array();
  32. protected $exts = array();
  33. protected $active = array();
  34. protected $headers = array(
  35. 'subject' => 'Subject',
  36. 'from' => 'From',
  37. 'to' => 'To',
  38. );
  39. protected $addr_headers = array(
  40. // Required
  41. "from", "to", "cc", "bcc", "sender", "resent-from", "resent-to",
  42. // Additional (RFC 822 / RFC 2822)
  43. "reply-to", "resent-reply-to", "resent-sender", "resent-cc", "resent-bcc",
  44. // Non-standard (RFC 2076, draft-palme-mailext-headers-08.txt)
  45. "for-approval", "for-handling", "for-comment", "apparently-to", "errors-to",
  46. "delivered-to", "return-receipt-to", "x-admin", "read-receipt-to",
  47. "x-confirm-reading-to", "return-receipt-requested",
  48. "registered-mail-reply-requested-by", "mail-followup-to", "mail-reply-to",
  49. "abuse-reports-to", "x-complaints-to", "x-report-abuse-to",
  50. // Undocumented
  51. "x-beenthere",
  52. );
  53. protected $notify_methods = array(
  54. 'mailto',
  55. // 'sms',
  56. // 'tel',
  57. );
  58. protected $notify_importance_options = array(
  59. 3 => 'notifyimportancelow',
  60. 2 => 'notifyimportancenormal',
  61. 1 => 'notifyimportancehigh'
  62. );
  63. const VERSION = '8.9';
  64. const PROGNAME = 'Roundcube (Managesieve)';
  65. const PORT = 4190;
  66. /**
  67. * Class constructor
  68. */
  69. function __construct($plugin)
  70. {
  71. $this->rc = rcube::get_instance();
  72. $this->plugin = $plugin;
  73. }
  74. /**
  75. * Loads configuration, initializes plugin (including sieve connection)
  76. */
  77. function start($mode = null)
  78. {
  79. // register UI objects
  80. $this->rc->output->add_handlers(array(
  81. 'filterslist' => array($this, 'filters_list'),
  82. 'filtersetslist' => array($this, 'filtersets_list'),
  83. 'filterframe' => array($this, 'filter_frame'),
  84. 'filterform' => array($this, 'filter_form'),
  85. 'filtersetform' => array($this, 'filterset_form'),
  86. 'filterseteditraw' => array($this, 'filterset_editraw'),
  87. ));
  88. // connect to managesieve server
  89. $error = $this->connect($_SESSION['username'], $this->rc->decrypt($_SESSION['password']));
  90. // load current/active script
  91. if (!$error) {
  92. // Get list of scripts
  93. $list = $this->list_scripts();
  94. // reset current script when entering filters UI (#1489412)
  95. if ($this->rc->action == 'plugin.managesieve') {
  96. $this->rc->session->remove('managesieve_current');
  97. }
  98. if ($mode != 'vacation') {
  99. if (!empty($_GET['_set']) || !empty($_POST['_set'])) {
  100. $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true);
  101. }
  102. else if (!empty($_SESSION['managesieve_current'])) {
  103. $script_name = $_SESSION['managesieve_current'];
  104. }
  105. }
  106. $error = $this->load_script($script_name);
  107. }
  108. // finally set script objects
  109. if ($error) {
  110. switch ($error) {
  111. case rcube_sieve::ERROR_CONNECTION:
  112. case rcube_sieve::ERROR_LOGIN:
  113. $this->rc->output->show_message('managesieve.filterconnerror', 'error');
  114. break;
  115. default:
  116. $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
  117. break;
  118. }
  119. // reload interface in case of possible error when specified script wasn't found (#1489412)
  120. if ($script_name !== null && !empty($list) && !in_array($script_name, $list)) {
  121. $this->rc->output->command('reload', 500);
  122. }
  123. // to disable 'Add filter' button set env variable
  124. $this->rc->output->set_env('filterconnerror', true);
  125. $this->script = array();
  126. }
  127. else {
  128. $this->exts = $this->sieve->get_extensions();
  129. $this->init_script();
  130. $this->rc->output->set_env('currentset', $this->sieve->current);
  131. $_SESSION['managesieve_current'] = $this->sieve->current;
  132. }
  133. $this->rc->output->set_env('raw_sieve_editor', $this->rc->config->get('managesieve_raw_editor', true));
  134. return $error;
  135. }
  136. /**
  137. * Connect to configured managesieve server
  138. *
  139. * @param string $username User login
  140. * @param string $password User password
  141. *
  142. * @return int Connection status: 0 on success, >0 on failure
  143. */
  144. public function connect($username, $password)
  145. {
  146. // Get connection parameters
  147. $host = $this->rc->config->get('managesieve_host', 'localhost');
  148. $port = $this->rc->config->get('managesieve_port');
  149. $tls = $this->rc->config->get('managesieve_usetls', false);
  150. $host = rcube_utils::parse_host($host);
  151. $host = rcube_utils::idn_to_ascii($host);
  152. // remove tls:// prefix, set TLS flag
  153. if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) {
  154. $tls = true;
  155. }
  156. if (empty($port)) {
  157. $port = getservbyname('sieve', 'tcp');
  158. if (empty($port)) {
  159. $port = self::PORT;
  160. }
  161. }
  162. $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array(
  163. 'user' => $username,
  164. 'password' => $password,
  165. 'host' => $host,
  166. 'port' => $port,
  167. 'usetls' => $tls,
  168. 'auth_type' => $this->rc->config->get('managesieve_auth_type'),
  169. 'disabled' => $this->rc->config->get('managesieve_disabled_extensions'),
  170. 'debug' => $this->rc->config->get('managesieve_debug', false),
  171. 'auth_cid' => $this->rc->config->get('managesieve_auth_cid'),
  172. 'auth_pw' => $this->rc->config->get('managesieve_auth_pw'),
  173. 'socket_options' => $this->rc->config->get('managesieve_conn_options'),
  174. ));
  175. // Handle per-host socket options
  176. rcube_utils::parse_socket_options($plugin['socket_options'], $plugin['host']);
  177. // try to connect to managesieve server and to fetch the script
  178. $this->sieve = new rcube_sieve(
  179. $plugin['user'],
  180. $plugin['password'],
  181. $plugin['host'],
  182. $plugin['port'],
  183. $plugin['auth_type'],
  184. $plugin['usetls'],
  185. $plugin['disabled'],
  186. $plugin['debug'],
  187. $plugin['auth_cid'],
  188. $plugin['auth_pw'],
  189. $plugin['socket_options']
  190. );
  191. $error = $this->sieve->error();
  192. if ($error) {
  193. rcube::raise_error(array(
  194. 'code' => 403,
  195. 'file' => __FILE__,
  196. 'line' => __LINE__,
  197. 'message' => "Unable to connect to managesieve on $host:$port"
  198. ), true, false);
  199. }
  200. return $error;
  201. }
  202. /**
  203. * Load specified (or active) script
  204. *
  205. * @param string $script_name Optional script name
  206. *
  207. * @return int Connection status: 0 on success, >0 on failure
  208. */
  209. protected function load_script($script_name = null)
  210. {
  211. // Get list of scripts
  212. $list = $this->list_scripts();
  213. if ($script_name === null || $script_name === '') {
  214. // get (first) active script
  215. if (!empty($this->active)) {
  216. $script_name = $this->active[0];
  217. }
  218. else if ($list) {
  219. $script_name = $list[0];
  220. }
  221. // create a new (initial) script
  222. else {
  223. // if script not exists build default script contents
  224. $script_file = $this->rc->config->get('managesieve_default');
  225. $script_name = $this->rc->config->get('managesieve_script_name');
  226. if (empty($script_name)) {
  227. $script_name = 'roundcube';
  228. }
  229. if ($script_file && is_readable($script_file)) {
  230. $content = file_get_contents($script_file);
  231. }
  232. // add script and set it active
  233. if ($this->sieve->save_script($script_name, $content)) {
  234. $this->activate_script($script_name);
  235. $this->list[] = $script_name;
  236. }
  237. }
  238. }
  239. if ($script_name) {
  240. $this->sieve->load($script_name);
  241. }
  242. return $this->sieve->error();
  243. }
  244. /**
  245. * User interface actions handler
  246. */
  247. function actions()
  248. {
  249. $error = $this->start();
  250. // Handle user requests
  251. if ($action = rcube_utils::get_input_value('_act', rcube_utils::INPUT_GPC)) {
  252. $fid = (int) rcube_utils::get_input_value('_fid', rcube_utils::INPUT_POST);
  253. if ($action == 'delete' && !$error) {
  254. if (isset($this->script[$fid])) {
  255. if ($this->sieve->script->delete_rule($fid))
  256. $result = $this->save_script();
  257. if ($result === true) {
  258. $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation');
  259. $this->rc->output->command('managesieve_updatelist', 'del', array('id' => $fid));
  260. }
  261. else {
  262. $this->rc->output->show_message('managesieve.filterdeleteerror', 'error');
  263. }
  264. }
  265. }
  266. else if ($action == 'move' && !$error) {
  267. if (isset($this->script[$fid])) {
  268. $to = (int) rcube_utils::get_input_value('_to', rcube_utils::INPUT_POST);
  269. $rule = $this->script[$fid];
  270. // remove rule
  271. unset($this->script[$fid]);
  272. $this->script = array_values($this->script);
  273. // add at target position
  274. if ($to >= count($this->script)) {
  275. $this->script[] = $rule;
  276. }
  277. else {
  278. $script = array();
  279. foreach ($this->script as $idx => $r) {
  280. if ($idx == $to)
  281. $script[] = $rule;
  282. $script[] = $r;
  283. }
  284. $this->script = $script;
  285. }
  286. $this->sieve->script->content = $this->script;
  287. $result = $this->save_script();
  288. if ($result === true) {
  289. $result = $this->list_rules();
  290. $this->rc->output->show_message('managesieve.moved', 'confirmation');
  291. $this->rc->output->command('managesieve_updatelist', 'list',
  292. array('list' => $result, 'clear' => true, 'set' => $to));
  293. }
  294. else {
  295. $this->rc->output->show_message('managesieve.moveerror', 'error');
  296. }
  297. }
  298. }
  299. else if ($action == 'act' && !$error) {
  300. if (isset($this->script[$fid])) {
  301. $rule = $this->script[$fid];
  302. $disabled = !empty($rule['disabled']);
  303. $rule['disabled'] = !$disabled;
  304. $result = $this->sieve->script->update_rule($fid, $rule);
  305. if ($result !== false)
  306. $result = $this->save_script();
  307. if ($result === true) {
  308. if ($rule['disabled'])
  309. $this->rc->output->show_message('managesieve.deactivated', 'confirmation');
  310. else
  311. $this->rc->output->show_message('managesieve.activated', 'confirmation');
  312. $this->rc->output->command('managesieve_updatelist', 'update',
  313. array('id' => $fid, 'disabled' => $rule['disabled']));
  314. }
  315. else {
  316. if ($rule['disabled'])
  317. $this->rc->output->show_message('managesieve.deactivateerror', 'error');
  318. else
  319. $this->rc->output->show_message('managesieve.activateerror', 'error');
  320. }
  321. }
  322. }
  323. else if ($action == 'setact' && !$error) {
  324. $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true);
  325. $result = $this->activate_script($script_name);
  326. $kep14 = $this->rc->config->get('managesieve_kolab_master');
  327. if ($result === true) {
  328. $this->rc->output->set_env('active_sets', $this->active);
  329. $this->rc->output->show_message('managesieve.setactivated', 'confirmation');
  330. $this->rc->output->command('managesieve_updatelist', 'setact',
  331. array('name' => $script_name, 'active' => true, 'all' => !$kep14));
  332. }
  333. else {
  334. $this->rc->output->show_message('managesieve.setactivateerror', 'error');
  335. }
  336. }
  337. else if ($action == 'deact' && !$error) {
  338. $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true);
  339. $result = $this->deactivate_script($script_name);
  340. if ($result === true) {
  341. $this->rc->output->set_env('active_sets', $this->active);
  342. $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation');
  343. $this->rc->output->command('managesieve_updatelist', 'setact',
  344. array('name' => $script_name, 'active' => false));
  345. }
  346. else {
  347. $this->rc->output->show_message('managesieve.setdeactivateerror', 'error');
  348. }
  349. }
  350. else if ($action == 'setdel' && !$error) {
  351. $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true);
  352. $result = $this->remove_script($script_name);
  353. if ($result === true) {
  354. $this->rc->output->show_message('managesieve.setdeleted', 'confirmation');
  355. $this->rc->output->command('managesieve_updatelist', 'setdel',
  356. array('name' => $script_name));
  357. $this->rc->session->remove('managesieve_current');
  358. }
  359. else {
  360. $this->rc->output->show_message('managesieve.setdeleteerror', 'error');
  361. }
  362. }
  363. else if ($action == 'setget') {
  364. $this->rc->request_security_check(rcube_utils::INPUT_GET);
  365. $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true);
  366. $script = $this->sieve->get_script($script_name);
  367. if ($script === false) {
  368. exit;
  369. }
  370. $browser = new rcube_browser;
  371. // send download headers
  372. header("Content-Type: application/octet-stream");
  373. header("Content-Length: ".strlen($script));
  374. if ($browser->ie) {
  375. header("Content-Type: application/force-download");
  376. $filename = rawurlencode($script_name);
  377. }
  378. else {
  379. $filename = addcslashes($script_name, '\\"');
  380. }
  381. header("Content-Disposition: attachment; filename=\"$filename.txt\"");
  382. echo $script;
  383. exit;
  384. }
  385. else if ($action == 'list') {
  386. $result = $this->list_rules();
  387. $this->rc->output->command('managesieve_updatelist', 'list', array('list' => $result));
  388. }
  389. else if ($action == 'ruleadd') {
  390. $rid = rcube_utils::get_input_value('_rid', rcube_utils::INPUT_POST);
  391. $id = $this->genid();
  392. $content = $this->rule_div($fid, $id, false);
  393. $this->rc->output->command('managesieve_rulefill', $content, $id, $rid);
  394. }
  395. else if ($action == 'actionadd') {
  396. $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST);
  397. $id = $this->genid();
  398. $content = $this->action_div($fid, $id, false);
  399. $this->rc->output->command('managesieve_actionfill', $content, $id, $aid);
  400. }
  401. else if ($action == 'addresses') {
  402. $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST);
  403. $this->rc->output->command('managesieve_vacation_addresses_update', $aid, $this->user_emails());
  404. }
  405. $this->rc->output->send();
  406. }
  407. else if ($this->rc->task == 'mail') {
  408. // Initialize the form
  409. $rules = rcube_utils::get_input_value('r', rcube_utils::INPUT_GET);
  410. if (!empty($rules)) {
  411. $tests = array();
  412. foreach ($rules as $rule) {
  413. list($header, $value) = explode(':', $rule, 2);
  414. $tests[] = array(
  415. 'type' => 'contains',
  416. 'test' => 'header',
  417. 'arg1' => $header,
  418. 'arg2' => $value,
  419. );
  420. }
  421. $this->form = array(
  422. 'join' => count($tests) > 1 ? 'allof' : 'anyof',
  423. 'name' => '',
  424. 'tests' => $tests,
  425. 'actions' => array(
  426. 0 => array('type' => 'fileinto'),
  427. 1 => array('type' => 'stop'),
  428. ),
  429. );
  430. }
  431. }
  432. $this->send();
  433. }
  434. function saveraw()
  435. {
  436. // Init plugin and handle managesieve connection
  437. $error = $this->start();
  438. $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST);
  439. $result = $this->sieve->save_script($script_name, $_POST['rawsetcontent']);
  440. if ($result === false) {
  441. $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
  442. $errorLines = $this->sieve->get_error_lines();
  443. if (count($errorLines) > 0) {
  444. $this->rc->output->set_env("sieve_errors", $errorLines);
  445. }
  446. }
  447. else {
  448. $this->rc->output->show_message('managesieve.setupdated', 'confirmation');
  449. $this->rc->output->command('parent.managesieve_updatelist', 'refresh');
  450. }
  451. $this->send();
  452. }
  453. function save()
  454. {
  455. // Init plugin and handle managesieve connection
  456. $error = $this->start();
  457. // get request size limits (#1488648)
  458. $max_post = max(array(
  459. ini_get('max_input_vars'),
  460. ini_get('suhosin.request.max_vars'),
  461. ini_get('suhosin.post.max_vars'),
  462. ));
  463. $max_depth = max(array(
  464. ini_get('suhosin.request.max_array_depth'),
  465. ini_get('suhosin.post.max_array_depth'),
  466. ));
  467. // check request size limit
  468. if ($max_post && count($_POST, COUNT_RECURSIVE) >= $max_post) {
  469. rcube::raise_error(array(
  470. 'code' => 500, 'type' => 'php',
  471. 'file' => __FILE__, 'line' => __LINE__,
  472. 'message' => "Request size limit exceeded (one of max_input_vars/suhosin.request.max_vars/suhosin.post.max_vars)"
  473. ), true, false);
  474. $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
  475. }
  476. // check request depth limits
  477. else if ($max_depth && count($_POST['_header']) > $max_depth) {
  478. rcube::raise_error(array(
  479. 'code' => 500, 'type' => 'php',
  480. 'file' => __FILE__, 'line' => __LINE__,
  481. 'message' => "Request size limit exceeded (one of suhosin.request.max_array_depth/suhosin.post.max_array_depth)"
  482. ), true, false);
  483. $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
  484. }
  485. // filters set add action
  486. else if (!empty($_POST['_newset'])) {
  487. $name = rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true);
  488. $copy = rcube_utils::get_input_value('_copy', rcube_utils::INPUT_POST, true);
  489. $from = rcube_utils::get_input_value('_from', rcube_utils::INPUT_POST);
  490. $exceptions = $this->rc->config->get('managesieve_filename_exceptions');
  491. $kolab = $this->rc->config->get('managesieve_kolab_master');
  492. $name_uc = mb_strtolower($name);
  493. $list = $this->list_scripts();
  494. if (!$name) {
  495. $this->errors['name'] = $this->plugin->gettext('cannotbeempty');
  496. }
  497. else if (mb_strlen($name) > 128) {
  498. $this->errors['name'] = $this->plugin->gettext('nametoolong');
  499. }
  500. else if (!empty($exceptions) && in_array($name, (array)$exceptions)) {
  501. $this->errors['name'] = $this->plugin->gettext('namereserved');
  502. }
  503. else if (!empty($kolab) && in_array($name_uc, array('MASTER', 'USER', 'MANAGEMENT'))) {
  504. $this->errors['name'] = $this->plugin->gettext('namereserved');
  505. }
  506. else if (in_array($name, $list)) {
  507. $this->errors['name'] = $this->plugin->gettext('setexist');
  508. }
  509. else if ($from == 'file') {
  510. // from file
  511. if (is_uploaded_file($_FILES['_file']['tmp_name'])) {
  512. $file = file_get_contents($_FILES['_file']['tmp_name']);
  513. $file = preg_replace('/\r/', '', $file);
  514. // for security don't save script directly
  515. // check syntax before, like this...
  516. $this->sieve->load_script($file);
  517. if (!$this->save_script($name)) {
  518. $this->errors['file'] = $this->plugin->gettext('setcreateerror');
  519. }
  520. }
  521. else { // upload failed
  522. $err = $_FILES['_file']['error'];
  523. if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
  524. $msg = $this->rc->gettext(array('name' => 'filesizeerror',
  525. 'vars' => array('size' =>
  526. $this->rc->show_bytes(rcube_utils::max_upload_size()))));
  527. }
  528. else {
  529. $this->errors['file'] = $this->plugin->gettext('fileuploaderror');
  530. }
  531. }
  532. }
  533. else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) {
  534. $error = 'managesieve.setcreateerror';
  535. }
  536. if (!$error && empty($this->errors)) {
  537. // Find position of the new script on the list
  538. $list[] = $name;
  539. asort($list, SORT_LOCALE_STRING);
  540. $list = array_values($list);
  541. $index = array_search($name, $list);
  542. $this->rc->output->show_message('managesieve.setcreated', 'confirmation');
  543. $this->rc->output->command('parent.managesieve_updatelist', 'setadd',
  544. array('name' => $name, 'index' => $index));
  545. }
  546. else if ($msg) {
  547. $this->rc->output->command('display_message', $msg, 'error');
  548. }
  549. else if ($error) {
  550. $this->rc->output->show_message($error, 'error');
  551. }
  552. }
  553. // filter add/edit action
  554. else if (isset($_POST['_name'])) {
  555. $name = trim(rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true));
  556. $fid = trim(rcube_utils::get_input_value('_fid', rcube_utils::INPUT_POST));
  557. $join = trim(rcube_utils::get_input_value('_join', rcube_utils::INPUT_POST));
  558. // and arrays
  559. $headers = rcube_utils::get_input_value('_header', rcube_utils::INPUT_POST);
  560. $cust_headers = rcube_utils::get_input_value('_custom_header', rcube_utils::INPUT_POST);
  561. $cust_vars = rcube_utils::get_input_value('_custom_var', rcube_utils::INPUT_POST);
  562. $ops = rcube_utils::get_input_value('_rule_op', rcube_utils::INPUT_POST);
  563. $sizeops = rcube_utils::get_input_value('_rule_size_op', rcube_utils::INPUT_POST);
  564. $sizeitems = rcube_utils::get_input_value('_rule_size_item', rcube_utils::INPUT_POST);
  565. $sizetargets = rcube_utils::get_input_value('_rule_size_target', rcube_utils::INPUT_POST);
  566. $targets = rcube_utils::get_input_value('_rule_target', rcube_utils::INPUT_POST, true);
  567. $mods = rcube_utils::get_input_value('_rule_mod', rcube_utils::INPUT_POST);
  568. $mod_types = rcube_utils::get_input_value('_rule_mod_type', rcube_utils::INPUT_POST);
  569. $body_trans = rcube_utils::get_input_value('_rule_trans', rcube_utils::INPUT_POST);
  570. $body_types = rcube_utils::get_input_value('_rule_trans_type', rcube_utils::INPUT_POST, true);
  571. $comparators = rcube_utils::get_input_value('_rule_comp', rcube_utils::INPUT_POST);
  572. $indexes = rcube_utils::get_input_value('_rule_index', rcube_utils::INPUT_POST);
  573. $lastindexes = rcube_utils::get_input_value('_rule_index_last', rcube_utils::INPUT_POST);
  574. $dateheaders = rcube_utils::get_input_value('_rule_date_header', rcube_utils::INPUT_POST);
  575. $dateparts = rcube_utils::get_input_value('_rule_date_part', rcube_utils::INPUT_POST);
  576. $message = rcube_utils::get_input_value('_rule_message', rcube_utils::INPUT_POST);
  577. $dup_handles = rcube_utils::get_input_value('_rule_duplicate_handle', rcube_utils::INPUT_POST, true);
  578. $dup_headers = rcube_utils::get_input_value('_rule_duplicate_header', rcube_utils::INPUT_POST, true);
  579. $dup_uniqueids = rcube_utils::get_input_value('_rule_duplicate_uniqueid', rcube_utils::INPUT_POST, true);
  580. $dup_seconds = rcube_utils::get_input_value('_rule_duplicate_seconds', rcube_utils::INPUT_POST);
  581. $dup_lasts = rcube_utils::get_input_value('_rule_duplicate_last', rcube_utils::INPUT_POST);
  582. $act_types = rcube_utils::get_input_value('_action_type', rcube_utils::INPUT_POST, true);
  583. $mailboxes = rcube_utils::get_input_value('_action_mailbox', rcube_utils::INPUT_POST, true);
  584. $act_targets = rcube_utils::get_input_value('_action_target', rcube_utils::INPUT_POST, true);
  585. $domain_targets = rcube_utils::get_input_value('_action_target_domain', rcube_utils::INPUT_POST);
  586. $area_targets = rcube_utils::get_input_value('_action_target_area', rcube_utils::INPUT_POST, true);
  587. $reasons = rcube_utils::get_input_value('_action_reason', rcube_utils::INPUT_POST, true);
  588. $addresses = rcube_utils::get_input_value('_action_addresses', rcube_utils::INPUT_POST, true);
  589. $intervals = rcube_utils::get_input_value('_action_interval', rcube_utils::INPUT_POST);
  590. $interval_types = rcube_utils::get_input_value('_action_interval_type', rcube_utils::INPUT_POST);
  591. $from = rcube_utils::get_input_value('_action_from', rcube_utils::INPUT_POST);
  592. $subject = rcube_utils::get_input_value('_action_subject', rcube_utils::INPUT_POST, true);
  593. $flags = rcube_utils::get_input_value('_action_flags', rcube_utils::INPUT_POST);
  594. $varnames = rcube_utils::get_input_value('_action_varname', rcube_utils::INPUT_POST);
  595. $varvalues = rcube_utils::get_input_value('_action_varvalue', rcube_utils::INPUT_POST);
  596. $varmods = rcube_utils::get_input_value('_action_varmods', rcube_utils::INPUT_POST);
  597. $notifymethods = rcube_utils::get_input_value('_action_notifymethod', rcube_utils::INPUT_POST);
  598. $notifytargets = rcube_utils::get_input_value('_action_notifytarget', rcube_utils::INPUT_POST, true);
  599. $notifyoptions = rcube_utils::get_input_value('_action_notifyoption', rcube_utils::INPUT_POST, true);
  600. $notifymessages = rcube_utils::get_input_value('_action_notifymessage', rcube_utils::INPUT_POST, true);
  601. $notifyfrom = rcube_utils::get_input_value('_action_notifyfrom', rcube_utils::INPUT_POST);
  602. $notifyimp = rcube_utils::get_input_value('_action_notifyimportance', rcube_utils::INPUT_POST);
  603. // we need a "hack" for radiobuttons
  604. foreach ($sizeitems as $item)
  605. $items[] = $item;
  606. $this->form['disabled'] = !empty($_POST['_disabled']);
  607. $this->form['join'] = $join == 'allof';
  608. $this->form['name'] = $name;
  609. $this->form['tests'] = array();
  610. $this->form['actions'] = array();
  611. if ($name == '')
  612. $this->errors['name'] = $this->plugin->gettext('cannotbeempty');
  613. else {
  614. foreach ($this->script as $idx => $rule)
  615. if($rule['name'] == $name && $idx != $fid) {
  616. $this->errors['name'] = $this->plugin->gettext('ruleexist');
  617. break;
  618. }
  619. }
  620. $i = 0;
  621. // rules
  622. if ($join == 'any') {
  623. $this->form['tests'][0]['test'] = 'true';
  624. }
  625. else {
  626. foreach ($headers as $idx => $header) {
  627. // targets are indexed differently (assume form order)
  628. $target = $this->strip_value(array_shift($targets), true);
  629. $header = $this->strip_value($header);
  630. $operator = $this->strip_value($ops[$idx]);
  631. $comparator = $this->strip_value($comparators[$idx]);
  632. if ($header == 'size') {
  633. $sizeop = $this->strip_value($sizeops[$idx]);
  634. $sizeitem = $this->strip_value($items[$idx]);
  635. $sizetarget = $this->strip_value($sizetargets[$idx]);
  636. $this->form['tests'][$i]['test'] = 'size';
  637. $this->form['tests'][$i]['type'] = $sizeop;
  638. $this->form['tests'][$i]['arg'] = $sizetarget;
  639. if ($sizetarget == '')
  640. $this->errors['tests'][$i]['sizetarget'] = $this->plugin->gettext('cannotbeempty');
  641. else if (!preg_match('/^[0-9]+(K|M|G)?$/i', $sizetarget.$sizeitem, $m)) {
  642. $this->errors['tests'][$i]['sizetarget'] = $this->plugin->gettext('forbiddenchars');
  643. $this->form['tests'][$i]['item'] = $sizeitem;
  644. }
  645. else
  646. $this->form['tests'][$i]['arg'] .= $m[1];
  647. }
  648. else if ($header == 'currentdate') {
  649. $datepart = $this->strip_value($dateparts[$idx]);
  650. if (preg_match('/^not/', $operator))
  651. $this->form['tests'][$i]['not'] = true;
  652. $type = preg_replace('/^not/', '', $operator);
  653. if ($type == 'exists') {
  654. $this->errors['tests'][$i]['op'] = true;
  655. }
  656. $this->form['tests'][$i]['test'] = 'currentdate';
  657. $this->form['tests'][$i]['type'] = $type;
  658. $this->form['tests'][$i]['part'] = $datepart;
  659. $this->form['tests'][$i]['arg'] = $target;
  660. if ($type != 'exists') {
  661. if (empty($target)) {
  662. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
  663. }
  664. else if (strpos($type, 'count-') === 0) {
  665. foreach ($target as $arg) {
  666. if (preg_match('/[^0-9]/', $arg)) {
  667. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
  668. }
  669. }
  670. }
  671. else if (strpos($type, 'value-') === 0) {
  672. // Some date/time formats do not support i;ascii-numeric comparator
  673. if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) {
  674. $comparator = '';
  675. }
  676. }
  677. if (!preg_match('/^(regex|matches|count-)/', $type) && !empty($target)) {
  678. foreach ($target as $arg) {
  679. if (!$this->validate_date_part($datepart, $arg)) {
  680. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat');
  681. break;
  682. }
  683. }
  684. }
  685. }
  686. }
  687. else if ($header == 'date') {
  688. $datepart = $this->strip_value($dateparts[$idx]);
  689. $dateheader = $this->strip_value($dateheaders[$idx]);
  690. $index = $this->strip_value($indexes[$idx]);
  691. $indexlast = $this->strip_value($lastindexes[$idx]);
  692. if (preg_match('/^not/', $operator))
  693. $this->form['tests'][$i]['not'] = true;
  694. $type = preg_replace('/^not/', '', $operator);
  695. if ($type == 'exists') {
  696. $this->errors['tests'][$i]['op'] = true;
  697. }
  698. if (!empty($index) && $mod != 'envelope') {
  699. $this->form['tests'][$i]['index'] = intval($index);
  700. $this->form['tests'][$i]['last'] = !empty($indexlast);
  701. }
  702. if (empty($dateheader)) {
  703. $dateheader = 'Date';
  704. }
  705. else if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/i', $dateheader)) {
  706. $this->errors['tests'][$i]['dateheader'] = $this->plugin->gettext('forbiddenchars');
  707. }
  708. $this->form['tests'][$i]['test'] = 'date';
  709. $this->form['tests'][$i]['type'] = $type;
  710. $this->form['tests'][$i]['part'] = $datepart;
  711. $this->form['tests'][$i]['arg'] = $target;
  712. $this->form['tests'][$i]['header'] = $dateheader;
  713. if ($type != 'exists') {
  714. if (empty($target)) {
  715. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
  716. }
  717. else if (strpos($type, 'count-') === 0) {
  718. foreach ($target as $arg) {
  719. if (preg_match('/[^0-9]/', $arg)) {
  720. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
  721. }
  722. }
  723. }
  724. else if (strpos($type, 'value-') === 0) {
  725. // Some date/time formats do not support i;ascii-numeric comparator
  726. if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) {
  727. $comparator = '';
  728. }
  729. }
  730. if (!empty($target) && !preg_match('/^(regex|matches|count-)/', $type)) {
  731. foreach ($target as $arg) {
  732. if (!$this->validate_date_part($datepart, $arg)) {
  733. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat');
  734. break;
  735. }
  736. }
  737. }
  738. }
  739. }
  740. else if ($header == 'body') {
  741. $trans = $this->strip_value($body_trans[$idx]);
  742. $trans_type = $this->strip_value($body_types[$idx], true);
  743. if (preg_match('/^not/', $operator))
  744. $this->form['tests'][$i]['not'] = true;
  745. $type = preg_replace('/^not/', '', $operator);
  746. if ($type == 'exists') {
  747. $this->errors['tests'][$i]['op'] = true;
  748. }
  749. $this->form['tests'][$i]['test'] = 'body';
  750. $this->form['tests'][$i]['type'] = $type;
  751. $this->form['tests'][$i]['arg'] = $target;
  752. if (empty($target) && $type != 'exists') {
  753. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
  754. }
  755. else if (preg_match('/^(value|count)-/', $type)) {
  756. foreach ($target as $target_value) {
  757. if (preg_match('/[^0-9]/', $target_value)) {
  758. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
  759. }
  760. }
  761. }
  762. $this->form['tests'][$i]['part'] = $trans;
  763. if ($trans == 'content') {
  764. $this->form['tests'][$i]['content'] = $trans_type;
  765. }
  766. }
  767. else if ($header == 'message') {
  768. $test = $this->strip_value($message[$idx]);
  769. if (preg_match('/^not/', $test)) {
  770. $this->form['tests'][$i]['not'] = true;
  771. $test = substr($test, 3);
  772. }
  773. $this->form['tests'][$i]['test'] = $test;
  774. if ($test == 'duplicate') {
  775. $this->form['tests'][$i]['last'] = !empty($dup_lasts[$idx]);
  776. $this->form['tests'][$i]['handle'] = trim($dup_handles[$idx]);
  777. $this->form['tests'][$i]['header'] = trim($dup_headers[$idx]);
  778. $this->form['tests'][$i]['uniqueid'] = trim($dup_uniqueids[$idx]);
  779. $this->form['tests'][$i]['seconds'] = trim($dup_seconds[$idx]);
  780. if ($this->form['tests'][$i]['seconds']
  781. && preg_match('/[^0-9]/', $this->form['tests'][$i]['seconds'])
  782. ) {
  783. $this->errors['tests'][$i]['duplicate_seconds'] = $this->plugin->gettext('forbiddenchars');
  784. }
  785. if ($this->form['tests'][$i]['header'] && $this->form['tests'][$i]['uniqueid']) {
  786. $this->errors['tests'][$i]['duplicate_uniqueid'] = $this->plugin->gettext('duplicate.conflict.err');
  787. }
  788. }
  789. }
  790. else {
  791. $cust_header = $headers = $this->strip_value(array_shift($cust_headers));
  792. $mod = $this->strip_value($mods[$idx]);
  793. $mod_type = $this->strip_value($mod_types[$idx]);
  794. $index = $this->strip_value($indexes[$idx]);
  795. $indexlast = $this->strip_value($lastindexes[$idx]);
  796. if ($header == 'string') {
  797. $cust_var = $headers = $this->strip_value(array_shift($cust_vars));
  798. }
  799. if (preg_match('/^not/', $operator))
  800. $this->form['tests'][$i]['not'] = true;
  801. $type = preg_replace('/^not/', '', $operator);
  802. if (!empty($index) && $mod != 'envelope') {
  803. $this->form['tests'][$i]['index'] = intval($index);
  804. $this->form['tests'][$i]['last'] = !empty($indexlast);
  805. }
  806. if ($header == '...' || $header == 'string') {
  807. if (!count($headers))
  808. $this->errors['tests'][$i]['header'] = $this->plugin->gettext('cannotbeempty');
  809. else if ($header == '...') {
  810. foreach ($headers as $hr) {
  811. // RFC2822: printable ASCII except colon
  812. if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/i', $hr)) {
  813. $this->errors['tests'][$i]['header'] = $this->plugin->gettext('forbiddenchars');
  814. }
  815. }
  816. }
  817. if (empty($this->errors['tests'][$i]['header']))
  818. $cust_header = $cust_var = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers;
  819. }
  820. $test = $header == 'string' ? 'string' : 'header';
  821. $header = $header == 'string' ? $cust_var : $header;
  822. $header = $header == '...' ? $cust_header : $header;
  823. if (is_array($header)) {
  824. foreach ($header as $h_index => $val) {
  825. if (isset($this->headers[$val])) {
  826. $header[$h_index] = $this->headers[$val];
  827. }
  828. }
  829. }
  830. if ($type == 'exists') {
  831. $this->form['tests'][$i]['test'] = 'exists';
  832. $this->form['tests'][$i]['arg'] = $header;
  833. }
  834. else {
  835. if ($mod == 'address' || $mod == 'envelope') {
  836. $found = false;
  837. if (empty($this->errors['tests'][$i]['header'])) {
  838. foreach ((array)$header as $hdr) {
  839. if (!in_array(strtolower(trim($hdr)), $this->addr_headers))
  840. $found = true;
  841. }
  842. }
  843. if (!$found)
  844. $test = $mod;
  845. }
  846. $this->form['tests'][$i]['type'] = $type;
  847. $this->form['tests'][$i]['test'] = $test;
  848. $this->form['tests'][$i]['arg1'] = $header;
  849. $this->form['tests'][$i]['arg2'] = $target;
  850. if (empty($target)) {
  851. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
  852. }
  853. else if (preg_match('/^(value|count)-/', $type)) {
  854. foreach ($target as $target_value) {
  855. if (preg_match('/[^0-9]/', $target_value)) {
  856. $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
  857. }
  858. }
  859. }
  860. if ($mod) {
  861. $this->form['tests'][$i]['part'] = $mod_type;
  862. }
  863. }
  864. }
  865. if ($header != 'size' && $comparator) {
  866. $this->form['tests'][$i]['comparator'] = $comparator;
  867. }
  868. $i++;
  869. }
  870. }
  871. $i = 0;
  872. // actions
  873. foreach ($act_types as $idx => $type) {
  874. $type = $this->strip_value($type);
  875. switch ($type) {
  876. case 'fileinto':
  877. case 'fileinto_copy':
  878. $mailbox = $this->strip_value($mailboxes[$idx], false, false);
  879. $this->form['actions'][$i]['target'] = $this->mod_mailbox($mailbox, 'in');
  880. if ($type == 'fileinto_copy') {
  881. $type = 'fileinto';
  882. $this->form['actions'][$i]['copy'] = true;
  883. }
  884. break;
  885. case 'reject':
  886. case 'ereject':
  887. $target = $this->strip_value($area_targets[$idx]);
  888. $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target);
  889. // if ($target == '')
  890. // $this->errors['actions'][$i]['targetarea'] = $this->plugin->gettext('cannotbeempty');
  891. break;
  892. case 'redirect':
  893. case 'redirect_copy':
  894. $target = $this->strip_value($act_targets[$idx]);
  895. $domain = $this->strip_value($domain_targets[$idx]);
  896. // force one of the configured domains
  897. $domains = (array) $this->rc->config->get('managesieve_domains');
  898. if (!empty($domains) && !empty($target)) {
  899. if (!$domain || !in_array($domain, $domains)) {
  900. $domain = $domains[0];
  901. }
  902. $target .= '@' . $domain;
  903. }
  904. $this->form['actions'][$i]['target'] = $target;
  905. if ($target == '')
  906. $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
  907. else if (!rcube_utils::check_email($target))
  908. $this->errors['actions'][$i]['target'] = $this->plugin->gettext(!empty($domains) ? 'forbiddenchars' : 'noemailwarning');
  909. if ($type == 'redirect_copy') {
  910. $type = 'redirect';
  911. $this->form['actions'][$i]['copy'] = true;
  912. }
  913. break;
  914. case 'addflag':
  915. case 'setflag':
  916. case 'removeflag':
  917. $_target = array();
  918. if (empty($flags[$idx])) {
  919. $this->errors['actions'][$i]['target'] = $this->plugin->gettext('noflagset');
  920. }
  921. else {
  922. foreach ($flags[$idx] as $flag) {
  923. $_target[] = $this->strip_value($flag);
  924. }
  925. }
  926. $this->form['actions'][$i]['target'] = $_target;
  927. break;
  928. case 'vacation':
  929. $reason = $this->strip_value($reasons[$idx]);
  930. $interval_type = $interval_types[$idx] == 'seconds' ? 'seconds' : 'days';
  931. $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason);
  932. $this->form['actions'][$i]['from'] = $from[$idx];
  933. $this->form['actions'][$i]['subject'] = $subject[$idx];
  934. $this->form['actions'][$i]['addresses'] = array_shift($addresses);
  935. $this->form['actions'][$i][$interval_type] = $intervals[$idx];
  936. // @TODO: vacation :mime, :handle
  937. foreach ((array)$this->form['actions'][$i]['addresses'] as $aidx => $address) {
  938. $this->form['actions'][$i]['addresses'][$aidx] = $address = trim($address);
  939. if (empty($address)) {
  940. unset($this->form['actions'][$i]['addresses'][$aidx]);
  941. }
  942. else if (!rcube_utils::check_email($address)) {
  943. $this->errors['actions'][$i]['addresses'] = $this->plugin->gettext('noemailwarning');
  944. break;
  945. }
  946. }
  947. if (!empty($this->form['actions'][$i]['from']) && !rcube_utils::check_email($this->form['actions'][$i]['from'])) {
  948. $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning');
  949. }
  950. if ($this->form['actions'][$i]['reason'] == '')
  951. $this->errors['actions'][$i]['reason'] = $this->plugin->gettext('cannotbeempty');
  952. if ($this->form['actions'][$i][$interval_type] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i][$interval_type]))
  953. $this->errors['actions'][$i]['interval'] = $this->plugin->gettext('forbiddenchars');
  954. break;
  955. case 'set':
  956. $this->form['actions'][$i]['name'] = $varnames[$idx];
  957. $this->form['actions'][$i]['value'] = $varvalues[$idx];
  958. foreach ((array)$varmods[$idx] as $v_m) {
  959. $this->form['actions'][$i][$v_m] = true;
  960. }
  961. if (empty($varnames[$idx])) {
  962. $this->errors['actions'][$i]['name'] = $this->plugin->gettext('cannotbeempty');
  963. }
  964. else if (!preg_match('/^[0-9a-z_]+$/i', $varnames[$idx])) {
  965. $this->errors['actions'][$i]['name'] = $this->plugin->gettext('forbiddenchars');
  966. }
  967. if (!isset($varvalues[$idx]) || $varvalues[$idx] === '') {
  968. $this->errors['actions'][$i]['value'] = $this->plugin->gettext('cannotbeempty');
  969. }
  970. break;
  971. case 'notify':
  972. if (empty($notifymethods[$idx])) {
  973. $this->errors['actions'][$i]['method'] = $this->plugin->gettext('cannotbeempty');
  974. }
  975. if (empty($notifytargets[$idx])) {
  976. $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
  977. }
  978. if (!empty($notifyfrom[$idx]) && !rcube_utils::check_email($notifyfrom[$idx])) {
  979. $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning');
  980. }
  981. // skip empty options
  982. foreach ((array)$notifyoptions[$idx] as $opt_idx => $opt) {
  983. if (!strlen(trim($opt))) {
  984. unset($notifyoptions[$idx][$opt_idx]);
  985. }
  986. }
  987. $this->form['actions'][$i]['method'] = $notifymethods[$idx] . ':' . $notifytargets[$idx];
  988. $this->form['actions'][$i]['options'] = $notifyoptions[$idx];
  989. $this->form['actions'][$i]['message'] = $notifymessages[$idx];
  990. $this->form['actions'][$i]['from'] = $notifyfrom[$idx];
  991. $this->form['actions'][$i]['importance'] = $notifyimp[$idx];
  992. break;
  993. }
  994. $this->form['actions'][$i]['type'] = $type;
  995. $i++;
  996. }
  997. if (!$this->errors && !$error) {
  998. // save the script
  999. if (!isset($this->script[$fid])) {
  1000. $fid = $this->sieve->script->add_rule($this->form);
  1001. $new = true;
  1002. }
  1003. else {
  1004. $fid = $this->sieve->script->update_rule($fid, $this->form);
  1005. }
  1006. if ($fid !== false) {
  1007. $save = $this->save_script();
  1008. }
  1009. if ($save && $fid !== false) {
  1010. $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
  1011. if ($this->rc->task != 'mail') {
  1012. $this->rc->output->command('parent.managesieve_updatelist',
  1013. isset($new) ? 'add' : 'update',
  1014. array(
  1015. 'name' => $this->form['name'],
  1016. 'id' => $fid,
  1017. 'disabled' => $this->form['disabled']
  1018. ));
  1019. }
  1020. else {
  1021. $this->rc->output->command('managesieve_dialog_close');
  1022. $this->rc->output->send('iframe');
  1023. }
  1024. }
  1025. else {
  1026. $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
  1027. }
  1028. }
  1029. else {
  1030. $this->rc->output->show_message('managesieve.filterformerror', 'warning');
  1031. }
  1032. }
  1033. $this->send();
  1034. }
  1035. protected function send()
  1036. {
  1037. // Handle form action
  1038. if (isset($_GET['_framed']) || isset($_POST['_framed'])) {
  1039. if (isset($_GET['_newset']) || isset($_POST['_newset'])) {
  1040. $this->rc->output->send('managesieve.setedit');
  1041. }
  1042. else if (isset($_GET['_seteditraw']) || isset($_POST['_seteditraw'])) {
  1043. $this->rc->output->send('managesieve.seteditraw');
  1044. }
  1045. else {
  1046. $this->rc->output->send('managesieve.filteredit');
  1047. }
  1048. }
  1049. else {
  1050. $this->rc->output->set_pagetitle($this->plugin->gettext('filters'));
  1051. $this->rc->output->send('managesieve.managesieve');
  1052. }
  1053. }
  1054. // return the filters list as HTML table
  1055. function filters_list($attrib)
  1056. {
  1057. // add id to message list table if not specified
  1058. if (!strlen($attrib['id']))
  1059. $attrib['id'] = 'rcmfilterslist';
  1060. // define list of cols to be displayed
  1061. $a_show_cols = array('name');
  1062. $result = $this->list_rules();
  1063. // create XHTML table
  1064. $out = $this->rc->table_output($attrib, $result, $a_show_cols, 'id');
  1065. // set client env
  1066. $this->rc->output->add_gui_object('filterslist', $attrib['id']);
  1067. $this->rc->output->include_script('list.js');
  1068. // add some labels to client
  1069. $this->rc->output->add_label('managesieve.filterdeleteconfirm');
  1070. return $out;
  1071. }
  1072. // return the filters list as <SELECT>
  1073. function filtersets_list($attrib, $no_env = false)
  1074. {
  1075. // add id to message list table if not specified
  1076. if (!strlen($attrib['id'])) {
  1077. $attrib['id'] = 'rcmfiltersetslist';
  1078. }
  1079. $list = $this->list_scripts();
  1080. if ($list) {
  1081. asort($list, SORT_LOCALE_STRING);
  1082. }
  1083. if (!empty($attrib['type']) && $attrib['type'] == 'list') {
  1084. // define list of cols to be displayed
  1085. $a_show_cols = array('name');
  1086. if ($list) {
  1087. foreach ($list as $idx => $set) {
  1088. $scripts['S'.$idx] = $set;
  1089. $result[] = array(
  1090. 'name' => $set,
  1091. 'id' => 'S'.$idx,
  1092. 'class' => !in_array($set, $this->active) ? 'disabled' : '',
  1093. );
  1094. }
  1095. }
  1096. // create XHTML table
  1097. $out = $this->rc->table_output($attrib, $result, $a_show_cols, 'id');
  1098. $this->rc->output->set_env('filtersets', $scripts);
  1099. $this->rc->output->include_script('list.js');
  1100. }
  1101. else {
  1102. $select = new html_select(array('name' => '_set', 'id' => $attrib['id'],
  1103. 'onchange' => $this->rc->task != 'mail' ? 'rcmail.managesieve_set()' : ''));
  1104. if ($list) {
  1105. foreach ($list as $set)
  1106. $select->add($set, $set);
  1107. }
  1108. $out = $select->show($this->sieve->current);
  1109. }
  1110. // set client env
  1111. if (!$no_env) {
  1112. $this->rc->output->add_gui_object('filtersetslist', $attrib['id']);
  1113. $this->rc->output->add_label('managesieve.setdeleteconfirm');
  1114. }
  1115. return $out;
  1116. }
  1117. function filter_frame($attrib)
  1118. {
  1119. return $this->rc->output->frame($attrib, true);
  1120. }
  1121. function filterset_editraw($attrib)
  1122. {
  1123. $script_name = isset($_GET['_set']) ? $_GET['_set'] : $_POST['_set'];
  1124. $script = $this->sieve->get_script($script_name);
  1125. $script_post = $_POST['rawsetcontent'];
  1126. $out = '<form name="filtersetrawform" action="./" method="post" enctype="multipart/form-data">'."\n";
  1127. $hiddenfields = new html_hiddenfield();
  1128. $hiddenfields->add(array('name' => '_task', 'value' => $this->rc->task));
  1129. $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-saveraw'));
  1130. $hiddenfields->add(array('name' => '_set', 'value' => $script_name));
  1131. $hiddenfields->add(array('name' => '_seteditraw', 'value' => 1));
  1132. $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
  1133. $out .= $hiddenfields->show();
  1134. $txtarea = new html_textarea(array(
  1135. 'id' => 'rawfiltersettxt',
  1136. 'name' => 'rawsetcontent',
  1137. 'rows' => '15'
  1138. ));
  1139. $out .= $txtarea->show($script_post !== null ? $script_post : ($script !== false ? rtrim($script) : ''));
  1140. $this->rc->output->add_gui_object('sievesetrawform', 'filtersetrawform');
  1141. $this->plugin->include_stylesheet('codemirror/lib/codemirror.css');
  1142. $this->plugin->include_script('codemirror/lib/codemirror.js');
  1143. $this->plugin->include_script('codemirror/addon/selection/active-line.js');
  1144. $this->plugin->include_script('codemirror/mode/sieve/sieve.js');
  1145. if ($script === false) {
  1146. $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
  1147. }
  1148. return $out;
  1149. }
  1150. function filterset_form($attrib)
  1151. {
  1152. if (!$attrib['id'])
  1153. $attrib['id'] = 'rcmfiltersetform';
  1154. $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n";
  1155. $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
  1156. $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
  1157. $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
  1158. $hiddenfields->add(array('name' => '_newset', 'value' => 1));
  1159. $out .= $hiddenfields->show();
  1160. $name = rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST);
  1161. $copy = rcube_utils::get_input_value('_copy', rcube_utils::INPUT_POST);
  1162. $selected = rcube_utils::get_input_value('_from', rcube_utils::INPUT_POST);
  1163. // filter set name input
  1164. $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30,
  1165. 'class' => ($this->errors['name'] ? 'error' : '')));
  1166. $out .= sprintf('<label for="%s"><b>%s:</b></label> %s<br><br>',
  1167. '_name', rcube::Q($this->plugin->gettext('filtersetname')), $input_name->show($name));
  1168. $out .="\n<fieldset class=\"itemlist\"><legend>" . $this->plugin->gettext('filters') . ":</legend>\n";
  1169. $out .= html::tag('input', array(
  1170. 'type' => 'radio',
  1171. 'id' => 'from_none',
  1172. 'name' => '_from',
  1173. 'value' => 'none',
  1174. 'checked' => !$selected || $selected == 'none'
  1175. ));
  1176. $out .= html::label('from_none', rcube::Q($this->plugin->gettext('none')));
  1177. // filters set list
  1178. $list = $this->list_scripts();
  1179. $select = new html_select(array('name' => '_copy', 'id' => '_copy'));
  1180. if (is_array($list)) {
  1181. asort($list, SORT_LOCALE_STRING);
  1182. if (!$copy)
  1183. $copy = $_SESSION['managesieve_current'];
  1184. foreach ($list as $set) {
  1185. $select->add($set, $set);
  1186. }
  1187. $out .= '<br>';
  1188. $out .= html::tag('input', array(
  1189. 'type' => 'radio',
  1190. 'id' => 'from_set',
  1191. 'name' => '_from',
  1192. 'value' => 'set',
  1193. 'checked' => $selected == 'set',
  1194. ));
  1195. $out .= html::label('from_set', rcube::Q($this->plugin->gettext('fromset')));
  1196. $out .= $select->show($copy);
  1197. }
  1198. // script upload box
  1199. $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30,
  1200. 'type' => 'file', 'class' => ($this->errors['file'] ? 'error' : '')));
  1201. $out .= '<br>';
  1202. $out .= html::tag('input', array(
  1203. 'type' => 'radio',
  1204. 'id' => 'from_file',
  1205. 'name' => '_from',
  1206. 'value' => 'file',
  1207. 'checked' => $selected == 'file',
  1208. ));
  1209. $out .= html::label('from_file', rcube::Q($this->plugin->gettext('fromfile')));
  1210. $out .= $upload->show();
  1211. $out .= '</fieldset>';
  1212. $this->rc->output->add_gui_object('sieveform', 'filtersetform');
  1213. if ($this->errors['name'])
  1214. $this->add_tip('_name', $this->errors['name'], true);
  1215. if ($this->errors['file'])
  1216. $this->add_tip('_file', $this->errors['file'], true);
  1217. $this->print_tips();
  1218. return $out;
  1219. }
  1220. function filter_form($attrib)
  1221. {
  1222. if (!$attrib['id'])
  1223. $attrib['id'] = 'rcmfilterform';
  1224. $fid = rcube_utils::get_input_value('_fid', rcube_utils::INPUT_GPC);
  1225. $scr = isset($this->form) ? $this->form : $this->script[$fid];
  1226. $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
  1227. $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
  1228. $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
  1229. $hiddenfields->add(array('name' => '_fid', 'value' => $fid));
  1230. $out = '<form name="filterform" action="./" method="post">'."\n";
  1231. $out .= $hiddenfields->show();
  1232. // 'any' flag
  1233. if ((!isset($this->form) && empty($scr['tests']) && !empty($scr))
  1234. || (is_array($scr['tests']) && count($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not'])
  1235. ) {
  1236. $any = true;
  1237. }
  1238. // filter name input
  1239. $field_id = '_name';
  1240. $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30,
  1241. 'class' => ($this->errors['name'] ? 'error' : '')));
  1242. if ($this->errors['name'])
  1243. $this->add_tip($field_id, $this->errors['name'], true);
  1244. if (isset($scr))
  1245. $input_name = $input_name->show($scr['name']);
  1246. else
  1247. $input_name = $input_name->show();
  1248. $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s\n",
  1249. $field_id, rcube::Q($this->plugin->gettext('filtername')), $input_name);
  1250. // filter set selector
  1251. if ($this->rc->task == 'mail') {
  1252. $out .= sprintf("\n&nbsp;<label for=\"%s\"><b>%s:</b></label> %s\n",
  1253. $field_id, rcube::Q($this->plugin->gettext('filterset')),
  1254. $this->filtersets_list(array('id' => 'sievescriptname'), true));
  1255. }
  1256. $out .= '<br><br><fieldset><legend>' . rcube::Q($this->plugin->gettext('messagesrules')) . "</legend>\n";
  1257. // any, allof, anyof radio buttons
  1258. $field_id = '_allof';
  1259. $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof',
  1260. 'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio'));
  1261. if (isset($scr) && !$any)
  1262. $input_join = $input_join->show($scr['join'] ? 'allof' : '');
  1263. else
  1264. $input_join = $input_join->show();
  1265. $out .= $input_join . html::label($field_id, rcube::Q($this->plugin->gettext('filterallof')));
  1266. $field_id = '_anyof';
  1267. $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof',
  1268. 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio'));
  1269. if (isset($scr) && !$any)
  1270. $input_join = $input_join->show($scr['join'] ? '' : 'anyof');
  1271. else
  1272. $input_join = $input_join->show('anyof'); // default
  1273. $out .= $input_join . html::label($field_id, rcube::Q($this->plugin->gettext('filteranyof')));
  1274. $field_id = '_any';
  1275. $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any',
  1276. 'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio'));
  1277. $input_join = $input_join->show($any ? 'any' : '');
  1278. $out .= $input_join . html::label($field_id, rcube::Q($this->plugin->gettext('filterany')));
  1279. $rows_num = !empty($scr['tests']) ? count($scr['tests']) : 1;
  1280. $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>';
  1281. for ($x=0; $x<$rows_num; $x++)
  1282. $out .= $this->rule_div($fid, $x);
  1283. $out .= "</div>\n";
  1284. $out .= "</fieldset>\n";
  1285. // actions
  1286. $out .= '<fieldset><legend>' . rcube::Q($this->plugin->gettext('messagesactions')) . "</legend>\n";
  1287. $rows_num = isset($scr) ? count($scr['actions']) : 1;
  1288. $out .= '<div id="actions">';
  1289. for ($x=0; $x<$rows_num; $x++)
  1290. $out .= $this->action_div($fid, $x);
  1291. $out .= "</div>\n";
  1292. $out .= "</fieldset>\n";
  1293. $this->print_tips();
  1294. if ($scr['disabled']) {
  1295. $this->rc->output->set_env('rule_disabled', true);
  1296. }
  1297. $this->rc->output->add_label(
  1298. 'managesieve.ruledeleteconfirm',
  1299. 'managesieve.actiondeleteconfirm'
  1300. );
  1301. $this->rc->output->add_gui_object('sieveform', 'filterform');
  1302. return $out;
  1303. }
  1304. function rule_div($fid, $id, $div=true)
  1305. {
  1306. $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id];
  1307. if (isset($this->form['tests'])) {
  1308. $rows_num = count($this->form['tests']);
  1309. }
  1310. else if (isset($this->script[$fid]['tests'])) {
  1311. $rows_num = count($this->script[$fid]['tests']);
  1312. }
  1313. else {
  1314. $rows_num = 0;
  1315. }
  1316. // headers select
  1317. $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id,
  1318. 'onchange' => 'rule_header_select(' .$id .')'));
  1319. foreach ($this->headers as $index => $header) {
  1320. $header = $this->rc->text_exists($index) ? $this->plugin->gettext($index) : $header;
  1321. $select_header->add($header, $index);
  1322. }
  1323. $select_header->add($this->plugin->gettext('...'), '...');
  1324. if (in_array('body', $this->exts)) {
  1325. $select_header->add($this->plugin->gettext('body'), 'body');
  1326. }
  1327. $select_header->add($this->plugin->gettext('size'), 'size');
  1328. if (in_array('date', $this->exts)) {
  1329. $select_header->add($this->plugin->gettext('datetest'), 'date');
  1330. $select_header->add($this->plugin->gettext('currdate'), 'currentdate');
  1331. }
  1332. if (in_array('variables', $this->exts)) {
  1333. $select_header->add($this->plugin->gettext('string'), 'string');
  1334. }
  1335. if (in_array('duplicate', $this->exts)) {
  1336. $select_header->add($this->plugin->gettext('message'), 'message');
  1337. }
  1338. if (isset($rule['test'])) {
  1339. if (in_array($rule['test'], array('header', 'address', 'envelope'))) {
  1340. if (is_array($rule['arg1']) && count($rule['arg1']) == 1) {
  1341. $rule['arg1'] = $rule['arg1'][0];
  1342. }
  1343. $matches = ($header = strtolower($rule['arg1'])) && isset($this->headers[$header]);
  1344. $test = $matches ? $header : '...';
  1345. }
  1346. else if ($rule['test'] == 'exists') {
  1347. if (is_array($rule['arg']) && count($rule['arg']) == 1) {
  1348. $rule['arg'] = $rule['arg'][0];
  1349. }
  1350. $matches = ($header = strtolower($rule['arg'])) && isset($this->headers[$header]);
  1351. $test = $matches ? $header : '...';
  1352. }
  1353. else if (in_array($rule['test'], array('size', 'body', 'date', 'currentdate', 'string'))) {
  1354. $test = $rule['test'];
  1355. }
  1356. else if (in_array($rule['test'], array('duplicate'))) {
  1357. $test = 'message';
  1358. }
  1359. else if ($rule['test'] != 'true') {
  1360. $test = '...';
  1361. }
  1362. }
  1363. $aout = $select_header->show($test);
  1364. // custom headers input
  1365. if (isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope'))) {
  1366. $custom = (array) $rule['arg1'];
  1367. if (count($custom) == 1 && isset($this->headers[strtolower($custom[0])])) {
  1368. unset($custom);
  1369. }
  1370. }
  1371. else if (isset($rule['test']) && $rule['test'] == 'string') {
  1372. $customv = (array) $rule['arg1'];
  1373. if (count($customv) == 1 && isset($this->headers[strtolower($customv[0])])) {
  1374. unset($customv);
  1375. }
  1376. }
  1377. else if (isset($rule['test']) && $rule['test'] == 'exists') {
  1378. $custom = (array) $rule['arg'];
  1379. if (count($custom) == 1 && isset($this->headers[strtolower($custom[0])])) {
  1380. unset($custom);
  1381. }
  1382. }
  1383. // custom variable input
  1384. $tout = $this->list_input($id, 'custom_header', $custom, isset($custom),
  1385. $this->error_class($id, 'test', 'header', 'custom_header'), 15) . "\n";
  1386. $tout .= $this->list_input($id, 'custom_var', $customv, isset($customv),
  1387. $this->error_class($id, 'test', 'header', 'custom_var'), 15) . "\n";
  1388. // matching type select (operator)
  1389. $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id,
  1390. 'style' => 'display:' .(!in_array($rule['test'], array('size', 'duplicate')) ? 'inline' : 'none'),
  1391. 'class' => 'operator_selector',
  1392. 'onchange' => 'rule_op_select(this, '.$id.')'));
  1393. $select_op->add(rcube::Q($this->plugin->gettext('filtercontains')), 'contains');
  1394. $select_op->add(rcube::Q($this->plugin->gettext('filternotcontains')), 'notcontains');
  1395. $select_op->add(rcube::Q($this->plugin->gettext('filteris')), 'is');
  1396. $select_op->add(rcube::Q($this->plugin->gettext('filterisnot')), 'notis');
  1397. $select_op->add(rcube::Q($this->plugin->gettext('filterexists')), 'exists');
  1398. $select_op->add(rcube::Q($this->plugin->gettext('filternotexists')), 'notexists');
  1399. $select_op->add(rcube::Q($this->plugin->gettext('filtermatches')), 'matches');
  1400. $select_op->add(rcube::Q($this->plugin->gettext('filternotmatches')), 'notmatches');
  1401. if (in_array('regex', $this->exts)) {
  1402. $select_op->add(rcube::Q($this->plugin->gettext('filterregex')), 'regex');
  1403. $select_op->add(rcube::Q($this->plugin->gettext('filternotregex')), 'notregex');
  1404. }
  1405. if (in_array('relational', $this->exts)) {
  1406. $select_op->add(rcube::Q($this->plugin->gettext('countisgreaterthan')), 'count-gt');
  1407. $select_op->add(rcube::Q($this->plugin->gettext('countisgreaterthanequal')), 'count-ge');
  1408. $select_op->add(rcube::Q($this->plugin->gettext('countislessthan')), 'count-lt');
  1409. $select_op->add(rcube::Q($this->plugin->gettext('countislessthanequal')), 'count-le');
  1410. $select_op->add(rcube::Q($this->plugin->gettext('countequals')), 'count-eq');
  1411. $select_op->add(rcube::Q($this->plugin->gettext('countnotequals')), 'count-ne');
  1412. $select_op->add(rcube::Q($this->plugin->gettext('valueisgreaterthan')), 'value-gt');
  1413. $select_op->add(rcube::Q($this->plugin->gettext('valueisgreaterthanequal')), 'value-ge');
  1414. $select_op->add(rcube::Q($this->plugin->gettext('valueislessthan')), 'value-lt');
  1415. $select_op->add(rcube::Q($this->plugin->gettext('valueislessthanequal')), 'value-le');
  1416. $select_op->add(rcube::Q($this->plugin->gettext('valueequals')), 'value-eq');
  1417. $select_op->add(rcube::Q($this->plugin->gettext('valuenotequals')), 'value-ne');
  1418. }
  1419. $test = self::rule_test($rule);
  1420. $target = '';
  1421. // target(s) input
  1422. if (in_array($rule['test'], array('header', 'address', 'envelope','string'))) {
  1423. $target = $rule['arg2'];
  1424. }
  1425. else if (in_array($rule['test'], array('body', 'date', 'currentdate'))) {
  1426. $target = $rule['arg'];
  1427. }
  1428. else if ($rule['test'] == 'size') {
  1429. if (preg_match('/^([0-9]+)(K|M|G)?$/', $rule['arg'], $matches)) {
  1430. $sizetarget = $matches[1];
  1431. $sizeitem = $matches[2];
  1432. }
  1433. else {
  1434. $sizetarget = $rule['arg'];
  1435. $sizeitem = $rule['item'];
  1436. }
  1437. }
  1438. // (current)date part select
  1439. if (in_array('date', $this->exts) || in_array('currentdate', $this->exts)) {
  1440. $date_parts = array('date', 'iso8601', 'std11', 'julian', 'time',
  1441. 'year', 'month', 'day', 'hour', 'minute', 'second', 'weekday', 'zone');
  1442. $select_dp = new html_select(array('name' => "_rule_date_part[]", 'id' => 'rule_date_part'.$id,
  1443. 'style' => in_array($rule['test'], array('currentdate', 'date')) && !preg_match('/^(notcount|count)-/', $test) ? '' : 'display:none',
  1444. 'class' => 'datepart_selector',
  1445. ));
  1446. foreach ($date_parts as $part) {
  1447. $select_dp->add(rcube::Q($this->plugin->gettext($part)), $part);
  1448. }
  1449. $tout .= $select_dp->show($rule['test'] == 'currentdate' || $rule['test'] == 'date' ? $rule['part'] : '');
  1450. }
  1451. // message test select (e.g. duplicate)
  1452. if (in_array('duplicate', $this->exts)) {
  1453. $select_msg = new html_select(array('name' => "_rule_message[]", 'id' => 'rule_message'.$id,
  1454. 'style' => in_array($rule['test'], array('duplicate')) ? '' : 'display:none',
  1455. 'class' => 'message_selector',
  1456. ));
  1457. $select_msg->add(rcube::Q($this->plugin->gettext('duplicate')), 'duplicate');
  1458. $select_msg->add(rcube::Q($this->plugin->gettext('notduplicate')), 'notduplicate');
  1459. $tout .= $select_msg->show($test);
  1460. }
  1461. $tout .= $select_op->show($test);
  1462. $tout .= $this->list_input($id, 'rule_target', $target,
  1463. $rule['test'] != 'size' && $rule['test'] != 'exists' && $rule['test'] != 'duplicate',
  1464. $this->error_class($id, 'test', 'target', 'rule_target')) . "\n";
  1465. $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id));
  1466. $select_size_op->add(rcube::Q($this->plugin->gettext('filterover')), 'over');
  1467. $select_size_op->add(rcube::Q($this->plugin->gettext('filterunder')), 'under');
  1468. $tout .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">';
  1469. $tout .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : '');
  1470. $tout .= html::tag('input', array(
  1471. 'type' => 'text',
  1472. 'name' => '_rule_size_target[]',
  1473. 'id' => 'rule_size_i'.$id,
  1474. 'value' => $sizetarget,
  1475. 'size' => 10,
  1476. 'class' => $this->error_class($id, 'test', 'sizetarget', 'rule_size_i'),
  1477. ));
  1478. foreach (array('', 'K', 'M', 'G') as $unit) {
  1479. $tout .= html::label(null, html::tag('input', array(
  1480. 'type' => 'radio',
  1481. 'name' => '_rule_size_item['.$id.']',
  1482. 'value' => $unit,
  1483. 'checked' => $sizeitem == $unit,
  1484. 'class' => 'radio',
  1485. )) . $this->rc->gettext($unit . 'B'));
  1486. }
  1487. $tout .= '</div>';
  1488. // Advanced modifiers (address, envelope)
  1489. $select_mod = new html_select(array('name' => "_rule_mod[]", 'id' => 'rule_mod_op'.$id,
  1490. 'onchange' => 'rule_mod_select(' .$id .')'));
  1491. $select_mod->add(rcube::Q($this->plugin->gettext('none')), '');
  1492. $select_mod->add(rcube::Q($this->plugin->gettext('address')), 'address');
  1493. if (in_array('envelope', $this->exts)) {
  1494. $select_mod->add(rcube::Q($this->plugin->gettext('envelope')), 'envelope');
  1495. }
  1496. $select_type = new html_select(array('name' => "_rule_mod_type[]", 'id' => 'rule_mod_type'.$id));
  1497. $select_type->add(rcube::Q($this->plugin->gettext('allparts')), 'all');
  1498. $select_type->add(rcube::Q($this->plugin->gettext('domain')), 'domain');
  1499. $select_type->add(rcube::Q($this->plugin->gettext('localpart')), 'localpart');
  1500. if (in_array('subaddress', $this->exts)) {
  1501. $select_type->add(rcube::Q($this->plugin->gettext('user')), 'user');
  1502. $select_type->add(rcube::Q($this->plugin->gettext('detail')), 'detail');
  1503. }
  1504. $need_mod = !in_array($rule['test'], array('size', 'body', 'date', 'currentdate', 'duplicate', 'string'));
  1505. $mout = '<div id="rule_mod' .$id. '" class="adv"' . (!$need_mod ? ' style="display:none"' : '') . '>';
  1506. $mout .= ' <span class="label">' . rcube::Q($this->plugin->gettext('modifier')) . ' </span>';
  1507. $mout .= $select_mod->show($rule['test']);
  1508. $mout .= ' <span id="rule_mod_type' . $id . '"';
  1509. $mout .= ' style="display:' . (in_array($rule['test'], array('address', 'envelope')) ? 'inline' : 'none') .'">';
  1510. $mout .= rcube::Q($this->plugin->gettext('modtype')) . ' ';
  1511. $mout .= $select_type->show($rule['part']);
  1512. $mout .= '</span>';
  1513. $mout .= '</div>';
  1514. // Advanced modifiers (body transformations)
  1515. $select_mod = new html_select(array('name' => "_rule_trans[]", 'id' => 'rule_trans_op'.$id,
  1516. 'onchange' => 'rule_trans_select(' .$id .')'));
  1517. $select_mod->add(rcube::Q($this->plugin->gettext('text')), 'text');
  1518. $select_mod->add(rcube::Q($this->plugin->gettext('undecoded')), 'raw');
  1519. $select_mod->add(rcube::Q($this->plugin->gettext('contenttype')), 'content');
  1520. $mout .= '<div id="rule_trans' .$id. '" class="adv"' . ($rule['test'] != 'body' ? ' style="display:none"' : '') . '>';
  1521. $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('modifier')) . '</span>';
  1522. $mout .= $select_mod->show($rule['part']);
  1523. $mout .= html::tag('input', array(
  1524. 'type' => 'text',
  1525. 'name' => '_rule_trans_type[]',
  1526. 'id' => 'rule_trans_type'.$id,
  1527. 'value' => is_array($rule['content']) ? implode(',', $rule['content']) : $rule['content'],
  1528. 'size' => 20,
  1529. 'style' => $rule['part'] != 'content' ? 'display:none' : '',
  1530. 'class' => $this->error_class($id, 'test', 'part', 'rule_trans_type'),
  1531. ));
  1532. $mout .= '</div>';
  1533. // Advanced modifiers (body transformations)
  1534. $select_comp = new html_select(array('name' => "_rule_comp[]", 'id' => 'rule_comp_op'.$id));
  1535. $select_comp->add(rcube::Q($this->plugin->gettext('default')), '');
  1536. $select_comp->add(rcube::Q($this->plugin->gettext('octet')), 'i;octet');
  1537. $select_comp->add(rcube::Q($this->plugin->gettext('asciicasemap')), 'i;ascii-casemap');
  1538. if (in_array('comparator-i;ascii-numeric', $this->exts)) {
  1539. $select_comp->add(rcube::Q($this->plugin->gettext('asciinumeric')), 'i;ascii-numeric');
  1540. }
  1541. // Comparators
  1542. $need_comp = $rule['test'] != 'size' && $rule['test'] != 'duplicate';
  1543. $mout .= '<div id="rule_comp' .$id. '" class="adv"' . (!$need_comp ? ' style="display:none"' : '') . '>';
  1544. $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('comparator')) . '</span>';
  1545. $mout .= $select_comp->show($rule['comparator']);
  1546. $mout .= '</div>';
  1547. // Date header
  1548. if (in_array('date', $this->exts)) {
  1549. $mout .= '<div id="rule_date_header_div' .$id. '" class="adv"'. ($rule['test'] != 'date' ? ' style="display:none"' : '') .'>';
  1550. $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('dateheader')) . '</span>';
  1551. $mout .= html::tag('input', array(
  1552. 'type' => 'text',
  1553. 'name' => '_rule_date_header[]',
  1554. 'id' => 'rule_date_header' . $id,
  1555. 'value' => $rule['test'] == 'date' ? $rule['header'] : '',
  1556. 'size' => 15,
  1557. 'class' => $this->error_class($id, 'test', 'dateheader', 'rule_date_header'),
  1558. ));
  1559. $mout .= '</div>';
  1560. }
  1561. // Index
  1562. if (in_array('index', $this->exts)) {
  1563. $need_index = in_array($rule['test'], array('header', ', address', 'date'));
  1564. $mout .= '<div id="rule_index_div' .$id. '" class="adv"'. (!$need_index ? ' style="display:none"' : '') .'>';
  1565. $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('index')) . '</span>';
  1566. $mout .= html::tag('input', array(
  1567. 'type' => 'text',
  1568. 'name' => '_rule_index[]',
  1569. 'id' => 'rule_index' . $id,
  1570. 'value' => $rule['index'] ? intval($rule['index']) : '',
  1571. 'size' => 3,
  1572. 'class' => $this->error_class($id, 'test', 'index', 'rule_index'),
  1573. ));
  1574. $mout .= '&nbsp;' . html::tag('input', array(
  1575. 'type' => 'checkbox',
  1576. 'name' => '_rule_index_last[]',
  1577. 'id' => 'rule_index_last' . $id,
  1578. 'value' => 1,
  1579. 'checked' => !empty($rule['last']),
  1580. ))
  1581. . html::label('rule_index_last' . $id, rcube::Q($this->plugin->gettext('indexlast')));
  1582. $mout .= '</div>';
  1583. }
  1584. // Duplicate
  1585. if (in_array('duplicate', $this->exts)) {
  1586. $need_duplicate = $rule['test'] == 'duplicate';
  1587. $mout .= '<div id="rule_duplicate_div' .$id. '" class="adv"'. (!$need_duplicate ? ' style="display:none"' : '') .'>';
  1588. foreach (array('handle', 'header', 'uniqueid') as $unit) {
  1589. $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('duplicate.handle')) . '</span>';
  1590. $mout .= html::tag('input', array(
  1591. 'type' => 'text',
  1592. 'name' => '_rule_duplicate_' . $unit . '[]',
  1593. 'id' => 'rule_duplicate_' . $unit . $id,
  1594. 'value' => $rule[$unit],
  1595. 'size' => 30,
  1596. 'class' => $this->error_class($id, 'test', 'duplicate_' . $unit, 'rule_duplicate_' . $unit),
  1597. ));
  1598. $mout .= '<br>';
  1599. }
  1600. $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('duplicate.seconds')) . '</span>';
  1601. $mout .= html::tag('input', array(
  1602. 'type' => 'text',
  1603. 'name' => '_rule_duplicate_seconds[]',
  1604. 'id' => 'rule_duplicate_seconds' . $id,
  1605. 'value' => $rule['seconds'],
  1606. 'size' => 6,
  1607. 'class' => $this->error_class($id, 'test', 'duplicate_seconds', 'rule_duplicate_seconds'),
  1608. ));
  1609. $mout .= '&nbsp;' . html::tag('input', array(
  1610. 'type' => 'checkbox',
  1611. 'name' => '_rule_duplicate_last[' . $id . ']',
  1612. 'id' => 'rule_duplicate_last' . $id,
  1613. 'value' => 1,
  1614. 'checked' => !empty($rule['last']),
  1615. ));
  1616. $mout .= html::label('rule_duplicate_last' . $id, rcube::Q($this->plugin->gettext('duplicate.last')));
  1617. $mout .= '</div>';
  1618. }
  1619. // Build output table
  1620. $out = $div ? '<div class="rulerow" id="rulerow' .$id .'">'."\n" : '';
  1621. $out .= '<table><tr>';
  1622. $out .= '<td class="advbutton">';
  1623. $out .= '<a href="#" id="ruleadv' . $id .'" title="'. rcube::Q($this->plugin->gettext('advancedopts')). '"
  1624. onclick="rule_adv_switch(' . $id .', this)" class="show">&nbsp;&nbsp;</a>';
  1625. $out .= '</td>';
  1626. $out .= '<td class="rowactions">' . $aout . '</td>';
  1627. $out .= '<td class="rowtargets">' . $tout . "\n";
  1628. $out .= '<div id="rule_advanced' .$id. '" style="display:none">' . $mout . '</div>';
  1629. $out .= '</td>';
  1630. // add/del buttons
  1631. $out .= '<td class="rowbuttons">';
  1632. $out .= '<a href="#" id="ruleadd' . $id .'" title="'. rcube::Q($this->plugin->gettext('add')). '"
  1633. onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button add"></a>';
  1634. $out .= '<a href="#" id="ruledel' . $id .'" title="'. rcube::Q($this->plugin->gettext('del')). '"
  1635. onclick="rcmail.managesieve_ruledel(' . $id .')" class="button del' . ($rows_num<2 ? ' disabled' : '') .'"></a>';
  1636. $out .= '</td>';
  1637. $out .= '</tr></table>';
  1638. $out .= $div ? "</div>\n" : '';
  1639. return $out;
  1640. }
  1641. private static function rule_test(&$rule)
  1642. {
  1643. // first modify value/count tests with 'not' keyword
  1644. // we'll revert the meaning of operators
  1645. if ($rule['not'] && preg_match('/^(count|value)-([gteqnl]{2})/', $rule['type'], $m)) {
  1646. $rule['not'] = false;
  1647. switch ($m[2]) {
  1648. case 'gt': $rule['type'] = $m[1] . '-le'; break;
  1649. case 'ge': $rule['type'] = $m[1] . '-lt'; break;
  1650. case 'lt': $rule['type'] = $m[1] . '-ge'; break;
  1651. case 'le': $rule['type'] = $m[1] . '-gt'; break;
  1652. case 'eq': $rule['type'] = $m[1] . '-ne'; break;
  1653. case 'ne': $rule['type'] = $m[1] . '-eq'; break;
  1654. }
  1655. }
  1656. else if ($rule['not'] && $rule['test'] == 'size') {
  1657. $rule['not'] = false;
  1658. $rule['type'] = $rule['type'] == 'over' ? 'under' : 'over';
  1659. }
  1660. $set = array('header', 'address', 'envelope', 'body', 'date', 'currentdate', 'string');
  1661. // build test string supported by select element
  1662. if ($rule['size']) {
  1663. $test = $rule['type'];
  1664. }
  1665. else if (in_array($rule['test'], $set)) {
  1666. $test = ($rule['not'] ? 'not' : '') . ($rule['type'] ?: 'is');
  1667. }
  1668. else {
  1669. $test = ($rule['not'] ? 'not' : '') . $rule['test'];
  1670. }
  1671. return $test;
  1672. }
  1673. function action_div($fid, $id, $div=true)
  1674. {
  1675. $action = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id];
  1676. if (isset($this->form['actions'])) {
  1677. $rows_num = count($this->form['actions']);
  1678. }
  1679. else if (isset($this->script[$fid]['actions'])) {
  1680. $rows_num = count($this->script[$fid]['actions']);
  1681. }
  1682. else {
  1683. $rows_num = 0;
  1684. }
  1685. $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : '';
  1686. $out .= '<table><tr><td class="rowactions">';
  1687. // action select
  1688. $select_action = new html_select(array('name' => "_action_type[$id]", 'id' => 'action_type'.$id,
  1689. 'onchange' => 'action_type_select(' .$id .')'));
  1690. if (in_array('fileinto', $this->exts))
  1691. $select_action->add(rcube::Q($this->plugin->gettext('messagemoveto')), 'fileinto');
  1692. if (in_array('fileinto', $this->exts) && in_array('copy', $this->exts))
  1693. $select_action->add(rcube::Q($this->plugin->gettext('messagecopyto')), 'fileinto_copy');
  1694. $select_action->add(rcube::Q($this->plugin->gettext('messageredirect')), 'redirect');
  1695. if (in_array('copy', $this->exts))
  1696. $select_action->add(rcube::Q($this->plugin->gettext('messagesendcopy')), 'redirect_copy');
  1697. if (in_array('reject', $this->exts))
  1698. $select_action->add(rcube::Q($this->plugin->gettext('messagediscard')), 'reject');
  1699. else if (in_array('ereject', $this->exts))
  1700. $select_action->add(rcube::Q($this->plugin->gettext('messagediscard')), 'ereject');
  1701. if (in_array('vacation', $this->exts))
  1702. $select_action->add(rcube::Q($this->plugin->gettext('messagereply')), 'vacation');
  1703. $select_action->add(rcube::Q($this->plugin->gettext('messagedelete')), 'discard');
  1704. if (in_array('imapflags', $this->exts) || in_array('imap4flags', $this->exts)) {
  1705. $select_action->add(rcube::Q($this->plugin->gettext('setflags')), 'setflag');
  1706. $select_action->add(rcube::Q($this->plugin->gettext('addflags')), 'addflag');
  1707. $select_action->add(rcube::Q($this->plugin->gettext('removeflags')), 'removeflag');
  1708. }
  1709. if (in_array('variables', $this->exts)) {
  1710. $select_action->add(rcube::Q($this->plugin->gettext('setvariable')), 'set');
  1711. }
  1712. if (in_array('enotify', $this->exts) || in_array('notify', $this->exts)) {
  1713. $select_action->add(rcube::Q($this->plugin->gettext('notify')), 'notify');
  1714. }
  1715. $select_action->add(rcube::Q($this->plugin->gettext('messagekeep')), 'keep');
  1716. $select_action->add(rcube::Q($this->plugin->gettext('rulestop')), 'stop');
  1717. $select_type = $action['type'];
  1718. if (in_array($action['type'], array('fileinto', 'redirect')) && $action['copy']) {
  1719. $select_type .= '_copy';
  1720. }
  1721. $out .= $select_action->show($select_type);
  1722. $out .= '</td>';
  1723. // actions target inputs
  1724. $out .= '<td class="rowtargets">';
  1725. // force domain selection in redirect email input
  1726. $domains = (array) $this->rc->config->get('managesieve_domains');
  1727. if (!empty($domains)) {
  1728. sort($domains);
  1729. $domain_select = new html_select(array('name' => "_action_target_domain[$id]", 'id' => 'action_target_domain'.$id));
  1730. $domain_select->add(array_combine($domains, $domains));
  1731. if ($action['type'] == 'redirect') {
  1732. $parts = explode('@', $action['target']);
  1733. if (!empty($parts)) {
  1734. $action['domain'] = array_pop($parts);
  1735. $action['target'] = implode('@', $parts);
  1736. }
  1737. }
  1738. }
  1739. // redirect target
  1740. $out .= '<span id="redirect_target' . $id . '" style="white-space:nowrap;'
  1741. . ' display:' . ($action['type'] == 'redirect' ? 'inline' : 'none') . '">'
  1742. . html::tag('input', array(
  1743. 'type' => 'text',
  1744. 'name' => '_action_target[' . $id . ']',
  1745. 'id' => 'action_target' . $id,
  1746. 'value' => $action['type'] == 'redirect' ? $action['target'] : '',
  1747. 'size' => !empty($domains) ? 20 : 35,
  1748. 'class' => $this->error_class($id, 'action', 'target', 'action_target'),
  1749. ));
  1750. $out .= !empty($domains) ? ' @ ' . $domain_select->show($action['domain']) : '';
  1751. $out .= '</span>';
  1752. // (e)reject target
  1753. $out .= html::tag('textarea', array(
  1754. 'name' => '_action_target_area[' . $id . ']',
  1755. 'id' => 'action_target_area' . $id,
  1756. 'rows' => 3,
  1757. 'cols' => 35,
  1758. 'class' => $this->error_class($id, 'action', 'targetarea', 'action_target_area'),
  1759. 'style' => 'display:' . (in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none'),
  1760. ), (in_array($action['type'], array('reject', 'ereject')) ? rcube::Q($action['target'], 'strict', false) : ''));
  1761. // vacation
  1762. $vsec = in_array('vacation-seconds', $this->exts);
  1763. $auto_addr = $this->rc->config->get('managesieve_vacation_addresses_init');
  1764. $from_addr = $this->rc->config->get('managesieve_vacation_from_init');
  1765. if (empty($action)) {
  1766. if ($auto_addr) {
  1767. $action['addresses'] = $this->user_emails();
  1768. }
  1769. if ($from_addr) {
  1770. $default_identity = $this->rc->user->list_emails(true);
  1771. $action['from'] = $default_identity['email'];
  1772. }
  1773. }
  1774. $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">';
  1775. $out .= '<span class="label">'. rcube::Q($this->plugin->gettext('vacationreason')) .'</span><br>';
  1776. $out .= html::tag('textarea', array(
  1777. 'name' => '_action_reason[' . $id . ']',
  1778. 'id' => 'action_reason' . $id,
  1779. 'rows' => 3,
  1780. 'cols' => 35,
  1781. 'class' => $this->error_class($id, 'action', 'reason', 'action_reason'),
  1782. ), rcube::Q($action['reason'], 'strict', false));
  1783. $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('vacationsubject')) . '</span><br>';
  1784. $out .= html::tag('input', array(
  1785. 'type' => 'text',
  1786. 'name' => '_action_subject[' . $id . ']',
  1787. 'id' => 'action_subject' . $id,
  1788. 'value' => is_array($action['subject']) ? implode(', ', $action['subject']) : $action['subject'],
  1789. 'size' => 35,
  1790. 'class' => $this->error_class($id, 'action', 'subject', 'action_subject'),
  1791. ));
  1792. $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('vacationfrom')) . '</span><br>';
  1793. $out .= html::tag('input', array(
  1794. 'type' => 'text',
  1795. 'name' => '_action_from[' . $id . ']',
  1796. 'id' => 'action_from' . $id,
  1797. 'value' => $action['from'],
  1798. 'size' => 35,
  1799. 'class' => $this->error_class($id, 'action', 'from', 'action_from'),
  1800. ));
  1801. $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('vacationaddr')) . '</span><br>';
  1802. $out .= $this->list_input($id, 'action_addresses', $action['addresses'], true,
  1803. $this->error_class($id, 'action', 'addresses', 'action_addresses'), 30)
  1804. . html::a(array('href' => '#', 'onclick' => rcmail_output::JS_OBJECT_NAME . ".managesieve_vacation_addresses($id)"),
  1805. rcube::Q($this->plugin->gettext('filladdresses')));
  1806. $out .= '<br><span class="label">' . rcube::Q($this->plugin->gettext($vsec ? 'vacationinterval' : 'vacationdays')) . '</span><br>';
  1807. $out .= html::tag('input', array(
  1808. 'type' => 'text',
  1809. 'name' => '_action_interval[' . $id . ']',
  1810. 'id' => 'action_interval' . $id,
  1811. 'value' => rcube_sieve_vacation::vacation_interval($action),
  1812. 'size' => 2,
  1813. 'class' => $this->error_class($id, 'action', 'interval', 'action_interval'),
  1814. ));
  1815. if ($vsec) {
  1816. foreach (array('days', 'seconds') as $unit) {
  1817. $out .= '&nbsp;' . html::label(null, html::tag('input', array(
  1818. 'type' => 'radio',
  1819. 'name' => '_action_interval_type[' . $id . ']',
  1820. 'value' => $unit,
  1821. 'checked' => ($unit == 'seconds' && isset($action['seconds'])
  1822. || $unit == 'deys' && !isset($action['seconds'])),
  1823. 'class' => 'radio',
  1824. )) . $this->plugin->gettext($unit));
  1825. }
  1826. }
  1827. $out .= '</div>';
  1828. // flags
  1829. $flags = array(
  1830. 'read' => '\\Seen',
  1831. 'answered' => '\\Answered',
  1832. 'flagged' => '\\Flagged',
  1833. 'deleted' => '\\Deleted',
  1834. 'draft' => '\\Draft',
  1835. );
  1836. $flags_target = (array)$action['target'];
  1837. $flout = '';
  1838. foreach ($flags as $fidx => $flag) {
  1839. $flout .= html::tag('input', array(
  1840. 'type' => 'checkbox',
  1841. 'name' => '_action_flags[' .$id .'][]',
  1842. 'value' => $flag,
  1843. 'checked' => in_array_nocase($flag, $flags_target),
  1844. ))
  1845. . rcube::Q($this->plugin->gettext('flag'.$fidx)) .'<br>';
  1846. }
  1847. $out .= html::div(array(
  1848. 'id' => 'action_flags' . $id,
  1849. 'style' => 'display:' . (preg_match('/^(set|add|remove)flag$/', $action['type']) ? 'inline' : 'none'),
  1850. 'class' => $this->error_class($id, 'action', 'flags', 'action_flags'),
  1851. ), $flout);
  1852. // set variable
  1853. $set_modifiers = array(
  1854. 'lower',
  1855. 'upper',
  1856. 'lowerfirst',
  1857. 'upperfirst',
  1858. 'quotewildcard',
  1859. 'length'
  1860. );
  1861. $out .= '<div id="action_set' .$id.'" style="display:' .($action['type']=='set' ? 'inline' : 'none') .'">';
  1862. foreach (array('name', 'value') as $unit) {
  1863. $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('setvar' . $unit)) . '</span><br>';
  1864. $out .= html::tag('input', array(
  1865. 'type' => 'text',
  1866. 'name' => '_action_var' . $unit . '[' . $id . ']',
  1867. 'id' => 'action_var' . $unit . $id,
  1868. 'value' => $action[$unit],
  1869. 'size' => 35,
  1870. 'class' => $this->error_class($id, 'action', $unit, 'action_var' . $unit),
  1871. ));
  1872. $out .= '<br>';
  1873. }
  1874. $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('setvarmodifiers')) . '</span>';
  1875. foreach ($set_modifiers as $s_m) {
  1876. $s_m_id = 'action_varmods' . $id . $s_m;
  1877. $out .= '<br>' . html::tag('input', array(
  1878. 'type' => 'checkbox',
  1879. 'name' => '_action_varmods[' . $id . '][]',
  1880. 'value' => $s_m,
  1881. 'id' => $s_m_id,
  1882. 'checked' => array_key_exists($s_m, (array)$action) && $action[$s_m],
  1883. ))
  1884. .rcube::Q($this->plugin->gettext('var' . $s_m));
  1885. }
  1886. $out .= '</div>';
  1887. // notify
  1888. $notify_methods = (array) $this->rc->config->get('managesieve_notify_methods');
  1889. $importance_options = $this->notify_importance_options;
  1890. if (empty($notify_methods)) {
  1891. $notify_methods = $this->notify_methods;
  1892. }
  1893. list($method, $target) = explode(':', $action['method'], 2);
  1894. $method = strtolower($method);
  1895. if ($method && !in_array($method, $notify_methods)) {
  1896. $notify_methods[] = $method;
  1897. }
  1898. $select_method = new html_select(array(
  1899. 'name' => "_action_notifymethod[$id]",
  1900. 'id' => "_action_notifymethod$id",
  1901. 'class' => $this->error_class($id, 'action', 'method', 'action_notifymethod'),
  1902. ));
  1903. foreach ($notify_methods as $m_n) {
  1904. $select_method->add(rcube::Q($this->rc->text_exists('managesieve.notifymethod'.$m_n) ? $this->plugin->gettext('managesieve.notifymethod'.$m_n) : $m_n), $m_n);
  1905. }
  1906. $select_importance = new html_select(array(
  1907. 'name' => "_action_notifyimportance[$id]",
  1908. 'id' => "_action_notifyimportance$id",
  1909. 'class' => $this->error_class($id, 'action', 'importance', 'action_notifyimportance')
  1910. ));
  1911. foreach ($importance_options as $io_v => $io_n) {
  1912. $select_importance->add(rcube::Q($this->plugin->gettext($io_n)), $io_v);
  1913. }
  1914. // @TODO: nice UI for mailto: (other methods too) URI parameters
  1915. $out .= '<div id="action_notify' .$id.'" style="display:' .($action['type'] == 'notify' ? 'inline' : 'none') .'">';
  1916. $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('notifytarget')) . '</span><br>';
  1917. $out .= $select_method->show($method);
  1918. $out .= html::tag('input', array(
  1919. 'type' => 'text',
  1920. 'name' => '_action_notifytarget[' . $id . ']',
  1921. 'id' => 'action_notifytarget' . $id,
  1922. 'value' => $target,
  1923. 'size' => 25,
  1924. 'class' => $this->error_class($id, 'action', 'target', 'action_notifytarget'),
  1925. ));
  1926. $out .= '<br><span class="label">'. rcube::Q($this->plugin->gettext('notifymessage')) .'</span><br>';
  1927. $out .= html::tag('textarea', array(
  1928. 'name' => '_action_notifymessage[' . $id . ']',
  1929. 'id' => 'action_notifymessage' . $id,
  1930. 'rows' => 3,
  1931. 'cols' => 35,
  1932. 'class' => $this->error_class($id, 'action', 'message', 'action_notifymessage'),
  1933. ), rcube::Q($action['message'], 'strict', false));
  1934. if (in_array('enotify', $this->exts)) {
  1935. $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('notifyfrom')) . '</span><br>';
  1936. $out .= html::tag('input', array(
  1937. 'type' => 'text',
  1938. 'name' => '_action_notifyfrom[' . $id . ']',
  1939. 'id' => 'action_notifyfrom' . $id,
  1940. 'value' => $action['from'],
  1941. 'size' => 35,
  1942. 'class' => $this->error_class($id, 'action', 'from', 'action_notifyfrom'),
  1943. ));
  1944. }
  1945. $out .= '<br><span class="label">' . rcube::Q($this->plugin->gettext('notifyimportance')) . '</span><br>';
  1946. $out .= $select_importance->show($action['importance'] ? (int) $action['importance'] : 2);
  1947. $out .= '<div id="action_notifyoption_div' . $id . '">'
  1948. .'<span class="label">' . rcube::Q($this->plugin->gettext('notifyoptions')) . '</span><br>'
  1949. .$this->list_input($id, 'action_notifyoption', (array)$action['options'], true,
  1950. $this->error_class($id, 'action', 'options', 'action_notifyoption'), 30) . '</div>';
  1951. $out .= '</div>';
  1952. // mailbox select
  1953. if ($action['type'] == 'fileinto') {
  1954. $mailbox = $this->mod_mailbox($action['target'], 'out');
  1955. // make sure non-existing (or unsubscribed) mailbox is listed (#1489956)
  1956. $additional = array($mailbox);
  1957. }
  1958. else {
  1959. $mailbox = '';
  1960. }
  1961. $select = $this->rc->folder_selector(array(
  1962. 'realnames' => false,
  1963. 'maxlength' => 100,
  1964. 'id' => 'action_mailbox' . $id,
  1965. 'name' => "_action_mailbox[$id]",
  1966. 'style' => 'display:'.(empty($action['type']) || $action['type'] == 'fileinto' ? 'inline' : 'none'),
  1967. 'additional' => $additional,
  1968. ));
  1969. $out .= $select->show($mailbox);
  1970. $out .= '</td>';
  1971. // add/del buttons
  1972. $out .= '<td class="rowbuttons">';
  1973. $out .= '<a href="#" id="actionadd' . $id .'" title="'. rcube::Q($this->plugin->gettext('add')). '"
  1974. onclick="rcmail.managesieve_actionadd(' . $id .')" class="button add"></a>';
  1975. $out .= '<a href="#" id="actiondel' . $id .'" title="'. rcube::Q($this->plugin->gettext('del')). '"
  1976. onclick="rcmail.managesieve_actiondel(' . $id .')" class="button del' . ($rows_num<2 ? ' disabled' : '') .'"></a>';
  1977. $out .= '</td>';
  1978. $out .= '</tr></table>';
  1979. $out .= $div ? "</div>\n" : '';
  1980. return $out;
  1981. }
  1982. protected function genid()
  1983. {
  1984. return preg_replace('/[^0-9]/', '', microtime(true));
  1985. }
  1986. protected function strip_value($str, $allow_html = false, $trim = true)
  1987. {
  1988. if (is_array($str)) {
  1989. foreach ($str as $idx => $val) {
  1990. $val = $this->strip_value($val, $allow_html, $trim);
  1991. if ($val === '') {
  1992. unset($str[$idx]);
  1993. }
  1994. }
  1995. return $str;
  1996. }
  1997. if (!$allow_html) {
  1998. $str = strip_tags($str);
  1999. }
  2000. return $trim ? trim($str) : $str;
  2001. }
  2002. protected function error_class($id, $type, $target, $elem_prefix='')
  2003. {
  2004. // TODO: tooltips
  2005. if (($type == 'test' && ($str = $this->errors['tests'][$id][$target])) ||
  2006. ($type == 'action' && ($str = $this->errors['actions'][$id][$target]))
  2007. ) {
  2008. $this->add_tip($elem_prefix.$id, $str, true);
  2009. return 'error';
  2010. }
  2011. return '';
  2012. }
  2013. protected function add_tip($id, $str, $error=false)
  2014. {
  2015. if ($error) {
  2016. $str = html::span('sieve error', $str);
  2017. }
  2018. $this->tips[] = array($id, $str);
  2019. }
  2020. protected function print_tips()
  2021. {
  2022. if (empty($this->tips)) {
  2023. return;
  2024. }
  2025. $script = rcmail_output::JS_OBJECT_NAME.'.managesieve_tip_register('.json_encode($this->tips).');';
  2026. $this->rc->output->add_script($script, 'foot');
  2027. }
  2028. protected function list_input($id, $name, $value, $enabled, $class, $size=null)
  2029. {
  2030. $value = (array) $value;
  2031. $value = array_map(array('rcube', 'Q'), $value);
  2032. $value = implode("\n", $value);
  2033. return html::tag('textarea', array(
  2034. 'data-type' => 'list',
  2035. 'data-size' => $size,
  2036. 'name' => '_' . $name . '['. $id .']',
  2037. 'id' => $name.$id,
  2038. 'disabled' => !$enabled,
  2039. 'class' => $class,
  2040. 'style' => 'display:none',
  2041. ), $value);
  2042. }
  2043. /**
  2044. * Validate input for date part elements
  2045. */
  2046. protected function validate_date_part($type, $value)
  2047. {
  2048. // we do simple validation of date/part format
  2049. switch ($type) {
  2050. case 'date': // yyyy-mm-dd
  2051. return preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value);
  2052. case 'iso8601':
  2053. return preg_match('/^[0-9: .,ZWT+-]+$/', $value);
  2054. case 'std11':
  2055. return preg_match('/^((Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s+)?[0-9]{1,2}\s+'
  2056. . '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+[0-9]{2,4}\s+'
  2057. . '[0-9]{2}:[0-9]{2}(:[0-9]{2})?\s+([+-]*[0-9]{4}|[A-Z]{1,3})$', $value);
  2058. case 'julian':
  2059. return preg_match('/^[0-9]+$/', $value);
  2060. case 'time': // hh:mm:ss
  2061. return preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $value);
  2062. case 'year':
  2063. return preg_match('/^[0-9]{4}$/', $value);
  2064. case 'month':
  2065. return preg_match('/^[0-9]{2}$/', $value) && $value > 0 && $value < 13;
  2066. case 'day':
  2067. return preg_match('/^[0-9]{2}$/', $value) && $value > 0 && $value < 32;
  2068. case 'hour':
  2069. return preg_match('/^[0-9]{2}$/', $value) && $value < 24;
  2070. case 'minute':
  2071. return preg_match('/^[0-9]{2}$/', $value) && $value < 60;
  2072. case 'second':
  2073. // According to RFC5260, seconds can be from 00 to 60
  2074. return preg_match('/^[0-9]{2}$/', $value) && $value < 61;
  2075. case 'weekday':
  2076. return preg_match('/^[0-9]$/', $value) && $value < 7;
  2077. case 'zone':
  2078. return preg_match('/^[+-][0-9]{4}$/', $value);
  2079. }
  2080. }
  2081. /**
  2082. * Converts mailbox name from/to UTF7-IMAP from/to internal Sieve encoding
  2083. * with delimiter replacement.
  2084. *
  2085. * @param string $mailbox Mailbox name
  2086. * @param string $mode Conversion direction ('in'|'out')
  2087. *
  2088. * @return string Mailbox name
  2089. */
  2090. protected function mod_mailbox($mailbox, $mode = 'out')
  2091. {
  2092. $delimiter = $_SESSION['imap_delimiter'];
  2093. $replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter');
  2094. $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP');
  2095. if ($mode == 'out') {
  2096. $mailbox = rcube_charset::convert($mailbox, $mbox_encoding, 'UTF7-IMAP');
  2097. if ($replace_delimiter && $replace_delimiter != $delimiter)
  2098. $mailbox = str_replace($replace_delimiter, $delimiter, $mailbox);
  2099. }
  2100. else {
  2101. $mailbox = rcube_charset::convert($mailbox, 'UTF7-IMAP', $mbox_encoding);
  2102. if ($replace_delimiter && $replace_delimiter != $delimiter)
  2103. $mailbox = str_replace($delimiter, $replace_delimiter, $mailbox);
  2104. }
  2105. return $mailbox;
  2106. }
  2107. /**
  2108. * List sieve scripts
  2109. *
  2110. * @return array Scripts list
  2111. */
  2112. public function list_scripts()
  2113. {
  2114. if ($this->list !== null) {
  2115. return $this->list;
  2116. }
  2117. $this->list = $this->sieve->get_scripts();
  2118. // Handle active script(s) and list of scripts according to Kolab's KEP:14
  2119. if ($this->rc->config->get('managesieve_kolab_master')) {
  2120. // Skip protected names
  2121. foreach ((array)$this->list as $idx => $name) {
  2122. $_name = strtoupper($name);
  2123. if ($_name == 'MASTER')
  2124. $master_script = $name;
  2125. else if ($_name == 'MANAGEMENT')
  2126. $management_script = $name;
  2127. else if($_name == 'USER')
  2128. $user_script = $name;
  2129. else
  2130. continue;
  2131. unset($this->list[$idx]);
  2132. }
  2133. // get active script(s), read USER script
  2134. if ($user_script) {
  2135. $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
  2136. $filename_regex = '/'.preg_quote($extension, '/').'$/';
  2137. $_SESSION['managesieve_user_script'] = $user_script;
  2138. $this->sieve->load($user_script);
  2139. foreach ($this->sieve->script->as_array() as $rules) {
  2140. foreach ($rules['actions'] as $action) {
  2141. if ($action['type'] == 'include' && empty($action['global'])) {
  2142. $name = preg_replace($filename_regex, '', $action['target']);
  2143. // make sure the script exist
  2144. if (in_array($name, $this->list)) {
  2145. $this->active[] = $name;
  2146. }
  2147. }
  2148. }
  2149. }
  2150. }
  2151. // create USER script if it doesn't exist
  2152. else {
  2153. $content = "# USER Management Script\n"
  2154. ."#\n"
  2155. ."# This script includes the various active sieve scripts\n"
  2156. ."# it is AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY!\n"
  2157. ."#\n"
  2158. ."# For more information, see http://wiki.kolab.org/KEP:14#USER\n"
  2159. ."#\n";
  2160. if ($this->sieve->save_script('USER', $content)) {
  2161. $_SESSION['managesieve_user_script'] = 'USER';
  2162. if (empty($this->master_file))
  2163. $this->sieve->activate('USER');
  2164. }
  2165. }
  2166. }
  2167. else if (!empty($this->list)) {
  2168. // Get active script name
  2169. if ($active = $this->sieve->get_active()) {
  2170. $this->active = array($active);
  2171. }
  2172. // Hide scripts from config
  2173. $exceptions = $this->rc->config->get('managesieve_filename_exceptions');
  2174. if (!empty($exceptions)) {
  2175. $this->list = array_diff($this->list, (array)$exceptions);
  2176. }
  2177. }
  2178. // reindex
  2179. if (!empty($this->list)) {
  2180. $this->list = array_values($this->list);
  2181. }
  2182. return $this->list;
  2183. }
  2184. /**
  2185. * Removes sieve script
  2186. *
  2187. * @param string $name Script name
  2188. *
  2189. * @return bool True on success, False on failure
  2190. */
  2191. public function remove_script($name)
  2192. {
  2193. $result = $this->sieve->remove($name);
  2194. // Kolab's KEP:14
  2195. if ($result && $this->rc->config->get('managesieve_kolab_master')) {
  2196. $this->deactivate_script($name);
  2197. }
  2198. return $result;
  2199. }
  2200. /**
  2201. * Activates sieve script
  2202. *
  2203. * @param string $name Script name
  2204. *
  2205. * @return bool True on success, False on failure
  2206. */
  2207. public function activate_script($name)
  2208. {
  2209. // Kolab's KEP:14
  2210. if ($this->rc->config->get('managesieve_kolab_master')) {
  2211. $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
  2212. $user_script = $_SESSION['managesieve_user_script'];
  2213. // if the script is not active...
  2214. if ($user_script && array_search($name, $this->active) === false) {
  2215. // ...rewrite USER file adding appropriate include command
  2216. if ($this->sieve->load($user_script)) {
  2217. $script = $this->sieve->script->as_array();
  2218. $list = array();
  2219. $regexp = '/' . preg_quote($extension, '/') . '$/';
  2220. // Create new include entry
  2221. $rule = array(
  2222. 'actions' => array(
  2223. 0 => array(
  2224. 'target' => $name.$extension,
  2225. 'type' => 'include',
  2226. 'personal' => true,
  2227. )));
  2228. // get all active scripts for sorting
  2229. foreach ($script as $rid => $rules) {
  2230. foreach ($rules['actions'] as $action) {
  2231. if ($action['type'] == 'include' && empty($action['global'])) {
  2232. $target = $extension ? preg_replace($regexp, '', $action['target']) : $action['target'];
  2233. $list[] = $target;
  2234. }
  2235. }
  2236. }
  2237. $list[] = $name;
  2238. // Sort and find current script position
  2239. asort($list, SORT_LOCALE_STRING);
  2240. $list = array_values($list);
  2241. $index = array_search($name, $list);
  2242. // add rule at the end of the script
  2243. if ($index === false || $index == count($list)-1) {
  2244. $this->sieve->script->add_rule($rule);
  2245. }
  2246. // add rule at index position
  2247. else {
  2248. $script2 = array();
  2249. foreach ($script as $rid => $rules) {
  2250. if ($rid == $index) {
  2251. $script2[] = $rule;
  2252. }
  2253. $script2[] = $rules;
  2254. }
  2255. $this->sieve->script->content = $script2;
  2256. }
  2257. $result = $this->sieve->save();
  2258. if ($result) {
  2259. $this->active[] = $name;
  2260. }
  2261. }
  2262. }
  2263. }
  2264. else {
  2265. $result = $this->sieve->activate($name);
  2266. if ($result)
  2267. $this->active = array($name);
  2268. }
  2269. return $result;
  2270. }
  2271. /**
  2272. * Deactivates sieve script
  2273. *
  2274. * @param string $name Script name
  2275. *
  2276. * @return bool True on success, False on failure
  2277. */
  2278. public function deactivate_script($name)
  2279. {
  2280. // Kolab's KEP:14
  2281. if ($this->rc->config->get('managesieve_kolab_master')) {
  2282. $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
  2283. $user_script = $_SESSION['managesieve_user_script'];
  2284. // if the script is active...
  2285. if ($user_script && ($key = array_search($name, $this->active)) !== false) {
  2286. // ...rewrite USER file removing appropriate include command
  2287. if ($this->sieve->load($user_script)) {
  2288. $script = $this->sieve->script->as_array();
  2289. $name = $name.$extension;
  2290. foreach ($script as $rid => $rules) {
  2291. foreach ($rules['actions'] as $action) {
  2292. if ($action['type'] == 'include' && empty($action['global'])
  2293. && $action['target'] == $name
  2294. ) {
  2295. break 2;
  2296. }
  2297. }
  2298. }
  2299. // Entry found
  2300. if ($rid < count($script)) {
  2301. $this->sieve->script->delete_rule($rid);
  2302. $result = $this->sieve->save();
  2303. if ($result) {
  2304. unset($this->active[$key]);
  2305. }
  2306. }
  2307. }
  2308. }
  2309. }
  2310. else {
  2311. $result = $this->sieve->deactivate();
  2312. if ($result)
  2313. $this->active = array();
  2314. }
  2315. return $result;
  2316. }
  2317. /**
  2318. * Saves current script (adding some variables)
  2319. */
  2320. public function save_script($name = null)
  2321. {
  2322. // Kolab's KEP:14
  2323. if ($this->rc->config->get('managesieve_kolab_master')) {
  2324. $this->sieve->script->set_var('EDITOR', self::PROGNAME);
  2325. $this->sieve->script->set_var('EDITOR_VERSION', self::VERSION);
  2326. }
  2327. return $this->sieve->save($name);
  2328. }
  2329. /**
  2330. * Returns list of rules from the current script
  2331. *
  2332. * @return array List of rules
  2333. */
  2334. public function list_rules()
  2335. {
  2336. $result = array();
  2337. $i = 1;
  2338. foreach ($this->script as $idx => $filter) {
  2339. if (empty($filter['actions'])) {
  2340. continue;
  2341. }
  2342. $fname = $filter['name'] ?: "#$i";
  2343. $result[] = array(
  2344. 'id' => $idx,
  2345. 'name' => $fname,
  2346. 'class' => $filter['disabled'] ? 'disabled' : '',
  2347. );
  2348. $i++;
  2349. }
  2350. return $result;
  2351. }
  2352. /**
  2353. * Initializes internal script data
  2354. */
  2355. protected function init_script()
  2356. {
  2357. if (!$this->sieve->script) {
  2358. return;
  2359. }
  2360. $this->script = $this->sieve->script->as_array();
  2361. $headers = array();
  2362. $exceptions = array('date', 'currentdate', 'size', 'body');
  2363. // find common headers used in script, will be added to the list
  2364. // of available (predefined) headers (#1489271)
  2365. foreach ($this->script as $rule) {
  2366. foreach ((array) $rule['tests'] as $test) {
  2367. if ($test['test'] == 'header') {
  2368. foreach ((array) $test['arg1'] as $header) {
  2369. $lc_header = strtolower($header);
  2370. // skip special names to not confuse UI
  2371. if (in_array($lc_header, $exceptions)) {
  2372. continue;
  2373. }
  2374. if (!isset($this->headers[$lc_header]) && !isset($headers[$lc_header])) {
  2375. $headers[$lc_header] = $header;
  2376. }
  2377. }
  2378. }
  2379. }
  2380. }
  2381. ksort($headers);
  2382. $this->headers += $headers;
  2383. }
  2384. /**
  2385. * Get all e-mail addresses of the user
  2386. */
  2387. protected function user_emails()
  2388. {
  2389. $addresses = $this->rc->user->list_emails();
  2390. foreach ($addresses as $idx => $email) {
  2391. $addresses[$idx] = $email['email'];
  2392. }
  2393. $addresses = array_unique($addresses);
  2394. sort($addresses);
  2395. return $addresses;
  2396. }
  2397. }