/modules/mailing/classes/TBGIncomingEmailAccount.class.php

https://github.com/pb30/thebuggenie · PHP · 388 lines · 226 code · 62 blank · 100 comment · 15 complexity · 750d3a9c32da7e6ccb9eff78375357a3 MD5 · raw file

  1. <?php
  2. /**
  3. * @Table(name="TBGIncomingEmailAccountTable")
  4. */
  5. class TBGIncomingEmailAccount extends TBGIdentifiableScopedClass
  6. {
  7. const SERVER_IMAP = 0;
  8. const SERVER_POP3 = 1;
  9. /**
  10. * @Column(type="string", length=200)
  11. */
  12. protected $_name;
  13. /**
  14. * @Column(type="string", length=200)
  15. */
  16. protected $_server;
  17. /**
  18. * @Column(type="integer", length=10)
  19. */
  20. protected $_port;
  21. /**
  22. * @Column(type="string", length=200)
  23. */
  24. protected $_folder;
  25. /**
  26. * @Column(type="integer", length=10)
  27. */
  28. protected $_server_type;
  29. /**
  30. * @Column(type="boolean")
  31. */
  32. protected $_ssl;
  33. /**
  34. * @Column(type="boolean")
  35. */
  36. protected $_keep_email;
  37. /**
  38. * @Column(type="string", length=200)
  39. */
  40. protected $_username;
  41. /**
  42. * @Column(type="string", length=200)
  43. */
  44. protected $_password;
  45. protected $_connection;
  46. /**
  47. * @var TBGProject
  48. * @Column(type="integer", length=10)
  49. * @Relates(class="TBGProject")
  50. */
  51. protected $_project;
  52. /**
  53. * @var TBGIssuetype
  54. * @Column(type="integer", length=10)
  55. * @Relates(class="TBGIssuetype")
  56. */
  57. protected $_issuetype;
  58. /**
  59. * @Column(type="integer", length=10)
  60. */
  61. protected $_num_last_fetched = 0;
  62. /**
  63. * @Column(type="integer", length=10)
  64. */
  65. protected $_time_last_fetched = 0;
  66. public static function getAll()
  67. {
  68. return TBGIncomingEmailAccountTable::getTable()->getAll();
  69. $accounts = array();
  70. if ($res = TBGIncomingEmailAccountTable::getTable()->doSelectAll())
  71. {
  72. while ($row = $res->getNextRow())
  73. {
  74. $accounts[] = TBGContext::factory()->TBGIncomingEmailAccount($row->get(TBGIncomingEmailAccountTable::ID), $row);
  75. }
  76. }
  77. return $accounts;
  78. }
  79. public static function getAllByProjectID($project_id)
  80. {
  81. $accounts = array();
  82. if ($res = TBGIncomingEmailAccountTable::getTable()->getAllByProjectID($project_id))
  83. {
  84. while ($row = $res->getNextRow())
  85. {
  86. $accounts[] = TBGContext::factory()->TBGIncomingEmailAccount($row->get(TBGIncomingEmailAccountTable::ID), $row);
  87. }
  88. }
  89. return $accounts;
  90. }
  91. /**
  92. * Return the items name
  93. *
  94. * @return string
  95. */
  96. public function getName()
  97. {
  98. return $this->_name;
  99. }
  100. /**
  101. * Set the edition name
  102. *
  103. * @param string $name
  104. */
  105. public function setName($name)
  106. {
  107. $this->_name = $name;
  108. }
  109. public function getServer()
  110. {
  111. return $this->_server;
  112. }
  113. public function setServer($server)
  114. {
  115. $this->_server = $server;
  116. }
  117. public function getFoldername()
  118. {
  119. return $this->_folder;
  120. }
  121. public function setFoldername($folder)
  122. {
  123. $this->_folder = $folder;
  124. }
  125. public function getPort()
  126. {
  127. return $this->_port;
  128. }
  129. public function setPort($port)
  130. {
  131. $this->_port = $port;
  132. }
  133. public function getServerType()
  134. {
  135. return $this->_server_type;
  136. }
  137. public function setServerType($server_type)
  138. {
  139. $this->_server_type = $server_type;
  140. }
  141. public function isImap()
  142. {
  143. return (bool) $this->getServerType() == self::SERVER_IMAP;
  144. }
  145. public function isPop3()
  146. {
  147. return (bool) $this->getServerType() == self::SERVER_POP3;
  148. }
  149. public function usesSSL()
  150. {
  151. return (boolean) $this->_ssl;
  152. }
  153. public function setSSL($ssl)
  154. {
  155. $this->_ssl = $ssl;
  156. }
  157. public function doesKeepEmails()
  158. {
  159. return (boolean) $this->_keep_email;
  160. }
  161. public function setKeepEmails($keep_emails)
  162. {
  163. $this->_keep_email = $keep_emails;
  164. }
  165. public function getUsername()
  166. {
  167. return $this->_username;
  168. }
  169. public function setUsername($username)
  170. {
  171. $this->_username = $username;
  172. }
  173. public function getPassword()
  174. {
  175. return $this->_password;
  176. }
  177. public function setPassword($password)
  178. {
  179. $this->_password = $password;
  180. }
  181. public function setProject($project)
  182. {
  183. $this->_project = $project;
  184. }
  185. public function setIssuetype($issuetype)
  186. {
  187. $this->_issuetype = $issuetype;
  188. }
  189. public function getNumberOfEmailsLastFetched()
  190. {
  191. return $this->_num_last_fetched;
  192. }
  193. public function setNumberOfEmailsLastFetched($num_last_fetched)
  194. {
  195. $this->_num_last_fetched = $num_last_fetched;
  196. }
  197. public function getTimeLastFetched()
  198. {
  199. return $this->_time_last_fetched;
  200. }
  201. public function setTimeLastFetched($time_last_fetched)
  202. {
  203. $this->_time_last_fetched = $time_last_fetched;
  204. }
  205. /**
  206. * Retrieve the imap connection string for this account
  207. *
  208. * @return string
  209. */
  210. public function getConnectionString()
  211. {
  212. $conn_string = "{".$this->getServer().":".$this->getPort()."/";
  213. $conn_string .= ($this->getServerType() == self::SERVER_IMAP) ? "imap" : "pop3";
  214. if ($this->usesSSL()) $conn_string .= "/ssl";
  215. $conn_string .= "}";
  216. $conn_string .= ($this->getFoldername() == '') ? "INBOX" : $this->getFoldername();
  217. return $conn_string;
  218. }
  219. /**
  220. * Create an imap connection for this account
  221. */
  222. public function connect()
  223. {
  224. if ($this->_connection === null)
  225. {
  226. $this->_connection = imap_open($this->getConnectionString(), $this->getUsername(), $this->getPassword());
  227. }
  228. if (!is_resource($this->_connection))
  229. {
  230. $error = imap_last_error();
  231. $error = ($error === false) ? TBGContext::getI18n()->__('No error message provided') : $error;
  232. throw new Exception(TBGContext::getI18n()->__('Could not connect to the specified email server(%connection_string%): %error_message%', array('%connection_string%' => $this->getConnectionString(), '%error_message%' => $error)));
  233. }
  234. }
  235. /**
  236. * Disconnects this account from the imap resource *
  237. */
  238. public function disconnect()
  239. {
  240. if(!$this->doesKeepEmails())
  241. {
  242. imap_expunge($this->connection);
  243. }
  244. imap_close($this->_connection);
  245. $this->_connection = null;
  246. }
  247. /**
  248. * Returns the imap connection resource
  249. *
  250. * @return resource
  251. */
  252. public function getConnection()
  253. {
  254. return $this->_connection;
  255. }
  256. /**
  257. * Returns imap overview objects for all unread emails on this account
  258. *
  259. * @return array
  260. */
  261. public function getUnprocessedEmails()
  262. {
  263. $this->connect();
  264. $uids = imap_search($this->_connection, 'UNSEEN');
  265. if ($uids)
  266. {
  267. return imap_fetch_overview($this->_connection, join(",", $uids), 0);
  268. }
  269. else
  270. {
  271. return array();
  272. }
  273. }
  274. /**
  275. * Takes an email object and looks up details for this particular email
  276. * Returns the primary body and the mime type
  277. * Sets the message for deletion when chosen to do not keep emails
  278. *
  279. * @param stdObject $email
  280. * @return TBGIncomingEmailMessage the message
  281. */
  282. public function getMessage($email)
  283. {
  284. $message = new TBGIncomingEmailMessage($this->_connection, $email->msgno);
  285. $is_structure = $message->fetch();
  286. if($is_structure && !$this->doesKeepEmails())
  287. {
  288. imap_delete($this->_connection, $email->msgno);
  289. }
  290. return $message;
  291. }
  292. /**
  293. * Returns number of unread emails on this account
  294. *
  295. * @return integer
  296. */
  297. public function getUnreadCount()
  298. {
  299. $this->connect();
  300. $result = imap_search($this->_connection, "UNSEEN");
  301. return ($result !== false) ? count($result) : 0;
  302. }
  303. /**
  304. * Returns the project associated with this account
  305. *
  306. * @return TBGProject
  307. */
  308. public function getProject()
  309. {
  310. return $this->_b2dbLazyload('_project');
  311. }
  312. /**
  313. * Returns the issuetype associated with this account
  314. *
  315. * @return TBGIssuetype
  316. */
  317. public function getIssuetype()
  318. {
  319. return $this->_b2dbLazyload('_issuetype');
  320. }
  321. public function getIssuetypeID()
  322. {
  323. $issuetype = $this->getIssuetype();
  324. return ($issuetype instanceof TBGIssuetype) ? $issuetype->getID() : null;
  325. }
  326. }