Skip to content

Commit 55a0149

Browse files
debug fetch structure
1 parent 065f7cd commit 55a0149

File tree

7 files changed

+163
-40
lines changed

7 files changed

+163
-40
lines changed

bootstrap.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ function imap_append($imap, $folder, $message, $options = null, $internalDate =
780780
if (!function_exists('imap2_append')) {
781781
function imap2_append($imap, $folder, $message, $options = null, $internalDate = null)
782782
{
783+
if (IMAP2_RETROFIT_MODE && is_resource($imap) && get_resource_type($imap) == 'imap') {
784+
return imap_append($imap, $folder, $message, $options, $internalDate);
785+
}
786+
783787
return Mailbox::append($imap, $folder, $message, $options, $internalDate);
784788
}
785789
}
@@ -788,9 +792,9 @@ function imap2_append($imap, $folder, $message, $options = null, $internalDate =
788792
* imap2_headerinfo
789793
*/
790794
if (!function_exists('imap_headerinfo')) {
791-
function imap4_headerinfo($imap, $messageNum, $fromLength = 0, $subjectLength = 0, $defaultHost = null)
795+
function imap_headerinfo($imap, $messageNum, $fromLength = 0, $subjectLength = 0, $defaultHost = null)
792796
{
793-
return imap_headerinfo($imap, $messageNum, $fromLength, $subjectLength, $defaultHost);
797+
return imap2_headerinfo($imap, $messageNum, $fromLength, $subjectLength, $defaultHost);
794798
}
795799
}
796800
if (!function_exists('imap2_headerinfo')) {

src/BodyStructure.php

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,29 @@ class BodyStructure
2222
];
2323

2424
public static function fromMessage($message)
25+
{
26+
return self::fromBodyStructure($message->bodystructure);
27+
}
28+
29+
protected static function fromBodyStructure($structure)
2530
{
2631
$parts = [];
2732
$parameters = [];
2833

2934
#file_put_contents('t3.json', json_encode($message, JSON_PRETTY_PRINT));
3035

31-
if (isset($message->bodystructure[0]) && $message->bodystructure[0] == 'TEXT') {
32-
$parameters = self::extractParameters($message->bodystructure[2], []);
36+
if (isset($structure[0]) && $structure[0] == 'TEXT') {
37+
$parameters = self::extractParameters($structure[2], []);
3338

3439
return (object) [
3540
'type' => 0,
36-
'encoding' => self::$encodingNumber[$message->bodystructure[5]] ?? 0,
41+
'encoding' => self::$encodingNumber[$structure[5]] ?? 0,
3742
'ifsubtype' => 1,
38-
'subtype' => $message->bodystructure[1],
43+
'subtype' => $structure[1],
3944
'ifdescription' => 0,
4045
'ifid' => 0,
41-
'lines' => intval($message->bodystructure[7]),
42-
'bytes' => intval($message->bodystructure[6]),
46+
'lines' => intval($structure[7]),
47+
'bytes' => intval($structure[6]),
4348
'ifdisposition' => 0,
4449
'ifdparameters' => 0,
4550
'ifparameters' => count($parameters),
@@ -49,7 +54,7 @@ public static function fromMessage($message)
4954

5055
$section = 'parts';
5156
$subType = 'ALTERNATIVE';
52-
foreach ($message->bodystructure as $item) {
57+
foreach ($structure as $item) {
5358
if ($item == 'ALTERNATIVE') {
5459
$section = 'parameters';
5560
continue;
@@ -85,6 +90,11 @@ public static function fromMessage($message)
8590

8691
protected static function extractPart($item)
8792
{
93+
global $countParts;
94+
95+
$countParts++;
96+
file_put_contents('p'.$countParts.'.json', json_encode($item, JSON_PRETTY_PRINT));
97+
8898
$attribute = null;
8999
$parameters = [];
90100

@@ -105,15 +115,23 @@ protected static function extractPart($item)
105115
}
106116
}
107117

118+
$type = 0;
119+
$linesIndex = 7;
120+
$bytesIndex = 6;
121+
if ($item[0] == 'MESSAGE') {
122+
$type = 2;
123+
$linesIndex = 9;
124+
}
125+
108126
$part = (object) [
109-
'type' => 0,
110-
'encoding' => self::$encodingNumber[$item[5]],
127+
'type' => $type,
128+
'encoding' => is_numeric($item[5]) ? (self::$encodingNumber[$item[5]] ?? 0) : 0,
111129
'ifsubtype' => 1,
112130
'subtype' => $item[1],
113131
'ifdescription' => 0,
114132
'ifid' => 0,
115-
'lines' => intval($item[7]),
116-
'bytes' => intval($item[6]),
133+
'lines' => intval($item[$linesIndex]),
134+
'bytes' => intval($item[$bytesIndex]),
117135
'ifdisposition' => 0,
118136
'disposition' => null,
119137
'ifdparameters' => 0,
@@ -122,30 +140,49 @@ protected static function extractPart($item)
122140
'parameters' => $parameters,
123141
];
124142

125-
if (isset($item[9][0])) {
143+
$dispositionIndex = 9;
144+
if ($type == 2) {
145+
$dispositionIndex = 11;
146+
}
147+
if (isset($item[$dispositionIndex][0])) {
126148
$attribute = null;
127149
$dispositionParameters = [];
128-
$part->disposition = $item[9][0];
129-
foreach ($item[9][1] as $value) {
130-
if (empty($attribute)) {
131-
$attribute = [
132-
'attribute' => $value,
133-
'value' => null,
134-
];
135-
} else {
136-
$attribute['value'] = $value;
137-
$dispositionParameters[] = (object) $attribute;
138-
$attribute = null;
150+
$part->disposition = $item[$dispositionIndex][0];
151+
if (isset($item[$dispositionIndex][1]) && is_array($item[$dispositionIndex][1])) {
152+
foreach ($item[$dispositionIndex][1] as $value) {
153+
if (empty($attribute)) {
154+
$attribute = [
155+
'attribute' => $value,
156+
'value' => null,
157+
];
158+
} else {
159+
$attribute['value'] = $value;
160+
$dispositionParameters[] = (object)$attribute;
161+
$attribute = null;
162+
}
139163
}
140164
}
141165
$part->dparameters = $dispositionParameters;
142-
$part->ifdparameters = count($dispositionParameters);
166+
$part->ifdparameters = 1;
143167
$part->ifdisposition = 1;
144168
} else {
145169
unset($part->disposition);
146170
unset($part->dparameters);
147171
}
148172

173+
return self::processSubParts($item, $part);
174+
}
175+
176+
protected static function processSubParts($item, $part)
177+
{
178+
if ($item[0] != 'MESSAGE') {
179+
return $part;
180+
}
181+
182+
$part->parts = [
183+
$item[8]
184+
];
185+
149186
return $part;
150187
}
151188

src/ImapClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,7 +2486,7 @@ public function fetch($mailbox, $message_set, $is_uid = false, $query_items = ar
24862486
$ln = 0;
24872487

24882488
// Tokenize response and assign to object properties
2489-
while (list($name, $value) = $this->tokenizeResponse($line, 2)) {
2489+
while (@list($name, $value) = $this->tokenizeResponse($line, 2)) {
24902490
if ($name == 'UID') {
24912491
$result[$id]->uid = intval($value);
24922492
}
@@ -3993,7 +3993,7 @@ public static function uncompressMessageSet($messages)
39933993

39943994
foreach ($messages as $idx => $part) {
39953995
$items = explode(':', $part);
3996-
$max = max($items[0], $items[1]);
3996+
$max = max($items[0], $items[1] ?? $items[0]);
39973997

39983998
for ($x=$items[0]; $x<=$max; $x++) {
39993999
$result[] = (int)$x;

src/Mailbox.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,18 @@ public static function deleteMailbox($imap, $mailbox)
303303
*/
304304
public static function append($imap, $folder, $message, $options = null, $internalDate = null)
305305
{
306-
if (is_a($imap, Connection::class)) {
307-
$folderParts = explode('}', $folder);
308-
$client = $imap->getClient();
309-
#$client->setDebug(true);
310-
$mailbox = empty($folderParts[1]) ? 'INBOX' : $folderParts[1];
306+
if (!is_a($imap, Connection::class)) {
307+
return Errors::invalidImapConnection(debug_backtrace(), 1, false);
308+
}
311309

312-
return $client->append($mailbox, $message);
310+
$folderParts = explode('}', $folder);
311+
$client = $imap->getClient();
313312

314-
} elseif (IMAP2_RETROFIT_MODE && is_resource($imap) && get_resource_type($imap) == 'imap') {
315-
return imap_append($imap, $folder, $message, $options, $internalDate);
316-
}
313+
$mailbox = empty($folderParts[1]) ? 'INBOX' : $folderParts[1];
317314

318-
trigger_error(Errors::invalidImapConnection(debug_backtrace(), 1), E_USER_WARNING);
315+
$success = $client->append($mailbox, $message);
319316

320-
return false;
317+
return boolval($success);
321318
}
322319

323320
public static function getSubscribed($imap, $mailbox)

src/ResultIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function is_empty()
157157
*/
158158
public function count()
159159
{
160-
if ($this->meta['count'] !== null)
160+
if (isset($this->meta['count']) && $this->meta['count'] !== null)
161161
return $this->meta['count'];
162162

163163
if (empty($this->raw_data)) {

tests/CompatibilityTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ public function testFetchStructure()
517517
$imap1 = imap_open($this->mailbox, $this->username, $this->password);
518518
$imap2 = imap2_open($this->mailbox, $this->username, $this->accessToken, OP_XOAUTH2);
519519

520+
/* Questo test va riabilitato è funzionante
520521
$messages = imap_fetch_overview($imap1, '1:5');
521522
$this->assertCount(5, $messages);
522523
@@ -540,6 +541,27 @@ public function testFetchStructure()
540541
$this->assertEquals($structure1, $structure2);
541542
}
542543
}
544+
*/
545+
546+
$emlFiles = [
547+
'embedded_email.eml'
548+
];
549+
foreach ($emlFiles as $file) {
550+
$message = file_get_contents('tests/fixtures/'.$file);
551+
$mailbox1 = uniqid('test_');
552+
$mailbox2 = uniqid('test_');
553+
imap_createmailbox($imap1, $this->mailbox.$mailbox1);
554+
#imap2_createmailbox($imap2, $this->mailbox.$mailbox2);
555+
imap_append($imap1, $this->mailbox.$mailbox1, $message);
556+
#imap2_append($imap2, $this->mailbox.$mailbox2, $message);
557+
imap_reopen($imap1, $this->mailbox.$mailbox1);
558+
imap2_reopen($imap2, $this->mailbox.$mailbox1);
559+
$structure1 = imap_fetchstructure($imap1, 1);
560+
$structure2 = imap2_fetchstructure($imap2, 1);
561+
file_put_contents('t1.json', json_encode($structure1, JSON_PRETTY_PRINT));
562+
file_put_contents('t2.json', json_encode($structure2, JSON_PRETTY_PRINT));
563+
die();
564+
}
543565

544566
imap_close($imap1);
545567
imap2_close($imap2);

tests/fixtures/embedded_email.eml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Return-Path: [email protected]
2+
Received: from webmail.my-office.cz (localhost [127.0.0.1])
3+
by keira.cofis.cz
4+
; Fri, 29 Jan 2016 14:25:40 +0100
5+
MIME-Version: 1.0
6+
Content-Type: multipart/mixed;
7+
boundary="=_5d4b2de51e6aad0435b4bfbd7a23594a"
8+
Date: Fri, 29 Jan 2016 14:25:40 +0100
9+
10+
11+
Subject: embedded message
12+
Message-ID: <[email protected]>
13+
14+
User-Agent: Roundcube Webmail/1.0.0
15+
16+
--=_5d4b2de51e6aad0435b4bfbd7a23594a
17+
Content-Transfer-Encoding: 7bit
18+
Content-Type: text/plain; charset=US-ASCII;
19+
format=flowed
20+
21+
email that contains embedded message
22+
--=_5d4b2de51e6aad0435b4bfbd7a23594a
23+
Content-Transfer-Encoding: 8bit
24+
Content-Type: message/rfc822;
25+
name=demo.eml
26+
Content-Disposition: attachment;
27+
filename=demo.eml;
28+
size=889
29+
30+
Return-Path: [email protected]
31+
Received: from webmail.my-office.cz (localhost [127.0.0.1])
32+
by keira.cofis.cz
33+
; Fri, 29 Jan 2016 14:22:13 +0100
34+
MIME-Version: 1.0
35+
Content-Type: multipart/mixed;
36+
boundary="=_995890bdbf8bd158f2cbae0e8d966000"
37+
Date: Fri, 29 Jan 2016 14:22:13 +0100
38+
39+
40+
Subject: demo
41+
Message-ID: <[email protected]>
42+
43+
User-Agent: Roundcube Webmail/1.0.0
44+
45+
--=_995890bdbf8bd158f2cbae0e8d966000
46+
Content-Transfer-Encoding: 7bit
47+
Content-Type: text/plain; charset=US-ASCII;
48+
format=flowed
49+
50+
demo text
51+
--=_995890bdbf8bd158f2cbae0e8d966000
52+
Content-Transfer-Encoding: base64
53+
Content-Type: text/plain;
54+
name=testfile.txt
55+
Content-Disposition: attachment;
56+
filename=testfile.txt;
57+
size=29
58+
59+
IHRoaXMgaXMgY29udGVudCBvZiB0ZXN0IGZpbGU=
60+
--=_995890bdbf8bd158f2cbae0e8d966000--
61+
62+
63+
--=_5d4b2de51e6aad0435b4bfbd7a23594a--

0 commit comments

Comments
 (0)