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