Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / Email / Pop3Client.php
bloba870d849603f84ee07a85563ea6cdb8c1b581468
1 <?php
2 /** @package verysimple::Email */
4 /**
5 * Generic interface for connecting to a POP3 or IMAP Server
6 * GetPart, GetMimeType & GetAttachments based on code by Kevin Steffer
7 * <http://www.linuxscope.net/articles/mailAttachmentsPHP.html>
9 * @package verysimple::Email
10 * @author VerySimple Inc.
11 * @copyright 1997-2007 VerySimple, Inc.
12 * @license LGPL
13 * @version 1.1
15 class Pop3Client
17 private $mbox;
18 private $do_delete = false;
19 public static $VERSION = "1.1";
20 function Pop3Client()
22 if (! function_exists("imap_open")) {
23 require_once('PEAR.php');
24 if (! pear::loadExtension('imap')) {
25 throw new Exception("Pop3Client: Unable to load imap extension");
30 /**
31 * Opens a connection to the mail server
33 * @param string $user
34 * username
35 * @param string $pass
36 * password
37 * @param string $host
38 * host (ex mail.server.com)
39 * @param int $port
40 * port (default = 110)
41 * @param string $mbtype
42 * type of mailbox (default = pop3) (refer to www.php.net/imap_open)
43 * @param string $mbfolder
44 * name of folder to read (default = INBOX)
45 * @param int $options
46 * Flags for opening connect (refer to www.php.net/imap_open)
47 * @param int $retries
48 * Number of times to retry the connection (default = 1)
51 function Open($user, $pass, $host, $port = "110", $mbtype = "pop3", $mbfolder = "INBOX", $options = null, $retries = 1)
53 $this->mbox = imap_open("{" . $host . ":" . $port . "/" . $mbtype . "/notls}" . $mbfolder . "", $user, $pass, $options, $retries);
56 /**
57 * Delete a message from the mailbox
59 * @param int $msgnum
60 * The id of the message within the mailbox
63 function DeleteMessage($msgnum)
65 imap_delete($this->mbox, $msgnum);
66 $this->do_delete = true;
69 /**
70 * Close the connection to the mail server
72 * @param bool $empty_trash
73 * (default true) whether to empty the trash upon exit
76 function Close($empty_trash = true)
78 if ($this->do_delete && $empty_trash) {
79 imap_expunge($this->mbox);
82 imap_close($this->mbox);
85 /**
86 * Returns the number of messages in the mail account folder
88 * @return int Number of messages
91 function GetMessageCount()
93 $summary = $this->GetSummary();
94 return $summary->Nmsgs;
97 /**
98 * Returns an object with the following properties:
99 * Date, Driver, Mailbox, Nmsgs, Recent
101 * @return stdClass
103 function GetSummary()
105 return imap_check($this->mbox);
109 * Returns an array containing summary information about the messages.
110 * Use this function to list messages without downloading the entire
111 * contents of each one
113 * @return Array
115 function GetQuickHeaders()
117 return imap_headers($this->mbox);
121 * Returns an object containing message header information
123 * @param int $msgno
124 * message number to retrieve
125 * @return stdClass
126 * @link http://www.php.net/imap_headerinfo
128 function GetHeader($msgno)
130 return imap_headerinfo($this->mbox, $msgno);
134 * Returns an array containing all files attached to message
136 * @param int $msgno
137 * The index of the message to retrieve
138 * @param bool $include_raw_data
139 * The raw data is the actual contents of the attachment file. Setting this to FALSE will allow you to display the name, size, type, etc of all attachments without actually downloading the contents of the files. Use GetAttachmentRawData to retrieve the raw data for an individual attachment.
140 * @return array An array of attachments
143 function GetAttachments($msgno, $include_raw_data = true)
145 $struct = imap_fetchstructure($this->mbox, $msgno);
146 $contentParts = count($struct->parts);
147 $attachments = array ();
149 if ($contentParts >= 2) {
150 for ($i = 2; $i <= $contentParts; $i ++) {
151 $att [$i - 2] = imap_bodystruct($this->mbox, $msgno, $i);
152 // these extra bits help us later...
153 $att [$i - 2]->x_msg_id = $msgno;
154 $att [$i - 2]->x_part_id = $i;
157 for ($k = 0; $k < sizeof($att); $k ++) {
158 if (strtolower($att [$k]->parameters [0]->value) == "us-ascii" && $att [$k]->parameters [1]->value != "") {
159 $attachments [$k] = $this->_getPartFromStruct($att [$k], $include_raw_data);
160 } elseif (strtolower($att [$k]->parameters [0]->value) != "iso-8859-1") {
161 $attachments [$k] = $this->_getPartFromStruct($att [$k], $include_raw_data);
166 return $attachments;
168 private function _getPartFromStruct($struct, $include_raw_data)
170 // print_r($struct);
171 $part = null;
172 $part->msgnum = $struct->x_msg_id;
173 $part->partnum = $struct->x_part_id;
174 $part->filename = $struct->parameters [0]->value;
175 $part->type = $this->GetPrimaryType($struct);
176 $part->subtype = $struct->subtype;
177 $part->mimetype = $this->GetMimeType($struct);
178 $part->rawdata = (! $include_raw_data) ? null : $this->GetAttachmentRawData($struct->x_msg_id, $struct->x_part_id, $struct->encoding);
179 return $part;
183 * Returns the raw data for an attachment.
184 * The raw data is the actual file contents of an attachment.
186 * @param int $msgno
187 * message number
188 * @param int $partnum
189 * which attachment to retrieve
190 * @param int $encoding_id
191 * 0 = no encoding, 3 = base64, 4 = qprint
192 * @return mixed file contents of the attachment
195 function GetAttachmentRawData($msgno, $partnum, $encoding_id = 0)
197 $content = imap_fetchbody($this->mbox, $msgno, $partnum);
199 if ($encoding_id == 3) {
200 return imap_base64($content);
201 } elseif ($encoding_id == 4) {
202 return imap_qprint($content);
205 return $content;
209 * Returns a text representation of the MIME type of the primary part of a message
211 * @param object $structure
212 * message structure
213 * @return string
216 function GetPrimaryType(&$structure)
218 $primary_mime_type = array (
219 "TEXT",
220 "MULTIPART",
221 "MESSAGE",
222 "APPLICATION",
223 "AUDIO",
224 "IMAGE",
225 "VIDEO",
226 "OTHER"
228 return $primary_mime_type [( int ) $structure->type];
232 * Returns the MimeType of the primary part of the given message
234 * @param object $structure
235 * The message to inspect
236 * @return string Mime Type
239 function GetMimeType(&$structure)
241 if ($structure->subtype) {
242 return $this->GetPrimaryType($structure) . '/' . $structure->subtype;
245 return "TEXT/PLAIN";
249 * Returns what is best determined to be the body text of the messages
251 * @param int $msgnum
252 * Message number in the mailbox
253 * @param bool $prefer_html
254 * If an html version is provided, return that
255 * @return string The contents of the message body
258 function GetMessageBody($msgnum, $prefer_html = true)
260 if ($prefer_html) {
261 $body = $this->GetPart($msgnum, "TEXT/HTML");
262 $body = $body ? $body : $this->GetPart($msgnum, "TEXT/PLAIN");
263 } else {
264 $body = $this->GetPart($msgnum, "TEXT/PLAIN");
265 $body = $body ? $body : $this->GetPart($msgnum, "TEXT/HTML");
268 return $body;
272 * Returns a single part of a message
274 * @param int $msg_number
275 * The message number in the mailbox
276 * @param string $mime_type
277 * The mime type of the part to retrieve
278 * @param object $structure
279 * The message structure
280 * @param int $part_number
281 * The id of the part number within the message
282 * @return object The message part, or false if no match exists
285 function GetPart($msg_number, $mime_type, $structure = false, $part_number = false)
287 $stream = $this->mbox;
288 $prefix = "";
290 $structure = $structure ? $structure : imap_fetchstructure($stream, $msg_number);
292 if ($structure) {
293 if ($mime_type == $this->GetMimeType($structure)) {
294 $part_number = $part_number ? $part_number : "1";
295 $text = imap_fetchbody($stream, $msg_number, $part_number);
297 if ($structure->encoding == 3) {
298 return imap_base64($text);
299 } else if ($structure->encoding == 4) {
300 return imap_qprint($text);
301 } else {
302 return $text;
306 if ($structure->type == 1) { /* multipart */
307 while (list ( $index, $sub_structure ) = each($structure->parts)) {
308 if ($part_number) {
309 $prefix = $part_number . '.';
312 $data = $this->GetPart($msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
314 if ($data) {
315 return $data;
321 // no structure returned
322 return false;