mailbox.inc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. class Mailbox {
  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. private $connected = false;
  15. public function __construct($id, $hostname, $port, $protocol, $useSsl, $noValidCert, $username, $password, $connect) {
  16. $this->id = $id;
  17. $this->hostname = $hostname;
  18. $this->port = $port;
  19. $this->protocol = $protocol;
  20. $this->useSsl = $useSsl;
  21. $this->noValidCert = $noValidCert;
  22. $this->username = $username;
  23. $this->password = $password;
  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. imap_errors();
  28. imap_alerts();
  29. if($this->mailbox == false) {
  30. $this->connected = false;
  31. } else {
  32. $this->connected = true;
  33. }
  34. }
  35. }
  36. public function changeFolder($folder) {
  37. imap_reopen($this->mailbox, $this->server . $folder);
  38. }
  39. public function listFolders() {
  40. $newFolders = array();
  41. $folders = imap_list($this->mailbox, $this->server, "*");
  42. foreach($folders as $folder) {
  43. $newFolders[] = preg_replace('/(\{(.*)\})(.*)/', '${3}', $folder);
  44. }
  45. $this->folders = $newFolders;
  46. }
  47. public function getID() {
  48. return $this->id;
  49. }
  50. public function getFolders() {
  51. return $this->folders;
  52. }
  53. public function getServer() {
  54. return $this->server;
  55. }
  56. public function getMailbox() {
  57. return $this->mailbox;
  58. }
  59. public function getHostname() {
  60. return $this->hostname;
  61. }
  62. public function getPort() {
  63. return $this->port;
  64. }
  65. public function getProtocol() {
  66. return $this->protocol;
  67. }
  68. public function getUseSsl() {
  69. return $this->useSsl;
  70. }
  71. public function getNoValidCert() {
  72. return $this->noValidCert;
  73. }
  74. public function getUsername() {
  75. return $this->username;
  76. }
  77. public function getConnected() {
  78. return $this->connected;
  79. }
  80. public function getStatus() {
  81. return imap_status($this->mailbox, $this->server, SA_ALL);
  82. }
  83. public function setConnected($connected) {
  84. $this->connected = $connected;
  85. }
  86. /**
  87. * Get all Mailboxes
  88. *
  89. *
  90. * @param bool $connect Whether to connect or just to store data
  91. *
  92. * @return Array(Mailbox) All Mailboxes
  93. *
  94. */
  95. public static function getAllMailboxes($connect = true) {
  96. global $db;
  97. $return = array();
  98. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes`;");
  99. foreach($mailboxes as $mailbox) {
  100. $useSsl = '';
  101. $noValidCert = '';
  102. if($mailbox->use_ssl) {
  103. $useSsl = '/ssl';
  104. }
  105. if(!$mailbox->valid_ssl) {
  106. $noValidCert = '/novalidate-cert';
  107. }
  108. $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect);
  109. }
  110. return $return;
  111. }
  112. /**
  113. * Get Mailbox by ID
  114. *
  115. *
  116. * @param int $userID ID of Mailbox
  117. * @param bool $connect Whether to connect or just to store data. Default: false
  118. *
  119. * @return Mailbox Selected Mailboxes
  120. *
  121. */
  122. public static function getMailboxByID($mailboxID, $connect = false) {
  123. global $db;
  124. $return = array();
  125. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `id` = " . $mailboxID . ";");
  126. foreach($mailboxes as $mailbox) {
  127. $useSsl = '';
  128. $noValidCert = '';
  129. if($mailbox->use_ssl) {
  130. $useSsl = '/ssl';
  131. }
  132. if(!$mailbox->valid_ssl) {
  133. $noValidCert = '/novalidate-cert';
  134. }
  135. $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect);
  136. }
  137. return $return[0];
  138. }
  139. /**
  140. * Get Mailboxes by User ID
  141. *
  142. *
  143. * @param int $userID ID of user
  144. *
  145. * @return Array(Mailbox) Array with selected Mailboxes
  146. *
  147. */
  148. public static function getMailboxesByUserID($userID) {
  149. global $db;
  150. $return = array();
  151. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userID . ";");
  152. foreach($mailboxes as $mailbox) {
  153. $useSsl = '';
  154. $noValidCert = '';
  155. if($mailbox->use_ssl) {
  156. $useSsl = '/ssl';
  157. }
  158. if(!$mailbox->valid_ssl) {
  159. $noValidCert = '/novalidate-cert';
  160. }
  161. $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, false);
  162. }
  163. return $return;
  164. }
  165. /**
  166. * Get name of Mailbox by MailAccountID
  167. *
  168. *
  169. * @param int $mID MailAccount ID
  170. *
  171. * @return string Name of Mailbox
  172. *
  173. */
  174. public static function getMailboxNameFromMailaccountID($mID) {
  175. global $db;
  176. $mailbox = $db->selectStringQuery("SELECT `username` FROM `mailboxes` WHERE id=" . $mID);
  177. return $mailbox;
  178. }
  179. /**
  180. * Get Mailbox by Username
  181. *
  182. * @param string $mailboxUsername Username of Mailbox
  183. * @param bool $connect Whether to connect
  184. *
  185. * @return Mailbox Selected Mailbox
  186. *
  187. */
  188. public static function getMailboxByUsername($mailboxUsername, $connect = false) {
  189. global $db;
  190. $mailbox = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `username` = '" . $mailboxUsername . "';");
  191. $useSsl = '';
  192. $noValidCert = '';
  193. if($mailbox[0]->use_ssl) {
  194. $useSsl = '/ssl';
  195. }
  196. if(!$mailbox[0]->valid_ssl) {
  197. $noValidCert = '/novalidate-cert';
  198. }
  199. return new Mailbox($mailbox[0]->id, $mailbox[0]->server, $mailbox[0]->port, $mailbox[0]->protocol, $useSsl, $noValidCert, $mailbox[0]->username, $mailbox[0]->password, $connect);
  200. }
  201. }