id = $id; $this->hostname = $hostname; $this->port = $port; $this->protocol = $protocol; $this->useSsl = $useSsl; $this->noValidCert = $noValidCert; $this->username = $username; $this->password = $password; // '{' . $mailbox->server . ':' . $mailbox->port . '/' . $mailbox->protocol . $useSsl . $noValidCert . '}' $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); } } 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; } /** * 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); // TODO: Encrypt password } return $return; } /** * Get Mailboxes by User ID * * * @param int $userId ID of user * @param bool $connect Whether to connect or just to store data * * @return Array(Mailbox) Array with selected Mailboxes * */ public static function getMailboxesByUserId($userId, $connect = true) { global $db; $return = array(); $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userId . ";"); 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); // TODO: Encrypt password } 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; } }