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.

vacation_sieve.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------+
  4. | Vacation Module for RoundCube |
  5. | |
  6. | Copyright (C) 2011 André Rodier <andre.rodier@gmail.com> |
  7. | Licensed under the GNU GPL |
  8. +-----------------------------------------------------------------------+
  9. */
  10. define ('PLUGIN_SUCCESS', 0);
  11. define ('PLUGIN_ERROR_DEFAULT', 1);
  12. define ('PLUGIN_ERROR_CONNECT', 2);
  13. define ('PLUGIN_ERROR_PROCESS', 3);
  14. class vacation_sieve extends rcube_plugin
  15. {
  16. public $task = 'settings';
  17. private $app;
  18. private $obj;
  19. private $config;
  20. /*
  21. * Initializes the plugin.
  22. */
  23. public function init()
  24. {
  25. try
  26. {
  27. $this->log_debug('Initialising');
  28. // use jQuery for popup window
  29. $this->require_plugin('jqueryui');
  30. $this->app = rcmail::get_instance();
  31. $this->add_texts('localization/', true);
  32. $this->app->output->add_label('vacation');
  33. $this->register_action('plugin.vacation_sieve', array($this, 'vacation_sieve_init'));
  34. $this->register_action('plugin.vacation_sieve-save', array($this, 'vacation_sieve_save'));
  35. $this->include_script('vacation_sieve.js');
  36. // Load the jQuery UI multiselect widget and i18n
  37. $this->include_script('js/jquery-ui-multiselect-widget/jquery.multiselect.min.js');
  38. $lang_s = substr($_SESSION['language'], 0, 2);
  39. if (file_exists($this->home . "/js/jquery-ui-multiselect-widget/i18n/jquery.multiselect.$lang_s.js")) {
  40. $this->include_script("js/jquery-ui-multiselect-widget/i18n/jquery.multiselect.$lang_s.js");
  41. }
  42. $this->include_stylesheet('styles.css');
  43. $src = $this->app->config->get('skin_path') . '/vacation_sieve.css';
  44. if (file_exists($this->home . '/' . $src)) {
  45. $this->include_stylesheet($src);
  46. }
  47. $this->include_stylesheet('js/jquery-ui-multiselect-widget/jquery.multiselect.css');
  48. # Load default config, and merge with users' settings
  49. $this->load_config('config-default.inc.php');
  50. $this->load_config('config.inc.php');
  51. $this->config = $this->app->config->get('vacation_sieve');
  52. require $this->home . '/model.php';
  53. $this->obj = new model();
  54. if (!empty($this->config['vacation_subject'])) {
  55. $this->obj->vacation_subject = $this->config['vacation_subject'];
  56. }
  57. if (!empty($this->config['vacation_message'])) {
  58. $this->obj->vacation_message = $this->config['vacation_message'];
  59. }
  60. $this->log_debug('Initialised');
  61. }
  62. catch ( Exception $exc )
  63. {
  64. $this->log_error('Fail to initialise: '.$exc->getMessage());
  65. }
  66. }
  67. /*
  68. * Plugin initialization function.
  69. */
  70. public function vacation_sieve_init()
  71. {
  72. try
  73. {
  74. $this->log_debug('Loading data');
  75. $this->read_data();
  76. $this->register_handler('plugin.body', array($this, 'vacation_sieve_form'));
  77. $this->app->output->set_pagetitle($this->gettext('vacation'));
  78. $this->app->output->send('plugin');
  79. }
  80. catch (Exception $exc)
  81. {
  82. $this->log_error('Fail to Loaded: '.$exc->getMessage());
  83. }
  84. }
  85. /*
  86. * Reads plugin data.
  87. */
  88. public function read_data()
  89. {
  90. $this->log_debug('Read data');
  91. # Open the config file, and save the script
  92. $transferParams = $this->config['transfer'];
  93. $path = $transferParams['path'];
  94. $userData = ($this->app->user->data);
  95. $userName = $userData['username'];
  96. list($logon,$domain) = preg_split('/@/', $userName);
  97. # callbacks can be used to transform logon names
  98. if ( isset($this->config['logon_transform']) ) {
  99. $pattern = $this->config['logon_transform'][0];
  100. $replace = $this->config['logon_transform'][1];
  101. $logon = preg_replace($pattern, $replace, $logon);
  102. }
  103. $path = str_replace('<domain>', $domain, $path);
  104. $path = str_replace('<logon>', $logon, $path);
  105. $transferParams['path'] = $path;
  106. # load the transfer class
  107. require 'transfer/factory.php';
  108. $mode = $transferParams['mode'];
  109. $transfer = GetTransferClass($mode, $transferParams);
  110. $script = $transfer->LoadScript($path);
  111. if ( !$script || $script === '' )
  112. {
  113. $msg = sprintf("Cannot load the script from '%s'", $path);
  114. $this->log_error($msg);
  115. }
  116. else
  117. {
  118. require 'scriptmanager.php';
  119. $scriptManager = new ScriptManager();
  120. mb_internal_encoding('UTF-8'); /* */
  121. $script = preg_replace('/.*STARTPARAMS(.*)ENDPARAMS.*/s', '${1}', $script);
  122. $params = $scriptManager->LoadParamsFromScript($script);
  123. $format = $this->app->config->get('date_format');
  124. if(isset($params['enable'])){ $this->obj->set_vacation_enable($params['enable']); }
  125. if(isset($params['start']))
  126. {
  127. $startdate = date_create_from_format($format, $params['start']);
  128. if($startdate) { $this->obj->set_vacation_start(date_timestamp_get($startdate)); }
  129. }
  130. if(isset($params['starttime'])){ $this->obj->set_vacation_starttime($params['starttime']); }
  131. if(isset($params['end']))
  132. {
  133. $enddate = date_create_from_format($format, $params['end']);
  134. if($enddate) { $this->obj->set_vacation_end(date_timestamp_get($enddate)); }
  135. }
  136. if(isset($params['endtime'])){ $this->obj->set_vacation_endtime($params['endtime']); }
  137. if(isset($params['every'])){ $this->obj->set_every($params['every']); }
  138. if(isset($params['subject'])){ $this->obj->set_vacation_subject($params['subject']); }
  139. if(isset($params['appendSubject'])){ $this->obj->set_append_subject($params['appendSubject']); }
  140. if(isset($params['addresses'])){ $this->obj->set_addressed_to($params['addresses']); }
  141. if(isset($params['sendFrom'])){ $this->obj->set_send_from($params['sendFrom']); }
  142. if(isset($params['message'])){ $this->obj->set_vacation_message($params['message']); }
  143. }
  144. return true;
  145. }
  146. /*
  147. * Plugin save function.
  148. */
  149. public function vacation_sieve_save()
  150. {
  151. try
  152. {
  153. $this->log_debug('Saving data');
  154. $this->write_data();
  155. $this->register_handler('plugin.body', array($this, 'vacation_sieve_form'));
  156. $this->app->output->set_pagetitle($this->gettext('vacation'));
  157. rcmail_overwrite_action('plugin.vacation_sieve');
  158. $this->app->output->send('plugin');
  159. $this->api->output->command('display_message', $this->gettext('filtersaved'), 'confirmation');
  160. }
  161. catch ( Exception $exc)
  162. {
  163. $this->log_error('Fail to save: '.$exc->getMessage());
  164. $this->api->output->command('display_message', $this->gettext('filtersaveerror'), 'error');
  165. }
  166. }
  167. /*
  168. * Writes plugin data.
  169. */
  170. public function write_data()
  171. {
  172. $this->log_debug('Write data');
  173. $params = array();
  174. $tmp = get_input_value('_vacation_enable', RCUBE_INPUT_POST);
  175. $params['enable'] = isset($tmp) ? true : false;
  176. $params['start'] = get_input_value('_vacation_start', RCUBE_INPUT_POST);
  177. $params['starttime'] = get_input_value('_vacation_starttime', RCUBE_INPUT_POST);
  178. $params['end'] = get_input_value('_vacation_end', RCUBE_INPUT_POST);
  179. $params['endtime'] = get_input_value('_vacation_endtime', RCUBE_INPUT_POST);
  180. $params['every'] = intval(get_input_value('_every', RCUBE_INPUT_POST));
  181. $params['subject'] = get_input_value('_vacation_subject', RCUBE_INPUT_POST);
  182. $tmp = get_input_value('_append_subject', RCUBE_INPUT_POST);
  183. $params['appendSubject'] = isset($tmp) ? true : false;
  184. unset($tmp);
  185. $params['addresses'] = get_input_value('_addressed_to', RCUBE_INPUT_POST, true);
  186. $params['sendFrom'] = get_input_value('_send_from', RCUBE_INPUT_POST, true);
  187. $params['message'] = get_input_value('_vacation_message', RCUBE_INPUT_POST, true);
  188. require 'scriptmanager.php';
  189. $scriptManager = new ScriptManager();
  190. $script = $scriptManager->BuildScriptFromParams($params);
  191. # Open the config file, and save the script
  192. $transferParams = $this->config['transfer'];
  193. $path = $transferParams['path'];
  194. $userData = ($this->app->user->data);
  195. $userName = $userData['alias'];
  196. if ( empty($userName) )
  197. $userName = $_SESSION['username'];
  198. list($logon,$domain) = preg_split('/@/', $userName);
  199. # callbacks can be used to transform logon names
  200. if ( isset($this->config['logon_transform']) ) {
  201. $pattern = $this->config['logon_transform'][0];
  202. $replace = $this->config['logon_transform'][1];
  203. $logon = preg_replace($pattern, $replace, $logon);
  204. }
  205. $path = str_replace('<domain>', $domain, $path);
  206. $path = str_replace('<logon>', $logon, $path);
  207. $transferParams['path'] = $path;
  208. $transferParams['enable'] = $params['enable'];
  209. # Update the model
  210. $format = $this->app->config->get('date_format');
  211. $this->obj->set_vacation_enable($params['enable']);
  212. $startdate = date_create_from_format($format, $params['start']);
  213. if($startdate) { $this->obj->set_vacation_start(date_timestamp_get($startdate)); }
  214. $this->obj->set_vacation_starttime($params['starttime']);
  215. $enddate = date_create_from_format($format, $params['end']);
  216. if($enddate) { $this->obj->set_vacation_end(date_timestamp_get($enddate)); }
  217. $this->obj->set_vacation_endtime($params['endtime']);
  218. $this->obj->set_every($params['every']);
  219. $this->obj->set_vacation_subject($params['subject']);
  220. $this->obj->set_append_subject($params['appendSubject']);
  221. $this->obj->set_addressed_to($params['addresses']);
  222. $this->obj->set_send_from($params['sendFrom']);
  223. $this->obj->set_vacation_message($params['message']);
  224. # load the transfer class
  225. require 'transfer/factory.php';
  226. $mode = $transferParams['mode'];
  227. $transfer = GetTransferClass($mode, $transferParams);
  228. $success = $transfer->SaveScript($path,$script);
  229. if ( !$success )
  230. {
  231. $msg = sprintf("Cannot save the script in '%s' (%d):\n", $path, $success);
  232. $msg .= ' Error: ' . $transfer->LastError();
  233. $this->log_error($msg);
  234. }
  235. }
  236. /*
  237. * Plugin UI form function.
  238. */
  239. public function vacation_sieve_form()
  240. {
  241. try
  242. {
  243. $table = new html_table(array('cols' => 2, 'class' => 'propform'));
  244. $format = $this->app->config->get('date_format');
  245. # Options
  246. $table->add(array('colspan' => 2, 'class' => 'section-first'),Q($this->gettext('options')));
  247. $table->add_row();
  248. $field_id = 'vacation_enable';
  249. $input_vacationenable = new html_checkbox(array('name' => '_vacation_enable', 'id' => $field_id, 'value' => 1));
  250. $table->add('title', html::label($field_id, Q($this->gettext('vacationenable'))));
  251. $table->add(null, $input_vacationenable->show($this->obj->is_vacation_enable() ? 1 : 0));
  252. $field_id = 'vacation_start';
  253. $input_vacationstart = new html_inputfield(array('name' => '_vacation_start', 'id' => $field_id, 'size' => 10));
  254. $table->add('title', html::label($field_id, Q($this->gettext('period'))));
  255. $vacStart = $this->obj->get_vacation_start();
  256. $hour_text = array();
  257. $hour_value = array();
  258. $wholeDay = Q($this->gettext('wholeday'));
  259. $hour_value[$wholeDay] = '';
  260. $hour_text[$wholeDay] = $wholeDay;
  261. $minHour = $this->config['working_hours'][0];
  262. $maxHour = $this->config['working_hours'][1];
  263. foreach(range($minHour,$maxHour) as $tmp_hour){
  264. $hour_text[$tmp_hour] = sprintf("%02d:00", $tmp_hour);
  265. $hour_value[$tmp_hour] = $tmp_hour;
  266. }
  267. unset($tmp_hour);
  268. $field_id = 'vacation_starttime';
  269. $input_vacationstarttime = new html_select(array('name' => '_vacation_starttime'));
  270. $input_vacationstarttime->add($hour_text, $hour_value);
  271. $field_id = 'vacation_endtime';
  272. $input_vacationendtime = new html_select(array('name' => '_vacation_endtime'));
  273. $input_vacationendtime->add($hour_text, $hour_value);
  274. $field_id = 'vacation_end';
  275. $input_vacationend = new html_inputfield(array('name' => '_vacation_end', 'id' => $field_id, 'size' => 10));
  276. $vacEnd = $this->obj->get_vacation_end();
  277. $periodFields = $this->gettext('vacationfrom').' '.
  278. $input_vacationstart->show(date($format, $vacStart)).' '.
  279. $input_vacationstarttime->show($hour_text[$this->obj->get_vacation_starttime()]).' '.
  280. $this->gettext('vacationto').' '.
  281. $input_vacationend->show(date($format, $vacEnd)).' '.
  282. $input_vacationendtime->show($hour_text[$this->obj->get_vacation_endtime()]);
  283. $table->add(null, $periodFields);
  284. $table->add_row();
  285. $field_id = 'every';
  286. $input_every = new html_inputfield(array('name' => '_every', 'id' => $field_id, 'size' => 5));
  287. $table->add('title', html::label($field_id, Q($this->gettext('frequency'))));
  288. $table->add(null, $this->gettext('answer_no_more_than_every').' '.
  289. $input_every->show($this->obj->get_every()).' '.
  290. $this->gettext('vacationdays'));
  291. $table->add_row();
  292. $identities = $this->get_identities();
  293. $default_identity = key($identities);
  294. $field_id = 'addressed_to';
  295. $input_addressed_to = new html_select(array('name' => '_addressed_to[]', 'id' => $field_id, 'multiple'=>true));
  296. $input_addressed_to->add($identities);
  297. $addressedTo = $this->obj->get_addressed_to();
  298. $table->add('title', html::label($field_id, Q($this->gettext('addressed_to'))));
  299. $table->add(null, $input_addressed_to->show($addressedTo ? $addressedTo : $default_identity));
  300. # Subject field
  301. $table->add(array('colspan' => 2, 'class' => 'section'),Q($this->gettext('subject')));
  302. $table->add_row();
  303. $field_id = 'vacation_subject';
  304. $input_vacationsubject = new html_inputfield(array('name' => '_vacation_subject', 'id' => $field_id, 'size' => 40));
  305. $table->add('title', html::label($field_id, Q($this->gettext('vacationsubject'))));
  306. $table->add(null, $input_vacationsubject->show($this->obj->get_vacation_subject()));
  307. $table->add_row();
  308. $field_id = '_append_subject';
  309. $input_appendsubject = new html_checkbox(array('name' => '_append_subject', 'id' => $field_id, 'value' => 1));
  310. $table->add('title', html::label($field_id, Q($this->gettext('append_subject'))));
  311. $table->add(null, $input_appendsubject->show($this->obj->get_append_subject() ? 1 : 0));
  312. # Message
  313. $table->add(array('colspan' => 2, 'class' => 'section'),Q($this->gettext('vacationmessage')));
  314. $table->add_row();
  315. $field_id = 'send_from';
  316. $input_sendfrom = new html_select(array('name' => '_send_from', 'id' => $field_id));
  317. $input_sendfrom->add($identities);
  318. $sendFrom = $this->obj->get_send_from();
  319. $table->add('title', html::label($field_id, Q($this->gettext('send_from'))));
  320. $table->add(null, $input_sendfrom->show($sendFrom ? $sendFrom : $default_identity));
  321. # Add the HTML Row
  322. $table->add_row();
  323. $field_id = 'vacation_message';
  324. if ($this->config['msg_format'] == 'html')
  325. {
  326. $this->app->output->add_label('converting', 'editorwarning');
  327. rcube_html_editor('identity');
  328. $text_vacationmessage =
  329. new html_textarea(
  330. array('name' => '_vacation_message',
  331. 'id' => $field_id, 'spellcheck' => 1,
  332. 'rows' => 6, 'cols' => 40, 'class' => 'mce_editor'));
  333. }
  334. else
  335. {
  336. $text_vacationmessage =
  337. new html_textarea(array('name' => '_vacation_message',
  338. 'id' => $field_id, 'spellcheck' => 1, 'rows' => 6, 'cols' => 40));
  339. }
  340. #
  341. $table->add('top title', html::label($field_id, Q($this->gettext('vacationmessage'))));
  342. $table->add(null, $text_vacationmessage->show($this->obj->get_vacation_message()));
  343. # Get the HTML
  344. $tableHtml = $table->show();
  345. $submitLine = html::p(null,
  346. $this->app->output->button(array('command' => 'plugin.vacation_sieve-save',
  347. 'type' => 'input', 'class' => 'button mainaction', 'label' => 'save')));
  348. # First line
  349. $boxTitle = html::div(array('id' => "prefs-title", 'class' => 'boxtitle'), $this->gettext('vacation'));
  350. $out = html::div(
  351. array('class' => 'box'),
  352. $boxTitle . html::div(array('class' => "boxcontent"), $tableHtml . $submitLine));
  353. $this->app->output->add_gui_object('vacationsieveform', 'vacation_sieve_form');
  354. return $this->app->output->form_tag(
  355. array(
  356. 'id' => 'vacation_sieve_form',
  357. 'name' => 'vacation_sieve_form',
  358. 'method' => 'post',
  359. 'action' => './?_task=settings&_action=plugin.vacation_sieve-save'),
  360. $out);
  361. }
  362. catch(Exception $exc)
  363. {
  364. $this->log_error('Fail to build form: '.$exc->getMessage());
  365. }
  366. }
  367. private function get_identities()
  368. {
  369. $select = array();
  370. $identities = $this->app->user->list_identities();
  371. foreach ( $identities as $identity )
  372. {
  373. $email = $identity['email'];
  374. $name = $identity['name'];
  375. $text = sprintf("%s <%s>", $name, $email);
  376. $select[$text] = $text;
  377. }
  378. return $select;
  379. }
  380. private function log_debug($msg)
  381. {
  382. if ($this->config['debug'])
  383. {
  384. write_log('vacation_sieve', $msg);
  385. }
  386. }
  387. private function log_error($msg)
  388. {
  389. write_log('vacation_sieve', $msg);
  390. }
  391. }