[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /********************************************************************************* 3 ** The contents of this file are subject to the vtiger CRM Public License Version 1.0 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 * 10 ********************************************************************************/ 11 12 require_once ('modules/Settings/MailConverter/handlers/MailScannerInfo.php'); 13 require_once ('modules/Settings/MailConverter/handlers/MailRecord.php'); 14 15 /** 16 * Class to work with server mailbox. 17 */ 18 class Vtiger_MailBox { 19 // Mailbox credential information 20 var $_scannerinfo = false; 21 // IMAP connection instance 22 var $_imap = false; 23 // IMAP url to use for connecting 24 var $_imapurl = false; 25 // IMAP folder currently opened 26 var $_imapfolder = false; 27 // Should we need to expunge while closing imap connection? 28 var $_needExpunge = false; 29 30 // Mailbox crendential information (as a map) 31 var $_mailboxsettings = false; 32 33 /** DEBUG functionality. */ 34 var $debug = false; 35 function log($message, $force=false) { 36 global $log; 37 if($log && ($force || $this->debug)) { $log->debug($message); } 38 else if( ($force || $this->debug) ) echo "$message\n"; 39 } 40 41 /** 42 * Constructor 43 */ 44 function __construct($scannerinfo) { 45 $this->_scannerinfo = $scannerinfo; 46 $this->_mailboxsettings = $scannerinfo->getAsMap(); 47 48 if($this->_mailboxsettings['ssltype'] == '') $this->_mailboxsettings['ssltype'] = 'notls'; 49 if($this->_mailboxsettings['sslmethod']== '') $this->_mailboxsettings['sslmethod'] = 'novalidate-cert'; 50 51 $server = $this->_mailboxsettings['server']; 52 $serverPort = explode(':', $server); 53 if(empty($serverPort[1])) { 54 if($this->_mailboxsettings['protocol'] == 'pop3') { $port = '110'; } 55 else { 56 if($this->_mailboxsettings['ssltype'] == 'tls' || 57 $this->_mailboxsettings['ssltype'] == 'ssl') { 58 $port = '993'; 59 } 60 else $port = '143'; 61 } 62 } else { 63 $port = $serverPort[1]; 64 $this->_mailboxsettings['server'] = $serverPort[0]; 65 } 66 $this->_mailboxsettings['port'] = $port; 67 } 68 69 /** 70 * Connect to mail box folder. 71 */ 72 function connect($folder='INBOX') { 73 $imap = false; 74 $mailboxsettings = $this->_mailboxsettings; 75 $isconnected = false; 76 77 // Connect using last successful url 78 if($mailboxsettings['connecturl']) { 79 $connecturl = $mailboxsettings['connecturl']; 80 $this->log("Trying to connect using connecturl $connecturl$folder", true); 81 $imap = @imap_open("$connecturl$folder", $mailboxsettings['username'], $mailboxsettings['password']); 82 if($imap) { 83 $this->_imapurl = $connecturl; 84 $this->_imapfolder = $folder; 85 $isconnected = true; 86 87 $this->log("Successfully connected", true); 88 } 89 } 90 91 if(!$imap) { 92 $connectString = '{'. "$mailboxsettings[server]:$mailboxsettings[port]/$mailboxsettings[protocol]/$mailboxsettings[ssltype]/$mailboxsettings[sslmethod]" ."}"; 93 $connectStringShort = '{'. "$mailboxsettings[server]/$mailboxsettings[protocol]:$mailboxsettings[port]" ."}"; 94 95 $this->log("Trying to connect using $connectString$folder", true); 96 if(!$imap = @imap_open("$connectString$folder", $mailboxsettings[username], $mailboxsettings[password])) { 97 $this->log("Connect failed using $connectString$folder, trying with $connectStringShort$folder...", true); 98 $imap = @imap_open("$connectStringShort$folder", $mailboxsettings[username], $mailboxsettings[password]); 99 if($imap) { 100 $this->_imapurl = $connectStringShort; 101 $this->_imapfolder = $folder; 102 $isconnected = true; 103 $this->log("Successfully connected", true); 104 } else { 105 $this->log("Connect failed using $connectStringShort$folder", true); 106 } 107 } else { 108 $this->_imapurl = $connectString; 109 $this->_imapfolder = $folder; 110 $isconnected = true; 111 $this->log("Successfully connected", true); 112 } 113 } 114 115 $this->_imap = $imap; 116 return $isconnected; 117 } 118 119 /** 120 * Open the mailbox folder. 121 * @param $folder Folder name to open 122 * @param $reopen set to true for re-opening folder if open (default=false) 123 * @return true if connected, false otherwise 124 */ 125 function open($folder, $reopen=false) { 126 /** Avoid re-opening of the box if not requested. */ 127 if(!$reopen && ($folder == $this->_imapfolder)) return true; 128 129 if(!$this->_imap) return $this->connect($folder); 130 131 $mailboxsettings = $this->_mailboxsettings; 132 133 $isconnected = false; 134 $connectString = $this->_imapurl; 135 $this->log("Trying to open folder using $connectString$folder"); 136 $imap = @imap_open("$connectString$folder", $mailboxsettings[username], $mailboxsettings[password]); 137 if($imap) { 138 139 // Perform cleanup task before re-initializing the connection 140 $this->close(); 141 142 $this->_imapfolder = $folder; 143 $this->_imap = $imap; 144 $isconnected = true; 145 } 146 return $isconnected; 147 } 148 149 /** 150 * Get the mails based on searchquery. 151 * @param $folder Folder in which mails to be read. 152 * @param $searchQuery IMAP query, (default false: fetches mails newer from lastscan) 153 * @return imap_search records or false 154 */ 155 function search($folder, $searchQuery=false) { 156 if(!$searchQuery) { 157 $lastscanOn = $this->_scannerinfo->getLastscan($folder); 158 $searchfor = $this->_scannerinfo->searchfor; 159 160 if($searchfor && $lastscanOn) { 161 if($searchfor == 'ALL') { 162 $searchQuery = "SINCE $lastscanOn"; 163 } else { 164 $searchQuery = "$searchfor SINCE $lastscanOn"; 165 } 166 } else { 167 $searchQuery = $lastscanOn? "SINCE $lastscanOn" : "BEFORE ". $this->_scannerinfo->dateBasedOnMailServerTimezone('d-M-Y'); 168 } 169 } 170 if($this->open($folder)) { 171 $this->log("Searching mailbox[$folder] using query: $searchQuery"); 172 return imap_search($this->_imap, $searchQuery); 173 } 174 return false; 175 } 176 177 /** 178 * Get folder names (as list) for the given mailbox connection 179 */ 180 function getFolders() { 181 $folders = false; 182 if($this->_imap) { 183 $imapfolders = imap_list($this->_imap, $this->_imapurl, '*'); 184 if($imapfolders) { 185 foreach($imapfolders as $imapfolder) { 186 $folders[] = substr($imapfolder, strlen($this->_imapurl)); 187 } 188 } else { 189 return imap_last_error(); 190 } 191 } 192 return $folders; 193 } 194 195 /** 196 * Fetch the email based on the messageid. 197 * @param $messageid messageid of the email 198 * @param $fetchbody set to false to defer fetching the body, (default: true) 199 */ 200 function getMessage($messageid, $fetchbody=true) { 201 return new Vtiger_MailRecord($this->_imap, $messageid, $fetchbody); 202 } 203 204 /** 205 * Mark the message in the mailbox. 206 */ 207 function markMessage($messageid) { 208 $markas = $this->_scannerinfo->markas; 209 if ($this->_imap && $markas) { 210 if (strtoupper($markas) == 'SEEN') { 211 $markas = "\\Seen"; 212 imap_setflag_full($this->_imap, $messageid, $markas); 213 } else if (strtoupper($markas) == 'UNSEEN') { 214 imap_clearflag_full($this->_imap, $messageid, "\\Seen"); 215 } 216 } 217 } 218 219 /** 220 * Close the open IMAP connection. 221 */ 222 function close() { 223 if($this->_needExpunge) { 224 imap_expunge($this->_imap); 225 } 226 $this->_needExpunge = false; 227 if($this->_imap) { 228 imap_close($this->_imap); 229 $this->_imap = false; 230 } 231 } 232 } 233 234 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |