Skip to content

Commit

Permalink
Improve Webklex/php-imap memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Aug 13, 2024
1 parent 823014d commit be4a123
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions overrides/webklex/php-imap/src/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,37 @@ private function parsePart(string $context, int $part_number = 0): array {
* @throws InvalidMessageDateException
*/
private function detectParts(string $boundary, string $context, int $part_number = 0): array {
$base_parts = explode( $boundary, $context);
// Below we get rid of exlode() as it consumes extra memory.
// https://github.com/freescout-help-desk/freescout/issues/3956#issuecomment-2284592925
//$base_parts = explode( $boundary, $context);

$final_parts = [];
foreach($base_parts as $ctx) {
$ctx = substr($ctx, 2);
//foreach($base_parts as $ctx) {

$boundary_len = strlen($boundary);
$last_pos = 0;
$positions = [];
while (($last_pos = strpos($context, $boundary, $last_pos)) !== false) {
$positions[] = $last_pos;
$last_pos = $last_pos + $boundary_len;
}
if (!count($positions) || $positions[0] != 0) {
array_unshift($positions, 0);
}

foreach ($positions as $pos_i => $pos) {
if ($pos == 0) {
// First.
$ctx = substr($context, 0+2, $positions[$pos_i+1]-2);
} elseif ($pos_i == count($positions)-1) {
// Last.
$ctx = substr($context, $pos+$boundary_len+2);
} else {
$ctx = substr($context, $pos+$boundary_len+2, $positions[$pos_i+1]-$pos-$boundary_len-2);
}

//$ctx = substr($ctx, 2);

if ($ctx !== "--" && $ctx != "" && $ctx != "\r\n") {
$parts = $this->parsePart($ctx, $part_number);
foreach ($parts as $part) {
Expand All @@ -140,6 +167,7 @@ private function detectParts(string $boundary, string $context, int $part_number
$part_number++;
}
}

return $final_parts;
}

Expand Down

0 comments on commit be4a123

Please sign in to comment.