mailbox.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if(!$mailboxes) {
  100. return false;
  101. }
  102. foreach($mailboxes as $mailbox) {
  103. $useSsl = '';
  104. $noValidCert = '';
  105. if($mailbox->use_ssl) {
  106. $useSsl = '/ssl';
  107. }
  108. if(!$mailbox->valid_ssl) {
  109. $noValidCert = '/novalidate-cert';
  110. }
  111. $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect);
  112. }
  113. return $return;
  114. }
  115. /**
  116. * Get Mailbox by ID
  117. *
  118. *
  119. * @param int $userID ID of Mailbox
  120. * @param bool $connect Whether to connect or just to store data. Default: false
  121. *
  122. * @return Mailbox Selected Mailboxes
  123. *
  124. */
  125. public static function getMailboxByID($mailboxID, $connect = false) {
  126. global $db;
  127. $return = array();
  128. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `id` = " . $mailboxID . ";");
  129. foreach($mailboxes as $mailbox) {
  130. $useSsl = '';
  131. $noValidCert = '';
  132. if($mailbox->use_ssl) {
  133. $useSsl = '/ssl';
  134. }
  135. if(!$mailbox->valid_ssl) {
  136. $noValidCert = '/novalidate-cert';
  137. }
  138. $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect);
  139. }
  140. return $return[0];
  141. }
  142. /**
  143. * Get Mailboxes by User ID
  144. *
  145. *
  146. * @param int $userID ID of user
  147. *
  148. * @return Array(Mailbox) Array with selected Mailboxes
  149. *
  150. */
  151. public static function getMailboxesByUserID($userID) {
  152. global $db;
  153. $return = array();
  154. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userID . ";");
  155. if(!$mailboxes) {
  156. return false;
  157. }
  158. foreach($mailboxes as $mailbox) {
  159. $useSsl = '';
  160. $noValidCert = '';
  161. if($mailbox->use_ssl) {
  162. $useSsl = '/ssl';
  163. }
  164. if(!$mailbox->valid_ssl) {
  165. $noValidCert = '/novalidate-cert';
  166. }
  167. $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, false);
  168. }
  169. return $return;
  170. }
  171. /**
  172. * Get name of Mailbox by MailAccountID
  173. *
  174. *
  175. * @param int $mID MailAccount ID
  176. *
  177. * @return string Name of Mailbox
  178. *
  179. */
  180. public static function getMailboxNameFromMailaccountID($mID) {
  181. global $db;
  182. $mailbox = $db->selectStringQuery("SELECT `username` FROM `mailboxes` WHERE id=" . $mID);
  183. return $mailbox;
  184. }
  185. /**
  186. * Get Mailbox by Username
  187. *
  188. * @param string $mailboxUsername Username of Mailbox
  189. * @param bool $connect Whether to connect
  190. *
  191. * @return Mailbox Selected Mailbox
  192. *
  193. */
  194. public static function getMailboxByUsername($mailboxUsername, $connect = false) {
  195. global $db;
  196. $mailbox = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `username` = '" . $mailboxUsername . "';");
  197. $useSsl = '';
  198. $noValidCert = '';
  199. if($mailbox[0]->use_ssl) {
  200. $useSsl = '/ssl';
  201. }
  202. if(!$mailbox[0]->valid_ssl) {
  203. $noValidCert = '/novalidate-cert';
  204. }
  205. return new Mailbox($mailbox[0]->id, $mailbox[0]->server, $mailbox[0]->port, $mailbox[0]->protocol, $useSsl, $noValidCert, $mailbox[0]->username, $mailbox[0]->password, $connect);
  206. }
  207. }