/** * @param $host * @return bool|array */ public function query($host) { if (getmxrr($host, $mx_records, $mx_weight) === false) { $this->logger->debug("no MX records for host <{$host}> found"); return false; } $this->logger->debug("found " . count($mx_records) . " MX records for host <{$host}>"); // TODO: sort by weight return $mx_records; }
/** * @param string $url * @param integer $port * @return bool */ protected function canConnect($url, $port) { $this->logger->debug("attempting to connect to <{$url}> on port <{$port}>"); $fp = fsockopen($url, $port, $error, $errorstr, self::CONNECTION_TIMEOUT); if (is_resource($fp)) { fclose($fp); $this->logger->debug("connection to <{$url}> on port <{$port}> established"); return true; } $this->logger->debug("cannot connect to <{$url}> on port <{$port}>"); return false; }
/** * @param $email * @param $password * @param $name * @param $host * @param $port * @param string|null $encryptionProtocol * @param $user * @return MailAccount */ public function connect($email, $password, $name, $host, $port, $encryptionProtocol, $user) { $account = new MailAccount(); $account->setUserId($this->userId); $account->setName($name); $account->setEmail($email); $account->setInboundHost($host); $account->setInboundPort($port); $account->setInboundSslMode($encryptionProtocol); $account->setInboundUser($user); $password = $this->crypto->encrypt($password); $account->setInboundPassword($password); $a = new Account($account); $a->getImapConnection(); $this->logger->info("Test-Account-Successful: {$this->userId}, {$host}, {$port}, {$user}, {$encryptionProtocol}"); return $account; }
/** * @NoAdminRequired * * @param int $accountId * @param string $folderId * @param string $id * @return JSONResponse */ public function destroy($accountId, $folderId, $id) { $this->logger->debug("deleting message <{$id}> of folder <{$folderId}>, account <{$accountId}>"); try { $account = $this->getAccount($accountId); $account->deleteMessage(base64_decode($folderId), $id); return new JSONResponse(); } catch (DoesNotExistException $e) { $this->logger->error("could not delete message <{$id}> of folder <{$folderId}>, " . "account <{$accountId}> because it does not exist"); return new JSONResponse([], 404); } }
/** * @param $email * @param $password * @param $name * @return MailAccount|null */ public function createAutoDetected($email, $password, $name) { // splitting the email address into user and host part // TODO: use horde libs for email address parsing list(, $host) = explode("@", $email); $ispdb = $this->mozillaIspDb->query($host); if (!empty($ispdb)) { $account = null; if (isset($ispdb['imap'])) { foreach ($ispdb['imap'] as $imap) { $host = $imap['hostname']; $port = $imap['port']; $encryptionProtocol = null; if ($imap['socketType'] === 'SSL') { $encryptionProtocol = 'ssl'; } if ($imap['socketType'] === 'STARTTLS') { $encryptionProtocol = 'tls'; } if ($imap['username'] === '%EMAILADDRESS%') { $user = $email; } elseif ($imap['username'] === '%EMAILLOCALPART%') { list($user, ) = explode("@", $email); } else { $this->logger->info("Unknown username variable: " . $imap['username']); return null; } try { $account = $this->imapConnector->connect($email, $password, $name, $host, $port, $encryptionProtocol, $user); break; } catch (\Horde_Imap_Client_Exception $e) { $error = $e->getMessage(); $this->logger->info("Test-Account-Failed: {$this->userId}, {$host}, {$port}, {$user}, {$encryptionProtocol} -> {$error}"); } } } if (!is_null($account)) { foreach ($ispdb['smtp'] as $smtp) { try { if ($smtp['username'] === '%EMAILADDRESS%') { $user = $email; } elseif ($smtp['username'] === '%EMAILLOCALPART%') { list($user, ) = explode("@", $email); } else { $this->logger->info("Unknown username variable: " . $smtp['username']); return null; } $account->setOutboundHost($smtp['hostname']); $account->setOutboundPort($smtp['port']); $password = $this->crypto->encrypt($password); $account->setOutboundPassword($password); $account->setOutboundUser($user); $account->setOutboundSslMode(strtolower($smtp['socketType'])); $a = new Account($account); $smtp = $a->createTransport(); if ($smtp instanceof Horde_Mail_Transport_Smtphorde) { $smtp->getSMTPObject(); } break; } catch (\PEAR_Exception $ex) { $error = $ex->getMessage(); $this->logger->info("Test-Account-Failed(smtp): {$error}"); } } return $account; } } $account = $this->detectImapAndSmtp($email, $password, $name); if (!is_null($account)) { return $account; } return null; }