Exemplo n.º 1
0
 /**
  * @param resource $rRead
  * @param array $aWrite
  * @param int $iBufferLen = 8192
  * @param bool $bResetTimeLimit = true
  * @param bool $bFixCrLf = false
  * @param bool $bRewindOnComplete = false
  *
  * @return int|bool
  */
 public static function MultipleStreamWriter($rRead, $aWrite, $iBufferLen = 8192, $bResetTimeLimit = true, $bFixCrLf = false, $bRewindOnComplete = false)
 {
     $mResult = false;
     if ($rRead && \is_array($aWrite) && 0 < \count($aWrite)) {
         $mResult = 0;
         while (!\feof($rRead)) {
             $sBuffer = \fread($rRead, $iBufferLen);
             if (false === $sBuffer) {
                 $mResult = false;
                 break;
             }
             if (0 === $iBufferLen || '' === $sBuffer) {
                 break;
             }
             if ($bFixCrLf) {
                 $sBuffer = \str_replace("\n", "\r\n", \str_replace("\r", '', $sBuffer));
             }
             $mResult += \strlen($sBuffer);
             foreach ($aWrite as $rWriteStream) {
                 $mWriteResult = \fwrite($rWriteStream, $sBuffer);
                 if (false === $mWriteResult) {
                     $mResult = false;
                     break 2;
                 }
             }
             if ($bResetTimeLimit) {
                 \MailSo\Base\Utils::ResetTimeLimit();
             }
         }
     }
     if ($mResult && $bRewindOnComplete) {
         foreach ($aWrite as $rWriteStream) {
             if (\is_resource($rWriteStream)) {
                 @\rewind($rWriteStream);
             }
         }
     }
     return $mResult;
 }
Exemplo n.º 2
0
 /**
  * @param string $sParent
  * @param string $sLiteralAtomUpperCase
  * @param resource $rImapStream
  * @param int $iLiteralLen
  *
  * @return bool
  */
 private function partialResponseLiteralCallbackCallable($sParent, $sLiteralAtomUpperCase, $rImapStream, $iLiteralLen)
 {
     $sLiteralAtomUpperCasePeek = '';
     if (0 === \strpos($sLiteralAtomUpperCase, 'BODY')) {
         $sLiteralAtomUpperCasePeek = \str_replace('BODY', 'BODY.PEEK', $sLiteralAtomUpperCase);
     }
     $sFetchKey = '';
     if (\is_array($this->aFetchCallbacks)) {
         if (0 < \strlen($sLiteralAtomUpperCasePeek) && isset($this->aFetchCallbacks[$sLiteralAtomUpperCasePeek])) {
             $sFetchKey = $sLiteralAtomUpperCasePeek;
         } else {
             if (0 < \strlen($sLiteralAtomUpperCase) && isset($this->aFetchCallbacks[$sLiteralAtomUpperCase])) {
                 $sFetchKey = $sLiteralAtomUpperCase;
             }
         }
     }
     $bResult = false;
     if (0 < \strlen($sFetchKey) && '' !== $this->aFetchCallbacks[$sFetchKey] && \is_callable($this->aFetchCallbacks[$sFetchKey])) {
         $rImapLiteralStream = \MailSo\Base\StreamWrappers\Literal::CreateStream($rImapStream, $iLiteralLen);
         $bResult = true;
         $this->writeLog('Start Callback for ' . $sParent . ' / ' . $sLiteralAtomUpperCase . ' - try to read ' . $iLiteralLen . ' bytes.', \MailSo\Log\Enumerations\Type::NOTE);
         $this->bRunningCallback = true;
         try {
             \call_user_func($this->aFetchCallbacks[$sFetchKey], $sParent, $sLiteralAtomUpperCase, $rImapLiteralStream);
         } catch (\Exception $oException) {
             $this->writeLog('Callback Exception', \MailSo\Log\Enumerations\Type::NOTICE);
             $this->writeLogException($oException);
         }
         if (\is_resource($rImapLiteralStream)) {
             $iNotReadLiteralLen = 0;
             $bFeof = \feof($rImapLiteralStream);
             $this->writeLog('End Callback for ' . $sParent . ' / ' . $sLiteralAtomUpperCase . ' - feof = ' . ($bFeof ? 'good' : 'BAD'), $bFeof ? \MailSo\Log\Enumerations\Type::NOTE : \MailSo\Log\Enumerations\Type::WARNING);
             if (!$bFeof) {
                 while (!@\feof($rImapLiteralStream)) {
                     $sBuf = @\fread($rImapLiteralStream, 1024 * 1024);
                     if (false === $sBuf || 0 === \strlen($sBuf) || null === $sBuf) {
                         break;
                     }
                     \MailSo\Base\Utils::ResetTimeLimit();
                     $iNotReadLiteralLen += \strlen($sBuf);
                 }
                 if (\is_resource($rImapLiteralStream) && !@\feof($rImapLiteralStream)) {
                     @\stream_get_contents($rImapLiteralStream);
                 }
             }
             if (\is_resource($rImapLiteralStream)) {
                 @\fclose($rImapLiteralStream);
             }
             if ($iNotReadLiteralLen > 0) {
                 $this->writeLog('Not read literal size is ' . $iNotReadLiteralLen . ' bytes.', \MailSo\Log\Enumerations\Type::WARNING);
             }
         } else {
             $this->writeLog('Literal stream is not resource after callback.', \MailSo\Log\Enumerations\Type::WARNING);
         }
         \MailSo\Base\Loader::IncStatistic('NetRead', $iLiteralLen);
         $this->bRunningCallback = false;
     }
     return $bResult;
 }
