| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- class Mailbox {
- private $id = NULL;
- private $server = NULL;
- private $hostname = NULL;
- private $port = NULL;
- private $protocol = NULL;
- private $useSsl = NULL;
- private $noValidCert = NULL;
- private $username = NULL;
- private $password = NULL;
- private $mailbox = NULL;
- private $folders = NULL;
- private $connected = false;
- public function __construct($id, $hostname, $port, $protocol, $useSsl, $noValidCert, $username, $password, $connect) {
- $this->id = $id;
- $this->hostname = $hostname;
- $this->port = $port;
- $this->protocol = $protocol;
- $this->useSsl = $useSsl;
- $this->noValidCert = $noValidCert;
- $this->username = $username;
- $this->password = $password;
- $this->server = '{' . $this->hostname . ':' . $this->port . '/' . $this->protocol . $this->useSsl . $this->noValidCert . '}';
- if($connect) {
- $this->mailbox = imap_open($this->server . 'INBOX', $this->username, $this->password); // or error('Failed to connect to IMAP: ' . $this->username . '@' . $this->hostname)
- imap_errors();
- imap_alerts();
- if($this->mailbox == false) {
- $this->connected = false;
- } else {
- $this->connected = true;
- }
- }
- }
- public function changeFolder($folder) {
- imap_reopen($this->mailbox, $this->server . $folder);
- }
- public function listFolders() {
- $newFolders = array();
- $folders = imap_list($this->mailbox, $this->server, "*");
- foreach($folders as $folder) {
- $newFolders[] = preg_replace('/(\{(.*)\})(.*)/', '${3}', $folder);
- }
- $this->folders = $newFolders;
- }
- public function getID() {
- return $this->id;
- }
- public function getFolders() {
- return $this->folders;
- }
- public function getServer() {
- return $this->server;
- }
- public function getMailbox() {
- return $this->mailbox;
- }
- public function getHostname() {
- return $this->hostname;
- }
- public function getPort() {
- return $this->port;
- }
- public function getProtocol() {
- return $this->protocol;
- }
- public function getUseSsl() {
- return $this->useSsl;
- }
- public function getNoValidCert() {
- return $this->noValidCert;
- }
- public function getUsername() {
- return $this->username;
- }
- public function getConnected() {
- return $this->connected;
- }
- public function getStatus() {
- return imap_status($this->mailbox, $this->server, SA_ALL);
- }
- public function setConnected($connected) {
- $this->connected = $connected;
- }
- /**
- * Get all Mailboxes
- *
- *
- * @param bool $connect Whether to connect or just to store data
- *
- * @return Array(Mailbox) All Mailboxes
- *
- */
- public static function getAllMailboxes($connect = true) {
- global $db;
- $return = array();
- $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes`;");
- foreach($mailboxes as $mailbox) {
- $useSsl = '';
- $noValidCert = '';
- if($mailbox->use_ssl) {
- $useSsl = '/ssl';
- }
- if(!$mailbox->valid_ssl) {
- $noValidCert = '/novalidate-cert';
- }
- $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect);
- }
- return $return;
- }
- /**
- * Get Mailbox by ID
- *
- *
- * @param int $userID ID of Mailbox
- * @param bool $connect Whether to connect or just to store data. Default: false
- *
- * @return Mailbox Selected Mailboxes
- *
- */
- public static function getMailboxByID($mailboxID, $connect = false) {
- global $db;
- $return = array();
- $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `id` = " . $mailboxID . ";");
- foreach($mailboxes as $mailbox) {
- $useSsl = '';
- $noValidCert = '';
- if($mailbox->use_ssl) {
- $useSsl = '/ssl';
- }
- if(!$mailbox->valid_ssl) {
- $noValidCert = '/novalidate-cert';
- }
- $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect);
- }
- return $return[0];
- }
- /**
- * Get Mailboxes by User ID
- *
- *
- * @param int $userID ID of user
- *
- * @return Array(Mailbox) Array with selected Mailboxes
- *
- */
- public static function getMailboxesByUserID($userID) {
- global $db;
- $return = array();
- $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userID . ";");
- if(!$mailboxes) {
- return false;
- }
- foreach($mailboxes as $mailbox) {
- $useSsl = '';
- $noValidCert = '';
- if($mailbox->use_ssl) {
- $useSsl = '/ssl';
- }
- if(!$mailbox->valid_ssl) {
- $noValidCert = '/novalidate-cert';
- }
- $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, false);
- }
- return $return;
- }
- /**
- * Get name of Mailbox by MailAccountID
- *
- *
- * @param int $mID MailAccount ID
- *
- * @return string Name of Mailbox
- *
- */
- public static function getMailboxNameFromMailaccountID($mID) {
- global $db;
- $mailbox = $db->selectStringQuery("SELECT `username` FROM `mailboxes` WHERE id=" . $mID);
- return $mailbox;
- }
- /**
- * Get Mailbox by Username
- *
- * @param string $mailboxUsername Username of Mailbox
- * @param bool $connect Whether to connect
- *
- * @return Mailbox Selected Mailbox
- *
- */
- public static function getMailboxByUsername($mailboxUsername, $connect = false) {
- global $db;
- $mailbox = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `username` = '" . $mailboxUsername . "';");
- $useSsl = '';
- $noValidCert = '';
- if($mailbox[0]->use_ssl) {
- $useSsl = '/ssl';
- }
- if(!$mailbox[0]->valid_ssl) {
- $noValidCert = '/novalidate-cert';
- }
- return new Mailbox($mailbox[0]->id, $mailbox[0]->server, $mailbox[0]->port, $mailbox[0]->protocol, $useSsl, $noValidCert, $mailbox[0]->username, $mailbox[0]->password, $connect);
- }
- }
|