From: stekkel Date: Wed, 24 Sep 2003 20:53:06 +0000 (+0000) Subject: Rewrote sqimap_fread to make it work on PHP 4.3.2. X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=commitdiff_plain;h=977a60854bf9271a68e8a9c8dc922c2a5b9e9175 Rewrote sqimap_fread to make it work on PHP 4.3.2. The fread behaviour was changed with as result that fread didn't return when the requested buffersize was full but at a earlier stage, at the end of a tcp packet. From now on fread stops on: EOF, full buffer and in case of sockets at end of packet if the imap server isn't providing the requested data quick enough. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@5771 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- diff --git a/functions/imap_general.php b/functions/imap_general.php index 4a8b5576..b53a2104 100755 --- a/functions/imap_general.php +++ b/functions/imap_general.php @@ -188,62 +188,46 @@ function sqimap_fread($imap_stream,$iSize,$filter=false, $iBufferSize = $iSize; } else { // see php bug 24033. They changed fread behaviour %$^&$% - $iBufferSize = 780; // multiple of 78 in case of base64 decoding. + $iBufferSize = 7800; // multiple of 78 in case of base64 decoding. + } + if ($iSize < $iBufferSize) { + $iBufferSize = $iSize; } - $iRet = $iSize - $iBufferSize; $iRetrieved = 0; - $i = 0; - $results = $sReadRem = ''; - $bFinished = $bBufferSizeAdapted = $bBufferIsOk = false; - while (($iRetrieved < ($iSize - $iBufferSize))) { + $results = ''; + $sRead = $sReadRem = ''; + // NB: fread can also stop at end of a packet on sockets. + while ($iRetrieved < $iSize) { $sRead = fread($imap_stream,$iBufferSize); + $iLength = strlen($sRead); + $iRetrieved += $iLength ; + $iRemaining = $iSize - $iRetrieved; + if ($iRemaining < $iBufferSize) { + $iBufferSize = $iRemaining; + } if (!$sRead) { $results = false; break; } - $iRetrieved += $iBufferSize; - if ($filter) { - // in case line-endings do not appear at position 78 we adapt the buffersize so we can base64 decode on the fly - if (!$bBufferSizeAdapted) { - $i = strpos($sRead,"\n"); - if ($i) { - ++$i; - $iFragments = floor($iBufferSize / $i); - $iNewBufferSize = $iFragments * $i; - $iRemainder = $iNewBufferSize + $i - $iBufferSize; - if ($iNewBufferSize == $iBufferSize) { - $bBufferIsOk = true; - $iRemainder = 0; - $iNewBufferSize = $iBufferSize; - $bBufferSizeAdapted = true; - } - if (!$bBufferIsOk && ($iRemainder + $iBufferSize) < $iSize) { - $sReadRem = fread($imap_stream,$iRemainder); - } else if (!$bBufferIsOk) { - $sReadRem = fread($imap_stream,$iSize - $iBufferSize); - $bFinished = true; - } - if (!$sReadRem && $sReadRem !== '') { - $results = false; - break; - } - $iBufferSize = $iNewBufferSize; - $bBufferSizeAdapted = true; - } else { - $sReadRem = fread($imap_stream,$iSize - $iBufferSize); - $bFinished = true; - if (!$sReadRem) { - $results = false; - break; - } - } - $sRead .= $sReadRem; - $iRetrieved += $iRemainder; - unset($sReadRem); - } + if ($sReadRem) { + $sRead = $sReadRem . $sRead; + $sReadRem = ''; + } + if (substr($sRead,-1) !== "\n") { + $i = strrpos($sRead,"\n"); + if ($i !== false && $iRetrieved<$iSize) { + ++$i; + $sReadRem = substr($sRead,$i); + $sRead = substr($sRead,0,$i); + } else if ($iLength && $iRetrieved<$iSize) { // linelength > received buffer + $sReadRem = $sRead; + $sRead = ''; + } + } + if ($filter && $sRead) { $filter($sRead); } - if ($outputstream) { + if ($outputstream && $sRead) { if (is_resource($outputstream)) { fwrite($outputstream,$sRead); } else if ($outputstream == 'php://stdout') { @@ -252,28 +236,13 @@ function sqimap_fread($imap_stream,$iSize,$filter=false, } if ($no_return) { $sRead = ''; - } - $results .= $sRead; - } - if (!$results && !$bFinished) { - $sRead = fread($imap_stream,($iSize - ($iRetrieved))); - if ($filter) { - $filter($sRead); - } - if ($outputstream) { - if (is_resource($outputstream)) { - fwrite($outputstream,$sRead); - } else if ($outputstream == 'php://stdout') { // FIXME - echo $sRead; - } + } else { + $results .= $sRead; } - if ($no_return) { - $sRead = ''; - } - $results .= $sRead; } return $results; } + /* obsolete function, inform plugins that use it */ function sqimap_read_data_list($imap_stream, $tag, $handle_errors, &$response, &$message, $query = '') {