@@ -86,7 +86,6 @@ protected function parse(){
86
86
* Determine the message content type
87
87
*/
88
88
public function findContentType (){
89
-
90
89
$ content_type = $ this ->header ->get ("content_type " );
91
90
$ content_type = (is_array ($ content_type )) ? implode (' ' , $ content_type ) : $ content_type ;
92
91
if (stripos ($ content_type , 'multipart ' ) === 0 ) {
@@ -97,28 +96,51 @@ public function findContentType(){
97
96
}
98
97
99
98
/**
100
- * Determine the message content type
99
+ * Find all available headers and return the left over body segment
100
+ * @var string $context
101
+ * @var integer $part_number
101
102
*
102
- * @return string|null
103
+ * @return Part[]
104
+ * @throws InvalidMessageDateException
103
105
*/
104
- public function getBoundary (){
105
- $ boundary = $ this ->header ->find ("/boundary\=(.*)/i " );
106
-
107
- if ($ boundary === null ) {
108
- return null ;
106
+ private function parsePart ($ context , $ part_number = 0 ){
107
+ $ body = $ context ;
108
+ while (($ pos = strpos ($ body , "\r\n" )) > 0 ) {
109
+ $ body = substr ($ body , $ pos + 2 );
109
110
}
111
+ $ headers = substr ($ context , 0 , strlen ($ body ) * -1 );
112
+ $ body = substr ($ body , 0 , -2 );
110
113
111
- return $ this ->clearBoundaryString ($ boundary );
114
+ $ headers = new Header ($ headers );
115
+ if (($ boundary = $ headers ->getBoundary ()) !== null ) {
116
+ return $ this ->detectParts ($ boundary , $ body , $ part_number );
117
+ }
118
+ return [new Part ($ body , $ headers , $ part_number )];
112
119
}
113
120
114
121
/**
115
- * Remove all unwanted chars from a given boundary
116
- * @param string $str
122
+ * @param string $boundary
123
+ * @param string $context
124
+ * @param int $part_number
117
125
*
118
- * @return string
126
+ * @return array
127
+ * @throws InvalidMessageDateException
119
128
*/
120
- private function clearBoundaryString ($ str ) {
121
- return str_replace (['" ' , '\r ' , '\n ' , "\n" , "\r" , "; " , "\s " ], "" , $ str );
129
+ private function detectParts ($ boundary , $ context , $ part_number = 0 ){
130
+ $ base_parts = explode ( $ boundary , $ context );
131
+ $ final_parts = [];
132
+ foreach ($ base_parts as $ ctx ) {
133
+ $ ctx = substr ($ ctx , 2 );
134
+ if ($ ctx !== "-- " && $ ctx != "" ) {
135
+ $ parts = $ this ->parsePart ($ ctx , $ part_number );
136
+ foreach ($ parts as $ part ) {
137
+ $ final_parts [] = $ part ;
138
+ $ part_number = $ part ->part_number ;
139
+ }
140
+ $ part_number ++;
141
+ }
142
+ }
143
+ return $ final_parts ;
122
144
}
123
145
124
146
/**
@@ -130,39 +152,23 @@ private function clearBoundaryString($str) {
130
152
*/
131
153
public function find_parts (){
132
154
if ($ this ->type === IMAP ::MESSAGE_TYPE_MULTIPART ) {
133
- if (($ boundary = $ this ->getBoundary ()) === null ) {
155
+ if (($ boundary = $ this ->header -> getBoundary ()) === null ) {
134
156
throw new MessageContentFetchingException ("no content found " , 0 );
135
157
}
136
158
137
- $ boundaries = [
138
- $ boundary
139
- ];
140
-
141
- if (preg_match ("/boundary\= \"?(.*) \"?/ " , $ this ->raw , $ match ) == 1 ) {
142
- if (is_array ($ match [1 ])){
143
- foreach ($ match [1 ] as $ matched ){
144
- $ boundaries [] = $ this ->clearBoundaryString ($ matched );
145
- }
146
- }else {
147
- if (!empty ($ match [1 ])) {
148
- $ boundaries [] = $ this ->clearBoundaryString ($ match [1 ]);
149
- }
150
- }
151
- }
152
-
153
- $ raw_parts = explode ( $ boundaries [0 ], str_replace ($ boundaries , $ boundaries [0 ], $ this ->raw ) );
154
- $ parts = [];
155
- $ part_number = 0 ;
156
- foreach ($ raw_parts as $ part ) {
157
- $ part = trim (rtrim ($ part ));
158
- if ($ part !== "-- " ) {
159
- $ parts [] = new Part ($ part , null , $ part_number );
160
- $ part_number ++;
161
- }
162
- }
163
- return $ parts ;
159
+ return $ this ->detectParts ($ boundary , $ this ->raw );
164
160
}
165
161
166
162
return [new Part ($ this ->raw , $ this ->header )];
167
163
}
164
+
165
+ /**
166
+ * Try to find a boundary if possible
167
+ *
168
+ * @return string|null
169
+ * @Depricated since version 2.4.4
170
+ */
171
+ public function getBoundary (){
172
+ return $ this ->header ->getBoundary ();
173
+ }
168
174
}
0 commit comments