📄 imap.class.inc
字号:
<?php/*** @copyright Intermesh 2004* @author Merijn Schering <mschering@intermesh.nl>* @version $Revision: 1.30 $ $Date: 2006/03/29 14:13:12 $* * This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation; either version 2 of the License, or (at your* option) any later version.*//*** A class to handle IMAP and POP-3 connections.** @package Framework* @subpackage Mail* @author Merijn Schering <mschering@intermesh.nl>* @since Group-Office 1.0*/require_once ($GO_CONFIG->class_path.'mail/RFC822.class.inc');class imap { /** * The recource ID that points to the IMAP stream * * @var Recource ID * @access private */ var $conn; /** * The number of messages in the current mailbox * * @var Int * @access private */ var $count; /** * The number of unseen messages in the current mailbox * * @var Int * @access private */ var $unseen; /** * Array of messages that were fetched with imap_fetch_overview() * * @var Array * @access private */ var $messages = array (); /** * Current message that is get with get_message(); * * @var Array * @access private */ var $message; /** * The type of the mailserver. IMAP or POP-3 * * @var String * @access private */ var $servertype; /** * The string to connect to the mailserver with imap_open(); * * @var String * @access private */ var $connectstring; /** * Array of mailboxes fetched with get_mailboxes(); * * @var Array * @access private */ var $mailboxes; /** * Currently opened mailbox * * @var String * @access private */ var $mailbox; /** * Sorted message uid's returned by imap_sort(); * * @var Array * @access private */ var $sort; /** * Array with mailbox info fetched with imap_mailboxinfo() * * @var Array * @access private */ var $mailbox_info; /** * Class to handle formatting of e-mail address strings. * * @var Object * @access private */ var $RFC822; /** * Constructor. Initialises RFC822 class * * @access public * @return void */ function imap() { $this->RFC822 = new RFC822(); } /** * Opens a connection to server * * @param string $host The hostname of the mailserver * @param string $type The type of the mailserver (IMAP or POP-3) * @param int $port The port to connect to * @param string $username The username * @param string $password The password * @param string $mailbox The mailbox to open * @param string $flags Connection flags (See PHP docs imap_open() * @param bool $ssl Connect in SSL mode or not * @param bool $novalidate_cert Don't validate SSL certificate * @access public * @return mixed The recource ID on success or false on failure */ function open($host, $type, $port, $username, $password, $mailbox = "INBOX", $flags = 0, $ssl = false, $novalidate_cert = false) { global $GO_CONFIG; $this->servertype = strtolower($type); $this->mailbox = $mailbox; $this->connectstring = $host.":".$port."/".$this->servertype.$GO_CONFIG->email_connectstring_options; if ($ssl) { $this->connectstring .= '/ssl'; } if ($novalidate_cert) { $this->connectstring .= '/novalidate-cert'; } if ($flags != 0) { $this->conn = @ imap_open("{".$this->connectstring."}".$this->mailbox, $username, $password, $flags); } else { $this->conn = @ imap_open("{".$this->connectstring."}".$this->mailbox, $username, $password); } return $this->conn; } /** * Close the connection to the mailserver * * @access public * @return void */ function close() { unset ($this->messages); unset ($this->count); unset ($this->unseen); unset ($this->next_message_id); @ imap_close($this->conn); unset ($this->conn); } /** * Count total and new messages on server * * @param string $mailbox The mailbox to get status of * @access public * @return void */ function status($mailbox = false, $options=SA_UNSEEN) { if (!$mailbox) $mailbox = $this->mailbox; $status = imap_status($this->conn, "{".$this->connectstring."}".$mailbox, $options); return $status; } /** * Count total and new messages on server * * @param string $mailbox The mailbox to get status of * @access public * @return void */ function get_unseen($mailbox = false, $recursive=false) { if (!$mailbox) $mailbox = $this->mailbox; if(!$recursive) { $status = imap_status($this->conn, "{".$this->connectstring."}".$mailbox, SA_UNSEEN); return $status->unseen; }else { $unseen =0; //Sometimes INBOX is subscribed, sometimes not. So check it manually and skip it //if it's subscribed. if($mailbox == 'INBOX') { $status = imap_status($this->conn, "{".$this->connectstring."}INBOX", SA_UNSEEN); $unseen+=$status->unseen; } $folders = $this->get_subscribed($mailbox); foreach($folders as $folder) { if($folder['name'] != 'INBOX') { $status = imap_status($this->conn, "{".$this->connectstring."}".$folder['name'], SA_UNSEEN); $unseen+=$status->unseen; } } return $unseen; } } /** * Check if this is an IMAP server * * @param string $mailbox The mailbox to get status of * @access public * @return bool True if this is an IMAP server */ function is_imap() { if ($this->servertype == "imap") { return true; } else { return false; } } /** * Sort message UID's into $this->sort (see imap_sort() PHP docs) * * @param int $sort_type The column * @param string $reverse Reverse sorting (0 or 1) * @param string $search Search query * @access public * @return int Number of sorted messages */ function sort($sort_type = SORTDATE, $reverse = "1", $query = '') { if ($query != '') { $this->sort = imap_sort($this->conn, $sort_type, $reverse, SE_UID, trim($query)); } else { $this->sort = imap_sort($this->conn, $sort_type, $reverse, SE_UID); } if ($this->sort) { $this->count = count($this->sort); } else { $this->sort = array (); $this->count = 0; } return $this->count; } function build_search_query($subject = '', $from = '', $to = '', $cc = '', $body = '', $before = '', $since = '', $before = '', $since = '', $flagged = '', $answered = '', $seen='') { $query = ''; if ($subject != '') { $query .= 'SUBJECT "'.$subject.'" '; } if ($from != '') { $query .= 'FROM "'.$from.'" '; } if ($to != '') { $query .= 'TO "'.$to.'" '; } if ($cc != '') { $query .= 'CC "'.$cc.'" '; } if ($body != '') { $query .= 'BODY "'.$body.'" '; } if ($before != '') { $unix_before = date_to_unixtime($before); $query .= 'BEFORE "'.date('d-M-Y', $unix_before).'" '; } if ($since != '') { $unix_since = date_to_unixtime($since); $query .= 'SINCE "'.date('d-M-Y', $unix_since).'" '; } if ($flagged != '') { $query .= $flagged.' '; } if ($answered != '') { $query .= $answered.' '; } if ($seen != '') { $query .= $seen.' '; } return $query; } /** * Fetches the sorted messages and puts them into $this->messages * * @param int $first The first message to fetch * @param int $offset The number of messages to fetch * @access public * @return void */ function get_messages($first, $offset) { //Use imap_fetch_overview and sort them correctly //for performance. if (!isset($this->sort)) { $this->sort(); } $last = $first + $offset; if ($offset == 0 || ($last > $this->count)) { $last = $this->count; } $get = ''; $index = 0; $sorted_uids = array (); for ($i = $first; $i < $last; $i ++) { if ($i != $first) { $get .= ','; } $get .= $this->sort[$i]; $sorted_uids[] = $this->sort[$i]; $index ++; } $tmp = $sorted_uids; sort($tmp); //this is the way they are returned by imap_fetch_overview $uid_map = array (); $count = count($tmp); for ($i = 0; $i < $count; $i ++) { $uid_map[$tmp[$i]] = $i; } $tmp_overviews = imap_fetch_overview($this->conn, $get, FT_UID); while ($message_overview = array_shift($tmp) && $uid = array_shift($sorted_uids)) { $this->messages[] = $tmp_overviews[$uid_map[$uid]]; } } function _next_message() { if (!isset ($this->sort)) { $this->sort(); } if($uid = array_shift($this->sort)) { return $this->get_message($uid); } return false; } /** * Loop trough all messages after calling get_messages() * * @access public * @return array The E-mail message elements */ function next_message() { if (!isset ($this->messages)) { die('Call get_messages() first'); } if ($overview = array_shift($this->messages)) { unset($this->message); $this->message["uid"] = $overview->uid; $this->message["msgno"] = $overview->msgno; //$this->message["udate"] = strtotime(str_replace('CEST', '', $overview->date)); $this->message["date"] = $overview->date; $this->message['subject'] = isset ($overview->subject) ? enc_utf8($overview->subject) : ''; $from = $this->RFC822->parse_address_list(enc_utf8($overview->from)); if (isset ($from[0])) { $email = $from[0]['email']; } $personal = isset ($from[0]['personal']) ? $from[0]['personal'] : ''; $this->message["from"] = $personal == '' ? $email : $personal; $this->message["sender"] = $email; $this->message["flagged"] = $overview->flagged; $this->message["size"] = $overview->size; $this->message["answered"] = $overview->answered; //$this->message['new'] = (($overview->recent==1 || $overview->seen==0) && $this->is_imap()) ? true : false; $this->message['new'] = ($overview->seen == 0 && $this->is_imap()) ? true : false; $headerinfo = imap_headerinfo($this->conn, imap_msgno($this->conn,$overview->uid)); $this->message['udate'] = $headerinfo->udate; if (isset ($headerinfo->to)) { $tmp = $headerinfo->to; for ($x = 0; $x < count($tmp); $x ++) { $email = ''; if (isset ($tmp[$x]->mailbox)) { $host = isset ($tmp[$x]->host) ? '@'.$tmp[$x]->host : ''; $email = $tmp[$x]->mailbox.$host; } $personal = isset ($tmp[$x]->personal) ? enc_utf8($tmp[$x]->personal) : ''; $this->message['to'][$x] = $this->RFC822->write_address($personal, $email); } } $this->message['udate'] = $headerinfo->udate; return $this->message; } else { return false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -