From 97d3ccef47d7047c74bdb08e076a695c38b23e4c Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 23 Jan 2014 11:35:25 -0600
Subject: [PATCH] emails: Allow specifying how deep to recurse

Previously, the mail parsing and fetching system would recurse unbounded
searching for bodies by content-type
---
 include/class.mailfetch.php | 7 ++++---
 include/class.mailparse.php | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 3c4306d1e..e6e52686d 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -311,7 +311,7 @@ class MailFetcher {
     }
 
     //search for specific mime type parts....encoding is the desired encoding.
-    function getPart($mid, $mimeType, $encoding=false, $struct=null, $partNumber=false) {
+    function getPart($mid, $mimeType, $encoding=false, $struct=null, $partNumber=false, $recurse=-1) {
 
         if(!$struct && $mid)
             $struct=@imap_fetchstructure($this->mbox, $mid);
@@ -345,11 +345,12 @@ class MailFetcher {
 
         //Do recursive search
         $text='';
-        if($struct && $struct->parts) {
+        if($struct && $struct->parts && $recurse) {
             while(list($i, $substruct) = each($struct->parts)) {
                 if($partNumber)
                     $prefix = $partNumber . '.';
-                if(($result=$this->getPart($mid, $mimeType, $encoding, $substruct, $prefix.($i+1))))
+                if (($result=$this->getPart($mid, $mimeType, $encoding,
+                        $substruct, $prefix.($i+1), $recurse-1)))
                     $text.=$result;
             }
         }
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 32b0083a6..0882fb3cd 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -197,7 +197,7 @@ class Mail_Parse {
         return $body;
     }
 
-    function getPart($struct, $ctypepart) {
+    function getPart($struct, $ctypepart, $recurse=-1) {
 
         if($struct && !$struct->parts) {
             $ctype = @strtolower($struct->ctype_primary.'/'.$struct->ctype_secondary);
@@ -213,9 +213,10 @@ class Mail_Parse {
         }
 
         $data='';
-        if($struct && $struct->parts) {
+        if($struct && $struct->parts && $recurse) {
             foreach($struct->parts as $i=>$part) {
-                if($part && !$part->disposition && ($text=$this->getPart($part,$ctypepart)))
+                if($part && !$part->disposition
+                        && ($text=$this->getPart($part,$ctypepart,$recurse - 1)))
                     $data.=$text;
             }
         }
-- 
GitLab