imap.inc.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class Imap {
  3. private $id = NULL;
  4. private $server = NULL;
  5. private $hostname = NULL;
  6. private $port = NULL;
  7. private $protocol = NULL;
  8. private $useSsl = NULL;
  9. private $noValidCert = NULL;
  10. private $username = NULL;
  11. private $password = NULL;
  12. private $mailbox = NULL;
  13. private $folders = NULL;
  14. public function __construct($id, $hostname, $port, $protocol, $useSsl, $noValidCert, $username, $password, $connect) {
  15. $this->id = $id;
  16. $this->hostname = $hostname;
  17. $this->port = $port;
  18. $this->protocol = $protocol;
  19. $this->useSsl = $useSsl;
  20. $this->noValidCert = $noValidCert;
  21. $this->username = $username;
  22. $this->password = $password;
  23. // '{' . $mailbox->server . ':' . $mailbox->port . '/' . $mailbox->protocol . $useSsl . $noValidCert . '}'
  24. $this->server = '{' . $this->hostname . ':' . $this->port . '/' . $this->protocol . $this->useSsl . $this->noValidCert . '}';
  25. if($connect) {
  26. $this->mailbox = imap_open($this->server . 'INBOX', $this->username, $this->password) or error('Failed to connect to IMAP: ' . $this->username . '@' . $this->hostname);
  27. }
  28. }
  29. public function changeFolder($folder) {
  30. imap_reopen($this->mailbox, $this->server . $folder);
  31. }
  32. public function listFolders() {
  33. $newFolders = array();
  34. $folders = imap_list($this->mailbox, $this->server, "*");
  35. foreach($folders as $folder) {
  36. $newFolders[] = preg_replace('/(\{(.*)\})(.*)/', '${3}', $folder);
  37. }
  38. $this->folders = $newFolders;
  39. }
  40. public function getId() {
  41. return $this->id;
  42. }
  43. public function getFolders() {
  44. return $this->folders;
  45. }
  46. public function getServer() {
  47. return $this->server;
  48. }
  49. public function getMailbox() {
  50. return $this->mailbox;
  51. }
  52. public function getHostname() {
  53. return $this->hostname;
  54. }
  55. public function getPort() {
  56. return $this->port;
  57. }
  58. public function getProtocol() {
  59. return $this->protocol;
  60. }
  61. public function getUseSsl() {
  62. return $this->useSsl;
  63. }
  64. public function getNoValidCert() {
  65. return $this->noValidCert;
  66. }
  67. public function getUsername() {
  68. return $this->username;
  69. }
  70. /**
  71. * Get all Mailboxes
  72. *
  73. *
  74. * @param bool $connect Whether to connect or just to store data
  75. *
  76. * @return Array(Mailbox) All Mailboxes
  77. *
  78. */
  79. public static function getAllMailboxes($connect = true) {
  80. global $db;
  81. $return = array();
  82. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes`;");
  83. foreach($mailboxes as $mailbox) {
  84. $useSsl = '';
  85. $noValidCert = '';
  86. if($mailbox->use_ssl) {
  87. $useSsl = '/ssl';
  88. }
  89. if(!$mailbox->valid_ssl) {
  90. $noValidCert = '/novalidate-cert';
  91. }
  92. $return[] = new Imap($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect); // TODO: Encrypt password
  93. }
  94. return $return;
  95. }
  96. /**
  97. * Get Mailboxes by User ID
  98. *
  99. *
  100. * @param int $userId ID of user
  101. * @param bool $connect Whether to connect or just to store data
  102. *
  103. * @return Array(Mailbox) Array with selected Mailboxes
  104. *
  105. */
  106. public static function getMailboxesByUserId($userId, $connect = true) {
  107. global $db;
  108. $return = array();
  109. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userId . ";");
  110. foreach($mailboxes as $mailbox) {
  111. $useSsl = '';
  112. $noValidCert = '';
  113. if($mailbox->use_ssl) {
  114. $useSsl = '/ssl';
  115. }
  116. if(!$mailbox->valid_ssl) {
  117. $noValidCert = '/novalidate-cert';
  118. }
  119. $return[] = new Imap($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect); // TODO: Encrypt password
  120. }
  121. return $return;
  122. }
  123. /**
  124. * Get name of Mailbox by MailAccountID
  125. *
  126. *
  127. * @param int $mId MailAccount ID
  128. *
  129. * @return string Name of Mailbox
  130. *
  131. */
  132. public static function getMailboxNameFromMailaccountId($mId) {
  133. global $db;
  134. $mailbox = $db->selectStringQuery("SELECT `username` FROM `mailboxes` WHERE id=" . $mId);
  135. return $mailbox;
  136. }
  137. }