xref: /PHP-8.2/ext/imap/tests/setup/clean.inc (revision c4646371)
1<?php
2require_once 'imap_include.inc';
3
4if (!isset($mailbox_suffix)) { throw new Exception("No suffix provided"); }
5
6$imap_stream = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD);
7
8// delete all msgs in default mailbox, i.e INBOX
9$check = imap_check($imap_stream);
10for ($i = 1; $i <= $check->Nmsgs; $i++) {
11    imap_delete($imap_stream, $i);
12}
13
14
15$mailboxes = imap_getmailboxes($imap_stream, IMAP_SERVER, '*');
16
17if (!is_array($mailbox_suffix)) {
18    $mailbox_suffixes = [$mailbox_suffix];
19} else {
20    $mailbox_suffixes = $mailbox_suffix;
21}
22
23foreach ($mailbox_suffixes as $mailbox_suffix) {
24    foreach($mailboxes as $value) {
25        // Only delete mailbox with our prefix (+ optional test suffix)
26        if (preg_match('/\{.*?\}INBOX\.' . IMAP_MAILBOX_PHPT_PREFIX . $mailbox_suffix .'$/', $value->name, $match) == 1) {
27            imap_deletemailbox($imap_stream, $value->name);
28        }
29    }
30}
31
32imap_close($imap_stream, CL_EXPUNGE);
33