/** * @param string $sCommand * @param array $aParams = array() * * @return void * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception */ public function SendRequest($sCommand, $aParams = array()) { if (!\MailSo\Base\Validator::NotEmptyString($sCommand, true) || !\is_array($aParams)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->IsConnected(true); $sTag = $this->getNewTag(); $sCommand = \trim($sCommand); $sRealCommand = $sTag . ' ' . $sCommand . $this->prepearParamLine($aParams); $sFakeCommand = ''; $aFakeParams = $this->secureRequestParams($sCommand, $aParams); if (null !== $aFakeParams) { $sFakeCommand = $sTag . ' ' . $sCommand . $this->prepearParamLine($aFakeParams); } $this->aTagTimeouts[$sTag] = \microtime(true); $this->sendRaw($sRealCommand, true, $sFakeCommand); }
/** * @param string $sFolderNameInUtf * @param string $sFolderParentFullNameRaw = '' * @param bool $bSubscribeOnCreation = true * * @return \MailSo\Mail\MailClient * * @throws \MailSo\Base\Exceptions\InvalidArgumentException */ public function FolderCreate($sFolderNameInUtf, $sFolderParentFullNameRaw = '', $bSubscribeOnCreation = true) { if (!\MailSo\Base\Validator::NotEmptyString($sFolderNameInUtf, true) || !\is_string($sFolderParentFullNameRaw)) { throw new \MailSo\Base\Exceptions\InvalidArgumentException(); } $sFolderNameInUtf = trim($sFolderNameInUtf); $aFolders = $this->oImapClient->FolderList('', 0 === \strlen(\trim($sFolderParentFullNameRaw)) ? 'INBOX' : $sFolderParentFullNameRaw); if (!\is_array($aFolders) || !isset($aFolders[0])) { // TODO throw new \MailSo\Mail\Exceptions\RuntimeException(0 === \strlen(trim($sFolderParentFullNameRaw)) ? 'Cannot get folder delimiter' : 'Cannot create folder in non-existen parent folder'); } $sDelimiter = $aFolders[0]->Delimiter(); if (0 < \strlen($sDelimiter) && 0 < \strlen(\trim($sFolderParentFullNameRaw))) { $sFolderParentFullNameRaw .= $sDelimiter; } $sFullNameRawToCreate = \MailSo\Base\Utils::ConvertEncoding($sFolderNameInUtf, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP); if (0 < \strlen($sDelimiter) && false !== \strpos($sFullNameRawToCreate, $sDelimiter)) { // TODO throw new \MailSo\Mail\Exceptions\RuntimeException('New folder name contain delimiter'); } $sFullNameRawToCreate = $sFolderParentFullNameRaw . $sFullNameRawToCreate; $this->oImapClient->FolderCreate($sFullNameRawToCreate); if ($bSubscribeOnCreation) { $this->oImapClient->FolderSubscribe($sFullNameRawToCreate); } return $this; }
/** * @param string $sCommand * @param array $aParams = array() * @param bool $bBreakOnLiteral = false * * @return string * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception */ public function SendRequest($sCommand, $aParams = array(), $bBreakOnLiteral = false) { if (!\MailSo\Base\Validator::NotEmptyString($sCommand, true) || !\is_array($aParams)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->IsConnected(true); $sTag = $this->getNewTag(); $sCommand = \trim($sCommand); $sRealCommand = $sTag . ' ' . $sCommand . $this->prepearParamLine($aParams); $sFakeCommand = ''; $aFakeParams = $this->secureRequestParams($sCommand, $aParams); if (null !== $aFakeParams) { $sFakeCommand = $sTag . ' ' . $sCommand . $this->prepearParamLine($aFakeParams); } $this->aTagTimeouts[$sTag] = \microtime(true); if ($bBreakOnLiteral && !\preg_match('/\\d\\+\\}\\r\\n/', $sRealCommand)) { $iPos = \strpos($sRealCommand, "}\r\n"); if (false !== $iPos) { $iFakePos = \strpos($sFakeCommand, "}\r\n"); $this->sendRaw(\substr($sRealCommand, 0, $iPos + 1), true, false !== $iFakePos ? \substr($sFakeCommand, 0, $iFakePos + 3) : ''); return \substr($sRealCommand, $iPos + 3); } } $this->sendRaw($sRealCommand, true, $sFakeCommand); return ''; }
/** * @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 $sEmail * * @return bool */ public static function SimpleEmailString($sEmail) { return \MailSo\Base\Validator::NotEmptyString($sEmail, true) && !!\preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\\.\\+\\-_]*@[a-zA-Z0-9][a-zA-Z0-9\\.\\+\\-_]*$/', $sEmail); }
/** * @param string $sRequest * * @return void * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception */ private function sendRequest($sRequest) { if (!\MailSo\Base\Validator::NotEmptyString($sRequest, true)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->IsConnected(true); $this->sendRaw($sRequest); }
/** * @param string $sEmailAddress * @return \MailSo\Mime\Email * * @throws \MailSo\Base\Exceptions\InvalidArgumentException */ public static function Parse($sEmailAddress) { if (!\MailSo\Base\Validator::NotEmptyString($sEmailAddress, true)) { throw new \MailSo\Base\Exceptions\InvalidArgumentException(); } $sName = ''; $sEmail = ''; $sComment = ''; $bInName = false; $bInAddress = false; $bInComment = false; $iStartIndex = 0; $iEndIndex = 0; $iCurrentIndex = 0; while ($iCurrentIndex < \strlen($sEmailAddress)) { switch ($sEmailAddress[$iCurrentIndex]) { // case '\'': case '"': // $sQuoteChar = $sEmailAddress{$iCurrentIndex}; if (!$bInName && !$bInAddress && !$bInComment) { $bInName = true; $iStartIndex = $iCurrentIndex; } else { if (!$bInAddress && !$bInComment) { $iEndIndex = $iCurrentIndex; $sName = \substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1); $sEmailAddress = \substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1); $iEndIndex = 0; $iCurrentIndex = 0; $iStartIndex = 0; $bInName = false; } } break; case '<': if (!$bInName && !$bInAddress && !$bInComment) { if ($iCurrentIndex > 0 && \strlen($sName) === 0) { $sName = \substr($sEmailAddress, 0, $iCurrentIndex); } $bInAddress = true; $iStartIndex = $iCurrentIndex; } break; case '>': if ($bInAddress) { $iEndIndex = $iCurrentIndex; $sEmail = \substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1); $sEmailAddress = \substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1); $iEndIndex = 0; $iCurrentIndex = 0; $iStartIndex = 0; $bInAddress = false; } break; case '(': if (!$bInName && !$bInAddress && !$bInComment) { $bInComment = true; $iStartIndex = $iCurrentIndex; } break; case ')': if ($bInComment) { $iEndIndex = $iCurrentIndex; $sComment = \substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1); $sEmailAddress = \substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1); $iEndIndex = 0; $iCurrentIndex = 0; $iStartIndex = 0; $bInComment = false; } break; case '\\': $iCurrentIndex++; break; } $iCurrentIndex++; } if (\strlen($sEmail) === 0) { $aRegs = array(''); if (\preg_match('/[^@\\s]+@\\S+/i', $sEmailAddress, $aRegs) && isset($aRegs[0])) { $sEmail = $aRegs[0]; } else { $sName = $sEmailAddress; } } if (\strlen($sEmail) > 0 && \strlen($sName) == 0 && \strlen($sComment) == 0) { $sName = \str_replace($sEmail, '', $sEmailAddress); } $sEmail = \trim(\trim($sEmail), '<>'); $sName = \trim(\trim($sName), '"'); $sName = \trim($sName, '\''); $sComment = \trim(\trim($sComment), '()'); // Remove backslash $sName = \preg_replace('/\\\\(.)/s', '$1', $sName); $sComment = \preg_replace('/\\\\(.)/s', '$1', $sComment); return Email::NewInstance($sEmail, $sName, $sComment); }
/** * @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 $sCommand * @param string $sAddToCommand = '' * @param bool $bSecureLog = false * * @return void * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception */ private function sendRequest($sCommand, $sAddToCommand = '', $bSecureLog = false) { if (!\MailSo\Base\Validator::NotEmptyString($sCommand, true)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } $this->IsConnected(true); $sCommand = \trim($sCommand); $sRealCommand = $sCommand . (0 === \strlen($sAddToCommand) ? '' : ' ' . $sAddToCommand); $sFakeCommand = $bSecureLog ? '********' : ''; $this->iRequestTime = \microtime(true); $this->sendRaw($sRealCommand, true, $sFakeCommand); return $this; }