Exemplo n.º 3
0
 /**
  * @param resource $rDataStream
  *
  * @return \MailSo\Smtp\SmtpClient
  *
  * @throws \MailSo\Base\Exceptions\InvalidArgumentException
  * @throws \MailSo\Net\Exceptions\Exception
  * @throws \MailSo\Smtp\Exceptions\Exception
  */
 public function DataWithStream($rDataStream)
 {
     if (!\is_resource($rDataStream)) {
         throw new \MailSo\Base\Exceptions\InvalidArgumentException();
     }
     if (!$this->bRcpt) {
         $this->writeLogException(new Exceptions\RuntimeException('No recipient forward path has been supplied'), \MailSo\Log\Enumerations\Type::ERROR, true);
     }
     $this->sendRequestWithCheck('DATA', 354);
     $this->writeLog('Message data.', \MailSo\Log\Enumerations\Type::NOTE);
     $this->bRunningCallback = true;
     while (!\feof($rDataStream)) {
         $sBuffer = \fgets($rDataStream);
         if (false !== $sBuffer) {
             if (0 === \strpos($sBuffer, '.')) {
                 $sBuffer = '.' . $sBuffer;
             }
             $this->sendRaw(\rtrim($sBuffer, "\r\n"), false);
             \MailSo\Base\Utils::ResetTimeLimit();
             continue;
         } else {
             if (!\feof($rDataStream)) {
                 $this->writeLogException(new Exceptions\RuntimeException('Cannot read input resource'), \MailSo\Log\Enumerations\Type::ERROR, true);
             }
         }
         break;
     }
     $this->sendRequestWithCheck('.', 250);
     $this->bRunningCallback = false;
     $this->bData = true;
     return $this;
 }
