Ejemplo n.º 1
0
 /**
  * @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;
 }
Ejemplo n.º 2
0
 /**
  * @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);
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * @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);
 }
Ejemplo n.º 4
0
 /**
  * @return array
  */
 public function AjaxSystemLogin()
 {
     setcookie('aft-cache-ctrl', '', time() - 3600);
     $sEmail = trim((string) $this->getParamValue('Email', ''));
     $sIncLogin = (string) $this->getParamValue('IncLogin', '');
     $sIncPassword = (string) $this->getParamValue('IncPassword', '');
     $sLanguage = (string) $this->getParamValue('Language', '');
     $bSignMe = '1' === (string) $this->getParamValue('SignMe', '0');
     try {
         \CApi::Plugin()->RunHook('webmail-login-custom-data', array($this->getParamValue('CustomRequestData', null)));
     } catch (\Exception $oException) {
         \CApi::LogEvent(\EEvents::LoginFailed, $sEmail);
         throw $oException;
     }
     $oSettings =& \CApi::GetSettings();
     $sAtDomain = trim($oSettings->GetConf('WebMail/LoginAtDomainValue'));
     if ((\ELoginFormType::Email === (int) $oSettings->GetConf('WebMail/LoginFormType') || \ELoginFormType::Both === (int) $oSettings->GetConf('WebMail/LoginFormType')) && 0 === strlen($sAtDomain) && 0 < strlen($sEmail) && !\MailSo\Base\Validator::EmailString($sEmail)) {
         throw new \ProjectCore\Exceptions\ClientException(\ProjectCore\Notifications::AuthError);
     }
     if (\ELoginFormType::Login === (int) $oSettings->GetConf('WebMail/LoginFormType') && 0 < strlen($sAtDomain)) {
         $sEmail = \api_Utils::GetAccountNameFromEmail($sIncLogin) . '@' . $sAtDomain;
         $sIncLogin = $sEmail;
     }
     if (0 === strlen($sIncPassword) || 0 === strlen($sEmail . $sIncLogin)) {
         \CApi::LogEvent(\EEvents::LoginFailed, $sEmail);
         \CApi::Log($sEmail, \ELogLevel::Full, 'a-');
         \CApi::Log($sIncLogin, \ELogLevel::Full, 'a-');
         \CApi::Log($sIncPassword, \ELogLevel::Full, 'a-');
         throw new \ProjectCore\Exceptions\ClientException(\ProjectCore\Notifications::InvalidInputParameter);
     }
     try {
         if (0 === strlen($sLanguage)) {
             $sLanguage = $this->oApiIntegrator->getLoginLanguage();
         }
         $oAccount = $this->oApiIntegrator->loginToAccount($sEmail, $sIncPassword, $sIncLogin, $sLanguage);
     } catch (\Exception $oException) {
         $iErrorCode = \ProjectCore\Notifications::UnknownError;
         if ($oException instanceof \CApiManagerException) {
             switch ($oException->getCode()) {
                 case \Errs::WebMailManager_AccountDisabled:
                 case \Errs::WebMailManager_AccountWebmailDisabled:
                     $iErrorCode = \ProjectCore\Notifications::AuthError;
                     break;
                 case \Errs::UserManager_AccountAuthenticationFailed:
                 case \Errs::WebMailManager_AccountAuthentication:
                 case \Errs::WebMailManager_NewUserRegistrationDisabled:
                 case \Errs::WebMailManager_AccountCreateOnLogin:
                 case \Errs::Mail_AccountAuthentication:
                 case \Errs::Mail_AccountLoginFailed:
                     $iErrorCode = \ProjectCore\Notifications::AuthError;
                     break;
                 case \Errs::UserManager_AccountConnectToMailServerFailed:
                 case \Errs::WebMailManager_AccountConnectToMailServerFailed:
                 case \Errs::Mail_AccountConnectToMailServerFailed:
                     $iErrorCode = \ProjectCore\Notifications::MailServerError;
                     break;
                 case \Errs::UserManager_LicenseKeyInvalid:
                 case \Errs::UserManager_AccountCreateUserLimitReached:
                 case \Errs::UserManager_LicenseKeyIsOutdated:
                 case \Errs::TenantsManager_AccountCreateUserLimitReached:
                     $iErrorCode = \ProjectCore\Notifications::LicenseProblem;
                     break;
                 case \Errs::Db_ExceptionError:
                     $iErrorCode = \ProjectCore\Notifications::DataBaseError;
                     break;
             }
         }
         \CApi::LogEvent(\EEvents::LoginFailed, $sEmail);
         throw new \ProjectCore\Exceptions\ClientException($iErrorCode, $oException, $oException instanceof \CApiBaseException ? $oException->GetPreviousMessage() : ($oException ? $oException->getMessage() : ''));
     }
     if ($oAccount instanceof \CAccount) {
         $sAuthToken = '';
         $bSetAccountAsLoggedIn = true;
         \CApi::Plugin()->RunHook('api-integrator-set-account-as-logged-in', array(&$bSetAccountAsLoggedIn));
         if ($bSetAccountAsLoggedIn) {
             \CApi::LogEvent(\EEvents::LoginSuccess, $oAccount);
             $sAuthToken = $this->oApiIntegrator->setAccountAsLoggedIn($oAccount, $bSignMe);
         }
         return $this->DefaultResponse($oAccount, __FUNCTION__, array('AuthToken' => $sAuthToken));
     }
     \CApi::LogEvent(\EEvents::LoginFailed, $oAccount);
     throw new \ProjectCore\Exceptions\ClientException(\ProjectCore\Notifications::AuthError);
 }
Ejemplo n.º 5
0
 /**
  * @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 '';
 }
Ejemplo n.º 6
0
 /**
  * @param int $iPort
  *
  * @return bool
  */
 public static function PortInt($iPort)
 {
     return \MailSo\Base\Validator::RangeInt($iPort, 0, 65535);
 }
Ejemplo n.º 7
0
 /**
  * @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);
 }
Ejemplo n.º 8
0
 /**
  * @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);
 }
Ejemplo n.º 9
0
 /**
  * @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);
         }
     }
 }
Ejemplo n.º 10
0
 /**
  * @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;
 }