From: Marc G. K. <st...@us...> - 2003-07-24 21:51:51
|
Update of /cvsroot/squirrelmail/squirrelmail/functions In directory sc8-pr-cvs1:/tmp/cvs-serv9033 Modified Files: Tag: SM-1_4-STABLE attachment_common.php imap_general.php mime.php Log Message: php 4.3.2 changed fread behaviour. We cannot use it any more wit large buffers on socket connections. I lowered the buffersize to 780 which appears save and merged the devel canges to stable in order to do on the fly decoding of base64 encoded attachments. That will save lots of memory :) Index: attachment_common.php =================================================================== RCS file: /cvsroot/squirrelmail/squirrelmail/functions/attachment_common.php,v retrieving revision 1.25 retrieving revision 1.25.2.1 diff -u -w -r1.25 -r1.25.2.1 --- attachment_common.php 9 Mar 2003 18:45:06 -0000 1.25 +++ attachment_common.php 24 Jul 2003 21:51:46 -0000 1.25.2.1 @@ -27,6 +27,7 @@ 'png' => 'image/png', 'rtf' => 'text/richtext', 'txt' => 'text/plain', + 'patch'=> 'text/plain', 'vcf' => 'text/x-vcard'); /* Register browser-supported image types */ Index: imap_general.php =================================================================== RCS file: /cvsroot/squirrelmail/squirrelmail/functions/imap_general.php,v retrieving revision 1.140.2.8 retrieving revision 1.140.2.9 diff -u -w -r1.140.2.8 -r1.140.2.9 --- imap_general.php 3 Jul 2003 16:20:25 -0000 1.140.2.8 +++ imap_general.php 24 Jul 2003 21:51:46 -0000 1.140.2.9 @@ -104,23 +104,60 @@ if (!$filter || !$outputstream) { $iBufferSize = $iSize; } else { - // FIXME This doesn't work with base64 decode, Why ? - // The idea is to use a buffersize of 32k i.e. - $iBufferSize = $iSize; + // see php bug 24033. They changed fread behaviour %$^&$% + $iBufferSize = 780; // multiple of 78 in case of base64 decoding. } $iRet = $iSize - $iBufferSize; $iRetrieved = 0; $i = 0; - $results = ''; + $results = $sReadRem = ''; + $bFinished = $bBufferSizeAdapted = $bBufferIsOk = false; while (($iRetrieved < ($iSize - $iBufferSize))) { $sRead = fread($imap_stream,$iBufferSize); - if ($sRead === false) { + 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); + } $filter($sRead); } if ($outputstream) { @@ -135,7 +172,7 @@ } $results .= $sRead; } - if ($results !== false) { + if (!$results && !$bFinished) { $sRead = fread($imap_stream,($iSize - ($iRetrieved))); if ($filter) { $filter($sRead); Index: mime.php =================================================================== RCS file: /cvsroot/squirrelmail/squirrelmail/functions/mime.php,v retrieving revision 1.265.2.9 retrieving revision 1.265.2.10 diff -u -w -r1.265.2.9 -r1.265.2.10 --- mime.php 30 Jun 2003 08:15:29 -0000 1.265.2.9 +++ mime.php 24 Jul 2003 21:51:46 -0000 1.265.2.10 @@ -156,7 +156,6 @@ function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) { global $uid_support; - $sid = sqimap_session_id($uid_support); /* Don't kill the connection if the browser is over a dialup * and it would take over 30 seconds to download it. * Don´t call set_time_limit in safe mode. @@ -165,43 +164,27 @@ if (!ini_get('safe_mode')) { set_time_limit(0); } - if ($uid_support) { - $sid_s = substr($sid,0,strpos($sid, ' ')); + /* in case of base64 encoded attachments, do not buffer them. + Instead, echo the decoded attachment directly to screen */ + if (strtolower($encoding) == 'base64') { + if (!$ent_id) { + $query = "FETCH $id BODY[]"; } else { - $sid_s = $sid; + $query = "FETCH $id BODY[$ent_id]"; } - + sqimap_run_command($imap_stream,$query,true,$response,$message,$uid_support,'sqimap_base64_decode','php://stdout',true); + } else { $body = mime_fetch_body ($imap_stream, $id, $ent_id); echo decodeBody($body, $encoding); - return; -/* - fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n"); - $cnt = 0; - $continue = true; - $read = fgets ($imap_stream,8192); - - - // This could be bad -- if the section has sqimap_session_id() . ' OK' - // or similar, it will kill the download. - while (!ereg("^".$sid_s." (OK|BAD|NO)(.*)$", $read, $regs)) { - if (trim($read) == ')==') { - $read1 = $read; - $read = fgets ($imap_stream,4096); - if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { - return; - } else { - echo decodeBody($read1, $encoding) . - decodeBody($read, $encoding); - } - } else if ($cnt) { - echo decodeBody($read, $encoding); } - $read = fgets ($imap_stream,4096); - $cnt++; -// break; + return; } -*/ + +function sqimap_base64_decode(&$string) { +// $string = str_replace("\r\n", "\n", $string); + $string = base64_decode($string); } + /* -[ END MIME DECODING ]----------------------------------------------------------- */ |