Exemplo n.º 4
0
 private function davClientRequest($oClient, $sCmd, $sUrl, $mData = null)
 {
     \MailSo\Base\Utils::ResetTimeLimit();
     $this->oLogger->Write($sCmd . ' ' . $sUrl . ('PUT' === $sCmd && null !== $mData ? ' (' . \strlen($mData) . ')' : ''), \MailSo\Log\Enumerations\Type::INFO, 'DAV');
     if ('PUT' === $sCmd) {
         $this->oLogger->Write($mData, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
     }
     $oResponse = false;
     try {
         $oResponse = 'PUT' === $sCmd && null !== $mData ? $oClient->request($sCmd, $sUrl, $mData) : $oClient->request($sCmd, $sUrl);
         if ('GET' === $sCmd && false) {
             $this->oLogger->WriteDump($oResponse, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
         }
     } catch (\Exception $oException) {
         $this->oLogger->WriteException($oException);
     }
     return $oResponse;
 }
Exemplo n.º 5
0
 /**
  * @param string $sEmail
  * @param string $sVcfData
  *
  * @return int
  */
 public function ImportVcfFile($sEmail, $sVcfData)
 {
     $iCount = 0;
     if ($this->IsActive() && \is_string($sVcfData)) {
         $sVcfData = \trim($sVcfData);
         if ("" === \substr($sVcfData, 0, 3)) {
             $sVcfData = \substr($sVcfData, 3);
         }
         $oVCardSplitter = null;
         try {
             $oVCardSplitter = new \Sabre\VObject\Splitter\VCard($sVcfData);
         } catch (\Exception $oExc) {
             $this->Logger()->WriteException($oExc);
         }
         if ($oVCardSplitter) {
             $oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
             $oVCard = null;
             while ($oVCard = $oVCardSplitter->getNext()) {
                 if ($oVCard instanceof \Sabre\VObject\Component\VCard) {
                     \MailSo\Base\Utils::ResetTimeLimit();
                     if (empty($oVCard->UID)) {
                         $oVCard->UID = \Sabre\DAV\UUIDUtil::getUUID();
                     }
                     $oContact->PopulateByVCard($oVCard->serialize());
                     if (0 < \count($oContact->Properties)) {
                         if ($this->ContactSave($sEmail, $oContact)) {
                             $iCount++;
                         }
                     }
                     $oContact->Clear();
                 }
             }
         }
     }
     return $iCount;
 }
Exemplo n.º 6
0
 private function davClientRequest($oClient, $sCmd, $sUrl, $mData = null)
 {
     \MailSo\Base\Utils::ResetTimeLimit();
     $this->oLogger->Write($sCmd . ' ' . $sUrl . (('PUT' === $sCmd || 'POST' === $sCmd) && null !== $mData ? ' (' . \strlen($mData) . ')' : ''), \MailSo\Log\Enumerations\Type::INFO, 'DAV');
     //		if ('PUT' === $sCmd || 'POST' === $sCmd)
     //		{
     //			$this->oLogger->Write($mData, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
     //		}
     $oResponse = false;
     try {
         if (('PUT' === $sCmd || 'POST' === $sCmd) && null !== $mData) {
             $oResponse = $oClient->request($sCmd, $sUrl, $mData, array('Content-Type' => 'text/vcard; charset=utf-8'));
         } else {
             $oResponse = $oClient->request($sCmd, $sUrl);
         }
         //			if ('GET' === $sCmd)
         //			{
         //				$this->oLogger->WriteDump($oResponse, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
         //			}
     } catch (\Exception $oException) {
         $this->oLogger->WriteException($oException);
     }
     return $oResponse;
 }
Exemplo n.º 7
0
 /**
  * @param string $sParent
  * @param string $sLiteralAtomUpperCase
  * @param resource $rImapStream
  * @param int $iLiteralLen
  *
  * @return bool
  */
 private function partialResponseLiteralCallbackCallable($sParent, $sLiteralAtomUpperCase, $rImapStream, $iLiteralLen)
 {
     $sLiteralAtomUpperCasePeek = '';
     if (0 === strpos($sLiteralAtomUpperCase, 'BODY')) {
         $sLiteralAtomUpperCasePeek = str_replace('BODY', 'BODY.PEEK', $sLiteralAtomUpperCase);
     }
     $sFetchKey = '';
     if (is_array($this->aFetchCallbacks)) {
         if (0 < strlen($sLiteralAtomUpperCasePeek) && isset($this->aFetchCallbacks[$sLiteralAtomUpperCasePeek])) {
             $sFetchKey = $sLiteralAtomUpperCasePeek;
         } else {
             if (0 < strlen($sLiteralAtomUpperCase) && isset($this->aFetchCallbacks[$sLiteralAtomUpperCase])) {
                 $sFetchKey = $sLiteralAtomUpperCase;
             }
         }
     }
     $bResult = false;
     if (0 < \strlen($sFetchKey) && '' !== $this->aFetchCallbacks[$sFetchKey] && \is_callable($this->aFetchCallbacks[$sFetchKey])) {
         $rImapLiteralStream = \MailSo\Base\StreamWrappers\Literal::CreateStream($rImapStream, $iLiteralLen);
         $bResult = true;
         $this->writeLog('Callback for ' . $sParent . ' / ' . $sLiteralAtomUpperCase . ' - try to read ' . $iLiteralLen . ' bytes.', \MailSo\Log\Enumerations\Type::NOTE);
         \call_user_func($this->aFetchCallbacks[$sFetchKey], $sParent, $sLiteralAtomUpperCase, $rImapLiteralStream);
         $iTimer = 0;
         $iNotReadLiteralLen = 0;
         while (!\feof($rImapLiteralStream)) {
             $sBuf = \fread($rImapLiteralStream, 8192);
             if (false !== $sBuf) {
                 \MailSo\Base\Utils::ResetTimeLimit($iTimer);
                 $iNotReadLiteralLen += \strlen($sBuf);
                 continue;
             }
             break;
         }
         if ($iNotReadLiteralLen > 0) {
             $this->writeLog('Not read literal size ' . $iNotReadLiteralLen . ' bytes.', \MailSo\Log\Enumerations\Type::WARNING);
         }
         \MailSo\Base\Loader::IncStatistic('NetRead', $iLiteralLen);
         if (\is_resource($rImapLiteralStream)) {
             \fclose($rImapLiteralStream);
         }
     }
     return $bResult;
 }