xref: /PHP-8.2/ext/imap/tests/setup/imap_include.inc (revision d2bb59cc)
1<?php
2/** If required change these values to make the test runs */
3const IMAP_SERVER_NO_DEBUG = '{127.0.0.1:143/norsh}';
4const IMAP_SERVER_DEBUG = '{127.0.0.1:143/debug/norsh}';
5const IMAP_SERVER = IMAP_SERVER_DEBUG;
6const IMAP_DEFAULT_MAILBOX = IMAP_SERVER . 'INBOX';
7const IMAP_MAIL_DOMAIN = 'example.com';
8const IMAP_ADMIN_USER = 'webmaster'; // a user with admin access
9const IMAP_MAILBOX_USERNAME = IMAP_ADMIN_USER . '@' . IMAP_MAIL_DOMAIN;
10const IMAP_MAILBOX_PASSWORD = 'p4ssw0rd';
11const IMAP_MAILBOX_PHPT_PREFIX = 'phpttest';
12/** Tests require 4 valid userids */
13const IMAP_USERS = ["webmaster", "info", "admin", "foo"];
14
15/** list of fields to expect */
16const MANDATORY_OVERVIEW_FIELDS = [
17    'size',
18    'uid',
19    'msgno',
20    'recent',
21    'flagged',
22    'answered',
23    'deleted',
24    'seen',
25    'draft',
26    'udate',
27];
28
29// record test start time (used by displayOverviewFields())
30$start_time = time();
31
32/**
33 * Display all fields in an element from an imap_fetch_overview() response
34 *
35 * Special handling for 'udate', which will vary run-to-run; assumes an IMAP
36 * server with its clock synced to the current system, which is consistent with
37 * setup instructions in ext/imap/tests/README.md
38 *
39 * @param $resp
40 * @param string[] $fields
41 */
42function displayOverviewFields($resp, array $fields = MANDATORY_OVERVIEW_FIELDS) {
43    global $start_time;
44    foreach ($fields as $mf) {
45        $z = $resp->$mf;
46        if ($mf == 'udate') {
47            if (($z >= $start_time) && ($z <= time())) {
48                echo "$mf is OK\n";
49            } else {
50                echo "$mf is BAD ($z)\n";
51            }
52        } else {
53            echo "$mf is $z\n";
54        }
55    }
56}
57
58
59/**
60 * Create a test mailbox and populate with msgs
61 *
62 * @param string mailbox_suffix Suffix used to uniquely identify mailboxes
63 * @param int message_count number of test msgs to be written to new mailbox
64 * @param null $new_mailbox
65 * @param bool $simpleMessages
66 * @param int $flags OP_* (or CL_EXPUNGE) flags to pass to imap_open() sub-call
67 * @return resource IMAP stream to new mailbox
68 * @throws Exception
69 */
70function setup_test_mailbox(
71    string $mailbox_suffix,
72    int $message_count,
73    &$new_mailbox = null,
74    bool $simpleMessages = true,
75    int $flags = 0,
76){
77    // open a stream to default mailbox
78    $imap_stream = imap_open(IMAP_DEFAULT_MAILBOX, IMAP_MAILBOX_USERNAME, IMAP_MAILBOX_PASSWORD, flags: $flags);
79
80    if ($imap_stream === false) {
81        throw new Exception("Cannot connect to IMAP server " . IMAP_SERVER . ": " . imap_last_error());
82    }
83
84    echo "Create a temporary mailbox and add " . $message_count .  " msgs\n";
85    $new_mailbox = create_mailbox($imap_stream, $mailbox_suffix, $message_count, $simpleMessages);
86
87    echo "New mailbox created\n";
88
89    // reopen stream to new mailbox
90    if (imap_reopen($imap_stream, $new_mailbox) === false) {
91        throw new Exception("Can't re-open '$new_mailbox' mailbox: " . imap_last_error());
92    }
93
94    return $imap_stream;
95}
96
97/**
98 * Create mailbox and fill with generic emails
99 *
100 * @param resource $imap_stream
101 * @param string $mailbox_suffix
102 * @param int $message_count
103 * @param bool $simpleMessages
104 * @return string
105 * @throws Exception
106 */
107function create_mailbox($imap_stream, string $mailbox_suffix, int $message_count, bool $simpleMessages = true): string {
108    $mailbox = IMAP_DEFAULT_MAILBOX . '.' . IMAP_MAILBOX_PHPT_PREFIX . $mailbox_suffix;
109
110    $mailboxes = imap_getmailboxes($imap_stream, $mailbox, '*');
111
112    // check mailbox does not already exist
113    if ($mailboxes) {
114        foreach($mailboxes as $value) {
115            if ($value->name == $mailbox) {
116                throw new Exception("Mailbox '$mailbox' already exists");
117            }
118        }
119    }
120
121    if (imap_createmailbox($imap_stream, $mailbox) === false) {
122        throw new Exception("Can't create a temporary mailbox: " . imap_last_error());
123    }
124
125    // Add number of test msgs requested
126    if ($message_count > 0) {
127        populate_mailbox($imap_stream, $mailbox, $message_count, $simpleMessages);
128    }
129
130    return $mailbox;
131}
132
133function setup_test_mailbox_for_uid_tests(string $mailbox_suffix, &$msg_no = null, &$msg_uid = null)
134{
135    $mail_box = setup_test_mailbox($mailbox_suffix, 10);
136    echo "Delete 4 messages for Unique ID generation\n";
137    // Delete messages to remove the numerical ordering
138    imap_delete($mail_box, 3);
139    imap_delete($mail_box, 4);
140    imap_delete($mail_box, 5);
141    imap_delete($mail_box, 6);
142    imap_expunge($mail_box);
143    $msg_no = 5;
144    $msg_uid = 9;
145
146    return $mail_box;
147}
148
149/**
150 * Populate a mailbox with generic emails
151 *
152 * @param resource $imap_stream
153 * @param string $mailbox
154 * @param int $message_count
155 * @param bool $simpleMessages
156 */
157function populate_mailbox($imap_stream, string $mailbox, int $message_count, bool $simpleMessages = true): void {
158    for ($i = 1; $i <= $message_count; $i++) {
159        if ($simpleMessages) {
160            $msg =  "From: foo@anywhere.com\r\n"
161                . "To: ". IMAP_USERS[0] . "@" . IMAP_MAIL_DOMAIN . "\r\n"
162                . "Subject: test$i\r\n"
163                . "\r\n"
164                . "$i: this is a test message, please ignore\r\nnewline";
165        } else {
166            $envelope["from"]= "foo@anywhere.com";
167            $envelope["to"]  = IMAP_USERS[0] . "@" . IMAP_MAIL_DOMAIN;
168            $envelope["subject"] = "Test msg $i";
169
170            $part1["type"] = TYPEMULTIPART;
171            $part1["subtype"] = "mixed";
172
173            $part2["type"] = TYPETEXT;
174            $part2["subtype"] = "plain";
175            $part2["description"] = "imap_mail_compose() function";
176            $part2["contents.data"] = "message 1:xxxxxxxxxxxxxxxxxxxxxxxxxx";
177
178            $part3["type"] = TYPETEXT;
179            $part3["subtype"] = "plain";
180            $part3["description"] = "Example";
181            $part3["contents.data"] = "message 2:yyyyyyyyyyyyyyyyyyyyyyyyyy";
182
183            $part4["type"] = TYPETEXT;
184            $part4["subtype"] = "plain";
185            $part4["description"] = "Return Values";
186            $part4["contents.data"] = "message 3:zzzzzzzzzzzzzzzzzzzzzzzzzz";
187
188            $body[1] = $part1;
189            $body[2] = $part2;
190            $body[3] = $part3;
191            $body[4] = $part4;
192
193            $msg = imap_mail_compose($envelope, $body);
194        }
195
196        imap_append($imap_stream, $mailbox, $msg);
197    }
198}
199
200/**
201 * Get the mailbox name from a mailbox description, i.e strip off server details.
202 *
203 * @param string mailbox complete mailbox name
204 * @return string mailbox name
205 */
206function get_mailbox_name(string $mailboxName): string {
207
208    if (preg_match('/\{.*?\}(.*)/', $mailboxName, $match) != 1) {
209        throw new Exception("Unrecognized mailbox name '$mailboxName'");
210    }
211
212    return $match[1];
213}
214