您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FetchmailHandler.php 8.4KB

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