/** * @access private */ private function __construct() { $this->oLogger = null; $this->oImapClient = \MailSo\Imap\ImapClient::NewInstance(); $this->oImapClient->SetTimeOuts(10, 30); // TODO }
/** * Returns ImapClient object from cache. * * @param CAccount $oAccount Account object. * @param int $iForceConnectTimeOut = 0. The value overrides connection timeout value. * @param int $iForceSocketTimeOut = 0. The value overrides socket timeout value. * * @return \MailSo\Imap\ImapClient|null */ protected function &_getImapClient(CAccount $oAccount, $iForceConnectTimeOut = 0, $iForceSocketTimeOut = 0) { $oResult = null; if ($oAccount) { $sCacheKey = $oAccount->Email; if (!isset($this->aImapClientCache[$sCacheKey])) { $iConnectTimeOut = CApi::GetConf('socket.connect-timeout', 10); $iSocketTimeOut = CApi::GetConf('socket.get-timeout', 20); $bVerifySsl = !!CApi::GetConf('socket.verify-ssl', false); if (0 < $iForceConnectTimeOut) { $iConnectTimeOut = $iForceConnectTimeOut; } if (0 < $iForceSocketTimeOut) { $iSocketTimeOut = $iForceSocketTimeOut; } CApi::Plugin()->RunHook('webmail-imap-update-socket-timeouts', array(&$iConnectTimeOut, &$iSocketTimeOut)); $this->aImapClientCache[$sCacheKey] = \MailSo\Imap\ImapClient::NewInstance(); $this->aImapClientCache[$sCacheKey]->SetTimeOuts($iConnectTimeOut, $iSocketTimeOut); // TODO $this->aImapClientCache[$sCacheKey]->SetLogger(\CApi::MailSoLogger()); } $oResult =& $this->aImapClientCache[$sCacheKey]; if (!$oResult->IsConnected()) { $oResult->Connect($oAccount->IncomingMailServer, $oAccount->IncomingMailPort, $oAccount->IncomingMailUseSSL ? \MailSo\Net\Enumerations\ConnectionSecurityType::SSL : \MailSo\Net\Enumerations\ConnectionSecurityType::NONE, $bVerifySsl); } if (!$oResult->IsLoggined()) { $sProxyAuthUser = !empty($oAccount->CustomFields['ProxyAuthUser']) ? $oAccount->CustomFields['ProxyAuthUser'] : ''; $oResult->Login($oAccount->IncomingMailLogin, $oAccount->IncomingMailPassword, $sProxyAuthUser); } } return $oResult; }
public function testFolderList() { $rConnect = \MailSo\Base\StreamWrappers\Test::CreateStream('* LIST (\\Noselect) "/" 0' . self::CRLF . '* LIST (\\UnMarked) "/" 0/1' . self::CRLF . '* LIST (\\Noselect) "/" 1' . self::CRLF . '* LIST (\\Noselect) "/" 1/2' . self::CRLF . '* LIST (\\UnMarked) "/" 1/2/3' . self::CRLF . '* LIST (\\UnMarked \\Inbox) "/" INBOX' . self::CRLF . '* LIST (\\UnMarked) "/" "INBOX/XXX XXX"' . self::CRLF . '* LIST (\\UnMarked) "/" &-BT,MAQBDoEM' . self::CRLF . '* LIST (\\UnMarked) "NIL" NILDelimiteFolder' . self::CRLF . '* LIST (\\UnMarked) "" EmptyDelimiteFolder' . self::CRLF . 'TAG1 OK Success' . self::CRLF); $oImapClient = \MailSo\Imap\ImapClient::NewInstance()->TestSetValues($rConnect); $aResult = $oImapClient->FolderList(); $this->assertTrue(is_array($aResult) && 0 < count($aResult)); $this->assertTrue($aResult[0] instanceof \MailSo\Imap\Folder); $this->assertEquals('0', $aResult[0]->FullNameRaw()); $this->assertEquals('0', $aResult[0]->NameRaw()); $this->assertEquals('0/1', $aResult[1]->FullNameRaw()); $this->assertEquals('1', $aResult[1]->NameRaw()); $this->assertEquals('1', $aResult[2]->FullNameRaw()); $this->assertEquals('1/2', $aResult[3]->FullNameRaw()); $this->assertEquals('1/2/3', $aResult[4]->FullNameRaw()); $this->assertEquals('3', $aResult[4]->NameRaw()); $this->assertEquals('INBOX', $aResult[5]->FullNameRaw()); $this->assertEquals('INBOX/XXX XXX', $aResult[6]->FullNameRaw()); $this->assertEquals('XXX XXX', $aResult[6]->NameRaw()); $this->assertEquals('&-BT,MAQBDoEM', $aResult[7]->FullNameRaw()); $this->assertTrue($aResult[5] instanceof \MailSo\Imap\Folder); $this->assertEquals('/', $aResult[5]->Delimiter()); $this->assertEquals(2, count($aResult[5]->FlagsLowerCase())); $this->assertTrue(in_array('\\inbox', $aResult[5]->FlagsLowerCase())); $this->assertTrue($aResult[8] instanceof \MailSo\Imap\Folder); $this->assertEquals('.', $aResult[8]->Delimiter()); $this->assertTrue($aResult[9] instanceof \MailSo\Imap\Folder); $this->assertEquals('.', $aResult[8]->Delimiter()); }
/** * @access private */ private function __construct() { $this->oLogger = null; $this->oImapClient = \MailSo\Imap\ImapClient::NewInstance(); $this->oImapClient->SetTimeOuts(10, \MailSo\Config::$ImapTimeout); }
/** * @return array */ public function AjaxAccountConfigureMail() { $mResult = false; $oAccount = $this->getDefaultAccountFromParam(); $this->populateAccountFromHttpPost(true, $oAccount); $oAccount->AllowMail = true; $iConnectTimeOut = \CApi::GetConf('socket.connect-timeout', 10); $iSocketTimeOut = \CApi::GetConf('socket.get-timeout', 20); \CApi::Plugin()->RunHook('webmail-imap-update-socket-timeouts', array(&$iConnectTimeOut, &$iSocketTimeOut)); $aConnectErrors = array(false, false); $bConnectValid = false; try { $oImapClient = \MailSo\Imap\ImapClient::NewInstance(); $oImapClient->SetTimeOuts($iConnectTimeOut, $iSocketTimeOut); $oImapClient->SetLogger(\CApi::MailSoLogger()); $oImapClient->Connect($oAccount->IncomingMailServer, $oAccount->IncomingMailPort, $oAccount->IncomingMailUseSSL ? \MailSo\Net\Enumerations\ConnectionSecurityType::SSL : \MailSo\Net\Enumerations\ConnectionSecurityType::NONE); $aConnectErrors[0] = true; $sProxyAuthUser = !empty($oAccount->CustomFields['ProxyAuthUser']) ? $oAccount->CustomFields['ProxyAuthUser'] : ''; $oImapClient->Login($oAccount->IncomingMailLogin, $oAccount->IncomingMailPassword, $sProxyAuthUser); $aConnectErrors[1] = true; $bConnectValid = true; $oImapClient->LogoutAndDisconnect(); } catch (\Exception $oExceprion) { } if ($bConnectValid) { if ($this->oApiUsers->updateAccount($oAccount)) { $mResult = true; } else { $iClientErrorCode = \ProjectCore\Notifications::CanNotCreateAccount; $oException = $this->oApiUsers->GetLastException(); if ($oException) { switch ($oException->getCode()) { case \Errs::WebMailManager_AccountDisabled: case \Errs::UserManager_AccountAuthenticationFailed: case \Errs::WebMailManager_AccountAuthentication: case \Errs::WebMailManager_NewUserRegistrationDisabled: case \Errs::WebMailManager_AccountWebmailDisabled: $iClientErrorCode = \ProjectCore\Notifications::AuthError; break; case \Errs::UserManager_AccountConnectToMailServerFailed: case \Errs::WebMailManager_AccountConnectToMailServerFailed: $iClientErrorCode = \ProjectCore\Notifications::MailServerError; break; case \Errs::UserManager_LicenseKeyInvalid: case \Errs::UserManager_AccountCreateUserLimitReached: case \Errs::UserManager_LicenseKeyIsOutdated: $iClientErrorCode = \ProjectCore\Notifications::LicenseProblem; break; case \Errs::Db_ExceptionError: $iClientErrorCode = \ProjectCore\Notifications::DataBaseError; break; } } return $this->FalseResponse($oAccount, __FUNCTION__, $iClientErrorCode); } } else { if ($aConnectErrors[0]) { throw new \CApiManagerException(\Errs::UserManager_AccountAuthenticationFailed); } else { throw new \CApiManagerException(\Errs::UserManager_AccountConnectToMailServerFailed); } } if ($mResult && $oAccount) { $aExtensions = $oAccount->getExtensionList(); $mResult = array('IdAccount' => $oAccount->IdAccount, 'Extensions' => $aExtensions); } else { $mResult = false; } return $this->DefaultResponse($oAccount, __FUNCTION__, $mResult); }
/** * @return array */ public function DoAdminDomainTest() { $this->IsAdminLoggined(); $bImapResult = false; $sImapErrorDesc = ''; $bSmtpResult = false; $sSmtpErrorDesc = ''; $bSieveResult = false; $sSieveErrorDesc = ''; $iImapTime = 0; $iSmtpTime = 0; $iSieveTime = 0; $iConnectionTimeout = 5; $oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de'); if ($oDomain) { try { $oImapClient = \MailSo\Imap\ImapClient::NewInstance()->SetLogger($this->Logger()); $oImapClient->SetTimeOuts($iConnectionTimeout); $iTime = \microtime(true); $oImapClient->Connect($oDomain->IncHost(), $oDomain->IncPort(), $oDomain->IncSecure(), !!$this->Config()->Get('ssl', 'verify_certificate', false), !!$this->Config()->Get('ssl', 'allow_self_signed', true)); $iImapTime = \microtime(true) - $iTime; $oImapClient->Disconnect(); $bImapResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sImapErrorDesc = $oException->getSocketMessage(); if (empty($sImapErrorDesc)) { $sImapErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sImapErrorDesc = $oException->getMessage(); } if ($oDomain->OutUsePhpMail()) { $bSmtpResult = \MailSo\Base\Utils::FunctionExistsAndEnabled('mail'); if (!$bSmtpResult) { $sSmtpErrorDesc = 'PHP: mail() function is undefined'; } } else { try { $oSmtpClient = \MailSo\Smtp\SmtpClient::NewInstance()->SetLogger($this->Logger()); $oSmtpClient->SetTimeOuts($iConnectionTimeout); $iTime = \microtime(true); $oSmtpClient->Connect($oDomain->OutHost(), $oDomain->OutPort(), \MailSo\Smtp\SmtpClient::EhloHelper(), $oDomain->OutSecure(), !!$this->Config()->Get('ssl', 'verify_certificate', false), !!$this->Config()->Get('ssl', 'allow_self_signed', true)); $iSmtpTime = \microtime(true) - $iTime; $oSmtpClient->Disconnect(); $bSmtpResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSmtpErrorDesc = $oException->getSocketMessage(); if (empty($sSmtpErrorDesc)) { $sSmtpErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSmtpErrorDesc = $oException->getMessage(); } } if ($oDomain->UseSieve()) { try { $oSieveClient = \MailSo\Sieve\ManageSieveClient::NewInstance()->SetLogger($this->Logger()); $oSieveClient->SetTimeOuts($iConnectionTimeout); $iTime = \microtime(true); $oSieveClient->Connect($oDomain->SieveHost(), $oDomain->SievePort(), $oDomain->SieveSecure(), !!$this->Config()->Get('ssl', 'verify_certificate', false), !!$this->Config()->Get('ssl', 'allow_self_signed', true)); $iSieveTime = \microtime(true) - $iTime; $oSieveClient->Disconnect(); $bSieveResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSieveErrorDesc = $oException->getSocketMessage(); if (empty($sSieveErrorDesc)) { $sSieveErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSieveErrorDesc = $oException->getMessage(); } } else { $bSieveResult = true; } } return $this->DefaultResponse(__FUNCTION__, array('Imap' => $bImapResult ? true : $sImapErrorDesc, 'Smtp' => $bSmtpResult ? true : $sSmtpErrorDesc, 'Sieve' => $bSieveResult ? true : $sSieveErrorDesc)); }
/** * Creates WebMail account. In most cases, using simpler loginToAccount wrapper is recommended. * * @api * * @param CAccount &$oAccount Object instance with prepopulated account properties. * @param bool $bWithMailConnection Default value is **true**. Defines whether account credentials should be verified against mail server. * * @return bool */ public function createAccount(CAccount &$oAccount, $bWithMailConnection = true) { $bResult = false; try { if ($oAccount->isValid()) { if (!$this->accountExists($oAccount)) { /* @var $oApiLicensingManager CApiLicensingManager */ $oApiLicensingManager = CApi::Manager('licensing'); if ($oApiLicensingManager) { $isValidKey = $oApiLicensingManager->IsValidKey(); if (!$isValidKey && in_array($oApiLicensingManager->GetLicenseType(), array(11, 13, 14))) { throw new CApiManagerException(Errs::UserManager_LicenseKeyIsOutdated); } else { if (!$isValidKey) { throw new CApiManagerException(Errs::UserManager_LicenseKeyInvalid); } } if ($oAccount->IsDefaultAccount && !$oApiLicensingManager->IsValidLimit(true)) { throw new CApiManagerException(Errs::UserManager_AccountCreateUserLimitReached); } } if (0 < $oAccount->Domain->IdTenant && CApi::GetConf('tenant', false)) { /* @var $oTenantsApi CApiTenantsManager */ $oTenantsApi = CApi::Manager('tenants'); if ($oTenantsApi) { /* @var $oTenant CTenant */ $oTenant = $oTenantsApi->getTenantById($oAccount->Domain->IdTenant); if (!$oTenant) { throw new CApiManagerException(Errs::TenantsManager_TenantDoesNotExist); } else { if (0 < $oTenant->UserCountLimit && $oTenant->UserCountLimit <= $oTenant->getUserCount()) { throw new CApiManagerException(Errs::TenantsManager_AccountCreateUserLimitReached); } $this->_validateAccountSubscriptionLimits($oAccount, $oTenant, true); } if (0 < $oTenant->QuotaInMB) { $iSize = $oTenantsApi->getTenantAllocatedSize($oTenant->IdTenant); if ((int) ($oAccount->getRealQuotaSize() / 1024) + $iSize > $oTenant->QuotaInMB) { throw new CApiManagerException(Errs::TenantsManager_QuotaLimitExided); } } } } $bConnectValid = true; $aConnectErrors = array(false, false); if ($bWithMailConnection && !$oAccount->IsMailingList && !$oAccount->IsInternal && !$oAccount->Domain->IsDefaultTenantDomain) { $bConnectValid = false; $iConnectTimeOut = CApi::GetConf('socket.connect-timeout', 10); $iSocketTimeOut = CApi::GetConf('socket.get-timeout', 20); CApi::Plugin()->RunHook('webmail-imap-update-socket-timeouts', array(&$iConnectTimeOut, &$iSocketTimeOut)); try { $oImapClient = \MailSo\Imap\ImapClient::NewInstance(); $oImapClient->SetTimeOuts($iConnectTimeOut, $iSocketTimeOut); $oImapClient->SetLogger(\CApi::MailSoLogger()); $oImapClient->Connect($oAccount->IncomingMailServer, $oAccount->IncomingMailPort, $oAccount->IncomingMailUseSSL ? \MailSo\Net\Enumerations\ConnectionSecurityType::SSL : \MailSo\Net\Enumerations\ConnectionSecurityType::NONE); $aConnectErrors[0] = true; $sProxyAuthUser = !empty($oAccount->CustomFields['ProxyAuthUser']) ? $oAccount->CustomFields['ProxyAuthUser'] : ''; $oImapClient->Login($oAccount->IncomingMailLogin, $oAccount->IncomingMailPassword, $sProxyAuthUser); $aConnectErrors[1] = true; $bConnectValid = true; $oImapClient->LogoutAndDisconnect(); } catch (\Exception $oExceprion) { } } if ($bConnectValid) { if (!$this->oStorage->createAccount($oAccount)) { throw new CApiManagerException(Errs::UserManager_AccountCreateFailed); } if ($oAccount && $oAccount->IsDefaultAccount) { /* @var $oApiContactsManager CApiContactsManager */ $oApiContactsManager = CApi::Manager('contactsmain'); if ($oApiContactsManager && 'db' === CApi::GetManager()->GetStorageByType('contactsmain')) { $oContact = $oApiContactsManager->createContactObject(); $oContact->BusinessEmail = $oAccount->Email; $oContact->PrimaryEmail = EPrimaryEmailType::Business; $oContact->FullName = $oAccount->FriendlyName; $oContact->Type = EContactType::GlobalAccounts; $oContact->IdTypeLink = $oAccount->IdUser; $oContact->IdDomain = 0 < $oAccount->IdDomain ? $oAccount->IdDomain : 0; $oContact->IdTenant = $oAccount->Domain ? $oAccount->Domain->IdTenant : 0; $oApiContactsManager->createContact($oContact); } } CApi::Plugin()->RunHook('statistics.signup', array(&$oAccount)); } else { if ($aConnectErrors[0]) { throw new CApiManagerException(Errs::UserManager_AccountAuthenticationFailed); } else { throw new CApiManagerException(Errs::UserManager_AccountConnectToMailServerFailed); } } } else { throw new CApiManagerException(Errs::UserManager_AccountAlreadyExists); } } $bResult = true; } catch (CApiBaseException $oException) { $bResult = false; $this->setLastException($oException); } return $bResult; }
/** * @return array */ public function DoAdminDomainTest() { $this->IsAdminLoggined(); $bImapResult = false; $sImapErrorDesc = ''; $bSmtpResult = false; $sSmtpErrorDesc = ''; $iImapTime = 0; $iSmtpTime = 0; $oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de'); if ($oDomain) { try { $oImapClient = \MailSo\Imap\ImapClient::NewInstance()->SetLogger($this->Logger()); $oImapClient->SetTimeOuts(5); $iTime = \microtime(true); $oImapClient->Connect($oDomain->IncHost($oDomain->Name()), $oDomain->IncPort(), $oDomain->IncSecure()); $iImapTime = \microtime(true) - $iTime; $oImapClient->Disconnect(); $bImapResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sImapErrorDesc = $oException->getSocketMessage(); if (empty($sImapErrorDesc)) { $sImapErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sImapErrorDesc = $oException->getMessage(); } try { $oSmtpClient = \MailSo\Smtp\SmtpClient::NewInstance()->SetLogger($this->Logger()); $oSmtpClient->SetTimeOuts(5); $iTime = \microtime(true); $oSmtpClient->Connect($oDomain->OutHost($oDomain->Name()), $oDomain->OutPort(), '127.0.0.1', $oDomain->OutSecure()); $iSmtpTime = \microtime(true) - $iTime; $oSmtpClient->Disconnect(); $bSmtpResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSmtpErrorDesc = $oException->getSocketMessage(); if (empty($sSmtpErrorDesc)) { $sSmtpErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSmtpErrorDesc = $oException->getMessage(); } } return $this->DefaultResponse(__FUNCTION__, array('Imap' => $bImapResult ? true : $sImapErrorDesc, 'Smtp' => $bSmtpResult ? true : $sSmtpErrorDesc)); }