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.

VacationHandler.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. # $Id$
  3. class VacationHandler extends PFAHandler {
  4. /**
  5. * @var string
  6. */
  7. protected $db_table = 'vacation';
  8. /**
  9. * @var string
  10. */
  11. protected $id_field = 'email';
  12. /**
  13. * @var string
  14. */
  15. protected $domain_field = 'domain';
  16. /**
  17. * @return void
  18. */
  19. public function init($id) {
  20. die('VacationHandler is not yet ready to be used with *Handler methods'); # obvious TODO: remove when it's ready ;-)
  21. }
  22. /**
  23. * @return void
  24. */
  25. protected function initStruct() {
  26. $this->struct=array(
  27. # field name allow display in... type $PALANG label $PALANG description default / options / ...
  28. # editing? form list
  29. 'email' => pacol($this->new, 1, 1, 'text', 'pLogin_username' , '' , '' ),
  30. 'domain' => pacol(1, 0, 0, 'text', '' , '' , '' ),
  31. 'subject' => pacol(1, 1, 0, 'text', 'pUsersVacation_subject' , '' , '' ),
  32. 'body' => pacol(1, 1, 0, 'text', 'pUsersVacation_body' , '' , '' ),
  33. 'activefrom' => pacol(1, 1, 1, 'text', 'pUsersVacation_activefrom' , '' , '' ),
  34. 'activeuntil' => pacol(1, 1, 1, 'text', 'pUsersVacation_activeuntil' , '' , '' ),
  35. 'active' => pacol(1, 1, 1, 'bool', 'active' , '' , 1 ),
  36. 'created' => pacol(0, 0, 1, 'ts', 'created' , '' ),
  37. 'modified' => pacol(0, 0, 1, 'ts', 'last_modified' , '' ),
  38. # TODO: add virtual 'notified' column and allow to display who received a vacation response?
  39. );
  40. if ( ! db_pgsql() ) {
  41. $this->struct['cache'] = pacol(0, 0, 0, 'text', '' , '' , '' ); # leftover from 2.2
  42. }
  43. }
  44. /**
  45. * @return void
  46. */
  47. protected function initMsg() {
  48. $this->msg['error_already_exists'] = 'pCreate_mailbox_username_text_error1'; # TODO: better error message
  49. $this->msg['error_does_not_exist'] = 'pCreate_mailbox_username_text_error1'; # TODO: better error message
  50. $this->msg['confirm_delete'] = 'confirm_delete_vacation'; # unused?
  51. if ($this->new) {
  52. $this->msg['logname'] = 'edit_vacation';
  53. $this->msg['store_error'] = 'pVacation_result_error';
  54. $this->msg['successmessage'] = 'pVacation_result_removed'; # TODO: or pVacation_result_added - depends on 'active'... -> we probably need a new message
  55. } else {
  56. $this->msg['logname'] = 'edit_vacation';
  57. $this->msg['store_error'] = 'pVacation_result_error';
  58. $this->msg['successmessage'] = 'pVacation_result_removed'; # TODO: or pVacation_result_added - depends on 'active'... -> we probably need a new message
  59. }
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function webformConfig() {
  65. return array(
  66. # $PALANG labels
  67. 'formtitle_create' => 'pUsersVacation_welcome',
  68. 'formtitle_edit' => 'pUsersVacation_welcome',
  69. 'create_button' => 'save',
  70. # various settings
  71. 'required_role' => 'admin',
  72. 'listview' => 'list-virtual.php',
  73. 'early_init' => 1, # 0 for create-domain
  74. );
  75. }
  76. protected function validate_new_id() {
  77. # vacation can only be enabled if a mailbox with this name exists
  78. if ($this->is_admin) {
  79. $handler = new MailboxHandler(0, $this->admin_username);
  80. if ($handler->init($this->id)) {
  81. return true;
  82. }
  83. } else {
  84. if ($this->id == $this->username) {
  85. return true;
  86. }
  87. }
  88. # still here? This means the mailbox doesn't exist or the admin/user doesn't have permissions to view it
  89. $this->errormsg[] = Config::Lang('invalid_parameter');
  90. return false;
  91. }
  92. /**
  93. * @return bool
  94. */
  95. public function delete() {
  96. $this->errormsg[] = '*** deletion not implemented yet ***';
  97. return false; # XXX function aborts here! XXX
  98. }
  99. /**
  100. * @var string
  101. */
  102. protected $username;
  103. /**
  104. * @param string $username
  105. */
  106. public function __construct($username) {
  107. $this->username = $username;
  108. $this->id = $username;
  109. }
  110. /**
  111. * Removes the autoreply alias etc for this user; namely, if they're away we remove their vacation alias and
  112. * set the vacation table record to false.
  113. * @return boolean true on success.
  114. */
  115. public function remove() {
  116. if (!$this->updateAlias(0)) {
  117. return false;
  118. }
  119. // tidy up vacation table.
  120. $vacation_data = array(
  121. 'active' => db_get_boolean(false),
  122. );
  123. $result = db_update('vacation', 'email', $this->username, $vacation_data);
  124. $result = db_delete('vacation_notification', 'on_vacation', $this->username);
  125. # TODO db_log() call (maybe except if called from set_away?)
  126. /* crap error handling; oh for exceptions... */
  127. return true;
  128. }
  129. /**
  130. * @return boolean true indicates this server supports vacation messages, and users are able to change their own.
  131. */
  132. public function vacation_supported() {
  133. return Config::bool('vacation') && Config::bool('vacation_control');
  134. }
  135. /**
  136. * @return boolean true if on vacation, otherwise false
  137. * Why do we bother storing true/false in the vacation table if the alias dictates it anyway?
  138. */
  139. public function check_vacation() {
  140. $handler = new AliasHandler();
  141. if (!$handler->init($this->id)) {
  142. # print_r($handler->errormsg); # TODO: error handling
  143. return false;
  144. }
  145. if (!$handler->view()) {
  146. # print_r($handler->errormsg); # TODO: error handling
  147. return false;
  148. }
  149. $result = $handler->result();
  150. if ($result['on_vacation']) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * Retrieve information on someone who is on vacation
  157. * @return array|boolean stored information on vacation - array(subject - string, message - string, active - boolean, activeFrom - date, activeUntil - date)
  158. * will return false if no existing data
  159. */
  160. public function get_details() {
  161. $table_vacation = table_by_key('vacation');
  162. $E_username = escape_string($this->username);
  163. $sql = "SELECT * FROM $table_vacation WHERE email = '$E_username'";
  164. $result = db_query($sql);
  165. if ($result['rows'] != 1) {
  166. return false;
  167. }
  168. $row = db_assoc($result['result']);
  169. $boolean = ($row['active'] == db_get_boolean(true));
  170. # TODO: only return true and store the db result array in $this->whatever for consistency with the other classes
  171. return array(
  172. 'subject' => $row['subject'],
  173. 'body' => $row['body'],
  174. 'active' => $boolean ,
  175. 'interval_time' => $row['interval_time'],
  176. 'activeFrom' => $row['activefrom'],
  177. 'activeUntil' => $row['activeuntil'],
  178. );
  179. }
  180. /**
  181. * @param string $subject
  182. * @param string $body
  183. * @param string $interval_time
  184. * @param string $activeFrom - something strtotime understands
  185. * @param string $activeUntil - something strtotime understands
  186. * @return boolean
  187. */
  188. public function set_away($subject, $body, $interval_time, $activeFrom, $activeUntil) {
  189. $this->remove(); // clean out any notifications that might already have been sent.
  190. $E_username = escape_string($this->username);
  191. $activeFrom = date("Y-m-d 00:00:00", strtotime($activeFrom)); # TODO check if result looks like a valid date
  192. $activeUntil = date("Y-m-d 23:59:59", strtotime($activeUntil)); # TODO check if result looks like a valid date
  193. list(/*NULL*/, $domain) = explode('@', $this->username);
  194. $vacation_data = array(
  195. 'email' => $this->username,
  196. 'domain' => $domain,
  197. 'subject' => $subject,
  198. 'body' => $body,
  199. 'interval_time' => $interval_time,
  200. 'active' => db_get_boolean(true),
  201. 'activefrom' => $activeFrom,
  202. 'activeuntil' => $activeUntil,
  203. );
  204. if (! db_pgsql()) {
  205. $vacation_data['cache'] = ''; # leftover from 2.2
  206. }
  207. // is there an entry in the vacaton table for the user, or do we need to insert?
  208. $table_vacation = table_by_key('vacation');
  209. $result = db_query("SELECT * FROM $table_vacation WHERE email = '$E_username'");
  210. if ($result['rows'] == 1) {
  211. $result = db_update('vacation', 'email', $this->username, $vacation_data);
  212. } else {
  213. $result = db_insert('vacation', $vacation_data);
  214. }
  215. # TODO error check
  216. # TODO wrap whole function in db_begin / db_commit (or rollback)?
  217. return $this->updateAlias(1);
  218. }
  219. /**
  220. * add/remove the vacation alias
  221. * @param int $vacationActive
  222. * @return boolean
  223. */
  224. protected function updateAlias($vacationActive) {
  225. $handler = new AliasHandler();
  226. if (!$handler->init($this->id)) {
  227. # print_r($handler->errormsg); # TODO: error handling
  228. return false;
  229. }
  230. $values = array(
  231. 'on_vacation' => $vacationActive,
  232. );
  233. if (!$handler->set($values)) {
  234. # print_r($handler->errormsg); # TODO: error handling
  235. return false;
  236. }
  237. # TODO: supress logging in AliasHandler if called from VacationHandler (VacationHandler should log itsself)
  238. if (!$handler->store()) {
  239. print_r($handler->errormsg); # TODO: error handling
  240. return false;
  241. }
  242. # still here? then everything worked
  243. return true;
  244. }
  245. }
  246. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */