/** * @param string $sServerName * @param int $iPort = 110 * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT * @param bool $bVerifySsl = false * * @return \MailSo\Pop3\Pop3Client * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Pop3\Exceptions\ResponseException */ public function Connect($sServerName, $iPort = 110, $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false) { $this->iRequestTime = microtime(true); parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl); $this->validateResponse(); if (\MailSo\Net\Enumerations\ConnectionSecurityType::UseStartTLS(in_array('STLS', $this->Capa()), $this->iSecurityType)) { $this->sendRequestWithCheck('STLS'); $this->EnableCrypto(); $this->aCapa = null; } else { if (\MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'), \MailSo\Log\Enumerations\Type::ERROR, true); } } return $this; }
/** * @param string $sServerName * @param int $iPort = 110 * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT * * @return \MailSo\Pop3\Pop3Client * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Pop3\Exceptions\ResponseException */ public function Connect($sServerName, $iPort = 110, $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT) { $this->iRequestTime = microtime(true); parent::Connect($sServerName, $iPort, $iSecurityType); $this->validateResponse(); if (\MailSo\Net\Enumerations\ConnectionSecurityType::UseStartTLS(in_array('STLS', $this->Capa()), $this->iSecurityType)) { $this->sendRequestWithCheck('STLS'); if (!@stream_socket_enable_crypto($this->rConnect, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { $this->writeLogException(new \MailSo\Pop3\Exceptions\RuntimeException('Cannot enable STARTTLS'), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->aCapa = null; } else { if (\MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'), \MailSo\Log\Enumerations\Type::ERROR, true); } } return $this; }
/** * @param string $sServerName * @param int $iPort = 143 * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT * @param bool $bVerifySsl = false * @param bool $bAllowSelfSigned = true * * @return \MailSo\Imap\ImapClient * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception */ public function Connect($sServerName, $iPort = 143, $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false, $bAllowSelfSigned = true) { $this->aTagTimeouts['*'] = \microtime(true); parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl, $bAllowSelfSigned); $this->parseResponseWithValidation('*', true); if (\MailSo\Net\Enumerations\ConnectionSecurityType::UseStartTLS($this->IsSupported('STARTTLS'), $this->iSecurityType)) { $this->SendRequestWithCheck('STARTTLS'); $this->EnableCrypto(); $this->aCapabilityItems = null; } else { if (\MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'), \MailSo\Log\Enumerations\Type::ERROR, true); } } return $this; }
/** * @param string $sServerName * @param int $iPort = 143 * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT * * @return \MailSo\Imap\ImapClient * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception */ public function Connect($sServerName, $iPort = 143, $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT) { $this->aTagTimeouts['*'] = \microtime(true); parent::Connect($sServerName, $iPort, $iSecurityType); $this->parseResponseWithValidation('*', true); if (\MailSo\Net\Enumerations\ConnectionSecurityType::UseStartTLS($this->IsSupported('STARTTLS'), $this->iSecurityType)) { $this->SendRequestWithCheck('STARTTLS'); if (!@\stream_socket_enable_crypto($this->rConnect, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { $this->writeLogException(new \MailSo\Imap\Exceptions\RuntimeException('Cannot enable STARTTLS'), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->aCapabilityItems = null; } else { if (\MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'), \MailSo\Log\Enumerations\Type::ERROR, true); } } return $this; }
/** * @param string $sServerName * @param int $iPort * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT * @param bool $bVerifySsl = false * * @return void * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\SocketAlreadyConnectedException * @throws \MailSo\Net\Exceptions\SocketCanNotConnectToHostException */ public function Connect($sServerName, $iPort, $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false) { if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } if ($this->IsConnected()) { $this->writeLogException(new Exceptions\SocketAlreadyConnectedException(), \MailSo\Log\Enumerations\Type::ERROR, true); } $sServerName = \trim($sServerName); $sErrorStr = ''; $iErrorNo = 0; $this->sConnectedHost = $sServerName; $this->iConnectedPort = $iPort; $this->iSecurityType = $iSecurityType; $this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL($this->iConnectedPort, $this->iSecurityType); $this->sConnectedHost = \in_array(\strtolower(\substr($this->sConnectedHost, 0, 6)), array('ssl://', 'tcp://')) ? \substr($this->sConnectedHost, 6) : $this->sConnectedHost; $this->sConnectedHost = ($this->bSecure ? 'ssl://' : 'tcp://') . $this->sConnectedHost; // $this->sConnectedHost = ($this->bSecure ? 'ssl://' : '').$this->sConnectedHost; if (!$this->bSecure && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported: (' . \implode(', ', \stream_get_transports()) . ')'), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->iStartConnectTime = \microtime(true); $this->writeLog('Start connection to "' . $this->sConnectedHost . ':' . $this->iConnectedPort . '"', \MailSo\Log\Enumerations\Type::NOTE); // $this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort, // $iErrorNo, $sErrorStr, $this->iConnectTimeOut); $bVerifySsl = !!$bVerifySsl; $aStreamContextSettings = array('ssl' => array('verify_host' => $bVerifySsl, 'verify_peer' => $bVerifySsl, 'verify_peer_name' => $bVerifySsl, 'allow_self_signed' => !$bVerifySsl)); \MailSo\Hooks::Run('Net.NetClient.StreamContextSettings/Filter', array(&$aStreamContextSettings)); $rStreamContext = \stream_context_create($aStreamContextSettings); \set_error_handler(array(&$this, 'capturePhpErrorWithException')); try { $this->rConnect = \stream_socket_client($this->sConnectedHost . ':' . $this->iConnectedPort, $iErrorNo, $sErrorStr, $this->iConnectTimeOut, STREAM_CLIENT_CONNECT, $rStreamContext); } catch (\Exception $oExc) { $sErrorStr = $oExc->getMessage(); $iErrorNo = $oExc->getCode(); } \restore_error_handler(); if (!\is_resource($this->rConnect)) { $this->writeLogException(new Exceptions\SocketCanNotConnectToHostException(\MailSo\Base\Utils::ConvertSystemString($sErrorStr), (int) $iErrorNo, 'Can\'t connect to host "' . $this->sConnectedHost . ':' . $this->iConnectedPort . '"'), \MailSo\Log\Enumerations\Type::NOTICE, true); } $this->writeLog(\microtime(true) - $this->iStartConnectTime . ' (raw connection)', \MailSo\Log\Enumerations\Type::TIME); if ($this->rConnect) { if (\MailSo\Base\Utils::FunctionExistsAndEnabled('stream_set_timeout')) { @\stream_set_timeout($this->rConnect, $this->iSocketTimeOut); } } }
/** * @param string $sServerName * @param int $iPort * @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT * * @return void * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\SocketAlreadyConnectedException * @throws \MailSo\Net\Exceptions\SocketCanNotConnectToHostException */ public function Connect($sServerName, $iPort, $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT) { if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } if ($this->IsConnected()) { $this->writeLogException(new Exceptions\SocketAlreadyConnectedException(), \MailSo\Log\Enumerations\Type::ERROR, true); } $sServerName = \trim($sServerName); $sErrorStr = ''; $iErrorNo = 0; $this->iSecurityType = $iSecurityType; $this->iConnectedPort = $iPort; $this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL($iPort, $iSecurityType); $this->sConnectedHost = $this->bSecure ? 'ssl://' . $sServerName : $sServerName; if (!$this->bSecure && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported'), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->iStartConnectTime = \microtime(true); $this->writeLog('Start connection to "' . $this->sConnectedHost . ':' . $this->iConnectedPort . '"', \MailSo\Log\Enumerations\Type::NOTE); $this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort, $iErrorNo, $sErrorStr, $this->iConnectTimeOut); if (!is_resource($this->rConnect)) { $this->writeLogException(new Exceptions\SocketCanNotConnectToHostException($sErrorStr, $iErrorNo, 'Can\'t connect to host "' . $this->sConnectedHost . ':' . $this->iConnectedPort . '"'), \MailSo\Log\Enumerations\Type::NOTICE, true); } $this->writeLog(\microtime(true) - $this->iStartConnectTime . ' (raw connection)', \MailSo\Log\Enumerations\Type::TIME); if ($this->rConnect) { if (\MailSo\Base\Utils::FunctionExistsAndEnabled('stream_set_timeout')) { @\stream_set_timeout($this->rConnect, $this->iSocketTimeOut); } if (\MailSo\Base\Utils::FunctionExistsAndEnabled('stream_set_blocking')) { @\stream_set_blocking($this->rConnect, 1); } } }
/** * @param string $sEhloHost * * @return void */ private function preLoginStartTLSAndEhloProcess($sEhloHost) { if ($this->bHelo) { $this->writeLogException(new Exceptions\RuntimeException('Cannot issue EHLO/HELO to existing session'), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->ehloOrHelo($sEhloHost); if (\MailSo\Net\Enumerations\ConnectionSecurityType::UseStartTLS($this->IsSupported('STARTTLS'), $this->iSecurityType, $this->HasSupportedAuth())) { $this->sendRequestWithCheck('STARTTLS', 220); if (!@\stream_socket_enable_crypto($this->rConnect, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { $this->writeLogException(new \MailSo\Smtp\Exceptions\RuntimeException('Cannot enable STARTTLS'), \MailSo\Log\Enumerations\Type::WARNING, true); } $this->ehloOrHelo($sEhloHost); } else { if (\MailSo\Net\Enumerations\ConnectionSecurityType::STARTTLS === $this->iSecurityType) { $this->writeLogException(new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'), \MailSo\Log\Enumerations\Type::ERROR, true); } } $this->bHelo = true; }