/** * @param array $aSubStreams * * @return resource|bool */ public static function CreateStream($aSubStreams) { if (!\in_array(self::STREAM_NAME, \stream_get_wrappers())) { \stream_wrapper_register(self::STREAM_NAME, '\\MailSo\\Base\\StreamWrappers\\SubStreams'); } $sHashName = \md5(\microtime(true) . \rand(1000, 9999)); self::$aStreams[$sHashName] = $aSubStreams; \MailSo\Base\Loader::IncStatistic('CreateStream/SubStreams'); return \fopen(self::STREAM_NAME . '://' . $sHashName, 'rb'); }
/** * @param resource $rStream * @param int $iLiteralLen * * @return resource|bool */ public static function CreateStream($rStream, $iLiteralLen) { if (!in_array(self::STREAM_NAME, stream_get_wrappers())) { stream_wrapper_register(self::STREAM_NAME, '\\MailSo\\Base\\StreamWrappers\\Literal'); } $sHashName = md5(microtime(true) . rand(1000, 9999)); self::$aStreams[$sHashName] = array($rStream, $iLiteralLen); \MailSo\Base\Loader::IncStatistic('CreateStream/Literal'); return fopen(self::STREAM_NAME . '://' . $sHashName, 'rb'); }
/** * @param string $sRawResponse * * @return resource|bool */ public static function CreateStream($sRawResponse) { if (!in_array(self::STREAM_NAME, stream_get_wrappers())) { stream_wrapper_register(self::STREAM_NAME, '\\MailSo\\Base\\StreamWrappers\\Test'); } $sHashName = md5(microtime(true) . rand(1000, 9999)); $rConnect = fopen('php://memory', 'r+b'); fwrite($rConnect, $sRawResponse); fseek($rConnect, 0); self::$aStreams[$sHashName] = $rConnect; \MailSo\Base\Loader::IncStatistic('CreateStream/Test'); return fopen(self::STREAM_NAME . '://' . $sHashName, 'r+b'); }
/** * @param string $sPath * * @return bool */ public function stream_open($sPath) { $bResult = false; $aPath = parse_url($sPath); if (isset($aPath['host']) && isset($aPath['scheme']) && 0 < strlen($aPath['host']) && 0 < strlen($aPath['scheme']) && self::STREAM_NAME === $aPath['scheme']) { $sHashName = $aPath['host']; if (isset(self::$aStreams[$sHashName]) && is_resource(self::$aStreams[$sHashName])) { $this->rSream = self::$aStreams[$sHashName]; $bResult = true; } else { $this->rSream = fopen('php://memory', 'r+b'); self::$aStreams[$sHashName] = $this->rSream; $bResult = true; \MailSo\Base\Loader::IncStatistic('CreateStream/TempFile'); } } return $bResult; }
/** * @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; }
/** * @param mixed $mReadLen = null * @param bool $bForceLogin = false * * @return void * * @throws \MailSo\Net\Exceptions\SocketConnectionDoesNotAvailableException * @throws \MailSo\Net\Exceptions\SocketReadException */ protected function getNextBuffer($mReadLen = null, $bForceLogin = false) { if (null === $mReadLen) { $this->sResponseBuffer = @\fgets($this->rConnect); } else { $this->sResponseBuffer = ''; $iRead = $mReadLen; while (0 < $iRead) { $sAddRead = @\fread($this->rConnect, $iRead); if (false === $sAddRead) { $this->sResponseBuffer = false; break; } $this->sResponseBuffer .= $sAddRead; $iRead -= \strlen($sAddRead); } } if (false === $this->sResponseBuffer) { $this->IsConnected(true); $this->bUnreadBuffer = true; $aSocketStatus = @\stream_get_meta_data($this->rConnect); if (isset($aSocketStatus['timed_out']) && $aSocketStatus['timed_out']) { $this->writeLogException(new Exceptions\SocketReadTimeoutException(), \MailSo\Log\Enumerations\Type::ERROR, true); } else { // $this->writeLog('Stream Meta: '. // \print_r($aSocketStatus, true), \MailSo\Log\Enumerations\Type::ERROR); $this->writeLogException(new Exceptions\SocketReadException(), \MailSo\Log\Enumerations\Type::ERROR, true); } } else { $iReadedLen = \strlen($this->sResponseBuffer); if (null === $mReadLen || $bForceLogin) { $iLimit = 5000; // 5kb if ($iLimit < $iReadedLen) { $this->writeLogWithCrlf('[cutted:' . $iReadedLen . 'b] < ' . \substr($this->sResponseBuffer . '...', 0, $iLimit), \MailSo\Log\Enumerations\Type::INFO); } else { $this->writeLogWithCrlf('< ' . $this->sResponseBuffer, \MailSo\Log\Enumerations\Type::INFO); } } else { $this->writeLog('Received ' . $iReadedLen . '/' . $mReadLen . ' bytes.', \MailSo\Log\Enumerations\Type::INFO); } \MailSo\Base\Loader::IncStatistic('NetRead', $iReadedLen); } }
/** * @param resource $rResource * * @return void */ public static function CloseMemoryResource(&$rResource) { if (\is_resource($rResource)) { $sKey = (string) $rResource; if (isset(\MailSo\Base\ResourceRegistry::$Resources[$sKey])) { \fclose(\MailSo\Base\ResourceRegistry::$Resources[$sKey]); \MailSo\Base\ResourceRegistry::$Resources[$sKey] = null; unset(\MailSo\Base\ResourceRegistry::$Resources[$sKey]); \MailSo\Base\Loader::IncStatistic('CloseMemoryResource'); } if (\is_resource($rResource)) { \fclose($rResource); } $rResource = null; } }
/** * @param resource $rStream * @param string $sUtilsDecodeOrEncodeFunctionName = null * @param string $sFromEncoding = null * @param string $sToEncoding = null * * @return resource|bool */ public static function CreateStream($rStream, $sUtilsDecodeOrEncodeFunctionName = null, $sFromEncoding = null, $sToEncoding = null) { if (!in_array(self::STREAM_NAME, stream_get_wrappers())) { stream_wrapper_register(self::STREAM_NAME, '\\MailSo\\Base\\StreamWrappers\\Binary'); } if (null === $sUtilsDecodeOrEncodeFunctionName || 0 === strlen($sUtilsDecodeOrEncodeFunctionName)) { $sUtilsDecodeOrEncodeFunctionName = 'InlineNullDecode'; } $sHashName = md5(microtime(true) . rand(1000, 9999)); if (null !== $sFromEncoding && null !== $sToEncoding && $sFromEncoding !== $sToEncoding) { $rStream = self::CreateStream($rStream, $sUtilsDecodeOrEncodeFunctionName); $sUtilsDecodeOrEncodeFunctionName = 'InlineConvertDecode'; } if (in_array($sUtilsDecodeOrEncodeFunctionName, array('convert.base64-decode', 'convert.base64-encode', 'convert.quoted-printable-decode', 'convert.quoted-printable-encode'))) { $rFilter = \stream_filter_append($rStream, $sUtilsDecodeOrEncodeFunctionName, STREAM_FILTER_READ, array('line-length' => \MailSo\Mime\Enumerations\Constants::LINE_LENGTH, 'line-break-chars' => \MailSo\Mime\Enumerations\Constants::CRLF)); return \is_resource($rFilter) ? $rStream : false; } self::$aStreams[$sHashName] = array($rStream, $sUtilsDecodeOrEncodeFunctionName, $sFromEncoding, $sToEncoding); \MailSo\Base\Loader::IncStatistic('CreateStream/Binary'); return \fopen(self::STREAM_NAME . '://' . $sHashName, 'rb'); }
/** * @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; }