FetchmailHandler.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. # $Id$
  3. /**
  4. * Handler for fetchmail jobs
  5. */
  6. class FetchmailHandler extends PFAHandler {
  7. protected $db_table = 'fetchmail';
  8. protected $id_field = 'id';
  9. protected $domain_field = 'domain';
  10. protected $order_by = 'domain, mailbox';
  11. protected function initStruct() {
  12. $src_auth_options = array('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any');
  13. $src_protocol_options = array('POP3','IMAP','POP2','ETRN','AUTO');
  14. $extra = Config::intbool('fetchmail_extra_options');
  15. $this->struct=array(
  16. # field name allow display in... type $PALANG label $PALANG description default / options / ...
  17. # editing? form list
  18. 'id' => pacol(0, 0, 1, 'num' , '' , '' , '', array(),
  19. array('dont_write_to_db' => 1) ),
  20. 'domain' => pacol(0, 0, 1, 'text', '' , '' ),
  21. 'mailbox' => pacol(1, 1, 1, 'enum', 'pFetchmail_field_mailbox' , 'pFetchmail_desc_mailbox' ), # mailbox list
  22. 'src_server' => pacol(1, 1, 1, 'text', 'pFetchmail_field_src_server' , 'pFetchmail_desc_src_server' ),
  23. 'src_port' => pacol(1, 1, 1, 'num', 'pFetchmail_field_src_port' , 'pFetchmail_desc_src_port' , 0 ),
  24. 'src_auth' => pacol(1, 1, 1, 'enum', 'pFetchmail_field_src_auth' , 'pFetchmail_desc_src_auth' , '', $src_auth_options),
  25. 'src_user' => pacol(1, 1, 1, 'text', 'pFetchmail_field_src_user' , 'pFetchmail_desc_src_user' ),
  26. 'src_password' => pacol(1, 1, 0, 'b64p', 'pFetchmail_field_src_password' , 'pFetchmail_desc_src_password' ),
  27. 'src_folder' => pacol(1, 1, 1, 'text', 'pFetchmail_field_src_folder' , 'pFetchmail_desc_src_folder' ),
  28. 'poll_time' => pacol(1, 1, 1, 'num' , 'pFetchmail_field_poll_time' , 'pFetchmail_desc_poll_time' , 10 ),
  29. 'fetchall' => pacol(1, 1, 1, 'bool', 'pFetchmail_field_fetchall' , 'pFetchmail_desc_fetchall' ),
  30. 'keep' => pacol(1, 1, 1, 'bool', 'pFetchmail_field_keep' , 'pFetchmail_desc_keep' ),
  31. 'protocol' => pacol(1, 1, 1, 'enum', 'pFetchmail_field_protocol' , 'pFetchmail_desc_protocol' , '', $src_protocol_options),
  32. 'usessl' => pacol(1, 1, 1, 'bool', 'pFetchmail_field_usessl' , 'pFetchmail_desc_usessl' ),
  33. 'sslcertck' => pacol(1, 1, 1, 'bool', 'pFetchmail_field_sslcertck' , '' ),
  34. 'sslcertpath' => pacol($extra, $extra, $extra, 'text', 'pFetchmail_field_sslcertpath' , '' ),
  35. 'sslfingerprint' => pacol($extra, $extra, $extra, 'text', 'pFetchmail_field_sslfingerprint','' ),
  36. 'extra_options' => pacol($extra, $extra, $extra, 'text', 'pFetchmail_field_extra_options', 'pFetchmail_desc_extra_options' ),
  37. 'mda' => pacol($extra, $extra, $extra, 'text', 'pFetchmail_field_mda' , 'pFetchmail_desc_mda' ),
  38. 'date' => pacol(0, 0, 1, 'text', 'pFetchmail_field_date' , 'pFetchmail_desc_date' , '2000-01-01' ),
  39. 'returned_text' => pacol(0, 0, 1, 'text', 'pFetchmail_field_returned_text', 'pFetchmail_desc_returned_text' ),
  40. 'active' => pacol(1, 1, 1, 'bool', 'active' , '' , 1 ),
  41. 'created' => pacol(0, 0, 0, 'ts', 'created' , '' ),
  42. 'modified' => pacol(0, 0, 1, 'ts', 'last_modified' , '' ),
  43. );
  44. # get list of mailboxes (for currently logged in user)
  45. $handler = new MailboxHandler(0, $this->admin_username);
  46. $handler->getList('1=1');
  47. $this->struct['mailbox']['options'] = array_keys($handler->result);
  48. }
  49. protected function initMsg() {
  50. $this->msg['error_already_exists'] = 'fetchmail_already_exists';
  51. $this->msg['error_does_not_exist'] = 'fetchmail_does_not_exist';
  52. $this->msg['confirm_delete'] = 'confirm_delete_fetchmail';
  53. if ($this->new) {
  54. $this->msg['logname'] = 'create_fetchmail';
  55. $this->msg['store_error'] = 'pFetchmail_database_save_error';
  56. $this->msg['successmessage'] = 'pFetchmail_database_save_success';
  57. } else {
  58. $this->msg['logname'] = 'edit_fetchmail';
  59. $this->msg['store_error'] = 'pFetchmail_database_save_error';
  60. $this->msg['successmessage'] = 'pFetchmail_database_save_success';
  61. }
  62. }
  63. public function webformConfig() {
  64. return array(
  65. # $PALANG labels
  66. 'formtitle_create' => 'pMenu_fetchmail',
  67. 'formtitle_edit' => 'pMenu_fetchmail',
  68. 'create_button' => 'pFetchmail_new_entry',
  69. # various settings
  70. 'required_role' => 'admin',
  71. 'listview' => 'list.php?table=fetchmail',
  72. 'early_init' => 0,
  73. 'prefill' => array('mailbox'),
  74. );
  75. }
  76. protected function setmore(array $values) {
  77. # set domain based on the target mailbox
  78. if ($this->new || isset($values['mailbox'])) {
  79. list(/*NULL*/, $domain) = explode('@', $values['mailbox']);
  80. $this->values['domain'] = $domain;
  81. $this->domain = $domain;
  82. }
  83. }
  84. protected function validate_new_id() {
  85. # auto_increment - any non-empty ID is an error
  86. if ($this->id != '') {
  87. $this->errormsg[$this->id_field] = 'auto_increment value, you must pass an empty string!';
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * @return boolean
  94. */
  95. public function delete() {
  96. if (! $this->view()) {
  97. $this->errormsg[] = Config::lang($this->msg['error_does_not_exist']);
  98. return false;
  99. }
  100. db_delete($this->db_table, $this->id_field, $this->id);
  101. db_log($this->id, 'delete_fetchmail', $this->result['id']);
  102. $this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->result['src_user'] . ' -> ' . $this->result['mailbox']);
  103. return true;
  104. }
  105. /**
  106. * validate src_server - must be non-empty and survive check_domain()
  107. */
  108. protected function _validate_src_server($field, $val) {
  109. if ($val == '') {
  110. $msg = Config::Lang('pFetchmail_server_missing');
  111. } else {
  112. $msg = check_domain($val);
  113. }
  114. if ($msg == '') {
  115. return true;
  116. } else {
  117. $this->errormsg[$field] = $msg;
  118. return false;
  119. }
  120. }
  121. /**
  122. * validate src_user and src_password - must be non-empty
  123. * (we can't assume anything about valid usernames and passwords on remote
  124. * servers, so the validation can't be more strict)
  125. */
  126. protected function _validate_src_user($field, $val) {
  127. if ($val == '') {
  128. $this->errormsg[$field] = Config::lang('pFetchmail_user_missing');
  129. return false;
  130. }
  131. return true;
  132. }
  133. protected function _validate_src_password($field, $val) {
  134. if ($val == '') {
  135. $this->errormsg[$field] = Config::lang('pFetchmail_password_missing');
  136. return false;
  137. }
  138. return true;
  139. }
  140. /**
  141. * validate poll interval - must be numeri and > 0
  142. */
  143. protected function _validate_poll_time($field, $val) {
  144. # must be > 0
  145. if ($val < 1) {
  146. $this->errormsg[$field] = Config::Lang_f('must_be_numeric_bigger_than_null', $field);
  147. return false;
  148. }
  149. return true;
  150. }
  151. public function domain_from_id() {
  152. return '';
  153. }
  154. }
  155. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */