1<?php
2// Change these to make tests run successfully
3$server   = '{localhost}';
4$default_mailbox = $server . "INBOX";
5$domain = "example.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/**
13 * Create a test mailbox and populate with msgs
14 *
15 * @para, string mailbox_suffix Suffix used to uniquely identify mailboxes
16 * @param int message_count number of test msgs to be written to new mailbox
17 *
18 * @return IMAP stream to new mailbox on sucesss; FALSE on failure
19 */
20function setup_test_mailbox($mailbox_suffix, $message_count, &$new_mailbox = null, $msg_type = "simple"){
21	global $server, $default_mailbox, $username, $password;
22
23	// open a stream to default mailbox
24	$imap_stream = imap_open($default_mailbox, $username, $password);
25
26	if ($imap_stream === false) {
27		echo "Cannot connect to IMAP server $server: " . imap_last_error() . "\n";
28		return false;
29	}
30
31	echo "Create a temporary mailbox and add " . $message_count .  " msgs\n";
32	$new_mailbox = create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type);
33	if ($new_mailbox === false) {
34	   echo "Cant create a temporary mailbox: " . imap_last_error(). "\n";
35	   return false;
36	}
37
38	echo ".. mailbox '$new_mailbox' created\n";
39
40	// reopen stream to new mailbox
41	if (imap_reopen($imap_stream, $new_mailbox) === false) {
42		echo "cant re-open '$new_mailbox' mailbox: " . imap_last_error() . "\n";
43		return false;
44	}
45
46	return $imap_stream;
47}
48
49/**
50 * Create mailbox and fill with generic emails
51 *
52 * @param resource $imap_stream
53 * @param string $mailbox
54 */
55function create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type= "simple"){
56	global $default_mailbox, $mailbox_prefix;
57	$mailbox = $default_mailbox . "." . $mailbox_prefix . $mailbox_suffix;
58
59	$mailboxes = imap_getmailboxes($imap_stream, $mailbox, '*');
60
61	// check mailbox does not already exist
62	if ($mailboxes) {
63		foreach($mailboxes as $value) {
64			if ($value->name == $mailbox) {
65				exit ("TEST FAILED : Mailbox '$mailbox' already exists\n");
66			}
67		}
68	}
69
70	if (imap_createmailbox($imap_stream, $mailbox) === false) {
71		return false;
72	}
73
74	// Add number of test msgs requested
75	if ($message_count > 0) {
76		populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type);
77	}
78
79	return $mailbox;
80}
81
82/**
83 * Populate a mailbox with generic emails
84 *
85 * @param resource $imap_stream
86 * @param string $mailbox
87 */
88function populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type = "simple"){
89
90	global $users, $domain;
91
92	for($i = 1; $i <= $message_count; $i++) {
93		if ($msg_type == "simple") {
94			$msg =  "From: foo@anywhere.com\r\n"
95				. "To: $users[0]@$domain\r\n"
96				. "Subject: test$i\r\n"
97				. "\r\n"
98				. "$i: this is a test message, please ignore\r\n";
99		} else {
100			$envelope["from"]= "foo@anywhere.com";
101			$envelope["to"]  = "$users[0]@$domain";
102			$envelope["subject"] = "Test msg $i";
103
104			$part1["type"] = TYPEMULTIPART;
105			$part1["subtype"] = "mixed";
106
107			$part2["type"] = TYPETEXT;
108			$part2["subtype"] = "plain";
109			$part2["description"] = "imap_mail_compose() function";
110			$part2["contents.data"] = "message 1:xxxxxxxxxxxxxxxxxxxxxxxxxx";
111
112			$part3["type"] = TYPETEXT;
113			$part3["subtype"] = "plain";
114			$part3["description"] = "Example";
115			$part3["contents.data"] = "message 2:yyyyyyyyyyyyyyyyyyyyyyyyyy";
116
117			$part4["type"] = TYPETEXT;
118			$part4["subtype"] = "plain";
119			$part4["description"] = "Return Values";
120			$part4["contents.data"] = "message 3:zzzzzzzzzzzzzzzzzzzzzzzzzz";
121
122			$body[1] = $part1;
123			$body[2] = $part2;
124			$body[3] = $part3;
125			$body[4] = $part4;
126
127			$msg = imap_mail_compose($envelope, $body);
128		}
129
130		imap_append($imap_stream, $mailbox, $msg);
131	}
132}
133
134/**
135 * Get the mailbox name from a mailbox decription, i.e strip off server details.
136 *
137 * @param string mailbox complete mailbox name
138 * @return mailbox name
139 */
140function get_mailbox_name($mailbox){
141
142	if (preg_match('/\{.*?\}(.*)/', $mailbox, $match) != 1) {
143		echo "Unrecpognized mailbox name\n";
144		return false;
145	}
146
147	return $match[1];
148}
149
150?>
151