/modules/Settings/MailScanner/core/MailBox.php

https://bitbucket.org/jhunsinfotech/blue-blues · PHP · 222 lines · 141 code · 27 blank · 54 comment · 45 complexity · f753f852d0c2a5bd076c505c3e82802b MD5 · raw file

  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. require_once('modules/Settings/MailScanner/core/MailScannerInfo.php');
  12. require_once('modules/Settings/MailScanner/core/MailRecord.php');
  13. /**
  14. * Class to work with server mailbox.
  15. */
  16. class Vtiger_MailBox {
  17. // Mailbox credential information
  18. var $_scannerinfo = false;
  19. // IMAP connection instance
  20. var $_imap = false;
  21. // IMAP url to use for connecting
  22. var $_imapurl = false;
  23. // IMAP folder currently opened
  24. var $_imapfolder = false;
  25. // Should we need to expunge while closing imap connection?
  26. var $_needExpunge = false;
  27. // Mailbox crendential information (as a map)
  28. var $_mailboxsettings = false;
  29. /** DEBUG functionality. */
  30. var $debug = false;
  31. function log($message, $force=false) {
  32. global $log;
  33. if($log && ($force || $this->debug)) { $log->debug($message); }
  34. else if( ($force || $this->debug) ) echo "$message\n";
  35. }
  36. /**
  37. * Constructor
  38. */
  39. function __construct($scannerinfo) {
  40. $this->_scannerinfo = $scannerinfo;
  41. $this->_mailboxsettings = $scannerinfo->getAsMap();
  42. if($this->_mailboxsettings[ssltype] == '') $this->_mailboxsettings[ssltype] = 'notls';
  43. if($this->_mailboxsettings[sslmethod]== '') $this->_mailboxsettings[sslmethod] = 'novalidate-cert';
  44. if($this->_mailboxsettings[protocol] == 'pop3') { $port = '110'; }
  45. else {
  46. if($this->_mailboxsettings[ssltype] == 'tls' ||
  47. $this->_mailboxsettings[ssltype] == 'ssl') {
  48. $port = '993';
  49. }
  50. else $port = '143';
  51. }
  52. $this->_mailboxsettings[port] = $port;
  53. }
  54. /**
  55. * Connect to mail box folder.
  56. */
  57. function connect($folder='INBOX') {
  58. $imap = false;
  59. $mailboxsettings = $this->_mailboxsettings;
  60. $isconnected = false;
  61. // Connect using last successful url
  62. if($mailboxsettings['connecturl']) {
  63. $connecturl = $mailboxsettings['connecturl'];
  64. $this->log("Trying to connect using connecturl $connecturl$folder", true);
  65. $imap = @imap_open("$connecturl$folder", $mailboxsettings[username], $mailboxsettings[password]);
  66. if($imap) {
  67. $this->_imapurl = $connecturl;
  68. $this->_imapfolder = $folder;
  69. $isconnected = true;
  70. $this->log("Successfully connected", true);
  71. }
  72. }
  73. if(!$imap) {
  74. $connectString = '{'. "$mailboxsettings[server]:$mailboxsettings[port]/$mailboxsettings[protocol]/$mailboxsettings[ssltype]/$mailboxsettings[sslmethod]" ."}";
  75. $connectStringShort = '{'. "$mailboxsettings[server]/$mailboxsettings[protocol]:$mailboxsettings[port]" ."}";
  76. $this->log("Trying to connect using $connectString$folder", true);
  77. if(!$imap = @imap_open("$connectString$folder", $mailboxsettings[username], $mailboxsettings[password])) {
  78. $this->log("Connect failed using $connectString$folder, trying with $connectStringShort$folder...", true);
  79. $imap = @imap_open("$connectStringShort$folder", $mailboxsettings[username], $mailboxsettings[password]);
  80. if($imap) {
  81. $this->_imapurl = $connectStringShort;
  82. $this->_imapfolder = $folder;
  83. $isconnected = true;
  84. $this->log("Successfully connected", true);
  85. } else {
  86. $this->log("Connect failed using $connectStringShort$folder", true);
  87. }
  88. } else {
  89. $this->_imapurl = $connectString;
  90. $this->_imapfolder = $folder;
  91. $isconnected = true;
  92. $this->log("Successfully connected", true);
  93. }
  94. }
  95. $this->_imap = $imap;
  96. return $isconnected;
  97. }
  98. /**
  99. * Open the mailbox folder.
  100. * @param $folder Folder name to open
  101. * @param $reopen set to true for re-opening folder if open (default=false)
  102. * @return true if connected, false otherwise
  103. */
  104. function open($folder, $reopen=false) {
  105. /** Avoid re-opening of the box if not requested. */
  106. if(!$reopen && ($folder == $this->_imapfolder)) return true;
  107. if(!$this->_imap) return $this->connect($folder);
  108. $mailboxsettings = $this->_mailboxsettings;
  109. $isconnected = false;
  110. $connectString = $this->_imapurl;
  111. $this->log("Trying to open folder using $connectString$folder");
  112. $imap = @imap_open("$connectString$folder", $mailboxsettings[username], $mailboxsettings[password]);
  113. if($imap) {
  114. // Perform cleanup task before re-initializing the connection
  115. $this->close();
  116. $this->_imapfolder = $folder;
  117. $this->_imap = $imap;
  118. $isconnected = true;
  119. }
  120. return $isconnected;
  121. }
  122. /**
  123. * Get the mails based on searchquery.
  124. * @param $folder Folder in which mails to be read.
  125. * @param $searchQuery IMAP query, (default false: fetches mails newer from lastscan)
  126. * @return imap_search records or false
  127. */
  128. function search($folder, $searchQuery=false) {
  129. if(!$searchQuery) {
  130. $lastscanOn = $this->_scannerinfo->getLastscan($folder);
  131. $searchfor = $this->_scannerinfo->searchfor;
  132. if($searchfor && $lastscanOn) {
  133. if($searchfor == 'ALL') {
  134. $searchQuery = "SINCE $lastscanOn";
  135. } else {
  136. $searchQuery = "$searchfor SINCE $lastscanOn";
  137. }
  138. } else {
  139. $searchQuery = $lastscanOn? "SINCE $lastscanOn" : "BEFORE ". date('d-M-Y');
  140. }
  141. }
  142. if($this->open($folder)) {
  143. $this->log("Searching mailbox[$folder] using query: $searchQuery");
  144. return imap_search($this->_imap, $searchQuery);
  145. }
  146. return false;
  147. }
  148. /**
  149. * Get folder names (as list) for the given mailbox connection
  150. */
  151. function getFolders() {
  152. $folders = false;
  153. if($this->_imap) {
  154. $imapfolders = imap_list($this->_imap, $this->_imapurl, '*');
  155. if($imapfolders) {
  156. foreach($imapfolders as $imapfolder) {
  157. $folders[] = substr($imapfolder, strlen($this->_imapurl));
  158. }
  159. }
  160. }
  161. return $folders;
  162. }
  163. /**
  164. * Fetch the email based on the messageid.
  165. * @param $messageid messageid of the email
  166. * @param $fetchbody set to false to defer fetching the body, (default: true)
  167. */
  168. function getMessage($messageid, $fetchbody=true) {
  169. return new Vtiger_MailRecord($this->_imap, $messageid, $fetchbody);
  170. }
  171. /**
  172. * Mark the message in the mailbox.
  173. */
  174. function markMessage($messageid) {
  175. $markas = $this->_scannerinfo->markas;
  176. if($this->_imap && $markas) {
  177. if(strtoupper($markas) == 'SEEN') $markas = "\\Seen";
  178. imap_setflag_full($this->_imap, $messageid, $markas);
  179. }
  180. }
  181. /**
  182. * Close the open IMAP connection.
  183. */
  184. function close() {
  185. if($this->_needExpunge) {
  186. imap_expunge($this->_imap);
  187. }
  188. $this->_needExpunge = false;
  189. if($this->_imap) {
  190. imap_close($this->_imap);
  191. $this->_imap = false;
  192. }
  193. }
  194. }
  195. ?>